]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-watch.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2009 Canonical Ltd.
6 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
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.
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.
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/>.
26 #include <sys/inotify.h>
29 #include "stdio-util.h"
32 static int inotify_fd
= -1;
34 /* inotify descriptor, will be shared with rules directory;
35 * set to cloexec since we need our children to be able to add
38 int udev_watch_init(struct udev
*udev
) {
39 inotify_fd
= inotify_init1(IN_CLOEXEC
);
41 log_error_errno(errno
, "inotify_init failed: %m");
45 /* move any old watches directory out of the way, and then restore
48 void udev_watch_restore(struct udev
*udev
) {
52 if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
56 dir
= opendir("/run/udev/watch.old");
58 log_error_errno(errno
, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
62 for (ent
= readdir(dir
); ent
!= NULL
; ent
= readdir(dir
)) {
63 char device
[UTIL_PATH_SIZE
];
65 struct udev_device
*dev
;
67 if (ent
->d_name
[0] == '.')
70 len
= readlinkat(dirfd(dir
), ent
->d_name
, device
, sizeof(device
));
71 if (len
<= 0 || len
== (ssize_t
)sizeof(device
))
75 dev
= udev_device_new_from_device_id(udev
, device
);
79 log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev
));
80 udev_watch_begin(udev
, dev
);
81 udev_device_unref(dev
);
83 unlinkat(dirfd(dir
), ent
->d_name
, 0);
87 rmdir("/run/udev/watch.old");
89 } else if (errno
!= ENOENT
)
90 log_error_errno(errno
, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
93 void udev_watch_begin(struct udev
*udev
, struct udev_device
*dev
) {
94 char filename
[UTIL_PATH_SIZE
];
101 log_debug("adding watch on '%s'", udev_device_get_devnode(dev
));
102 wd
= inotify_add_watch(inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
104 log_error_errno(errno
, "inotify_add_watch(%d, %s, %o) failed: %m",
105 inotify_fd
, udev_device_get_devnode(dev
), IN_CLOSE_WRITE
);
109 xsprintf(filename
, "/run/udev/watch/%d", wd
);
110 mkdir_parents(filename
, 0755);
112 r
= symlink(udev_device_get_id_filename(dev
), filename
);
114 log_error_errno(errno
, "Failed to create symlink %s: %m", filename
);
116 udev_device_set_watch_handle(dev
, wd
);
119 void udev_watch_end(struct udev
*udev
, struct udev_device
*dev
) {
121 char filename
[UTIL_PATH_SIZE
];
126 wd
= udev_device_get_watch_handle(dev
);
130 log_debug("removing watch on '%s'", udev_device_get_devnode(dev
));
131 inotify_rm_watch(inotify_fd
, wd
);
133 xsprintf(filename
, "/run/udev/watch/%d", wd
);
136 udev_device_set_watch_handle(dev
, -1);
139 struct udev_device
*udev_watch_lookup(struct udev
*udev
, int wd
) {
140 char filename
[UTIL_PATH_SIZE
];
141 char device
[UTIL_NAME_SIZE
];
144 if (inotify_fd
< 0 || wd
< 0)
147 xsprintf(filename
, "/run/udev/watch/%d", wd
);
148 len
= readlink(filename
, device
, sizeof(device
));
149 if (len
<= 0 || (size_t)len
== sizeof(device
))
153 return udev_device_new_from_device_id(udev
, device
);