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