chiark / gitweb /
udev: add missing newline when writing to /dev/kmsg
[elogind.git] / src / udev / udev-event.c
index 2b4cc69..2b9fdf6 100644 (file)
@@ -86,8 +86,8 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char *
                 SUBST_SYS,
         };
         static const struct subst_map {
-                char *name;
-                char fmt;
+                const char *name;
+                const char fmt;
                 enum subst_type type;
         } map[] = {
                 { .name = "devnode",        .fmt = 'N',        .type = SUBST_DEVNODE },
@@ -132,8 +132,8 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char *
                                         goto copy;
                                 }
 
-                                for (i = 0; i < ARRAY_SIZE(map); i++) {
-                                        if (strncmp(&from[1], map[i].name, strlen(map[i].name)) == 0) {
+                                for (i = 0; i < ELEMENTSOF(map); i++) {
+                                        if (startswith(&from[1], map[i].name)) {
                                                 type = map[i].type;
                                                 from += strlen(map[i].name)+1;
                                                 goto subst;
@@ -148,7 +148,7 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char *
                                         goto copy;
                                 }
 
-                                for (i = 0; i < ARRAY_SIZE(map); i++) {
+                                for (i = 0; i < ELEMENTSOF(map); i++) {
                                         if (from[1] == map[i].fmt) {
                                                 type = map[i].type;
                                                 from += 2;
@@ -318,46 +318,38 @@ subst:
                         if (dev_parent == NULL)
                                 break;
                         devnode = udev_device_get_devnode(dev_parent);
-                        if (devnode != NULL) {
-                                size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
-
-                                l = util_strpcpy(&s, l, &devnode[devlen]);
-                        }
+                        if (devnode != NULL)
+                                l = util_strpcpy(&s, l, devnode + strlen("/dev/"));
                         break;
                 }
                 case SUBST_DEVNODE:
                         if (udev_device_get_devnode(dev) != NULL)
                                 l = util_strpcpy(&s, l, udev_device_get_devnode(dev));
                         break;
-                case SUBST_NAME: {
-                        if (event->name != NULL) {
+                case SUBST_NAME:
+                        if (event->name != NULL)
                                 l = util_strpcpy(&s, l, event->name);
-                        } else if (udev_device_get_devnode(dev) != NULL) {
-                                size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
-
-                                l = util_strpcpy(&s, l, &udev_device_get_devnode(dev)[devlen]);
-                        } else {
+                        else if (udev_device_get_devnode(dev) != NULL)
+                                l = util_strpcpy(&s, l, udev_device_get_devnode(dev) + strlen("/dev/"));
+                        else
                                 l = util_strpcpy(&s, l, udev_device_get_sysname(dev));
-                        }
                         break;
-                }
                 case SUBST_LINKS: {
-                        size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
                         struct udev_list_entry *list_entry;
 
                         list_entry = udev_device_get_devlinks_list_entry(dev);
                         if (list_entry == NULL)
                                 break;
-                        l = util_strpcpy(&s, l, &udev_list_entry_get_name(list_entry)[devlen]);
+                        l = util_strpcpy(&s, l, udev_list_entry_get_name(list_entry) + strlen("/dev/"));
                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
-                                l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
+                                l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry) + strlen("/dev/"), NULL);
                         break;
                 }
                 case SUBST_ROOT:
-                        l = util_strpcpy(&s, l, udev_get_dev_path(event->udev));
+                        l = util_strpcpy(&s, l, "/dev");
                         break;
                 case SUBST_SYS:
-                        l = util_strpcpy(&s, l, udev_get_sys_path(event->udev));
+                        l = util_strpcpy(&s, l, "/sys");
                         break;
                 case SUBST_ENV:
                         if (attr == NULL) {
@@ -386,7 +378,6 @@ static int spawn_exec(struct udev_event *event,
                       const char *cmd, char *const argv[], char **envp, const sigset_t *sigmask,
                       int fd_stdout, int fd_stderr)
 {
-        struct udev *udev = event->udev;
         int err;
         int fd;
 
@@ -433,7 +424,6 @@ static void spawn_read(struct udev_event *event,
                       int fd_stdout, int fd_stderr,
                       char *result, size_t ressize)
 {
-        struct udev *udev = event->udev;
         size_t respos = 0;
         int fd_ep = -1;
         struct epoll_event ep_outpipe, ep_errpipe;
@@ -488,7 +478,7 @@ static void spawn_read(struct udev_event *event,
                         timeout = -1;
                 }
 
-                fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), timeout);
+                fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
                 if (fdcount < 0) {
                         if (errno == EINTR)
                                 continue;
@@ -553,7 +543,6 @@ out:
 
 static int spawn_wait(struct udev_event *event, const char *cmd, pid_t pid)
 {
-        struct udev *udev = event->udev;
         struct pollfd pfd[1];
         int err = 0;
 
@@ -718,8 +707,8 @@ int udev_event_spawn(struct udev_event *event,
 
                 log_debug("starting '%s'\n", cmd);
 
-                err = spawn_exec(event, cmd, argv, envp, sigmask,
-                                 outpipe[WRITE_END], errpipe[WRITE_END]);
+                spawn_exec(event, cmd, argv, envp, sigmask,
+                           outpipe[WRITE_END], errpipe[WRITE_END]);
 
                 _exit(2 );
         case -1:
@@ -756,32 +745,11 @@ out:
         return err;
 }
 
-static void rename_netif_kernel_log(struct ifreq ifr)
-{
-        int klog;
-        FILE *f;
-
-        klog = open("/dev/kmsg", O_WRONLY);
-        if (klog < 0)
-                return;
-
-        f = fdopen(klog, "w");
-        if (f == NULL) {
-                close(klog);
-                return;
-        }
-
-        fprintf(f, "<30>udevd[%u]: renamed network interface %s to %s\n",
-                getpid(), ifr.ifr_name, ifr.ifr_newname);
-        fclose(f);
-}
-
 static int rename_netif(struct udev_event *event)
 {
         struct udev_device *dev = event->dev;
         int sk;
         struct ifreq ifr;
-        int loop;
         int err;
 
         log_debug("changing net interface name from '%s' to '%s'\n",
@@ -798,49 +766,12 @@ static int rename_netif(struct udev_event *event)
         util_strscpy(ifr.ifr_name, IFNAMSIZ, udev_device_get_sysname(dev));
         util_strscpy(ifr.ifr_newname, IFNAMSIZ, event->name);
         err = ioctl(sk, SIOCSIFNAME, &ifr);
-        if (err == 0) {
-                rename_netif_kernel_log(ifr);
-                goto out;
-        }
-
-        /* keep trying if the destination interface name already exists */
-        err = -errno;
-        if (err != -EEXIST)
-                goto out;
-
-        /* free our own name, another process may wait for us */
-        snprintf(ifr.ifr_newname, IFNAMSIZ, "rename%u", udev_device_get_ifindex(dev));
-        err = ioctl(sk, SIOCSIFNAME, &ifr);
-        if (err < 0) {
-                err = -errno;
-                goto out;
-        }
-
-        /* log temporary name */
-        rename_netif_kernel_log(ifr);
-
-        /* wait a maximum of 90 seconds for our target to become available */
-        util_strscpy(ifr.ifr_name, IFNAMSIZ, ifr.ifr_newname);
-        util_strscpy(ifr.ifr_newname, IFNAMSIZ, event->name);
-        loop = 90 * 20;
-        while (loop--) {
-                const struct timespec duration = { 0, 1000 * 1000 * 1000 / 20 };
-
-                nanosleep(&duration, NULL);
-
-                err = ioctl(sk, SIOCSIFNAME, &ifr);
-                if (err == 0) {
-                        rename_netif_kernel_log(ifr);
-                        break;
-                }
+        if (err >= 0) {
+                print_kmsg("renamed network interface %s to %s\n", ifr.ifr_name, ifr.ifr_newname);
+        } else {
                 err = -errno;
-                if (err != -EEXIST)
-                        break;
-        }
-
-out:
-        if (err < 0)
                 log_error("error changing net interface name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname);
+        }
         close(sk);
         return err;
 }
@@ -939,7 +870,6 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules,
                 udev_device_unref(event->dev_db);
                 event->dev_db = NULL;
         }
-out:
         return err;
 }
 
@@ -956,14 +886,6 @@ void udev_event_execute_run(struct udev_event *event, const sigset_t *sigmask)
 
                         udev_event_apply_format(event, cmd, command, sizeof(command));
                         udev_builtin_run(event->dev, builtin_cmd, command, false);
-                } else if (strncmp(cmd, "socket:", strlen("socket:")) == 0) {
-                        struct udev_monitor *monitor;
-
-                        monitor = udev_monitor_new_from_socket(event->udev, &cmd[strlen("socket:")]);
-                        if (monitor == NULL)
-                                continue;
-                        udev_monitor_send_device(monitor, NULL, event->dev);
-                        udev_monitor_unref(monitor);
                 } else {
                         char program[UTIL_PATH_SIZE];
                         char **envp;