blob: 5e3ee23b3a8ba99ad9ccac93646e26ab1613f829 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002/*
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6/*
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00007 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*/
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000056#include "libbb.h"
57#include "xregex.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000058#include <syslog.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000059
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000060#include <sys/un.h>
61#include <sys/sysmacros.h>
Eric Andersen2af70002003-10-09 21:02:23 +000062
63/* Various defines taken from linux/major.h */
64#define IDE0_MAJOR 3
65#define IDE1_MAJOR 22
66#define IDE2_MAJOR 33
67#define IDE3_MAJOR 34
68#define IDE4_MAJOR 56
69#define IDE5_MAJOR 57
70#define IDE6_MAJOR 88
71#define IDE7_MAJOR 89
72#define IDE8_MAJOR 90
73#define IDE9_MAJOR 91
74
75
76/* Various defines taken from linux/devfs_fs.h */
77#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
78#define DEVFSD_IOCTL_BASE 'd'
79/* These are the various ioctls */
80#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
81#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
82#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000083#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000084#define DEVFSD_NOTIFY_REGISTERED 0
85#define DEVFSD_NOTIFY_UNREGISTERED 1
86#define DEVFSD_NOTIFY_ASYNC_OPEN 2
87#define DEVFSD_NOTIFY_CLOSE 3
88#define DEVFSD_NOTIFY_LOOKUP 4
89#define DEVFSD_NOTIFY_CHANGE 5
90#define DEVFSD_NOTIFY_CREATE 6
91#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +000092#define DEVFS_PATHLEN 1024
93/* Never change this otherwise the binary interface will change */
94
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020095struct devfsd_notify_struct {
96 /* Use native C types to ensure same types in kernel and user space */
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000097 unsigned int type; /* DEVFSD_NOTIFY_* value */
98 unsigned int mode; /* Mode of the inode or device entry */
99 unsigned int major; /* Major number of device entry */
100 unsigned int minor; /* Minor number of device entry */
101 unsigned int uid; /* Uid of process, inode or device entry */
102 unsigned int gid; /* Gid of process, inode or device entry */
103 unsigned int overrun_count; /* Number of lost events */
104 unsigned int namelen; /* Number of characters not including '\0' */
105 /* The device name MUST come last */
106 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
Eric Andersen2af70002003-10-09 21:02:23 +0000107};
108
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000109#define BUFFER_SIZE 16384
110#define DEVFSD_VERSION "1.3.25"
111#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000112#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000113#define MODPROBE_SWITCH_1 "-k"
114#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000115#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000116#define MAX_ARGS (6 + 1)
117#define MAX_SUBEXPR 10
118#define STRING_LENGTH 255
119
120/* for get_uid_gid() */
121#define UID 0
122#define GID 1
123
Rob Landley1ba19d62005-10-08 17:42:35 +0000124/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000125# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000126# define NO_DIE 0
127
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000128/* for dir_operation() */
129#define RESTORE 0
130#define SERVICE 1
131#define READ_CONFIG 2
132
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000133/* Update only after changing code to reflect new protocol */
134#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
135
136/* Compile-time check */
137#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
138#error protocol version mismatch. Update your kernel headers
139#endif
140
141#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000142#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000143#define AC_EXECUTE 2
144#define AC_MFUNCTION 3 /* not supported by busybox */
145#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000146#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000147#define AC_IGNORE 6
148#define AC_MKOLDCOMPAT 7
149#define AC_MKNEWCOMPAT 8
150#define AC_RMOLDCOMPAT 9
151#define AC_RMNEWCOMPAT 10
152#define AC_RESTORE 11
153
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200154struct permissions_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000155 mode_t mode;
156 uid_t uid;
157 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000158};
159
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200160struct execute_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000161 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000162};
163
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200164struct copy_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000165 const char *source;
166 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000167};
168
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200169struct action_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000170 unsigned int what;
171 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000172};
173
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200174struct config_entry_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000175 struct action_type action;
176 regex_t preg;
177 union
178 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000179 struct permissions_type permissions;
180 struct execute_type execute;
181 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000182 }
183 u;
184 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000185};
186
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200187struct get_variable_info {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000188 const struct devfsd_notify_struct *info;
189 const char *devname;
190 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000191};
192
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000193static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000194static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000195static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000196static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000197static int mksymlink(const char *oldpath, const char *newpath);
198static void read_config_file(char *path, int optional, unsigned long *event_mask);
199static void process_config_line(const char *, unsigned long *);
200static int do_servicing(int, unsigned long);
201static void service_name(const struct devfsd_notify_struct *);
202static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
203static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000204 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000205static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
206static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000207 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000208static void action_compat(const struct devfsd_notify_struct *, unsigned);
209static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000210static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000211static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
212static mode_t get_mode(const char *);
213static void signal_handler(int);
214static const char *get_variable(const char *, void *);
215static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000216static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000217 const char *, const regmatch_t *, unsigned);
218static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000219static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000220 const char *(*)(const char *, void *), void *);
221static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
222static char get_old_ide_name(unsigned , unsigned);
223static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000224
225/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000226static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000227static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000228static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
229static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000230
231/* Structs and vars */
232static struct config_entry_struct *first_config = NULL;
233static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000234static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000235static volatile int caught_signal = FALSE;
236static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000237static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000238 const char *dest;
239 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000240} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000241 {"/proc/self/fd", "fd"},
242 {"fd/0", "stdin"},
243 {"fd/1", "stdout"},
244 {"fd/2", "stderr"},
245 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000246};
247
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000248static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000249 unsigned int type; /* The DEVFSD_NOTIFY_* value */
250 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000251} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000252 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
253 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
254 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
255 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
256 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
257 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
258 {DEVFSD_NOTIFY_CREATE, "CREATE"},
259 {DEVFSD_NOTIFY_DELETE, "DELETE"},
260 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000261};
262
Rob Landley1ba19d62005-10-08 17:42:35 +0000263/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000264
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000265static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
266static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
267static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
268static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000269
Rob Landley1ba19d62005-10-08 17:42:35 +0000270/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000271#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
272#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
273#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
274#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
275#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
276#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000277#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000278#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000279#define msg_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000280#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000281#define error_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000282#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Eric Andersenf18bd892003-12-19 11:07:59 +0000283#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000284
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000285static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000286{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000287 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000288 dest[len] = '\0';
289}
290
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000291static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000292{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000293 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000294 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000295 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000296 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000297 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000298 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000299 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000300 return 5 + addendum;
301 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000302}
303
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000304static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000305{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000306 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
307 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
308 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
309 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000310 return 1;
311 return scan_dev_name_common(d, n, 0, ptr);
312 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000313 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
314 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
315 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000316 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000317 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000318 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000319 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000320 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000321 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000322 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000323 return 0;
324}
325
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000326/* Public functions follow */
327
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000328int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000329int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000330{
331 int print_version = FALSE;
332 int do_daemon = TRUE;
333 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000334 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000335 int fd, proto_rev, count;
336 unsigned long event_mask = 0;
337 struct sigaction new_action;
338 struct initial_symlink_struct *curr;
339
340 if (argc < 2)
341 bb_show_usage();
342
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000343 for (count = 2; count < argc; ++count) {
344 if (argv[count][0] == '-') {
345 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000346 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000347 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000348 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
349 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000350 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000351 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
352 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000353 else
354 bb_show_usage();
355 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000356 }
357
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000358 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000359
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000360 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000361
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000362 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000363 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000364 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000365
366 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000367 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000368 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000369
370 /* NB: The check for CONFIG_FILE is done in read_config_file() */
371
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000372 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000373 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000374 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000375 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000376 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000377 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000378 exit(EXIT_SUCCESS); /* -v */
379 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000380 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000381 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000382
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000383 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000384 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000385 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000386 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000387 sigaction_set(SIGHUP, &new_action);
388 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000389
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000390 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000391
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000392 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000393 umask(0);
394 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000395 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000396 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000397
Rob Landley1ba19d62005-10-08 17:42:35 +0000398 if (ENABLE_DEVFSD_FG_NP && no_polling)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000399 exit(EXIT_SUCCESS);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000400
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000401 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
402 logmode = LOGMODE_BOTH;
403 else if (do_daemon == TRUE)
404 logmode = LOGMODE_SYSLOG;
405 /* This is the default */
406 /*else
407 logmode = LOGMODE_STDIO; */
408
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000409 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000410 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000411 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000412 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000413 } else if (ENABLE_DEVFSD_FG_NP) {
414 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000415 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000416
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000417 while (TRUE) {
418 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000419
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000420 free_config();
421 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000422 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000423 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000424 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000425 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000426} /* End Function main */
427
428
429/* Private functions follow */
430
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000431static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000432/* [SUMMARY] Read a configuration database.
433 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000434 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000435 <optional> If TRUE, the routine will silently ignore a missing config file.
436 <event_mask> The event mask is written here. This is not initialised.
437 [RETURNS] Nothing.
438*/
439{
440 struct stat statbuf;
441 FILE *fp;
442 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000443 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000444 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000445
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000446 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000447 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000448 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000449 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000450 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000451 p = bb_simplify_path(path);
452 dir_operation(READ_CONFIG, p, 0, event_mask);
453 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000454 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000455 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000456 fp = fopen_for_read(path);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000457 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000458 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000459 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000460 line = buf;
461 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000462 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000463 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000464 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000465 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000466 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000467 } else {
468 goto read_config_file_err;
469 }
470 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000471read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000472 if (optional == 0 && errno == ENOENT)
473 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000474 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000475} /* End Function read_config_file */
476
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000477static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000478/* [SUMMARY] Process a line from a configuration file.
479 <line> The configuration line.
480 <event_mask> The event mask is written here. This is not initialised.
481 [RETURNS] Nothing.
482*/
483{
484 int num_args, count;
485 struct config_entry_struct *new;
486 char p[MAX_ARGS][STRING_LENGTH];
487 char when[STRING_LENGTH], what[STRING_LENGTH];
488 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000489 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000490 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000491 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000492
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000493 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000494 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000495 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
496 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
497 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
498 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000499
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000500 for (count = 0; count < MAX_ARGS; ++count)
501 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000502 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000503 when, name, what,
504 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000505
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000506 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000507
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000508 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000509 if (i == 0) {
510 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000511 *event_mask = 0;
512 return;
513 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000514
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000515 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000516 goto process_config_line_err;
517
Eric Andersenf18bd892003-12-19 11:07:59 +0000518 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000519 if (i == 1 || i == 2) {
520 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000521 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000522 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000523 return;
524 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000525 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000526 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000527 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000528 return;
529 }
530 if (num_args < 3)
531 goto process_config_line_err;
532
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000533 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000534
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000535 for (count = 0; event_types[count].config_name != NULL; ++count) {
536 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000537 continue;
538 new->action.when = event_types[count].type;
539 break;
540 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000541 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000542 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000543 goto process_config_line_err;
544 }
545
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000546 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000547
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000548 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000549 case 4: /* "PERMISSIONS" */
550 new->action.what = AC_PERMISSIONS;
551 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000552 ptr = strchr(p[0], '.');
553 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000554 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000555 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000556 }
557
558 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000559 new->u.permissions.uid = get_uid_gid(UID, p[0]);
560 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000561 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000562 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000563 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000564 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000565 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000566 the device name) to the module loading facility. In addition,
567 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000568 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000569 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000570 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000571 case 6: /* EXECUTE */
572 new->action.what = AC_EXECUTE;
573 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000574
Eric Andersenf18bd892003-12-19 11:07:59 +0000575 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000576 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000577
Eric Andersenf18bd892003-12-19 11:07:59 +0000578 new->u.execute.argv[num_args] = NULL;
579 break;
580 case 7: /* COPY */
581 new->action.what = AC_COPY;
582 num_args -= 3;
583 if (num_args != 2)
584 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000585
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000586 new->u.copy.source = xstrdup(p[0]);
587 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000588 break;
589 case 8: /* IGNORE */
590 /* FALLTROUGH */
591 case 9: /* MKOLDCOMPAT */
592 /* FALLTROUGH */
593 case 10: /* MKNEWCOMPAT */
594 /* FALLTROUGH */
595 case 11:/* RMOLDCOMPAT */
596 /* FALLTROUGH */
597 case 12: /* RMNEWCOMPAT */
598 /* AC_IGNORE 6
599 AC_MKOLDCOMPAT 7
600 AC_MKNEWCOMPAT 8
601 AC_RMOLDCOMPAT 9
602 AC_RMNEWCOMPAT 10*/
603 new->action.what = i - 2;
604 break;
605 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000606 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000607 goto process_config_line_err;
608 /*esac*/
609 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000610
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000611 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000612
613 *event_mask |= 1 << new->action.when;
614 new->next = NULL;
615 if (first_config == NULL)
616 first_config = new;
617 else
618 last_config->next = new;
619 last_config = new;
620 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000621
622 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000623 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000624} /* End Function process_config_line */
625
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000626static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000627/* [SUMMARY] Service devfs changes until a signal is received.
628 <fd> The open control file.
629 <event_mask> The event mask.
630 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
631*/
632{
633 ssize_t bytes;
634 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000635
Denis Vlasenko856be772007-08-17 08:29:48 +0000636 /* (void*) cast is only in order to match prototype */
637 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000638 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000639 errno = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000640 bytes = read(fd,(char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000641 if (caught_signal)
642 break; /* Must test for this first */
643 if (errno == EINTR)
644 continue; /* Yes, the order is important */
645 if (bytes < 1)
646 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000647 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000648 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000649 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000650 int c_sighup = caught_sighup;
651
652 caught_signal = FALSE;
653 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000654 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000655 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000656 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000657} /* End Function do_servicing */
658
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000659static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000660/* [SUMMARY] Service a single devfs change.
661 <info> The devfs change.
662 [RETURNS] Nothing.
663*/
664{
665 unsigned int n;
666 regmatch_t mbuf[MAX_SUBEXPR];
667 struct config_entry_struct *entry;
668
Rob Landley1ba19d62005-10-08 17:42:35 +0000669 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000670 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000671
672 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000673 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000674 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000675 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000676 || (info->devname[0] == 'i' && info->devname[1] == 'n'
677 && info->devname[2] == 'i' && info->devname[3] == 't'
678 && info->devname[4] == 'c' && info->devname[5] == 't'
679 && info->devname[6] == 'l' && !info->devname[7]))
680 )
681 return;
682
683 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000684 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000685 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000686 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000687 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000688 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000689 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000690
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000691 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000692 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000693 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000694 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000695 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000696 if (ENABLE_DEVFSD_MODLOAD)
697 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000698 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000700 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000701 break;
702 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000703 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000704 break;
705 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000706 return;
707 /*break;*/
708 case AC_MKOLDCOMPAT:
709 case AC_MKNEWCOMPAT:
710 case AC_RMOLDCOMPAT:
711 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000712 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000713 break;
714 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000715 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000716 }
717 }
718} /* End Function service_name */
719
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000720static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000721 const struct config_entry_struct *entry)
722/* [SUMMARY] Update permissions for a device entry.
723 <info> The devfs change.
724 <entry> The config file entry.
725 [RETURNS] Nothing.
726*/
727{
728 struct stat statbuf;
729
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000730 if (stat(info->devname, &statbuf) != 0
731 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
732 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000733 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000734 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000735} /* End Function action_permissions */
736
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000737static void action_modload(const struct devfsd_notify_struct *info,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000738 const struct config_entry_struct *entry UNUSED_PARAM)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000739/* [SUMMARY] Load a module.
740 <info> The devfs change.
741 <entry> The config file entry.
742 [RETURNS] Nothing.
743*/
744{
745 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000746
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000747 argv[0] = (char*)MODPROBE;
748 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
749 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
750 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000751 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000752 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000753
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000754 wait4pid(xspawn(argv));
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000755 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000756} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000757
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000758static void action_execute(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000759 const struct config_entry_struct *entry,
760 const regmatch_t *regexpr, unsigned int numexpr)
761/* [SUMMARY] Execute a programme.
762 <info> The devfs change.
763 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000764 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000765 device name.
766 <numexpr> The number of elements within <<regexpr>>.
767 [RETURNS] Nothing.
768*/
769{
770 unsigned int count;
771 struct get_variable_info gv_info;
772 char *argv[MAX_ARGS + 1];
773 char largv[MAX_ARGS + 1][STRING_LENGTH];
774
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000775 gv_info.info = info;
776 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000777 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
778 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
779 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000780 entry->u.execute.argv[count],
781 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000782 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000783 argv[count] = largv[count];
784 }
785 argv[count] = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000786 wait4pid(spawn(argv));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000787} /* End Function action_execute */
788
789
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000790static void action_copy(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000791 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000792 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000793/* [SUMMARY] Copy permissions.
794 <info> The devfs change.
795 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000796 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000797 device name.
798 <numexpr> The number of elements in <<regexpr>>.
799 [RETURNS] Nothing.
800*/
801{
802 mode_t new_mode;
803 struct get_variable_info gv_info;
804 struct stat source_stat, dest_stat;
805 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000806 int ret = 0;
807
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000808 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000809
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000810 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000811 return;
812 gv_info.info = info;
813 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000814
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000815 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
816 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000817 get_variable, &gv_info, gv_info.devname,
818 regexpr, numexpr);
819
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000820 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000821 get_variable, &gv_info, gv_info.devname,
822 regexpr, numexpr);
823
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000824 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000825 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000826 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000827 new_mode = source_stat.st_mode & ~S_ISVTX;
828 if (info->type == DEVFSD_NOTIFY_CREATE)
829 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000830 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000831 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000832 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000833 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000834 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000835} /* End Function action_copy */
836
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000837static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000838/* [SUMMARY] Process a compatibility request.
839 <info> The devfs change.
840 <action> The action to take.
841 [RETURNS] Nothing.
842*/
843{
Rob Landley1ba19d62005-10-08 17:42:35 +0000844 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000845 const char *compat_name = NULL;
846 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000847 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000848 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000849 int mode, host, bus, target, lun;
850 unsigned int i;
851 char rewind_;
852 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000853 static const char *const fmt[] = {
854 NULL ,
855 "sg/c%db%dt%du%d", /* scsi/generic */
856 "sd/c%db%dt%du%d", /* scsi/disc */
857 "sr/c%db%dt%du%d", /* scsi/cd */
858 "sd/c%db%dt%du%dp%d", /* scsi/part */
859 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
860 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
861 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
862 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
863 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
864 NULL
865 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000866
867 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000868 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000869 case AC_MKOLDCOMPAT:
870 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000871 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000872 break;
873 case AC_MKNEWCOMPAT:
874 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000875 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000876 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000877
878 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000879 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000880 return;
881
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000882 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
883 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000884 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000885 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000886
Eric Andersenf18bd892003-12-19 11:07:59 +0000887
888 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000889 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
890 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000891
892 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000893 if (i == 4 || i == 8)
894 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000895
896 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000897 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000898 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000899 if (rewind_ != 'n')
900 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000901 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000902 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000903 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000904 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000905 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000906 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000907 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000908
Eric Andersenf18bd892003-12-19 11:07:59 +0000909 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000910 if (i == 9)
911 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000912 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000913 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000914
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000915 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000916 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000917
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000918 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000919 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000920 case AC_MKOLDCOMPAT:
921 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000922 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000923 break;
924 case AC_RMOLDCOMPAT:
925 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000926 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +0000927 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000928 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000929 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000930 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000931 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000932} /* End Function action_compat */
933
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000934static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000935{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000936 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000937 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000938
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000939 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000940 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000941 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000942 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000943 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000944 copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000945
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000946 if (S_ISDIR(source_stat.st_mode))
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000947 dir_operation(RESTORE, spath, rootlen,NULL);
948}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000949
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000951static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000952 mode_t new_mode,
953 const char *sourcepath, const struct stat *source_stat)
954/* [SUMMARY] Copy an inode.
955 <destpath> The destination path. An existing inode may be deleted.
956 <dest_stat> The destination stat(2) information.
957 <new_mode> The desired new mode for the destination.
958 <sourcepath> The source path.
959 <source_stat> The source stat(2) information.
960 [RETURNS] TRUE on success, else FALSE.
961*/
962{
Eric Andersenf18bd892003-12-19 11:07:59 +0000963 int source_len, dest_len;
964 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
965 int fd, val;
966 struct sockaddr_un un_addr;
967 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000968
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000969 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000970 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000971 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000972 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
973 if ((source_len < 0)
974 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000975 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000976 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000977 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000978 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000979 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
980 unlink(destpath);
981 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000982 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000983 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000984 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000985 chmod(destpath, new_mode & ~S_IFMT);
986 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000987 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000988 }
989 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000990 unlink(destpath);
991 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000992 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000993 fd = socket(AF_UNIX, SOCK_STREAM, 0);
994 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000995 break;
996 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000997 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
998 val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
999 close(fd);
1000 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001001 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001002 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001003 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001004 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1005 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001006 break;
1007 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001008 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001009 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001010 break;
1011 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001012 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1013 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001014 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001015 close(fd);
1016 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001017 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001018 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001019 case S_IFBLK:
1020 case S_IFCHR:
1021 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001022 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001023 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001024 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001025 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001026 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001027 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001028do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001029 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001030 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001031 /*break;*/
1032 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001033 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001034} /* End Function copy_inode */
1035
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001036static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001037/* [SUMMARY] Free the configuration information.
1038 [RETURNS] Nothing.
1039*/
1040{
1041 struct config_entry_struct *c_entry;
1042 void *next;
1043
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001044 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001045 unsigned int count;
1046
1047 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001048 regfree(&c_entry->preg);
1049 if (c_entry->action.what == AC_EXECUTE) {
1050 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001051 if (c_entry->u.execute.argv[count] == NULL)
1052 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001053 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001054 }
1055 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001056 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001057 }
1058 first_config = NULL;
1059 last_config = NULL;
1060} /* End Function free_config */
1061
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001062static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001063/* [SUMMARY] Convert a string to a UID or GID value.
1064 <flag> "UID" or "GID".
1065 <string> The string.
1066 [RETURNS] The UID or GID value.
1067*/
1068{
1069 struct passwd *pw_ent;
1070 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001071 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001072
Rob Landley1ba19d62005-10-08 17:42:35 +00001073 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001074 msg = "user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001075
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001076 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001077 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001078
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001079 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001080 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001081
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001082 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001083 return grp_ent->gr_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001084 else if (ENABLE_DEVFSD_VERBOSE)
1085 msg = "group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001086
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001087 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001088 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001089 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001090}/* End Function get_uid_gid */
1091
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001092static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001093/* [SUMMARY] Convert a string to a mode value.
1094 <string> The string.
1095 [RETURNS] The mode value.
1096*/
1097{
1098 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001099 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001100
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001101 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001102 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001103 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001104 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001105
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001106 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001107 i = S_IRUSR;
1108 while (i > 0) {
1109 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1110 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001111 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001112 string++;
1113 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001114 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001115} /* End Function get_mode */
1116
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001117static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001118{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001119 caught_signal = TRUE;
1120 if (sig == SIGHUP)
1121 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001122
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001123 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001124} /* End Function signal_handler */
1125
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001126static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001127{
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001128 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001129 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001130
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001131 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001132 const char *field_names[] = {
1133 "hostname", "mntpt", "devpath", "devname",
1134 "uid", "gid", "mode", hostname, mount_point,
1135 gv_info->devpath, gv_info->devname, NULL
1136 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001137 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001138
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001139 if (!hostname)
1140 hostname = safe_gethostname();
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001141 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001142 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001143
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001144 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001145 return NULL;
1146 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001147 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001148
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001149 if (i == 4)
1150 sprintf(sbuf, "%u", gv_info->info->uid);
1151 else if (i == 5)
1152 sprintf(sbuf, "%u", gv_info->info->gid);
1153 else if (i == 6)
1154 sprintf(sbuf, "%o", gv_info->info->mode);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001155 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001156} /* End Function get_variable */
1157
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001158static void service(struct stat statbuf, char *path)
1159{
1160 struct devfsd_notify_struct info;
1161
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001162 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001163 info.type = DEVFSD_NOTIFY_REGISTERED;
1164 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001165 info.major = major(statbuf.st_rdev);
1166 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001167 info.uid = statbuf.st_uid;
1168 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001169 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1170 info.namelen = strlen(info.devname);
1171 service_name(&info);
1172 if (S_ISDIR(statbuf.st_mode))
1173 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001174}
1175
1176static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001177/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001178 <flag> To choose which function to perform
1179 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001180 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001181 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001182 [RETURNS] Nothing.
1183*/
1184{
1185 struct stat statbuf;
1186 DIR *dp;
1187 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001188 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001189
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001190 dp = warn_opendir(dir_name);
1191 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001192 return;
1193
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001194 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001196 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001197 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001198 path = concat_path_file(dir_name, de->d_name);
1199 if (lstat(path, &statbuf) == 0) {
1200 switch (type) {
1201 case SERVICE:
1202 service(statbuf, path);
1203 break;
1204 case RESTORE:
1205 restore(path, statbuf, var);
1206 break;
1207 case READ_CONFIG:
1208 read_config_file(path, var, event_mask);
1209 break;
1210 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001211 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001212 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001213 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001214 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001215} /* End Function do_scan_and_service */
1216
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001217static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001218/* [SUMMARY] Create a symlink, creating intervening directories as required.
1219 <oldpath> The string contained in the symlink.
1220 <newpath> The name of the new symlink.
1221 [RETURNS] 0 on success, else -1.
1222*/
1223{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001224 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001225 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001226
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001227 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001228 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001229 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001230 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001231 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001232} /* End Function mksymlink */
1233
1234
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001235static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001236/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001237 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001238 [RETURNS] TRUE on success, else FALSE.
1239*/
1240{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001241 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001242 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001243 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001244} /* End Function make_dir_tree */
1245
1246static int expand_expression(char *output, unsigned int outsize,
1247 const char *input,
1248 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001249 void *info,
1250 const char *devname,
1251 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001252/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001253 <output> The output expanded expression is written here.
1254 <length> The size of the output buffer.
1255 <input> The input expression. This may equal <<output>>.
1256 <get_variable> A function which will be used to get variable values. If
1257 this returns NULL, the environment is searched instead. If this is NULL,
1258 only the environment is searched.
1259 <info> An arbitrary pointer passed to <<get_variable>>.
1260 <devname> Device name; specifically, this is the string that contains all
1261 of the regular subexpressions.
1262 <ex> Array of start / end offsets into info->devname for each subexpression
1263 <numexp> Number of regular subexpressions found in <<devname>>.
1264 [RETURNS] TRUE on success, else FALSE.
1265*/
1266{
1267 char temp[STRING_LENGTH];
1268
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001269 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001270 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001271 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001272 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001273} /* End Function expand_expression */
1274
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001275static void expand_regexp(char *output, size_t outsize, const char *input,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001276 const char *devname,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001277 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001278/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1279 <output> The output expanded expression is written here.
1280 <outsize> The size of the output buffer.
1281 <input> The input expression. This may NOT equal <<output>>, because
1282 supporting that would require yet another string-copy. However, it's not
1283 hard to write a simple wrapper function to add this functionality for those
1284 few cases that need it.
1285 <devname> Device name; specifically, this is the string that contains all
1286 of the regular subexpressions.
1287 <ex> An array of start and end offsets into <<devname>>, one for each
1288 subexpression
1289 <numex> Number of subexpressions in the offset-array <<ex>>.
1290 [RETURNS] Nothing.
1291*/
1292{
1293 const char last_exp = '0' - 1 + numex;
1294 int c = -1;
1295
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001296 /* Guarantee NULL termination by writing an explicit '\0' character into
1297 the very last byte */
1298 if (outsize)
1299 output[--outsize] = '\0';
1300 /* Copy the input string into the output buffer, replacing '\\' with '\'
1301 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1302 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001303 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001304 c = *input;
1305 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001306 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001307 c = *input;
1308 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001309 if (c != '\\') {
1310 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001311 const regmatch_t *subexp = ex + (c - '0');
1312 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1313
1314 /* Range checking */
1315 if (sublen > outsize)
1316 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001317 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001318 output += sublen;
1319 outsize -= sublen;
1320 }
1321 continue;
1322 }
1323 }
1324 *output = c;
1325 ++output;
1326 --outsize;
1327 } /* while */
1328} /* End Function expand_regexp */
1329
1330
1331/* from compat_name.c */
1332
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +02001333struct translate_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001334 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001335 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001336 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001337};
1338
1339static struct translate_struct translate_table[] =
1340{
1341 {"sound/", NULL},
1342 {"printers/", "lp%s"},
1343 {"v4l/", NULL},
1344 {"parports/", "parport%s"},
1345 {"fb/", "fb%s"},
1346 {"netlink/", NULL},
1347 {"loop/", "loop%s"},
1348 {"floppy/", "fd%s"},
1349 {"rd/", "ram%s"},
1350 {"md/", "md%s"}, /* Meta-devices */
1351 {"vc/", "tty%s"},
1352 {"misc/", NULL},
1353 {"isdn/", NULL},
1354 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1355 {"i2c/", "i2c-%s"},
1356 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1357 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1358 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1359 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1360 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1361 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1362 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1363 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1364 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1365 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1366 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1367 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1368 {"input/js", "js%s"}, /* Joystick driver */
1369 {NULL, NULL}
1370};
1371
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001372const char *get_old_name(const char *devname, unsigned int namelen,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001373 char *buffer, unsigned int major, unsigned int minor)
1374/* [SUMMARY] Translate a kernel-supplied name into an old name.
1375 <devname> The device name provided by the kernel.
1376 <namelen> The length of the name.
1377 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1378 <major> The major number for the device.
1379 <minor> The minor number for the device.
1380 [RETURNS] A pointer to the old name if known, else NULL.
1381*/
1382{
1383 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001384 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001385 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001386 unsigned int i;
1387 char mode;
1388 int indexx;
1389 const char *pty1;
1390 const char *pty2;
1391 size_t len;
1392 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001393 static const char *const fmt[] = {
1394 NULL ,
1395 "sg%u", /* scsi/generic */
1396 NULL, /* scsi/disc */
1397 "sr%u", /* scsi/cd */
1398 NULL, /* scsi/part */
1399 "nst%u%c", /* scsi/mt */
1400 "hd%c" , /* ide/host/disc */
1401 "hd%c" , /* ide/host/cd */
1402 "hd%c%s", /* ide/host/part */
1403 "%sht%d", /* ide/host/mt */
1404 "sbpcd%u", /* sbp/ */
1405 "vcs%s", /* vcc/ */
1406 "%cty%c%c", /* pty/ */
1407 NULL
1408 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001409
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001410 for (trans = translate_table; trans->match != NULL; ++trans) {
1411 len = strlen(trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001412
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001413 if (strncmp(devname, trans->match, len) == 0) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001414 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001415 return devname + len;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001416 sprintf(buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001417 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001418 }
1419 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001420
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001421 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001422 i = scan_dev_name(devname, namelen, ptr);
1423
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001424 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001425 compat_name = buffer;
1426 else
1427 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001428
Eric Andersenf18bd892003-12-19 11:07:59 +00001429 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001430 if (i == 1 || i == 3 || i == 10)
1431 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001432
1433 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001434 if (i == 2 || i == 4)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001435 compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001436
1437 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001438 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001439 mode = ptr[2];
1440 if (mode == 'n')
1441 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001442 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001443 if (devname[namelen - 1] != 'n')
1444 ++compat_name;
1445 }
1446 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001447 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001448 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001449 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001450
1451 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001452 if (i == 9)
1453 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001454
1455 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001456 if (i == 11) {
1457 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001458 if (buffer[3] == '0')
1459 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001460 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001461 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001462 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001463 pty1 = "pqrstuvwxyzabcde";
1464 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001465 indexx = atoi(devname + 5);
1466 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001467 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001468 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001469} /* End Function get_old_name */
1470
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001471static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001472/* [SUMMARY] Get the old IDE name for a device.
1473 <major> The major number for the device.
1474 <minor> The minor number for the device.
1475 [RETURNS] The drive letter.
1476*/
1477{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001478 char letter = 'y'; /* 121 */
1479 char c = 'a'; /* 97 */
1480 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001481
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001482 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1483 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001484 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1485 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1486 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1487 || i == IDE9_MAJOR
1488 ) {
1489 if ((unsigned int)i == major) {
1490 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001491 break;
1492 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001493 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001494 }
1495 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001496 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001497
1498 if (minor > 63)
1499 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001500 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001501} /* End Function get_old_ide_name */
1502
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001503static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001504 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001505 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001506/* [SUMMARY] Write the old SCSI disc name to a buffer.
1507 <buffer> The buffer to write to.
1508 <major> The major number for the device.
1509 <minor> The minor number for the device.
1510 <part> The partition string. Must be "" for a whole-disc entry.
1511 [RETURNS] A pointer to the buffer on success, else NULL.
1512*/
1513{
1514 unsigned int disc_index;
1515
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001516 if (major == 8) {
1517 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001518 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001519 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001520 if ((major > 64) && (major < 72)) {
1521 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001522 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001523 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001524 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001525 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001526 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001527 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001528 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001529} /* End Function write_old_sd_name */
1530
1531
1532/* expression.c */
1533
1534/*EXPERIMENTAL_FUNCTION*/
1535
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001536int st_expr_expand(char *output, unsigned int length, const char *input,
1537 const char *(*get_variable_func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001538 void *info),
1539 void *info)
1540/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1541 <output> The output expanded expression is written here.
1542 <length> The size of the output buffer.
1543 <input> The input expression. This may equal <<output>>.
1544 <get_variable> A function which will be used to get variable values. If
1545 this returns NULL, the environment is searched instead. If this is NULL,
1546 only the environment is searched.
1547 <info> An arbitrary pointer passed to <<get_variable>>.
1548 [RETURNS] TRUE on success, else FALSE.
1549*/
1550{
1551 char ch;
1552 unsigned int len;
1553 unsigned int out_pos = 0;
1554 const char *env;
1555 const char *ptr;
1556 struct passwd *pwent;
1557 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1558
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001559 if (length > BUFFER_SIZE)
1560 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001561 for (; TRUE; ++input) {
1562 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001563 case '$':
1564 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001565 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001566 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001567 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001568 break;
1569 case '~':
1570 /* Home directory expansion */
1571 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001572 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001573 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001574 env = getenv("HOME");
1575 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001576 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001577 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001578 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001579 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001580 if (len + out_pos >= length)
1581 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001582 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001583 out_pos += len;
1584 continue;
1585 }
1586 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001587 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001588 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001589 len = ptr - input;
1590 if (len >= sizeof tmp)
1591 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001592 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001593 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001594 pwent = getpwnam(tmp);
1595 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001596 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001597 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001598 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001599 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001600 if (len + out_pos >= length)
1601 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001602 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001603 out_pos += len;
1604 break;
1605 case '\0':
1606 /* Falltrough */
1607 default:
1608 if (out_pos >= length)
1609 goto st_expr_expand_out;
1610 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001611 if (ch == '\0') {
1612 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001613 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001614 }
1615 break;
1616 /* esac */
1617 }
1618 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001619 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001620st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001621 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001622 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001623} /* End Function st_expr_expand */
1624
1625
1626/* Private functions follow */
1627
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001628static const char *expand_variable(char *buffer, unsigned int length,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001629 unsigned int *out_pos, const char *input,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001630 const char *(*func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001631 void *info),
1632 void *info)
1633/* [SUMMARY] Expand a variable.
1634 <buffer> The buffer to write to.
1635 <length> The length of the output buffer.
1636 <out_pos> The current output position. This is updated.
1637 <input> A pointer to the input character pointer.
1638 <func> A function which will be used to get variable values. If this
1639 returns NULL, the environment is searched instead. If this is NULL, only
1640 the environment is searched.
1641 <info> An arbitrary pointer passed to <<func>>.
1642 <errfp> Diagnostic messages are written here.
1643 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1644*/
1645{
1646 char ch;
1647 int len;
1648 unsigned int open_braces;
1649 const char *env, *ptr;
1650 char tmp[STRING_LENGTH];
1651
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001652 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001653 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001654 /* Special case for "$$": PID */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001655 sprintf(tmp, "%d",(int) getpid());
1656 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001657 if (len + *out_pos >= length)
1658 goto expand_variable_out;
1659
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001660 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001661 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001662 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001663 }
1664 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001665 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001666 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001667 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001668 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001669 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001670 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001671 goto expand_variable_out;
1672
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001673 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001674 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001675 env = get_variable_v2(tmp, func, info);
1676 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001677 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001678 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001679 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001680 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681 if (len + *out_pos >= length)
1682 goto expand_variable_out;
1683
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001684 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001685 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001686 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001687 }
1688 /* Variable in braces: check for ':' tricks */
1689 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001690 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001691 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001692 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001693 /* Must be simple variable expansion with "${var}" */
1694 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001695 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001696 goto expand_variable_out;
1697
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001698 safe_memcpy(tmp, input, len);
1699 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001700 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001701 return NULL;
1702 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001703 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001704 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001705 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001706 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001707 }
1708 /* It's that handy "${var:-word}" expression. Check if var is defined */
1709 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001710 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001711 goto expand_variable_out;
1712
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001713 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001714 /* Move input pointer to ':' */
1715 input = ptr;
1716 /* First skip to closing brace, taking note of nested expressions */
1717 ptr += 2;
1718 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001719 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1720 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001721 case '{':
1722 ++open_braces;
1723 break;
1724 case '}':
1725 --open_braces;
1726 break;
1727 case '\0':
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001728 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001729 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001730 default:
1731 break;
1732 }
1733 }
1734 --ptr;
1735 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001736 env = get_variable_v2(tmp, func, info);
1737 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001738 /* Found environment variable, so skip the input to the closing brace
1739 and return the variable */
1740 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001741 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001742 if (len + *out_pos >= length)
1743 goto expand_variable_out;
1744
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001745 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001746 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001747 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001748 }
1749 /* Environment variable was not found, so process word. Advance input
1750 pointer to start of word in "${var:-word}" */
1751 input += 2;
1752 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001753 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001754 goto expand_variable_out;
1755
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001756 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001757 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001758 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001759 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001760 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001761 if (len + *out_pos >= length)
1762 goto expand_variable_out;
1763
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001764 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001765 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001766 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001767expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001768 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001769 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001770} /* End Function expand_variable */
1771
1772
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001773static const char *get_variable_v2(const char *variable,
1774 const char *(*func)(const char *variable, void *info),
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001775 void *info)
1776/* [SUMMARY] Get a variable from the environment or .
1777 <variable> The variable name.
1778 <func> A function which will be used to get the variable. If this returns
1779 NULL, the environment is searched instead. If this is NULL, only the
1780 environment is searched.
1781 [RETURNS] The value of the variable on success, else NULL.
1782*/
1783{
1784 const char *value;
1785
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001786 if (func != NULL) {
1787 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001788 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001789 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001790 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001791 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001792} /* End Function get_variable */
1793
1794/* END OF CODE */