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