]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
2 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
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.
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.
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/>.
24 #include <sys/inotify.h>
27 #include "stdio-util.h"
30 static int inotify_fd
= -1;
32 /* inotify descriptor, will be shared with rules directory;
33 * set to cloexec since we need our children to be able to add
36 int udev_watch_init(struct udev
*udev
) {
37 inotify_fd
= inotify_init1(IN_CLOEXEC
);
39 log_error_errno(errno
, "inotify_init failed: %m");
43 /* move any old watches directory out of the way, and then restore
46 void udev_watch_restore(struct udev
*udev
) {
50 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
54 dir
= opendir("/run/udev/watch.old");
56 log_error_errno(errno
, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
60 for (ent
= readdir(dir
); ent
!= NULL
; ent
= readdir(dir
)) {
61 char device
[UTIL_PATH_SIZE
];
63 struct udev_device
*dev
;
65 if (ent
->d_name
[0] == '.')
68 len
= readlinkat(dirfd(dir
), ent
->d_name
, device
, sizeof(device
));
69 if (len
<= 0 || len
== (ssize_t
)sizeof(device
))
73 dev
= udev_device_new_from_device_id(udev
, device
);
77 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev
));
78 udev_watch_begin(udev
, dev
);
79 udev_device_unref(dev
);
81 unlinkat(dirfd(dir
), ent
->d_name
, 0);
85 rmdir("/run/udev/watch.old");
87 } else if (errno
!= ENOENT
)
88 log_error_errno(errno
, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
91 void udev_watch_begin(struct udev
*udev
, struct udev_device
*dev
) {
92 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
99 log_debug("adding watch on '%s'", udev_device_get_devnode(dev
));
100 wd
= inotify_add_watch(inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
102 log_error_errno(errno
, "inotify_add_watch(%d, %s, %o) failed: %m",
103 inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
107 xsprintf(filename
, "/run/udev/watch/%d", wd
);
108 mkdir_parents(filename
, 0755);
110 r
= symlink(udev_device_get_id_filename(dev
), filename
);
112 log_error_errno(errno
, "Failed to create symlink %s: %m", filename
);
114 udev_device_set_watch_handle(dev
, wd
);
117 void udev_watch_end(struct udev
*udev
, struct udev_device
*dev
) {
119 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
124 wd
= udev_device_get_watch_handle(dev
);
128 log_debug("removing watch on '%s'", udev_device_get_devnode(dev
));
129 inotify_rm_watch(inotify_fd
, wd
);
131 xsprintf(filename
, "/run/udev/watch/%d", wd
);
134 udev_device_set_watch_handle(dev
, -1);
137 struct udev_device
*udev_watch_lookup(struct udev
*udev
, int wd
) {
138 char filename
[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
139 char device
[UTIL_NAME_SIZE
];
142 if (inotify_fd
< 0 || wd
< 0)
145 xsprintf(filename
, "/run/udev/watch/%d", wd
);
146 len
= readlink(filename
, device
, sizeof(device
));
147 if (len
<= 0 || (size_t)len
== sizeof(device
))
151 return udev_device_new_from_device_id(udev
, device
);