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