chiark / gitweb /
treewide: use log_*_errno whenever %m is in the format string
[elogind.git] / src / udev / udev-watch.c
index 228d18fedf4935707abaa20f11c558004f2dfca7..6ba8674d771f4b6820c479c386e3e7742209327e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
  * Copyright (C) 2009 Canonical Ltd.
  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
  *
@@ -36,58 +36,48 @@ static int inotify_fd = -1;
  * set to cloexec since we need our children to be able to add
  * watches for us
  */
-int udev_watch_init(struct udev *udev)
-{
+int udev_watch_init(struct udev *udev) {
         inotify_fd = inotify_init1(IN_CLOEXEC);
         if (inotify_fd < 0)
-                err(udev, "inotify_init failed: %m\n");
+                log_error_errno(errno, "inotify_init failed: %m");
         return inotify_fd;
 }
 
 /* move any old watches directory out of the way, and then restore
  * the watches
  */
-void udev_watch_restore(struct udev *udev)
-{
-        char filename[UTIL_PATH_SIZE], oldname[UTIL_PATH_SIZE];
-
+void udev_watch_restore(struct udev *udev) {
         if (inotify_fd < 0)
                 return;
 
-        util_strscpyl(oldname, sizeof(oldname), udev_get_run_path(udev), "/watch.old", NULL);
-        util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/watch", NULL);
-        if (rename(filename, oldname) == 0) {
+        if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
                 DIR *dir;
                 struct dirent *ent;
 
-                dir = opendir(oldname);
+                dir = opendir("/run/udev/watch.old");
                 if (dir == NULL) {
-                        err(udev, "unable to open old watches dir '%s', old watches will not be restored: %m", oldname);
+                        log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
                         return;
                 }
 
                 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
                         char device[UTIL_PATH_SIZE];
-                        char *s;
-                        size_t l;
                         ssize_t len;
                         struct udev_device *dev;
 
                         if (ent->d_name[0] == '.')
                                 continue;
 
-                        s = device;
-                        l = util_strpcpy(&s, sizeof(device), udev_get_sys_path(udev));
-                        len = readlinkat(dirfd(dir), ent->d_name, s, l);
-                        if (len <= 0 || len == (ssize_t)l)
+                        len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
+                        if (len <= 0 || len == (ssize_t)sizeof(device))
                                 goto unlink;
-                        s[len] = '\0';
+                        device[len] = '\0';
 
-                        dev = udev_device_new_from_id_filename(udev, s);
+                        dev = udev_device_new_from_device_id(udev, device);
                         if (dev == NULL)
                                 goto unlink;
 
-                        info(udev, "restoring old watch on '%s'\n", udev_device_get_devnode(dev));
+                        log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev));
                         udev_watch_begin(udev, dev);
                         udev_device_unref(dev);
 unlink:
@@ -95,39 +85,40 @@ unlink:
                 }
 
                 closedir(dir);
-                rmdir(oldname);
+                rmdir("/run/udev/watch.old");
 
         } else if (errno != ENOENT) {
-                err(udev, "unable to move watches dir '%s', old watches will not be restored: %m", filename);
+                log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
         }
 }
 
-void udev_watch_begin(struct udev *udev, struct udev_device *dev)
-{
+void udev_watch_begin(struct udev *udev, struct udev_device *dev) {
         char filename[UTIL_PATH_SIZE];
         int wd;
+        int r;
 
         if (inotify_fd < 0)
                 return;
 
-        info(udev, "adding watch on '%s'\n", udev_device_get_devnode(dev));
+        log_debug("adding watch on '%s'", udev_device_get_devnode(dev));
         wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
         if (wd < 0) {
-                err(udev, "inotify_add_watch(%d, %s, %o) failed: %m\n",
+                log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m",
                     inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
                 return;
         }
 
-        snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd);
-        util_create_path(udev, filename);
+        snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
+        mkdir_parents(filename, 0755);
         unlink(filename);
-        symlink(udev_device_get_id_filename(dev), filename);
+        r = symlink(udev_device_get_id_filename(dev), filename);
+        if (r < 0)
+                log_error_errno(errno, "Failed to create symlink %s: %m", filename);
 
         udev_device_set_watch_handle(dev, wd);
 }
 
-void udev_watch_end(struct udev *udev, struct udev_device *dev)
-{
+void udev_watch_end(struct udev *udev, struct udev_device *dev) {
         int wd;
         char filename[UTIL_PATH_SIZE];
 
@@ -138,33 +129,28 @@ void udev_watch_end(struct udev *udev, struct udev_device *dev)
         if (wd < 0)
                 return;
 
-        info(udev, "removing watch on '%s'\n", udev_device_get_devnode(dev));
+        log_debug("removing watch on '%s'", udev_device_get_devnode(dev));
         inotify_rm_watch(inotify_fd, wd);
 
-        snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd);
+        snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
         unlink(filename);
 
         udev_device_set_watch_handle(dev, -1);
 }
 
-struct udev_device *udev_watch_lookup(struct udev *udev, int wd)
-{
+struct udev_device *udev_watch_lookup(struct udev *udev, int wd) {
         char filename[UTIL_PATH_SIZE];
-        char majmin[UTIL_PATH_SIZE];
-        char *s;
-        size_t l;
+        char device[UTIL_NAME_SIZE];
         ssize_t len;
 
         if (inotify_fd < 0 || wd < 0)
                 return NULL;
 
-        snprintf(filename, sizeof(filename), "%s/watch/%d", udev_get_run_path(udev), wd);
-        s = majmin;
-        l = util_strpcpy(&s, sizeof(majmin), udev_get_sys_path(udev));
-        len = readlink(filename, s, l);
-        if (len <= 0 || (size_t)len == l)
+        snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
+        len = readlink(filename, device, sizeof(device));
+        if (len <= 0 || (size_t)len == sizeof(device))
                 return NULL;
-        s[len] = '\0';
+        device[len] = '\0';
 
-        return udev_device_new_from_id_filename(udev, s);
+        return udev_device_new_from_device_id(udev, device);
 }