blob: 99bdc72b8714093a48b7610c2d96aead04f038dc [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*/
Denys Vlasenkofb4da162016-11-22 23:14:24 +010056//config:config DEVFSD
57//config: bool "devfsd (obsolete)"
58//config: default n
59//config: select PLATFORM_LINUX
60//config: select FEATURE_SYSLOG
61//config: help
62//config: This is deprecated and should NOT be used anymore.
63//config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
64//config: See docs/mdev.txt for detailed instructions on how to use mdev
65//config: instead.
66//config:
67//config: Provides compatibility with old device names on a devfs systems.
68//config: You should set it to true if you have devfs enabled.
69//config: The following keywords in devsfd.conf are supported:
70//config: "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
71//config: "PERMISSIONS", "EXECUTE", "COPY", "IGNORE",
72//config: "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT".
73//config:
74//config: But only if they are written UPPERCASE!!!!!!!!
75//config:
76//config:config DEVFSD_MODLOAD
77//config: bool "Adds support for MODLOAD keyword in devsfd.conf"
78//config: default y
79//config: depends on DEVFSD
80//config: help
81//config: This actually doesn't work with busybox modutils but needs
82//config: the external modutils.
83//config:
84//config:config DEVFSD_FG_NP
85//config: bool "Enables the -fg and -np options"
86//config: default y
87//config: depends on DEVFSD
88//config: help
89//config: -fg Run the daemon in the foreground.
90//config: -np Exit after parsing the configuration file.
91//config: Do not poll for events.
92//config:
93//config:config DEVFSD_VERBOSE
94//config: bool "Increases logging (and size)"
95//config: default y
96//config: depends on DEVFSD
97//config: help
98//config: Increases logging to stderr or syslog.
99//config:
100//config:config FEATURE_DEVFS
101//config: bool "Use devfs names for all devices (obsolete)"
102//config: default n
103//config: select PLATFORM_LINUX
104//config: help
105//config: This is obsolete and should NOT be used anymore.
106//config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
107//config:
108//config: For legacy systems -- if there is no way around devfsd -- this
109//config: tells busybox to look for names like /dev/loop/0 instead of
110//config: /dev/loop0. If your /dev directory has normal names instead of
111//config: devfs names, you don't want this.
Pere Orga5bc8c002011-04-11 03:29:49 +0200112
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +0100113//applet:IF_DEVFSD(APPLET(devfsd, BB_DIR_SBIN, BB_SUID_DROP))
114
115//kbuild:lib-$(CONFIG_DEVFSD) += devfsd.o
116
Pere Orga5bc8c002011-04-11 03:29:49 +0200117//usage:#define devfsd_trivial_usage
118//usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
119//usage:#define devfsd_full_usage "\n\n"
120//usage: "Manage devfs permissions and old device name symlinks\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200121//usage: "\n mntpnt The mount point where devfs is mounted"
122//usage: "\n -v Print the protocol version numbers for devfsd"
123//usage: "\n and the kernel-side protocol version and exit"
124//usage: IF_DEVFSD_FG_NP(
125//usage: "\n -fg Run in foreground"
126//usage: "\n -np Exit after parsing the configuration file"
127//usage: "\n and processing synthetic REGISTER events,"
128//usage: "\n don't poll for events"
129//usage: )
130
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +0000131#include "libbb.h"
132#include "xregex.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000133#include <syslog.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000134
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000135#include <sys/un.h>
136#include <sys/sysmacros.h>
Eric Andersen2af70002003-10-09 21:02:23 +0000137
138/* Various defines taken from linux/major.h */
139#define IDE0_MAJOR 3
140#define IDE1_MAJOR 22
141#define IDE2_MAJOR 33
142#define IDE3_MAJOR 34
143#define IDE4_MAJOR 56
144#define IDE5_MAJOR 57
145#define IDE6_MAJOR 88
146#define IDE7_MAJOR 89
147#define IDE8_MAJOR 90
148#define IDE9_MAJOR 91
149
150
151/* Various defines taken from linux/devfs_fs.h */
152#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200153#define DEVFSD_IOCTL_BASE 'd'
Eric Andersen2af70002003-10-09 21:02:23 +0000154/* These are the various ioctls */
155#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
156#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
157#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +0000158#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +0000159#define DEVFSD_NOTIFY_REGISTERED 0
160#define DEVFSD_NOTIFY_UNREGISTERED 1
161#define DEVFSD_NOTIFY_ASYNC_OPEN 2
162#define DEVFSD_NOTIFY_CLOSE 3
163#define DEVFSD_NOTIFY_LOOKUP 4
164#define DEVFSD_NOTIFY_CHANGE 5
165#define DEVFSD_NOTIFY_CREATE 6
166#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000167#define DEVFS_PATHLEN 1024
168/* Never change this otherwise the binary interface will change */
169
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200170struct devfsd_notify_struct {
171 /* Use native C types to ensure same types in kernel and user space */
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000172 unsigned int type; /* DEVFSD_NOTIFY_* value */
173 unsigned int mode; /* Mode of the inode or device entry */
174 unsigned int major; /* Major number of device entry */
175 unsigned int minor; /* Minor number of device entry */
176 unsigned int uid; /* Uid of process, inode or device entry */
177 unsigned int gid; /* Gid of process, inode or device entry */
178 unsigned int overrun_count; /* Number of lost events */
179 unsigned int namelen; /* Number of characters not including '\0' */
180 /* The device name MUST come last */
181 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
Eric Andersen2af70002003-10-09 21:02:23 +0000182};
183
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000184#define BUFFER_SIZE 16384
185#define DEVFSD_VERSION "1.3.25"
186#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000187#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000188#define MODPROBE_SWITCH_1 "-k"
189#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000190#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000191#define MAX_ARGS (6 + 1)
192#define MAX_SUBEXPR 10
193#define STRING_LENGTH 255
194
195/* for get_uid_gid() */
196#define UID 0
197#define GID 1
198
Rob Landley1ba19d62005-10-08 17:42:35 +0000199/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000200# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000201# define NO_DIE 0
202
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000203/* for dir_operation() */
204#define RESTORE 0
205#define SERVICE 1
206#define READ_CONFIG 2
207
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000208/* Update only after changing code to reflect new protocol */
209#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
210
211/* Compile-time check */
212#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
213#error protocol version mismatch. Update your kernel headers
214#endif
215
216#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000217#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000218#define AC_EXECUTE 2
219#define AC_MFUNCTION 3 /* not supported by busybox */
220#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000221#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000222#define AC_IGNORE 6
223#define AC_MKOLDCOMPAT 7
224#define AC_MKNEWCOMPAT 8
225#define AC_RMOLDCOMPAT 9
226#define AC_RMNEWCOMPAT 10
227#define AC_RESTORE 11
228
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200229struct permissions_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000230 mode_t mode;
231 uid_t uid;
232 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000233};
234
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200235struct execute_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000236 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000237};
238
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200239struct copy_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000240 const char *source;
241 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000242};
243
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200244struct action_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000245 unsigned int what;
246 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000247};
248
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200249struct config_entry_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000250 struct action_type action;
251 regex_t preg;
252 union
253 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000254 struct permissions_type permissions;
255 struct execute_type execute;
256 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000257 }
258 u;
259 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000260};
261
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200262struct get_variable_info {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000263 const struct devfsd_notify_struct *info;
264 const char *devname;
265 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000266};
267
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000268static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000269static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000270static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000271static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000272static int mksymlink(const char *oldpath, const char *newpath);
273static void read_config_file(char *path, int optional, unsigned long *event_mask);
274static void process_config_line(const char *, unsigned long *);
275static int do_servicing(int, unsigned long);
276static void service_name(const struct devfsd_notify_struct *);
277static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
278static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000279 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000280static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
281static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100282 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000283static void action_compat(const struct devfsd_notify_struct *, unsigned);
284static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000285static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000286static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
287static mode_t get_mode(const char *);
288static void signal_handler(int);
289static const char *get_variable(const char *, void *);
290static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000291static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100292 const char *, const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000293static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000294static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000295 const char *(*)(const char *, void *), void *);
296static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100297static char get_old_ide_name(unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000298static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000299
300/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000301static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000302static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000303static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
304static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000305
306/* Structs and vars */
307static struct config_entry_struct *first_config = NULL;
308static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000309static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000310static volatile int caught_signal = FALSE;
311static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000312static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000313 const char *dest;
314 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000315} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000316 {"/proc/self/fd", "fd"},
317 {"fd/0", "stdin"},
318 {"fd/1", "stdout"},
319 {"fd/2", "stderr"},
320 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000321};
322
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000323static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000324 unsigned int type; /* The DEVFSD_NOTIFY_* value */
325 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000326} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000327 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
328 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
329 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
330 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
331 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
332 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
333 {DEVFSD_NOTIFY_CREATE, "CREATE"},
334 {DEVFSD_NOTIFY_DELETE, "DELETE"},
335 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000336};
337
Rob Landley1ba19d62005-10-08 17:42:35 +0000338/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000339
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000340static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
341static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
342static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
343static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000344
Rob Landley1ba19d62005-10-08 17:42:35 +0000345/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000346#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
Denys Vlasenko066e76b2016-03-30 16:20:28 +0200347#define info_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000348#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
349#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
350#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
351#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000352#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000353#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000354#define msg_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000355#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000356#define error_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000357#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Eric Andersenf18bd892003-12-19 11:07:59 +0000358#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000359
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000360static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000361{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000362 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000363 dest[len] = '\0';
364}
365
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000366static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000367{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000368 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000369 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000370 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000371 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000372 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000373 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000374 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000375 return 5 + addendum;
376 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000377}
378
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000379static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000380{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000381 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
382 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
383 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
384 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000385 return 1;
386 return scan_dev_name_common(d, n, 0, ptr);
387 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000388 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
389 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
390 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000391 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000392 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000393 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000394 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000395 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000396 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000397 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000398 return 0;
399}
400
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000401/* Public functions follow */
402
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000403int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000404int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000405{
406 int print_version = FALSE;
407 int do_daemon = TRUE;
408 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000409 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000410 int fd, proto_rev, count;
411 unsigned long event_mask = 0;
412 struct sigaction new_action;
413 struct initial_symlink_struct *curr;
414
415 if (argc < 2)
416 bb_show_usage();
417
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000418 for (count = 2; count < argc; ++count) {
419 if (argv[count][0] == '-') {
420 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000421 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000422 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000423 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
424 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000425 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000426 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
427 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000428 else
429 bb_show_usage();
430 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000431 }
432
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000433 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000434
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000435 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000436
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000437 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000438 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000439 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000440
441 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000442 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000443 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000444
445 /* NB: The check for CONFIG_FILE is done in read_config_file() */
446
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000447 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000448 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000449 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000450 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000451 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000452 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000453 exit(EXIT_SUCCESS); /* -v */
454 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000455 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000456 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000457
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000458 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000459 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000460 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000461 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000462 sigaction_set(SIGHUP, &new_action);
463 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000464
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000465 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000466
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000467 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000468 umask(0);
469 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000470 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000471 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000472
Rob Landley1ba19d62005-10-08 17:42:35 +0000473 if (ENABLE_DEVFSD_FG_NP && no_polling)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000474 exit(EXIT_SUCCESS);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000475
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000476 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
477 logmode = LOGMODE_BOTH;
478 else if (do_daemon == TRUE)
479 logmode = LOGMODE_SYSLOG;
480 /* This is the default */
481 /*else
482 logmode = LOGMODE_STDIO; */
483
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000484 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000485 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000486 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000487 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000488 } else if (ENABLE_DEVFSD_FG_NP) {
489 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000490 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000491
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000492 while (TRUE) {
493 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000494
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000495 free_config();
496 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000497 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000498 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000499 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000500 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000501} /* End Function main */
502
503
504/* Private functions follow */
505
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000506static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000507/* [SUMMARY] Read a configuration database.
508 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000509 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510 <optional> If TRUE, the routine will silently ignore a missing config file.
511 <event_mask> The event mask is written here. This is not initialised.
512 [RETURNS] Nothing.
513*/
514{
515 struct stat statbuf;
516 FILE *fp;
517 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000518 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000519 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000520
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000521 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000522 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000523 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000524 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000525 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000526 p = bb_simplify_path(path);
527 dir_operation(READ_CONFIG, p, 0, event_mask);
528 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000529 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000530 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000531 fp = fopen_for_read(path);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000532 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000533 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000534 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000535 line = buf;
536 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000537 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000538 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000539 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000540 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000541 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000542 } else {
543 goto read_config_file_err;
544 }
545 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000546read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000547 if (optional == 0 && errno == ENOENT)
548 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000549 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000550} /* End Function read_config_file */
551
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000552static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000553/* [SUMMARY] Process a line from a configuration file.
554 <line> The configuration line.
555 <event_mask> The event mask is written here. This is not initialised.
556 [RETURNS] Nothing.
557*/
558{
559 int num_args, count;
560 struct config_entry_struct *new;
561 char p[MAX_ARGS][STRING_LENGTH];
562 char when[STRING_LENGTH], what[STRING_LENGTH];
563 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000564 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000565 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000566 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000567
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000568 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000569 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000570 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
571 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
572 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
573 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000574
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000575 for (count = 0; count < MAX_ARGS; ++count)
576 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000577 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000578 when, name, what,
579 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000580
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000581 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000582
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000583 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000584 if (i == 0) {
585 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000586 *event_mask = 0;
587 return;
588 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000589
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000590 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000591 goto process_config_line_err;
592
Eric Andersenf18bd892003-12-19 11:07:59 +0000593 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000594 if (i == 1 || i == 2) {
595 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000596 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000597 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000598 return;
599 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000600 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000601 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000602 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000603 return;
604 }
605 if (num_args < 3)
606 goto process_config_line_err;
607
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000608 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000609
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000610 for (count = 0; event_types[count].config_name != NULL; ++count) {
611 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000612 continue;
613 new->action.when = event_types[count].type;
614 break;
615 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000616 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000617 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000618 goto process_config_line_err;
619 }
620
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000621 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000622
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000623 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000624 case 4: /* "PERMISSIONS" */
625 new->action.what = AC_PERMISSIONS;
626 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000627 ptr = strchr(p[0], '.');
628 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000629 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000630 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000631 }
632
633 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000634 new->u.permissions.uid = get_uid_gid(UID, p[0]);
635 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000636 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000637 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000638 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000639 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000640 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000641 the device name) to the module loading facility. In addition,
642 the /etc/modules.devfs configuration file is used.*/
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100643 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000644 new->action.what = AC_MODLOAD;
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100645 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000646 case 6: /* EXECUTE */
647 new->action.what = AC_EXECUTE;
648 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000649
Eric Andersenf18bd892003-12-19 11:07:59 +0000650 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000651 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000652
Eric Andersenf18bd892003-12-19 11:07:59 +0000653 new->u.execute.argv[num_args] = NULL;
654 break;
655 case 7: /* COPY */
656 new->action.what = AC_COPY;
657 num_args -= 3;
658 if (num_args != 2)
659 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000660
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000661 new->u.copy.source = xstrdup(p[0]);
662 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000663 break;
664 case 8: /* IGNORE */
665 /* FALLTROUGH */
666 case 9: /* MKOLDCOMPAT */
667 /* FALLTROUGH */
668 case 10: /* MKNEWCOMPAT */
669 /* FALLTROUGH */
670 case 11:/* RMOLDCOMPAT */
671 /* FALLTROUGH */
672 case 12: /* RMNEWCOMPAT */
673 /* AC_IGNORE 6
674 AC_MKOLDCOMPAT 7
675 AC_MKNEWCOMPAT 8
676 AC_RMOLDCOMPAT 9
677 AC_RMNEWCOMPAT 10*/
678 new->action.what = i - 2;
679 break;
680 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000681 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000682 goto process_config_line_err;
683 /*esac*/
684 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000685
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000686 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000687
688 *event_mask |= 1 << new->action.when;
689 new->next = NULL;
690 if (first_config == NULL)
691 first_config = new;
692 else
693 last_config->next = new;
694 last_config = new;
695 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000696
697 process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000698 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699} /* End Function process_config_line */
700
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000701static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000702/* [SUMMARY] Service devfs changes until a signal is received.
703 <fd> The open control file.
704 <event_mask> The event mask.
705 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
706*/
707{
708 ssize_t bytes;
709 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000710
Denis Vlasenko856be772007-08-17 08:29:48 +0000711 /* (void*) cast is only in order to match prototype */
712 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000713 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000714 errno = 0;
Denys Vlasenko083e1722010-01-28 12:30:24 +0100715 bytes = read(fd, (char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000716 if (caught_signal)
717 break; /* Must test for this first */
718 if (errno == EINTR)
719 continue; /* Yes, the order is important */
720 if (bytes < 1)
721 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000722 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000723 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000724 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000725 int c_sighup = caught_sighup;
726
727 caught_signal = FALSE;
728 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000729 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000730 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000731 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000732} /* End Function do_servicing */
733
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000734static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000735/* [SUMMARY] Service a single devfs change.
736 <info> The devfs change.
737 [RETURNS] Nothing.
738*/
739{
740 unsigned int n;
741 regmatch_t mbuf[MAX_SUBEXPR];
742 struct config_entry_struct *entry;
743
Rob Landley1ba19d62005-10-08 17:42:35 +0000744 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000745 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000746
747 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000748 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000749 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000750 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000751 || (info->devname[0] == 'i' && info->devname[1] == 'n'
752 && info->devname[2] == 'i' && info->devname[3] == 't'
753 && info->devname[4] == 'c' && info->devname[5] == 't'
754 && info->devname[6] == 'l' && !info->devname[7]))
755 )
756 return;
757
758 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000759 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000760 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000761 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000762 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000763 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000764 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000765
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000766 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000767 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000768 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000769 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000770 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000771 if (ENABLE_DEVFSD_MODLOAD)
772 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000773 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000774 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000775 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000776 break;
777 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000778 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000779 break;
780 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000781 return;
782 /*break;*/
783 case AC_MKOLDCOMPAT:
784 case AC_MKNEWCOMPAT:
785 case AC_RMOLDCOMPAT:
786 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000787 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000788 break;
789 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000790 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000791 }
792 }
793} /* End Function service_name */
794
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000795static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000796 const struct config_entry_struct *entry)
797/* [SUMMARY] Update permissions for a device entry.
798 <info> The devfs change.
799 <entry> The config file entry.
800 [RETURNS] Nothing.
801*/
802{
803 struct stat statbuf;
804
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000805 if (stat(info->devname, &statbuf) != 0
806 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
807 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000808 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000809 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000810} /* End Function action_permissions */
811
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000812static void action_modload(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100813 const struct config_entry_struct *entry UNUSED_PARAM)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000814/* [SUMMARY] Load a module.
815 <info> The devfs change.
816 <entry> The config file entry.
817 [RETURNS] Nothing.
818*/
819{
820 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000821
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000822 argv[0] = (char*)MODPROBE;
823 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
824 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
825 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000826 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000827 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000828
Denys Vlasenko8531d762010-03-18 22:44:00 +0100829 spawn_and_wait(argv);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000830 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000831} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000832
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000833static void action_execute(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100834 const struct config_entry_struct *entry,
835 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000836/* [SUMMARY] Execute a programme.
837 <info> The devfs change.
838 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000839 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000840 device name.
841 <numexpr> The number of elements within <<regexpr>>.
842 [RETURNS] Nothing.
843*/
844{
845 unsigned int count;
846 struct get_variable_info gv_info;
847 char *argv[MAX_ARGS + 1];
848 char largv[MAX_ARGS + 1][STRING_LENGTH];
849
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000850 gv_info.info = info;
851 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000852 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
853 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
854 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000855 entry->u.execute.argv[count],
856 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000857 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000858 argv[count] = largv[count];
859 }
860 argv[count] = NULL;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100861 spawn_and_wait(argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000862} /* End Function action_execute */
863
864
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000865static void action_copy(const struct devfsd_notify_struct *info,
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100866 const struct config_entry_struct *entry,
867 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000868/* [SUMMARY] Copy permissions.
869 <info> The devfs change.
870 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000871 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000872 device name.
873 <numexpr> The number of elements in <<regexpr>>.
874 [RETURNS] Nothing.
875*/
876{
877 mode_t new_mode;
878 struct get_variable_info gv_info;
879 struct stat source_stat, dest_stat;
880 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000881 int ret = 0;
882
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000883 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000884
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000885 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000886 return;
887 gv_info.info = info;
888 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000889
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000890 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
891 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000892 get_variable, &gv_info, gv_info.devname,
893 regexpr, numexpr);
894
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000895 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000896 get_variable, &gv_info, gv_info.devname,
897 regexpr, numexpr);
898
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000899 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000900 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000901 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000902 new_mode = source_stat.st_mode & ~S_ISVTX;
903 if (info->type == DEVFSD_NOTIFY_CREATE)
904 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000905 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000906 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000907 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000908 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000909 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000910} /* End Function action_copy */
911
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000912static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000913/* [SUMMARY] Process a compatibility request.
914 <info> The devfs change.
915 <action> The action to take.
916 [RETURNS] Nothing.
917*/
918{
Rob Landley1ba19d62005-10-08 17:42:35 +0000919 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000920 const char *compat_name = NULL;
921 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000922 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000923 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000924 int mode, host, bus, target, lun;
925 unsigned int i;
926 char rewind_;
927 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000928 static const char *const fmt[] = {
929 NULL ,
930 "sg/c%db%dt%du%d", /* scsi/generic */
931 "sd/c%db%dt%du%d", /* scsi/disc */
932 "sr/c%db%dt%du%d", /* scsi/cd */
933 "sd/c%db%dt%du%dp%d", /* scsi/part */
934 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
935 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
936 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
937 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
938 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
939 NULL
940 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000941
942 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000943 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944 case AC_MKOLDCOMPAT:
945 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000946 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000947 break;
948 case AC_MKNEWCOMPAT:
949 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000950 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000951 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000952
953 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000954 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000955 return;
956
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000957 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
958 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000959 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000960 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000961
Eric Andersenf18bd892003-12-19 11:07:59 +0000962
963 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000964 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
965 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000966
967 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000968 if (i == 4 || i == 8)
969 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000970
971 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000972 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000973 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000974 if (rewind_ != 'n')
975 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000976 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000977 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000978 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000979 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000980 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000981 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000982 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000983
Eric Andersenf18bd892003-12-19 11:07:59 +0000984 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000985 if (i == 9)
986 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000987 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000988 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000989
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000990 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000991 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000992
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000993 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000994 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000995 case AC_MKOLDCOMPAT:
996 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000997 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000998 break;
999 case AC_RMOLDCOMPAT:
1000 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001001 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001002 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001003 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001004 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001005 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001006 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007} /* End Function action_compat */
1008
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001009static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001010{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001011 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001012 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001013
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001014 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001015 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001016 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001017 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001018 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001019 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001020
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001021 if (S_ISDIR(source_stat.st_mode))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001022 dir_operation(RESTORE, spath, rootlen, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001023}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001024
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001025
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001026static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001027 mode_t new_mode,
1028 const char *sourcepath, const struct stat *source_stat)
1029/* [SUMMARY] Copy an inode.
1030 <destpath> The destination path. An existing inode may be deleted.
1031 <dest_stat> The destination stat(2) information.
1032 <new_mode> The desired new mode for the destination.
1033 <sourcepath> The source path.
1034 <source_stat> The source stat(2) information.
1035 [RETURNS] TRUE on success, else FALSE.
1036*/
1037{
Eric Andersenf18bd892003-12-19 11:07:59 +00001038 int source_len, dest_len;
1039 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1040 int fd, val;
1041 struct sockaddr_un un_addr;
1042 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001043
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001044 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001045 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001046 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001047 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
1048 if ((source_len < 0)
1049 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001050 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001051 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001052 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001053 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001054 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
1055 unlink(destpath);
1056 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001057 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001058 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001059 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001060 chmod(destpath, new_mode & ~S_IFMT);
1061 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001062 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001063 }
1064 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001065 unlink(destpath);
1066 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001067 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001068 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1069 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001070 break;
1071 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001072 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
Denys Vlasenko083e1722010-01-28 12:30:24 +01001073 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001074 close(fd);
1075 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001076 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001077 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001078 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001079 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1080 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001081 break;
1082 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001083 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001084 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001085 break;
1086 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001087 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1088 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001089 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001090 close(fd);
1091 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001092 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001093 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001094 case S_IFBLK:
1095 case S_IFCHR:
1096 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001097 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001098 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001099 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001100 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001101 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001102 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001103do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001104 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001105 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001106 /*break;*/
1107 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001108 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001109} /* End Function copy_inode */
1110
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001111static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001112/* [SUMMARY] Free the configuration information.
1113 [RETURNS] Nothing.
1114*/
1115{
1116 struct config_entry_struct *c_entry;
1117 void *next;
1118
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001119 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001120 unsigned int count;
1121
1122 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001123 regfree(&c_entry->preg);
1124 if (c_entry->action.what == AC_EXECUTE) {
1125 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001126 if (c_entry->u.execute.argv[count] == NULL)
1127 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001128 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001129 }
1130 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001131 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001132 }
1133 first_config = NULL;
1134 last_config = NULL;
1135} /* End Function free_config */
1136
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001137static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001138/* [SUMMARY] Convert a string to a UID or GID value.
1139 <flag> "UID" or "GID".
1140 <string> The string.
1141 [RETURNS] The UID or GID value.
1142*/
1143{
1144 struct passwd *pw_ent;
1145 struct group *grp_ent;
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001146 const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001147
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001148 if (isdigit(string[0]) || ((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001149 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001150
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001151 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001152 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001153
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001154 if (ENABLE_DEVFSD_VERBOSE)
1155 msg = "user";
1156
1157 if (flag == GID) {
1158 if ((grp_ent = getgrnam(string)) != NULL)
1159 return grp_ent->gr_gid;
1160 if (ENABLE_DEVFSD_VERBOSE)
1161 msg = "group";
1162 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001163
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001164 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001165 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001166 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001167}/* End Function get_uid_gid */
1168
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001169static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001170/* [SUMMARY] Convert a string to a mode value.
1171 <string> The string.
1172 [RETURNS] The mode value.
1173*/
1174{
1175 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001176 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001177
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001178 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001179 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001180 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001181 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001182
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001183 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001184 i = S_IRUSR;
1185 while (i > 0) {
1186 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1187 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001188 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001189 string++;
1190 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001191 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001192} /* End Function get_mode */
1193
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001194static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001196 caught_signal = TRUE;
1197 if (sig == SIGHUP)
1198 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001199
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001200 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001201} /* End Function signal_handler */
1202
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001203static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001204{
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001205 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001206
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001207 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001208 const char *field_names[] = {
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001209 "hostname", "mntpt", "devpath", "devname", "uid", "gid", "mode",
1210 NULL, mount_point, gv_info->devpath, gv_info->devname, NULL
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001211 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001212 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001213
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001214 if (!hostname)
1215 hostname = safe_gethostname();
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001216 field_names[7] = hostname;
1217
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001218 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001219 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001220
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001221 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001222 return NULL;
1223 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001224 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001225
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001226 if (i == 4)
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001227 return auto_string(xasprintf("%u", gv_info->info->uid));
1228 if (i == 5)
1229 return auto_string(xasprintf("%u", gv_info->info->gid));
1230 /* i == 6 */
1231 return auto_string(xasprintf("%o", gv_info->info->mode));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001232} /* End Function get_variable */
1233
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001234static void service(struct stat statbuf, char *path)
1235{
1236 struct devfsd_notify_struct info;
1237
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001238 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001239 info.type = DEVFSD_NOTIFY_REGISTERED;
1240 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001241 info.major = major(statbuf.st_rdev);
1242 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001243 info.uid = statbuf.st_uid;
1244 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001245 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1246 info.namelen = strlen(info.devname);
1247 service_name(&info);
1248 if (S_ISDIR(statbuf.st_mode))
1249 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001250}
1251
1252static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001253/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001254 <flag> To choose which function to perform
1255 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001256 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001257 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001258 [RETURNS] Nothing.
1259*/
1260{
1261 struct stat statbuf;
1262 DIR *dp;
1263 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001264 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001265
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001266 dp = warn_opendir(dir_name);
1267 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001268 return;
1269
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001270 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001271
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001272 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001273 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001274 path = concat_path_file(dir_name, de->d_name);
1275 if (lstat(path, &statbuf) == 0) {
1276 switch (type) {
1277 case SERVICE:
1278 service(statbuf, path);
1279 break;
1280 case RESTORE:
1281 restore(path, statbuf, var);
1282 break;
1283 case READ_CONFIG:
1284 read_config_file(path, var, event_mask);
1285 break;
1286 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001287 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001288 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001289 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001290 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001291} /* End Function do_scan_and_service */
1292
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001293static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001294/* [SUMMARY] Create a symlink, creating intervening directories as required.
1295 <oldpath> The string contained in the symlink.
1296 <newpath> The name of the new symlink.
1297 [RETURNS] 0 on success, else -1.
1298*/
1299{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001300 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001301 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001302
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001303 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001304 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001305 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001306 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001307 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001308} /* End Function mksymlink */
1309
1310
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001311static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001312/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001313 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001314 [RETURNS] TRUE on success, else FALSE.
1315*/
1316{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001317 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001318 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001319 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001320} /* End Function make_dir_tree */
1321
1322static int expand_expression(char *output, unsigned int outsize,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001323 const char *input,
1324 const char *(*get_variable_func)(const char *variable, void *info),
1325 void *info,
1326 const char *devname,
1327 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001328/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001329 <output> The output expanded expression is written here.
1330 <length> The size of the output buffer.
1331 <input> The input expression. This may equal <<output>>.
1332 <get_variable> A function which will be used to get variable values. If
1333 this returns NULL, the environment is searched instead. If this is NULL,
1334 only the environment is searched.
1335 <info> An arbitrary pointer passed to <<get_variable>>.
1336 <devname> Device name; specifically, this is the string that contains all
1337 of the regular subexpressions.
1338 <ex> Array of start / end offsets into info->devname for each subexpression
1339 <numexp> Number of regular subexpressions found in <<devname>>.
1340 [RETURNS] TRUE on success, else FALSE.
1341*/
1342{
1343 char temp[STRING_LENGTH];
1344
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001345 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001346 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001347 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001348 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001349} /* End Function expand_expression */
1350
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001351static void expand_regexp(char *output, size_t outsize, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001352 const char *devname,
1353 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001354/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1355 <output> The output expanded expression is written here.
1356 <outsize> The size of the output buffer.
1357 <input> The input expression. This may NOT equal <<output>>, because
1358 supporting that would require yet another string-copy. However, it's not
1359 hard to write a simple wrapper function to add this functionality for those
1360 few cases that need it.
1361 <devname> Device name; specifically, this is the string that contains all
1362 of the regular subexpressions.
1363 <ex> An array of start and end offsets into <<devname>>, one for each
1364 subexpression
1365 <numex> Number of subexpressions in the offset-array <<ex>>.
1366 [RETURNS] Nothing.
1367*/
1368{
1369 const char last_exp = '0' - 1 + numex;
1370 int c = -1;
1371
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001372 /* Guarantee NULL termination by writing an explicit '\0' character into
1373 the very last byte */
1374 if (outsize)
1375 output[--outsize] = '\0';
1376 /* Copy the input string into the output buffer, replacing '\\' with '\'
1377 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1378 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001379 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001380 c = *input;
1381 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001382 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001383 c = *input;
1384 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001385 if (c != '\\') {
1386 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001387 const regmatch_t *subexp = ex + (c - '0');
1388 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1389
1390 /* Range checking */
1391 if (sublen > outsize)
1392 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001393 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001394 output += sublen;
1395 outsize -= sublen;
1396 }
1397 continue;
1398 }
1399 }
1400 *output = c;
1401 ++output;
1402 --outsize;
1403 } /* while */
1404} /* End Function expand_regexp */
1405
1406
1407/* from compat_name.c */
1408
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +02001409struct translate_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001410 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001411 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001412 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001413};
1414
1415static struct translate_struct translate_table[] =
1416{
1417 {"sound/", NULL},
1418 {"printers/", "lp%s"},
1419 {"v4l/", NULL},
1420 {"parports/", "parport%s"},
1421 {"fb/", "fb%s"},
1422 {"netlink/", NULL},
1423 {"loop/", "loop%s"},
1424 {"floppy/", "fd%s"},
1425 {"rd/", "ram%s"},
1426 {"md/", "md%s"}, /* Meta-devices */
1427 {"vc/", "tty%s"},
1428 {"misc/", NULL},
1429 {"isdn/", NULL},
1430 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1431 {"i2c/", "i2c-%s"},
1432 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1433 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1434 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1435 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1436 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1437 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1438 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1439 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1440 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1441 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1442 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1443 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1444 {"input/js", "js%s"}, /* Joystick driver */
1445 {NULL, NULL}
1446};
1447
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001448const char *get_old_name(const char *devname, unsigned int namelen,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001449 char *buffer, unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001450/* [SUMMARY] Translate a kernel-supplied name into an old name.
1451 <devname> The device name provided by the kernel.
1452 <namelen> The length of the name.
1453 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1454 <major> The major number for the device.
1455 <minor> The minor number for the device.
1456 [RETURNS] A pointer to the old name if known, else NULL.
1457*/
1458{
1459 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001460 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001461 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001462 unsigned int i;
1463 char mode;
1464 int indexx;
1465 const char *pty1;
1466 const char *pty2;
Eric Andersenf18bd892003-12-19 11:07:59 +00001467 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001468 static const char *const fmt[] = {
1469 NULL ,
1470 "sg%u", /* scsi/generic */
1471 NULL, /* scsi/disc */
1472 "sr%u", /* scsi/cd */
1473 NULL, /* scsi/part */
1474 "nst%u%c", /* scsi/mt */
1475 "hd%c" , /* ide/host/disc */
1476 "hd%c" , /* ide/host/cd */
1477 "hd%c%s", /* ide/host/part */
1478 "%sht%d", /* ide/host/mt */
1479 "sbpcd%u", /* sbp/ */
1480 "vcs%s", /* vcc/ */
1481 "%cty%c%c", /* pty/ */
1482 NULL
1483 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001484
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001485 for (trans = translate_table; trans->match != NULL; ++trans) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001486 char *after_match = is_prefixed_with(devname, trans->match);
1487 if (after_match) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001488 if (trans->format == NULL)
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001489 return after_match;
1490 sprintf(buffer, trans->format, after_match);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001491 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001492 }
1493 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001494
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001495 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001496 i = scan_dev_name(devname, namelen, ptr);
1497
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001498 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001499 compat_name = buffer;
1500 else
1501 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001502
Eric Andersenf18bd892003-12-19 11:07:59 +00001503 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001504 if (i == 1 || i == 3 || i == 10)
1505 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001506
1507 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001508 if (i == 2 || i == 4)
Denys Vlasenko083e1722010-01-28 12:30:24 +01001509 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001510
1511 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001512 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001513 mode = ptr[2];
1514 if (mode == 'n')
1515 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001516 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001517 if (devname[namelen - 1] != 'n')
1518 ++compat_name;
1519 }
1520 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001521 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001522 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001523 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001524
1525 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001526 if (i == 9)
1527 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001528
1529 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001530 if (i == 11) {
1531 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001532 if (buffer[3] == '0')
1533 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001534 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001535 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001536 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001537 pty1 = "pqrstuvwxyzabcde";
1538 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001539 indexx = atoi(devname + 5);
1540 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001541 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001542 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001543} /* End Function get_old_name */
1544
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001545static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001546/* [SUMMARY] Get the old IDE name for a device.
1547 <major> The major number for the device.
1548 <minor> The minor number for the device.
1549 [RETURNS] The drive letter.
1550*/
1551{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001552 char letter = 'y'; /* 121 */
1553 char c = 'a'; /* 97 */
1554 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001555
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001556 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1557 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001558 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1559 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1560 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1561 || i == IDE9_MAJOR
1562 ) {
1563 if ((unsigned int)i == major) {
1564 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001565 break;
1566 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001567 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001568 }
1569 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001570 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001571
1572 if (minor > 63)
1573 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001574 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001575} /* End Function get_old_ide_name */
1576
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001577static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001578 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001579 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001580/* [SUMMARY] Write the old SCSI disc name to a buffer.
1581 <buffer> The buffer to write to.
1582 <major> The major number for the device.
1583 <minor> The minor number for the device.
1584 <part> The partition string. Must be "" for a whole-disc entry.
1585 [RETURNS] A pointer to the buffer on success, else NULL.
1586*/
1587{
1588 unsigned int disc_index;
1589
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001590 if (major == 8) {
1591 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001592 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001593 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001594 if ((major > 64) && (major < 72)) {
1595 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001596 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001597 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001598 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001599 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001600 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001601 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001602 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001603} /* End Function write_old_sd_name */
1604
1605
1606/* expression.c */
1607
1608/*EXPERIMENTAL_FUNCTION*/
1609
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001610int st_expr_expand(char *output, unsigned int length, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001611 const char *(*get_variable_func)(const char *variable,
1612 void *info),
1613 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001614/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1615 <output> The output expanded expression is written here.
1616 <length> The size of the output buffer.
1617 <input> The input expression. This may equal <<output>>.
1618 <get_variable> A function which will be used to get variable values. If
1619 this returns NULL, the environment is searched instead. If this is NULL,
1620 only the environment is searched.
1621 <info> An arbitrary pointer passed to <<get_variable>>.
1622 [RETURNS] TRUE on success, else FALSE.
1623*/
1624{
1625 char ch;
1626 unsigned int len;
1627 unsigned int out_pos = 0;
1628 const char *env;
1629 const char *ptr;
1630 struct passwd *pwent;
1631 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1632
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001633 if (length > BUFFER_SIZE)
1634 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001635 for (; TRUE; ++input) {
1636 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001637 case '$':
1638 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001639 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001640 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001641 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001642 break;
1643 case '~':
1644 /* Home directory expansion */
1645 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001646 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001647 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001648 env = getenv("HOME");
1649 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001650 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001651 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001652 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001653 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001654 if (len + out_pos >= length)
1655 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001656 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001657 out_pos += len;
1658 continue;
1659 }
1660 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001661 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001662 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001663 len = ptr - input;
1664 if (len >= sizeof tmp)
1665 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001666 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001667 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001668 pwent = getpwnam(tmp);
1669 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001670 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001671 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001672 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001673 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001674 if (len + out_pos >= length)
1675 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001676 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001677 out_pos += len;
1678 break;
1679 case '\0':
1680 /* Falltrough */
1681 default:
1682 if (out_pos >= length)
1683 goto st_expr_expand_out;
1684 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001685 if (ch == '\0') {
1686 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001687 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001688 }
1689 break;
1690 /* esac */
1691 }
1692 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001693 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001694st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001695 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001696 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001697} /* End Function st_expr_expand */
1698
1699
1700/* Private functions follow */
1701
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001702static const char *expand_variable(char *buffer, unsigned int length,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001703 unsigned int *out_pos, const char *input,
1704 const char *(*func)(const char *variable,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001705 void *info),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001706 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001707/* [SUMMARY] Expand a variable.
1708 <buffer> The buffer to write to.
1709 <length> The length of the output buffer.
1710 <out_pos> The current output position. This is updated.
1711 <input> A pointer to the input character pointer.
1712 <func> A function which will be used to get variable values. If this
1713 returns NULL, the environment is searched instead. If this is NULL, only
1714 the environment is searched.
1715 <info> An arbitrary pointer passed to <<func>>.
1716 <errfp> Diagnostic messages are written here.
1717 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1718*/
1719{
1720 char ch;
1721 int len;
1722 unsigned int open_braces;
1723 const char *env, *ptr;
1724 char tmp[STRING_LENGTH];
1725
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001726 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001727 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001728 /* Special case for "$$": PID */
Denys Vlasenko083e1722010-01-28 12:30:24 +01001729 sprintf(tmp, "%d", (int) getpid());
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001730 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001731 if (len + *out_pos >= length)
1732 goto expand_variable_out;
1733
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001734 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001735 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001736 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001737 }
1738 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001739 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001740 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001741 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001742 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001743 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001744 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001745 goto expand_variable_out;
1746
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001747 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001748 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001749 env = get_variable_v2(tmp, func, info);
1750 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001751 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001752 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001753 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001754 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001755 if (len + *out_pos >= length)
1756 goto expand_variable_out;
1757
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001758 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001759 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001760 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001761 }
1762 /* Variable in braces: check for ':' tricks */
1763 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001764 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001765 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001766 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001767 /* Must be simple variable expansion with "${var}" */
1768 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001769 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001770 goto expand_variable_out;
1771
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001772 safe_memcpy(tmp, input, len);
1773 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001774 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001775 return NULL;
1776 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001777 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001778 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001779 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001780 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001781 }
1782 /* It's that handy "${var:-word}" expression. Check if var is defined */
1783 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001784 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001785 goto expand_variable_out;
1786
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001787 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001788 /* Move input pointer to ':' */
1789 input = ptr;
1790 /* First skip to closing brace, taking note of nested expressions */
1791 ptr += 2;
1792 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001793 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1794 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001795 case '{':
1796 ++open_braces;
1797 break;
1798 case '}':
1799 --open_braces;
1800 break;
1801 case '\0':
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001802 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001803 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001804 default:
1805 break;
1806 }
1807 }
1808 --ptr;
1809 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001810 env = get_variable_v2(tmp, func, info);
1811 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001812 /* Found environment variable, so skip the input to the closing brace
1813 and return the variable */
1814 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001815 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001816 if (len + *out_pos >= length)
1817 goto expand_variable_out;
1818
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001819 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001820 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001821 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001822 }
1823 /* Environment variable was not found, so process word. Advance input
1824 pointer to start of word in "${var:-word}" */
1825 input += 2;
1826 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001827 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001828 goto expand_variable_out;
1829
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001830 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001831 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001832 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001833 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001834 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001835 if (len + *out_pos >= length)
1836 goto expand_variable_out;
1837
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001838 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001839 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001840 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001841expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001842 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001843 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001844} /* End Function expand_variable */
1845
1846
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001847static const char *get_variable_v2(const char *variable,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001848 const char *(*func)(const char *variable, void *info),
1849 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001850/* [SUMMARY] Get a variable from the environment or .
1851 <variable> The variable name.
1852 <func> A function which will be used to get the variable. If this returns
1853 NULL, the environment is searched instead. If this is NULL, only the
1854 environment is searched.
1855 [RETURNS] The value of the variable on success, else NULL.
1856*/
1857{
1858 const char *value;
1859
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001860 if (func != NULL) {
1861 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001862 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001863 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001864 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001865 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001866} /* End Function get_variable */
1867
1868/* END OF CODE */