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