blob: 9990142c215effa1f9e50e7e4b78a45204b507d0 [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*/
56
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000057//#include <sys/wait.h>
58//#include <sys/ioctl.h>
59//#include <sys/socket.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000060#include <sys/un.h>
61#include <dirent.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000062#include <syslog.h>
Eric Andersen2af70002003-10-09 21:02:23 +000063#include <sys/sysmacros.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000064#include "libbb.h"
65#include "xregex.h"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000066
Eric Andersen2af70002003-10-09 21:02:23 +000067
68/* Various defines taken from linux/major.h */
69#define IDE0_MAJOR 3
70#define IDE1_MAJOR 22
71#define IDE2_MAJOR 33
72#define IDE3_MAJOR 34
73#define IDE4_MAJOR 56
74#define IDE5_MAJOR 57
75#define IDE6_MAJOR 88
76#define IDE7_MAJOR 89
77#define IDE8_MAJOR 90
78#define IDE9_MAJOR 91
79
80
81/* Various defines taken from linux/devfs_fs.h */
82#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
83#define DEVFSD_IOCTL_BASE 'd'
84/* These are the various ioctls */
85#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
86#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
87#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000088#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000089#define DEVFSD_NOTIFY_REGISTERED 0
90#define DEVFSD_NOTIFY_UNREGISTERED 1
91#define DEVFSD_NOTIFY_ASYNC_OPEN 2
92#define DEVFSD_NOTIFY_CLOSE 3
93#define DEVFSD_NOTIFY_LOOKUP 4
94#define DEVFSD_NOTIFY_CHANGE 5
95#define DEVFSD_NOTIFY_CREATE 6
96#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +000097#define DEVFS_PATHLEN 1024
98/* Never change this otherwise the binary interface will change */
99
Eric Andersen2af70002003-10-09 21:02:23 +0000100struct devfsd_notify_struct
101{ /* Use native C types to ensure same types in kernel and user space */
102 unsigned int type; /* DEVFSD_NOTIFY_* value */
103 unsigned int mode; /* Mode of the inode or device entry */
104 unsigned int major; /* Major number of device entry */
105 unsigned int minor; /* Minor number of device entry */
106 unsigned int uid; /* Uid of process, inode or device entry */
107 unsigned int gid; /* Gid of process, inode or device entry */
108 unsigned int overrun_count; /* Number of lost events */
109 unsigned int namelen; /* Number of characters not including '\0' */
110 /* The device name MUST come last */
111 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
112};
113
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000114#define BUFFER_SIZE 16384
115#define DEVFSD_VERSION "1.3.25"
116#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000117#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000118#define MODPROBE_SWITCH_1 "-k"
119#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000120#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000121#define MAX_ARGS (6 + 1)
122#define MAX_SUBEXPR 10
123#define STRING_LENGTH 255
124
125/* for get_uid_gid() */
126#define UID 0
127#define GID 1
128
Rob Landley1ba19d62005-10-08 17:42:35 +0000129/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000130# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000131# define NO_DIE 0
132
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000133/* for dir_operation() */
134#define RESTORE 0
135#define SERVICE 1
136#define READ_CONFIG 2
137
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000138/* Update only after changing code to reflect new protocol */
139#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
140
141/* Compile-time check */
142#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
143#error protocol version mismatch. Update your kernel headers
144#endif
145
146#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000147#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000148#define AC_EXECUTE 2
149#define AC_MFUNCTION 3 /* not supported by busybox */
150#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000151#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000152#define AC_IGNORE 6
153#define AC_MKOLDCOMPAT 7
154#define AC_MKNEWCOMPAT 8
155#define AC_RMOLDCOMPAT 9
156#define AC_RMNEWCOMPAT 10
157#define AC_RESTORE 11
158
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000159struct permissions_type
160{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000161 mode_t mode;
162 uid_t uid;
163 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000164};
165
166struct execute_type
167{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000168 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000169};
170
171struct copy_type
172{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000173 const char *source;
174 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000175};
176
177struct action_type
178{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000179 unsigned int what;
180 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000181};
182
183struct config_entry_struct
184{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000185 struct action_type action;
186 regex_t preg;
187 union
188 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000189 struct permissions_type permissions;
190 struct execute_type execute;
191 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000192 }
193 u;
194 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000195};
196
197struct get_variable_info
198{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000199 const struct devfsd_notify_struct *info;
200 const char *devname;
201 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000202};
203
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000204static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000205static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000206static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000207static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000208static int mksymlink(const char *oldpath, const char *newpath);
209static void read_config_file(char *path, int optional, unsigned long *event_mask);
210static void process_config_line(const char *, unsigned long *);
211static int do_servicing(int, unsigned long);
212static void service_name(const struct devfsd_notify_struct *);
213static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
214static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000215 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000216static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
217static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000218 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000219static void action_compat(const struct devfsd_notify_struct *, unsigned);
220static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000221static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000222static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
223static mode_t get_mode(const char *);
224static void signal_handler(int);
225static const char *get_variable(const char *, void *);
226static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000227static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000228 const char *, const regmatch_t *, unsigned);
229static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000230static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000231 const char *(*)(const char *, void *), void *);
232static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
233static char get_old_ide_name(unsigned , unsigned);
234static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000235
236/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000237static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000238static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000239static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
240static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000241
242/* Structs and vars */
243static struct config_entry_struct *first_config = NULL;
244static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000245static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000246static volatile int caught_signal = FALSE;
247static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000248static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000249 const char *dest;
250 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000251} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000252 {"/proc/self/fd", "fd"},
253 {"fd/0", "stdin"},
254 {"fd/1", "stdout"},
255 {"fd/2", "stderr"},
256 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000257};
258
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000259static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000260 unsigned int type; /* The DEVFSD_NOTIFY_* value */
261 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000262} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000263 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
264 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
265 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
266 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
267 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
268 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
269 {DEVFSD_NOTIFY_CREATE, "CREATE"},
270 {DEVFSD_NOTIFY_DELETE, "DELETE"},
271 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000272};
273
Rob Landley1ba19d62005-10-08 17:42:35 +0000274/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000275
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000276static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
277static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
278static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
279static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000280
Rob Landley1ba19d62005-10-08 17:42:35 +0000281/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000282#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
283#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
284#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
285#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
286#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
287#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000288#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000289#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000290#define msg_logger(p, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000291#define msg_logger_and_die(p, fmt, args...) exit(1)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000292#define error_logger(p, fmt, args...)
293#define error_logger_and_die(p, fmt, args...) exit(1)
Eric Andersenf18bd892003-12-19 11:07:59 +0000294#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000295
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000296static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000297{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000298 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000299 dest[len] = '\0';
300}
301
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000302static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000303{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000304 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000305 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000306 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000307 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000308 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000309 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000310 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000311 return 5 + addendum;
312 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000313}
314
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000315static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000316{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000317 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
318 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
319 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
320 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000321 return 1;
322 return scan_dev_name_common(d, n, 0, ptr);
323 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000324 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
325 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
326 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000327 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000328 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000329 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000330 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000331 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000332 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000333 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000334 return 0;
335}
336
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000337/* Public functions follow */
338
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000339int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000340int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000341{
342 int print_version = FALSE;
343 int do_daemon = TRUE;
344 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000345 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000346 int fd, proto_rev, count;
347 unsigned long event_mask = 0;
348 struct sigaction new_action;
349 struct initial_symlink_struct *curr;
350
351 if (argc < 2)
352 bb_show_usage();
353
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000354 for (count = 2; count < argc; ++count) {
355 if (argv[count][0] == '-') {
356 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000357 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000358 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000359 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
360 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000361 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000362 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
363 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000364 else
365 bb_show_usage();
366 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000367 }
368
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000369 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000370
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000371 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000372
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000373 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000374 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000375 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000376
377 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000378 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000379 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000380
381 /* NB: The check for CONFIG_FILE is done in read_config_file() */
382
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000383 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000384 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000385 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000386 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000387 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000388 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000389 exit(EXIT_SUCCESS); /* -v */
390 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000391 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000392 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000393
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000394 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000395 new_action.sa_flags = 0;
396
397 /* Set up SIGHUP and SIGUSR1 handlers */
398 new_action.sa_handler = signal_handler;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000399 if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000400 bb_error_msg_and_die("sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000401
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000402 printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000403
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000404 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000405 umask(0);
406 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000407 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000408 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000409
Rob Landley1ba19d62005-10-08 17:42:35 +0000410 if (ENABLE_DEVFSD_FG_NP && no_polling)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000411 exit(0);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000412
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000413 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
414 logmode = LOGMODE_BOTH;
415 else if (do_daemon == TRUE)
416 logmode = LOGMODE_SYSLOG;
417 /* This is the default */
418 /*else
419 logmode = LOGMODE_STDIO; */
420
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000421 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000422 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000423 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000424 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000425 } else if (ENABLE_DEVFSD_FG_NP) {
426 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000427 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000428
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000429 while (TRUE) {
430 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000431
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000432 free_config();
433 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000434 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000435 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000436 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000437 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000438} /* End Function main */
439
440
441/* Private functions follow */
442
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000443static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000444/* [SUMMARY] Read a configuration database.
445 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000446 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000447 <optional> If TRUE, the routine will silently ignore a missing config file.
448 <event_mask> The event mask is written here. This is not initialised.
449 [RETURNS] Nothing.
450*/
451{
452 struct stat statbuf;
453 FILE *fp;
454 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000455 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000456 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000457
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000458 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000459 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000460 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000461 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000462 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000463 p = bb_simplify_path(path);
464 dir_operation(READ_CONFIG, p, 0, event_mask);
465 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000466 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000467 }
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000468 fp = fopen(path, "r");
469 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000470 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000471 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000472 line = buf;
473 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000474 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000475 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000476 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000477 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000478 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000479 } else {
480 goto read_config_file_err;
481 }
482 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000483read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000484 if (optional == 0 && errno == ENOENT)
485 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000486 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000487} /* End Function read_config_file */
488
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000489static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000490/* [SUMMARY] Process a line from a configuration file.
491 <line> The configuration line.
492 <event_mask> The event mask is written here. This is not initialised.
493 [RETURNS] Nothing.
494*/
495{
496 int num_args, count;
497 struct config_entry_struct *new;
498 char p[MAX_ARGS][STRING_LENGTH];
499 char when[STRING_LENGTH], what[STRING_LENGTH];
500 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000501 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000502 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000503 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000504
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000505 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000506 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000507 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
508 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
509 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
510 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000511
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000512 for (count = 0; count < MAX_ARGS; ++count)
513 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000514 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000515 when, name, what,
516 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000517
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000518 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000519
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000520 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000521 if (i == 0) {
522 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000523 *event_mask = 0;
524 return;
525 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000526
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000527 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000528 goto process_config_line_err;
529
Eric Andersenf18bd892003-12-19 11:07:59 +0000530 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000531 if (i == 1 || i == 2) {
532 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000533 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000534 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000535 return;
536 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000537 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000538 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000539 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000540 return;
541 }
542 if (num_args < 3)
543 goto process_config_line_err;
544
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000545 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000546
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000547 for (count = 0; event_types[count].config_name != NULL; ++count) {
548 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000549 continue;
550 new->action.when = event_types[count].type;
551 break;
552 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000553 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000554 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000555 goto process_config_line_err;
556 }
557
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000558 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000559
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000560 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000561 case 4: /* "PERMISSIONS" */
562 new->action.what = AC_PERMISSIONS;
563 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000564 ptr = strchr(p[0], '.');
565 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000566 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000567 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000568 }
569
570 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000571 new->u.permissions.uid = get_uid_gid(UID, p[0]);
572 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000573 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000574 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000575 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000576 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000577 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000578 the device name) to the module loading facility. In addition,
579 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000580 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000581 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000582 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000583 case 6: /* EXECUTE */
584 new->action.what = AC_EXECUTE;
585 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000586
Eric Andersenf18bd892003-12-19 11:07:59 +0000587 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000588 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000589
Eric Andersenf18bd892003-12-19 11:07:59 +0000590 new->u.execute.argv[num_args] = NULL;
591 break;
592 case 7: /* COPY */
593 new->action.what = AC_COPY;
594 num_args -= 3;
595 if (num_args != 2)
596 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000597
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000598 new->u.copy.source = xstrdup(p[0]);
599 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000600 break;
601 case 8: /* IGNORE */
602 /* FALLTROUGH */
603 case 9: /* MKOLDCOMPAT */
604 /* FALLTROUGH */
605 case 10: /* MKNEWCOMPAT */
606 /* FALLTROUGH */
607 case 11:/* RMOLDCOMPAT */
608 /* FALLTROUGH */
609 case 12: /* RMNEWCOMPAT */
610 /* AC_IGNORE 6
611 AC_MKOLDCOMPAT 7
612 AC_MKNEWCOMPAT 8
613 AC_RMOLDCOMPAT 9
614 AC_RMNEWCOMPAT 10*/
615 new->action.what = i - 2;
616 break;
617 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000618 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000619 goto process_config_line_err;
620 /*esac*/
621 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000622
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000623 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000624
625 *event_mask |= 1 << new->action.when;
626 new->next = NULL;
627 if (first_config == NULL)
628 first_config = new;
629 else
630 last_config->next = new;
631 last_config = new;
632 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000633
634 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000635 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000636} /* End Function process_config_line */
637
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000638static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000639/* [SUMMARY] Service devfs changes until a signal is received.
640 <fd> The open control file.
641 <event_mask> The event mask.
642 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
643*/
644{
645 ssize_t bytes;
646 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000647
Denis Vlasenko856be772007-08-17 08:29:48 +0000648 /* (void*) cast is only in order to match prototype */
649 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000650 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000651 errno = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000652 bytes = read(fd,(char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000653 if (caught_signal)
654 break; /* Must test for this first */
655 if (errno == EINTR)
656 continue; /* Yes, the order is important */
657 if (bytes < 1)
658 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000659 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000660 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000661 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000662 int c_sighup = caught_sighup;
663
664 caught_signal = FALSE;
665 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000666 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000667 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000668 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000669} /* End Function do_servicing */
670
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000671static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000672/* [SUMMARY] Service a single devfs change.
673 <info> The devfs change.
674 [RETURNS] Nothing.
675*/
676{
677 unsigned int n;
678 regmatch_t mbuf[MAX_SUBEXPR];
679 struct config_entry_struct *entry;
680
Rob Landley1ba19d62005-10-08 17:42:35 +0000681 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000682 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000683
684 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000685 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000686 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000687 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000688 || (info->devname[0] == 'i' && info->devname[1] == 'n'
689 && info->devname[2] == 'i' && info->devname[3] == 't'
690 && info->devname[4] == 'c' && info->devname[5] == 't'
691 && info->devname[6] == 'l' && !info->devname[7]))
692 )
693 return;
694
695 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000696 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000697 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000698 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000700 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000701 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000702
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000703 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000704 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000705 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000706 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000707 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000708 if (ENABLE_DEVFSD_MODLOAD)
709 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000710 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000711 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000712 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000713 break;
714 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000715 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000716 break;
717 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000718 return;
719 /*break;*/
720 case AC_MKOLDCOMPAT:
721 case AC_MKNEWCOMPAT:
722 case AC_RMOLDCOMPAT:
723 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000724 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000725 break;
726 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000727 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000728 }
729 }
730} /* End Function service_name */
731
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000732static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000733 const struct config_entry_struct *entry)
734/* [SUMMARY] Update permissions for a device entry.
735 <info> The devfs change.
736 <entry> The config file entry.
737 [RETURNS] Nothing.
738*/
739{
740 struct stat statbuf;
741
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000742 if (stat(info->devname, &statbuf) != 0
743 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
744 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000745 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000746 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000747} /* End Function action_permissions */
748
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000749static void action_modload(const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000750 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000751/* [SUMMARY] Load a module.
752 <info> The devfs change.
753 <entry> The config file entry.
754 [RETURNS] Nothing.
755*/
756{
757 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000758
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000759 argv[0] = (char*)MODPROBE;
760 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
761 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
762 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000763 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000764 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000765
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000766 wait4pid(xspawn(argv));
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000767 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000768} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000769
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000770static void action_execute(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000771 const struct config_entry_struct *entry,
772 const regmatch_t *regexpr, unsigned int numexpr)
773/* [SUMMARY] Execute a programme.
774 <info> The devfs change.
775 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000776 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000777 device name.
778 <numexpr> The number of elements within <<regexpr>>.
779 [RETURNS] Nothing.
780*/
781{
782 unsigned int count;
783 struct get_variable_info gv_info;
784 char *argv[MAX_ARGS + 1];
785 char largv[MAX_ARGS + 1][STRING_LENGTH];
786
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000787 gv_info.info = info;
788 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000789 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
790 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
791 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000792 entry->u.execute.argv[count],
793 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000794 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000795 argv[count] = largv[count];
796 }
797 argv[count] = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000798 wait4pid(spawn(argv));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000799} /* End Function action_execute */
800
801
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000802static void action_copy(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000803 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000804 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000805/* [SUMMARY] Copy permissions.
806 <info> The devfs change.
807 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000808 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000809 device name.
810 <numexpr> The number of elements in <<regexpr>>.
811 [RETURNS] Nothing.
812*/
813{
814 mode_t new_mode;
815 struct get_variable_info gv_info;
816 struct stat source_stat, dest_stat;
817 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000818 int ret = 0;
819
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000820 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000821
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000822 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000823 return;
824 gv_info.info = info;
825 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000826
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000827 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
828 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000829 get_variable, &gv_info, gv_info.devname,
830 regexpr, numexpr);
831
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000832 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000833 get_variable, &gv_info, gv_info.devname,
834 regexpr, numexpr);
835
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000836 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000837 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000838 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000839 new_mode = source_stat.st_mode & ~S_ISVTX;
840 if (info->type == DEVFSD_NOTIFY_CREATE)
841 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000842 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000843 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000844 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000845 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000846 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000847} /* End Function action_copy */
848
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000849static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000850/* [SUMMARY] Process a compatibility request.
851 <info> The devfs change.
852 <action> The action to take.
853 [RETURNS] Nothing.
854*/
855{
Rob Landley1ba19d62005-10-08 17:42:35 +0000856 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000857 const char *compat_name = NULL;
858 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000859 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000860 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000861 int mode, host, bus, target, lun;
862 unsigned int i;
863 char rewind_;
864 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000865 static const char *const fmt[] = {
866 NULL ,
867 "sg/c%db%dt%du%d", /* scsi/generic */
868 "sd/c%db%dt%du%d", /* scsi/disc */
869 "sr/c%db%dt%du%d", /* scsi/cd */
870 "sd/c%db%dt%du%dp%d", /* scsi/part */
871 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
872 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
873 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
874 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
875 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
876 NULL
877 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000878
879 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000880 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000881 case AC_MKOLDCOMPAT:
882 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000883 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000884 break;
885 case AC_MKNEWCOMPAT:
886 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000887 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000888 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000889
890 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000891 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000892 return;
893
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000894 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
895 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000896 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000897 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000898
Eric Andersenf18bd892003-12-19 11:07:59 +0000899
900 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000901 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
902 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000903
904 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000905 if (i == 4 || i == 8)
906 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000907
908 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000909 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000910 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000911 if (rewind_ != 'n')
912 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000913 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000914 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000915 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000916 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000917 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000918 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000919 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000920
Eric Andersenf18bd892003-12-19 11:07:59 +0000921 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000922 if (i == 9)
923 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000924 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000925 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000926
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000927 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000928 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000929
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000930 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000931 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000932 case AC_MKOLDCOMPAT:
933 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000934 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000935 break;
936 case AC_RMOLDCOMPAT:
937 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000938 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +0000939 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000940 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000941 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000942 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000943 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944} /* End Function action_compat */
945
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000946static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000947{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000948 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000949 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000951 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000952 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000953 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000954 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000955 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000956 copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000957
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000958 if (S_ISDIR(source_stat.st_mode))
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000959 dir_operation(RESTORE, spath, rootlen,NULL);
960}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000961
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000962
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000963static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000964 mode_t new_mode,
965 const char *sourcepath, const struct stat *source_stat)
966/* [SUMMARY] Copy an inode.
967 <destpath> The destination path. An existing inode may be deleted.
968 <dest_stat> The destination stat(2) information.
969 <new_mode> The desired new mode for the destination.
970 <sourcepath> The source path.
971 <source_stat> The source stat(2) information.
972 [RETURNS] TRUE on success, else FALSE.
973*/
974{
Eric Andersenf18bd892003-12-19 11:07:59 +0000975 int source_len, dest_len;
976 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
977 int fd, val;
978 struct sockaddr_un un_addr;
979 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000980
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000981 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000982 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000983 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000984 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
985 if ((source_len < 0)
986 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000987 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000988 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000989 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000990 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000991 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
992 unlink(destpath);
993 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000994 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000995 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000996 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000997 chmod(destpath, new_mode & ~S_IFMT);
998 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000999 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001000 }
1001 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001002 unlink(destpath);
1003 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001004 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001005 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1006 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007 break;
1008 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001009 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
1010 val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
1011 close(fd);
1012 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001013 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001014 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001015 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001016 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1017 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001018 break;
1019 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001020 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001021 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001022 break;
1023 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001024 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1025 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001026 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001027 close(fd);
1028 if (chmod(destpath, new_mode & ~S_IFMT) != 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_IFBLK:
1032 case S_IFCHR:
1033 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001034 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001035 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001036 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001037 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001038 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001039 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001040do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001041 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001042 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001043 /*break;*/
1044 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001045 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001046} /* End Function copy_inode */
1047
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001048static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001049/* [SUMMARY] Free the configuration information.
1050 [RETURNS] Nothing.
1051*/
1052{
1053 struct config_entry_struct *c_entry;
1054 void *next;
1055
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001056 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001057 unsigned int count;
1058
1059 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001060 regfree(&c_entry->preg);
1061 if (c_entry->action.what == AC_EXECUTE) {
1062 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001063 if (c_entry->u.execute.argv[count] == NULL)
1064 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001065 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001066 }
1067 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001068 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001069 }
1070 first_config = NULL;
1071 last_config = NULL;
1072} /* End Function free_config */
1073
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001074static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001075/* [SUMMARY] Convert a string to a UID or GID value.
1076 <flag> "UID" or "GID".
1077 <string> The string.
1078 [RETURNS] The UID or GID value.
1079*/
1080{
1081 struct passwd *pw_ent;
1082 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001083 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001084
Rob Landley1ba19d62005-10-08 17:42:35 +00001085 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001086 msg = "user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001087
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001088 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001089 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001090
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001091 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001092 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001093
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001094 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001095 return grp_ent->gr_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001096 else if (ENABLE_DEVFSD_VERBOSE)
1097 msg = "group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001098
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001099 if (ENABLE_DEVFSD_VERBOSE)
Rob Landley1ba19d62005-10-08 17:42:35 +00001100 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001101 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001102}/* End Function get_uid_gid */
1103
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001104static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001105/* [SUMMARY] Convert a string to a mode value.
1106 <string> The string.
1107 [RETURNS] The mode value.
1108*/
1109{
1110 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001111 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001112
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001113 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001114 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001115 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001116 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001117
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001118 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001119 i = S_IRUSR;
1120 while (i > 0) {
1121 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1122 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001123 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001124 string++;
1125 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001126 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001127} /* End Function get_mode */
1128
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001129static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001130{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001131 caught_signal = TRUE;
1132 if (sig == SIGHUP)
1133 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001134
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001135 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001136} /* End Function signal_handler */
1137
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001138static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001139{
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001140 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
1141
1142 char hostname[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001143 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001144 const char *field_names[] = {
1145 "hostname", "mntpt", "devpath", "devname",
1146 "uid", "gid", "mode", hostname, mount_point,
1147 gv_info->devpath, gv_info->devname, NULL
1148 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001149 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001150
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001151 if (gethostname(hostname, STRING_LENGTH - 1) != 0)
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001152 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001153 error_logger_and_die(LOG_ERR, "gethostname");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001154
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001155 hostname[STRING_LENGTH - 1] = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001156
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001157 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001158 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001159
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001160 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001161 return NULL;
1162 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001163 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001164
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001165 if (i == 4)
1166 sprintf(sbuf, "%u", gv_info->info->uid);
1167 else if (i == 5)
1168 sprintf(sbuf, "%u", gv_info->info->gid);
1169 else if (i == 6)
1170 sprintf(sbuf, "%o", gv_info->info->mode);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001171 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001172} /* End Function get_variable */
1173
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001174static void service(struct stat statbuf, char *path)
1175{
1176 struct devfsd_notify_struct info;
1177
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001178 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001179 info.type = DEVFSD_NOTIFY_REGISTERED;
1180 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001181 info.major = major(statbuf.st_rdev);
1182 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001183 info.uid = statbuf.st_uid;
1184 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001185 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1186 info.namelen = strlen(info.devname);
1187 service_name(&info);
1188 if (S_ISDIR(statbuf.st_mode))
1189 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001190}
1191
1192static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001193/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001194 <flag> To choose which function to perform
1195 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001196 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001197 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001198 [RETURNS] Nothing.
1199*/
1200{
1201 struct stat statbuf;
1202 DIR *dp;
1203 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001204 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001205
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001206 dp = warn_opendir(dir_name);
1207 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001208 return;
1209
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001210 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001211
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001212 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001213 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001214 path = concat_path_file(dir_name, de->d_name);
1215 if (lstat(path, &statbuf) == 0) {
1216 switch (type) {
1217 case SERVICE:
1218 service(statbuf, path);
1219 break;
1220 case RESTORE:
1221 restore(path, statbuf, var);
1222 break;
1223 case READ_CONFIG:
1224 read_config_file(path, var, event_mask);
1225 break;
1226 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001227 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001228 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001229 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001230 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001231} /* End Function do_scan_and_service */
1232
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001233static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001234/* [SUMMARY] Create a symlink, creating intervening directories as required.
1235 <oldpath> The string contained in the symlink.
1236 <newpath> The name of the new symlink.
1237 [RETURNS] 0 on success, else -1.
1238*/
1239{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001240 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001241 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001242
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001243 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001244 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001245 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001246 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001247 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001248} /* End Function mksymlink */
1249
1250
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001251static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001252/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001253 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001254 [RETURNS] TRUE on success, else FALSE.
1255*/
1256{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001257 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001258 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001259 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001260} /* End Function make_dir_tree */
1261
1262static int expand_expression(char *output, unsigned int outsize,
1263 const char *input,
1264 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001265 void *info,
1266 const char *devname,
1267 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001268/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001269 <output> The output expanded expression is written here.
1270 <length> The size of the output buffer.
1271 <input> The input expression. This may equal <<output>>.
1272 <get_variable> A function which will be used to get variable values. If
1273 this returns NULL, the environment is searched instead. If this is NULL,
1274 only the environment is searched.
1275 <info> An arbitrary pointer passed to <<get_variable>>.
1276 <devname> Device name; specifically, this is the string that contains all
1277 of the regular subexpressions.
1278 <ex> Array of start / end offsets into info->devname for each subexpression
1279 <numexp> Number of regular subexpressions found in <<devname>>.
1280 [RETURNS] TRUE on success, else FALSE.
1281*/
1282{
1283 char temp[STRING_LENGTH];
1284
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001285 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001286 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001287 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001288 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001289} /* End Function expand_expression */
1290
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001291static void expand_regexp(char *output, size_t outsize, const char *input,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001292 const char *devname,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001293 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001294/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1295 <output> The output expanded expression is written here.
1296 <outsize> The size of the output buffer.
1297 <input> The input expression. This may NOT equal <<output>>, because
1298 supporting that would require yet another string-copy. However, it's not
1299 hard to write a simple wrapper function to add this functionality for those
1300 few cases that need it.
1301 <devname> Device name; specifically, this is the string that contains all
1302 of the regular subexpressions.
1303 <ex> An array of start and end offsets into <<devname>>, one for each
1304 subexpression
1305 <numex> Number of subexpressions in the offset-array <<ex>>.
1306 [RETURNS] Nothing.
1307*/
1308{
1309 const char last_exp = '0' - 1 + numex;
1310 int c = -1;
1311
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001312 /* Guarantee NULL termination by writing an explicit '\0' character into
1313 the very last byte */
1314 if (outsize)
1315 output[--outsize] = '\0';
1316 /* Copy the input string into the output buffer, replacing '\\' with '\'
1317 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1318 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001319 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001320 c = *input;
1321 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001322 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001323 c = *input;
1324 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001325 if (c != '\\') {
1326 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001327 const regmatch_t *subexp = ex + (c - '0');
1328 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1329
1330 /* Range checking */
1331 if (sublen > outsize)
1332 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001333 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001334 output += sublen;
1335 outsize -= sublen;
1336 }
1337 continue;
1338 }
1339 }
1340 *output = c;
1341 ++output;
1342 --outsize;
1343 } /* while */
1344} /* End Function expand_regexp */
1345
1346
1347/* from compat_name.c */
1348
1349struct translate_struct
1350{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001351 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001352 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001353 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001354};
1355
1356static struct translate_struct translate_table[] =
1357{
1358 {"sound/", NULL},
1359 {"printers/", "lp%s"},
1360 {"v4l/", NULL},
1361 {"parports/", "parport%s"},
1362 {"fb/", "fb%s"},
1363 {"netlink/", NULL},
1364 {"loop/", "loop%s"},
1365 {"floppy/", "fd%s"},
1366 {"rd/", "ram%s"},
1367 {"md/", "md%s"}, /* Meta-devices */
1368 {"vc/", "tty%s"},
1369 {"misc/", NULL},
1370 {"isdn/", NULL},
1371 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1372 {"i2c/", "i2c-%s"},
1373 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1374 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1375 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1376 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1377 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1378 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1379 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1380 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1381 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1382 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1383 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1384 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1385 {"input/js", "js%s"}, /* Joystick driver */
1386 {NULL, NULL}
1387};
1388
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001389const char *get_old_name(const char *devname, unsigned int namelen,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001390 char *buffer, unsigned int major, unsigned int minor)
1391/* [SUMMARY] Translate a kernel-supplied name into an old name.
1392 <devname> The device name provided by the kernel.
1393 <namelen> The length of the name.
1394 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1395 <major> The major number for the device.
1396 <minor> The minor number for the device.
1397 [RETURNS] A pointer to the old name if known, else NULL.
1398*/
1399{
1400 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001401 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001402 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001403 unsigned int i;
1404 char mode;
1405 int indexx;
1406 const char *pty1;
1407 const char *pty2;
1408 size_t len;
1409 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001410 static const char *const fmt[] = {
1411 NULL ,
1412 "sg%u", /* scsi/generic */
1413 NULL, /* scsi/disc */
1414 "sr%u", /* scsi/cd */
1415 NULL, /* scsi/part */
1416 "nst%u%c", /* scsi/mt */
1417 "hd%c" , /* ide/host/disc */
1418 "hd%c" , /* ide/host/cd */
1419 "hd%c%s", /* ide/host/part */
1420 "%sht%d", /* ide/host/mt */
1421 "sbpcd%u", /* sbp/ */
1422 "vcs%s", /* vcc/ */
1423 "%cty%c%c", /* pty/ */
1424 NULL
1425 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001426
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001427 for (trans = translate_table; trans->match != NULL; ++trans) {
1428 len = strlen(trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001429
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001430 if (strncmp(devname, trans->match, len) == 0) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001431 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001432 return devname + len;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001433 sprintf(buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001434 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001435 }
1436 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001437
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001438 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001439 i = scan_dev_name(devname, namelen, ptr);
1440
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001441 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001442 compat_name = buffer;
1443 else
1444 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001445
Eric Andersenf18bd892003-12-19 11:07:59 +00001446 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001447 if (i == 1 || i == 3 || i == 10)
1448 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001449
1450 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001451 if (i == 2 || i == 4)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001452 compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001453
1454 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001455 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001456 mode = ptr[2];
1457 if (mode == 'n')
1458 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001459 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001460 if (devname[namelen - 1] != 'n')
1461 ++compat_name;
1462 }
1463 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001464 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001465 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001466 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001467
1468 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001469 if (i == 9)
1470 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001471
1472 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001473 if (i == 11) {
1474 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001475 if (buffer[3] == '0')
1476 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001477 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001478 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001479 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001480 pty1 = "pqrstuvwxyzabcde";
1481 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001482 indexx = atoi(devname + 5);
1483 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001484 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001485 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001486} /* End Function get_old_name */
1487
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001488static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001489/* [SUMMARY] Get the old IDE name for a device.
1490 <major> The major number for the device.
1491 <minor> The minor number for the device.
1492 [RETURNS] The drive letter.
1493*/
1494{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001495 char letter = 'y'; /* 121 */
1496 char c = 'a'; /* 97 */
1497 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001498
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001499 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1500 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001501 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1502 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1503 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1504 || i == IDE9_MAJOR
1505 ) {
1506 if ((unsigned int)i == major) {
1507 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001508 break;
1509 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001510 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001511 }
1512 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001513 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001514
1515 if (minor > 63)
1516 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001517 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001518} /* End Function get_old_ide_name */
1519
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001520static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001521 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001522 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001523/* [SUMMARY] Write the old SCSI disc name to a buffer.
1524 <buffer> The buffer to write to.
1525 <major> The major number for the device.
1526 <minor> The minor number for the device.
1527 <part> The partition string. Must be "" for a whole-disc entry.
1528 [RETURNS] A pointer to the buffer on success, else NULL.
1529*/
1530{
1531 unsigned int disc_index;
1532
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001533 if (major == 8) {
1534 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001535 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001536 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001537 if ((major > 64) && (major < 72)) {
1538 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001539 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001540 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001541 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001542 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001543 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001544 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001545 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001546} /* End Function write_old_sd_name */
1547
1548
1549/* expression.c */
1550
1551/*EXPERIMENTAL_FUNCTION*/
1552
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001553int st_expr_expand(char *output, unsigned int length, const char *input,
1554 const char *(*get_variable_func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001555 void *info),
1556 void *info)
1557/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1558 <output> The output expanded expression is written here.
1559 <length> The size of the output buffer.
1560 <input> The input expression. This may equal <<output>>.
1561 <get_variable> A function which will be used to get variable values. If
1562 this returns NULL, the environment is searched instead. If this is NULL,
1563 only the environment is searched.
1564 <info> An arbitrary pointer passed to <<get_variable>>.
1565 [RETURNS] TRUE on success, else FALSE.
1566*/
1567{
1568 char ch;
1569 unsigned int len;
1570 unsigned int out_pos = 0;
1571 const char *env;
1572 const char *ptr;
1573 struct passwd *pwent;
1574 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1575
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001576 if (length > BUFFER_SIZE)
1577 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001578 for (; TRUE; ++input) {
1579 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001580 case '$':
1581 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001582 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001583 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001584 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001585 break;
1586 case '~':
1587 /* Home directory expansion */
1588 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001589 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001590 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001591 env = getenv("HOME");
1592 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001593 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001594 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001595 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001596 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001597 if (len + out_pos >= length)
1598 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001599 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001600 out_pos += len;
1601 continue;
1602 }
1603 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001604 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001605 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001606 len = ptr - input;
1607 if (len >= sizeof tmp)
1608 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001609 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001610 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001611 pwent = getpwnam(tmp);
1612 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001613 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001614 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001615 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001616 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001617 if (len + out_pos >= length)
1618 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001619 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001620 out_pos += len;
1621 break;
1622 case '\0':
1623 /* Falltrough */
1624 default:
1625 if (out_pos >= length)
1626 goto st_expr_expand_out;
1627 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001628 if (ch == '\0') {
1629 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001630 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001631 }
1632 break;
1633 /* esac */
1634 }
1635 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001636 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001637st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001638 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001639 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001640} /* End Function st_expr_expand */
1641
1642
1643/* Private functions follow */
1644
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001645static const char *expand_variable(char *buffer, unsigned int length,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001646 unsigned int *out_pos, const char *input,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001647 const char *(*func)(const char *variable,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001648 void *info),
1649 void *info)
1650/* [SUMMARY] Expand a variable.
1651 <buffer> The buffer to write to.
1652 <length> The length of the output buffer.
1653 <out_pos> The current output position. This is updated.
1654 <input> A pointer to the input character pointer.
1655 <func> A function which will be used to get variable values. If this
1656 returns NULL, the environment is searched instead. If this is NULL, only
1657 the environment is searched.
1658 <info> An arbitrary pointer passed to <<func>>.
1659 <errfp> Diagnostic messages are written here.
1660 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1661*/
1662{
1663 char ch;
1664 int len;
1665 unsigned int open_braces;
1666 const char *env, *ptr;
1667 char tmp[STRING_LENGTH];
1668
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001669 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001670 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001671 /* Special case for "$$": PID */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001672 sprintf(tmp, "%d",(int) getpid());
1673 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001674 if (len + *out_pos >= length)
1675 goto expand_variable_out;
1676
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001677 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001678 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001679 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001680 }
1681 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001682 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001683 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001684 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001685 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001686 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001687 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001688 goto expand_variable_out;
1689
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001690 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001691 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001692 env = get_variable_v2(tmp, func, info);
1693 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001694 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001695 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001696 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001697 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001698 if (len + *out_pos >= length)
1699 goto expand_variable_out;
1700
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001701 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001702 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001703 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001704 }
1705 /* Variable in braces: check for ':' tricks */
1706 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001707 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001708 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001709 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001710 /* Must be simple variable expansion with "${var}" */
1711 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001712 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001713 goto expand_variable_out;
1714
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001715 safe_memcpy(tmp, input, len);
1716 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001717 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001718 return NULL;
1719 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001720 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001721 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001722 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001723 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001724 }
1725 /* It's that handy "${var:-word}" expression. Check if var is defined */
1726 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001727 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001728 goto expand_variable_out;
1729
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001730 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001731 /* Move input pointer to ':' */
1732 input = ptr;
1733 /* First skip to closing brace, taking note of nested expressions */
1734 ptr += 2;
1735 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001736 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1737 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001738 case '{':
1739 ++open_braces;
1740 break;
1741 case '}':
1742 --open_braces;
1743 break;
1744 case '\0':
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001745 info_logger(LOG_INFO,"\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001746 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001747 default:
1748 break;
1749 }
1750 }
1751 --ptr;
1752 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001753 env = get_variable_v2(tmp, func, info);
1754 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001755 /* Found environment variable, so skip the input to the closing brace
1756 and return the variable */
1757 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001758 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001759 if (len + *out_pos >= length)
1760 goto expand_variable_out;
1761
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001762 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001763 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001764 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001765 }
1766 /* Environment variable was not found, so process word. Advance input
1767 pointer to start of word in "${var:-word}" */
1768 input += 2;
1769 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001770 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001771 goto expand_variable_out;
1772
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001773 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001774 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001775 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001776 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001777 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001778 if (len + *out_pos >= length)
1779 goto expand_variable_out;
1780
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001781 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001782 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001783 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001784expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001785 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001786 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001787} /* End Function expand_variable */
1788
1789
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001790static const char *get_variable_v2(const char *variable,
1791 const char *(*func)(const char *variable, void *info),
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001792 void *info)
1793/* [SUMMARY] Get a variable from the environment or .
1794 <variable> The variable name.
1795 <func> A function which will be used to get the variable. If this returns
1796 NULL, the environment is searched instead. If this is NULL, only the
1797 environment is searched.
1798 [RETURNS] The value of the variable on success, else NULL.
1799*/
1800{
1801 const char *value;
1802
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001803 if (func != NULL) {
1804 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001805 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001806 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001807 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001808 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001809} /* End Function get_variable */
1810
1811/* END OF CODE */