blob: 3bf06b965b5d8e9de82e64a36e87a2098d45dea0 [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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020062//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.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010066//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020067//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".
Denys Vlasenkofb4da162016-11-22 23:14:24 +010073//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020074//config: But only if they are written UPPERCASE!!!!!!!!
Denys Vlasenkofb4da162016-11-22 23:14:24 +010075//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020081//config: This actually doesn't work with busybox modutils but needs
82//config: the external modutils.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010083//config:
84//config:config DEVFSD_FG_NP
Denys Vlasenkof5604222017-01-10 14:58:54 +010085//config: bool "Enable the -fg and -np options"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010086//config: default y
87//config: depends on DEVFSD
88//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020089//config: -fg Run the daemon in the foreground.
90//config: -np Exit after parsing config. Do not poll for events.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010091//config:
92//config:config DEVFSD_VERBOSE
93//config: bool "Increases logging (and size)"
94//config: default y
95//config: depends on DEVFSD
96//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020097//config: Increases logging to stderr or syslog.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010098//config:
99//config:config FEATURE_DEVFS
100//config: bool "Use devfs names for all devices (obsolete)"
101//config: default n
102//config: select PLATFORM_LINUX
103//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200104//config: This is obsolete and should NOT be used anymore.
105//config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
Denys Vlasenkofb4da162016-11-22 23:14:24 +0100106//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200107//config: For legacy systems -- if there is no way around devfsd -- this
108//config: tells busybox to look for names like /dev/loop/0 instead of
109//config: /dev/loop0. If your /dev directory has normal names instead of
110//config: devfs names, you don't want this.
Pere Orga5bc8c002011-04-11 03:29:49 +0200111
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +0100112//applet:IF_DEVFSD(APPLET(devfsd, BB_DIR_SBIN, BB_SUID_DROP))
113
114//kbuild:lib-$(CONFIG_DEVFSD) += devfsd.o
115
Pere Orga5bc8c002011-04-11 03:29:49 +0200116//usage:#define devfsd_trivial_usage
117//usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
118//usage:#define devfsd_full_usage "\n\n"
119//usage: "Manage devfs permissions and old device name symlinks\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200120//usage: "\n mntpnt The mount point where devfs is mounted"
121//usage: "\n -v Print the protocol version numbers for devfsd"
122//usage: "\n and the kernel-side protocol version and exit"
123//usage: IF_DEVFSD_FG_NP(
124//usage: "\n -fg Run in foreground"
125//usage: "\n -np Exit after parsing the configuration file"
126//usage: "\n and processing synthetic REGISTER events,"
127//usage: "\n don't poll for events"
128//usage: )
129
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +0000130#include "libbb.h"
131#include "xregex.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000132#include <syslog.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000133
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000134#include <sys/un.h>
135#include <sys/sysmacros.h>
Eric Andersen2af70002003-10-09 21:02:23 +0000136
137/* Various defines taken from linux/major.h */
138#define IDE0_MAJOR 3
139#define IDE1_MAJOR 22
140#define IDE2_MAJOR 33
141#define IDE3_MAJOR 34
142#define IDE4_MAJOR 56
143#define IDE5_MAJOR 57
144#define IDE6_MAJOR 88
145#define IDE7_MAJOR 89
146#define IDE8_MAJOR 90
147#define IDE9_MAJOR 91
148
149
150/* Various defines taken from linux/devfs_fs.h */
151#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200152#define DEVFSD_IOCTL_BASE 'd'
Eric Andersen2af70002003-10-09 21:02:23 +0000153/* These are the various ioctls */
154#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
155#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
156#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +0000157#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +0000158#define DEVFSD_NOTIFY_REGISTERED 0
159#define DEVFSD_NOTIFY_UNREGISTERED 1
160#define DEVFSD_NOTIFY_ASYNC_OPEN 2
161#define DEVFSD_NOTIFY_CLOSE 3
162#define DEVFSD_NOTIFY_LOOKUP 4
163#define DEVFSD_NOTIFY_CHANGE 5
164#define DEVFSD_NOTIFY_CREATE 6
165#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000166#define DEVFS_PATHLEN 1024
167/* Never change this otherwise the binary interface will change */
168
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200169struct devfsd_notify_struct {
170 /* Use native C types to ensure same types in kernel and user space */
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000171 unsigned int type; /* DEVFSD_NOTIFY_* value */
172 unsigned int mode; /* Mode of the inode or device entry */
173 unsigned int major; /* Major number of device entry */
174 unsigned int minor; /* Minor number of device entry */
175 unsigned int uid; /* Uid of process, inode or device entry */
176 unsigned int gid; /* Gid of process, inode or device entry */
177 unsigned int overrun_count; /* Number of lost events */
178 unsigned int namelen; /* Number of characters not including '\0' */
179 /* The device name MUST come last */
180 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
Eric Andersen2af70002003-10-09 21:02:23 +0000181};
182
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000183#define BUFFER_SIZE 16384
184#define DEVFSD_VERSION "1.3.25"
185#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000186#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000187#define MODPROBE_SWITCH_1 "-k"
188#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000189#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000190#define MAX_ARGS (6 + 1)
191#define MAX_SUBEXPR 10
192#define STRING_LENGTH 255
193
194/* for get_uid_gid() */
195#define UID 0
196#define GID 1
197
Rob Landley1ba19d62005-10-08 17:42:35 +0000198/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000199# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000200# define NO_DIE 0
201
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000202/* for dir_operation() */
203#define RESTORE 0
204#define SERVICE 1
205#define READ_CONFIG 2
206
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000207/* Update only after changing code to reflect new protocol */
208#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
209
210/* Compile-time check */
211#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
212#error protocol version mismatch. Update your kernel headers
213#endif
214
215#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000216#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000217#define AC_EXECUTE 2
218#define AC_MFUNCTION 3 /* not supported by busybox */
219#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000220#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000221#define AC_IGNORE 6
222#define AC_MKOLDCOMPAT 7
223#define AC_MKNEWCOMPAT 8
224#define AC_RMOLDCOMPAT 9
225#define AC_RMNEWCOMPAT 10
226#define AC_RESTORE 11
227
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200228struct permissions_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000229 mode_t mode;
230 uid_t uid;
231 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000232};
233
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200234struct execute_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000235 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000236};
237
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200238struct copy_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000239 const char *source;
240 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000241};
242
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200243struct action_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000244 unsigned int what;
245 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000246};
247
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200248struct config_entry_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000249 struct action_type action;
250 regex_t preg;
251 union
252 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000253 struct permissions_type permissions;
254 struct execute_type execute;
255 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000256 }
257 u;
258 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000259};
260
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200261struct get_variable_info {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000262 const struct devfsd_notify_struct *info;
263 const char *devname;
264 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000265};
266
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000267static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000268static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000269static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000270static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000271static int mksymlink(const char *oldpath, const char *newpath);
272static void read_config_file(char *path, int optional, unsigned long *event_mask);
273static void process_config_line(const char *, unsigned long *);
274static int do_servicing(int, unsigned long);
275static void service_name(const struct devfsd_notify_struct *);
276static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
277static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000278 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000279static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
280static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100281 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000282static void action_compat(const struct devfsd_notify_struct *, unsigned);
283static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000284static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000285static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
286static mode_t get_mode(const char *);
287static void signal_handler(int);
288static const char *get_variable(const char *, void *);
289static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000290static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100291 const char *, const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000292static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000293static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000294 const char *(*)(const char *, void *), void *);
295static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100296static char get_old_ide_name(unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000297static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000298
299/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000300static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000301static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000302static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
303static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000304
305/* Structs and vars */
306static struct config_entry_struct *first_config = NULL;
307static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000308static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000309static volatile int caught_signal = FALSE;
310static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000311static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000312 const char *dest;
313 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000314} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000315 {"/proc/self/fd", "fd"},
316 {"fd/0", "stdin"},
317 {"fd/1", "stdout"},
318 {"fd/2", "stderr"},
319 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000320};
321
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000322static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000323 unsigned int type; /* The DEVFSD_NOTIFY_* value */
324 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000325} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000326 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
327 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
328 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
329 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
330 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
331 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
332 {DEVFSD_NOTIFY_CREATE, "CREATE"},
333 {DEVFSD_NOTIFY_DELETE, "DELETE"},
334 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000335};
336
Rob Landley1ba19d62005-10-08 17:42:35 +0000337/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000338
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000339static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
340static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
341static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
342static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000343
Rob Landley1ba19d62005-10-08 17:42:35 +0000344/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000345#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
Denys Vlasenko066e76b2016-03-30 16:20:28 +0200346#define info_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000347#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
348#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
349#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
350#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000351#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000352#define info_logger(p, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000353#define msg_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000354#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000355#define error_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000356#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Eric Andersenf18bd892003-12-19 11:07:59 +0000357#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000358
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000359static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000360{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000361 memcpy(dest , src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000362 dest[len] = '\0';
363}
364
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000365static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000366{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000367 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000368 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000369 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000370 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000371 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000372 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000373 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000374 return 5 + addendum;
375 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000376}
377
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000378static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000379{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000380 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
381 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
382 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
383 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000384 return 1;
385 return scan_dev_name_common(d, n, 0, ptr);
386 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000387 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
388 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
389 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000390 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000391 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000392 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000393 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000394 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000395 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000396 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000397 return 0;
398}
399
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000400/* Public functions follow */
401
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000402int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000403int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000404{
405 int print_version = FALSE;
406 int do_daemon = TRUE;
407 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000408 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000409 int fd, proto_rev, count;
410 unsigned long event_mask = 0;
411 struct sigaction new_action;
412 struct initial_symlink_struct *curr;
413
414 if (argc < 2)
415 bb_show_usage();
416
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000417 for (count = 2; count < argc; ++count) {
418 if (argv[count][0] == '-') {
419 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000420 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000421 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000422 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
423 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000424 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000425 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
426 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000427 else
428 bb_show_usage();
429 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000430 }
431
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000432 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000433
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000434 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000435
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000436 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000437 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000438 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000439
440 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000441 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000442 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000443
444 /* NB: The check for CONFIG_FILE is done in read_config_file() */
445
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000446 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000447 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000448 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000449 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000450 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000451 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000452 exit(EXIT_SUCCESS); /* -v */
453 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000454 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000455 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000456
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000457 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000458 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000459 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000460 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000461 sigaction_set(SIGHUP, &new_action);
462 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000463
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000464 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000465
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000466 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000467 umask(0);
468 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000469 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000470 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000471
Rob Landley1ba19d62005-10-08 17:42:35 +0000472 if (ENABLE_DEVFSD_FG_NP && no_polling)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000473 exit(EXIT_SUCCESS);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000474
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000475 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
476 logmode = LOGMODE_BOTH;
477 else if (do_daemon == TRUE)
478 logmode = LOGMODE_SYSLOG;
479 /* This is the default */
480 /*else
481 logmode = LOGMODE_STDIO; */
482
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000483 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000484 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000485 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000486 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000487 } else if (ENABLE_DEVFSD_FG_NP) {
488 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000489 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000490
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000491 while (TRUE) {
492 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000493
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000494 free_config();
495 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000496 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000497 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000498 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000499 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000500} /* End Function main */
501
502
503/* Private functions follow */
504
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000505static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000506/* [SUMMARY] Read a configuration database.
507 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000508 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000509 <optional> If TRUE, the routine will silently ignore a missing config file.
510 <event_mask> The event mask is written here. This is not initialised.
511 [RETURNS] Nothing.
512*/
513{
514 struct stat statbuf;
515 FILE *fp;
516 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000517 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000518 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000519
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000520 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000521 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000522 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000523 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000524 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000525 p = bb_simplify_path(path);
526 dir_operation(READ_CONFIG, p, 0, event_mask);
527 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000528 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000529 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000530 fp = fopen_for_read(path);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000531 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000532 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000533 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000534 line = buf;
535 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000536 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000537 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000538 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000539 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000540 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000541 } else {
542 goto read_config_file_err;
543 }
544 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000545read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000546 if (optional == 0 && errno == ENOENT)
547 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000548 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000549} /* End Function read_config_file */
550
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000551static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000552/* [SUMMARY] Process a line from a configuration file.
553 <line> The configuration line.
554 <event_mask> The event mask is written here. This is not initialised.
555 [RETURNS] Nothing.
556*/
557{
558 int num_args, count;
559 struct config_entry_struct *new;
560 char p[MAX_ARGS][STRING_LENGTH];
561 char when[STRING_LENGTH], what[STRING_LENGTH];
562 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000563 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000564 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000565 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000566
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000567 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000568 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000569 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
570 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
571 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
572 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000573
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000574 for (count = 0; count < MAX_ARGS; ++count)
575 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000576 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000577 when, name, what,
578 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000579
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000580 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000581
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000582 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000583 if (i == 0) {
584 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000585 *event_mask = 0;
586 return;
587 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000588
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000589 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000590 goto process_config_line_err;
591
Eric Andersenf18bd892003-12-19 11:07:59 +0000592 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000593 if (i == 1 || i == 2) {
594 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000595 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000596 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000597 return;
598 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000599 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000600 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000601 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000602 return;
603 }
604 if (num_args < 3)
605 goto process_config_line_err;
606
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000607 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000608
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000609 for (count = 0; event_types[count].config_name != NULL; ++count) {
610 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000611 continue;
612 new->action.when = event_types[count].type;
613 break;
614 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000615 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000616 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000617 goto process_config_line_err;
618 }
619
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000620 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000621
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000622 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000623 case 4: /* "PERMISSIONS" */
624 new->action.what = AC_PERMISSIONS;
625 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000626 ptr = strchr(p[0], '.');
627 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000628 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000629 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000630 }
631
632 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000633 new->u.permissions.uid = get_uid_gid(UID, p[0]);
634 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000635 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000636 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000637 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000638 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000639 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000640 the device name) to the module loading facility. In addition,
641 the /etc/modules.devfs configuration file is used.*/
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100642 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000643 new->action.what = AC_MODLOAD;
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100644 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000645 case 6: /* EXECUTE */
646 new->action.what = AC_EXECUTE;
647 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000648
Eric Andersenf18bd892003-12-19 11:07:59 +0000649 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000650 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000651
Eric Andersenf18bd892003-12-19 11:07:59 +0000652 new->u.execute.argv[num_args] = NULL;
653 break;
654 case 7: /* COPY */
655 new->action.what = AC_COPY;
656 num_args -= 3;
657 if (num_args != 2)
658 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000659
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000660 new->u.copy.source = xstrdup(p[0]);
661 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000662 break;
663 case 8: /* IGNORE */
664 /* FALLTROUGH */
665 case 9: /* MKOLDCOMPAT */
666 /* FALLTROUGH */
667 case 10: /* MKNEWCOMPAT */
668 /* FALLTROUGH */
669 case 11:/* RMOLDCOMPAT */
670 /* FALLTROUGH */
671 case 12: /* RMNEWCOMPAT */
672 /* AC_IGNORE 6
673 AC_MKOLDCOMPAT 7
674 AC_MKNEWCOMPAT 8
675 AC_RMOLDCOMPAT 9
676 AC_RMNEWCOMPAT 10*/
677 new->action.what = i - 2;
678 break;
679 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000680 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000681 goto process_config_line_err;
682 /*esac*/
683 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000684
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000685 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000686
687 *event_mask |= 1 << new->action.when;
688 new->next = NULL;
689 if (first_config == NULL)
690 first_config = new;
691 else
692 last_config->next = new;
693 last_config = new;
694 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000695
696 process_config_line_err:
Denys Vlasenko3d27d432018-12-27 18:03:20 +0100697 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg, line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000698} /* End Function process_config_line */
699
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000700static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000701/* [SUMMARY] Service devfs changes until a signal is received.
702 <fd> The open control file.
703 <event_mask> The event mask.
704 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
705*/
706{
707 ssize_t bytes;
708 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000709
Denis Vlasenko856be772007-08-17 08:29:48 +0000710 /* (void*) cast is only in order to match prototype */
711 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000712 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000713 errno = 0;
Denys Vlasenko083e1722010-01-28 12:30:24 +0100714 bytes = read(fd, (char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000715 if (caught_signal)
716 break; /* Must test for this first */
717 if (errno == EINTR)
718 continue; /* Yes, the order is important */
719 if (bytes < 1)
720 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000721 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000722 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000723 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000724 int c_sighup = caught_sighup;
725
726 caught_signal = FALSE;
727 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000728 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000729 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000730 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000731} /* End Function do_servicing */
732
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000733static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000734/* [SUMMARY] Service a single devfs change.
735 <info> The devfs change.
736 [RETURNS] Nothing.
737*/
738{
739 unsigned int n;
740 regmatch_t mbuf[MAX_SUBEXPR];
741 struct config_entry_struct *entry;
742
Rob Landley1ba19d62005-10-08 17:42:35 +0000743 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000744 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000745
746 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000747 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000748 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000749 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000750 || (info->devname[0] == 'i' && info->devname[1] == 'n'
751 && info->devname[2] == 'i' && info->devname[3] == 't'
752 && info->devname[4] == 'c' && info->devname[5] == 't'
753 && info->devname[6] == 'l' && !info->devname[7]))
754 )
755 return;
756
757 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000758 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000759 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000760 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000761 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000762 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000763 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000764
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000765 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000766 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000767 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000768 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000769 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000770 if (ENABLE_DEVFSD_MODLOAD)
771 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000772 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000773 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000774 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000775 break;
776 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000777 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000778 break;
779 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000780 return;
781 /*break;*/
782 case AC_MKOLDCOMPAT:
783 case AC_MKNEWCOMPAT:
784 case AC_RMOLDCOMPAT:
785 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000786 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000787 break;
788 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000789 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000790 }
791 }
792} /* End Function service_name */
793
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000794static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000795 const struct config_entry_struct *entry)
796/* [SUMMARY] Update permissions for a device entry.
797 <info> The devfs change.
798 <entry> The config file entry.
799 [RETURNS] Nothing.
800*/
801{
802 struct stat statbuf;
803
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000804 if (stat(info->devname, &statbuf) != 0
805 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
806 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000807 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000808 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000809} /* End Function action_permissions */
810
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000811static void action_modload(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100812 const struct config_entry_struct *entry UNUSED_PARAM)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000813/* [SUMMARY] Load a module.
814 <info> The devfs change.
815 <entry> The config file entry.
816 [RETURNS] Nothing.
817*/
818{
819 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000820
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000821 argv[0] = (char*)MODPROBE;
822 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
823 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
824 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000825 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000826 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000827
Denys Vlasenko8531d762010-03-18 22:44:00 +0100828 spawn_and_wait(argv);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000829 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000830} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000831
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000832static void action_execute(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100833 const struct config_entry_struct *entry,
834 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000835/* [SUMMARY] Execute a programme.
836 <info> The devfs change.
837 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000838 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000839 device name.
840 <numexpr> The number of elements within <<regexpr>>.
841 [RETURNS] Nothing.
842*/
843{
844 unsigned int count;
845 struct get_variable_info gv_info;
846 char *argv[MAX_ARGS + 1];
847 char largv[MAX_ARGS + 1][STRING_LENGTH];
848
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000849 gv_info.info = info;
850 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000851 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
852 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
853 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000854 entry->u.execute.argv[count],
855 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000856 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000857 argv[count] = largv[count];
858 }
859 argv[count] = NULL;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100860 spawn_and_wait(argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000861} /* End Function action_execute */
862
863
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000864static void action_copy(const struct devfsd_notify_struct *info,
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100865 const struct config_entry_struct *entry,
866 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000867/* [SUMMARY] Copy permissions.
868 <info> The devfs change.
869 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000870 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000871 device name.
872 <numexpr> The number of elements in <<regexpr>>.
873 [RETURNS] Nothing.
874*/
875{
876 mode_t new_mode;
877 struct get_variable_info gv_info;
878 struct stat source_stat, dest_stat;
879 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000880 int ret = 0;
881
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000882 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000883
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000884 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000885 return;
886 gv_info.info = info;
887 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000888
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000889 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
890 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000891 get_variable, &gv_info, gv_info.devname,
892 regexpr, numexpr);
893
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000894 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000895 get_variable, &gv_info, gv_info.devname,
896 regexpr, numexpr);
897
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000898 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000899 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000900 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000901 new_mode = source_stat.st_mode & ~S_ISVTX;
902 if (info->type == DEVFSD_NOTIFY_CREATE)
903 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000904 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000905 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000906 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000907 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000908 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000909} /* End Function action_copy */
910
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000911static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000912/* [SUMMARY] Process a compatibility request.
913 <info> The devfs change.
914 <action> The action to take.
915 [RETURNS] Nothing.
916*/
917{
Rob Landley1ba19d62005-10-08 17:42:35 +0000918 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000919 const char *compat_name = NULL;
920 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000921 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000922 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000923 int mode, host, bus, target, lun;
924 unsigned int i;
925 char rewind_;
926 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000927 static const char *const fmt[] = {
928 NULL ,
929 "sg/c%db%dt%du%d", /* scsi/generic */
930 "sd/c%db%dt%du%d", /* scsi/disc */
931 "sr/c%db%dt%du%d", /* scsi/cd */
932 "sd/c%db%dt%du%dp%d", /* scsi/part */
933 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
934 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
935 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
936 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
937 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
938 NULL
939 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000940
941 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000942 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000943 case AC_MKOLDCOMPAT:
944 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000945 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000946 break;
947 case AC_MKNEWCOMPAT:
948 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000949 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000950 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000951
952 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000953 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000954 return;
955
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000956 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
957 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000958 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000959 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000960
Eric Andersenf18bd892003-12-19 11:07:59 +0000961
962 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000963 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
964 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000965
966 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000967 if (i == 4 || i == 8)
968 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000969
970 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000971 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000972 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000973 if (rewind_ != 'n')
974 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +0000975 mode=0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000976 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000977 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000978 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000979 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000980 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000981 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000982
Eric Andersenf18bd892003-12-19 11:07:59 +0000983 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000984 if (i == 9)
985 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000986 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000987 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000988
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000989 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000990 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000991
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000992 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000993 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000994 case AC_MKOLDCOMPAT:
995 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000996 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000997 break;
998 case AC_RMOLDCOMPAT:
999 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001000 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001001 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001002 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001003 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001004 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001005 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001006} /* End Function action_compat */
1007
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001008static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001009{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001010 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001011 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001012
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001013 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001014 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001015 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001016 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001017 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001018 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001019
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001020 if (S_ISDIR(source_stat.st_mode))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001021 dir_operation(RESTORE, spath, rootlen, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001022}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001023
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001024
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001025static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001026 mode_t new_mode,
1027 const char *sourcepath, const struct stat *source_stat)
1028/* [SUMMARY] Copy an inode.
1029 <destpath> The destination path. An existing inode may be deleted.
1030 <dest_stat> The destination stat(2) information.
1031 <new_mode> The desired new mode for the destination.
1032 <sourcepath> The source path.
1033 <source_stat> The source stat(2) information.
1034 [RETURNS] TRUE on success, else FALSE.
1035*/
1036{
Eric Andersenf18bd892003-12-19 11:07:59 +00001037 int source_len, dest_len;
1038 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1039 int fd, val;
1040 struct sockaddr_un un_addr;
1041 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001042
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001043 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001044 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001045 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001046 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
1047 if ((source_len < 0)
1048 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001049 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001050 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001051 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001052 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001053 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
1054 unlink(destpath);
1055 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001056 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001057 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001058 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001059 chmod(destpath, new_mode & ~S_IFMT);
1060 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001061 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001062 }
1063 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001064 unlink(destpath);
1065 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001066 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001067 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1068 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001069 break;
1070 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001071 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
Denys Vlasenko083e1722010-01-28 12:30:24 +01001072 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001073 close(fd);
1074 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001075 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001076 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001077 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001078 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1079 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001080 break;
1081 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001082 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001083 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001084 break;
1085 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001086 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1087 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001088 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001089 close(fd);
1090 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001091 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001092 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001093 case S_IFBLK:
1094 case S_IFCHR:
1095 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001096 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001097 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001098 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001099 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001100 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001101 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001102do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001103 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001104 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001105 /*break;*/
1106 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001107 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001108} /* End Function copy_inode */
1109
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001110static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001111/* [SUMMARY] Free the configuration information.
1112 [RETURNS] Nothing.
1113*/
1114{
1115 struct config_entry_struct *c_entry;
1116 void *next;
1117
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001118 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001119 unsigned int count;
1120
1121 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001122 regfree(&c_entry->preg);
1123 if (c_entry->action.what == AC_EXECUTE) {
1124 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001125 if (c_entry->u.execute.argv[count] == NULL)
1126 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001127 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001128 }
1129 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001130 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001131 }
1132 first_config = NULL;
1133 last_config = NULL;
1134} /* End Function free_config */
1135
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001136static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001137/* [SUMMARY] Convert a string to a UID or GID value.
1138 <flag> "UID" or "GID".
1139 <string> The string.
1140 [RETURNS] The UID or GID value.
1141*/
1142{
1143 struct passwd *pw_ent;
1144 struct group *grp_ent;
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001145 const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001146
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001147 if (isdigit(string[0]) || ((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001148 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001149
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001150 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001151 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001152
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001153 if (ENABLE_DEVFSD_VERBOSE)
1154 msg = "user";
1155
1156 if (flag == GID) {
1157 if ((grp_ent = getgrnam(string)) != NULL)
1158 return grp_ent->gr_gid;
1159 if (ENABLE_DEVFSD_VERBOSE)
1160 msg = "group";
1161 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001162
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001163 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001164 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001165 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001166}/* End Function get_uid_gid */
1167
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001168static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001169/* [SUMMARY] Convert a string to a mode value.
1170 <string> The string.
1171 [RETURNS] The mode value.
1172*/
1173{
1174 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001175 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001176
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001177 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001178 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001179 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001180 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001181
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001182 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001183 i = S_IRUSR;
1184 while (i > 0) {
1185 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1186 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001187 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001188 string++;
1189 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001190 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001191} /* End Function get_mode */
1192
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001193static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001194{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195 caught_signal = TRUE;
1196 if (sig == SIGHUP)
1197 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001198
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001199 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001200} /* End Function signal_handler */
1201
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001202static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001203{
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001204 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001205
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001206 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001207 const char *field_names[] = {
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001208 "hostname", "mntpt", "devpath", "devname", "uid", "gid", "mode",
1209 NULL, mount_point, gv_info->devpath, gv_info->devname, NULL
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001210 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001211 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001212
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001213 if (!hostname)
1214 hostname = safe_gethostname();
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001215 field_names[7] = hostname;
1216
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001217 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001218 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001219
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001220 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001221 return NULL;
1222 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001223 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001224
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001225 if (i == 4)
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001226 return auto_string(xasprintf("%u", gv_info->info->uid));
1227 if (i == 5)
1228 return auto_string(xasprintf("%u", gv_info->info->gid));
1229 /* i == 6 */
1230 return auto_string(xasprintf("%o", gv_info->info->mode));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001231} /* End Function get_variable */
1232
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001233static void service(struct stat statbuf, char *path)
1234{
1235 struct devfsd_notify_struct info;
1236
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001237 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001238 info.type = DEVFSD_NOTIFY_REGISTERED;
1239 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001240 info.major = major(statbuf.st_rdev);
1241 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001242 info.uid = statbuf.st_uid;
1243 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001244 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1245 info.namelen = strlen(info.devname);
1246 service_name(&info);
1247 if (S_ISDIR(statbuf.st_mode))
1248 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001249}
1250
1251static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001252/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001253 <flag> To choose which function to perform
1254 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001255 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001256 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001257 [RETURNS] Nothing.
1258*/
1259{
1260 struct stat statbuf;
1261 DIR *dp;
1262 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001263 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001264
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001265 dp = warn_opendir(dir_name);
1266 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001267 return;
1268
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001269 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001270
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001271 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001272 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001273 path = concat_path_file(dir_name, de->d_name);
1274 if (lstat(path, &statbuf) == 0) {
1275 switch (type) {
1276 case SERVICE:
1277 service(statbuf, path);
1278 break;
1279 case RESTORE:
1280 restore(path, statbuf, var);
1281 break;
1282 case READ_CONFIG:
1283 read_config_file(path, var, event_mask);
1284 break;
1285 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001286 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001287 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001288 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001289 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001290} /* End Function do_scan_and_service */
1291
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001292static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001293/* [SUMMARY] Create a symlink, creating intervening directories as required.
1294 <oldpath> The string contained in the symlink.
1295 <newpath> The name of the new symlink.
1296 [RETURNS] 0 on success, else -1.
1297*/
1298{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001299 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001300 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001301
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001302 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001303 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001304 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001305 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001306 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001307} /* End Function mksymlink */
1308
1309
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001310static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001311/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001312 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001313 [RETURNS] TRUE on success, else FALSE.
1314*/
1315{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001316 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001317 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001318 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001319} /* End Function make_dir_tree */
1320
1321static int expand_expression(char *output, unsigned int outsize,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001322 const char *input,
1323 const char *(*get_variable_func)(const char *variable, void *info),
1324 void *info,
1325 const char *devname,
1326 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001327/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001328 <output> The output expanded expression is written here.
1329 <length> The size of the output buffer.
1330 <input> The input expression. This may equal <<output>>.
1331 <get_variable> A function which will be used to get variable values. If
1332 this returns NULL, the environment is searched instead. If this is NULL,
1333 only the environment is searched.
1334 <info> An arbitrary pointer passed to <<get_variable>>.
1335 <devname> Device name; specifically, this is the string that contains all
1336 of the regular subexpressions.
1337 <ex> Array of start / end offsets into info->devname for each subexpression
1338 <numexp> Number of regular subexpressions found in <<devname>>.
1339 [RETURNS] TRUE on success, else FALSE.
1340*/
1341{
1342 char temp[STRING_LENGTH];
1343
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001344 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001345 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001346 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001347 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001348} /* End Function expand_expression */
1349
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001350static void expand_regexp(char *output, size_t outsize, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001351 const char *devname,
1352 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001353/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1354 <output> The output expanded expression is written here.
1355 <outsize> The size of the output buffer.
1356 <input> The input expression. This may NOT equal <<output>>, because
1357 supporting that would require yet another string-copy. However, it's not
1358 hard to write a simple wrapper function to add this functionality for those
1359 few cases that need it.
1360 <devname> Device name; specifically, this is the string that contains all
1361 of the regular subexpressions.
1362 <ex> An array of start and end offsets into <<devname>>, one for each
1363 subexpression
1364 <numex> Number of subexpressions in the offset-array <<ex>>.
1365 [RETURNS] Nothing.
1366*/
1367{
1368 const char last_exp = '0' - 1 + numex;
1369 int c = -1;
1370
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001371 /* Guarantee NULL termination by writing an explicit '\0' character into
1372 the very last byte */
1373 if (outsize)
1374 output[--outsize] = '\0';
1375 /* Copy the input string into the output buffer, replacing '\\' with '\'
1376 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1377 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001378 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001379 c = *input;
1380 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001381 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001382 c = *input;
1383 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001384 if (c != '\\') {
1385 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001386 const regmatch_t *subexp = ex + (c - '0');
1387 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1388
1389 /* Range checking */
1390 if (sublen > outsize)
1391 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001392 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001393 output += sublen;
1394 outsize -= sublen;
1395 }
1396 continue;
1397 }
1398 }
1399 *output = c;
1400 ++output;
1401 --outsize;
1402 } /* while */
1403} /* End Function expand_regexp */
1404
1405
1406/* from compat_name.c */
1407
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +02001408struct translate_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001409 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001410 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001411 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001412};
1413
1414static struct translate_struct translate_table[] =
1415{
1416 {"sound/", NULL},
1417 {"printers/", "lp%s"},
1418 {"v4l/", NULL},
1419 {"parports/", "parport%s"},
1420 {"fb/", "fb%s"},
1421 {"netlink/", NULL},
1422 {"loop/", "loop%s"},
1423 {"floppy/", "fd%s"},
1424 {"rd/", "ram%s"},
1425 {"md/", "md%s"}, /* Meta-devices */
1426 {"vc/", "tty%s"},
1427 {"misc/", NULL},
1428 {"isdn/", NULL},
1429 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1430 {"i2c/", "i2c-%s"},
1431 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1432 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1433 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1434 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1435 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1436 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1437 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1438 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1439 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1440 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1441 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1442 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1443 {"input/js", "js%s"}, /* Joystick driver */
1444 {NULL, NULL}
1445};
1446
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001447const char *get_old_name(const char *devname, unsigned int namelen,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001448 char *buffer, unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001449/* [SUMMARY] Translate a kernel-supplied name into an old name.
1450 <devname> The device name provided by the kernel.
1451 <namelen> The length of the name.
1452 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1453 <major> The major number for the device.
1454 <minor> The minor number for the device.
1455 [RETURNS] A pointer to the old name if known, else NULL.
1456*/
1457{
1458 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001459 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001460 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001461 unsigned int i;
1462 char mode;
1463 int indexx;
1464 const char *pty1;
1465 const char *pty2;
Eric Andersenf18bd892003-12-19 11:07:59 +00001466 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001467 static const char *const fmt[] = {
1468 NULL ,
1469 "sg%u", /* scsi/generic */
1470 NULL, /* scsi/disc */
1471 "sr%u", /* scsi/cd */
1472 NULL, /* scsi/part */
1473 "nst%u%c", /* scsi/mt */
1474 "hd%c" , /* ide/host/disc */
1475 "hd%c" , /* ide/host/cd */
1476 "hd%c%s", /* ide/host/part */
1477 "%sht%d", /* ide/host/mt */
1478 "sbpcd%u", /* sbp/ */
1479 "vcs%s", /* vcc/ */
1480 "%cty%c%c", /* pty/ */
1481 NULL
1482 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001483
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001484 for (trans = translate_table; trans->match != NULL; ++trans) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001485 char *after_match = is_prefixed_with(devname, trans->match);
1486 if (after_match) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001487 if (trans->format == NULL)
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001488 return after_match;
1489 sprintf(buffer, trans->format, after_match);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001490 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001491 }
1492 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001493
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001494 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001495 i = scan_dev_name(devname, namelen, ptr);
1496
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001497 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001498 compat_name = buffer;
1499 else
1500 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001501
Eric Andersenf18bd892003-12-19 11:07:59 +00001502 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001503 if (i == 1 || i == 3 || i == 10)
1504 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001505
1506 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001507 if (i == 2 || i == 4)
Denys Vlasenko083e1722010-01-28 12:30:24 +01001508 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001509
1510 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001511 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001512 mode = ptr[2];
1513 if (mode == 'n')
1514 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001515 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001516 if (devname[namelen - 1] != 'n')
1517 ++compat_name;
1518 }
1519 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001520 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001521 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001522 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001523
1524 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001525 if (i == 9)
1526 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001527
1528 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001529 if (i == 11) {
1530 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001531 if (buffer[3] == '0')
1532 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001533 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001534 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001535 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001536 pty1 = "pqrstuvwxyzabcde";
1537 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001538 indexx = atoi(devname + 5);
1539 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001540 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001541 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001542} /* End Function get_old_name */
1543
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001544static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001545/* [SUMMARY] Get the old IDE name for a device.
1546 <major> The major number for the device.
1547 <minor> The minor number for the device.
1548 [RETURNS] The drive letter.
1549*/
1550{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001551 char letter = 'y'; /* 121 */
1552 char c = 'a'; /* 97 */
1553 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001554
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001555 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1556 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001557 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1558 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1559 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1560 || i == IDE9_MAJOR
1561 ) {
1562 if ((unsigned int)i == major) {
1563 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001564 break;
1565 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001566 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001567 }
1568 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001569 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001570
1571 if (minor > 63)
1572 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001573 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001574} /* End Function get_old_ide_name */
1575
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001576static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001577 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001578 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001579/* [SUMMARY] Write the old SCSI disc name to a buffer.
1580 <buffer> The buffer to write to.
1581 <major> The major number for the device.
1582 <minor> The minor number for the device.
1583 <part> The partition string. Must be "" for a whole-disc entry.
1584 [RETURNS] A pointer to the buffer on success, else NULL.
1585*/
1586{
1587 unsigned int disc_index;
1588
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001589 if (major == 8) {
1590 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001591 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001592 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001593 if ((major > 64) && (major < 72)) {
1594 disc_index = ((major - 64) << 4) +(minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001595 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001596 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001597 else
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001598 sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001599 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001600 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001601 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001602} /* End Function write_old_sd_name */
1603
1604
1605/* expression.c */
1606
1607/*EXPERIMENTAL_FUNCTION*/
1608
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001609int st_expr_expand(char *output, unsigned int length, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001610 const char *(*get_variable_func)(const char *variable,
1611 void *info),
1612 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001613/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1614 <output> The output expanded expression is written here.
1615 <length> The size of the output buffer.
1616 <input> The input expression. This may equal <<output>>.
1617 <get_variable> A function which will be used to get variable values. If
1618 this returns NULL, the environment is searched instead. If this is NULL,
1619 only the environment is searched.
1620 <info> An arbitrary pointer passed to <<get_variable>>.
1621 [RETURNS] TRUE on success, else FALSE.
1622*/
1623{
1624 char ch;
1625 unsigned int len;
1626 unsigned int out_pos = 0;
1627 const char *env;
1628 const char *ptr;
1629 struct passwd *pwent;
1630 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1631
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001632 if (length > BUFFER_SIZE)
1633 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001634 for (; TRUE; ++input) {
1635 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001636 case '$':
1637 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001638 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001639 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001640 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001641 break;
1642 case '~':
1643 /* Home directory expansion */
1644 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001645 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001646 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001647 env = getenv("HOME");
1648 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001649 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001650 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001651 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001652 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001653 if (len + out_pos >= length)
1654 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001655 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001656 out_pos += len;
1657 continue;
1658 }
1659 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001660 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001661 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001662 len = ptr - input;
1663 if (len >= sizeof tmp)
1664 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001665 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001666 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001667 pwent = getpwnam(tmp);
1668 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001669 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001670 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001671 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001672 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001673 if (len + out_pos >= length)
1674 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001675 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001676 out_pos += len;
1677 break;
1678 case '\0':
1679 /* Falltrough */
1680 default:
1681 if (out_pos >= length)
1682 goto st_expr_expand_out;
1683 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001684 if (ch == '\0') {
1685 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001686 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001687 }
1688 break;
1689 /* esac */
1690 }
1691 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001692 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001693st_expr_expand_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001694 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001695 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001696} /* End Function st_expr_expand */
1697
1698
1699/* Private functions follow */
1700
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001701static const char *expand_variable(char *buffer, unsigned int length,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001702 unsigned int *out_pos, const char *input,
1703 const char *(*func)(const char *variable,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001704 void *info),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001705 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001706/* [SUMMARY] Expand a variable.
1707 <buffer> The buffer to write to.
1708 <length> The length of the output buffer.
1709 <out_pos> The current output position. This is updated.
1710 <input> A pointer to the input character pointer.
1711 <func> A function which will be used to get variable values. If this
1712 returns NULL, the environment is searched instead. If this is NULL, only
1713 the environment is searched.
1714 <info> An arbitrary pointer passed to <<func>>.
1715 <errfp> Diagnostic messages are written here.
1716 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1717*/
1718{
1719 char ch;
1720 int len;
1721 unsigned int open_braces;
1722 const char *env, *ptr;
1723 char tmp[STRING_LENGTH];
1724
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001725 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001726 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001727 /* Special case for "$$": PID */
Denys Vlasenko083e1722010-01-28 12:30:24 +01001728 sprintf(tmp, "%d", (int) getpid());
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001729 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001730 if (len + *out_pos >= length)
1731 goto expand_variable_out;
1732
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001733 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001734 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001735 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001736 }
1737 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001738 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001739 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001740 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001741 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001742 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001743 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001744 goto expand_variable_out;
1745
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001746 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001747 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001748 env = get_variable_v2(tmp, func, info);
1749 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001750 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001751 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001752 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001753 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001754 if (len + *out_pos >= length)
1755 goto expand_variable_out;
1756
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001757 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001758 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001759 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001760 }
1761 /* Variable in braces: check for ':' tricks */
1762 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001763 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001764 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001765 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001766 /* Must be simple variable expansion with "${var}" */
1767 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001768 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001769 goto expand_variable_out;
1770
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001771 safe_memcpy(tmp, input, len);
1772 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001773 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001774 return NULL;
1775 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001776 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001777 if (ch != ':' || ptr[1] != '-') {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001778 info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001779 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001780 }
1781 /* It's that handy "${var:-word}" expression. Check if var is defined */
1782 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001783 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001784 goto expand_variable_out;
1785
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001786 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001787 /* Move input pointer to ':' */
1788 input = ptr;
1789 /* First skip to closing brace, taking note of nested expressions */
1790 ptr += 2;
1791 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001792 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1793 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001794 case '{':
1795 ++open_braces;
1796 break;
1797 case '}':
1798 --open_braces;
1799 break;
1800 case '\0':
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001801 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001802 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001803 default:
1804 break;
1805 }
1806 }
1807 --ptr;
1808 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001809 env = get_variable_v2(tmp, func, info);
1810 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001811 /* Found environment variable, so skip the input to the closing brace
1812 and return the variable */
1813 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001814 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001815 if (len + *out_pos >= length)
1816 goto expand_variable_out;
1817
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001818 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001819 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001820 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001821 }
1822 /* Environment variable was not found, so process word. Advance input
1823 pointer to start of word in "${var:-word}" */
1824 input += 2;
1825 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001826 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001827 goto expand_variable_out;
1828
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001829 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001830 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001831 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001832 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001833 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001834 if (len + *out_pos >= length)
1835 goto expand_variable_out;
1836
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001837 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001838 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001839 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001840expand_variable_out:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001841 info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001842 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001843} /* End Function expand_variable */
1844
1845
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001846static const char *get_variable_v2(const char *variable,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001847 const char *(*func)(const char *variable, void *info),
1848 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001849/* [SUMMARY] Get a variable from the environment or .
1850 <variable> The variable name.
1851 <func> A function which will be used to get the variable. If this returns
1852 NULL, the environment is searched instead. If this is NULL, only the
1853 environment is searched.
1854 [RETURNS] The value of the variable on success, else NULL.
1855*/
1856{
1857 const char *value;
1858
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001859 if (func != NULL) {
1860 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001861 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001862 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001863 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001864 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001865} /* End Function get_variable */
1866
1867/* END OF CODE */