"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 2 | /* |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 4 | */ |
| 5 | |
| 6 | /* |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 7 | devfsd implementation for busybox |
| 8 | |
| 9 | Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it> |
| 10 | |
| 11 | Busybox version is based on some previous work and ideas |
| 12 | Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it> |
| 13 | |
| 14 | devfsd.c |
| 15 | |
| 16 | Main file for devfsd (devfs daemon for Linux). |
| 17 | |
| 18 | Copyright (C) 1998-2002 Richard Gooch |
| 19 | |
| 20 | devfsd.h |
| 21 | |
| 22 | Header file for devfsd (devfs daemon for Linux). |
| 23 | |
| 24 | Copyright (C) 1998-2000 Richard Gooch |
| 25 | |
| 26 | compat_name.c |
| 27 | |
| 28 | Compatibility name file for devfsd (build compatibility names). |
| 29 | |
| 30 | Copyright (C) 1998-2002 Richard Gooch |
| 31 | |
| 32 | expression.c |
| 33 | |
| 34 | This code provides Borne Shell-like expression expansion. |
| 35 | |
| 36 | Copyright (C) 1997-1999 Richard Gooch |
| 37 | |
| 38 | This program is free software; you can redistribute it and/or modify |
| 39 | it under the terms of the GNU General Public License as published by |
| 40 | the Free Software Foundation; either version 2 of the License, or |
| 41 | (at your option) any later version. |
| 42 | |
| 43 | This program is distributed in the hope that it will be useful, |
| 44 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 45 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 46 | GNU General Public License for more details. |
| 47 | |
| 48 | You should have received a copy of the GNU General Public License |
| 49 | along with this program; if not, write to the Free Software |
| 50 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 51 | |
| 52 | Richard Gooch may be reached by email at rgooch@atnf.csiro.au |
| 53 | The postal address is: |
| 54 | Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia. |
| 55 | */ |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 56 | #include "libbb.h" |
| 57 | #include "xregex.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 58 | #include <syslog.h> |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 59 | |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 60 | #include <sys/un.h> |
| 61 | #include <sys/sysmacros.h> |
Eric Andersen | 2af7000 | 2003-10-09 21:02:23 +0000 | [diff] [blame] | 62 | |
| 63 | /* Various defines taken from linux/major.h */ |
| 64 | #define IDE0_MAJOR 3 |
| 65 | #define IDE1_MAJOR 22 |
| 66 | #define IDE2_MAJOR 33 |
| 67 | #define IDE3_MAJOR 34 |
| 68 | #define IDE4_MAJOR 56 |
| 69 | #define IDE5_MAJOR 57 |
| 70 | #define IDE6_MAJOR 88 |
| 71 | #define IDE7_MAJOR 89 |
| 72 | #define IDE8_MAJOR 90 |
| 73 | #define IDE9_MAJOR 91 |
| 74 | |
| 75 | |
| 76 | /* Various defines taken from linux/devfs_fs.h */ |
| 77 | #define DEVFSD_PROTOCOL_REVISION_KERNEL 5 |
| 78 | #define DEVFSD_IOCTL_BASE 'd' |
| 79 | /* These are the various ioctls */ |
| 80 | #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int) |
| 81 | #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int) |
| 82 | #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 83 | #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int) |
Eric Andersen | 2af7000 | 2003-10-09 21:02:23 +0000 | [diff] [blame] | 84 | #define DEVFSD_NOTIFY_REGISTERED 0 |
| 85 | #define DEVFSD_NOTIFY_UNREGISTERED 1 |
| 86 | #define DEVFSD_NOTIFY_ASYNC_OPEN 2 |
| 87 | #define DEVFSD_NOTIFY_CLOSE 3 |
| 88 | #define DEVFSD_NOTIFY_LOOKUP 4 |
| 89 | #define DEVFSD_NOTIFY_CHANGE 5 |
| 90 | #define DEVFSD_NOTIFY_CREATE 6 |
| 91 | #define DEVFSD_NOTIFY_DELETE 7 |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 92 | #define DEVFS_PATHLEN 1024 |
| 93 | /* Never change this otherwise the binary interface will change */ |
| 94 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 95 | struct devfsd_notify_struct { |
| 96 | /* Use native C types to ensure same types in kernel and user space */ |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 97 | unsigned int type; /* DEVFSD_NOTIFY_* value */ |
| 98 | unsigned int mode; /* Mode of the inode or device entry */ |
| 99 | unsigned int major; /* Major number of device entry */ |
| 100 | unsigned int minor; /* Minor number of device entry */ |
| 101 | unsigned int uid; /* Uid of process, inode or device entry */ |
| 102 | unsigned int gid; /* Gid of process, inode or device entry */ |
| 103 | unsigned int overrun_count; /* Number of lost events */ |
| 104 | unsigned int namelen; /* Number of characters not including '\0' */ |
| 105 | /* The device name MUST come last */ |
| 106 | char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */ |
Eric Andersen | 2af7000 | 2003-10-09 21:02:23 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 109 | #define BUFFER_SIZE 16384 |
| 110 | #define DEVFSD_VERSION "1.3.25" |
| 111 | #define CONFIG_FILE "/etc/devfsd.conf" |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 112 | #define MODPROBE "/sbin/modprobe" |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 113 | #define MODPROBE_SWITCH_1 "-k" |
| 114 | #define MODPROBE_SWITCH_2 "-C" |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 115 | #define CONFIG_MODULES_DEVFS "/etc/modules.devfs" |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 116 | #define MAX_ARGS (6 + 1) |
| 117 | #define MAX_SUBEXPR 10 |
| 118 | #define STRING_LENGTH 255 |
| 119 | |
| 120 | /* for get_uid_gid() */ |
| 121 | #define UID 0 |
| 122 | #define GID 1 |
| 123 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 124 | /* fork_and_execute() */ |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 125 | # define DIE 1 |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 126 | # define NO_DIE 0 |
| 127 | |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 128 | /* for dir_operation() */ |
| 129 | #define RESTORE 0 |
| 130 | #define SERVICE 1 |
| 131 | #define READ_CONFIG 2 |
| 132 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 133 | /* Update only after changing code to reflect new protocol */ |
| 134 | #define DEVFSD_PROTOCOL_REVISION_DAEMON 5 |
| 135 | |
| 136 | /* Compile-time check */ |
| 137 | #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON |
| 138 | #error protocol version mismatch. Update your kernel headers |
| 139 | #endif |
| 140 | |
| 141 | #define AC_PERMISSIONS 0 |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 142 | #define AC_MODLOAD 1 |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 143 | #define AC_EXECUTE 2 |
| 144 | #define AC_MFUNCTION 3 /* not supported by busybox */ |
| 145 | #define AC_CFUNCTION 4 /* not supported by busybox */ |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 146 | #define AC_COPY 5 |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 147 | #define AC_IGNORE 6 |
| 148 | #define AC_MKOLDCOMPAT 7 |
| 149 | #define AC_MKNEWCOMPAT 8 |
| 150 | #define AC_RMOLDCOMPAT 9 |
| 151 | #define AC_RMNEWCOMPAT 10 |
| 152 | #define AC_RESTORE 11 |
| 153 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 154 | struct permissions_type { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 155 | mode_t mode; |
| 156 | uid_t uid; |
| 157 | gid_t gid; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 160 | struct execute_type { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 161 | char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 164 | struct copy_type { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 165 | const char *source; |
| 166 | const char *destination; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 169 | struct action_type { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 170 | unsigned int what; |
| 171 | unsigned int when; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 174 | struct config_entry_struct { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 175 | struct action_type action; |
| 176 | regex_t preg; |
| 177 | union |
| 178 | { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 179 | struct permissions_type permissions; |
| 180 | struct execute_type execute; |
| 181 | struct copy_type copy; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 182 | } |
| 183 | u; |
| 184 | struct config_entry_struct *next; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 187 | struct get_variable_info { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 188 | const struct devfsd_notify_struct *info; |
| 189 | const char *devname; |
| 190 | char devpath[STRING_LENGTH]; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 193 | static void dir_operation(int , const char * , int, unsigned long*); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 194 | static void service(struct stat statbuf, char *path); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 195 | static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 196 | static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 197 | static int mksymlink(const char *oldpath, const char *newpath); |
| 198 | static void read_config_file(char *path, int optional, unsigned long *event_mask); |
| 199 | static void process_config_line(const char *, unsigned long *); |
| 200 | static int do_servicing(int, unsigned long); |
| 201 | static void service_name(const struct devfsd_notify_struct *); |
| 202 | static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *); |
| 203 | static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 204 | const regmatch_t *, unsigned); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 205 | static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry); |
| 206 | static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 207 | const regmatch_t *, unsigned); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 208 | static void action_compat(const struct devfsd_notify_struct *, unsigned); |
| 209 | static void free_config(void); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 210 | static void restore(char *spath, struct stat source_stat, int rootlen); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 211 | static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *); |
| 212 | static mode_t get_mode(const char *); |
| 213 | static void signal_handler(int); |
| 214 | static const char *get_variable(const char *, void *); |
| 215 | static int make_dir_tree(const char *); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 216 | static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 217 | const char *, const regmatch_t *, unsigned); |
| 218 | static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 219 | static const char *expand_variable( char *, unsigned, unsigned *, const char *, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 220 | const char *(*)(const char *, void *), void *); |
| 221 | static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *); |
| 222 | static char get_old_ide_name(unsigned , unsigned); |
| 223 | static char *write_old_sd_name(char *, unsigned, unsigned, const char *); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 224 | |
| 225 | /* busybox functions */ |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 226 | static int get_uid_gid(int flag, const char *string); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 227 | static void safe_memcpy(char * dest, const char * src, int len); |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 228 | static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr); |
| 229 | static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 230 | |
| 231 | /* Structs and vars */ |
| 232 | static struct config_entry_struct *first_config = NULL; |
| 233 | static struct config_entry_struct *last_config = NULL; |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 234 | static char *mount_point = NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 235 | static volatile int caught_signal = FALSE; |
| 236 | static volatile int caught_sighup = FALSE; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 237 | static struct initial_symlink_struct { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 238 | const char *dest; |
| 239 | const char *name; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 240 | } initial_symlinks[] = { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 241 | {"/proc/self/fd", "fd"}, |
| 242 | {"fd/0", "stdin"}, |
| 243 | {"fd/1", "stdout"}, |
| 244 | {"fd/2", "stderr"}, |
| 245 | {NULL, NULL}, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 248 | static struct event_type { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 249 | unsigned int type; /* The DEVFSD_NOTIFY_* value */ |
| 250 | const char *config_name; /* The name used in the config file */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 251 | } event_types[] = { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 252 | {DEVFSD_NOTIFY_REGISTERED, "REGISTER"}, |
| 253 | {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"}, |
| 254 | {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"}, |
| 255 | {DEVFSD_NOTIFY_CLOSE, "CLOSE"}, |
| 256 | {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"}, |
| 257 | {DEVFSD_NOTIFY_CHANGE, "CHANGE"}, |
| 258 | {DEVFSD_NOTIFY_CREATE, "CREATE"}, |
| 259 | {DEVFSD_NOTIFY_DELETE, "DELETE"}, |
| 260 | {0xffffffff, NULL} |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 263 | /* Busybox messages */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 265 | static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision"; |
| 266 | static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s"; |
| 267 | static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small"; |
| 268 | static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found"; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 269 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 270 | /* Busybox stuff */ |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 271 | #if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG |
| 272 | #define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args) |
| 273 | #define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args) |
| 274 | #define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args) |
| 275 | #define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args) |
| 276 | #define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args) |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 277 | #else |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 278 | #define info_logger(p, fmt, args...) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 279 | #define msg_logger(p, fmt, args...) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 280 | #define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 281 | #define error_logger(p, fmt, args...) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 282 | #define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 283 | #endif |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 284 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 285 | static void safe_memcpy(char *dest, const char *src, int len) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 286 | { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 287 | memcpy(dest , src, len); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 288 | dest[len] = '\0'; |
| 289 | } |
| 290 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 291 | static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 292 | { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 293 | if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 294 | return 2 + addendum; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 295 | if (d[n - 2] == 'c' && d[n - 1] == 'd') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 296 | return 3 + addendum; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 297 | if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 298 | return 4 + addendum; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 299 | if (ptr[n - 2] == 'm' && ptr[n - 1] == 't') |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 300 | return 5 + addendum; |
| 301 | return 0; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 304 | static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 305 | { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 306 | if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') { |
| 307 | if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n' |
| 308 | && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c' |
| 309 | ) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 310 | return 1; |
| 311 | return scan_dev_name_common(d, n, 0, ptr); |
| 312 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 313 | if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/' |
| 314 | && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't' |
| 315 | ) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 316 | return scan_dev_name_common(d, n, 4, ptr); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 317 | if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/') |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 318 | return 10; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 319 | if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/') |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 320 | return 11; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 321 | if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/') |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 322 | return 12; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 326 | /* Public functions follow */ |
| 327 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 328 | int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 329 | int devfsd_main(int argc, char **argv) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 330 | { |
| 331 | int print_version = FALSE; |
| 332 | int do_daemon = TRUE; |
| 333 | int no_polling = FALSE; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 334 | int do_scan; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 335 | int fd, proto_rev, count; |
| 336 | unsigned long event_mask = 0; |
| 337 | struct sigaction new_action; |
| 338 | struct initial_symlink_struct *curr; |
| 339 | |
| 340 | if (argc < 2) |
| 341 | bb_show_usage(); |
| 342 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 343 | for (count = 2; count < argc; ++count) { |
| 344 | if (argv[count][0] == '-') { |
| 345 | if (argv[count][1] == 'v' && !argv[count][2]) /* -v */ |
Denis Vlasenko | 679b412 | 2007-07-01 18:18:54 +0000 | [diff] [blame] | 346 | print_version = TRUE; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 347 | else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f' |
Denis Vlasenko | 679b412 | 2007-07-01 18:18:54 +0000 | [diff] [blame] | 348 | && argv[count][2] == 'g' && !argv[count][3]) /* -fg */ |
| 349 | do_daemon = FALSE; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 350 | else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n' |
Denis Vlasenko | 679b412 | 2007-07-01 18:18:54 +0000 | [diff] [blame] | 351 | && argv[count][2] == 'p' && !argv[count][3]) /* -np */ |
| 352 | no_polling = TRUE; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 353 | else |
| 354 | bb_show_usage(); |
| 355 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 358 | mount_point = bb_simplify_path(argv[1]); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 360 | xchdir(mount_point); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 362 | fd = xopen(".devfsd", O_RDONLY); |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 363 | close_on_exec_on(fd); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 364 | xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 365 | |
| 366 | /*setup initial entries */ |
Denis Vlasenko | 679b412 | 2007-07-01 18:18:54 +0000 | [diff] [blame] | 367 | for (curr = initial_symlinks; curr->dest != NULL; ++curr) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 368 | symlink(curr->dest, curr->name); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 369 | |
| 370 | /* NB: The check for CONFIG_FILE is done in read_config_file() */ |
| 371 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 372 | if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) { |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 373 | printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n", |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 374 | applet_name, DEVFSD_VERSION, bb_msg_proto_rev, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 375 | DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 376 | if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 377 | bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 378 | exit(EXIT_SUCCESS); /* -v */ |
| 379 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 380 | /* Tell kernel we are special(i.e. we get to see hidden entries) */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 381 | xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 382 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 383 | /* Set up SIGHUP and SIGUSR1 handlers */ |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 384 | sigemptyset(&new_action.sa_mask); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 385 | new_action.sa_flags = 0; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 386 | new_action.sa_handler = signal_handler; |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 387 | sigaction_set(SIGHUP, &new_action); |
| 388 | sigaction_set(SIGUSR1, &new_action); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 390 | printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 391 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 392 | /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */ |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 393 | umask(0); |
| 394 | read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 395 | /* Do the scan before forking, so that boot scripts see the finished product */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 396 | dir_operation(SERVICE, mount_point, 0, NULL); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 397 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 398 | if (ENABLE_DEVFSD_FG_NP && no_polling) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 399 | exit(EXIT_SUCCESS); |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 400 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 401 | if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG) |
| 402 | logmode = LOGMODE_BOTH; |
| 403 | else if (do_daemon == TRUE) |
| 404 | logmode = LOGMODE_SYSLOG; |
| 405 | /* This is the default */ |
| 406 | /*else |
| 407 | logmode = LOGMODE_STDIO; */ |
| 408 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 409 | if (do_daemon) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 410 | /* Release so that the child can grab it */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 411 | xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 412 | bb_daemonize_or_rexec(0, argv); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 413 | } else if (ENABLE_DEVFSD_FG_NP) { |
| 414 | setpgid(0, 0); /* Become process group leader */ |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 415 | } |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 416 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 417 | while (TRUE) { |
| 418 | do_scan = do_servicing(fd, event_mask); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 419 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 420 | free_config(); |
| 421 | read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 422 | if (do_scan) |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 423 | dir_operation(SERVICE, mount_point, 0, NULL); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 424 | } |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 425 | if (ENABLE_FEATURE_CLEAN_UP) free(mount_point); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 426 | } /* End Function main */ |
| 427 | |
| 428 | |
| 429 | /* Private functions follow */ |
| 430 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 431 | static void read_config_file(char *path, int optional, unsigned long *event_mask) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 432 | /* [SUMMARY] Read a configuration database. |
| 433 | <path> The path to read the database from. If this is a directory, all |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 434 | entries in that directory will be read(except hidden entries). |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 435 | <optional> If TRUE, the routine will silently ignore a missing config file. |
| 436 | <event_mask> The event mask is written here. This is not initialised. |
| 437 | [RETURNS] Nothing. |
| 438 | */ |
| 439 | { |
| 440 | struct stat statbuf; |
| 441 | FILE *fp; |
| 442 | char buf[STRING_LENGTH]; |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 443 | char *line = NULL; |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 444 | char *p; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 446 | if (stat(path, &statbuf) == 0) { |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 447 | /* Don't read 0 length files: ignored */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 448 | /*if (statbuf.st_size == 0) |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 449 | return;*/ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 450 | if (S_ISDIR(statbuf.st_mode)) { |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 451 | p = bb_simplify_path(path); |
| 452 | dir_operation(READ_CONFIG, p, 0, event_mask); |
| 453 | free(p); |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 454 | return; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 455 | } |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 456 | fp = fopen_for_read(path); |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 457 | if (fp != NULL) { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 458 | while (fgets(buf, STRING_LENGTH, fp) != NULL) { |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 459 | /* Skip whitespace */ |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 460 | line = buf; |
| 461 | line = skip_whitespace(line); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 462 | if (line[0] == '\0' || line[0] == '#') |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 463 | continue; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 464 | process_config_line(line, event_mask); |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 465 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 466 | fclose(fp); |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 467 | } else { |
| 468 | goto read_config_file_err; |
| 469 | } |
| 470 | } else { |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 471 | read_config_file_err: |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 472 | if (optional == 0 && errno == ENOENT) |
| 473 | error_logger_and_die(LOG_ERR, "read config file: %s", path); |
Rob Landley | 06813d0 | 2005-06-07 03:47:00 +0000 | [diff] [blame] | 474 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 475 | } /* End Function read_config_file */ |
| 476 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 477 | static void process_config_line(const char *line, unsigned long *event_mask) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 478 | /* [SUMMARY] Process a line from a configuration file. |
| 479 | <line> The configuration line. |
| 480 | <event_mask> The event mask is written here. This is not initialised. |
| 481 | [RETURNS] Nothing. |
| 482 | */ |
| 483 | { |
| 484 | int num_args, count; |
| 485 | struct config_entry_struct *new; |
| 486 | char p[MAX_ARGS][STRING_LENGTH]; |
| 487 | char when[STRING_LENGTH], what[STRING_LENGTH]; |
| 488 | char name[STRING_LENGTH]; |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 489 | const char *msg = ""; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 490 | char *ptr; |
"Vladimir N. Oleynik" | 2f0a5f9 | 2005-12-06 12:00:39 +0000 | [diff] [blame] | 491 | int i; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 492 | |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 493 | /* !!!! Only Uppercase Keywords in devsfd.conf */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 494 | static const char options[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 495 | "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0" |
| 496 | "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0" |
| 497 | "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0" |
| 498 | "RMOLDCOMPAT\0""RMNEWCOMPAT\0"; |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 500 | for (count = 0; count < MAX_ARGS; ++count) |
| 501 | p[count][0] = '\0'; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 502 | num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s", |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 503 | when, name, what, |
| 504 | p[0], p[1], p[2], p[3], p[4], p[5], p[6]); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 505 | |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 506 | i = index_in_strings(options, when); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 507 | |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 508 | /* "CLEAR_CONFIG" */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 509 | if (i == 0) { |
| 510 | free_config(); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 511 | *event_mask = 0; |
| 512 | return; |
| 513 | } |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 514 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 515 | if (num_args < 2) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 516 | goto process_config_line_err; |
| 517 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 518 | /* "INCLUDE" & "OPTIONAL_INCLUDE" */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 519 | if (i == 1 || i == 2) { |
| 520 | st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL); |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 521 | info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 522 | read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 523 | return; |
| 524 | } |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 525 | /* "RESTORE" */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 526 | if (i == 3) { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 527 | dir_operation(RESTORE, name, strlen(name),NULL); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | if (num_args < 3) |
| 531 | goto process_config_line_err; |
| 532 | |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 533 | new = xzalloc(sizeof *new); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 534 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 535 | for (count = 0; event_types[count].config_name != NULL; ++count) { |
| 536 | if (strcasecmp(when, event_types[count].config_name) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 537 | continue; |
| 538 | new->action.when = event_types[count].type; |
| 539 | break; |
| 540 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 541 | if (event_types[count].config_name == NULL) { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 542 | msg = "WHEN in"; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 543 | goto process_config_line_err; |
| 544 | } |
| 545 | |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 546 | i = index_in_strings(options, what); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 547 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 548 | switch (i) { |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 549 | case 4: /* "PERMISSIONS" */ |
| 550 | new->action.what = AC_PERMISSIONS; |
| 551 | /* Get user and group */ |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 552 | ptr = strchr(p[0], '.'); |
| 553 | if (ptr == NULL) { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 554 | msg = "UID.GID"; |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 555 | goto process_config_line_err; /*"missing '.' in UID.GID"*/ |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | *ptr++ = '\0'; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 559 | new->u.permissions.uid = get_uid_gid(UID, p[0]); |
| 560 | new->u.permissions.gid = get_uid_gid(GID, ptr); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 561 | /* Get mode */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 562 | new->u.permissions.mode = get_mode(p[1]); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 563 | break; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 564 | case 5: /* MODLOAD */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 565 | /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 566 | the device name) to the module loading facility. In addition, |
| 567 | the /etc/modules.devfs configuration file is used.*/ |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 568 | if (ENABLE_DEVFSD_MODLOAD) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 569 | new->action.what = AC_MODLOAD; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 570 | break; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 571 | case 6: /* EXECUTE */ |
| 572 | new->action.what = AC_EXECUTE; |
| 573 | num_args -= 3; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 574 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 575 | for (count = 0; count < num_args; ++count) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 576 | new->u.execute.argv[count] = xstrdup(p[count]); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 577 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 578 | new->u.execute.argv[num_args] = NULL; |
| 579 | break; |
| 580 | case 7: /* COPY */ |
| 581 | new->action.what = AC_COPY; |
| 582 | num_args -= 3; |
| 583 | if (num_args != 2) |
| 584 | goto process_config_line_err; /* missing path and function in line */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 585 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 586 | new->u.copy.source = xstrdup(p[0]); |
| 587 | new->u.copy.destination = xstrdup(p[1]); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 588 | break; |
| 589 | case 8: /* IGNORE */ |
| 590 | /* FALLTROUGH */ |
| 591 | case 9: /* MKOLDCOMPAT */ |
| 592 | /* FALLTROUGH */ |
| 593 | case 10: /* MKNEWCOMPAT */ |
| 594 | /* FALLTROUGH */ |
| 595 | case 11:/* RMOLDCOMPAT */ |
| 596 | /* FALLTROUGH */ |
| 597 | case 12: /* RMNEWCOMPAT */ |
| 598 | /* AC_IGNORE 6 |
| 599 | AC_MKOLDCOMPAT 7 |
| 600 | AC_MKNEWCOMPAT 8 |
| 601 | AC_RMOLDCOMPAT 9 |
| 602 | AC_RMNEWCOMPAT 10*/ |
| 603 | new->action.what = i - 2; |
| 604 | break; |
| 605 | default: |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 606 | msg = "WHAT in"; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 607 | goto process_config_line_err; |
| 608 | /*esac*/ |
| 609 | } /* switch (i) */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 611 | xregcomp(&new->preg, name, REG_EXTENDED); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 612 | |
| 613 | *event_mask |= 1 << new->action.when; |
| 614 | new->next = NULL; |
| 615 | if (first_config == NULL) |
| 616 | first_config = new; |
| 617 | else |
| 618 | last_config->next = new; |
| 619 | last_config = new; |
| 620 | return; |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 621 | |
| 622 | process_config_line_err: |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 623 | msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 624 | } /* End Function process_config_line */ |
| 625 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 626 | static int do_servicing(int fd, unsigned long event_mask) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 627 | /* [SUMMARY] Service devfs changes until a signal is received. |
| 628 | <fd> The open control file. |
| 629 | <event_mask> The event mask. |
| 630 | [RETURNS] TRUE if SIGHUP was caught, else FALSE. |
| 631 | */ |
| 632 | { |
| 633 | ssize_t bytes; |
| 634 | struct devfsd_notify_struct info; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 635 | |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 636 | /* (void*) cast is only in order to match prototype */ |
| 637 | xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 638 | while (!caught_signal) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 639 | errno = 0; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 640 | bytes = read(fd,(char *) &info, sizeof info); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 641 | if (caught_signal) |
| 642 | break; /* Must test for this first */ |
| 643 | if (errno == EINTR) |
| 644 | continue; /* Yes, the order is important */ |
| 645 | if (bytes < 1) |
| 646 | break; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 647 | service_name(&info); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 648 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 649 | if (caught_signal) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 650 | int c_sighup = caught_sighup; |
| 651 | |
| 652 | caught_signal = FALSE; |
| 653 | caught_sighup = FALSE; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 654 | return c_sighup; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 655 | } |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 656 | msg_logger_and_die(LOG_ERR, "read error on control file"); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 657 | } /* End Function do_servicing */ |
| 658 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 659 | static void service_name(const struct devfsd_notify_struct *info) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 660 | /* [SUMMARY] Service a single devfs change. |
| 661 | <info> The devfs change. |
| 662 | [RETURNS] Nothing. |
| 663 | */ |
| 664 | { |
| 665 | unsigned int n; |
| 666 | regmatch_t mbuf[MAX_SUBEXPR]; |
| 667 | struct config_entry_struct *entry; |
| 668 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 669 | if (ENABLE_DEBUG && info->overrun_count > 0) |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 670 | msg_logger(LOG_ERR, "lost %u events", info->overrun_count); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 671 | |
| 672 | /* Discard lookups on "/dev/log" and "/dev/initctl" */ |
Denis Vlasenko | 49a128a | 2007-07-17 21:42:59 +0000 | [diff] [blame] | 673 | if (info->type == DEVFSD_NOTIFY_LOOKUP |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 674 | && ((info->devname[0] == 'l' && info->devname[1] == 'o' |
Denis Vlasenko | 49a128a | 2007-07-17 21:42:59 +0000 | [diff] [blame] | 675 | && info->devname[2] == 'g' && !info->devname[3]) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 676 | || (info->devname[0] == 'i' && info->devname[1] == 'n' |
| 677 | && info->devname[2] == 'i' && info->devname[3] == 't' |
| 678 | && info->devname[4] == 'c' && info->devname[5] == 't' |
| 679 | && info->devname[6] == 'l' && !info->devname[7])) |
| 680 | ) |
| 681 | return; |
| 682 | |
| 683 | for (entry = first_config; entry != NULL; entry = entry->next) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 684 | /* First check if action matches the type, then check if name matches */ |
Denis Vlasenko | 49a128a | 2007-07-17 21:42:59 +0000 | [diff] [blame] | 685 | if (info->type != entry->action.when |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 686 | || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 687 | continue; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 688 | for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 689 | /* VOID */; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 690 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 691 | switch (entry->action.what) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 692 | case AC_PERMISSIONS: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 693 | action_permissions(info, entry); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 694 | break; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 695 | case AC_MODLOAD: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 696 | if (ENABLE_DEVFSD_MODLOAD) |
| 697 | action_modload(info, entry); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 698 | break; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 699 | case AC_EXECUTE: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 700 | action_execute(info, entry, mbuf, n); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 701 | break; |
| 702 | case AC_COPY: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 703 | action_copy(info, entry, mbuf, n); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 704 | break; |
| 705 | case AC_IGNORE: |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 706 | return; |
| 707 | /*break;*/ |
| 708 | case AC_MKOLDCOMPAT: |
| 709 | case AC_MKNEWCOMPAT: |
| 710 | case AC_RMOLDCOMPAT: |
| 711 | case AC_RMNEWCOMPAT: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 712 | action_compat(info, entry->action.what); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 713 | break; |
| 714 | default: |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 715 | msg_logger_and_die(LOG_ERR, "Unknown action"); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | } /* End Function service_name */ |
| 719 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 720 | static void action_permissions(const struct devfsd_notify_struct *info, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 721 | const struct config_entry_struct *entry) |
| 722 | /* [SUMMARY] Update permissions for a device entry. |
| 723 | <info> The devfs change. |
| 724 | <entry> The config file entry. |
| 725 | [RETURNS] Nothing. |
| 726 | */ |
| 727 | { |
| 728 | struct stat statbuf; |
| 729 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 730 | if (stat(info->devname, &statbuf) != 0 |
| 731 | || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 |
| 732 | || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0 |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 733 | ) |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 734 | error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 735 | } /* End Function action_permissions */ |
| 736 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 737 | static void action_modload(const struct devfsd_notify_struct *info, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 738 | const struct config_entry_struct *entry UNUSED_PARAM) |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 739 | /* [SUMMARY] Load a module. |
| 740 | <info> The devfs change. |
| 741 | <entry> The config file entry. |
| 742 | [RETURNS] Nothing. |
| 743 | */ |
| 744 | { |
| 745 | char *argv[6]; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 746 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 747 | argv[0] = (char*)MODPROBE; |
| 748 | argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */ |
| 749 | argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */ |
| 750 | argv[3] = (char*)CONFIG_MODULES_DEVFS; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 751 | argv[4] = concat_path_file("/dev", info->devname); /* device */ |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 752 | argv[5] = NULL; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 753 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 754 | wait4pid(xspawn(argv)); |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 755 | free(argv[4]); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 756 | } /* End Function action_modload */ |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 757 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 758 | static void action_execute(const struct devfsd_notify_struct *info, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 759 | const struct config_entry_struct *entry, |
| 760 | const regmatch_t *regexpr, unsigned int numexpr) |
| 761 | /* [SUMMARY] Execute a programme. |
| 762 | <info> The devfs change. |
| 763 | <entry> The config file entry. |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 764 | <regexpr> The number of subexpression(start, end) offsets within the |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 765 | device name. |
| 766 | <numexpr> The number of elements within <<regexpr>>. |
| 767 | [RETURNS] Nothing. |
| 768 | */ |
| 769 | { |
| 770 | unsigned int count; |
| 771 | struct get_variable_info gv_info; |
| 772 | char *argv[MAX_ARGS + 1]; |
| 773 | char largv[MAX_ARGS + 1][STRING_LENGTH]; |
| 774 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 775 | gv_info.info = info; |
| 776 | gv_info.devname = info->devname; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 777 | snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); |
| 778 | for (count = 0; entry->u.execute.argv[count] != NULL; ++count) { |
| 779 | expand_expression(largv[count], STRING_LENGTH, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 780 | entry->u.execute.argv[count], |
| 781 | get_variable, &gv_info, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 782 | gv_info.devname, regexpr, numexpr); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 783 | argv[count] = largv[count]; |
| 784 | } |
| 785 | argv[count] = NULL; |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 786 | wait4pid(spawn(argv)); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 787 | } /* End Function action_execute */ |
| 788 | |
| 789 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 790 | static void action_copy(const struct devfsd_notify_struct *info, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 791 | const struct config_entry_struct *entry, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 792 | const regmatch_t *regexpr, unsigned int numexpr) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 793 | /* [SUMMARY] Copy permissions. |
| 794 | <info> The devfs change. |
| 795 | <entry> The config file entry. |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 796 | <regexpr> This list of subexpression(start, end) offsets within the |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 797 | device name. |
| 798 | <numexpr> The number of elements in <<regexpr>>. |
| 799 | [RETURNS] Nothing. |
| 800 | */ |
| 801 | { |
| 802 | mode_t new_mode; |
| 803 | struct get_variable_info gv_info; |
| 804 | struct stat source_stat, dest_stat; |
| 805 | char source[STRING_LENGTH], destination[STRING_LENGTH]; |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 806 | int ret = 0; |
| 807 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 808 | dest_stat.st_mode = 0; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 809 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 810 | if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode)) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 811 | return; |
| 812 | gv_info.info = info; |
| 813 | gv_info.devname = info->devname; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 814 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 815 | snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); |
| 816 | expand_expression(source, STRING_LENGTH, entry->u.copy.source, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 817 | get_variable, &gv_info, gv_info.devname, |
| 818 | regexpr, numexpr); |
| 819 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 820 | expand_expression(destination, STRING_LENGTH, entry->u.copy.destination, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 821 | get_variable, &gv_info, gv_info.devname, |
| 822 | regexpr, numexpr); |
| 823 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 824 | if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0) |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 825 | return; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 826 | lstat(destination, &dest_stat); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 827 | new_mode = source_stat.st_mode & ~S_ISVTX; |
| 828 | if (info->type == DEVFSD_NOTIFY_CREATE) |
| 829 | new_mode |= S_ISVTX; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 830 | else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX)) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 831 | new_mode |= S_ISVTX; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 832 | ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat); |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 833 | if (ENABLE_DEBUG && ret && (errno != EEXIST)) |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 834 | error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 835 | } /* End Function action_copy */ |
| 836 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 837 | static void action_compat(const struct devfsd_notify_struct *info, unsigned int action) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 838 | /* [SUMMARY] Process a compatibility request. |
| 839 | <info> The devfs change. |
| 840 | <action> The action to take. |
| 841 | [RETURNS] Nothing. |
| 842 | */ |
| 843 | { |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 844 | int ret; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 845 | const char *compat_name = NULL; |
| 846 | const char *dest_name = info->devname; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 847 | const char *ptr; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 848 | char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH]; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 849 | int mode, host, bus, target, lun; |
| 850 | unsigned int i; |
| 851 | char rewind_; |
| 852 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */ |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 853 | static const char *const fmt[] = { |
| 854 | NULL , |
| 855 | "sg/c%db%dt%du%d", /* scsi/generic */ |
| 856 | "sd/c%db%dt%du%d", /* scsi/disc */ |
| 857 | "sr/c%db%dt%du%d", /* scsi/cd */ |
| 858 | "sd/c%db%dt%du%dp%d", /* scsi/part */ |
| 859 | "st/c%db%dt%du%dm%d%c", /* scsi/mt */ |
| 860 | "ide/hd/c%db%dt%du%d", /* ide/host/disc */ |
| 861 | "ide/cd/c%db%dt%du%d", /* ide/host/cd */ |
| 862 | "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */ |
| 863 | "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */ |
| 864 | NULL |
| 865 | }; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 866 | |
| 867 | /* First construct compatibility name */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 868 | switch (action) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 869 | case AC_MKOLDCOMPAT: |
| 870 | case AC_RMOLDCOMPAT: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 871 | compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 872 | break; |
| 873 | case AC_MKNEWCOMPAT: |
| 874 | case AC_RMNEWCOMPAT: |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 875 | ptr = bb_basename(info->devname); |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 876 | i = scan_dev_name(info->devname, info->namelen, ptr); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 877 | |
| 878 | /* nothing found */ |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 879 | if (i == 0 || i > 9) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 880 | return; |
| 881 | |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 882 | sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun); |
| 883 | snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0)); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 884 | dest_name = dest_buf; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 885 | compat_name = compat_buf; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 886 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 887 | |
| 888 | /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 889 | if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7) |
| 890 | sprintf(compat_buf, fmt[i], host, bus, target, lun); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 891 | |
| 892 | /* 4 == scsi/part 8 == ide/host/part */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 893 | if (i == 4 || i == 8) |
| 894 | sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4)); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 895 | |
| 896 | /* 5 == scsi/mt */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 897 | if (i == 5) { |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 898 | rewind_ = info->devname[info->namelen - 1]; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 899 | if (rewind_ != 'n') |
| 900 | rewind_ = '\0'; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 901 | mode=0; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 902 | if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 903 | mode = ptr[2] - 107; /* 1 or 2 */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 904 | if (ptr[2] == 'a') |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 905 | mode = 3; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 906 | sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 907 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 908 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 909 | /* 9 == ide/host/mt */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 910 | if (i == 9) |
| 911 | snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 912 | /* esac */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 913 | } /* switch (action) */ |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 914 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 915 | if (compat_name == NULL) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 916 | return; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 917 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 918 | /* Now decide what to do with it */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 919 | switch (action) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 920 | case AC_MKOLDCOMPAT: |
| 921 | case AC_MKNEWCOMPAT: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 922 | mksymlink(dest_name, compat_name); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 923 | break; |
| 924 | case AC_RMOLDCOMPAT: |
| 925 | case AC_RMNEWCOMPAT: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 926 | ret = unlink(compat_name); |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 927 | if (ENABLE_DEBUG && ret) |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 928 | error_logger(LOG_ERR, "unlink: %s", compat_name); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 929 | break; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 930 | /*esac*/ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 931 | } /* switch (action) */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 932 | } /* End Function action_compat */ |
| 933 | |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 934 | static void restore(char *spath, struct stat source_stat, int rootlen) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 935 | { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 936 | char *dpath; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 937 | struct stat dest_stat; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 938 | |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 939 | dest_stat.st_mode = 0; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 940 | dpath = concat_path_file(mount_point, spath + rootlen); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 941 | lstat(dpath, &dest_stat); |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 942 | free(dpath); |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 943 | if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX)) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 944 | copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 945 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 946 | if (S_ISDIR(source_stat.st_mode)) |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 947 | dir_operation(RESTORE, spath, rootlen,NULL); |
| 948 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 949 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 950 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 951 | static int copy_inode(const char *destpath, const struct stat *dest_stat, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 952 | mode_t new_mode, |
| 953 | const char *sourcepath, const struct stat *source_stat) |
| 954 | /* [SUMMARY] Copy an inode. |
| 955 | <destpath> The destination path. An existing inode may be deleted. |
| 956 | <dest_stat> The destination stat(2) information. |
| 957 | <new_mode> The desired new mode for the destination. |
| 958 | <sourcepath> The source path. |
| 959 | <source_stat> The source stat(2) information. |
| 960 | [RETURNS] TRUE on success, else FALSE. |
| 961 | */ |
| 962 | { |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 963 | int source_len, dest_len; |
| 964 | char source_link[STRING_LENGTH], dest_link[STRING_LENGTH]; |
| 965 | int fd, val; |
| 966 | struct sockaddr_un un_addr; |
| 967 | char symlink_val[STRING_LENGTH]; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 968 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 969 | if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 970 | /* Same type */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 971 | if (S_ISLNK(source_stat->st_mode)) { |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 972 | source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1); |
| 973 | if ((source_len < 0) |
| 974 | || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0 |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 975 | ) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 976 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 977 | source_link[source_len] = '\0'; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 978 | dest_link[dest_len] = '\0'; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 979 | if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) { |
| 980 | unlink(destpath); |
| 981 | symlink(source_link, destpath); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 982 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 983 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 984 | } /* Else not a symlink */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 985 | chmod(destpath, new_mode & ~S_IFMT); |
| 986 | chown(destpath, source_stat->st_uid, source_stat->st_gid); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 987 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 988 | } |
| 989 | /* Different types: unlink and create */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 990 | unlink(destpath); |
| 991 | switch (source_stat->st_mode & S_IFMT) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 992 | case S_IFSOCK: |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 993 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 994 | if (fd < 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 995 | break; |
| 996 | un_addr.sun_family = AF_UNIX; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 997 | snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath); |
| 998 | val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr); |
| 999 | close(fd); |
| 1000 | if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1001 | break; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1002 | goto do_chown; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1003 | case S_IFLNK: |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1004 | val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1); |
| 1005 | if (val < 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1006 | break; |
| 1007 | symlink_val[val] = '\0'; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1008 | if (symlink(symlink_val, destpath) == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1009 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1010 | break; |
| 1011 | case S_IFREG: |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1012 | fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT); |
| 1013 | if (fd < 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1014 | break; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1015 | close(fd); |
| 1016 | if (chmod(destpath, new_mode & ~S_IFMT) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1017 | break; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1018 | goto do_chown; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1019 | case S_IFBLK: |
| 1020 | case S_IFCHR: |
| 1021 | case S_IFIFO: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1022 | if (mknod(destpath, new_mode, source_stat->st_rdev) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1023 | break; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1024 | goto do_chown; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1025 | case S_IFDIR: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1026 | if (mkdir(destpath, new_mode & ~S_IFMT) != 0) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1027 | break; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1028 | do_chown: |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1029 | if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1030 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1031 | /*break;*/ |
| 1032 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1033 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1034 | } /* End Function copy_inode */ |
| 1035 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1036 | static void free_config(void) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1037 | /* [SUMMARY] Free the configuration information. |
| 1038 | [RETURNS] Nothing. |
| 1039 | */ |
| 1040 | { |
| 1041 | struct config_entry_struct *c_entry; |
| 1042 | void *next; |
| 1043 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1044 | for (c_entry = first_config; c_entry != NULL; c_entry = next) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1045 | unsigned int count; |
| 1046 | |
| 1047 | next = c_entry->next; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1048 | regfree(&c_entry->preg); |
| 1049 | if (c_entry->action.what == AC_EXECUTE) { |
| 1050 | for (count = 0; count < MAX_ARGS; ++count) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1051 | if (c_entry->u.execute.argv[count] == NULL) |
| 1052 | break; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1053 | free(c_entry->u.execute.argv[count]); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1056 | free(c_entry); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1057 | } |
| 1058 | first_config = NULL; |
| 1059 | last_config = NULL; |
| 1060 | } /* End Function free_config */ |
| 1061 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1062 | static int get_uid_gid(int flag, const char *string) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1063 | /* [SUMMARY] Convert a string to a UID or GID value. |
| 1064 | <flag> "UID" or "GID". |
| 1065 | <string> The string. |
| 1066 | [RETURNS] The UID or GID value. |
| 1067 | */ |
| 1068 | { |
| 1069 | struct passwd *pw_ent; |
| 1070 | struct group *grp_ent; |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 1071 | static const char *msg; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1072 | |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1073 | if (ENABLE_DEVFSD_VERBOSE) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1074 | msg = "user"; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1075 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1076 | if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1]))) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1077 | return atoi(string); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1078 | |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1079 | if (flag == UID && (pw_ent = getpwnam(string)) != NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1080 | return pw_ent->pw_uid; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1081 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1082 | if (flag == GID && (grp_ent = getgrnam(string)) != NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1083 | return grp_ent->gr_gid; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1084 | else if (ENABLE_DEVFSD_VERBOSE) |
| 1085 | msg = "group"; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1086 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1087 | if (ENABLE_DEVFSD_VERBOSE) |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 1088 | msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1089 | return 0; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1090 | }/* End Function get_uid_gid */ |
| 1091 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1092 | static mode_t get_mode(const char *string) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1093 | /* [SUMMARY] Convert a string to a mode value. |
| 1094 | <string> The string. |
| 1095 | [RETURNS] The mode value. |
| 1096 | */ |
| 1097 | { |
| 1098 | mode_t mode; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1099 | int i; |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1100 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1101 | if (isdigit(string[0])) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1102 | return strtoul(string, NULL, 8); |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1103 | if (strlen(string) != 9) |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1104 | msg_logger_and_die(LOG_ERR, "bad mode: %s", string); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1105 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1106 | mode = 0; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1107 | i = S_IRUSR; |
| 1108 | while (i > 0) { |
| 1109 | if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x') |
| 1110 | mode += i; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1111 | i = i / 2; |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1112 | string++; |
| 1113 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1114 | return mode; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1115 | } /* End Function get_mode */ |
| 1116 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1117 | static void signal_handler(int sig) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1118 | { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1119 | caught_signal = TRUE; |
| 1120 | if (sig == SIGHUP) |
| 1121 | caught_sighup = TRUE; |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1122 | |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1123 | info_logger(LOG_INFO, "Caught signal %d", sig); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1124 | } /* End Function signal_handler */ |
| 1125 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1126 | static const char *get_variable(const char *variable, void *info) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1127 | { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1128 | static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 1129 | static char *hostname; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1130 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1131 | struct get_variable_info *gv_info = info; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1132 | const char *field_names[] = { |
| 1133 | "hostname", "mntpt", "devpath", "devname", |
| 1134 | "uid", "gid", "mode", hostname, mount_point, |
| 1135 | gv_info->devpath, gv_info->devname, NULL |
| 1136 | }; |
"Vladimir N. Oleynik" | 2f0a5f9 | 2005-12-06 12:00:39 +0000 | [diff] [blame] | 1137 | int i; |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1138 | |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 1139 | if (!hostname) |
| 1140 | hostname = safe_gethostname(); |
Denis Vlasenko | 5af906e | 2006-11-05 18:05:09 +0000 | [diff] [blame] | 1141 | /* index_in_str_array returns i>=0 */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1142 | i = index_in_str_array(field_names, variable); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1143 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1144 | if (i > 6 || i < 0 || (i > 1 && gv_info == NULL)) |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1145 | return NULL; |
| 1146 | if (i >= 0 && i <= 3) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1147 | return field_names[i + 7]; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1148 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1149 | if (i == 4) |
| 1150 | sprintf(sbuf, "%u", gv_info->info->uid); |
| 1151 | else if (i == 5) |
| 1152 | sprintf(sbuf, "%u", gv_info->info->gid); |
| 1153 | else if (i == 6) |
| 1154 | sprintf(sbuf, "%o", gv_info->info->mode); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1155 | return sbuf; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1156 | } /* End Function get_variable */ |
| 1157 | |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1158 | static void service(struct stat statbuf, char *path) |
| 1159 | { |
| 1160 | struct devfsd_notify_struct info; |
| 1161 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1162 | memset(&info, 0, sizeof info); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1163 | info.type = DEVFSD_NOTIFY_REGISTERED; |
| 1164 | info.mode = statbuf.st_mode; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1165 | info.major = major(statbuf.st_rdev); |
| 1166 | info.minor = minor(statbuf.st_rdev); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1167 | info.uid = statbuf.st_uid; |
| 1168 | info.gid = statbuf.st_gid; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1169 | snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1); |
| 1170 | info.namelen = strlen(info.devname); |
| 1171 | service_name(&info); |
| 1172 | if (S_ISDIR(statbuf.st_mode)) |
| 1173 | dir_operation(SERVICE, path, 0, NULL); |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1177 | /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes. |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1178 | <flag> To choose which function to perform |
| 1179 | <dp> The directory pointer. This is closed upon completion. |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1180 | <dir_name> The name of the directory. |
Glenn L McGrath | 3860b2e | 2003-11-30 23:46:06 +0000 | [diff] [blame] | 1181 | <rootlen> string length parameter. |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1182 | [RETURNS] Nothing. |
| 1183 | */ |
| 1184 | { |
| 1185 | struct stat statbuf; |
| 1186 | DIR *dp; |
| 1187 | struct dirent *de; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1188 | char *path; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1189 | |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1190 | dp = warn_opendir(dir_name); |
| 1191 | if (dp == NULL) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1194 | while ((de = readdir(dp)) != NULL) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1195 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1196 | if (de->d_name && DOT_OR_DOTDOT(de->d_name)) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1197 | continue; |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1198 | path = concat_path_file(dir_name, de->d_name); |
| 1199 | if (lstat(path, &statbuf) == 0) { |
| 1200 | switch (type) { |
| 1201 | case SERVICE: |
| 1202 | service(statbuf, path); |
| 1203 | break; |
| 1204 | case RESTORE: |
| 1205 | restore(path, statbuf, var); |
| 1206 | break; |
| 1207 | case READ_CONFIG: |
| 1208 | read_config_file(path, var, event_mask); |
| 1209 | break; |
| 1210 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1211 | } |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1212 | free(path); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1213 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1214 | closedir(dp); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1215 | } /* End Function do_scan_and_service */ |
| 1216 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1217 | static int mksymlink(const char *oldpath, const char *newpath) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1218 | /* [SUMMARY] Create a symlink, creating intervening directories as required. |
| 1219 | <oldpath> The string contained in the symlink. |
| 1220 | <newpath> The name of the new symlink. |
| 1221 | [RETURNS] 0 on success, else -1. |
| 1222 | */ |
| 1223 | { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1224 | if (!make_dir_tree(newpath)) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1225 | return -1; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1226 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1227 | if (symlink(oldpath, newpath) != 0) { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1228 | if (errno != EEXIST) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1229 | return -1; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1230 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1231 | return 0; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1232 | } /* End Function mksymlink */ |
| 1233 | |
| 1234 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1235 | static int make_dir_tree(const char *path) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1236 | /* [SUMMARY] Creating intervening directories for a path as required. |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1237 | <path> The full pathname(including the leaf node). |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1238 | [RETURNS] TRUE on success, else FALSE. |
| 1239 | */ |
| 1240 | { |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1241 | if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1242 | return FALSE; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1243 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1244 | } /* End Function make_dir_tree */ |
| 1245 | |
| 1246 | static int expand_expression(char *output, unsigned int outsize, |
| 1247 | const char *input, |
| 1248 | const char *(*get_variable_func)(const char *variable, void *info), |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1249 | void *info, |
| 1250 | const char *devname, |
| 1251 | const regmatch_t *ex, unsigned int numexp) |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1252 | /* [SUMMARY] Expand environment variables and regular subexpressions in string. |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1253 | <output> The output expanded expression is written here. |
| 1254 | <length> The size of the output buffer. |
| 1255 | <input> The input expression. This may equal <<output>>. |
| 1256 | <get_variable> A function which will be used to get variable values. If |
| 1257 | this returns NULL, the environment is searched instead. If this is NULL, |
| 1258 | only the environment is searched. |
| 1259 | <info> An arbitrary pointer passed to <<get_variable>>. |
| 1260 | <devname> Device name; specifically, this is the string that contains all |
| 1261 | of the regular subexpressions. |
| 1262 | <ex> Array of start / end offsets into info->devname for each subexpression |
| 1263 | <numexp> Number of regular subexpressions found in <<devname>>. |
| 1264 | [RETURNS] TRUE on success, else FALSE. |
| 1265 | */ |
| 1266 | { |
| 1267 | char temp[STRING_LENGTH]; |
| 1268 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1269 | if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info)) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1270 | return FALSE; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1271 | expand_regexp(output, outsize, temp, devname, ex, numexp); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1272 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1273 | } /* End Function expand_expression */ |
| 1274 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1275 | static void expand_regexp(char *output, size_t outsize, const char *input, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1276 | const char *devname, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1277 | const regmatch_t *ex, unsigned int numex) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1278 | /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9. |
| 1279 | <output> The output expanded expression is written here. |
| 1280 | <outsize> The size of the output buffer. |
| 1281 | <input> The input expression. This may NOT equal <<output>>, because |
| 1282 | supporting that would require yet another string-copy. However, it's not |
| 1283 | hard to write a simple wrapper function to add this functionality for those |
| 1284 | few cases that need it. |
| 1285 | <devname> Device name; specifically, this is the string that contains all |
| 1286 | of the regular subexpressions. |
| 1287 | <ex> An array of start and end offsets into <<devname>>, one for each |
| 1288 | subexpression |
| 1289 | <numex> Number of subexpressions in the offset-array <<ex>>. |
| 1290 | [RETURNS] Nothing. |
| 1291 | */ |
| 1292 | { |
| 1293 | const char last_exp = '0' - 1 + numex; |
| 1294 | int c = -1; |
| 1295 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1296 | /* Guarantee NULL termination by writing an explicit '\0' character into |
| 1297 | the very last byte */ |
| 1298 | if (outsize) |
| 1299 | output[--outsize] = '\0'; |
| 1300 | /* Copy the input string into the output buffer, replacing '\\' with '\' |
| 1301 | and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x |
| 1302 | codes are deleted */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1303 | while ((c != '\0') && (outsize != 0)) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1304 | c = *input; |
| 1305 | ++input; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1306 | if (c == '\\') { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1307 | c = *input; |
| 1308 | ++input; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1309 | if (c != '\\') { |
| 1310 | if ((c >= '0') && (c <= last_exp)) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1311 | const regmatch_t *subexp = ex + (c - '0'); |
| 1312 | unsigned int sublen = subexp->rm_eo - subexp->rm_so; |
| 1313 | |
| 1314 | /* Range checking */ |
| 1315 | if (sublen > outsize) |
| 1316 | sublen = outsize; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1317 | strncpy(output, devname + subexp->rm_so, sublen); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1318 | output += sublen; |
| 1319 | outsize -= sublen; |
| 1320 | } |
| 1321 | continue; |
| 1322 | } |
| 1323 | } |
| 1324 | *output = c; |
| 1325 | ++output; |
| 1326 | --outsize; |
| 1327 | } /* while */ |
| 1328 | } /* End Function expand_regexp */ |
| 1329 | |
| 1330 | |
| 1331 | /* from compat_name.c */ |
| 1332 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1333 | struct translate_struct { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1334 | const char *match; /* The string to match to(up to length) */ |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 1335 | const char *format; /* Format of output, "%s" takes data past match string, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1336 | NULL is effectively "%s"(just more efficient) */ |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1337 | }; |
| 1338 | |
| 1339 | static struct translate_struct translate_table[] = |
| 1340 | { |
| 1341 | {"sound/", NULL}, |
| 1342 | {"printers/", "lp%s"}, |
| 1343 | {"v4l/", NULL}, |
| 1344 | {"parports/", "parport%s"}, |
| 1345 | {"fb/", "fb%s"}, |
| 1346 | {"netlink/", NULL}, |
| 1347 | {"loop/", "loop%s"}, |
| 1348 | {"floppy/", "fd%s"}, |
| 1349 | {"rd/", "ram%s"}, |
| 1350 | {"md/", "md%s"}, /* Meta-devices */ |
| 1351 | {"vc/", "tty%s"}, |
| 1352 | {"misc/", NULL}, |
| 1353 | {"isdn/", NULL}, |
| 1354 | {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/ |
| 1355 | {"i2c/", "i2c-%s"}, |
| 1356 | {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */ |
| 1357 | {"tts/E", "ttyE%s"}, /* Stallion serial driver */ |
| 1358 | {"cua/E", "cue%s"}, /* Stallion serial driver callout */ |
| 1359 | {"tts/R", "ttyR%s"}, /* Rocketport serial driver */ |
| 1360 | {"cua/R", "cur%s"}, /* Rocketport serial driver callout */ |
| 1361 | {"ip2/", "ip2%s"}, /* Computone serial driver control */ |
| 1362 | {"tts/F", "ttyF%s"}, /* Computone serial driver */ |
| 1363 | {"cua/F", "cuf%s"}, /* Computone serial driver callout */ |
| 1364 | {"tts/C", "ttyC%s"}, /* Cyclades serial driver */ |
| 1365 | {"cua/C", "cub%s"}, /* Cyclades serial driver callout */ |
| 1366 | {"tts/", "ttyS%s"}, /* Generic serial: must be after others */ |
| 1367 | {"cua/", "cua%s"}, /* Generic serial: must be after others */ |
| 1368 | {"input/js", "js%s"}, /* Joystick driver */ |
| 1369 | {NULL, NULL} |
| 1370 | }; |
| 1371 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1372 | const char *get_old_name(const char *devname, unsigned int namelen, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1373 | char *buffer, unsigned int major, unsigned int minor) |
| 1374 | /* [SUMMARY] Translate a kernel-supplied name into an old name. |
| 1375 | <devname> The device name provided by the kernel. |
| 1376 | <namelen> The length of the name. |
| 1377 | <buffer> A buffer that may be used. This should be at least 128 bytes long. |
| 1378 | <major> The major number for the device. |
| 1379 | <minor> The minor number for the device. |
| 1380 | [RETURNS] A pointer to the old name if known, else NULL. |
| 1381 | */ |
| 1382 | { |
| 1383 | const char *compat_name = NULL; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 1384 | const char *ptr; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1385 | struct translate_struct *trans; |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1386 | unsigned int i; |
| 1387 | char mode; |
| 1388 | int indexx; |
| 1389 | const char *pty1; |
| 1390 | const char *pty2; |
| 1391 | size_t len; |
| 1392 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */ |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 1393 | static const char *const fmt[] = { |
| 1394 | NULL , |
| 1395 | "sg%u", /* scsi/generic */ |
| 1396 | NULL, /* scsi/disc */ |
| 1397 | "sr%u", /* scsi/cd */ |
| 1398 | NULL, /* scsi/part */ |
| 1399 | "nst%u%c", /* scsi/mt */ |
| 1400 | "hd%c" , /* ide/host/disc */ |
| 1401 | "hd%c" , /* ide/host/cd */ |
| 1402 | "hd%c%s", /* ide/host/part */ |
| 1403 | "%sht%d", /* ide/host/mt */ |
| 1404 | "sbpcd%u", /* sbp/ */ |
| 1405 | "vcs%s", /* vcc/ */ |
| 1406 | "%cty%c%c", /* pty/ */ |
| 1407 | NULL |
| 1408 | }; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1409 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1410 | for (trans = translate_table; trans->match != NULL; ++trans) { |
| 1411 | len = strlen(trans->match); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1412 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1413 | if (strncmp(devname, trans->match, len) == 0) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1414 | if (trans->format == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1415 | return devname + len; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1416 | sprintf(buffer, trans->format, devname + len); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1417 | return buffer; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1420 | |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 1421 | ptr = bb_basename(devname); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1422 | i = scan_dev_name(devname, namelen, ptr); |
| 1423 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1424 | if (i > 0 && i < 13) |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1425 | compat_name = buffer; |
| 1426 | else |
| 1427 | return NULL; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1428 | |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1429 | /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1430 | if (i == 1 || i == 3 || i == 10) |
| 1431 | sprintf(buffer, fmt[i], minor); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1432 | |
| 1433 | /* 2 ==scsi/disc, 4 == scsi/part */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1434 | if (i == 2 || i == 4) |
Denis Vlasenko | c965f4b | 2007-06-27 00:20:38 +0000 | [diff] [blame] | 1435 | compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4))); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* 5 == scsi/mt */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1438 | if (i == 5) { |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1439 | mode = ptr[2]; |
| 1440 | if (mode == 'n') |
| 1441 | mode = '\0'; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1442 | sprintf(buffer, fmt[i], minor & 0x1f, mode); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1443 | if (devname[namelen - 1] != 'n') |
| 1444 | ++compat_name; |
| 1445 | } |
| 1446 | /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1447 | if (i == 6 || i == 7 || i == 8) |
Rob Landley | 1ba19d6 | 2005-10-08 17:42:35 +0000 | [diff] [blame] | 1448 | /* last arg should be ignored for i == 6 or i== 7 */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1449 | sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* 9 == ide/host/mt */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1452 | if (i == 9) |
| 1453 | sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f); |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1454 | |
| 1455 | /* 11 == vcc/ */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1456 | if (i == 11) { |
| 1457 | sprintf(buffer, fmt[i], devname + 4); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1458 | if (buffer[3] == '0') |
| 1459 | buffer[3] = '\0'; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1460 | } |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1461 | /* 12 == pty/ */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1462 | if (i == 12) { |
Eric Andersen | f18bd89 | 2003-12-19 11:07:59 +0000 | [diff] [blame] | 1463 | pty1 = "pqrstuvwxyzabcde"; |
| 1464 | pty2 = "0123456789abcdef"; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1465 | indexx = atoi(devname + 5); |
| 1466 | sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1467 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1468 | return compat_name; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1469 | } /* End Function get_old_name */ |
| 1470 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1471 | static char get_old_ide_name(unsigned int major, unsigned int minor) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1472 | /* [SUMMARY] Get the old IDE name for a device. |
| 1473 | <major> The major number for the device. |
| 1474 | <minor> The minor number for the device. |
| 1475 | [RETURNS] The drive letter. |
| 1476 | */ |
| 1477 | { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1478 | char letter = 'y'; /* 121 */ |
| 1479 | char c = 'a'; /* 97 */ |
| 1480 | int i = IDE0_MAJOR; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1481 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1482 | /* I hope it works like the previous code as it saves a few bytes. Tito ;P */ |
| 1483 | do { |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1484 | if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR |
| 1485 | || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR |
| 1486 | || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR |
| 1487 | || i == IDE9_MAJOR |
| 1488 | ) { |
| 1489 | if ((unsigned int)i == major) { |
| 1490 | letter = c; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1491 | break; |
| 1492 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1493 | c += 2; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1494 | } |
| 1495 | i++; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1496 | } while (i <= IDE9_MAJOR); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1497 | |
| 1498 | if (minor > 63) |
| 1499 | ++letter; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1500 | return letter; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1501 | } /* End Function get_old_ide_name */ |
| 1502 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1503 | static char *write_old_sd_name(char *buffer, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1504 | unsigned int major, unsigned int minor, |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 1505 | const char *part) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1506 | /* [SUMMARY] Write the old SCSI disc name to a buffer. |
| 1507 | <buffer> The buffer to write to. |
| 1508 | <major> The major number for the device. |
| 1509 | <minor> The minor number for the device. |
| 1510 | <part> The partition string. Must be "" for a whole-disc entry. |
| 1511 | [RETURNS] A pointer to the buffer on success, else NULL. |
| 1512 | */ |
| 1513 | { |
| 1514 | unsigned int disc_index; |
| 1515 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1516 | if (major == 8) { |
| 1517 | sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1518 | return buffer; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1519 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1520 | if ((major > 64) && (major < 72)) { |
| 1521 | disc_index = ((major - 64) << 4) +(minor >> 4); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1522 | if (disc_index < 26) |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1523 | sprintf(buffer, "sd%c%s", 'a' + disc_index, part); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1524 | else |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1525 | sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1526 | return buffer; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1527 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1528 | return NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1529 | } /* End Function write_old_sd_name */ |
| 1530 | |
| 1531 | |
| 1532 | /* expression.c */ |
| 1533 | |
| 1534 | /*EXPERIMENTAL_FUNCTION*/ |
| 1535 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1536 | int st_expr_expand(char *output, unsigned int length, const char *input, |
| 1537 | const char *(*get_variable_func)(const char *variable, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1538 | void *info), |
| 1539 | void *info) |
| 1540 | /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules. |
| 1541 | <output> The output expanded expression is written here. |
| 1542 | <length> The size of the output buffer. |
| 1543 | <input> The input expression. This may equal <<output>>. |
| 1544 | <get_variable> A function which will be used to get variable values. If |
| 1545 | this returns NULL, the environment is searched instead. If this is NULL, |
| 1546 | only the environment is searched. |
| 1547 | <info> An arbitrary pointer passed to <<get_variable>>. |
| 1548 | [RETURNS] TRUE on success, else FALSE. |
| 1549 | */ |
| 1550 | { |
| 1551 | char ch; |
| 1552 | unsigned int len; |
| 1553 | unsigned int out_pos = 0; |
| 1554 | const char *env; |
| 1555 | const char *ptr; |
| 1556 | struct passwd *pwent; |
| 1557 | char buffer[BUFFER_SIZE], tmp[STRING_LENGTH]; |
| 1558 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1559 | if (length > BUFFER_SIZE) |
| 1560 | length = BUFFER_SIZE; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1561 | for (; TRUE; ++input) { |
| 1562 | switch (ch = *input) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1563 | case '$': |
| 1564 | /* Variable expansion */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1565 | input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1566 | if (input == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1567 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | case '~': |
| 1570 | /* Home directory expansion */ |
| 1571 | ch = input[1]; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1572 | if (isspace(ch) ||(ch == '/') ||(ch == '\0')) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1573 | /* User's own home directory: leave separator for next time */ |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1574 | env = getenv("HOME"); |
| 1575 | if (env == NULL) { |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1576 | info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1577 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1578 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1579 | len = strlen(env); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1580 | if (len + out_pos >= length) |
| 1581 | goto st_expr_expand_out; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1582 | memcpy(buffer + out_pos, env, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1583 | out_pos += len; |
| 1584 | continue; |
| 1585 | } |
| 1586 | /* Someone else's home directory */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1587 | for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr) |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 1588 | /* VOID */; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1589 | len = ptr - input; |
| 1590 | if (len >= sizeof tmp) |
| 1591 | goto st_expr_expand_out; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1592 | safe_memcpy(tmp, input, len); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1593 | input = ptr - 1; |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1594 | pwent = getpwnam(tmp); |
| 1595 | if (pwent == NULL) { |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1596 | info_logger(LOG_INFO, "no pwent for: %s", tmp); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1597 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1598 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1599 | len = strlen(pwent->pw_dir); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1600 | if (len + out_pos >= length) |
| 1601 | goto st_expr_expand_out; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1602 | memcpy(buffer + out_pos, pwent->pw_dir, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1603 | out_pos += len; |
| 1604 | break; |
| 1605 | case '\0': |
| 1606 | /* Falltrough */ |
| 1607 | default: |
| 1608 | if (out_pos >= length) |
| 1609 | goto st_expr_expand_out; |
| 1610 | buffer[out_pos++] = ch; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1611 | if (ch == '\0') { |
| 1612 | memcpy(output, buffer, out_pos); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1613 | return TRUE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1614 | } |
| 1615 | break; |
| 1616 | /* esac */ |
| 1617 | } |
| 1618 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1619 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1620 | st_expr_expand_out: |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1621 | info_logger(LOG_INFO, bb_msg_small_buffer); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1622 | return FALSE; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1623 | } /* End Function st_expr_expand */ |
| 1624 | |
| 1625 | |
| 1626 | /* Private functions follow */ |
| 1627 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1628 | static const char *expand_variable(char *buffer, unsigned int length, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1629 | unsigned int *out_pos, const char *input, |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1630 | const char *(*func)(const char *variable, |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1631 | void *info), |
| 1632 | void *info) |
| 1633 | /* [SUMMARY] Expand a variable. |
| 1634 | <buffer> The buffer to write to. |
| 1635 | <length> The length of the output buffer. |
| 1636 | <out_pos> The current output position. This is updated. |
| 1637 | <input> A pointer to the input character pointer. |
| 1638 | <func> A function which will be used to get variable values. If this |
| 1639 | returns NULL, the environment is searched instead. If this is NULL, only |
| 1640 | the environment is searched. |
| 1641 | <info> An arbitrary pointer passed to <<func>>. |
| 1642 | <errfp> Diagnostic messages are written here. |
| 1643 | [RETURNS] A pointer to the end of this subexpression on success, else NULL. |
| 1644 | */ |
| 1645 | { |
| 1646 | char ch; |
| 1647 | int len; |
| 1648 | unsigned int open_braces; |
| 1649 | const char *env, *ptr; |
| 1650 | char tmp[STRING_LENGTH]; |
| 1651 | |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1652 | ch = input[0]; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1653 | if (ch == '$') { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1654 | /* Special case for "$$": PID */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1655 | sprintf(tmp, "%d",(int) getpid()); |
| 1656 | len = strlen(tmp); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1657 | if (len + *out_pos >= length) |
| 1658 | goto expand_variable_out; |
| 1659 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1660 | memcpy(buffer + *out_pos, tmp, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1661 | out_pos += len; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1662 | return input; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1663 | } |
| 1664 | /* Ordinary variable expansion, possibly in braces */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1665 | if (ch != '{') { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1666 | /* Simple variable expansion */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1667 | for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr) |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 1668 | /* VOID */; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1669 | len = ptr - input; |
"Vladimir N. Oleynik" | 73ffd76 | 2006-02-01 12:56:19 +0000 | [diff] [blame] | 1670 | if ((size_t)len >= sizeof tmp) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1671 | goto expand_variable_out; |
| 1672 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1673 | safe_memcpy(tmp, input, len); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1674 | input = ptr - 1; |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1675 | env = get_variable_v2(tmp, func, info); |
| 1676 | if (env == NULL) { |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1677 | info_logger(LOG_INFO, bb_msg_variable_not_found, tmp); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1678 | return NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1679 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1680 | len = strlen(env); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1681 | if (len + *out_pos >= length) |
| 1682 | goto expand_variable_out; |
| 1683 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1684 | memcpy(buffer + *out_pos, env, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1685 | *out_pos += len; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1686 | return input; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1687 | } |
| 1688 | /* Variable in braces: check for ':' tricks */ |
| 1689 | ch = *++input; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1690 | for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1691 | /* VOID */; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1692 | if (ch == '}') { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1693 | /* Must be simple variable expansion with "${var}" */ |
| 1694 | len = ptr - input; |
"Vladimir N. Oleynik" | 73ffd76 | 2006-02-01 12:56:19 +0000 | [diff] [blame] | 1695 | if ((size_t)len >= sizeof tmp) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1696 | goto expand_variable_out; |
| 1697 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1698 | safe_memcpy(tmp, input, len); |
| 1699 | ptr = expand_variable(buffer, length, out_pos, tmp, func, info); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1700 | if (ptr == NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1701 | return NULL; |
| 1702 | return input + len; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1703 | } |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1704 | if (ch != ':' || ptr[1] != '-') { |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1705 | info_logger(LOG_INFO, "illegal char in var name"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1706 | return NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1707 | } |
| 1708 | /* It's that handy "${var:-word}" expression. Check if var is defined */ |
| 1709 | len = ptr - input; |
"Vladimir N. Oleynik" | 73ffd76 | 2006-02-01 12:56:19 +0000 | [diff] [blame] | 1710 | if ((size_t)len >= sizeof tmp) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1711 | goto expand_variable_out; |
| 1712 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1713 | safe_memcpy(tmp, input, len); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1714 | /* Move input pointer to ':' */ |
| 1715 | input = ptr; |
| 1716 | /* First skip to closing brace, taking note of nested expressions */ |
| 1717 | ptr += 2; |
| 1718 | ch = ptr[0]; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1719 | for (open_braces = 1; open_braces > 0; ch = *++ptr) { |
| 1720 | switch (ch) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1721 | case '{': |
| 1722 | ++open_braces; |
| 1723 | break; |
| 1724 | case '}': |
| 1725 | --open_braces; |
| 1726 | break; |
| 1727 | case '\0': |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 1728 | info_logger(LOG_INFO, "\"}\" not found in: %s", input); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1729 | return NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1730 | default: |
| 1731 | break; |
| 1732 | } |
| 1733 | } |
| 1734 | --ptr; |
| 1735 | /* At this point ptr should point to closing brace of "${var:-word}" */ |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 1736 | env = get_variable_v2(tmp, func, info); |
| 1737 | if (env != NULL) { |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1738 | /* Found environment variable, so skip the input to the closing brace |
| 1739 | and return the variable */ |
| 1740 | input = ptr; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1741 | len = strlen(env); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1742 | if (len + *out_pos >= length) |
| 1743 | goto expand_variable_out; |
| 1744 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1745 | memcpy(buffer + *out_pos, env, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1746 | *out_pos += len; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1747 | return input; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1748 | } |
| 1749 | /* Environment variable was not found, so process word. Advance input |
| 1750 | pointer to start of word in "${var:-word}" */ |
| 1751 | input += 2; |
| 1752 | len = ptr - input; |
"Vladimir N. Oleynik" | 73ffd76 | 2006-02-01 12:56:19 +0000 | [diff] [blame] | 1753 | if ((size_t)len >= sizeof tmp) |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1754 | goto expand_variable_out; |
| 1755 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1756 | safe_memcpy(tmp, input, len); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1757 | input = ptr; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1758 | if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info)) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1759 | return NULL; |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1760 | len = strlen(tmp); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1761 | if (len + *out_pos >= length) |
| 1762 | goto expand_variable_out; |
| 1763 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1764 | memcpy(buffer + *out_pos, tmp, len + 1); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1765 | *out_pos += len; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1766 | return input; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1767 | expand_variable_out: |
Denis Vlasenko | 10aea3e | 2007-07-01 22:25:33 +0000 | [diff] [blame] | 1768 | info_logger(LOG_INFO, bb_msg_small_buffer); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1769 | return NULL; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1770 | } /* End Function expand_variable */ |
| 1771 | |
| 1772 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1773 | static const char *get_variable_v2(const char *variable, |
| 1774 | const char *(*func)(const char *variable, void *info), |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1775 | void *info) |
| 1776 | /* [SUMMARY] Get a variable from the environment or . |
| 1777 | <variable> The variable name. |
| 1778 | <func> A function which will be used to get the variable. If this returns |
| 1779 | NULL, the environment is searched instead. If this is NULL, only the |
| 1780 | environment is searched. |
| 1781 | [RETURNS] The value of the variable on success, else NULL. |
| 1782 | */ |
| 1783 | { |
| 1784 | const char *value; |
| 1785 | |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 1786 | if (func != NULL) { |
| 1787 | value = (*func)(variable, info); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1788 | if (value != NULL) |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1789 | return value; |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1790 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1791 | return getenv(variable); |
Glenn L McGrath | 17d21fa | 2003-10-09 11:46:23 +0000 | [diff] [blame] | 1792 | } /* End Function get_variable */ |
| 1793 | |
| 1794 | /* END OF CODE */ |