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