blob: 52a65bc9843d7949493fcf7a533efb724f3b9c24 [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
Eric Andersen2af70002003-10-09 21:02:23 +000095struct devfsd_notify_struct
96{ /* Use native C types to ensure same types in kernel and user space */
97 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 */
107};
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
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000154struct permissions_type
155{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000156 mode_t mode;
157 uid_t uid;
158 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000159};
160
161struct execute_type
162{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000163 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000164};
165
166struct copy_type
167{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000168 const char *source;
169 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000170};
171
172struct action_type
173{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000174 unsigned int what;
175 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000176};
177
178struct config_entry_struct
179{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000180 struct action_type action;
181 regex_t preg;
182 union
183 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000184 struct permissions_type permissions;
185 struct execute_type execute;
186 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000187 }
188 u;
189 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000190};
191
192struct get_variable_info
193{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000194 const struct devfsd_notify_struct *info;
195 const char *devname;
196 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000197};
198
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000199static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000200static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000201static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000202static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000203static int mksymlink(const char *oldpath, const char *newpath);
204static void read_config_file(char *path, int optional, unsigned long *event_mask);
205static void process_config_line(const char *, unsigned long *);
206static int do_servicing(int, unsigned long);
207static void service_name(const struct devfsd_notify_struct *);
208static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
209static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000210 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000211static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
212static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000213 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000214static void action_compat(const struct devfsd_notify_struct *, unsigned);
215static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000216static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000217static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
218static mode_t get_mode(const char *);
219static void signal_handler(int);
220static const char *get_variable(const char *, void *);
221static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000222static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000223 const char *, const regmatch_t *, unsigned);
224static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000225static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000226 const char *(*)(const char *, void *), void *);
227static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
228static char get_old_ide_name(unsigned , unsigned);
229static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000230
231/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000232static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000233static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000234static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
235static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000236
237/* Structs and vars */
238static struct config_entry_struct *first_config = NULL;
239static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000240static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000241static volatile int caught_signal = FALSE;
242static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000243static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000244 const char *dest;
245 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000246} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000247 {"/proc/self/fd", "fd"},
248 {"fd/0", "stdin"},
249 {"fd/1", "stdout"},
250 {"fd/2", "stderr"},
251 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000252};
253
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000254static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000255 unsigned int type; /* The DEVFSD_NOTIFY_* value */
256 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000257} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000258 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
259 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
260 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
261 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
262 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
263 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
264 {DEVFSD_NOTIFY_CREATE, "CREATE"},
265 {DEVFSD_NOTIFY_DELETE, "DELETE"},
266 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000267};
268
Rob Landley1ba19d62005-10-08 17:42:35 +0000269/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000270
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000271static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
272static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
273static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
274static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000275
Rob Landley1ba19d62005-10-08 17:42:35 +0000276/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000277#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
278#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
279#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
280#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
281#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
282#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000283#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000284#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000285#define msg_logger(p, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000286#define msg_logger_and_die(p, fmt, args...) exit(1)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000287#define error_logger(p, fmt, args...)
288#define error_logger_and_die(p, fmt, args...) exit(1)
Eric Andersenf18bd892003-12-19 11:07:59 +0000289#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000290
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000291static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000292{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000293 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000294 dest[len] = '\0';
295}
296
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000297static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000298{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000299 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000300 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000301 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000302 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000303 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000304 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000305 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000306 return 5 + addendum;
307 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000308}
309
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000310static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000311{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000312 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
313 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
314 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
315 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000316 return 1;
317 return scan_dev_name_common(d, n, 0, ptr);
318 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000319 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
320 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
321 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000322 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000323 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000324 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000325 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000326 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000327 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000328 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000329 return 0;
330}
331
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000332/* Public functions follow */
333
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000334int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000335int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000336{
337 int print_version = FALSE;
338 int do_daemon = TRUE;
339 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000340 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000341 int fd, proto_rev, count;
342 unsigned long event_mask = 0;
343 struct sigaction new_action;
344 struct initial_symlink_struct *curr;
345
346 if (argc < 2)
347 bb_show_usage();
348
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000349 for (count = 2; count < argc; ++count) {
350 if (argv[count][0] == '-') {
351 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000352 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000353 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000354 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
355 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000356 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000357 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
358 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000359 else
360 bb_show_usage();
361 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000362 }
363
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000364 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000365
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000366 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000367
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000368 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000369 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000370 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000371
372 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000373 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000374 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000375
376 /* NB: The check for CONFIG_FILE is done in read_config_file() */
377
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000378 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000379 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000380 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000381 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000382 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000383 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000384 exit(EXIT_SUCCESS); /* -v */
385 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000386 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000387 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000388
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000389 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000390 new_action.sa_flags = 0;
391
392 /* Set up SIGHUP and SIGUSR1 handlers */
393 new_action.sa_handler = signal_handler;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000394 if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000395 bb_error_msg_and_die("sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000396
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000397 printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000398
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000399 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000400 umask(0);
401 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000402 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000403 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000404
Rob Landley1ba19d62005-10-08 17:42:35 +0000405 if (ENABLE_DEVFSD_FG_NP && no_polling)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000406 exit(0);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000407
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000408 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
409 logmode = LOGMODE_BOTH;
410 else if (do_daemon == TRUE)
411 logmode = LOGMODE_SYSLOG;
412 /* This is the default */
413 /*else
414 logmode = LOGMODE_STDIO; */
415
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000416 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000417 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000418 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000419 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000420 } else if (ENABLE_DEVFSD_FG_NP) {
421 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000422 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000423
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000424 while (TRUE) {
425 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000426
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000427 free_config();
428 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000429 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000430 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000431 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000432 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000433} /* End Function main */
434
435
436/* Private functions follow */
437
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000438static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000439/* [SUMMARY] Read a configuration database.
440 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000441 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000442 <optional> If TRUE, the routine will silently ignore a missing config file.
443 <event_mask> The event mask is written here. This is not initialised.
444 [RETURNS] Nothing.
445*/
446{
447 struct stat statbuf;
448 FILE *fp;
449 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000450 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000451 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000452
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000453 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000454 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000455 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000456 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000457 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000458 p = bb_simplify_path(path);
459 dir_operation(READ_CONFIG, p, 0, event_mask);
460 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000461 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000462 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000463 fp = fopen(path, "r");
464 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000465 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000466 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000467 line = buf;
468 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000469 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000470 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000471 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000472 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000473 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000474 } else {
475 goto read_config_file_err;
476 }
477 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000478read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000479 if (optional == 0 && errno == ENOENT)
480 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000481 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000482} /* End Function read_config_file */
483
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000484static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000485/* [SUMMARY] Process a line from a configuration file.
486 <line> The configuration line.
487 <event_mask> The event mask is written here. This is not initialised.
488 [RETURNS] Nothing.
489*/
490{
491 int num_args, count;
492 struct config_entry_struct *new;
493 char p[MAX_ARGS][STRING_LENGTH];
494 char when[STRING_LENGTH], what[STRING_LENGTH];
495 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000496 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000497 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000498 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000499
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000500 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000501 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000502 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
503 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
504 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
505 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000506
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000507 for (count = 0; count < MAX_ARGS; ++count)
508 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000509 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510 when, name, what,
511 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000512
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000513 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000514
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000515 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000516 if (i == 0) {
517 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000518 *event_mask = 0;
519 return;
520 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000521
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000522 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000523 goto process_config_line_err;
524
Eric Andersenf18bd892003-12-19 11:07:59 +0000525 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000526 if (i == 1 || i == 2) {
527 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000528 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000529 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000530 return;
531 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000532 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000533 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000534 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000535 return;
536 }
537 if (num_args < 3)
538 goto process_config_line_err;
539
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000540 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000541
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000542 for (count = 0; event_types[count].config_name != NULL; ++count) {
543 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000544 continue;
545 new->action.when = event_types[count].type;
546 break;
547 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000548 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000549 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000550 goto process_config_line_err;
551 }
552
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000553 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000554
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000555 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000556 case 4: /* "PERMISSIONS" */
557 new->action.what = AC_PERMISSIONS;
558 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000559 ptr = strchr(p[0], '.');
560 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000561 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000562 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000563 }
564
565 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000566 new->u.permissions.uid = get_uid_gid(UID, p[0]);
567 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000568 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000569 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000570 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000571 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000572 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000573 the device name) to the module loading facility. In addition,
574 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000575 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000576 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000577 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000578 case 6: /* EXECUTE */
579 new->action.what = AC_EXECUTE;
580 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000581
Eric Andersenf18bd892003-12-19 11:07:59 +0000582 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000583 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000584
Eric Andersenf18bd892003-12-19 11:07:59 +0000585 new->u.execute.argv[num_args] = NULL;
586 break;
587 case 7: /* COPY */
588 new->action.what = AC_COPY;
589 num_args -= 3;
590 if (num_args != 2)
591 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000592
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000593 new->u.copy.source = xstrdup(p[0]);
594 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000595 break;
596 case 8: /* IGNORE */
597 /* FALLTROUGH */
598 case 9: /* MKOLDCOMPAT */
599 /* FALLTROUGH */
600 case 10: /* MKNEWCOMPAT */
601 /* FALLTROUGH */
602 case 11:/* RMOLDCOMPAT */
603 /* FALLTROUGH */
604 case 12: /* RMNEWCOMPAT */
605 /* AC_IGNORE 6
606 AC_MKOLDCOMPAT 7
607 AC_MKNEWCOMPAT 8
608 AC_RMOLDCOMPAT 9
609 AC_RMNEWCOMPAT 10*/
610 new->action.what = i - 2;
611 break;
612 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000613 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000614 goto process_config_line_err;
615 /*esac*/
616 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000617
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000618 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000619
620 *event_mask |= 1 << new->action.when;
621 new->next = NULL;
622 if (first_config == NULL)
623 first_config = new;
624 else
625 last_config->next = new;
626 last_config = new;
627 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000628
629 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000630 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000631} /* End Function process_config_line */
632
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000633static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000634/* [SUMMARY] Service devfs changes until a signal is received.
635 <fd> The open control file.
636 <event_mask> The event mask.
637 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
638*/
639{
640 ssize_t bytes;
641 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000642
Denis Vlasenko856be772007-08-17 08:29:48 +0000643 /* (void*) cast is only in order to match prototype */
644 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000645 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000646 errno = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000647 bytes = read(fd,(char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000648 if (caught_signal)
649 break; /* Must test for this first */
650 if (errno == EINTR)
651 continue; /* Yes, the order is important */
652 if (bytes < 1)
653 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000654 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000655 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000656 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000657 int c_sighup = caught_sighup;
658
659 caught_signal = FALSE;
660 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000661 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000662 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000663 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000664} /* End Function do_servicing */
665
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000666static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000667/* [SUMMARY] Service a single devfs change.
668 <info> The devfs change.
669 [RETURNS] Nothing.
670*/
671{
672 unsigned int n;
673 regmatch_t mbuf[MAX_SUBEXPR];
674 struct config_entry_struct *entry;
675
Rob Landley1ba19d62005-10-08 17:42:35 +0000676 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000677 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000678
679 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000680 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000681 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000682 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000683 || (info->devname[0] == 'i' && info->devname[1] == 'n'
684 && info->devname[2] == 'i' && info->devname[3] == 't'
685 && info->devname[4] == 'c' && info->devname[5] == 't'
686 && info->devname[6] == 'l' && !info->devname[7]))
687 )
688 return;
689
690 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000691 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000692 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000693 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000694 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000695 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000696 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000697
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000698 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000700 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000701 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000702 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000703 if (ENABLE_DEVFSD_MODLOAD)
704 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000705 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000706 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000707 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000708 break;
709 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000710 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000711 break;
712 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000713 return;
714 /*break;*/
715 case AC_MKOLDCOMPAT:
716 case AC_MKNEWCOMPAT:
717 case AC_RMOLDCOMPAT:
718 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000719 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000720 break;
721 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000722 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000723 }
724 }
725} /* End Function service_name */
726
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000727static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000728 const struct config_entry_struct *entry)
729/* [SUMMARY] Update permissions for a device entry.
730 <info> The devfs change.
731 <entry> The config file entry.
732 [RETURNS] Nothing.
733*/
734{
735 struct stat statbuf;
736
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000737 if (stat(info->devname, &statbuf) != 0
738 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
739 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000740 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000741 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000742} /* End Function action_permissions */
743
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000744static void action_modload(const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000745 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000746/* [SUMMARY] Load a module.
747 <info> The devfs change.
748 <entry> The config file entry.
749 [RETURNS] Nothing.
750*/
751{
752 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000753
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000754 argv[0] = (char*)MODPROBE;
755 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
756 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
757 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000758 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000759 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000760
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000761 wait4pid(xspawn(argv));
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000762 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000763} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000764
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000765static void action_execute(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000766 const struct config_entry_struct *entry,
767 const regmatch_t *regexpr, unsigned int numexpr)
768/* [SUMMARY] Execute a programme.
769 <info> The devfs change.
770 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000771 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000772 device name.
773 <numexpr> The number of elements within <<regexpr>>.
774 [RETURNS] Nothing.
775*/
776{
777 unsigned int count;
778 struct get_variable_info gv_info;
779 char *argv[MAX_ARGS + 1];
780 char largv[MAX_ARGS + 1][STRING_LENGTH];
781
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000782 gv_info.info = info;
783 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000784 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
785 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
786 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000787 entry->u.execute.argv[count],
788 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000789 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000790 argv[count] = largv[count];
791 }
792 argv[count] = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000793 wait4pid(spawn(argv));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000794} /* End Function action_execute */
795
796
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000797static void action_copy(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000798 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000799 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000800/* [SUMMARY] Copy permissions.
801 <info> The devfs change.
802 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000803 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000804 device name.
805 <numexpr> The number of elements in <<regexpr>>.
806 [RETURNS] Nothing.
807*/
808{
809 mode_t new_mode;
810 struct get_variable_info gv_info;
811 struct stat source_stat, dest_stat;
812 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000813 int ret = 0;
814
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000815 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000816
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000817 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000818 return;
819 gv_info.info = info;
820 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000821
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000822 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
823 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000824 get_variable, &gv_info, gv_info.devname,
825 regexpr, numexpr);
826
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000827 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000828 get_variable, &gv_info, gv_info.devname,
829 regexpr, numexpr);
830
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000831 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000832 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000833 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000834 new_mode = source_stat.st_mode & ~S_ISVTX;
835 if (info->type == DEVFSD_NOTIFY_CREATE)
836 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000837 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000838 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000839 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000840 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000841 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000842} /* End Function action_copy */
843
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000844static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000845/* [SUMMARY] Process a compatibility request.
846 <info> The devfs change.
847 <action> The action to take.
848 [RETURNS] Nothing.
849*/
850{
Rob Landley1ba19d62005-10-08 17:42:35 +0000851 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000852 const char *compat_name = NULL;
853 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000854 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000855 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000856 int mode, host, bus, target, lun;
857 unsigned int i;
858 char rewind_;
859 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000860 static const char *const fmt[] = {
861 NULL ,
862 "sg/c%db%dt%du%d", /* scsi/generic */
863 "sd/c%db%dt%du%d", /* scsi/disc */
864 "sr/c%db%dt%du%d", /* scsi/cd */
865 "sd/c%db%dt%du%dp%d", /* scsi/part */
866 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
867 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
868 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
869 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
870 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
871 NULL
872 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000873
874 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000875 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000876 case AC_MKOLDCOMPAT:
877 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000878 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000879 break;
880 case AC_MKNEWCOMPAT:
881 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000882 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000883 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000884
885 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000886 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000887 return;
888
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000889 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
890 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000891 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000892 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000893
Eric Andersenf18bd892003-12-19 11:07:59 +0000894
895 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000896 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
897 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000898
899 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000900 if (i == 4 || i == 8)
901 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000902
903 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000904 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000905 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000906 if (rewind_ != 'n')
907 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000908 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000909 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000910 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000911 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000912 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000913 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000914 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000915
Eric Andersenf18bd892003-12-19 11:07:59 +0000916 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000917 if (i == 9)
918 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000919 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000920 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000921
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000922 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000923 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000924
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000925 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000926 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000927 case AC_MKOLDCOMPAT:
928 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000929 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000930 break;
931 case AC_RMOLDCOMPAT:
932 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000933 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +0000934 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000935 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000936 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000937 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000938 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000939} /* End Function action_compat */
940
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000941static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000942{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000943 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000944 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000945
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000946 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000947 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000948 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000949 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000950 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000951 copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000952
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000953 if (S_ISDIR(source_stat.st_mode))
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000954 dir_operation(RESTORE, spath, rootlen,NULL);
955}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000956
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000957
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000958static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000959 mode_t new_mode,
960 const char *sourcepath, const struct stat *source_stat)
961/* [SUMMARY] Copy an inode.
962 <destpath> The destination path. An existing inode may be deleted.
963 <dest_stat> The destination stat(2) information.
964 <new_mode> The desired new mode for the destination.
965 <sourcepath> The source path.
966 <source_stat> The source stat(2) information.
967 [RETURNS] TRUE on success, else FALSE.
968*/
969{
Eric Andersenf18bd892003-12-19 11:07:59 +0000970 int source_len, dest_len;
971 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
972 int fd, val;
973 struct sockaddr_un un_addr;
974 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000975
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000976 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000977 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000978 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000979 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
980 if ((source_len < 0)
981 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000982 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000983 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000984 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000985 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000986 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
987 unlink(destpath);
988 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000989 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000990 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000991 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000992 chmod(destpath, new_mode & ~S_IFMT);
993 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000994 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000995 }
996 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000997 unlink(destpath);
998 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000999 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001000 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1001 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001002 break;
1003 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001004 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
1005 val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
1006 close(fd);
1007 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001008 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001009 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001010 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001011 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1012 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001013 break;
1014 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001015 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001016 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001017 break;
1018 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001019 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1020 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001021 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001022 close(fd);
1023 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001024 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001025 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001026 case S_IFBLK:
1027 case S_IFCHR:
1028 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001029 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001030 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001031 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001032 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001033 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001034 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001035do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001036 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001037 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001038 /*break;*/
1039 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001040 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001041} /* End Function copy_inode */
1042
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001043static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001044/* [SUMMARY] Free the configuration information.
1045 [RETURNS] Nothing.
1046*/
1047{
1048 struct config_entry_struct *c_entry;
1049 void *next;
1050
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001051 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001052 unsigned int count;
1053
1054 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001055 regfree(&c_entry->preg);
1056 if (c_entry->action.what == AC_EXECUTE) {
1057 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001058 if (c_entry->u.execute.argv[count] == NULL)
1059 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001060 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001061 }
1062 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001063 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001064 }
1065 first_config = NULL;
1066 last_config = NULL;
1067} /* End Function free_config */
1068
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001069static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001070/* [SUMMARY] Convert a string to a UID or GID value.
1071 <flag> "UID" or "GID".
1072 <string> The string.
1073 [RETURNS] The UID or GID value.
1074*/
1075{
1076 struct passwd *pw_ent;
1077 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001078 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001079
Rob Landley1ba19d62005-10-08 17:42:35 +00001080 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001081 msg = "user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001082
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001083 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001084 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001085
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001086 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001087 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001088
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001089 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001090 return grp_ent->gr_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001091 else if (ENABLE_DEVFSD_VERBOSE)
1092 msg = "group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001093
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001094 if (ENABLE_DEVFSD_VERBOSE)
Rob Landley1ba19d62005-10-08 17:42:35 +00001095 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001096 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001097}/* End Function get_uid_gid */
1098
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001099static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001100/* [SUMMARY] Convert a string to a mode value.
1101 <string> The string.
1102 [RETURNS] The mode value.
1103*/
1104{
1105 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001106 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001107
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001108 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001109 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001110 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001111 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001112
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001113 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001114 i = S_IRUSR;
1115 while (i > 0) {
1116 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1117 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001118 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001119 string++;
1120 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001121 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001122} /* End Function get_mode */
1123
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001124static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001125{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001126 caught_signal = TRUE;
1127 if (sig == SIGHUP)
1128 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001129
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001130 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001131} /* End Function signal_handler */
1132
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001133static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001134{
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001135 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
1136
1137 char hostname[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001138 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001139 const char *field_names[] = {
1140 "hostname", "mntpt", "devpath", "devname",
1141 "uid", "gid", "mode", hostname, mount_point,
1142 gv_info->devpath, gv_info->devname, NULL
1143 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001144 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001145
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001146 if (gethostname(hostname, STRING_LENGTH - 1) != 0)
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001147 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001148 error_logger_and_die(LOG_ERR, "gethostname");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001149
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001150 hostname[STRING_LENGTH - 1] = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001151
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001152 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001153 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001154
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001155 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001156 return NULL;
1157 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001158 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001159
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001160 if (i == 4)
1161 sprintf(sbuf, "%u", gv_info->info->uid);
1162 else if (i == 5)
1163 sprintf(sbuf, "%u", gv_info->info->gid);
1164 else if (i == 6)
1165 sprintf(sbuf, "%o", gv_info->info->mode);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001166 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001167} /* End Function get_variable */
1168
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001169static void service(struct stat statbuf, char *path)
1170{
1171 struct devfsd_notify_struct info;
1172
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001173 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001174 info.type = DEVFSD_NOTIFY_REGISTERED;
1175 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001176 info.major = major(statbuf.st_rdev);
1177 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001178 info.uid = statbuf.st_uid;
1179 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001180 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1181 info.namelen = strlen(info.devname);
1182 service_name(&info);
1183 if (S_ISDIR(statbuf.st_mode))
1184 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001185}
1186
1187static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001188/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001189 <flag> To choose which function to perform
1190 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001191 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001192 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001193 [RETURNS] Nothing.
1194*/
1195{
1196 struct stat statbuf;
1197 DIR *dp;
1198 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001199 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001200
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001201 dp = warn_opendir(dir_name);
1202 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001203 return;
1204
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001205 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001206
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001207 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001208 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001209 path = concat_path_file(dir_name, de->d_name);
1210 if (lstat(path, &statbuf) == 0) {
1211 switch (type) {
1212 case SERVICE:
1213 service(statbuf, path);
1214 break;
1215 case RESTORE:
1216 restore(path, statbuf, var);
1217 break;
1218 case READ_CONFIG:
1219 read_config_file(path, var, event_mask);
1220 break;
1221 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001222 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001223 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001224 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001225 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001226} /* End Function do_scan_and_service */
1227
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001228static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001229/* [SUMMARY] Create a symlink, creating intervening directories as required.
1230 <oldpath> The string contained in the symlink.
1231 <newpath> The name of the new symlink.
1232 [RETURNS] 0 on success, else -1.
1233*/
1234{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001235 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001236 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001237
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001238 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001239 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001240 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001241 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001242 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001243} /* End Function mksymlink */
1244
1245
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001246static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001247/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001248 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001249 [RETURNS] TRUE on success, else FALSE.
1250*/
1251{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001252 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001253 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001254 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001255} /* End Function make_dir_tree */
1256
1257static int expand_expression(char *output, unsigned int outsize,
1258 const char *input,
1259 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001260 void *info,
1261 const char *devname,
1262 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001263/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001264 <output> The output expanded expression is written here.
1265 <length> The size of the output buffer.
1266 <input> The input expression. This may equal <<output>>.
1267 <get_variable> A function which will be used to get variable values. If
1268 this returns NULL, the environment is searched instead. If this is NULL,
1269 only the environment is searched.
1270 <info> An arbitrary pointer passed to <<get_variable>>.
1271 <devname> Device name; specifically, this is the string that contains all
1272 of the regular subexpressions.
1273 <ex> Array of start / end offsets into info->devname for each subexpression
1274 <numexp> Number of regular subexpressions found in <<devname>>.
1275 [RETURNS] TRUE on success, else FALSE.
1276*/
1277{
1278 char temp[STRING_LENGTH];
1279
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001280 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001281 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001282 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001283 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001284} /* End Function expand_expression */
1285
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001286static void expand_regexp(char *output, size_t outsize, const char *input,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001287 const char *devname,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001288 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001289/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1290 <output> The output expanded expression is written here.
1291 <outsize> The size of the output buffer.
1292 <input> The input expression. This may NOT equal <<output>>, because
1293 supporting that would require yet another string-copy. However, it's not
1294 hard to write a simple wrapper function to add this functionality for those
1295 few cases that need it.
1296 <devname> Device name; specifically, this is the string that contains all
1297 of the regular subexpressions.
1298 <ex> An array of start and end offsets into <<devname>>, one for each
1299 subexpression
1300 <numex> Number of subexpressions in the offset-array <<ex>>.
1301 [RETURNS] Nothing.
1302*/
1303{
1304 const char last_exp = '0' - 1 + numex;
1305 int c = -1;
1306
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001307 /* Guarantee NULL termination by writing an explicit '\0' character into
1308 the very last byte */
1309 if (outsize)
1310 output[--outsize] = '\0';
1311 /* Copy the input string into the output buffer, replacing '\\' with '\'
1312 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1313 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001314 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001315 c = *input;
1316 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001317 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001318 c = *input;
1319 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001320 if (c != '\\') {
1321 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001322 const regmatch_t *subexp = ex + (c - '0');
1323 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1324
1325 /* Range checking */
1326 if (sublen > outsize)
1327 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001328 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001329 output += sublen;
1330 outsize -= sublen;
1331 }
1332 continue;
1333 }
1334 }
1335 *output = c;
1336 ++output;
1337 --outsize;
1338 } /* while */
1339} /* End Function expand_regexp */
1340
1341
1342/* from compat_name.c */
1343
1344struct translate_struct
1345{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001346 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001347 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001348 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001349};
1350
1351static struct translate_struct translate_table[] =
1352{
1353 {"sound/", NULL},
1354 {"printers/", "lp%s"},
1355 {"v4l/", NULL},
1356 {"parports/", "parport%s"},
1357 {"fb/", "fb%s"},
1358 {"netlink/", NULL},
1359 {"loop/", "loop%s"},
1360 {"floppy/", "fd%s"},
1361 {"rd/", "ram%s"},
1362 {"md/", "md%s"}, /* Meta-devices */
1363 {"vc/", "tty%s"},
1364 {"misc/", NULL},
1365 {"isdn/", NULL},
1366 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1367 {"i2c/", "i2c-%s"},
1368 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1369 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1370 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1371 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1372 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1373 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1374 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1375 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1376 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1377 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1378 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1379 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1380 {"input/js", "js%s"}, /* Joystick driver */
1381 {NULL, NULL}
1382};
1383
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001384const char *get_old_name(const char *devname, unsigned int namelen,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001385 char *buffer, unsigned int major, unsigned int minor)
1386/* [SUMMARY] Translate a kernel-supplied name into an old name.
1387 <devname> The device name provided by the kernel.
1388 <namelen> The length of the name.
1389 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1390 <major> The major number for the device.
1391 <minor> The minor number for the device.
1392 [RETURNS] A pointer to the old name if known, else NULL.
1393*/
1394{
1395 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001396 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001397 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001398 unsigned int i;
1399 char mode;
1400 int indexx;
1401 const char *pty1;
1402 const char *pty2;
1403 size_t len;
1404 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001405 static const char *const fmt[] = {
1406 NULL ,
1407 "sg%u", /* scsi/generic */
1408 NULL, /* scsi/disc */
1409 "sr%u", /* scsi/cd */
1410 NULL, /* scsi/part */
1411 "nst%u%c", /* scsi/mt */
1412 "hd%c" , /* ide/host/disc */
1413 "hd%c" , /* ide/host/cd */
1414 "hd%c%s", /* ide/host/part */
1415 "%sht%d", /* ide/host/mt */
1416 "sbpcd%u", /* sbp/ */
1417 "vcs%s", /* vcc/ */
1418 "%cty%c%c", /* pty/ */
1419 NULL
1420 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001421
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001422 for (trans = translate_table; trans->match != NULL; ++trans) {
1423 len = strlen(trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001424
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001425 if (strncmp(devname, trans->match, len) == 0) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001426 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001427 return devname + len;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001428 sprintf(buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001429 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001430 }
1431 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001432
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001433 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001434 i = scan_dev_name(devname, namelen, ptr);
1435
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001436 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001437 compat_name = buffer;
1438 else
1439 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001440
Eric Andersenf18bd892003-12-19 11:07:59 +00001441 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001442 if (i == 1 || i == 3 || i == 10)
1443 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001444
1445 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001446 if (i == 2 || i == 4)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001447 compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001448
1449 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001450 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001451 mode = ptr[2];
1452 if (mode == 'n')
1453 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001454 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001455 if (devname[namelen - 1] != 'n')
1456 ++compat_name;
1457 }
1458 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001459 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001460 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001461 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001462
1463 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001464 if (i == 9)
1465 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001466
1467 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001468 if (i == 11) {
1469 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001470 if (buffer[3] == '0')
1471 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001472 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001473 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001474 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001475 pty1 = "pqrstuvwxyzabcde";
1476 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001477 indexx = atoi(devname + 5);
1478 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001479 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001480 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001481} /* End Function get_old_name */
1482
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001483static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001484/* [SUMMARY] Get the old IDE name for a device.
1485 <major> The major number for the device.
1486 <minor> The minor number for the device.
1487 [RETURNS] The drive letter.
1488*/
1489{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001490 char letter = 'y'; /* 121 */
1491 char c = 'a'; /* 97 */
1492 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001493
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001494 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1495 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001496 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1497 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1498 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1499 || i == IDE9_MAJOR
1500 ) {
1501 if ((unsigned int)i == major) {
1502 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001503 break;
1504 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001505 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001506 }
1507 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001508 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001509
1510 if (minor > 63)
1511 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001512 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001513} /* End Function get_old_ide_name */
1514
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001515static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001516 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001517 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001518/* [SUMMARY] Write the old SCSI disc name to a buffer.
1519 <buffer> The buffer to write to.
1520 <major> The major number for the device.
1521 <minor> The minor number for the device.
1522 <part> The partition string. Must be "" for a whole-disc entry.
1523 [RETURNS] A pointer to the buffer on success, else NULL.
1524*/
1525{
1526 unsigned int disc_index;
1527
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001528 if (major == 8) {
1529 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001530 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001531 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001532 if ((major > 64) && (major < 72)) {
1533 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001534 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001535 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001536 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001537 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001538 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001539 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001540 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001541} /* End Function write_old_sd_name */
1542
1543
1544/* expression.c */
1545
1546/*EXPERIMENTAL_FUNCTION*/
1547
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001548int st_expr_expand(char *output, unsigned int length, const char *input,
1549 const char *(*get_variable_func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001550 void *info),
1551 void *info)
1552/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1553 <output> The output expanded expression is written here.
1554 <length> The size of the output buffer.
1555 <input> The input expression. This may equal <<output>>.
1556 <get_variable> A function which will be used to get variable values. If
1557 this returns NULL, the environment is searched instead. If this is NULL,
1558 only the environment is searched.
1559 <info> An arbitrary pointer passed to <<get_variable>>.
1560 [RETURNS] TRUE on success, else FALSE.
1561*/
1562{
1563 char ch;
1564 unsigned int len;
1565 unsigned int out_pos = 0;
1566 const char *env;
1567 const char *ptr;
1568 struct passwd *pwent;
1569 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1570
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001571 if (length > BUFFER_SIZE)
1572 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001573 for (; TRUE; ++input) {
1574 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001575 case '$':
1576 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001577 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001578 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001579 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001580 break;
1581 case '~':
1582 /* Home directory expansion */
1583 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001584 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001585 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001586 env = getenv("HOME");
1587 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001588 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001589 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001590 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001591 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001592 if (len + out_pos >= length)
1593 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001594 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001595 out_pos += len;
1596 continue;
1597 }
1598 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001599 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001600 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001601 len = ptr - input;
1602 if (len >= sizeof tmp)
1603 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001604 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001605 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001606 pwent = getpwnam(tmp);
1607 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001608 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001609 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001610 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001611 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001612 if (len + out_pos >= length)
1613 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001614 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001615 out_pos += len;
1616 break;
1617 case '\0':
1618 /* Falltrough */
1619 default:
1620 if (out_pos >= length)
1621 goto st_expr_expand_out;
1622 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001623 if (ch == '\0') {
1624 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001625 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001626 }
1627 break;
1628 /* esac */
1629 }
1630 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001631 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001632st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001633 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001634 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001635} /* End Function st_expr_expand */
1636
1637
1638/* Private functions follow */
1639
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001640static const char *expand_variable(char *buffer, unsigned int length,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001641 unsigned int *out_pos, const char *input,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001642 const char *(*func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001643 void *info),
1644 void *info)
1645/* [SUMMARY] Expand a variable.
1646 <buffer> The buffer to write to.
1647 <length> The length of the output buffer.
1648 <out_pos> The current output position. This is updated.
1649 <input> A pointer to the input character pointer.
1650 <func> A function which will be used to get variable values. If this
1651 returns NULL, the environment is searched instead. If this is NULL, only
1652 the environment is searched.
1653 <info> An arbitrary pointer passed to <<func>>.
1654 <errfp> Diagnostic messages are written here.
1655 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1656*/
1657{
1658 char ch;
1659 int len;
1660 unsigned int open_braces;
1661 const char *env, *ptr;
1662 char tmp[STRING_LENGTH];
1663
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001664 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001665 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001666 /* Special case for "$$": PID */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001667 sprintf(tmp, "%d",(int) getpid());
1668 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001669 if (len + *out_pos >= length)
1670 goto expand_variable_out;
1671
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001672 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001673 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001674 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001675 }
1676 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001677 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001678 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001679 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001680 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001682 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001683 goto expand_variable_out;
1684
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001685 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001686 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001687 env = get_variable_v2(tmp, func, info);
1688 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001689 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001690 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001691 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001692 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001693 if (len + *out_pos >= length)
1694 goto expand_variable_out;
1695
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001696 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001697 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001698 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001699 }
1700 /* Variable in braces: check for ':' tricks */
1701 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001702 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001703 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001704 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001705 /* Must be simple variable expansion with "${var}" */
1706 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001707 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001708 goto expand_variable_out;
1709
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001710 safe_memcpy(tmp, input, len);
1711 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001712 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001713 return NULL;
1714 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001715 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001716 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001717 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001718 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001719 }
1720 /* It's that handy "${var:-word}" expression. Check if var is defined */
1721 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001722 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001723 goto expand_variable_out;
1724
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001725 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001726 /* Move input pointer to ':' */
1727 input = ptr;
1728 /* First skip to closing brace, taking note of nested expressions */
1729 ptr += 2;
1730 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001731 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1732 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001733 case '{':
1734 ++open_braces;
1735 break;
1736 case '}':
1737 --open_braces;
1738 break;
1739 case '\0':
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001740 info_logger(LOG_INFO,"\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001741 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001742 default:
1743 break;
1744 }
1745 }
1746 --ptr;
1747 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001748 env = get_variable_v2(tmp, func, info);
1749 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001750 /* Found environment variable, so skip the input to the closing brace
1751 and return the variable */
1752 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001753 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001754 if (len + *out_pos >= length)
1755 goto expand_variable_out;
1756
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001757 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001758 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001759 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001760 }
1761 /* Environment variable was not found, so process word. Advance input
1762 pointer to start of word in "${var:-word}" */
1763 input += 2;
1764 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001765 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001766 goto expand_variable_out;
1767
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001768 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001769 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001770 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001771 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001772 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001773 if (len + *out_pos >= length)
1774 goto expand_variable_out;
1775
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001776 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001777 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001778 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001779expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001780 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001781 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001782} /* End Function expand_variable */
1783
1784
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001785static const char *get_variable_v2(const char *variable,
1786 const char *(*func)(const char *variable, void *info),
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001787 void *info)
1788/* [SUMMARY] Get a variable from the environment or .
1789 <variable> The variable name.
1790 <func> A function which will be used to get the variable. If this returns
1791 NULL, the environment is searched instead. If this is NULL, only the
1792 environment is searched.
1793 [RETURNS] The value of the variable on success, else NULL.
1794*/
1795{
1796 const char *value;
1797
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001798 if (func != NULL) {
1799 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001800 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001801 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001802 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001803 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001804} /* End Function get_variable */
1805
1806/* END OF CODE */