]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-watch.c
Merge pull request #4835 from poettering/unit-name-printf
[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
cf0fbc49 20#include <errno.h>
bd284db1 21#include <stddef.h>
cf0fbc49 22#include <stdio.h>
bd284db1 23#include <sys/inotify.h>
cf0fbc49 24#include <unistd.h>
bd284db1 25
8fb3f009 26#include "dirent-util.h"
d054f0a4 27#include "stdio-util.h"
bd284db1
SJR
28#include "udev.h"
29
1e03b754 30static int inotify_fd = -1;
bd284db1
SJR
31
32/* inotify descriptor, will be shared with rules directory;
33 * set to cloexec since we need our children to be able to add
34 * watches for us
35 */
9ec6e95b 36int udev_watch_init(struct udev *udev) {
912541b0
KS
37 inotify_fd = inotify_init1(IN_CLOEXEC);
38 if (inotify_fd < 0)
56f64d95 39 log_error_errno(errno, "inotify_init failed: %m");
912541b0 40 return inotify_fd;
bd284db1
SJR
41}
42
43/* move any old watches directory out of the way, and then restore
44 * the watches
45 */
9ec6e95b 46void udev_watch_restore(struct udev *udev) {
912541b0
KS
47 if (inotify_fd < 0)
48 return;
49
6ada823a 50 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
912541b0
KS
51 DIR *dir;
52 struct dirent *ent;
53
6ada823a 54 dir = opendir("/run/udev/watch.old");
912541b0 55 if (dir == NULL) {
56f64d95 56 log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
912541b0
KS
57 return;
58 }
59
8fb3f009 60 FOREACH_DIRENT_ALL(ent, dir, break) {
912541b0 61 char device[UTIL_PATH_SIZE];
912541b0
KS
62 ssize_t len;
63 struct udev_device *dev;
64
65 if (ent->d_name[0] == '.')
66 continue;
67
6ada823a
KS
68 len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
69 if (len <= 0 || len == (ssize_t)sizeof(device))
912541b0 70 goto unlink;
6ada823a 71 device[len] = '\0';
912541b0 72
dbf61afb 73 dev = udev_device_new_from_device_id(udev, device);
912541b0
KS
74 if (dev == NULL)
75 goto unlink;
76
9f6445e3 77 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev));
912541b0
KS
78 udev_watch_begin(udev, dev);
79 udev_device_unref(dev);
777239cb 80unlink:
912541b0
KS
81 unlinkat(dirfd(dir), ent->d_name, 0);
82 }
bd284db1 83
912541b0 84 closedir(dir);
6ada823a 85 rmdir("/run/udev/watch.old");
bd284db1 86
1f6b4113 87 } else if (errno != ENOENT)
56f64d95 88 log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
bd284db1
SJR
89}
90
9ec6e95b 91void udev_watch_begin(struct udev *udev, struct udev_device *dev) {
e02c6135 92 char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
912541b0 93 int wd;
6bb2f0a0 94 int r;
912541b0
KS
95
96 if (inotify_fd < 0)
97 return;
98
9f6445e3 99 log_debug("adding watch on '%s'", udev_device_get_devnode(dev));
912541b0
KS
100 wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
101 if (wd < 0) {
56f64d95 102 log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m",
1f6b4113 103 inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
912541b0
KS
104 return;
105 }
106
d054f0a4 107 xsprintf(filename, "/run/udev/watch/%d", wd);
667e3924 108 mkdir_parents(filename, 0755);
912541b0 109 unlink(filename);
6bb2f0a0
VP
110 r = symlink(udev_device_get_id_filename(dev), filename);
111 if (r < 0)
56f64d95 112 log_error_errno(errno, "Failed to create symlink %s: %m", filename);
912541b0
KS
113
114 udev_device_set_watch_handle(dev, wd);
bd284db1
SJR
115}
116
9ec6e95b 117void udev_watch_end(struct udev *udev, struct udev_device *dev) {
912541b0 118 int wd;
e02c6135 119 char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
bd284db1 120
912541b0
KS
121 if (inotify_fd < 0)
122 return;
03e0170d 123
912541b0
KS
124 wd = udev_device_get_watch_handle(dev);
125 if (wd < 0)
126 return;
bd284db1 127
9f6445e3 128 log_debug("removing watch on '%s'", udev_device_get_devnode(dev));
912541b0 129 inotify_rm_watch(inotify_fd, wd);
bd284db1 130
d054f0a4 131 xsprintf(filename, "/run/udev/watch/%d", wd);
912541b0 132 unlink(filename);
047f88bc 133
912541b0 134 udev_device_set_watch_handle(dev, -1);
bd284db1
SJR
135}
136
9ec6e95b 137struct udev_device *udev_watch_lookup(struct udev *udev, int wd) {
e02c6135 138 char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
6ada823a 139 char device[UTIL_NAME_SIZE];
912541b0
KS
140 ssize_t len;
141
142 if (inotify_fd < 0 || wd < 0)
143 return NULL;
144
d054f0a4 145 xsprintf(filename, "/run/udev/watch/%d", wd);
6ada823a
KS
146 len = readlink(filename, device, sizeof(device));
147 if (len <= 0 || (size_t)len == sizeof(device))
912541b0 148 return NULL;
6ada823a 149 device[len] = '\0';
912541b0 150
dbf61afb 151 return udev_device_new_from_device_id(udev, device);
bd284db1 152}