]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-watch.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / udev / udev-watch.c
CommitLineData
bd284db1 1/*
1298001e 2 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
bd284db1 3 * Copyright (C) 2009 Canonical Ltd.
64746532 4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
bd284db1
SJR
5 *
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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <sys/types.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdio.h>
24#include <dirent.h>
25#include <stddef.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
bd284db1 29#include <sys/inotify.h>
bd284db1
SJR
30
31#include "udev.h"
32
1e03b754 33static int inotify_fd = -1;
bd284db1
SJR
34
35/* inotify descriptor, will be shared with rules directory;
36 * set to cloexec since we need our children to be able to add
37 * watches for us
38 */
9ec6e95b 39int udev_watch_init(struct udev *udev) {
912541b0
KS
40 inotify_fd = inotify_init1(IN_CLOEXEC);
41 if (inotify_fd < 0)
56f64d95 42 log_error_errno(errno, "inotify_init failed: %m");
912541b0 43 return inotify_fd;
bd284db1
SJR
44}
45
46/* move any old watches directory out of the way, and then restore
47 * the watches
48 */
9ec6e95b 49void udev_watch_restore(struct udev *udev) {
912541b0
KS
50 if (inotify_fd < 0)
51 return;
52
6ada823a 53 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
912541b0
KS
54 DIR *dir;
55 struct dirent *ent;
56
6ada823a 57 dir = opendir("/run/udev/watch.old");
912541b0 58 if (dir == NULL) {
56f64d95 59 log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
912541b0
KS
60 return;
61 }
62
63 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
64 char device[UTIL_PATH_SIZE];
912541b0
KS
65 ssize_t len;
66 struct udev_device *dev;
67
68 if (ent->d_name[0] == '.')
69 continue;
70
6ada823a
KS
71 len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
72 if (len <= 0 || len == (ssize_t)sizeof(device))
912541b0 73 goto unlink;
6ada823a 74 device[len] = '\0';
912541b0 75
dbf61afb 76 dev = udev_device_new_from_device_id(udev, device);
912541b0
KS
77 if (dev == NULL)
78 goto unlink;
79
9f6445e3 80 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev));
912541b0
KS
81 udev_watch_begin(udev, dev);
82 udev_device_unref(dev);
777239cb 83unlink:
912541b0
KS
84 unlinkat(dirfd(dir), ent->d_name, 0);
85 }
bd284db1 86
912541b0 87 closedir(dir);
6ada823a 88 rmdir("/run/udev/watch.old");
bd284db1 89
912541b0 90 } else if (errno != ENOENT) {
56f64d95 91 log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
912541b0 92 }
bd284db1
SJR
93}
94
9ec6e95b 95void udev_watch_begin(struct udev *udev, struct udev_device *dev) {
912541b0
KS
96 char filename[UTIL_PATH_SIZE];
97 int wd;
6bb2f0a0 98 int r;
912541b0
KS
99
100 if (inotify_fd < 0)
101 return;
102
9f6445e3 103 log_debug("adding watch on '%s'", udev_device_get_devnode(dev));
912541b0
KS
104 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
105 if (wd < 0) {
56f64d95 106 log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m",
912541b0
KS
107 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
108 return;
109 }
110
6ada823a 111 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
667e3924 112 mkdir_parents(filename, 0755);
912541b0 113 unlink(filename);
6bb2f0a0
VP
114 r = symlink(udev_device_get_id_filename(dev), filename);
115 if (r < 0)
56f64d95 116 log_error_errno(errno, "Failed to create symlink %s: %m", filename);
912541b0
KS
117
118 udev_device_set_watch_handle(dev, wd);
bd284db1
SJR
119}
120
9ec6e95b 121void udev_watch_end(struct udev *udev, struct udev_device *dev) {
912541b0
KS
122 int wd;
123 char filename[UTIL_PATH_SIZE];
bd284db1 124
912541b0
KS
125 if (inotify_fd < 0)
126 return;
03e0170d 127
912541b0
KS
128 wd = udev_device_get_watch_handle(dev);
129 if (wd < 0)
130 return;
bd284db1 131
9f6445e3 132 log_debug("removing watch on '%s'", udev_device_get_devnode(dev));
912541b0 133 inotify_rm_watch(inotify_fd, wd);
bd284db1 134
6ada823a 135 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
912541b0 136 unlink(filename);
047f88bc 137
912541b0 138 udev_device_set_watch_handle(dev, -1);
bd284db1
SJR
139}
140
9ec6e95b 141struct udev_device *udev_watch_lookup(struct udev *udev, int wd) {
912541b0 142 char filename[UTIL_PATH_SIZE];
6ada823a 143 char device[UTIL_NAME_SIZE];
912541b0
KS
144 ssize_t len;
145
146 if (inotify_fd < 0 || wd < 0)
147 return NULL;
148
6ada823a
KS
149 snprintf(filename, sizeof(filename), "/run/udev/watch/%d", wd);
150 len = readlink(filename, device, sizeof(device));
151 if (len <= 0 || (size_t)len == sizeof(device))
912541b0 152 return NULL;
6ada823a 153 device[len] = '\0';
912541b0 154
dbf61afb 155 return udev_device_new_from_device_id(udev, device);
bd284db1 156}