Commits

braindamaged  committed b9bdb0d

udev: avoid creating /dev/kmsg with fopen() to support CONFIG_PRINTK=n

  • Participants
  • Parent commits ed8deac

Comments (0)

Files changed (4)

File src/libudev-private.h

                                       char *result, size_t maxsize, int read_value);
 unsigned long long ts_usec(const struct timespec *ts);
 unsigned long long now_usec(void);
+ssize_t print_kmsg(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
 
 /* libudev-selinux-private.c */
 #ifndef WITH_SELINUX

File src/libudev-util.c

                 return 0;
         return ts_usec(&ts);
 }
+
+ssize_t print_kmsg(const char *fmt, ...)
+{
+        int fd;
+        va_list ap;
+        char text[1024];
+        ssize_t len;
+        ssize_t ret;
+
+        fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        len = snprintf(text, sizeof(text), "<30>udevd[%u]: ", getpid());
+
+        va_start(ap, fmt);
+        len += vsnprintf(text + len, sizeof(text) - len, fmt, ap);
+        va_end(ap);
+
+        ret = write(fd, text, len);
+        if (ret < 0)
+                ret = -errno;
+        close(fd);
+        return ret;
+}

File src/udev-event.c

         return err;
 }
 
-static void rename_netif_kernel_log(struct ifreq ifr)
+static inline 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);
+        print_kmsg("renamed network interface %s to %s", ifr.ifr_name, ifr.ifr_newname);
 }
 
 static int rename_netif(struct udev_event *event)
         if (access(filename, F_OK) < 0)
                 return 0;
 
-        f = fopen("/dev/kmsg", "w");
-        if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: converting old udev database\n", getpid());
-                fclose(f);
-        }
+        print_kmsg("converting old udev database\n");
 
         udev_enumerate = udev_enumerate_new(udev);
         if (udev_enumerate == NULL)
                 }
         }
 
-        f = fopen("/dev/kmsg", "w");
-        if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: starting version " VERSION "\n", getpid());
-                fclose(f);
-        }
+        print_kmsg("starting version" VERSION "\n");
 
         if (!debug) {
                 int fd;