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"
30 static char root[PATH_SIZE] = "/dev";
31 static char **devices;
34 void log_message(int priority, const char *format, ...)
37 static int udev_log = -1;
42 value = getenv("UDEV_LOG");
44 udev_log = log_priority(value);
48 if (debug && udev_log < LOG_INFO)
52 if (priority > udev_log)
55 va_start(args, format);
57 fprintf(stderr, "[%d] ", (int) getpid());
58 vfprintf(stderr, format, args);
60 vsyslog(priority, format, args);
65 static int matches_device_list(const char *name)
69 for (i = 0; devices[i] != NULL; i++) {
70 info("compare '%s' == '%s'\n", name, devices[i]);
71 if (strcmp(devices[i], name) == 0)
77 static void print_fstab_entry(struct mntent *mnt)
79 printf("FSTAB_NAME=%s\n", mnt->mnt_fsname);
80 printf("FSTAB_DIR=%s\n", mnt->mnt_dir);
81 printf("FSTAB_TYPE=%s\n", mnt->mnt_type);
82 printf("FSTAB_OPTS=%s\n", mnt->mnt_opts);
83 printf("FSTAB_FREQ=%d\n", mnt->mnt_freq);
84 printf("FSTAB_PASSNO=%d\n", mnt->mnt_passno);
87 int main(int argc, char *argv[])
89 static const struct option options[] = {
90 { "export", 0, NULL, 'x' },
91 { "root", 1, NULL, 'r' },
92 { "debug", 0, NULL, 'd' },
93 { "help", 0, NULL, 'h' },
101 logging_init("fstab_id");
106 option = getopt_long(argc, argv, "dr:xh", options, NULL);
112 strlcpy(root, optarg, sizeof(root));
118 printf("Usage: fstab_id [OPTIONS] name [...]\n"
119 " --export print environment keys\n"
120 " --root device node root (default /dev)\n"
121 " --debug debug to stderr\n"
122 " --help print this help text\n\n");
129 devices = &argv[optind];
130 if (devices[0] == NULL) {
131 fprintf(stderr, "error: missing device(s) to match\n");
136 fp = setmntent ("/etc/fstab", "r");
138 fprintf(stderr, "error: opening fstab: %s\n", strerror(errno));
148 /* skip root device */
149 if (strcmp(mnt->mnt_dir, "/") == 0)
153 if (strncmp(mnt->mnt_fsname, "LABEL=", 6) == 0) {
157 label = &mnt->mnt_fsname[6];
158 if (label[0] == '"' || label[0] == '\'') {
161 strlcpy(str, &label[1], sizeof(str));
162 pos = strrchr(str, label[0]);
168 if (matches_device_list(str)) {
169 print_fstab_entry(mnt);
176 if (strncmp(mnt->mnt_fsname, "UUID=", 5) == 0) {
180 uuid = &mnt->mnt_fsname[5];
181 if (uuid[0] == '"' || uuid[0] == '\'') {
184 strlcpy(str, &uuid[1], sizeof(str));
185 pos = strrchr(str, uuid[0]);
191 if (matches_device_list(str)) {
192 print_fstab_entry(mnt);
199 if (strncmp(mnt->mnt_fsname, root, strlen(root)) != 0)
202 if (matches_device_list(&mnt->mnt_fsname[strlen(root)+1])) {
203 print_fstab_entry(mnt);