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