/*
- * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org>
* Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
*
- * 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 version 2 of the License.
- *
- * 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, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * 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 <stdlib.h>
#include <sys/un.h>
#include "udev.h"
-#include "udevd.h"
#include "udev_rules.h"
static int verbose;
return 0;
}
-static int device_list_insert(const char *path)
+static int device_list_insert(struct udev *udev, const char *path)
{
char filename[PATH_SIZE];
char devpath[PATH_SIZE];
struct stat statbuf;
- dbg("add '%s'\n" , path);
+ dbg(udev, "add '%s'\n" , path);
/* we only have a device, if we have an uevent file */
strlcpy(filename, path, sizeof(filename));
if (!(statbuf.st_mode & S_IWUSR))
return -1;
- strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
+ strlcpy(devpath, &path[strlen(udev_get_sys_path(udev))], sizeof(devpath));
/* resolve possible link to real target */
if (lstat(path, &statbuf) < 0)
return -1;
if (S_ISLNK(statbuf.st_mode))
- if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
+ if (sysfs_resolve_link(udev, devpath, sizeof(devpath)) != 0)
return -1;
- name_list_add(&device_list, devpath, 1);
+ name_list_add(udev, &device_list, devpath, 1);
return 0;
}
-static void trigger_uevent(const char *devpath, const char *action)
+static void trigger_uevent(struct udev *udev, const char *devpath, const char *action)
{
char filename[PATH_SIZE];
int fd;
- strlcpy(filename, sysfs_path, sizeof(filename));
+ strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
strlcat(filename, devpath, sizeof(filename));
strlcat(filename, "/uevent", sizeof(filename));
fd = open(filename, O_WRONLY);
if (fd < 0) {
- dbg("error on opening %s: %s\n", filename, strerror(errno));
+ dbg(udev, "error on opening %s: %s\n", filename, strerror(errno));
return;
}
if (write(fd, action, strlen(action)) < 0)
- info("error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
+ info(udev, "error writing '%s' to '%s': %s\n", action, filename, strerror(errno));
close(fd);
}
-static int pass_to_socket(const char *devpath, const char *action, const char *env)
+static int pass_to_socket(struct udev *udev, const char *devpath, const char *action, const char *env)
{
- struct udevice *udev;
+ struct udevice *udevice;
struct name_entry *name_loop;
char buf[4096];
size_t bufpos = 0;
if (verbose)
printf("%s\n", devpath);
- udev = udev_device_init();
- if (udev == NULL)
+ udevice = udev_device_init(udev);
+ if (udevice == NULL)
return -1;
- udev_db_get_device(udev, devpath);
+ udev_db_get_device(udevice, devpath);
/* add header */
bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
bufpos++;
/* add subsystem */
- strlcpy(path, sysfs_path, sizeof(path));
+ strlcpy(path, udev_get_sys_path(udev), sizeof(path));
strlcat(path, devpath, sizeof(path));
strlcat(path, "/subsystem", sizeof(path));
len = readlink(path, link_target, sizeof(link_target));
/* add symlinks and node name */
path[0] = '\0';
- list_for_each_entry(name_loop, &udev->symlink_list, node) {
- strlcat(path, udev_root, sizeof(path));
+ list_for_each_entry(name_loop, &udevice->symlink_list, node) {
+ strlcat(path, udev_get_dev_path(udev), sizeof(path));
strlcat(path, "/", sizeof(path));
strlcat(path, name_loop->name, sizeof(path));
strlcat(path, " ", sizeof(path));
}
- remove_trailing_chars(path, ' ');
+ util_remove_trailing_chars(path, ' ');
if (path[0] != '\0') {
bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
bufpos++;
}
- if (udev->name[0] != '\0') {
- strlcpy(path, udev_root, sizeof(path));
+ if (udevice->name[0] != '\0') {
+ strlcpy(path, udev_get_dev_path(udev), sizeof(path));
strlcat(path, "/", sizeof(path));
- strlcat(path, udev->name, sizeof(path));
+ strlcat(path, udevice->name, sizeof(path));
bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
bufpos++;
}
/* add keys from device "uevent" file */
- strlcpy(path, sysfs_path, sizeof(path));
+ strlcpy(path, udev_get_sys_path(udev), sizeof(path));
strlcat(path, devpath, sizeof(path));
strlcat(path, "/uevent", sizeof(path));
fd = open(path, O_RDONLY);
}
/* add keys from database */
- list_for_each_entry(name_loop, &udev->env_list, node) {
+ list_for_each_entry(name_loop, &udevice->env_list, node) {
bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
bufpos++;
}
return err;
}
-static void exec_list(const char *action, const char *env)
+static void exec_list(struct udev *udev, const char *action, const char *env)
{
struct name_entry *loop_device;
struct name_entry *tmp_device;
if (delay_device(loop_device->name))
continue;
if (sock >= 0)
- pass_to_socket(loop_device->name, action, env);
+ pass_to_socket(udev, loop_device->name, action, env);
else
- trigger_uevent(loop_device->name, action);
+ trigger_uevent(udev, loop_device->name, action);
list_del(&loop_device->node);
free(loop_device);
}
/* trigger remaining delayed devices */
list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
if (sock >= 0)
- pass_to_socket(loop_device->name, action, env);
+ pass_to_socket(udev, loop_device->name, action, env);
else
- trigger_uevent(loop_device->name, action);
+ trigger_uevent(udev, loop_device->name, action);
list_del(&loop_device->node);
free(loop_device);
}
if (size < 0)
return 0;
value[size] = '\0';
- remove_trailing_chars(value, '\n');
+ util_remove_trailing_chars(value, '\n');
/* match if attribute value matches */
if (fnmatch(match_value, value, 0) == 0)
SCAN_SUBSYSTEM,
};
-static void scan_subsystem(const char *subsys, enum scan_type scan)
+static void scan_subsystem(struct udev *udev, const char *subsys, enum scan_type scan)
{
char base[PATH_SIZE];
DIR *dir;
else
return;
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/", sizeof(base));
strlcat(base, subsys, sizeof(base));
if (attr_filtered(dirname))
continue;
if (!subsystem_filtered("subsystem"))
- device_list_insert(dirname);
+ device_list_insert(udev, dirname);
if (subsystem_filtered("drivers"))
continue;
}
strlcat(dirname2, dent2->d_name, sizeof(dirname2));
if (attr_filtered(dirname2))
continue;
- device_list_insert(dirname2);
+ device_list_insert(udev, dirname2);
}
closedir(dir2);
}
}
}
-static void scan_block(void)
+static void scan_block(struct udev *udev)
{
char base[PATH_SIZE];
DIR *dir;
if (subsystem_filtered("block"))
return;
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/block", sizeof(base));
dir = opendir(base);
strlcat(dirname, dent->d_name, sizeof(dirname));
if (attr_filtered(dirname))
continue;
- if (device_list_insert(dirname) != 0)
+ if (device_list_insert(udev, dirname) != 0)
continue;
/* look for partitions */
strlcat(dirname2, dent2->d_name, sizeof(dirname2));
if (attr_filtered(dirname2))
continue;
- device_list_insert(dirname2);
+ device_list_insert(udev, dirname2);
}
closedir(dir2);
}
}
}
-static void scan_class(void)
+static void scan_class(struct udev *udev)
{
char base[PATH_SIZE];
DIR *dir;
struct dirent *dent;
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/class", sizeof(base));
dir = opendir(base);
strlcat(dirname2, dent2->d_name, sizeof(dirname2));
if (attr_filtered(dirname2))
continue;
- device_list_insert(dirname2);
+ device_list_insert(udev, dirname2);
}
closedir(dir2);
}
}
}
-static void scan_failed(void)
+static void scan_failed(struct udev *udev)
{
char base[PATH_SIZE];
DIR *dir;
struct dirent *dent;
- strlcpy(base, udev_root, sizeof(base));
+ strlcpy(base, udev_get_dev_path(udev), sizeof(base));
strlcat(base, "/.udev/failed", sizeof(base));
dir = opendir(base);
if (dent->d_name[0] == '.')
continue;
- start = strlcpy(device, sysfs_path, sizeof(device));
+ start = strlcpy(device, udev_get_sys_path(udev), sizeof(device));
if(start >= sizeof(device))
start = sizeof(device) - 1;
strlcat(device, dent->d_name, sizeof(device));
- path_decode(&device[start]);
- device_list_insert(device);
+ util_path_decode(&device[start]);
+ device_list_insert(udev, device);
}
closedir(dir);
}
}
-int udevadm_trigger(int argc, char *argv[])
+int udevadm_trigger(struct udev *udev, int argc, char *argv[])
{
int failed = 0;
const char *sockpath = NULL;
{}
};
- dbg("version %s\n", VERSION);
+ dbg(udev, "version %s\n", VERSION);
while (1) {
option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
env = optarg;
break;
case 's':
- name_list_add(&filter_subsystem_match_list, optarg, 0);
+ name_list_add(udev, &filter_subsystem_match_list, optarg, 0);
break;
case 'S':
- name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
+ name_list_add(udev, &filter_subsystem_nomatch_list, optarg, 0);
break;
case 'a':
- name_list_add(&filter_attr_match_list, optarg, 0);
+ name_list_add(udev, &filter_attr_match_list, optarg, 0);
break;
case 'A':
- name_list_add(&filter_attr_nomatch_list, optarg, 0);
+ name_list_add(udev, &filter_attr_nomatch_list, optarg, 0);
break;
case 'h':
printf("Usage: udevadm trigger OPTIONS\n"
}
if (failed) {
- scan_failed();
- exec_list(action, env);
+ scan_failed(udev);
+ exec_list(udev, action, env);
} else {
char base[PATH_SIZE];
struct stat statbuf;
/* if we have /sys/subsystem, forget all the old stuff */
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/subsystem", sizeof(base));
if (stat(base, &statbuf) == 0) {
- scan_subsystem("subsystem", SCAN_SUBSYSTEM);
- exec_list(action, env);
- scan_subsystem("subsystem", SCAN_DEVICES);
- exec_list(action, env);
+ scan_subsystem(udev, "subsystem", SCAN_SUBSYSTEM);
+ exec_list(udev, action, env);
+ scan_subsystem(udev, "subsystem", SCAN_DEVICES);
+ exec_list(udev, action, env);
} else {
- scan_subsystem("bus", SCAN_SUBSYSTEM);
- exec_list(action, env);
- scan_subsystem("bus", SCAN_DEVICES);
- scan_class();
+ scan_subsystem(udev, "bus", SCAN_SUBSYSTEM);
+ exec_list(udev, action, env);
+ scan_subsystem(udev, "bus", SCAN_DEVICES);
+ scan_class(udev);
/* scan "block" if it isn't a "class" */
- strlcpy(base, sysfs_path, sizeof(base));
+ strlcpy(base, udev_get_sys_path(udev), sizeof(base));
strlcat(base, "/class/block", sizeof(base));
if (stat(base, &statbuf) != 0)
- scan_block();
- exec_list(action, env);
+ scan_block(udev);
+ exec_list(udev, action, env);
}
}
exit:
- name_list_cleanup(&filter_subsystem_match_list);
- name_list_cleanup(&filter_subsystem_nomatch_list);
- name_list_cleanup(&filter_attr_match_list);
- name_list_cleanup(&filter_attr_nomatch_list);
+ name_list_cleanup(udev, &filter_subsystem_match_list);
+ name_list_cleanup(udev, &filter_subsystem_nomatch_list);
+ name_list_cleanup(udev, &filter_attr_match_list);
+ name_list_cleanup(udev, &filter_attr_nomatch_list);
if (sock >= 0)
close(sock);