chiark / gitweb /
udevd: add --verbose option to log also to stdout
[elogind.git] / udevd.c
diff --git a/udevd.c b/udevd.c
index bb956b520c2866d26be448b0eade558738d61788..eb632bd85242d32a29e772efcb30da7349c02a87 100644 (file)
--- a/udevd.c
+++ b/udevd.c
@@ -29,6 +29,7 @@
 #include <fcntl.h>
 #include <syslog.h>
 #include <time.h>
+#include <getopt.h>
 #include <sys/select.h>
 #include <sys/wait.h>
 #include <sys/types.h>
@@ -44,6 +45,8 @@
 #include "udevd.h"
 #include "udev_selinux.h"
 
+static int verbose;
+
 static struct udev_rules rules;
 static int udevd_sock = -1;
 static int uevent_netlink_sock = -1;
@@ -75,7 +78,15 @@ void log_message(int priority, const char *format, ...)
        va_start(args, format);
        vsyslog(priority, format, args);
        va_end(args);
+
+       if (verbose) {
+               va_start(args, format);
+               vprintf(format, args);
+               va_end(args);
+               printf("\n");
+       }
 }
+
 #endif
 
 static void asmlinkage udev_event_sig_handler(int signum)
@@ -155,9 +166,9 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st
 {
        char filename[PATH_SIZE];
        char filename_failed[PATH_SIZE];
-       char target[PATH_SIZE];
        size_t start, end, i;
        struct udevd_uevent_msg *loop_msg;
+       int fd;
 
        /* add location of queue files */
        strlcpy(filename, udev_root, sizeof(filename));
@@ -189,11 +200,10 @@ static void export_event_state(struct udevd_uevent_msg *msg, enum event_state st
        case EVENT_QUEUED:
                unlink(filename_failed);
                delete_path(filename_failed);
-
-               strlcpy(target, sysfs_path, sizeof(target));
-               strlcat(target, msg->devpath, sizeof(target));
                create_path(filename);
-               symlink(target, filename);
+               fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
+               if (fd > 0)
+                       close(fd);
                return;
        case EVENT_FINISHED:
        case EVENT_FAILED:
@@ -917,7 +927,13 @@ int main(int argc, char *argv[], char *envp[])
        fd_set readfds;
        const char *value;
        int daemonize = 0;
-       int i;
+       int option;
+       static const struct option options[] = {
+               { "daemon", 0, NULL, 'd' },
+               { "verbose", 0, NULL, 'v' },
+               { "help", 0, NULL, 'h' },
+               {}
+       };
        int rc = 1;
        int maxfd;
 
@@ -926,26 +942,35 @@ int main(int argc, char *argv[], char *envp[])
        selinux_init();
        dbg("version %s", UDEV_VERSION);
 
-       if (getuid() != 0) {
-               fprintf(stderr, "root privileges required\n");
-               err("root privileges required");
-               goto exit;
-       }
-
        /* parse commandline options */
-       for (i = 1 ; i < argc; i++) {
-               char *arg = argv[i];
-               if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0)
+       while (1) {
+               option = getopt_long(argc, argv, "dtvh", options, NULL);
+               if (option == -1)
+                       break;
+
+               switch (option) {
+               case 'd':
                        daemonize = 1;
-               else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
-                       printf("Usage: udevd [--help] [--daemon]\n");
+                       break;
+               case 'v':
+                       verbose = 1;
+                       if (udev_log_priority < LOG_INFO)
+                               udev_log_priority = LOG_INFO;
+                       break;
+               case 'h':
+                       printf("Usage: udevd [--help] [--daemon] [--verbose]\n");
+                       goto exit;
+               default:
                        goto exit;
-               } else {
-                       fprintf(stderr, "unrecognized option '%s'\n", arg);
-                       err("unrecognized option '%s'\n", arg);
                }
        }
 
+       if (getuid() != 0) {
+               fprintf(stderr, "root privileges required\n");
+               err("root privileges required");
+               goto exit;
+       }
+
        /* init sockets to receive events */
        if (init_udevd_socket() < 0) {
                if (errno == EADDRINUSE) {
@@ -996,7 +1021,8 @@ int main(int argc, char *argv[], char *envp[])
        fd = open("/dev/null", O_RDWR);
        if (fd >= 0) {
                dup2(fd, STDIN_FILENO);
-               dup2(fd, STDOUT_FILENO);
+               if (!verbose)
+                       dup2(fd, STDOUT_FILENO);
                dup2(fd, STDERR_FILENO);
                if (fd > STDERR_FILENO)
                        close(fd);