From 9af5bb2f8fdbf54c064ddbd319d61092f28a4132 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Thu, 25 Nov 2004 02:44:38 +0100 Subject: [PATCH] rename udev_lib to udev_utils and dev_d to udev_multiplex --- Makefile | 10 +- dev_d.c | 114 --------------------- klibc_fixups/klibc_fixups.c | 2 +- namedev.c | 2 +- namedev_parse.c | 2 +- udev.c | 8 +- udev.h | 2 +- udev_add.c | 2 +- udev_config.c | 2 +- udev_db.c | 2 +- udev_lib.c | 244 -------------------------------------------- udev_lib.h | 91 ----------------- udev_multiplex.c | 114 +++++++++++++++++++++ udev_remove.c | 2 +- udev_start.c | 4 +- udev_sysfs.c | 2 +- udev_utils.c | 244 ++++++++++++++++++++++++++++++++++++++++++++ udev_utils.h | 91 +++++++++++++++++ udevd.c | 2 +- udevinfo.c | 2 +- udevsend.c | 1 - udevtest.c | 2 +- 22 files changed, 472 insertions(+), 473 deletions(-) delete mode 100644 dev_d.c delete mode 100644 udev_lib.c delete mode 100644 udev_lib.h create mode 100644 udev_multiplex.c create mode 100644 udev_utils.c create mode 100644 udev_utils.h diff --git a/Makefile b/Makefile index c89fa0feee..4d48b3c9e0 100644 --- a/Makefile +++ b/Makefile @@ -204,20 +204,20 @@ SYSFS = $(PWD)/libsysfs/sysfs_bus.o \ $(PWD)/libsysfs/sysfs_utils.o \ $(PWD)/libsysfs/dlist.o -OBJS = udev_lib.o \ +OBJS = udev_utils.o \ udev_config.o \ udev_add.o \ udev_remove.o \ udev_start.o \ udev_sysfs.o \ udev_db.o \ + udev_multiplex.o\ namedev.o \ namedev_parse.o \ - dev_d.o \ $(SYSFS) HEADERS = udev.h \ - udev_lib.h \ + udev_utils.h \ namedev.h \ udev_version.h \ udev_db.h \ @@ -291,11 +291,11 @@ $(TESTER): $(LIBC) $(TESTER).o $(OBJS) $(HEADERS) $(QUIET) $(STRIPCMD) $@ $(INFO): $(LIBC) $(INFO).o $(OBJS) $(HEADERS) - $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_lib.o udev_config.o udev_db.o $(SYSFS) $(LIB_OBJS) $(ARCH_LIB_OBJS) + $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_utils.o udev_config.o udev_db.o $(SYSFS) $(LIB_OBJS) $(ARCH_LIB_OBJS) $(QUIET) $(STRIPCMD) $@ $(DAEMON): $(LIBC) $(DAEMON).o $(OBJS) udevd.h - $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevd.o udev_lib.o $(KLIBC_FIXUP) $(LIB_OBJS) $(ARCH_LIB_OBJS) + $(QUIET) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevd.o udev_utils.o $(KLIBC_FIXUP) $(LIB_OBJS) $(ARCH_LIB_OBJS) $(QUIET) $(STRIPCMD) $@ $(SENDER): $(LIBC) $(SENDER).o $(OBJS) udevd.h diff --git a/dev_d.c b/dev_d.c deleted file mode 100644 index 817355af60..0000000000 --- a/dev_d.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * dev_d.c - dev.d/ multiplexer - * - * Copyright (C) 2004 Greg Kroah-Hartman - * - * 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 essentially emulates the following shell script logic in C: - * DIR="/etc/dev.d" - * export DEVNAME="whatever_dev_name_udev_just_gave" - * for I in "${DIR}/$DEVNAME/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do - * if [ -f $I ]; then $I $1 ; fi - * done - * exit 1; - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "udev.h" -#include "udev_lib.h" -#include "logging.h" - -static int run_program(const char *filename, void *data) -{ - pid_t pid; - int fd; - struct udevice *udev = data; - - dbg("running %s", filename); - - pid = fork(); - switch (pid) { - case 0: - /* child */ - fd = open("/dev/null", O_RDWR); - if ( fd >= 0) { - dup2(fd, STDOUT_FILENO); - dup2(fd, STDIN_FILENO); - dup2(fd, STDERR_FILENO); - } - close(fd); - - execl(filename, filename, udev->subsystem, NULL); - dbg("exec of child failed"); - _exit(1); - case -1: - dbg("fork of child failed"); - break; - return -1; - default: - waitpid(pid, NULL, 0); - } - - return 0; -} - -/* - * runs files in these directories in order: - * / - * subsystem/ - * default/ - */ -void dev_d_execute(struct udevice *udev, const char *basedir, const char *suffix) -{ - char dirname[PATH_MAX]; - char devname[NAME_SIZE]; - char *temp; - - /* skip if UDEV_NO_DEVD is set */ - if (udev_dev_d == 0) - return; - - strfieldcpy(devname, udev->name); - - /* chop the device name up into pieces based on '/' */ - temp = strchr(devname, '/'); - while (temp != NULL) { - temp[0] = '\0'; - snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname); - dirname[PATH_MAX-1] = '\0'; - call_foreach_file(run_program, dirname, suffix, udev); - - temp[0] = '/'; - ++temp; - temp = strchr(temp, '/'); - } - - if (udev->name[0] != '\0') { - snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name); - dirname[PATH_MAX-1] = '\0'; - call_foreach_file(run_program, dirname, suffix, udev); - } - - if (udev->subsystem[0] != '\0') { - snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem); - dirname[PATH_MAX-1] = '\0'; - call_foreach_file(run_program, dirname, suffix, udev); - } - - snprintf(dirname, PATH_MAX, "%s/default", basedir); - dirname[PATH_MAX-1] = '\0'; - call_foreach_file(run_program, dirname, suffix, udev); -} diff --git a/klibc_fixups/klibc_fixups.c b/klibc_fixups/klibc_fixups.c index 2d673d8ca9..175cf27fe4 100644 --- a/klibc_fixups/klibc_fixups.c +++ b/klibc_fixups/klibc_fixups.c @@ -30,7 +30,7 @@ #include "pwd.h" #include "../udev.h" -#include "../udev_lib.h" +#include "../udev_utils.h" #include "../logging.h" #define PW_FILE "/etc/passwd" diff --git a/namedev.c b/namedev.c index dca405ceba..126453cc89 100644 --- a/namedev.c +++ b/namedev.c @@ -36,7 +36,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "list.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "logging.h" #include "namedev.h" diff --git a/namedev_parse.c b/namedev_parse.c index cb1760fd57..d39141bfc7 100644 --- a/namedev_parse.c +++ b/namedev_parse.c @@ -36,7 +36,7 @@ #include #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "logging.h" #include "namedev.h" diff --git a/udev.c b/udev.c index e71cddd3ee..9b0a00fb78 100644 --- a/udev.c +++ b/udev.c @@ -33,7 +33,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_sysfs.h" #include "udev_version.h" #include "namedev.h" @@ -182,7 +182,7 @@ int main(int argc, char *argv[], char *envp[]) /* run dev.d/ scripts if we created a node or changed a netif name */ if (udev.devname[0] != '\0') { setenv("DEVNAME", udev.devname, 1); - dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX); + udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); } sysfs_close_class_device(class_dev); @@ -200,7 +200,7 @@ int main(int argc, char *argv[], char *envp[]) /* run dev.d/ scripts if we're not instructed to ignore the event */ if (udev.devname[0] != '\0') { setenv("DEVNAME", udev.devname, 1); - dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX); + udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); } } @@ -229,7 +229,7 @@ int main(int argc, char *argv[], char *envp[]) hotplug: if (manage_hotplug_event()) - dev_d_execute(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX); + udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX); exit: logging_close(); diff --git a/udev.h b/udev.h index e834f1e5b9..1e3db5af27 100644 --- a/udev.h +++ b/udev.h @@ -78,7 +78,7 @@ extern int udev_remove_device(struct udevice *udev); extern void udev_init_config(void); extern int udev_start(void); extern int parse_get_pair(char **orig_string, char **left, char **right); -extern void dev_d_execute(struct udevice *udev, const char *basedir, const char *suffix); +extern void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix); extern char sysfs_path[SYSFS_PATH_MAX]; extern char udev_root[PATH_MAX]; diff --git a/udev_add.c b/udev_add.c index 4fb5ef18c0..f537f567cc 100644 --- a/udev_add.c +++ b/udev_add.c @@ -38,7 +38,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "logging.h" #include "namedev.h" diff --git a/udev_config.c b/udev_config.c index 198473936f..51bdc720a7 100644 --- a/udev_config.c +++ b/udev_config.c @@ -34,7 +34,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "logging.h" #include "namedev.h" diff --git a/udev_db.c b/udev_db.c index e07e72b858..36da264716 100644 --- a/udev_db.c +++ b/udev_db.c @@ -33,7 +33,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "logging.h" #include "udev_db.h" diff --git a/udev_lib.c b/udev_lib.c deleted file mode 100644 index 86a526f7e8..0000000000 --- a/udev_lib.c +++ /dev/null @@ -1,244 +0,0 @@ -/* - * udev_lib - generic stuff used by udev - * - * Copyright (C) 2004 Kay Sievers - * - * 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., - * 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "udev.h" -#include "logging.h" -#include "udev_lib.h" -#include "list.h" - - -void udev_set_values(struct udevice *udev, const char* devpath, - const char *subsystem, const char* action) -{ - memset(udev, 0x00, sizeof(struct udevice)); - - if (devpath) - strfieldcpy(udev->devpath, devpath); - if (subsystem) - strfieldcpy(udev->subsystem, subsystem); - if (action) - strfieldcpy(udev->action, action); - - if (strcmp(udev->subsystem, "block") == 0) - udev->type = 'b'; - else if (strcmp(udev->subsystem, "net") == 0) - udev->type = 'n'; - else if (strncmp(udev->devpath, "/block/", 7) == 0) - udev->type = 'b'; - else if (strncmp(udev->devpath, "/class/net/", 11) == 0) - udev->type = 'n'; - else if (strncmp(udev->devpath, "/class/", 7) == 0) - udev->type = 'c'; -} - -int kernel_release_satisfactory(int version, int patchlevel, int sublevel) -{ - static int kversion = 0; - static int kpatchlevel; - static int ksublevel; - - if (kversion == 0) { - struct utsname uts; - if (uname(&uts) != 0) - return -1; - - if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) { - kversion = 0; - return -1; - } - } - - if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel) - return 1; - else - return 0; -} - -int create_path(const char *path) -{ - char p[NAME_SIZE]; - char *pos; - struct stat stats; - - strcpy (p, path); - pos = strrchr(p, '/'); - if (pos == p || pos == NULL) - return 0; - - while (pos[-1] == '/') - pos--; - - pos[0] = '\0'; - - dbg("stat '%s'\n", p); - if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) - return 0; - - if (create_path (p) != 0) - return -1; - - dbg("mkdir '%s'\n", p); - return mkdir(p, 0755); -} - -int file_map(const char *filename, char **buf, size_t *bufsize) -{ - struct stat stats; - int fd; - - fd = open(filename, O_RDONLY); - if (fd < 0) { - return -1; - } - - if (fstat(fd, &stats) < 0) { - close(fd); - return -1; - } - - *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (*buf == MAP_FAILED) { - close(fd); - return -1; - } - *bufsize = stats.st_size; - - close(fd); - - return 0; -} - -void file_unmap(char *buf, size_t bufsize) -{ - munmap(buf, bufsize); -} - -size_t buf_get_line(char *buf, size_t buflen, size_t cur) -{ - size_t count = 0; - - for (count = cur; count < buflen && buf[count] != '\n'; count++); - - return count - cur; -} - -void no_trailing_slash(char *path) -{ - int len; - - len = strlen(path); - if (len > 0 && path[len-1] == '/') - path[len-1] = '\0'; -} - -struct files { - struct list_head list; - char name[NAME_SIZE]; -}; - -/* sort files in lexical order */ -static int file_list_insert(char *filename, struct list_head *file_list) -{ - struct files *loop_file; - struct files *new_file; - - list_for_each_entry(loop_file, file_list, list) { - if (strcmp(loop_file->name, filename) > 0) { - break; - } - } - - new_file = malloc(sizeof(struct files)); - if (new_file == NULL) { - dbg("error malloc"); - return -ENOMEM; - } - - strfieldcpy(new_file->name, filename); - list_add_tail(&new_file->list, &loop_file->list); - return 0; -} - -/* calls function for every file found in specified directory */ -int call_foreach_file(file_fnct_t fnct, const char *dirname, - const char *suffix, void *data) -{ - struct dirent *ent; - DIR *dir; - char *ext; - struct files *loop_file; - struct files *tmp_file; - LIST_HEAD(file_list); - - dbg("open directory '%s'", dirname); - dir = opendir(dirname); - if (dir == NULL) { - dbg("unable to open '%s'", dirname); - return -1; - } - - while (1) { - ent = readdir(dir); - if (ent == NULL || ent->d_name[0] == '\0') - break; - - if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER)) - continue; - - /* look for file with specified suffix */ - ext = strrchr(ent->d_name, '.'); - if (ext == NULL) - continue; - - if (strcmp(ext, suffix) != 0) - continue; - - dbg("put file '%s/%s' in list", dirname, ent->d_name); - file_list_insert(ent->d_name, &file_list); - } - - /* call function for every file in the list */ - list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) { - char filename[NAME_SIZE]; - - snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name); - filename[NAME_SIZE-1] = '\0'; - - fnct(filename, data); - - list_del(&loop_file->list); - free(loop_file); - } - - closedir(dir); - return 0; -} diff --git a/udev_lib.h b/udev_lib.h deleted file mode 100644 index b9b0fda678..0000000000 --- a/udev_lib.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * udev_lib - generic stuff used by udev - * - * Copyright (C) 2004 Kay Sievers - * - * - * 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., - * 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#ifndef _UDEV_LIB_H_ -#define _UDEV_LIB_H_ - -#include "udev.h" - -#define strfieldcpy(to, from) \ -do { \ - to[sizeof(to)-1] = '\0'; \ - strncpy(to, from, sizeof(to)-1); \ -} while (0) - -#define strfieldcat(to, from) \ -do { \ - to[sizeof(to)-1] = '\0'; \ - strncat(to, from, sizeof(to) - strlen(to)-1); \ -} while (0) - -#define strfieldcpymax(to, from, maxsize) \ -do { \ - to[maxsize-1] = '\0'; \ - strncpy(to, from, maxsize-1); \ -} while (0) - -#define strfieldcatmax(to, from, maxsize) \ -do { \ - to[maxsize-1] = '\0'; \ - strncat(to, from, maxsize - strlen(to)-1); \ -} while (0) - -#define strintcat(to, i) \ -do { \ - to[sizeof(to)-1] = '\0'; \ - snprintf((to) + strlen(to), sizeof(to) - strlen(to)-1, "%u", i); \ -} while (0) - -#define strintcatmax(to, i, maxsize) \ -do { \ - to[maxsize-1] = '\0'; \ - snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \ -} while (0) - -#define foreach_strpart(str, separator, pos, len) \ - for(pos = str, len = 0; \ - (pos) < ((str) + strlen(str)); \ - pos = pos + len + strspn(pos, separator), len = strcspn(pos, separator)) \ - if (len > 0) - -#ifdef asmlinkage -# undef asmlinkage -#endif -#ifdef __i386__ -# define asmlinkage __attribute__((regparm(0))) -#endif -#ifndef asmlinkage -# define asmlinkage /* nothing */ -#endif - -extern void udev_set_values(struct udevice *udev, const char* devpath, - const char *subsystem, const char* action); -extern int kernel_release_satisfactory(int version, int patchlevel, int sublevel); -extern int create_path(const char *path); -extern int file_map(const char *filename, char **buf, size_t *bufsize); -extern void file_unmap(char *buf, size_t bufsize); -extern size_t buf_get_line(char *buf, size_t buflen, size_t cur); -extern void no_trailing_slash(char *path); -typedef int (*file_fnct_t)(const char *filename, void *data); -extern int call_foreach_file(file_fnct_t fnct, const char *dirname, - const char *suffix, void *data); - -#endif diff --git a/udev_multiplex.c b/udev_multiplex.c new file mode 100644 index 0000000000..7e3dc656d6 --- /dev/null +++ b/udev_multiplex.c @@ -0,0 +1,114 @@ +/* + * udev_multiplex.c directory multiplexer + * + * Copyright (C) 2004 Greg Kroah-Hartman + * + * 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 essentially emulates the following shell script logic in C: + * DIR="/etc/dev.d" + * export DEVNAME="whatever_dev_name_udev_just_gave" + * for I in "${DIR}/$DEVNAME/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do + * if [ -f $I ]; then $I $1 ; fi + * done + * exit 1; + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "udev.h" +#include "udev_utils.h" +#include "logging.h" + +static int run_program(const char *filename, void *data) +{ + pid_t pid; + int fd; + struct udevice *udev = data; + + dbg("running %s", filename); + + pid = fork(); + switch (pid) { + case 0: + /* child */ + fd = open("/dev/null", O_RDWR); + if ( fd >= 0) { + dup2(fd, STDOUT_FILENO); + dup2(fd, STDIN_FILENO); + dup2(fd, STDERR_FILENO); + } + close(fd); + + execl(filename, filename, udev->subsystem, NULL); + dbg("exec of child failed"); + _exit(1); + case -1: + dbg("fork of child failed"); + break; + return -1; + default: + waitpid(pid, NULL, 0); + } + + return 0; +} + +/* + * runs files in these directories in order: + * / + * subsystem/ + * default/ + */ +void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix) +{ + char dirname[PATH_MAX]; + char devname[NAME_SIZE]; + char *temp; + + /* skip if UDEV_NO_DEVD is set */ + if (udev_dev_d == 0) + return; + + strfieldcpy(devname, udev->name); + + /* chop the device name up into pieces based on '/' */ + temp = strchr(devname, '/'); + while (temp != NULL) { + temp[0] = '\0'; + snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname); + dirname[PATH_MAX-1] = '\0'; + call_foreach_file(run_program, dirname, suffix, udev); + + temp[0] = '/'; + ++temp; + temp = strchr(temp, '/'); + } + + if (udev->name[0] != '\0') { + snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name); + dirname[PATH_MAX-1] = '\0'; + call_foreach_file(run_program, dirname, suffix, udev); + } + + if (udev->subsystem[0] != '\0') { + snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem); + dirname[PATH_MAX-1] = '\0'; + call_foreach_file(run_program, dirname, suffix, udev); + } + + snprintf(dirname, PATH_MAX, "%s/default", basedir); + dirname[PATH_MAX-1] = '\0'; + call_foreach_file(run_program, dirname, suffix, udev); +} diff --git a/udev_remove.c b/udev_remove.c index 8887125dd8..142503589a 100644 --- a/udev_remove.c +++ b/udev_remove.c @@ -31,7 +31,7 @@ #include #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "namedev.h" #include "udev_db.h" diff --git a/udev_start.c b/udev_start.c index 3e3062c019..129af4291a 100644 --- a/udev_start.c +++ b/udev_start.c @@ -35,7 +35,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "logging.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "list.h" #include "udev.h" @@ -111,7 +111,7 @@ static int add_device(char *devpath, char *subsystem) /* run dev.d/ scripts if we created a node or changed a netif name */ if (udev.devname[0] != '\0') { setenv("DEVNAME", udev.devname, 1); - dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX); + udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX); } sysfs_close_class_device(class_dev); diff --git a/udev_sysfs.c b/udev_sysfs.c index 4976b8f308..6655968433 100644 --- a/udev_sysfs.c +++ b/udev_sysfs.c @@ -31,7 +31,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev_version.h" #include "udev_sysfs.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "logging.h" /* list of subsystem specific files, NULL if there is no file to wait for */ diff --git a/udev_utils.c b/udev_utils.c new file mode 100644 index 0000000000..bdf0233e3a --- /dev/null +++ b/udev_utils.c @@ -0,0 +1,244 @@ +/* + * udev_lib - generic stuff used by udev + * + * Copyright (C) 2004 Kay Sievers + * + * 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., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "udev.h" +#include "logging.h" +#include "udev_utils.h" +#include "list.h" + + +void udev_set_values(struct udevice *udev, const char* devpath, + const char *subsystem, const char* action) +{ + memset(udev, 0x00, sizeof(struct udevice)); + + if (devpath) + strfieldcpy(udev->devpath, devpath); + if (subsystem) + strfieldcpy(udev->subsystem, subsystem); + if (action) + strfieldcpy(udev->action, action); + + if (strcmp(udev->subsystem, "block") == 0) + udev->type = 'b'; + else if (strcmp(udev->subsystem, "net") == 0) + udev->type = 'n'; + else if (strncmp(udev->devpath, "/block/", 7) == 0) + udev->type = 'b'; + else if (strncmp(udev->devpath, "/class/net/", 11) == 0) + udev->type = 'n'; + else if (strncmp(udev->devpath, "/class/", 7) == 0) + udev->type = 'c'; +} + +int kernel_release_satisfactory(int version, int patchlevel, int sublevel) +{ + static int kversion = 0; + static int kpatchlevel; + static int ksublevel; + + if (kversion == 0) { + struct utsname uts; + if (uname(&uts) != 0) + return -1; + + if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) { + kversion = 0; + return -1; + } + } + + if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel) + return 1; + else + return 0; +} + +int create_path(const char *path) +{ + char p[NAME_SIZE]; + char *pos; + struct stat stats; + + strcpy (p, path); + pos = strrchr(p, '/'); + if (pos == p || pos == NULL) + return 0; + + while (pos[-1] == '/') + pos--; + + pos[0] = '\0'; + + dbg("stat '%s'\n", p); + if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) + return 0; + + if (create_path (p) != 0) + return -1; + + dbg("mkdir '%s'\n", p); + return mkdir(p, 0755); +} + +int file_map(const char *filename, char **buf, size_t *bufsize) +{ + struct stat stats; + int fd; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + return -1; + } + + if (fstat(fd, &stats) < 0) { + close(fd); + return -1; + } + + *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (*buf == MAP_FAILED) { + close(fd); + return -1; + } + *bufsize = stats.st_size; + + close(fd); + + return 0; +} + +void file_unmap(char *buf, size_t bufsize) +{ + munmap(buf, bufsize); +} + +size_t buf_get_line(char *buf, size_t buflen, size_t cur) +{ + size_t count = 0; + + for (count = cur; count < buflen && buf[count] != '\n'; count++); + + return count - cur; +} + +void no_trailing_slash(char *path) +{ + int len; + + len = strlen(path); + if (len > 0 && path[len-1] == '/') + path[len-1] = '\0'; +} + +struct files { + struct list_head list; + char name[NAME_SIZE]; +}; + +/* sort files in lexical order */ +static int file_list_insert(char *filename, struct list_head *file_list) +{ + struct files *loop_file; + struct files *new_file; + + list_for_each_entry(loop_file, file_list, list) { + if (strcmp(loop_file->name, filename) > 0) { + break; + } + } + + new_file = malloc(sizeof(struct files)); + if (new_file == NULL) { + dbg("error malloc"); + return -ENOMEM; + } + + strfieldcpy(new_file->name, filename); + list_add_tail(&new_file->list, &loop_file->list); + return 0; +} + +/* calls function for every file found in specified directory */ +int call_foreach_file(file_fnct_t fnct, const char *dirname, + const char *suffix, void *data) +{ + struct dirent *ent; + DIR *dir; + char *ext; + struct files *loop_file; + struct files *tmp_file; + LIST_HEAD(file_list); + + dbg("open directory '%s'", dirname); + dir = opendir(dirname); + if (dir == NULL) { + dbg("unable to open '%s'", dirname); + return -1; + } + + while (1) { + ent = readdir(dir); + if (ent == NULL || ent->d_name[0] == '\0') + break; + + if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER)) + continue; + + /* look for file with specified suffix */ + ext = strrchr(ent->d_name, '.'); + if (ext == NULL) + continue; + + if (strcmp(ext, suffix) != 0) + continue; + + dbg("put file '%s/%s' in list", dirname, ent->d_name); + file_list_insert(ent->d_name, &file_list); + } + + /* call function for every file in the list */ + list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) { + char filename[NAME_SIZE]; + + snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name); + filename[NAME_SIZE-1] = '\0'; + + fnct(filename, data); + + list_del(&loop_file->list); + free(loop_file); + } + + closedir(dir); + return 0; +} diff --git a/udev_utils.h b/udev_utils.h new file mode 100644 index 0000000000..b9b0fda678 --- /dev/null +++ b/udev_utils.h @@ -0,0 +1,91 @@ +/* + * udev_lib - generic stuff used by udev + * + * Copyright (C) 2004 Kay Sievers + * + * + * 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., + * 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#ifndef _UDEV_LIB_H_ +#define _UDEV_LIB_H_ + +#include "udev.h" + +#define strfieldcpy(to, from) \ +do { \ + to[sizeof(to)-1] = '\0'; \ + strncpy(to, from, sizeof(to)-1); \ +} while (0) + +#define strfieldcat(to, from) \ +do { \ + to[sizeof(to)-1] = '\0'; \ + strncat(to, from, sizeof(to) - strlen(to)-1); \ +} while (0) + +#define strfieldcpymax(to, from, maxsize) \ +do { \ + to[maxsize-1] = '\0'; \ + strncpy(to, from, maxsize-1); \ +} while (0) + +#define strfieldcatmax(to, from, maxsize) \ +do { \ + to[maxsize-1] = '\0'; \ + strncat(to, from, maxsize - strlen(to)-1); \ +} while (0) + +#define strintcat(to, i) \ +do { \ + to[sizeof(to)-1] = '\0'; \ + snprintf((to) + strlen(to), sizeof(to) - strlen(to)-1, "%u", i); \ +} while (0) + +#define strintcatmax(to, i, maxsize) \ +do { \ + to[maxsize-1] = '\0'; \ + snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \ +} while (0) + +#define foreach_strpart(str, separator, pos, len) \ + for(pos = str, len = 0; \ + (pos) < ((str) + strlen(str)); \ + pos = pos + len + strspn(pos, separator), len = strcspn(pos, separator)) \ + if (len > 0) + +#ifdef asmlinkage +# undef asmlinkage +#endif +#ifdef __i386__ +# define asmlinkage __attribute__((regparm(0))) +#endif +#ifndef asmlinkage +# define asmlinkage /* nothing */ +#endif + +extern void udev_set_values(struct udevice *udev, const char* devpath, + const char *subsystem, const char* action); +extern int kernel_release_satisfactory(int version, int patchlevel, int sublevel); +extern int create_path(const char *path); +extern int file_map(const char *filename, char **buf, size_t *bufsize); +extern void file_unmap(char *buf, size_t bufsize); +extern size_t buf_get_line(char *buf, size_t buflen, size_t cur); +extern void no_trailing_slash(char *path); +typedef int (*file_fnct_t)(const char *filename, void *data); +extern int call_foreach_file(file_fnct_t fnct, const char *dirname, + const char *suffix, void *data); + +#endif diff --git a/udevd.c b/udevd.c index bd36d2c192..06b172bc25 100644 --- a/udevd.c +++ b/udevd.c @@ -38,8 +38,8 @@ #include "list.h" #include "udev.h" -#include "udev_lib.h" #include "udev_version.h" +#include "udev_utils.h" #include "udevd.h" #include "logging.h" diff --git a/udevinfo.c b/udevinfo.c index 9ba302cbdc..6e6b9aeb3e 100644 --- a/udevinfo.c +++ b/udevinfo.c @@ -30,7 +30,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "libsysfs/dlist.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "udev_db.h" #include "logging.h" diff --git a/udevsend.c b/udevsend.c index 7d865573c2..341ed4cc36 100644 --- a/udevsend.c +++ b/udevsend.c @@ -36,7 +36,6 @@ #include #include "udev.h" -#include "udev_lib.h" #include "udev_version.h" #include "udevd.h" #include "logging.h" diff --git a/udevtest.c b/udevtest.c index b9a5fea489..37ec767e13 100644 --- a/udevtest.c +++ b/udevtest.c @@ -29,7 +29,7 @@ #include "libsysfs/sysfs/libsysfs.h" #include "udev.h" -#include "udev_lib.h" +#include "udev_utils.h" #include "udev_version.h" #include "namedev.h" #include "logging.h" -- cgit v1.2.3-54-g00ecf