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.
27 #include "../../udev/udev.h"
31 static void log_fn(struct udev *udev, int priority,
32 const char *file, int line, const char *fn,
33 const char *format, va_list args)
36 fprintf(stderr, "%s: ", fn);
37 vfprintf(stderr, format, args);
39 vsyslog(priority, format, args);
43 static int matches_device_list(struct udev *udev, char **devices, const char *name)
47 for (i = 0; devices[i] != NULL; i++) {
48 info(udev, "compare '%s' == '%s'\n", name, devices[i]);
49 if (strcmp(devices[i], name) == 0)
55 static void print_fstab_entry(struct udev *udev, struct mntent *mnt)
57 printf("FSTAB_NAME=%s\n", mnt->mnt_fsname);
58 printf("FSTAB_DIR=%s\n", mnt->mnt_dir);
59 printf("FSTAB_TYPE=%s\n", mnt->mnt_type);
60 printf("FSTAB_OPTS=%s\n", mnt->mnt_opts);
61 printf("FSTAB_FREQ=%d\n", mnt->mnt_freq);
62 printf("FSTAB_PASSNO=%d\n", mnt->mnt_passno);
65 int main(int argc, char *argv[])
68 static const struct option options[] = {
69 { "export", no_argument, NULL, 'x' },
70 { "debug", no_argument, NULL, 'd' },
71 { "help", no_argument, NULL, 'h' },
83 logging_init("fstab_id");
84 udev_set_log_fn(udev, log_fn);
89 option = getopt_long(argc, argv, "dxh", options, NULL);
96 if (udev_get_log_priority(udev) < LOG_INFO)
97 udev_set_log_priority(udev, LOG_INFO);
100 printf("Usage: fstab_id [OPTIONS] name [...]\n"
101 " --export print environment keys\n"
102 " --debug debug to stderr\n"
103 " --help print this help text\n\n");
113 devices = &argv[optind];
114 if (devices[0] == NULL) {
115 fprintf(stderr, "error: missing device(s) to match\n");
120 fp = setmntent ("/etc/fstab", "r");
122 fprintf(stderr, "error: opening fstab: %s\n", strerror(errno));
132 info(udev, "found '%s'@'%s'\n", mnt->mnt_fsname, mnt->mnt_dir);
134 /* skip root device */
135 if (strcmp(mnt->mnt_dir, "/") == 0)
139 if (strncmp(mnt->mnt_fsname, "LABEL=", 6) == 0) {
143 label = &mnt->mnt_fsname[6];
144 if (label[0] == '"' || label[0] == '\'') {
147 util_strlcpy(str, &label[1], sizeof(str));
148 pos = strrchr(str, label[0]);
154 if (matches_device_list(udev, devices, str)) {
155 print_fstab_entry(udev, mnt);
163 if (strncmp(mnt->mnt_fsname, "UUID=", 5) == 0) {
167 uuid = &mnt->mnt_fsname[5];
168 if (uuid[0] == '"' || uuid[0] == '\'') {
171 util_strlcpy(str, &uuid[1], sizeof(str));
172 pos = strrchr(str, uuid[0]);
178 if (matches_device_list(udev, devices, str)) {
179 print_fstab_entry(udev, mnt);
187 if (strncmp(mnt->mnt_fsname, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) != 0)
190 if (matches_device_list(udev, devices, &mnt->mnt_fsname[strlen(udev_get_dev_path(udev))+1])) {
191 print_fstab_entry(udev, mnt);