blob: 1b88f050e85cc1d0f0d6b0181e345875970503c0 [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 Vlasenko4cf1d082008-03-12 23:13:50 +0000389 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000390 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000391 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000392 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000393 sigaction_set(SIGHUP, &new_action);
394 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000395
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000396 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000397
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000398 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000399 umask(0);
400 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000401 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000402 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000403
Rob Landley1ba19d62005-10-08 17:42:35 +0000404 if (ENABLE_DEVFSD_FG_NP && no_polling)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000405 exit(0);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000406
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000407 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
408 logmode = LOGMODE_BOTH;
409 else if (do_daemon == TRUE)
410 logmode = LOGMODE_SYSLOG;
411 /* This is the default */
412 /*else
413 logmode = LOGMODE_STDIO; */
414
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000415 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000416 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000417 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000418 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000419 } else if (ENABLE_DEVFSD_FG_NP) {
420 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000421 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000422
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000423 while (TRUE) {
424 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000425
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000426 free_config();
427 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000428 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000429 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000430 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000431 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000432} /* End Function main */
433
434
435/* Private functions follow */
436
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000437static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000438/* [SUMMARY] Read a configuration database.
439 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000440 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000441 <optional> If TRUE, the routine will silently ignore a missing config file.
442 <event_mask> The event mask is written here. This is not initialised.
443 [RETURNS] Nothing.
444*/
445{
446 struct stat statbuf;
447 FILE *fp;
448 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000449 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000450 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000451
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000452 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000453 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000454 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000455 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000456 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000457 p = bb_simplify_path(path);
458 dir_operation(READ_CONFIG, p, 0, event_mask);
459 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000460 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000461 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000462 fp = fopen(path, "r");
463 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000464 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000465 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000466 line = buf;
467 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000468 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000469 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000470 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000471 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000472 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000473 } else {
474 goto read_config_file_err;
475 }
476 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000477read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000478 if (optional == 0 && errno == ENOENT)
479 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000480 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000481} /* End Function read_config_file */
482
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000483static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000484/* [SUMMARY] Process a line from a configuration file.
485 <line> The configuration line.
486 <event_mask> The event mask is written here. This is not initialised.
487 [RETURNS] Nothing.
488*/
489{
490 int num_args, count;
491 struct config_entry_struct *new;
492 char p[MAX_ARGS][STRING_LENGTH];
493 char when[STRING_LENGTH], what[STRING_LENGTH];
494 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000495 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000496 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000497 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000498
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000499 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000500 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000501 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
502 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
503 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
504 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000505
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000506 for (count = 0; count < MAX_ARGS; ++count)
507 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000508 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000509 when, name, what,
510 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000511
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000512 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000513
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000514 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000515 if (i == 0) {
516 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000517 *event_mask = 0;
518 return;
519 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000520
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000521 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000522 goto process_config_line_err;
523
Eric Andersenf18bd892003-12-19 11:07:59 +0000524 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000525 if (i == 1 || i == 2) {
526 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000527 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000528 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000529 return;
530 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000531 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000532 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000533 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000534 return;
535 }
536 if (num_args < 3)
537 goto process_config_line_err;
538
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000539 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000540
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000541 for (count = 0; event_types[count].config_name != NULL; ++count) {
542 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000543 continue;
544 new->action.when = event_types[count].type;
545 break;
546 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000547 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000548 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000549 goto process_config_line_err;
550 }
551
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000552 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000553
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000554 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000555 case 4: /* "PERMISSIONS" */
556 new->action.what = AC_PERMISSIONS;
557 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000558 ptr = strchr(p[0], '.');
559 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000560 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000561 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000562 }
563
564 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000565 new->u.permissions.uid = get_uid_gid(UID, p[0]);
566 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000567 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000568 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000569 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000570 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000571 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000572 the device name) to the module loading facility. In addition,
573 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000574 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000575 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000576 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000577 case 6: /* EXECUTE */
578 new->action.what = AC_EXECUTE;
579 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000580
Eric Andersenf18bd892003-12-19 11:07:59 +0000581 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000582 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000583
Eric Andersenf18bd892003-12-19 11:07:59 +0000584 new->u.execute.argv[num_args] = NULL;
585 break;
586 case 7: /* COPY */
587 new->action.what = AC_COPY;
588 num_args -= 3;
589 if (num_args != 2)
590 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000591
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000592 new->u.copy.source = xstrdup(p[0]);
593 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000594 break;
595 case 8: /* IGNORE */
596 /* FALLTROUGH */
597 case 9: /* MKOLDCOMPAT */
598 /* FALLTROUGH */
599 case 10: /* MKNEWCOMPAT */
600 /* FALLTROUGH */
601 case 11:/* RMOLDCOMPAT */
602 /* FALLTROUGH */
603 case 12: /* RMNEWCOMPAT */
604 /* AC_IGNORE 6
605 AC_MKOLDCOMPAT 7
606 AC_MKNEWCOMPAT 8
607 AC_RMOLDCOMPAT 9
608 AC_RMNEWCOMPAT 10*/
609 new->action.what = i - 2;
610 break;
611 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000612 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000613 goto process_config_line_err;
614 /*esac*/
615 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000616
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000617 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000618
619 *event_mask |= 1 << new->action.when;
620 new->next = NULL;
621 if (first_config == NULL)
622 first_config = new;
623 else
624 last_config->next = new;
625 last_config = new;
626 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000627
628 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000629 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000630} /* End Function process_config_line */
631
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000632static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000633/* [SUMMARY] Service devfs changes until a signal is received.
634 <fd> The open control file.
635 <event_mask> The event mask.
636 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
637*/
638{
639 ssize_t bytes;
640 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000641
Denis Vlasenko856be772007-08-17 08:29:48 +0000642 /* (void*) cast is only in order to match prototype */
643 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000644 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000645 errno = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000646 bytes = read(fd,(char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000647 if (caught_signal)
648 break; /* Must test for this first */
649 if (errno == EINTR)
650 continue; /* Yes, the order is important */
651 if (bytes < 1)
652 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000653 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000654 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000655 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000656 int c_sighup = caught_sighup;
657
658 caught_signal = FALSE;
659 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000660 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000661 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000662 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000663} /* End Function do_servicing */
664
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000665static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000666/* [SUMMARY] Service a single devfs change.
667 <info> The devfs change.
668 [RETURNS] Nothing.
669*/
670{
671 unsigned int n;
672 regmatch_t mbuf[MAX_SUBEXPR];
673 struct config_entry_struct *entry;
674
Rob Landley1ba19d62005-10-08 17:42:35 +0000675 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000676 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000677
678 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000679 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000680 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000681 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000682 || (info->devname[0] == 'i' && info->devname[1] == 'n'
683 && info->devname[2] == 'i' && info->devname[3] == 't'
684 && info->devname[4] == 'c' && info->devname[5] == 't'
685 && info->devname[6] == 'l' && !info->devname[7]))
686 )
687 return;
688
689 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000690 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000691 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000692 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000693 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000694 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000695 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000696
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000697 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000698 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000699 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000700 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000701 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000702 if (ENABLE_DEVFSD_MODLOAD)
703 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000704 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000705 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000706 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000707 break;
708 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000709 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000710 break;
711 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000712 return;
713 /*break;*/
714 case AC_MKOLDCOMPAT:
715 case AC_MKNEWCOMPAT:
716 case AC_RMOLDCOMPAT:
717 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000718 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000719 break;
720 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000721 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000722 }
723 }
724} /* End Function service_name */
725
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000726static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000727 const struct config_entry_struct *entry)
728/* [SUMMARY] Update permissions for a device entry.
729 <info> The devfs change.
730 <entry> The config file entry.
731 [RETURNS] Nothing.
732*/
733{
734 struct stat statbuf;
735
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000736 if (stat(info->devname, &statbuf) != 0
737 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
738 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000739 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000740 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000741} /* End Function action_permissions */
742
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000743static void action_modload(const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000744 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000745/* [SUMMARY] Load a module.
746 <info> The devfs change.
747 <entry> The config file entry.
748 [RETURNS] Nothing.
749*/
750{
751 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000752
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000753 argv[0] = (char*)MODPROBE;
754 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
755 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
756 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000757 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000758 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000759
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000760 wait4pid(xspawn(argv));
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000761 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000762} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000763
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000764static void action_execute(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000765 const struct config_entry_struct *entry,
766 const regmatch_t *regexpr, unsigned int numexpr)
767/* [SUMMARY] Execute a programme.
768 <info> The devfs change.
769 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000770 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000771 device name.
772 <numexpr> The number of elements within <<regexpr>>.
773 [RETURNS] Nothing.
774*/
775{
776 unsigned int count;
777 struct get_variable_info gv_info;
778 char *argv[MAX_ARGS + 1];
779 char largv[MAX_ARGS + 1][STRING_LENGTH];
780
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000781 gv_info.info = info;
782 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000783 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
784 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
785 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000786 entry->u.execute.argv[count],
787 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000788 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000789 argv[count] = largv[count];
790 }
791 argv[count] = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000792 wait4pid(spawn(argv));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000793} /* End Function action_execute */
794
795
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000796static void action_copy(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000797 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000798 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000799/* [SUMMARY] Copy permissions.
800 <info> The devfs change.
801 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000802 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000803 device name.
804 <numexpr> The number of elements in <<regexpr>>.
805 [RETURNS] Nothing.
806*/
807{
808 mode_t new_mode;
809 struct get_variable_info gv_info;
810 struct stat source_stat, dest_stat;
811 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000812 int ret = 0;
813
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000814 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000815
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000816 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000817 return;
818 gv_info.info = info;
819 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000820
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000821 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
822 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000823 get_variable, &gv_info, gv_info.devname,
824 regexpr, numexpr);
825
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000826 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000827 get_variable, &gv_info, gv_info.devname,
828 regexpr, numexpr);
829
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000830 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000831 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000832 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000833 new_mode = source_stat.st_mode & ~S_ISVTX;
834 if (info->type == DEVFSD_NOTIFY_CREATE)
835 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000836 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000837 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000838 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000839 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000840 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000841} /* End Function action_copy */
842
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000843static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000844/* [SUMMARY] Process a compatibility request.
845 <info> The devfs change.
846 <action> The action to take.
847 [RETURNS] Nothing.
848*/
849{
Rob Landley1ba19d62005-10-08 17:42:35 +0000850 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000851 const char *compat_name = NULL;
852 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000853 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000854 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000855 int mode, host, bus, target, lun;
856 unsigned int i;
857 char rewind_;
858 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000859 static const char *const fmt[] = {
860 NULL ,
861 "sg/c%db%dt%du%d", /* scsi/generic */
862 "sd/c%db%dt%du%d", /* scsi/disc */
863 "sr/c%db%dt%du%d", /* scsi/cd */
864 "sd/c%db%dt%du%dp%d", /* scsi/part */
865 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
866 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
867 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
868 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
869 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
870 NULL
871 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000872
873 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000874 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000875 case AC_MKOLDCOMPAT:
876 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000877 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000878 break;
879 case AC_MKNEWCOMPAT:
880 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000881 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000882 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000883
884 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000885 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000886 return;
887
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000888 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
889 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000890 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000891 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000892
Eric Andersenf18bd892003-12-19 11:07:59 +0000893
894 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000895 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
896 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000897
898 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000899 if (i == 4 || i == 8)
900 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000901
902 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000903 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000904 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000905 if (rewind_ != 'n')
906 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000907 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000908 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000909 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000910 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000911 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000912 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000913 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000914
Eric Andersenf18bd892003-12-19 11:07:59 +0000915 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000916 if (i == 9)
917 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000918 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000919 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000920
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000921 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000922 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000923
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000924 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000925 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000926 case AC_MKOLDCOMPAT:
927 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000928 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000929 break;
930 case AC_RMOLDCOMPAT:
931 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000932 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +0000933 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000934 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000935 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000936 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000937 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000938} /* End Function action_compat */
939
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000940static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000941{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000942 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000943 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000945 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000946 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000947 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000948 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000949 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000950 copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000951
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000952 if (S_ISDIR(source_stat.st_mode))
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000953 dir_operation(RESTORE, spath, rootlen,NULL);
954}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000955
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000956
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000957static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000958 mode_t new_mode,
959 const char *sourcepath, const struct stat *source_stat)
960/* [SUMMARY] Copy an inode.
961 <destpath> The destination path. An existing inode may be deleted.
962 <dest_stat> The destination stat(2) information.
963 <new_mode> The desired new mode for the destination.
964 <sourcepath> The source path.
965 <source_stat> The source stat(2) information.
966 [RETURNS] TRUE on success, else FALSE.
967*/
968{
Eric Andersenf18bd892003-12-19 11:07:59 +0000969 int source_len, dest_len;
970 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
971 int fd, val;
972 struct sockaddr_un un_addr;
973 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000974
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000975 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000976 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000977 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000978 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
979 if ((source_len < 0)
980 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000981 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000982 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000983 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000984 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000985 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
986 unlink(destpath);
987 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000988 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000989 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000990 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000991 chmod(destpath, new_mode & ~S_IFMT);
992 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000993 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000994 }
995 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000996 unlink(destpath);
997 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000998 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000999 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1000 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001001 break;
1002 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001003 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
1004 val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
1005 close(fd);
1006 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001008 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001009 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001010 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1011 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001012 break;
1013 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001014 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001015 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001016 break;
1017 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001018 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1019 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001020 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001021 close(fd);
1022 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001023 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001024 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001025 case S_IFBLK:
1026 case S_IFCHR:
1027 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001028 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001029 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001030 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001031 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001032 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001033 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001034do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001035 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001036 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001037 /*break;*/
1038 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001039 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001040} /* End Function copy_inode */
1041
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001042static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001043/* [SUMMARY] Free the configuration information.
1044 [RETURNS] Nothing.
1045*/
1046{
1047 struct config_entry_struct *c_entry;
1048 void *next;
1049
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001050 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001051 unsigned int count;
1052
1053 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001054 regfree(&c_entry->preg);
1055 if (c_entry->action.what == AC_EXECUTE) {
1056 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001057 if (c_entry->u.execute.argv[count] == NULL)
1058 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001059 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001060 }
1061 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001062 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001063 }
1064 first_config = NULL;
1065 last_config = NULL;
1066} /* End Function free_config */
1067
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001068static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001069/* [SUMMARY] Convert a string to a UID or GID value.
1070 <flag> "UID" or "GID".
1071 <string> The string.
1072 [RETURNS] The UID or GID value.
1073*/
1074{
1075 struct passwd *pw_ent;
1076 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001077 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001078
Rob Landley1ba19d62005-10-08 17:42:35 +00001079 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001080 msg = "user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001081
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001082 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001083 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001084
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001085 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001086 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001087
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001088 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001089 return grp_ent->gr_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001090 else if (ENABLE_DEVFSD_VERBOSE)
1091 msg = "group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001092
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001093 if (ENABLE_DEVFSD_VERBOSE)
Rob Landley1ba19d62005-10-08 17:42:35 +00001094 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001095 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001096}/* End Function get_uid_gid */
1097
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001098static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001099/* [SUMMARY] Convert a string to a mode value.
1100 <string> The string.
1101 [RETURNS] The mode value.
1102*/
1103{
1104 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001105 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001106
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001107 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001108 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001109 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001110 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001111
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001112 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001113 i = S_IRUSR;
1114 while (i > 0) {
1115 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1116 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001117 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001118 string++;
1119 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001120 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001121} /* End Function get_mode */
1122
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001123static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001124{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001125 caught_signal = TRUE;
1126 if (sig == SIGHUP)
1127 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001128
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001129 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001130} /* End Function signal_handler */
1131
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001132static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001133{
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001134 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001135 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001136
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001137 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001138 const char *field_names[] = {
1139 "hostname", "mntpt", "devpath", "devname",
1140 "uid", "gid", "mode", hostname, mount_point,
1141 gv_info->devpath, gv_info->devname, NULL
1142 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001143 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001144
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001145 if (!hostname)
1146 hostname = safe_gethostname();
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001147 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001148 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001149
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001150 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001151 return NULL;
1152 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001153 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001154
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001155 if (i == 4)
1156 sprintf(sbuf, "%u", gv_info->info->uid);
1157 else if (i == 5)
1158 sprintf(sbuf, "%u", gv_info->info->gid);
1159 else if (i == 6)
1160 sprintf(sbuf, "%o", gv_info->info->mode);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001161 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001162} /* End Function get_variable */
1163
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001164static void service(struct stat statbuf, char *path)
1165{
1166 struct devfsd_notify_struct info;
1167
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001168 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001169 info.type = DEVFSD_NOTIFY_REGISTERED;
1170 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001171 info.major = major(statbuf.st_rdev);
1172 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001173 info.uid = statbuf.st_uid;
1174 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001175 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1176 info.namelen = strlen(info.devname);
1177 service_name(&info);
1178 if (S_ISDIR(statbuf.st_mode))
1179 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001180}
1181
1182static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001183/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001184 <flag> To choose which function to perform
1185 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001186 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001187 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001188 [RETURNS] Nothing.
1189*/
1190{
1191 struct stat statbuf;
1192 DIR *dp;
1193 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001194 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001196 dp = warn_opendir(dir_name);
1197 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001198 return;
1199
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001200 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001201
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001202 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001203 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001204 path = concat_path_file(dir_name, de->d_name);
1205 if (lstat(path, &statbuf) == 0) {
1206 switch (type) {
1207 case SERVICE:
1208 service(statbuf, path);
1209 break;
1210 case RESTORE:
1211 restore(path, statbuf, var);
1212 break;
1213 case READ_CONFIG:
1214 read_config_file(path, var, event_mask);
1215 break;
1216 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001217 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001218 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001219 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001220 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001221} /* End Function do_scan_and_service */
1222
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001223static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001224/* [SUMMARY] Create a symlink, creating intervening directories as required.
1225 <oldpath> The string contained in the symlink.
1226 <newpath> The name of the new symlink.
1227 [RETURNS] 0 on success, else -1.
1228*/
1229{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001230 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001231 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001232
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001233 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001234 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001235 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001236 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001237 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001238} /* End Function mksymlink */
1239
1240
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001241static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001242/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001243 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001244 [RETURNS] TRUE on success, else FALSE.
1245*/
1246{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001247 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001248 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001249 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001250} /* End Function make_dir_tree */
1251
1252static int expand_expression(char *output, unsigned int outsize,
1253 const char *input,
1254 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001255 void *info,
1256 const char *devname,
1257 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001258/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001259 <output> The output expanded expression is written here.
1260 <length> The size of the output buffer.
1261 <input> The input expression. This may equal <<output>>.
1262 <get_variable> A function which will be used to get variable values. If
1263 this returns NULL, the environment is searched instead. If this is NULL,
1264 only the environment is searched.
1265 <info> An arbitrary pointer passed to <<get_variable>>.
1266 <devname> Device name; specifically, this is the string that contains all
1267 of the regular subexpressions.
1268 <ex> Array of start / end offsets into info->devname for each subexpression
1269 <numexp> Number of regular subexpressions found in <<devname>>.
1270 [RETURNS] TRUE on success, else FALSE.
1271*/
1272{
1273 char temp[STRING_LENGTH];
1274
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001275 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001276 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001277 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001278 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001279} /* End Function expand_expression */
1280
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001281static void expand_regexp(char *output, size_t outsize, const char *input,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001282 const char *devname,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001283 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001284/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1285 <output> The output expanded expression is written here.
1286 <outsize> The size of the output buffer.
1287 <input> The input expression. This may NOT equal <<output>>, because
1288 supporting that would require yet another string-copy. However, it's not
1289 hard to write a simple wrapper function to add this functionality for those
1290 few cases that need it.
1291 <devname> Device name; specifically, this is the string that contains all
1292 of the regular subexpressions.
1293 <ex> An array of start and end offsets into <<devname>>, one for each
1294 subexpression
1295 <numex> Number of subexpressions in the offset-array <<ex>>.
1296 [RETURNS] Nothing.
1297*/
1298{
1299 const char last_exp = '0' - 1 + numex;
1300 int c = -1;
1301
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001302 /* Guarantee NULL termination by writing an explicit '\0' character into
1303 the very last byte */
1304 if (outsize)
1305 output[--outsize] = '\0';
1306 /* Copy the input string into the output buffer, replacing '\\' with '\'
1307 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1308 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001309 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001310 c = *input;
1311 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001312 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001313 c = *input;
1314 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001315 if (c != '\\') {
1316 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001317 const regmatch_t *subexp = ex + (c - '0');
1318 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1319
1320 /* Range checking */
1321 if (sublen > outsize)
1322 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001323 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001324 output += sublen;
1325 outsize -= sublen;
1326 }
1327 continue;
1328 }
1329 }
1330 *output = c;
1331 ++output;
1332 --outsize;
1333 } /* while */
1334} /* End Function expand_regexp */
1335
1336
1337/* from compat_name.c */
1338
1339struct translate_struct
1340{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001341 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001342 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001343 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001344};
1345
1346static struct translate_struct translate_table[] =
1347{
1348 {"sound/", NULL},
1349 {"printers/", "lp%s"},
1350 {"v4l/", NULL},
1351 {"parports/", "parport%s"},
1352 {"fb/", "fb%s"},
1353 {"netlink/", NULL},
1354 {"loop/", "loop%s"},
1355 {"floppy/", "fd%s"},
1356 {"rd/", "ram%s"},
1357 {"md/", "md%s"}, /* Meta-devices */
1358 {"vc/", "tty%s"},
1359 {"misc/", NULL},
1360 {"isdn/", NULL},
1361 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1362 {"i2c/", "i2c-%s"},
1363 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1364 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1365 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1366 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1367 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1368 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1369 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1370 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1371 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1372 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1373 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1374 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1375 {"input/js", "js%s"}, /* Joystick driver */
1376 {NULL, NULL}
1377};
1378
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001379const char *get_old_name(const char *devname, unsigned int namelen,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001380 char *buffer, unsigned int major, unsigned int minor)
1381/* [SUMMARY] Translate a kernel-supplied name into an old name.
1382 <devname> The device name provided by the kernel.
1383 <namelen> The length of the name.
1384 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1385 <major> The major number for the device.
1386 <minor> The minor number for the device.
1387 [RETURNS] A pointer to the old name if known, else NULL.
1388*/
1389{
1390 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001391 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001392 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001393 unsigned int i;
1394 char mode;
1395 int indexx;
1396 const char *pty1;
1397 const char *pty2;
1398 size_t len;
1399 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001400 static const char *const fmt[] = {
1401 NULL ,
1402 "sg%u", /* scsi/generic */
1403 NULL, /* scsi/disc */
1404 "sr%u", /* scsi/cd */
1405 NULL, /* scsi/part */
1406 "nst%u%c", /* scsi/mt */
1407 "hd%c" , /* ide/host/disc */
1408 "hd%c" , /* ide/host/cd */
1409 "hd%c%s", /* ide/host/part */
1410 "%sht%d", /* ide/host/mt */
1411 "sbpcd%u", /* sbp/ */
1412 "vcs%s", /* vcc/ */
1413 "%cty%c%c", /* pty/ */
1414 NULL
1415 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001416
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001417 for (trans = translate_table; trans->match != NULL; ++trans) {
1418 len = strlen(trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001419
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001420 if (strncmp(devname, trans->match, len) == 0) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001421 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001422 return devname + len;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001423 sprintf(buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001424 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001425 }
1426 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001427
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001428 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001429 i = scan_dev_name(devname, namelen, ptr);
1430
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001431 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001432 compat_name = buffer;
1433 else
1434 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001435
Eric Andersenf18bd892003-12-19 11:07:59 +00001436 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001437 if (i == 1 || i == 3 || i == 10)
1438 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001439
1440 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001441 if (i == 2 || i == 4)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001442 compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001443
1444 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001445 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001446 mode = ptr[2];
1447 if (mode == 'n')
1448 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001449 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001450 if (devname[namelen - 1] != 'n')
1451 ++compat_name;
1452 }
1453 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001454 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001455 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001456 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001457
1458 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001459 if (i == 9)
1460 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001461
1462 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001463 if (i == 11) {
1464 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001465 if (buffer[3] == '0')
1466 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001467 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001468 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001469 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001470 pty1 = "pqrstuvwxyzabcde";
1471 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001472 indexx = atoi(devname + 5);
1473 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001474 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001475 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001476} /* End Function get_old_name */
1477
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001478static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001479/* [SUMMARY] Get the old IDE name for a device.
1480 <major> The major number for the device.
1481 <minor> The minor number for the device.
1482 [RETURNS] The drive letter.
1483*/
1484{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001485 char letter = 'y'; /* 121 */
1486 char c = 'a'; /* 97 */
1487 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001488
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001489 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1490 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001491 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1492 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1493 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1494 || i == IDE9_MAJOR
1495 ) {
1496 if ((unsigned int)i == major) {
1497 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001498 break;
1499 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001500 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001501 }
1502 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001503 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001504
1505 if (minor > 63)
1506 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001507 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001508} /* End Function get_old_ide_name */
1509
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001510static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001511 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001512 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001513/* [SUMMARY] Write the old SCSI disc name to a buffer.
1514 <buffer> The buffer to write to.
1515 <major> The major number for the device.
1516 <minor> The minor number for the device.
1517 <part> The partition string. Must be "" for a whole-disc entry.
1518 [RETURNS] A pointer to the buffer on success, else NULL.
1519*/
1520{
1521 unsigned int disc_index;
1522
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001523 if (major == 8) {
1524 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001525 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001526 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001527 if ((major > 64) && (major < 72)) {
1528 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001529 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001530 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001531 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001532 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001533 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001534 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001535 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001536} /* End Function write_old_sd_name */
1537
1538
1539/* expression.c */
1540
1541/*EXPERIMENTAL_FUNCTION*/
1542
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001543int st_expr_expand(char *output, unsigned int length, const char *input,
1544 const char *(*get_variable_func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001545 void *info),
1546 void *info)
1547/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1548 <output> The output expanded expression is written here.
1549 <length> The size of the output buffer.
1550 <input> The input expression. This may equal <<output>>.
1551 <get_variable> A function which will be used to get variable values. If
1552 this returns NULL, the environment is searched instead. If this is NULL,
1553 only the environment is searched.
1554 <info> An arbitrary pointer passed to <<get_variable>>.
1555 [RETURNS] TRUE on success, else FALSE.
1556*/
1557{
1558 char ch;
1559 unsigned int len;
1560 unsigned int out_pos = 0;
1561 const char *env;
1562 const char *ptr;
1563 struct passwd *pwent;
1564 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1565
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001566 if (length > BUFFER_SIZE)
1567 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001568 for (; TRUE; ++input) {
1569 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001570 case '$':
1571 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001572 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001573 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001574 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001575 break;
1576 case '~':
1577 /* Home directory expansion */
1578 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001579 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001580 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001581 env = getenv("HOME");
1582 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001583 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001584 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001585 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001586 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001587 if (len + out_pos >= length)
1588 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001589 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001590 out_pos += len;
1591 continue;
1592 }
1593 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001594 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001595 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001596 len = ptr - input;
1597 if (len >= sizeof tmp)
1598 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001599 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001600 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001601 pwent = getpwnam(tmp);
1602 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001603 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001604 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001605 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001606 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001607 if (len + out_pos >= length)
1608 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001609 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001610 out_pos += len;
1611 break;
1612 case '\0':
1613 /* Falltrough */
1614 default:
1615 if (out_pos >= length)
1616 goto st_expr_expand_out;
1617 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001618 if (ch == '\0') {
1619 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001620 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001621 }
1622 break;
1623 /* esac */
1624 }
1625 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001626 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001627st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001628 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001629 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001630} /* End Function st_expr_expand */
1631
1632
1633/* Private functions follow */
1634
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001635static const char *expand_variable(char *buffer, unsigned int length,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001636 unsigned int *out_pos, const char *input,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001637 const char *(*func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001638 void *info),
1639 void *info)
1640/* [SUMMARY] Expand a variable.
1641 <buffer> The buffer to write to.
1642 <length> The length of the output buffer.
1643 <out_pos> The current output position. This is updated.
1644 <input> A pointer to the input character pointer.
1645 <func> A function which will be used to get variable values. If this
1646 returns NULL, the environment is searched instead. If this is NULL, only
1647 the environment is searched.
1648 <info> An arbitrary pointer passed to <<func>>.
1649 <errfp> Diagnostic messages are written here.
1650 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1651*/
1652{
1653 char ch;
1654 int len;
1655 unsigned int open_braces;
1656 const char *env, *ptr;
1657 char tmp[STRING_LENGTH];
1658
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001659 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001660 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001661 /* Special case for "$$": PID */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001662 sprintf(tmp, "%d",(int) getpid());
1663 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001664 if (len + *out_pos >= length)
1665 goto expand_variable_out;
1666
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001667 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001668 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001669 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001670 }
1671 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001672 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001673 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001674 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001675 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001676 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001677 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001678 goto expand_variable_out;
1679
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001680 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001682 env = get_variable_v2(tmp, func, info);
1683 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001684 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001685 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001686 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001687 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001688 if (len + *out_pos >= length)
1689 goto expand_variable_out;
1690
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001691 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001692 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001693 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001694 }
1695 /* Variable in braces: check for ':' tricks */
1696 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001697 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001698 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001699 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001700 /* Must be simple variable expansion with "${var}" */
1701 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001702 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001703 goto expand_variable_out;
1704
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001705 safe_memcpy(tmp, input, len);
1706 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001707 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001708 return NULL;
1709 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001710 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001711 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001712 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001713 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001714 }
1715 /* It's that handy "${var:-word}" expression. Check if var is defined */
1716 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001717 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001718 goto expand_variable_out;
1719
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001720 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001721 /* Move input pointer to ':' */
1722 input = ptr;
1723 /* First skip to closing brace, taking note of nested expressions */
1724 ptr += 2;
1725 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001726 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1727 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001728 case '{':
1729 ++open_braces;
1730 break;
1731 case '}':
1732 --open_braces;
1733 break;
1734 case '\0':
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001735 info_logger(LOG_INFO,"\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001736 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001737 default:
1738 break;
1739 }
1740 }
1741 --ptr;
1742 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001743 env = get_variable_v2(tmp, func, info);
1744 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001745 /* Found environment variable, so skip the input to the closing brace
1746 and return the variable */
1747 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001748 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001749 if (len + *out_pos >= length)
1750 goto expand_variable_out;
1751
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001752 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001753 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001754 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001755 }
1756 /* Environment variable was not found, so process word. Advance input
1757 pointer to start of word in "${var:-word}" */
1758 input += 2;
1759 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001760 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001761 goto expand_variable_out;
1762
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001763 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001764 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001765 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001766 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001767 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001768 if (len + *out_pos >= length)
1769 goto expand_variable_out;
1770
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001771 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001772 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001773 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001774expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001775 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001776 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001777} /* End Function expand_variable */
1778
1779
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001780static const char *get_variable_v2(const char *variable,
1781 const char *(*func)(const char *variable, void *info),
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001782 void *info)
1783/* [SUMMARY] Get a variable from the environment or .
1784 <variable> The variable name.
1785 <func> A function which will be used to get the variable. If this returns
1786 NULL, the environment is searched instead. If this is NULL, only the
1787 environment is searched.
1788 [RETURNS] The value of the variable on success, else NULL.
1789*/
1790{
1791 const char *value;
1792
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001793 if (func != NULL) {
1794 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001795 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001796 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001797 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001798 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001799} /* End Function get_variable */
1800
1801/* END OF CODE */