blob: 24c953bacd7d85dd0c7dc8486f0057029c81215a [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 */
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*/
Pere Orga5bc8c002011-04-11 03:29:49 +020056
57//usage:#define devfsd_trivial_usage
58//usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
59//usage:#define devfsd_full_usage "\n\n"
60//usage: "Manage devfs permissions and old device name symlinks\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020061//usage: "\n mntpnt The mount point where devfs is mounted"
62//usage: "\n -v Print the protocol version numbers for devfsd"
63//usage: "\n and the kernel-side protocol version and exit"
64//usage: IF_DEVFSD_FG_NP(
65//usage: "\n -fg Run in foreground"
66//usage: "\n -np Exit after parsing the configuration file"
67//usage: "\n and processing synthetic REGISTER events,"
68//usage: "\n don't poll for events"
69//usage: )
70
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000071#include "libbb.h"
72#include "xregex.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000073#include <syslog.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000074
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000075#include <sys/un.h>
76#include <sys/sysmacros.h>
Eric Andersen2af70002003-10-09 21:02:23 +000077
78/* Various defines taken from linux/major.h */
79#define IDE0_MAJOR 3
80#define IDE1_MAJOR 22
81#define IDE2_MAJOR 33
82#define IDE3_MAJOR 34
83#define IDE4_MAJOR 56
84#define IDE5_MAJOR 57
85#define IDE6_MAJOR 88
86#define IDE7_MAJOR 89
87#define IDE8_MAJOR 90
88#define IDE9_MAJOR 91
89
90
91/* Various defines taken from linux/devfs_fs.h */
92#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020093#define DEVFSD_IOCTL_BASE 'd'
Eric Andersen2af70002003-10-09 21:02:23 +000094/* These are the various ioctls */
95#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
96#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
97#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000098#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000099#define DEVFSD_NOTIFY_REGISTERED 0
100#define DEVFSD_NOTIFY_UNREGISTERED 1
101#define DEVFSD_NOTIFY_ASYNC_OPEN 2
102#define DEVFSD_NOTIFY_CLOSE 3
103#define DEVFSD_NOTIFY_LOOKUP 4
104#define DEVFSD_NOTIFY_CHANGE 5
105#define DEVFSD_NOTIFY_CREATE 6
106#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000107#define DEVFS_PATHLEN 1024
108/* Never change this otherwise the binary interface will change */
109
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200110struct devfsd_notify_struct {
111 /* Use native C types to ensure same types in kernel and user space */
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000112 unsigned int type; /* DEVFSD_NOTIFY_* value */
113 unsigned int mode; /* Mode of the inode or device entry */
114 unsigned int major; /* Major number of device entry */
115 unsigned int minor; /* Minor number of device entry */
116 unsigned int uid; /* Uid of process, inode or device entry */
117 unsigned int gid; /* Gid of process, inode or device entry */
118 unsigned int overrun_count; /* Number of lost events */
119 unsigned int namelen; /* Number of characters not including '\0' */
120 /* The device name MUST come last */
121 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
Eric Andersen2af70002003-10-09 21:02:23 +0000122};
123
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000124#define BUFFER_SIZE 16384
125#define DEVFSD_VERSION "1.3.25"
126#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000127#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000128#define MODPROBE_SWITCH_1 "-k"
129#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000130#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000131#define MAX_ARGS (6 + 1)
132#define MAX_SUBEXPR 10
133#define STRING_LENGTH 255
134
135/* for get_uid_gid() */
136#define UID 0
137#define GID 1
138
Rob Landley1ba19d62005-10-08 17:42:35 +0000139/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000140# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000141# define NO_DIE 0
142
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000143/* for dir_operation() */
144#define RESTORE 0
145#define SERVICE 1
146#define READ_CONFIG 2
147
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000148/* Update only after changing code to reflect new protocol */
149#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
150
151/* Compile-time check */
152#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
153#error protocol version mismatch. Update your kernel headers
154#endif
155
156#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000157#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000158#define AC_EXECUTE 2
159#define AC_MFUNCTION 3 /* not supported by busybox */
160#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000161#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000162#define AC_IGNORE 6
163#define AC_MKOLDCOMPAT 7
164#define AC_MKNEWCOMPAT 8
165#define AC_RMOLDCOMPAT 9
166#define AC_RMNEWCOMPAT 10
167#define AC_RESTORE 11
168
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200169struct permissions_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000170 mode_t mode;
171 uid_t uid;
172 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000173};
174
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200175struct execute_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000176 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000177};
178
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200179struct copy_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000180 const char *source;
181 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000182};
183
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200184struct action_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000185 unsigned int what;
186 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000187};
188
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200189struct config_entry_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000190 struct action_type action;
191 regex_t preg;
192 union
193 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000194 struct permissions_type permissions;
195 struct execute_type execute;
196 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000197 }
198 u;
199 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000200};
201
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200202struct get_variable_info {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000203 const struct devfsd_notify_struct *info;
204 const char *devname;
205 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000206};
207
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000208static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000209static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000210static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000211static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000212static int mksymlink(const char *oldpath, const char *newpath);
213static void read_config_file(char *path, int optional, unsigned long *event_mask);
214static void process_config_line(const char *, unsigned long *);
215static int do_servicing(int, unsigned long);
216static void service_name(const struct devfsd_notify_struct *);
217static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
218static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000219 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000220static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
221static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100222 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000223static void action_compat(const struct devfsd_notify_struct *, unsigned);
224static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000225static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000226static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
227static mode_t get_mode(const char *);
228static void signal_handler(int);
229static const char *get_variable(const char *, void *);
230static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000231static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100232 const char *, const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000233static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000234static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000235 const char *(*)(const char *, void *), void *);
236static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100237static char get_old_ide_name(unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000238static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000239
240/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000241static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000242static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000243static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
244static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000245
246/* Structs and vars */
247static struct config_entry_struct *first_config = NULL;
248static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000249static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000250static volatile int caught_signal = FALSE;
251static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000252static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000253 const char *dest;
254 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000255} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000256 {"/proc/self/fd", "fd"},
257 {"fd/0", "stdin"},
258 {"fd/1", "stdout"},
259 {"fd/2", "stderr"},
260 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000261};
262
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000263static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000264 unsigned int type; /* The DEVFSD_NOTIFY_* value */
265 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000266} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000267 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
268 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
269 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
270 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
271 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
272 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
273 {DEVFSD_NOTIFY_CREATE, "CREATE"},
274 {DEVFSD_NOTIFY_DELETE, "DELETE"},
275 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000276};
277
Rob Landley1ba19d62005-10-08 17:42:35 +0000278/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000279
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000280static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
281static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
282static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
283static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000284
Rob Landley1ba19d62005-10-08 17:42:35 +0000285/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000286#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
287#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
288#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
289#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
290#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
291#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000292#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000293#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000294#define msg_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000295#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000296#define error_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000297#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Eric Andersenf18bd892003-12-19 11:07:59 +0000298#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000299
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000300static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000301{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000302 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000303 dest[len] = '\0';
304}
305
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000306static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000307{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000308 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000309 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000310 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000311 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000312 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000313 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000314 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000315 return 5 + addendum;
316 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000317}
318
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000319static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000320{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000321 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
322 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
323 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
324 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000325 return 1;
326 return scan_dev_name_common(d, n, 0, ptr);
327 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000328 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
329 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
330 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000331 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000332 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000333 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000334 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000335 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000336 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000337 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000338 return 0;
339}
340
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000341/* Public functions follow */
342
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000343int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000344int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000345{
346 int print_version = FALSE;
347 int do_daemon = TRUE;
348 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000349 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000350 int fd, proto_rev, count;
351 unsigned long event_mask = 0;
352 struct sigaction new_action;
353 struct initial_symlink_struct *curr;
354
355 if (argc < 2)
356 bb_show_usage();
357
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000358 for (count = 2; count < argc; ++count) {
359 if (argv[count][0] == '-') {
360 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000361 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000362 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000363 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
364 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000365 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000366 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
367 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000368 else
369 bb_show_usage();
370 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000371 }
372
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000373 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000374
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000375 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000376
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000377 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000378 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000379 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000380
381 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000382 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000383 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000384
385 /* NB: The check for CONFIG_FILE is done in read_config_file() */
386
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000387 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000388 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000389 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000390 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000391 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000392 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000393 exit(EXIT_SUCCESS); /* -v */
394 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000395 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000396 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000397
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000398 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000399 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000400 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000401 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000402 sigaction_set(SIGHUP, &new_action);
403 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000404
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000405 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000406
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000407 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000408 umask(0);
409 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000410 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000411 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000412
Rob Landley1ba19d62005-10-08 17:42:35 +0000413 if (ENABLE_DEVFSD_FG_NP && no_polling)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000414 exit(EXIT_SUCCESS);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000415
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000416 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
417 logmode = LOGMODE_BOTH;
418 else if (do_daemon == TRUE)
419 logmode = LOGMODE_SYSLOG;
420 /* This is the default */
421 /*else
422 logmode = LOGMODE_STDIO; */
423
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000424 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000425 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000426 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000427 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000428 } else if (ENABLE_DEVFSD_FG_NP) {
429 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000430 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000431
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000432 while (TRUE) {
433 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000434
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000435 free_config();
436 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000437 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000438 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000439 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000440 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000441} /* End Function main */
442
443
444/* Private functions follow */
445
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000446static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000447/* [SUMMARY] Read a configuration database.
448 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000449 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000450 <optional> If TRUE, the routine will silently ignore a missing config file.
451 <event_mask> The event mask is written here. This is not initialised.
452 [RETURNS] Nothing.
453*/
454{
455 struct stat statbuf;
456 FILE *fp;
457 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000458 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000459 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000460
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000461 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000462 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000463 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000464 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000465 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000466 p = bb_simplify_path(path);
467 dir_operation(READ_CONFIG, p, 0, event_mask);
468 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000469 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000470 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000471 fp = fopen_for_read(path);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000472 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000473 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000474 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000475 line = buf;
476 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000477 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000478 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000479 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000480 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000481 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000482 } else {
483 goto read_config_file_err;
484 }
485 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000486read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000487 if (optional == 0 && errno == ENOENT)
488 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000489 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000490} /* End Function read_config_file */
491
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000492static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000493/* [SUMMARY] Process a line from a configuration file.
494 <line> The configuration line.
495 <event_mask> The event mask is written here. This is not initialised.
496 [RETURNS] Nothing.
497*/
498{
499 int num_args, count;
500 struct config_entry_struct *new;
501 char p[MAX_ARGS][STRING_LENGTH];
502 char when[STRING_LENGTH], what[STRING_LENGTH];
503 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000504 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000505 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000506 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000507
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000508 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000509 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000510 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
511 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
512 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
513 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000514
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000515 for (count = 0; count < MAX_ARGS; ++count)
516 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000517 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000518 when, name, what,
519 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000520
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000521 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000522
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000523 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000524 if (i == 0) {
525 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000526 *event_mask = 0;
527 return;
528 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000529
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000530 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000531 goto process_config_line_err;
532
Eric Andersenf18bd892003-12-19 11:07:59 +0000533 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000534 if (i == 1 || i == 2) {
535 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000536 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000537 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000538 return;
539 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000540 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000541 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000542 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000543 return;
544 }
545 if (num_args < 3)
546 goto process_config_line_err;
547
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000548 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000549
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000550 for (count = 0; event_types[count].config_name != NULL; ++count) {
551 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000552 continue;
553 new->action.when = event_types[count].type;
554 break;
555 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000556 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000557 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000558 goto process_config_line_err;
559 }
560
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000561 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000562
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000563 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000564 case 4: /* "PERMISSIONS" */
565 new->action.what = AC_PERMISSIONS;
566 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000567 ptr = strchr(p[0], '.');
568 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000569 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000570 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000571 }
572
573 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000574 new->u.permissions.uid = get_uid_gid(UID, p[0]);
575 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000576 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000577 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000578 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000579 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000580 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000581 the device name) to the module loading facility. In addition,
582 the /etc/modules.devfs configuration file is used.*/
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100583 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000584 new->action.what = AC_MODLOAD;
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100585 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000586 case 6: /* EXECUTE */
587 new->action.what = AC_EXECUTE;
588 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000589
Eric Andersenf18bd892003-12-19 11:07:59 +0000590 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000591 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000592
Eric Andersenf18bd892003-12-19 11:07:59 +0000593 new->u.execute.argv[num_args] = NULL;
594 break;
595 case 7: /* COPY */
596 new->action.what = AC_COPY;
597 num_args -= 3;
598 if (num_args != 2)
599 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000600
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000601 new->u.copy.source = xstrdup(p[0]);
602 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000603 break;
604 case 8: /* IGNORE */
605 /* FALLTROUGH */
606 case 9: /* MKOLDCOMPAT */
607 /* FALLTROUGH */
608 case 10: /* MKNEWCOMPAT */
609 /* FALLTROUGH */
610 case 11:/* RMOLDCOMPAT */
611 /* FALLTROUGH */
612 case 12: /* RMNEWCOMPAT */
613 /* AC_IGNORE 6
614 AC_MKOLDCOMPAT 7
615 AC_MKNEWCOMPAT 8
616 AC_RMOLDCOMPAT 9
617 AC_RMNEWCOMPAT 10*/
618 new->action.what = i - 2;
619 break;
620 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000621 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000622 goto process_config_line_err;
623 /*esac*/
624 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000625
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000626 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000627
628 *event_mask |= 1 << new->action.when;
629 new->next = NULL;
630 if (first_config == NULL)
631 first_config = new;
632 else
633 last_config->next = new;
634 last_config = new;
635 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000636
637 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000638 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000639} /* End Function process_config_line */
640
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000641static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000642/* [SUMMARY] Service devfs changes until a signal is received.
643 <fd> The open control file.
644 <event_mask> The event mask.
645 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
646*/
647{
648 ssize_t bytes;
649 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000650
Denis Vlasenko856be772007-08-17 08:29:48 +0000651 /* (void*) cast is only in order to match prototype */
652 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000653 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000654 errno = 0;
Denys Vlasenko083e1722010-01-28 12:30:24 +0100655 bytes = read(fd, (char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000656 if (caught_signal)
657 break; /* Must test for this first */
658 if (errno == EINTR)
659 continue; /* Yes, the order is important */
660 if (bytes < 1)
661 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000662 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000663 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000664 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000665 int c_sighup = caught_sighup;
666
667 caught_signal = FALSE;
668 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000669 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000670 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000671 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000672} /* End Function do_servicing */
673
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000674static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000675/* [SUMMARY] Service a single devfs change.
676 <info> The devfs change.
677 [RETURNS] Nothing.
678*/
679{
680 unsigned int n;
681 regmatch_t mbuf[MAX_SUBEXPR];
682 struct config_entry_struct *entry;
683
Rob Landley1ba19d62005-10-08 17:42:35 +0000684 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000685 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000686
687 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000688 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000689 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000690 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000691 || (info->devname[0] == 'i' && info->devname[1] == 'n'
692 && info->devname[2] == 'i' && info->devname[3] == 't'
693 && info->devname[4] == 'c' && info->devname[5] == 't'
694 && info->devname[6] == 'l' && !info->devname[7]))
695 )
696 return;
697
698 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000700 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000701 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000702 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000703 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000704 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000705
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000706 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000707 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000708 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000709 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000710 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000711 if (ENABLE_DEVFSD_MODLOAD)
712 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000713 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000714 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000715 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000716 break;
717 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000718 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000719 break;
720 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000721 return;
722 /*break;*/
723 case AC_MKOLDCOMPAT:
724 case AC_MKNEWCOMPAT:
725 case AC_RMOLDCOMPAT:
726 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000727 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000728 break;
729 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000730 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000731 }
732 }
733} /* End Function service_name */
734
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000735static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000736 const struct config_entry_struct *entry)
737/* [SUMMARY] Update permissions for a device entry.
738 <info> The devfs change.
739 <entry> The config file entry.
740 [RETURNS] Nothing.
741*/
742{
743 struct stat statbuf;
744
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000745 if (stat(info->devname, &statbuf) != 0
746 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
747 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000748 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000749 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000750} /* End Function action_permissions */
751
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000752static void action_modload(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100753 const struct config_entry_struct *entry UNUSED_PARAM)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000754/* [SUMMARY] Load a module.
755 <info> The devfs change.
756 <entry> The config file entry.
757 [RETURNS] Nothing.
758*/
759{
760 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000761
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000762 argv[0] = (char*)MODPROBE;
763 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
764 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
765 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000766 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000767 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000768
Denys Vlasenko8531d762010-03-18 22:44:00 +0100769 spawn_and_wait(argv);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000770 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000771} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000772
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000773static void action_execute(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100774 const struct config_entry_struct *entry,
775 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000776/* [SUMMARY] Execute a programme.
777 <info> The devfs change.
778 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000779 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000780 device name.
781 <numexpr> The number of elements within <<regexpr>>.
782 [RETURNS] Nothing.
783*/
784{
785 unsigned int count;
786 struct get_variable_info gv_info;
787 char *argv[MAX_ARGS + 1];
788 char largv[MAX_ARGS + 1][STRING_LENGTH];
789
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000790 gv_info.info = info;
791 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000792 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
793 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
794 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000795 entry->u.execute.argv[count],
796 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000797 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000798 argv[count] = largv[count];
799 }
800 argv[count] = NULL;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100801 spawn_and_wait(argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000802} /* End Function action_execute */
803
804
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000805static void action_copy(const struct devfsd_notify_struct *info,
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100806 const struct config_entry_struct *entry,
807 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000808/* [SUMMARY] Copy permissions.
809 <info> The devfs change.
810 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000811 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000812 device name.
813 <numexpr> The number of elements in <<regexpr>>.
814 [RETURNS] Nothing.
815*/
816{
817 mode_t new_mode;
818 struct get_variable_info gv_info;
819 struct stat source_stat, dest_stat;
820 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000821 int ret = 0;
822
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000823 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000824
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000825 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000826 return;
827 gv_info.info = info;
828 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000829
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000830 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
831 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000832 get_variable, &gv_info, gv_info.devname,
833 regexpr, numexpr);
834
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000835 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000836 get_variable, &gv_info, gv_info.devname,
837 regexpr, numexpr);
838
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000839 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000840 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000841 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000842 new_mode = source_stat.st_mode & ~S_ISVTX;
843 if (info->type == DEVFSD_NOTIFY_CREATE)
844 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000845 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000846 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000847 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000848 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000849 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000850} /* End Function action_copy */
851
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000852static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000853/* [SUMMARY] Process a compatibility request.
854 <info> The devfs change.
855 <action> The action to take.
856 [RETURNS] Nothing.
857*/
858{
Rob Landley1ba19d62005-10-08 17:42:35 +0000859 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000860 const char *compat_name = NULL;
861 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000862 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000863 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000864 int mode, host, bus, target, lun;
865 unsigned int i;
866 char rewind_;
867 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000868 static const char *const fmt[] = {
869 NULL ,
870 "sg/c%db%dt%du%d", /* scsi/generic */
871 "sd/c%db%dt%du%d", /* scsi/disc */
872 "sr/c%db%dt%du%d", /* scsi/cd */
873 "sd/c%db%dt%du%dp%d", /* scsi/part */
874 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
875 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
876 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
877 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
878 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
879 NULL
880 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000881
882 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000883 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000884 case AC_MKOLDCOMPAT:
885 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000886 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000887 break;
888 case AC_MKNEWCOMPAT:
889 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000890 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000891 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000892
893 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000894 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000895 return;
896
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000897 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
898 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000899 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000900 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000901
Eric Andersenf18bd892003-12-19 11:07:59 +0000902
903 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000904 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
905 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000906
907 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000908 if (i == 4 || i == 8)
909 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000910
911 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000912 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000913 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000914 if (rewind_ != 'n')
915 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000916 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000917 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000918 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000919 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000920 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000921 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000922 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000923
Eric Andersenf18bd892003-12-19 11:07:59 +0000924 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000925 if (i == 9)
926 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000927 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000928 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000929
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000930 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000931 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000932
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000933 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000934 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000935 case AC_MKOLDCOMPAT:
936 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000937 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000938 break;
939 case AC_RMOLDCOMPAT:
940 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000941 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +0000942 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000943 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000945 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000946 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000947} /* End Function action_compat */
948
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000949static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000951 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000952 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000953
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000954 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000955 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000956 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000957 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000958 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denys Vlasenko083e1722010-01-28 12:30:24 +0100959 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000960
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000961 if (S_ISDIR(source_stat.st_mode))
Denys Vlasenko083e1722010-01-28 12:30:24 +0100962 dir_operation(RESTORE, spath, rootlen, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000963}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000964
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000965
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000966static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000967 mode_t new_mode,
968 const char *sourcepath, const struct stat *source_stat)
969/* [SUMMARY] Copy an inode.
970 <destpath> The destination path. An existing inode may be deleted.
971 <dest_stat> The destination stat(2) information.
972 <new_mode> The desired new mode for the destination.
973 <sourcepath> The source path.
974 <source_stat> The source stat(2) information.
975 [RETURNS] TRUE on success, else FALSE.
976*/
977{
Eric Andersenf18bd892003-12-19 11:07:59 +0000978 int source_len, dest_len;
979 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
980 int fd, val;
981 struct sockaddr_un un_addr;
982 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000983
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000984 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000985 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000986 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000987 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
988 if ((source_len < 0)
989 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000990 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000991 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000992 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000993 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000994 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
995 unlink(destpath);
996 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000997 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000998 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000999 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001000 chmod(destpath, new_mode & ~S_IFMT);
1001 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001002 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001003 }
1004 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001005 unlink(destpath);
1006 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001008 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1009 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001010 break;
1011 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001012 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
Denys Vlasenko083e1722010-01-28 12:30:24 +01001013 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001014 close(fd);
1015 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001016 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001017 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001018 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001019 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1020 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001021 break;
1022 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001023 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001024 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001025 break;
1026 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001027 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1028 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001029 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001030 close(fd);
1031 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001032 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001033 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001034 case S_IFBLK:
1035 case S_IFCHR:
1036 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001037 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001038 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001039 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001040 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001041 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001042 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001043do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001044 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001045 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001046 /*break;*/
1047 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001048 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001049} /* End Function copy_inode */
1050
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001051static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001052/* [SUMMARY] Free the configuration information.
1053 [RETURNS] Nothing.
1054*/
1055{
1056 struct config_entry_struct *c_entry;
1057 void *next;
1058
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001059 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001060 unsigned int count;
1061
1062 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001063 regfree(&c_entry->preg);
1064 if (c_entry->action.what == AC_EXECUTE) {
1065 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001066 if (c_entry->u.execute.argv[count] == NULL)
1067 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001068 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001069 }
1070 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001071 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001072 }
1073 first_config = NULL;
1074 last_config = NULL;
1075} /* End Function free_config */
1076
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001077static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001078/* [SUMMARY] Convert a string to a UID or GID value.
1079 <flag> "UID" or "GID".
1080 <string> The string.
1081 [RETURNS] The UID or GID value.
1082*/
1083{
1084 struct passwd *pw_ent;
1085 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001086 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001087
Rob Landley1ba19d62005-10-08 17:42:35 +00001088 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001089 msg = "user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001090
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001091 if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001092 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001093
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001094 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001095 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001096
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001097 if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001098 return grp_ent->gr_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001099 else if (ENABLE_DEVFSD_VERBOSE)
1100 msg = "group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001101
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001102 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001103 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001104 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001105}/* End Function get_uid_gid */
1106
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001107static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001108/* [SUMMARY] Convert a string to a mode value.
1109 <string> The string.
1110 [RETURNS] The mode value.
1111*/
1112{
1113 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001114 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001115
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001116 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001117 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001118 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001119 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001120
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001121 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001122 i = S_IRUSR;
1123 while (i > 0) {
1124 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1125 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001126 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001127 string++;
1128 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001129 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001130} /* End Function get_mode */
1131
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001132static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001133{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001134 caught_signal = TRUE;
1135 if (sig == SIGHUP)
1136 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001137
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001138 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001139} /* End Function signal_handler */
1140
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001141static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001142{
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001143 static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001144 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001145
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001146 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001147 const char *field_names[] = {
1148 "hostname", "mntpt", "devpath", "devname",
1149 "uid", "gid", "mode", hostname, mount_point,
1150 gv_info->devpath, gv_info->devname, NULL
1151 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001152 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001153
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001154 if (!hostname)
1155 hostname = safe_gethostname();
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001156 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001157 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001158
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001159 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001160 return NULL;
1161 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001162 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001163
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001164 if (i == 4)
1165 sprintf(sbuf, "%u", gv_info->info->uid);
1166 else if (i == 5)
1167 sprintf(sbuf, "%u", gv_info->info->gid);
1168 else if (i == 6)
1169 sprintf(sbuf, "%o", gv_info->info->mode);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001170 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001171} /* End Function get_variable */
1172
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001173static void service(struct stat statbuf, char *path)
1174{
1175 struct devfsd_notify_struct info;
1176
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001177 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001178 info.type = DEVFSD_NOTIFY_REGISTERED;
1179 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001180 info.major = major(statbuf.st_rdev);
1181 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001182 info.uid = statbuf.st_uid;
1183 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001184 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1185 info.namelen = strlen(info.devname);
1186 service_name(&info);
1187 if (S_ISDIR(statbuf.st_mode))
1188 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001189}
1190
1191static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001192/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001193 <flag> To choose which function to perform
1194 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001196 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001197 [RETURNS] Nothing.
1198*/
1199{
1200 struct stat statbuf;
1201 DIR *dp;
1202 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001203 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001204
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001205 dp = warn_opendir(dir_name);
1206 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001207 return;
1208
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001209 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001210
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001211 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001212 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001213 path = concat_path_file(dir_name, de->d_name);
1214 if (lstat(path, &statbuf) == 0) {
1215 switch (type) {
1216 case SERVICE:
1217 service(statbuf, path);
1218 break;
1219 case RESTORE:
1220 restore(path, statbuf, var);
1221 break;
1222 case READ_CONFIG:
1223 read_config_file(path, var, event_mask);
1224 break;
1225 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001226 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001227 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001228 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001229 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001230} /* End Function do_scan_and_service */
1231
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001232static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001233/* [SUMMARY] Create a symlink, creating intervening directories as required.
1234 <oldpath> The string contained in the symlink.
1235 <newpath> The name of the new symlink.
1236 [RETURNS] 0 on success, else -1.
1237*/
1238{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001239 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001240 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001241
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001242 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001243 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001244 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001245 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001246 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001247} /* End Function mksymlink */
1248
1249
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001250static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001251/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001252 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001253 [RETURNS] TRUE on success, else FALSE.
1254*/
1255{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001256 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001257 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001258 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001259} /* End Function make_dir_tree */
1260
1261static int expand_expression(char *output, unsigned int outsize,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001262 const char *input,
1263 const char *(*get_variable_func)(const char *variable, void *info),
1264 void *info,
1265 const char *devname,
1266 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001267/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001268 <output> The output expanded expression is written here.
1269 <length> The size of the output buffer.
1270 <input> The input expression. This may equal <<output>>.
1271 <get_variable> A function which will be used to get variable values. If
1272 this returns NULL, the environment is searched instead. If this is NULL,
1273 only the environment is searched.
1274 <info> An arbitrary pointer passed to <<get_variable>>.
1275 <devname> Device name; specifically, this is the string that contains all
1276 of the regular subexpressions.
1277 <ex> Array of start / end offsets into info->devname for each subexpression
1278 <numexp> Number of regular subexpressions found in <<devname>>.
1279 [RETURNS] TRUE on success, else FALSE.
1280*/
1281{
1282 char temp[STRING_LENGTH];
1283
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001284 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001285 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001286 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001287 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001288} /* End Function expand_expression */
1289
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001290static void expand_regexp(char *output, size_t outsize, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001291 const char *devname,
1292 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001293/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1294 <output> The output expanded expression is written here.
1295 <outsize> The size of the output buffer.
1296 <input> The input expression. This may NOT equal <<output>>, because
1297 supporting that would require yet another string-copy. However, it's not
1298 hard to write a simple wrapper function to add this functionality for those
1299 few cases that need it.
1300 <devname> Device name; specifically, this is the string that contains all
1301 of the regular subexpressions.
1302 <ex> An array of start and end offsets into <<devname>>, one for each
1303 subexpression
1304 <numex> Number of subexpressions in the offset-array <<ex>>.
1305 [RETURNS] Nothing.
1306*/
1307{
1308 const char last_exp = '0' - 1 + numex;
1309 int c = -1;
1310
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001311 /* Guarantee NULL termination by writing an explicit '\0' character into
1312 the very last byte */
1313 if (outsize)
1314 output[--outsize] = '\0';
1315 /* Copy the input string into the output buffer, replacing '\\' with '\'
1316 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1317 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001318 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001319 c = *input;
1320 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001321 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001322 c = *input;
1323 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001324 if (c != '\\') {
1325 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001326 const regmatch_t *subexp = ex + (c - '0');
1327 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1328
1329 /* Range checking */
1330 if (sublen > outsize)
1331 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001332 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001333 output += sublen;
1334 outsize -= sublen;
1335 }
1336 continue;
1337 }
1338 }
1339 *output = c;
1340 ++output;
1341 --outsize;
1342 } /* while */
1343} /* End Function expand_regexp */
1344
1345
1346/* from compat_name.c */
1347
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +02001348struct translate_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001349 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001350 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001351 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001352};
1353
1354static struct translate_struct translate_table[] =
1355{
1356 {"sound/", NULL},
1357 {"printers/", "lp%s"},
1358 {"v4l/", NULL},
1359 {"parports/", "parport%s"},
1360 {"fb/", "fb%s"},
1361 {"netlink/", NULL},
1362 {"loop/", "loop%s"},
1363 {"floppy/", "fd%s"},
1364 {"rd/", "ram%s"},
1365 {"md/", "md%s"}, /* Meta-devices */
1366 {"vc/", "tty%s"},
1367 {"misc/", NULL},
1368 {"isdn/", NULL},
1369 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1370 {"i2c/", "i2c-%s"},
1371 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1372 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1373 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1374 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1375 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1376 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1377 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1378 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1379 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1380 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1381 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1382 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1383 {"input/js", "js%s"}, /* Joystick driver */
1384 {NULL, NULL}
1385};
1386
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001387const char *get_old_name(const char *devname, unsigned int namelen,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001388 char *buffer, unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001389/* [SUMMARY] Translate a kernel-supplied name into an old name.
1390 <devname> The device name provided by the kernel.
1391 <namelen> The length of the name.
1392 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1393 <major> The major number for the device.
1394 <minor> The minor number for the device.
1395 [RETURNS] A pointer to the old name if known, else NULL.
1396*/
1397{
1398 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001399 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001400 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001401 unsigned int i;
1402 char mode;
1403 int indexx;
1404 const char *pty1;
1405 const char *pty2;
1406 size_t len;
1407 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001408 static const char *const fmt[] = {
1409 NULL ,
1410 "sg%u", /* scsi/generic */
1411 NULL, /* scsi/disc */
1412 "sr%u", /* scsi/cd */
1413 NULL, /* scsi/part */
1414 "nst%u%c", /* scsi/mt */
1415 "hd%c" , /* ide/host/disc */
1416 "hd%c" , /* ide/host/cd */
1417 "hd%c%s", /* ide/host/part */
1418 "%sht%d", /* ide/host/mt */
1419 "sbpcd%u", /* sbp/ */
1420 "vcs%s", /* vcc/ */
1421 "%cty%c%c", /* pty/ */
1422 NULL
1423 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001424
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001425 for (trans = translate_table; trans->match != NULL; ++trans) {
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001426 len = strlen(trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001427
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001428 if (strncmp(devname, trans->match, len) == 0) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001429 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001430 return devname + len;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001431 sprintf(buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001432 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001433 }
1434 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001435
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001436 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001437 i = scan_dev_name(devname, namelen, ptr);
1438
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001439 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001440 compat_name = buffer;
1441 else
1442 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001443
Eric Andersenf18bd892003-12-19 11:07:59 +00001444 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001445 if (i == 1 || i == 3 || i == 10)
1446 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001447
1448 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001449 if (i == 2 || i == 4)
Denys Vlasenko083e1722010-01-28 12:30:24 +01001450 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001451
1452 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001453 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001454 mode = ptr[2];
1455 if (mode == 'n')
1456 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001457 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001458 if (devname[namelen - 1] != 'n')
1459 ++compat_name;
1460 }
1461 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001462 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001463 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001464 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001465
1466 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001467 if (i == 9)
1468 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001469
1470 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001471 if (i == 11) {
1472 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001473 if (buffer[3] == '0')
1474 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001475 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001476 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001477 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001478 pty1 = "pqrstuvwxyzabcde";
1479 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001480 indexx = atoi(devname + 5);
1481 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001482 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001483 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001484} /* End Function get_old_name */
1485
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001486static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001487/* [SUMMARY] Get the old IDE name for a device.
1488 <major> The major number for the device.
1489 <minor> The minor number for the device.
1490 [RETURNS] The drive letter.
1491*/
1492{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001493 char letter = 'y'; /* 121 */
1494 char c = 'a'; /* 97 */
1495 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001496
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001497 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1498 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001499 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1500 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1501 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1502 || i == IDE9_MAJOR
1503 ) {
1504 if ((unsigned int)i == major) {
1505 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001506 break;
1507 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001508 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001509 }
1510 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001511 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001512
1513 if (minor > 63)
1514 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001515 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001516} /* End Function get_old_ide_name */
1517
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001518static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001519 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001520 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001521/* [SUMMARY] Write the old SCSI disc name to a buffer.
1522 <buffer> The buffer to write to.
1523 <major> The major number for the device.
1524 <minor> The minor number for the device.
1525 <part> The partition string. Must be "" for a whole-disc entry.
1526 [RETURNS] A pointer to the buffer on success, else NULL.
1527*/
1528{
1529 unsigned int disc_index;
1530
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001531 if (major == 8) {
1532 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001533 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001534 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001535 if ((major > 64) && (major < 72)) {
1536 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001537 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001538 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001539 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001540 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001541 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001542 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001543 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001544} /* End Function write_old_sd_name */
1545
1546
1547/* expression.c */
1548
1549/*EXPERIMENTAL_FUNCTION*/
1550
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001551int st_expr_expand(char *output, unsigned int length, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001552 const char *(*get_variable_func)(const char *variable,
1553 void *info),
1554 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001555/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1556 <output> The output expanded expression is written here.
1557 <length> The size of the output buffer.
1558 <input> The input expression. This may equal <<output>>.
1559 <get_variable> A function which will be used to get variable values. If
1560 this returns NULL, the environment is searched instead. If this is NULL,
1561 only the environment is searched.
1562 <info> An arbitrary pointer passed to <<get_variable>>.
1563 [RETURNS] TRUE on success, else FALSE.
1564*/
1565{
1566 char ch;
1567 unsigned int len;
1568 unsigned int out_pos = 0;
1569 const char *env;
1570 const char *ptr;
1571 struct passwd *pwent;
1572 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1573
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001574 if (length > BUFFER_SIZE)
1575 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001576 for (; TRUE; ++input) {
1577 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001578 case '$':
1579 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001580 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001581 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001582 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001583 break;
1584 case '~':
1585 /* Home directory expansion */
1586 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001587 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001588 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001589 env = getenv("HOME");
1590 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001591 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001592 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001593 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001594 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001595 if (len + out_pos >= length)
1596 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001597 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001598 out_pos += len;
1599 continue;
1600 }
1601 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001602 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001603 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001604 len = ptr - input;
1605 if (len >= sizeof tmp)
1606 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001607 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001608 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001609 pwent = getpwnam(tmp);
1610 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001611 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001612 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001613 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001614 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001615 if (len + out_pos >= length)
1616 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001617 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001618 out_pos += len;
1619 break;
1620 case '\0':
1621 /* Falltrough */
1622 default:
1623 if (out_pos >= length)
1624 goto st_expr_expand_out;
1625 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001626 if (ch == '\0') {
1627 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001628 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001629 }
1630 break;
1631 /* esac */
1632 }
1633 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001634 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001635st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001636 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001637 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001638} /* End Function st_expr_expand */
1639
1640
1641/* Private functions follow */
1642
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001643static const char *expand_variable(char *buffer, unsigned int length,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001644 unsigned int *out_pos, const char *input,
1645 const char *(*func)(const char *variable,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001646 void *info),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001647 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001648/* [SUMMARY] Expand a variable.
1649 <buffer> The buffer to write to.
1650 <length> The length of the output buffer.
1651 <out_pos> The current output position. This is updated.
1652 <input> A pointer to the input character pointer.
1653 <func> A function which will be used to get variable values. If this
1654 returns NULL, the environment is searched instead. If this is NULL, only
1655 the environment is searched.
1656 <info> An arbitrary pointer passed to <<func>>.
1657 <errfp> Diagnostic messages are written here.
1658 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1659*/
1660{
1661 char ch;
1662 int len;
1663 unsigned int open_braces;
1664 const char *env, *ptr;
1665 char tmp[STRING_LENGTH];
1666
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001667 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001668 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001669 /* Special case for "$$": PID */
Denys Vlasenko083e1722010-01-28 12:30:24 +01001670 sprintf(tmp, "%d", (int) getpid());
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001671 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001672 if (len + *out_pos >= length)
1673 goto expand_variable_out;
1674
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001675 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001676 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001677 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001678 }
1679 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001680 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001682 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001683 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001684 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001685 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001686 goto expand_variable_out;
1687
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001688 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001689 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001690 env = get_variable_v2(tmp, func, info);
1691 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001692 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001693 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001694 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001695 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001696 if (len + *out_pos >= length)
1697 goto expand_variable_out;
1698
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001699 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001700 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001701 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001702 }
1703 /* Variable in braces: check for ':' tricks */
1704 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001705 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001706 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001707 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001708 /* Must be simple variable expansion with "${var}" */
1709 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001710 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001711 goto expand_variable_out;
1712
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001713 safe_memcpy(tmp, input, len);
1714 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001715 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001716 return NULL;
1717 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001718 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001719 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001720 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001721 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001722 }
1723 /* It's that handy "${var:-word}" expression. Check if var is defined */
1724 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001725 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001726 goto expand_variable_out;
1727
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001728 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001729 /* Move input pointer to ':' */
1730 input = ptr;
1731 /* First skip to closing brace, taking note of nested expressions */
1732 ptr += 2;
1733 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001734 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1735 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001736 case '{':
1737 ++open_braces;
1738 break;
1739 case '}':
1740 --open_braces;
1741 break;
1742 case '\0':
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001743 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001744 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001745 default:
1746 break;
1747 }
1748 }
1749 --ptr;
1750 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001751 env = get_variable_v2(tmp, func, info);
1752 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001753 /* Found environment variable, so skip the input to the closing brace
1754 and return the variable */
1755 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001756 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001757 if (len + *out_pos >= length)
1758 goto expand_variable_out;
1759
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001760 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001761 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001762 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001763 }
1764 /* Environment variable was not found, so process word. Advance input
1765 pointer to start of word in "${var:-word}" */
1766 input += 2;
1767 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001768 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001769 goto expand_variable_out;
1770
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001771 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001772 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001773 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001774 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001775 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001776 if (len + *out_pos >= length)
1777 goto expand_variable_out;
1778
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001779 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001780 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001781 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001782expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001783 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001784 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001785} /* End Function expand_variable */
1786
1787
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001788static const char *get_variable_v2(const char *variable,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001789 const char *(*func)(const char *variable, void *info),
1790 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001791/* [SUMMARY] Get a variable from the environment or .
1792 <variable> The variable name.
1793 <func> A function which will be used to get the variable. If this returns
1794 NULL, the environment is searched instead. If this is NULL, only the
1795 environment is searched.
1796 [RETURNS] The value of the variable on success, else NULL.
1797*/
1798{
1799 const char *value;
1800
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001801 if (func != NULL) {
1802 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001803 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001804 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001805 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001806 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001807} /* End Function get_variable */
1808
1809/* END OF CODE */