2009-02-11 18:38:56 +01:00
|
|
|
/*
|
2012-11-12 19:36:23 +01:00
|
|
|
* Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
|
2009-02-11 18:38:56 +01:00
|
|
|
* Copyright (C) 2009 Canonical Ltd.
|
2009-02-13 16:02:36 +01:00
|
|
|
* Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
|
2009-02-11 18:38:56 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <errno.h>
|
2009-02-11 18:38:56 +01:00
|
|
|
#include <stddef.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <stdio.h>
|
2009-02-11 18:38:56 +01:00
|
|
|
#include <sys/inotify.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <unistd.h>
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2016-01-12 15:34:20 +01:00
|
|
|
#include "stdio-util.h"
|
2009-02-11 18:38:56 +01:00
|
|
|
#include "udev.h"
|
|
|
|
|
2009-06-04 01:44:04 +02:00
|
|
|
static int inotify_fd = -1;
|
2009-02-11 18:38:56 +01:00
|
|
|
|
|
|
|
/* inotify descriptor, will be shared with rules directory;
|
|
|
|
* set to cloexec since we need our children to be able to add
|
|
|
|
* watches for us
|
|
|
|
*/
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_watch_init(struct udev *udev) {
|
2012-01-10 01:34:15 +01:00
|
|
|
inotify_fd = inotify_init1(IN_CLOEXEC);
|
|
|
|
if (inotify_fd < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "inotify_init failed: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
return inotify_fd;
|
2009-02-11 18:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* move any old watches directory out of the way, and then restore
|
|
|
|
* the watches
|
|
|
|
*/
|
2014-07-29 15:47:41 +02:00
|
|
|
void udev_watch_restore(struct udev *udev) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (inotify_fd < 0)
|
|
|
|
return;
|
|
|
|
|
2012-04-16 17:21:22 +02:00
|
|
|
if (rename("/run/udev/watch", "/run/udev/watch.old") == 0) {
|
2012-01-10 01:34:15 +01:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
2012-04-16 17:21:22 +02:00
|
|
|
dir = opendir("/run/udev/watch.old");
|
2012-01-10 01:34:15 +01:00
|
|
|
if (dir == NULL) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "unable to open old watches dir /run/udev/watch.old; old watches will not be restored: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
|
|
|
|
char device[UTIL_PATH_SIZE];
|
|
|
|
ssize_t len;
|
|
|
|
struct udev_device *dev;
|
|
|
|
|
|
|
|
if (ent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2012-04-16 17:21:22 +02:00
|
|
|
len = readlinkat(dirfd(dir), ent->d_name, device, sizeof(device));
|
|
|
|
if (len <= 0 || len == (ssize_t)sizeof(device))
|
2012-01-10 01:34:15 +01:00
|
|
|
goto unlink;
|
2012-04-16 17:21:22 +02:00
|
|
|
device[len] = '\0';
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2012-08-10 19:56:57 +02:00
|
|
|
dev = udev_device_new_from_device_id(udev, device);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (dev == NULL)
|
|
|
|
goto unlink;
|
|
|
|
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("restoring old watch on '%s'", udev_device_get_devnode(dev));
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_watch_begin(udev, dev);
|
|
|
|
udev_device_unref(dev);
|
2010-03-10 18:54:35 +01:00
|
|
|
unlink:
|
2012-01-10 01:34:15 +01:00
|
|
|
unlinkat(dirfd(dir), ent->d_name, 0);
|
|
|
|
}
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
closedir(dir);
|
2012-04-16 17:21:22 +02:00
|
|
|
rmdir("/run/udev/watch.old");
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2015-09-09 14:23:02 +02:00
|
|
|
} else if (errno != ENOENT)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "unable to move watches dir /run/udev/watch; old watches will not be restored: %m");
|
2009-02-11 18:38:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
void udev_watch_begin(struct udev *udev, struct udev_device *dev) {
|
2016-11-03 03:05:48 +01:00
|
|
|
char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
|
2012-01-10 01:34:15 +01:00
|
|
|
int wd;
|
2012-09-21 12:52:08 +02:00
|
|
|
int r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
if (inotify_fd < 0)
|
|
|
|
return;
|
|
|
|
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("adding watch on '%s'", udev_device_get_devnode(dev));
|
2012-01-10 01:34:15 +01:00
|
|
|
wd = inotify_add_watch(inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
|
|
|
|
if (wd < 0) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "inotify_add_watch(%d, %s, %o) failed: %m",
|
2015-09-09 14:23:02 +02:00
|
|
|
inotify_fd, udev_device_get_devnode(dev), IN_CLOSE_WRITE);
|
2012-01-10 01:34:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(filename, "/run/udev/watch/%d", wd);
|
2012-05-31 13:20:06 +02:00
|
|
|
mkdir_parents(filename, 0755);
|
2012-01-10 01:34:15 +01:00
|
|
|
unlink(filename);
|
2012-09-21 12:52:08 +02:00
|
|
|
r = symlink(udev_device_get_id_filename(dev), filename);
|
|
|
|
if (r < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to create symlink %s: %m", filename);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev_device_set_watch_handle(dev, wd);
|
2009-02-11 18:38:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
void udev_watch_end(struct udev *udev, struct udev_device *dev) {
|
2012-01-10 01:34:15 +01:00
|
|
|
int wd;
|
2016-11-03 03:05:48 +01:00
|
|
|
char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (inotify_fd < 0)
|
|
|
|
return;
|
2009-02-23 18:21:24 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
wd = udev_device_get_watch_handle(dev);
|
|
|
|
if (wd < 0)
|
|
|
|
return;
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2013-12-24 16:39:37 +01:00
|
|
|
log_debug("removing watch on '%s'", udev_device_get_devnode(dev));
|
2012-01-10 01:34:15 +01:00
|
|
|
inotify_rm_watch(inotify_fd, wd);
|
2009-02-11 18:38:56 +01:00
|
|
|
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(filename, "/run/udev/watch/%d", wd);
|
2012-01-10 01:34:15 +01:00
|
|
|
unlink(filename);
|
2009-02-23 18:43:53 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_device_set_watch_handle(dev, -1);
|
2009-02-11 18:38:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
struct udev_device *udev_watch_lookup(struct udev *udev, int wd) {
|
2016-11-03 03:05:48 +01:00
|
|
|
char filename[sizeof("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
|
2012-04-16 17:21:22 +02:00
|
|
|
char device[UTIL_NAME_SIZE];
|
2012-01-10 01:34:15 +01:00
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
if (inotify_fd < 0 || wd < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(filename, "/run/udev/watch/%d", wd);
|
2012-04-16 17:21:22 +02:00
|
|
|
len = readlink(filename, device, sizeof(device));
|
|
|
|
if (len <= 0 || (size_t)len == sizeof(device))
|
2012-01-10 01:34:15 +01:00
|
|
|
return NULL;
|
2012-04-16 17:21:22 +02:00
|
|
|
device[len] = '\0';
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2012-08-10 19:56:57 +02:00
|
|
|
return udev_device_new_from_device_id(udev, device);
|
2009-02-11 18:38:56 +01:00
|
|
|
}
|