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