2 * find matching entry in fstab and export it
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
29 #include "../../udev/udev.h"
33 static void log_fn(struct udev *udev, int priority,
34 const char *file, int line, const char *fn,
35 const char *format, va_list args)
38 fprintf(stderr, "%s: ", fn);
39 vfprintf(stderr, format, args);
41 vsyslog(priority, format, args);
45 static int matches_device_list(struct udev *udev, char **devices, const char *name)
49 for (i = 0; devices[i] != NULL; i++) {
50 info(udev, "compare '%s' == '%s'\n", name, devices[i]);
51 if (strcmp(devices[i], name) == 0)
57 static void print_fstab_entry(struct udev *udev, struct mntent *mnt)
59 printf("FSTAB_NAME=%s\n", mnt->mnt_fsname);
60 printf("FSTAB_DIR=%s\n", mnt->mnt_dir);
61 printf("FSTAB_TYPE=%s\n", mnt->mnt_type);
62 printf("FSTAB_OPTS=%s\n", mnt->mnt_opts);
63 printf("FSTAB_FREQ=%d\n", mnt->mnt_freq);
64 printf("FSTAB_PASSNO=%d\n", mnt->mnt_passno);
67 int main(int argc, char *argv[])
70 static const struct option options[] = {
71 { "export", 0, NULL, 'x' },
72 { "debug", 0, NULL, 'd' },
73 { "help", 0, NULL, 'h' },
85 logging_init("fstab_id");
86 udev_set_log_fn(udev, log_fn);
91 option = getopt_long(argc, argv, "dxh", options, NULL);
98 if (udev_get_log_priority(udev) < LOG_INFO)
99 udev_set_log_priority(udev, LOG_INFO);
102 printf("Usage: fstab_id [OPTIONS] name [...]\n"
103 " --export print environment keys\n"
104 " --debug debug to stderr\n"
105 " --help print this help text\n\n");
115 devices = &argv[optind];
116 if (devices[0] == NULL) {
117 fprintf(stderr, "error: missing device(s) to match\n");
122 fp = setmntent ("/etc/fstab", "r");
124 fprintf(stderr, "error: opening fstab: %s\n", strerror(errno));
134 info(udev, "found '%s'@'%s'\n", mnt->mnt_fsname, mnt->mnt_dir);
136 /* skip root device */
137 if (strcmp(mnt->mnt_dir, "/") == 0)
141 if (strncmp(mnt->mnt_fsname, "LABEL=", 6) == 0) {
145 label = &mnt->mnt_fsname[6];
146 if (label[0] == '"' || label[0] == '\'') {
149 util_strlcpy(str, &label[1], sizeof(str));
150 pos = strrchr(str, label[0]);
156 if (matches_device_list(udev, devices, str)) {
157 print_fstab_entry(udev, mnt);
165 if (strncmp(mnt->mnt_fsname, "UUID=", 5) == 0) {
169 uuid = &mnt->mnt_fsname[5];
170 if (uuid[0] == '"' || uuid[0] == '\'') {
173 util_strlcpy(str, &uuid[1], sizeof(str));
174 pos = strrchr(str, uuid[0]);
180 if (matches_device_list(udev, devices, str)) {
181 print_fstab_entry(udev, mnt);
189 if (strncmp(mnt->mnt_fsname, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) != 0)
192 if (matches_device_list(udev, devices, &mnt->mnt_fsname[strlen(udev_get_dev_path(udev))+1])) {
193 print_fstab_entry(udev, mnt);