blob: 297693f8c66b534c1729cde43762e636802d6b70 [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
Denys Vlasenkofb4da162016-11-22 23:14:24 +010059//config: select FEATURE_SYSLOG
60//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020061//config: This is deprecated and should NOT be used anymore.
62//config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
63//config: See docs/mdev.txt for detailed instructions on how to use mdev
64//config: instead.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010065//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020066//config: Provides compatibility with old device names on a devfs systems.
67//config: You should set it to true if you have devfs enabled.
68//config: The following keywords in devsfd.conf are supported:
69//config: "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
70//config: "PERMISSIONS", "EXECUTE", "COPY", "IGNORE",
71//config: "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT".
Denys Vlasenkofb4da162016-11-22 23:14:24 +010072//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020073//config: But only if they are written UPPERCASE!!!!!!!!
Denys Vlasenkofb4da162016-11-22 23:14:24 +010074//config:
75//config:config DEVFSD_MODLOAD
76//config: bool "Adds support for MODLOAD keyword in devsfd.conf"
77//config: default y
78//config: depends on DEVFSD
79//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020080//config: This actually doesn't work with busybox modutils but needs
81//config: the external modutils.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010082//config:
83//config:config DEVFSD_FG_NP
Denys Vlasenkof5604222017-01-10 14:58:54 +010084//config: bool "Enable the -fg and -np options"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010085//config: default y
86//config: depends on DEVFSD
87//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020088//config: -fg Run the daemon in the foreground.
89//config: -np Exit after parsing config. Do not poll for events.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010090//config:
91//config:config DEVFSD_VERBOSE
92//config: bool "Increases logging (and size)"
93//config: default y
94//config: depends on DEVFSD
95//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020096//config: Increases logging to stderr or syslog.
Denys Vlasenkofb4da162016-11-22 23:14:24 +010097//config:
98//config:config FEATURE_DEVFS
99//config: bool "Use devfs names for all devices (obsolete)"
100//config: default n
Denys Vlasenkofb4da162016-11-22 23:14:24 +0100101//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200102//config: This is obsolete and should NOT be used anymore.
103//config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
Denys Vlasenkofb4da162016-11-22 23:14:24 +0100104//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200105//config: For legacy systems -- if there is no way around devfsd -- this
106//config: tells busybox to look for names like /dev/loop/0 instead of
107//config: /dev/loop0. If your /dev directory has normal names instead of
108//config: devfs names, you don't want this.
Pere Orga5bc8c002011-04-11 03:29:49 +0200109
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +0100110//applet:IF_DEVFSD(APPLET(devfsd, BB_DIR_SBIN, BB_SUID_DROP))
111
112//kbuild:lib-$(CONFIG_DEVFSD) += devfsd.o
113
Pere Orga5bc8c002011-04-11 03:29:49 +0200114//usage:#define devfsd_trivial_usage
115//usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
116//usage:#define devfsd_full_usage "\n\n"
117//usage: "Manage devfs permissions and old device name symlinks\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200118//usage: "\n mntpnt The mount point where devfs is mounted"
119//usage: "\n -v Print the protocol version numbers for devfsd"
120//usage: "\n and the kernel-side protocol version and exit"
121//usage: IF_DEVFSD_FG_NP(
122//usage: "\n -fg Run in foreground"
123//usage: "\n -np Exit after parsing the configuration file"
124//usage: "\n and processing synthetic REGISTER events,"
125//usage: "\n don't poll for events"
126//usage: )
127
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +0000128#include "libbb.h"
129#include "xregex.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000130#include <syslog.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000131
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000132#include <sys/un.h>
133#include <sys/sysmacros.h>
Eric Andersen2af70002003-10-09 21:02:23 +0000134
135/* Various defines taken from linux/major.h */
136#define IDE0_MAJOR 3
137#define IDE1_MAJOR 22
138#define IDE2_MAJOR 33
139#define IDE3_MAJOR 34
140#define IDE4_MAJOR 56
141#define IDE5_MAJOR 57
142#define IDE6_MAJOR 88
143#define IDE7_MAJOR 89
144#define IDE8_MAJOR 90
145#define IDE9_MAJOR 91
146
147
148/* Various defines taken from linux/devfs_fs.h */
149#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200150#define DEVFSD_IOCTL_BASE 'd'
Eric Andersen2af70002003-10-09 21:02:23 +0000151/* These are the various ioctls */
152#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
153#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
154#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +0000155#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +0000156#define DEVFSD_NOTIFY_REGISTERED 0
157#define DEVFSD_NOTIFY_UNREGISTERED 1
158#define DEVFSD_NOTIFY_ASYNC_OPEN 2
159#define DEVFSD_NOTIFY_CLOSE 3
160#define DEVFSD_NOTIFY_LOOKUP 4
161#define DEVFSD_NOTIFY_CHANGE 5
162#define DEVFSD_NOTIFY_CREATE 6
163#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000164#define DEVFS_PATHLEN 1024
165/* Never change this otherwise the binary interface will change */
166
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200167struct devfsd_notify_struct {
168 /* Use native C types to ensure same types in kernel and user space */
Denis Vlasenko2570b2e2008-03-28 01:00:09 +0000169 unsigned int type; /* DEVFSD_NOTIFY_* value */
170 unsigned int mode; /* Mode of the inode or device entry */
171 unsigned int major; /* Major number of device entry */
172 unsigned int minor; /* Minor number of device entry */
173 unsigned int uid; /* Uid of process, inode or device entry */
174 unsigned int gid; /* Gid of process, inode or device entry */
175 unsigned int overrun_count; /* Number of lost events */
176 unsigned int namelen; /* Number of characters not including '\0' */
177 /* The device name MUST come last */
178 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
Eric Andersen2af70002003-10-09 21:02:23 +0000179};
180
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000181#define BUFFER_SIZE 16384
182#define DEVFSD_VERSION "1.3.25"
183#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000184#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000185#define MODPROBE_SWITCH_1 "-k"
186#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000187#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000188#define MAX_ARGS (6 + 1)
189#define MAX_SUBEXPR 10
190#define STRING_LENGTH 255
191
192/* for get_uid_gid() */
193#define UID 0
194#define GID 1
195
Rob Landley1ba19d62005-10-08 17:42:35 +0000196/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000197# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000198# define NO_DIE 0
199
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000200/* for dir_operation() */
201#define RESTORE 0
202#define SERVICE 1
203#define READ_CONFIG 2
204
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000205/* Update only after changing code to reflect new protocol */
206#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
207
208/* Compile-time check */
209#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
210#error protocol version mismatch. Update your kernel headers
211#endif
212
213#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000214#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000215#define AC_EXECUTE 2
216#define AC_MFUNCTION 3 /* not supported by busybox */
217#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000218#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000219#define AC_IGNORE 6
220#define AC_MKOLDCOMPAT 7
221#define AC_MKNEWCOMPAT 8
222#define AC_RMOLDCOMPAT 9
223#define AC_RMNEWCOMPAT 10
224#define AC_RESTORE 11
225
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200226struct permissions_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000227 mode_t mode;
228 uid_t uid;
229 gid_t gid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000230};
231
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200232struct execute_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000233 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000234};
235
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200236struct copy_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000237 const char *source;
238 const char *destination;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000239};
240
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200241struct action_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000242 unsigned int what;
243 unsigned int when;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000244};
245
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200246struct config_entry_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000247 struct action_type action;
248 regex_t preg;
249 union
250 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000251 struct permissions_type permissions;
252 struct execute_type execute;
253 struct copy_type copy;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000254 }
255 u;
256 struct config_entry_struct *next;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000257};
258
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200259struct get_variable_info {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000260 const struct devfsd_notify_struct *info;
261 const char *devname;
262 char devpath[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000263};
264
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000265static void dir_operation(int , const char * , int, unsigned long*);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000266static void service(struct stat statbuf, char *path);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000267static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000268static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000269static int mksymlink(const char *oldpath, const char *newpath);
270static void read_config_file(char *path, int optional, unsigned long *event_mask);
271static void process_config_line(const char *, unsigned long *);
272static int do_servicing(int, unsigned long);
273static void service_name(const struct devfsd_notify_struct *);
274static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
275static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000276 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000277static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
278static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100279 const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000280static void action_compat(const struct devfsd_notify_struct *, unsigned);
281static void free_config(void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000282static void restore(char *spath, struct stat source_stat, int rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000283static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
284static mode_t get_mode(const char *);
285static void signal_handler(int);
286static const char *get_variable(const char *, void *);
287static int make_dir_tree(const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000288static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100289 const char *, const regmatch_t *, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000290static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000291static const char *expand_variable( char *, unsigned, unsigned *, const char *,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000292 const char *(*)(const char *, void *), void *);
293static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100294static char get_old_ide_name(unsigned, unsigned);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000295static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000296
297/* busybox functions */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000298static int get_uid_gid(int flag, const char *string);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000299static void safe_memcpy(char * dest, const char * src, int len);
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000300static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
301static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000302
303/* Structs and vars */
304static struct config_entry_struct *first_config = NULL;
305static struct config_entry_struct *last_config = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000306static char *mount_point = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000307static volatile int caught_signal = FALSE;
308static volatile int caught_sighup = FALSE;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000309static struct initial_symlink_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000310 const char *dest;
311 const char *name;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000312} initial_symlinks[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000313 {"/proc/self/fd", "fd"},
314 {"fd/0", "stdin"},
315 {"fd/1", "stdout"},
316 {"fd/2", "stderr"},
317 {NULL, NULL},
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000318};
319
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000320static struct event_type {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000321 unsigned int type; /* The DEVFSD_NOTIFY_* value */
322 const char *config_name; /* The name used in the config file */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000323} event_types[] = {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000324 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
325 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
326 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
327 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
328 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
329 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
330 {DEVFSD_NOTIFY_CREATE, "CREATE"},
331 {DEVFSD_NOTIFY_DELETE, "DELETE"},
332 {0xffffffff, NULL}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000333};
334
Rob Landley1ba19d62005-10-08 17:42:35 +0000335/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000336
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000337static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
338static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
339static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
340static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000341
Rob Landley1ba19d62005-10-08 17:42:35 +0000342/* Busybox stuff */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000343#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
James Byrne253c4e72019-04-12 17:01:51 +0000344#define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
James Byrne69374872019-07-02 11:35:03 +0200345#define simple_info_logger(p, msg) bb_simple_info_msg(msg)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000346#define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
James Byrne69374872019-07-02 11:35:03 +0200347#define simple_msg_logger(p, msg) bb_simple_error_msg(msg)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000348#define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
James Byrne69374872019-07-02 11:35:03 +0200349#define simple_msg_logger_and_die(p, msg) bb_simple_error_msg_and_die(msg)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000350#define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
351#define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000352#else
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000353#define info_logger(p, fmt, args...)
Denys Vlasenko03ab2122020-06-23 03:43:39 +0200354#define simple_info_logger(p, msg)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000355#define msg_logger(p, fmt, args...)
James Byrne69374872019-07-02 11:35:03 +0200356#define simple_msg_logger(p, msg)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000357#define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
James Byrne69374872019-07-02 11:35:03 +0200358#define simple_msg_logger_and_die(p, msg) exit(EXIT_FAILURE)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000359#define error_logger(p, fmt, args...)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000360#define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
Eric Andersenf18bd892003-12-19 11:07:59 +0000361#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000362
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000363static void safe_memcpy(char *dest, const char *src, int len)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000364{
Denys Vlasenko77a51a22020-12-29 16:53:11 +0100365 memcpy(dest, src, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000366 dest[len] = '\0';
367}
368
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000369static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000370{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000371 if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000372 return 2 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000373 if (d[n - 2] == 'c' && d[n - 1] == 'd')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000374 return 3 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000375 if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000376 return 4 + addendum;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000377 if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000378 return 5 + addendum;
379 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000380}
381
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000382static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
Eric Andersenf18bd892003-12-19 11:07:59 +0000383{
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000384 if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
385 if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
386 && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
387 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000388 return 1;
389 return scan_dev_name_common(d, n, 0, ptr);
390 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000391 if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
392 && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
393 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000394 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000395 if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000396 return 10;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000397 if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000398 return 11;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000399 if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000400 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000401 return 0;
402}
403
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000404/* Public functions follow */
405
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000406int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000407int devfsd_main(int argc, char **argv)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000408{
409 int print_version = FALSE;
410 int do_daemon = TRUE;
411 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000412 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000413 int fd, proto_rev, count;
414 unsigned long event_mask = 0;
415 struct sigaction new_action;
416 struct initial_symlink_struct *curr;
417
418 if (argc < 2)
419 bb_show_usage();
420
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000421 for (count = 2; count < argc; ++count) {
422 if (argv[count][0] == '-') {
423 if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000424 print_version = TRUE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000425 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000426 && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
427 do_daemon = FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000428 else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
Denis Vlasenko679b4122007-07-01 18:18:54 +0000429 && argv[count][2] == 'p' && !argv[count][3]) /* -np */
430 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000431 else
432 bb_show_usage();
433 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000434 }
435
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000436 mount_point = bb_simplify_path(argv[1]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000437
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000438 xchdir(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000439
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000440 fd = xopen(".devfsd", O_RDONLY);
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000441 close_on_exec_on(fd);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000442 xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000443
444 /*setup initial entries */
Denis Vlasenko679b4122007-07-01 18:18:54 +0000445 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000446 symlink(curr->dest, curr->name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000447
448 /* NB: The check for CONFIG_FILE is done in read_config_file() */
449
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000450 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000451 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000452 applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000453 DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000454 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000455 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100456 exit_SUCCESS(); /* -v */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000457 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000458 /* Tell kernel we are special(i.e. we get to see hidden entries) */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000459 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000460
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000461 /* Set up SIGHUP and SIGUSR1 handlers */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000462 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000463 new_action.sa_flags = 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000464 new_action.sa_handler = signal_handler;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000465 sigaction_set(SIGHUP, &new_action);
466 sigaction_set(SIGUSR1, &new_action);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000467
Denis Vlasenko4cf1d082008-03-12 23:13:50 +0000468 printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000469
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000470 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000471 umask(0);
472 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000473 /* Do the scan before forking, so that boot scripts see the finished product */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000474 dir_operation(SERVICE, mount_point, 0, NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000475
Rob Landley1ba19d62005-10-08 17:42:35 +0000476 if (ENABLE_DEVFSD_FG_NP && no_polling)
Denys Vlasenkodb5546c2022-01-05 22:16:06 +0100477 exit_SUCCESS();
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000478
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000479 if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
480 logmode = LOGMODE_BOTH;
481 else if (do_daemon == TRUE)
482 logmode = LOGMODE_SYSLOG;
483 /* This is the default */
484 /*else
485 logmode = LOGMODE_STDIO; */
486
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000487 if (do_daemon) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000488 /* Release so that the child can grab it */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000489 xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000490 bb_daemonize_or_rexec(0, argv);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000491 } else if (ENABLE_DEVFSD_FG_NP) {
492 setpgid(0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000493 }
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000494
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000495 while (TRUE) {
496 do_scan = do_servicing(fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000497
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000498 free_config();
499 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000500 if (do_scan)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000501 dir_operation(SERVICE, mount_point, 0, NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000502 }
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000503 if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000504} /* End Function main */
505
506
507/* Private functions follow */
508
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000509static void read_config_file(char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510/* [SUMMARY] Read a configuration database.
511 <path> The path to read the database from. If this is a directory, all
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000512 entries in that directory will be read(except hidden entries).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000513 <optional> If TRUE, the routine will silently ignore a missing config file.
514 <event_mask> The event mask is written here. This is not initialised.
515 [RETURNS] Nothing.
516*/
517{
518 struct stat statbuf;
519 FILE *fp;
520 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000521 char *line = NULL;
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000522 char *p;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000523
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000524 if (stat(path, &statbuf) == 0) {
Rob Landley06813d02005-06-07 03:47:00 +0000525 /* Don't read 0 length files: ignored */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000526 /*if (statbuf.st_size == 0)
Rob Landley06813d02005-06-07 03:47:00 +0000527 return;*/
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000528 if (S_ISDIR(statbuf.st_mode)) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000529 p = bb_simplify_path(path);
530 dir_operation(READ_CONFIG, p, 0, event_mask);
531 free(p);
Rob Landley06813d02005-06-07 03:47:00 +0000532 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000533 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000534 fp = fopen_for_read(path);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000535 if (fp != NULL) {
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000536 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
Rob Landley06813d02005-06-07 03:47:00 +0000537 /* Skip whitespace */
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000538 line = buf;
539 line = skip_whitespace(line);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000540 if (line[0] == '\0' || line[0] == '#')
Rob Landley06813d02005-06-07 03:47:00 +0000541 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000542 process_config_line(line, event_mask);
Rob Landley06813d02005-06-07 03:47:00 +0000543 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000544 fclose(fp);
Rob Landley06813d02005-06-07 03:47:00 +0000545 } else {
546 goto read_config_file_err;
547 }
548 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000549read_config_file_err:
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000550 if (optional == 0 && errno == ENOENT)
551 error_logger_and_die(LOG_ERR, "read config file: %s", path);
Rob Landley06813d02005-06-07 03:47:00 +0000552 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000553} /* End Function read_config_file */
554
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000555static void process_config_line(const char *line, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000556/* [SUMMARY] Process a line from a configuration file.
557 <line> The configuration line.
558 <event_mask> The event mask is written here. This is not initialised.
559 [RETURNS] Nothing.
560*/
561{
562 int num_args, count;
563 struct config_entry_struct *new;
564 char p[MAX_ARGS][STRING_LENGTH];
565 char when[STRING_LENGTH], what[STRING_LENGTH];
566 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000567 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000568 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000569 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000570
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000571 /* !!!! Only Uppercase Keywords in devsfd.conf */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000572 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000573 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
574 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
575 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
576 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
Rob Landley1ba19d62005-10-08 17:42:35 +0000577
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000578 for (count = 0; count < MAX_ARGS; ++count)
579 p[count][0] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000580 num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000581 when, name, what,
582 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000583
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000584 i = index_in_strings(options, when);
Eric Andersenf18bd892003-12-19 11:07:59 +0000585
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000586 /* "CLEAR_CONFIG" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000587 if (i == 0) {
588 free_config();
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000589 *event_mask = 0;
590 return;
591 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000592
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000593 if (num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000594 goto process_config_line_err;
595
Eric Andersenf18bd892003-12-19 11:07:59 +0000596 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000597 if (i == 1 || i == 2) {
598 st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000599 info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000600 read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000601 return;
602 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000603 /* "RESTORE" */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000604 if (i == 3) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000605 dir_operation(RESTORE, name, strlen(name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000606 return;
607 }
608 if (num_args < 3)
609 goto process_config_line_err;
610
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000611 new = xzalloc(sizeof *new);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000612
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000613 for (count = 0; event_types[count].config_name != NULL; ++count) {
614 if (strcasecmp(when, event_types[count].config_name) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000615 continue;
616 new->action.when = event_types[count].type;
617 break;
618 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000619 if (event_types[count].config_name == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000620 msg = "WHEN in";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000621 goto process_config_line_err;
622 }
623
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000624 i = index_in_strings(options, what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000625
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000626 switch (i) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000627 case 4: /* "PERMISSIONS" */
628 new->action.what = AC_PERMISSIONS;
629 /* Get user and group */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000630 ptr = strchr(p[0], '.');
631 if (ptr == NULL) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000632 msg = "UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000633 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000634 }
635
636 *ptr++ = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000637 new->u.permissions.uid = get_uid_gid(UID, p[0]);
638 new->u.permissions.gid = get_uid_gid(GID, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000639 /* Get mode */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000640 new->u.permissions.mode = get_mode(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000641 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000642 case 5: /* MODLOAD */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000643 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
Eric Andersenf18bd892003-12-19 11:07:59 +0000644 the device name) to the module loading facility. In addition,
645 the /etc/modules.devfs configuration file is used.*/
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100646 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000647 new->action.what = AC_MODLOAD;
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100648 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000649 case 6: /* EXECUTE */
650 new->action.what = AC_EXECUTE;
651 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000652
Eric Andersenf18bd892003-12-19 11:07:59 +0000653 for (count = 0; count < num_args; ++count)
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000654 new->u.execute.argv[count] = xstrdup(p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000655
Eric Andersenf18bd892003-12-19 11:07:59 +0000656 new->u.execute.argv[num_args] = NULL;
657 break;
658 case 7: /* COPY */
659 new->action.what = AC_COPY;
660 num_args -= 3;
661 if (num_args != 2)
662 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000663
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000664 new->u.copy.source = xstrdup(p[0]);
665 new->u.copy.destination = xstrdup(p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000666 break;
667 case 8: /* IGNORE */
668 /* FALLTROUGH */
669 case 9: /* MKOLDCOMPAT */
670 /* FALLTROUGH */
671 case 10: /* MKNEWCOMPAT */
672 /* FALLTROUGH */
673 case 11:/* RMOLDCOMPAT */
674 /* FALLTROUGH */
675 case 12: /* RMNEWCOMPAT */
676 /* AC_IGNORE 6
677 AC_MKOLDCOMPAT 7
678 AC_MKNEWCOMPAT 8
679 AC_RMOLDCOMPAT 9
680 AC_RMNEWCOMPAT 10*/
681 new->action.what = i - 2;
682 break;
683 default:
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000684 msg = "WHAT in";
Eric Andersenf18bd892003-12-19 11:07:59 +0000685 goto process_config_line_err;
686 /*esac*/
687 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000688
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000689 xregcomp(&new->preg, name, REG_EXTENDED);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000690
691 *event_mask |= 1 << new->action.when;
692 new->next = NULL;
693 if (first_config == NULL)
694 first_config = new;
695 else
696 last_config->next = new;
697 last_config = new;
698 return;
Denis Vlasenko856be772007-08-17 08:29:48 +0000699
700 process_config_line_err:
Denys Vlasenko3d27d432018-12-27 18:03:20 +0100701 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg, line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000702} /* End Function process_config_line */
703
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000704static int do_servicing(int fd, unsigned long event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000705/* [SUMMARY] Service devfs changes until a signal is received.
706 <fd> The open control file.
707 <event_mask> The event mask.
708 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
709*/
710{
711 ssize_t bytes;
712 struct devfsd_notify_struct info;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000713
Denis Vlasenko856be772007-08-17 08:29:48 +0000714 /* (void*) cast is only in order to match prototype */
715 xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000716 while (!caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000717 errno = 0;
Denys Vlasenko083e1722010-01-28 12:30:24 +0100718 bytes = read(fd, (char *) &info, sizeof info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000719 if (caught_signal)
720 break; /* Must test for this first */
721 if (errno == EINTR)
722 continue; /* Yes, the order is important */
723 if (bytes < 1)
724 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000725 service_name(&info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000726 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000727 if (caught_signal) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000728 int c_sighup = caught_sighup;
729
730 caught_signal = FALSE;
731 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000732 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000733 }
James Byrne69374872019-07-02 11:35:03 +0200734 simple_msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000735} /* End Function do_servicing */
736
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000737static void service_name(const struct devfsd_notify_struct *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000738/* [SUMMARY] Service a single devfs change.
739 <info> The devfs change.
740 [RETURNS] Nothing.
741*/
742{
743 unsigned int n;
744 regmatch_t mbuf[MAX_SUBEXPR];
745 struct config_entry_struct *entry;
746
Rob Landley1ba19d62005-10-08 17:42:35 +0000747 if (ENABLE_DEBUG && info->overrun_count > 0)
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000748 msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000749
750 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000751 if (info->type == DEVFSD_NOTIFY_LOOKUP
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000752 && ((info->devname[0] == 'l' && info->devname[1] == 'o'
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000753 && info->devname[2] == 'g' && !info->devname[3])
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000754 || (info->devname[0] == 'i' && info->devname[1] == 'n'
755 && info->devname[2] == 'i' && info->devname[3] == 't'
756 && info->devname[4] == 'c' && info->devname[5] == 't'
757 && info->devname[6] == 'l' && !info->devname[7]))
758 )
759 return;
760
761 for (entry = first_config; entry != NULL; entry = entry->next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000762 /* First check if action matches the type, then check if name matches */
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000763 if (info->type != entry->action.when
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000764 || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000765 continue;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000766 for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000767 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000768
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000769 switch (entry->action.what) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000770 case AC_PERMISSIONS:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000771 action_permissions(info, entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000772 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000773 case AC_MODLOAD:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000774 if (ENABLE_DEVFSD_MODLOAD)
775 action_modload(info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000776 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000777 case AC_EXECUTE:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000778 action_execute(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000779 break;
780 case AC_COPY:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000781 action_copy(info, entry, mbuf, n);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000782 break;
783 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000784 return;
785 /*break;*/
786 case AC_MKOLDCOMPAT:
787 case AC_MKNEWCOMPAT:
788 case AC_RMOLDCOMPAT:
789 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000790 action_compat(info, entry->action.what);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000791 break;
792 default:
James Byrne69374872019-07-02 11:35:03 +0200793 simple_msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000794 }
795 }
796} /* End Function service_name */
797
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000798static void action_permissions(const struct devfsd_notify_struct *info,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000799 const struct config_entry_struct *entry)
800/* [SUMMARY] Update permissions for a device entry.
801 <info> The devfs change.
802 <entry> The config file entry.
803 [RETURNS] Nothing.
804*/
805{
806 struct stat statbuf;
807
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000808 if (stat(info->devname, &statbuf) != 0
809 || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
810 || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000811 )
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000812 error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000813} /* End Function action_permissions */
814
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000815static void action_modload(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100816 const struct config_entry_struct *entry UNUSED_PARAM)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000817/* [SUMMARY] Load a module.
818 <info> The devfs change.
819 <entry> The config file entry.
820 [RETURNS] Nothing.
821*/
822{
823 char *argv[6];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000824
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000825 argv[0] = (char*)MODPROBE;
826 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
827 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
828 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000829 argv[4] = concat_path_file("/dev", info->devname); /* device */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000830 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000831
Denys Vlasenko8531d762010-03-18 22:44:00 +0100832 spawn_and_wait(argv);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000833 free(argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000834} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000835
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000836static void action_execute(const struct devfsd_notify_struct *info,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100837 const struct config_entry_struct *entry,
838 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000839/* [SUMMARY] Execute a programme.
840 <info> The devfs change.
841 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000842 <regexpr> The number of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000843 device name.
844 <numexpr> The number of elements within <<regexpr>>.
845 [RETURNS] Nothing.
846*/
847{
848 unsigned int count;
849 struct get_variable_info gv_info;
850 char *argv[MAX_ARGS + 1];
851 char largv[MAX_ARGS + 1][STRING_LENGTH];
852
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000853 gv_info.info = info;
854 gv_info.devname = info->devname;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000855 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
856 for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
857 expand_expression(largv[count], STRING_LENGTH,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000858 entry->u.execute.argv[count],
859 get_variable, &gv_info,
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000860 gv_info.devname, regexpr, numexpr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000861 argv[count] = largv[count];
862 }
863 argv[count] = NULL;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100864 spawn_and_wait(argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000865} /* End Function action_execute */
866
867
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000868static void action_copy(const struct devfsd_notify_struct *info,
Denys Vlasenko6830ade2013-01-15 13:58:01 +0100869 const struct config_entry_struct *entry,
870 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000871/* [SUMMARY] Copy permissions.
872 <info> The devfs change.
873 <entry> The config file entry.
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000874 <regexpr> This list of subexpression(start, end) offsets within the
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000875 device name.
876 <numexpr> The number of elements in <<regexpr>>.
877 [RETURNS] Nothing.
878*/
879{
880 mode_t new_mode;
881 struct get_variable_info gv_info;
882 struct stat source_stat, dest_stat;
883 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000884 int ret = 0;
885
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000886 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000887
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000888 if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000889 return;
890 gv_info.info = info;
891 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000892
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000893 snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
894 expand_expression(source, STRING_LENGTH, entry->u.copy.source,
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 expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000899 get_variable, &gv_info, gv_info.devname,
900 regexpr, numexpr);
901
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000902 if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000903 return;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000904 lstat(destination, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000905 new_mode = source_stat.st_mode & ~S_ISVTX;
906 if (info->type == DEVFSD_NOTIFY_CREATE)
907 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000908 else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000909 new_mode |= S_ISVTX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000910 ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000911 if (ENABLE_DEBUG && ret && (errno != EEXIST))
Denis Vlasenko10aea3e2007-07-01 22:25:33 +0000912 error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000913} /* End Function action_copy */
914
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000915static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000916/* [SUMMARY] Process a compatibility request.
917 <info> The devfs change.
918 <action> The action to take.
919 [RETURNS] Nothing.
920*/
921{
Rob Landley1ba19d62005-10-08 17:42:35 +0000922 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000923 const char *compat_name = NULL;
924 const char *dest_name = info->devname;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000925 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000926 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000927 int mode, host, bus, target, lun;
928 unsigned int i;
929 char rewind_;
930 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Denys Vlasenko987be932022-02-06 20:07:12 +0100931 static const char *const fmt[] ALIGN_PTR = {
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000932 NULL ,
933 "sg/c%db%dt%du%d", /* scsi/generic */
934 "sd/c%db%dt%du%d", /* scsi/disc */
935 "sr/c%db%dt%du%d", /* scsi/cd */
936 "sd/c%db%dt%du%dp%d", /* scsi/part */
937 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
938 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
939 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
940 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
941 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
942 NULL
943 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944
945 /* First construct compatibility name */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000946 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000947 case AC_MKOLDCOMPAT:
948 case AC_RMOLDCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000949 compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950 break;
951 case AC_MKNEWCOMPAT:
952 case AC_RMNEWCOMPAT:
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000953 ptr = bb_basename(info->devname);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000954 i = scan_dev_name(info->devname, info->namelen, ptr);
Eric Andersenf18bd892003-12-19 11:07:59 +0000955
956 /* nothing found */
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000957 if (i == 0 || i > 9)
Eric Andersenf18bd892003-12-19 11:07:59 +0000958 return;
959
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000960 sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
961 snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000962 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +0000963 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000964
Eric Andersenf18bd892003-12-19 11:07:59 +0000965
966 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000967 if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
968 sprintf(compat_buf, fmt[i], host, bus, target, lun);
Eric Andersenf18bd892003-12-19 11:07:59 +0000969
970 /* 4 == scsi/part 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000971 if (i == 4 || i == 8)
972 sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
Eric Andersenf18bd892003-12-19 11:07:59 +0000973
974 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000975 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +0000976 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000977 if (rewind_ != 'n')
978 rewind_ = '\0';
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200979 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000980 if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
Eric Andersenf18bd892003-12-19 11:07:59 +0000981 mode = ptr[2] - 107; /* 1 or 2 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000982 if (ptr[2] == 'a')
Eric Andersenf18bd892003-12-19 11:07:59 +0000983 mode = 3;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +0000984 sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000985 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000986
Eric Andersenf18bd892003-12-19 11:07:59 +0000987 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000988 if (i == 9)
989 snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
Eric Andersenf18bd892003-12-19 11:07:59 +0000990 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000991 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +0000992
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000993 if (compat_name == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000994 return;
Eric Andersenf18bd892003-12-19 11:07:59 +0000995
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000996 /* Now decide what to do with it */
Denis Vlasenko1fc62382007-06-25 22:55:34 +0000997 switch (action) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000998 case AC_MKOLDCOMPAT:
999 case AC_MKNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001000 mksymlink(dest_name, compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001001 break;
1002 case AC_RMOLDCOMPAT:
1003 case AC_RMNEWCOMPAT:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001004 ret = unlink(compat_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001005 if (ENABLE_DEBUG && ret)
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001006 error_logger(LOG_ERR, "unlink: %s", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001008 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001009 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001010} /* End Function action_compat */
1011
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001012static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001013{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001014 char *dpath;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001015 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001016
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001017 dest_stat.st_mode = 0;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001018 dpath = concat_path_file(mount_point, spath + rootlen);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001019 lstat(dpath, &dest_stat);
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001020 free(dpath);
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001021 if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001022 copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001023
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001024 if (S_ISDIR(source_stat.st_mode))
Denys Vlasenko083e1722010-01-28 12:30:24 +01001025 dir_operation(RESTORE, spath, rootlen, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001026}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001027
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001028
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001029static int copy_inode(const char *destpath, const struct stat *dest_stat,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001030 mode_t new_mode,
1031 const char *sourcepath, const struct stat *source_stat)
1032/* [SUMMARY] Copy an inode.
1033 <destpath> The destination path. An existing inode may be deleted.
1034 <dest_stat> The destination stat(2) information.
1035 <new_mode> The desired new mode for the destination.
1036 <sourcepath> The source path.
1037 <source_stat> The source stat(2) information.
1038 [RETURNS] TRUE on success, else FALSE.
1039*/
1040{
Eric Andersenf18bd892003-12-19 11:07:59 +00001041 int source_len, dest_len;
1042 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1043 int fd, val;
1044 struct sockaddr_un un_addr;
1045 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001046
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001047 if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001048 /* Same type */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001049 if (S_ISLNK(source_stat->st_mode)) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001050 source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
1051 if ((source_len < 0)
1052 || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001053 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001054 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001055 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001056 dest_link[dest_len] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001057 if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
1058 unlink(destpath);
1059 symlink(source_link, destpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001060 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001061 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001062 } /* Else not a symlink */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001063 chmod(destpath, new_mode & ~S_IFMT);
1064 chown(destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001065 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001066 }
1067 /* Different types: unlink and create */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001068 unlink(destpath);
1069 switch (source_stat->st_mode & S_IFMT) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001070 case S_IFSOCK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001071 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1072 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001073 break;
1074 un_addr.sun_family = AF_UNIX;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001075 snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
Denys Vlasenko083e1722010-01-28 12:30:24 +01001076 val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001077 close(fd);
1078 if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001079 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001080 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001081 case S_IFLNK:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001082 val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
1083 if (val < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001084 break;
1085 symlink_val[val] = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001086 if (symlink(symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001087 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001088 break;
1089 case S_IFREG:
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001090 fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
1091 if (fd < 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001092 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001093 close(fd);
1094 if (chmod(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001095 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001096 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001097 case S_IFBLK:
1098 case S_IFCHR:
1099 case S_IFIFO:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001100 if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001101 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001102 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001103 case S_IFDIR:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001104 if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001105 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001106do_chown:
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001107 if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001108 return TRUE;
Denys Vlasenko77a51a22020-12-29 16:53:11 +01001109 /*break;*/
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001110 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001111 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001112} /* End Function copy_inode */
1113
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001114static void free_config(void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001115/* [SUMMARY] Free the configuration information.
1116 [RETURNS] Nothing.
1117*/
1118{
1119 struct config_entry_struct *c_entry;
1120 void *next;
1121
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001122 for (c_entry = first_config; c_entry != NULL; c_entry = next) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001123 unsigned int count;
1124
1125 next = c_entry->next;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001126 regfree(&c_entry->preg);
1127 if (c_entry->action.what == AC_EXECUTE) {
1128 for (count = 0; count < MAX_ARGS; ++count) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001129 if (c_entry->u.execute.argv[count] == NULL)
1130 break;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001131 free(c_entry->u.execute.argv[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001132 }
1133 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001134 free(c_entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001135 }
1136 first_config = NULL;
1137 last_config = NULL;
1138} /* End Function free_config */
1139
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001140static int get_uid_gid(int flag, const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001141/* [SUMMARY] Convert a string to a UID or GID value.
1142 <flag> "UID" or "GID".
1143 <string> The string.
1144 [RETURNS] The UID or GID value.
1145*/
1146{
1147 struct passwd *pw_ent;
1148 struct group *grp_ent;
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001149 const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001150
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001151 if (isdigit(string[0]) || ((string[0] == '-') && isdigit(string[1])))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001152 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001153
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001154 if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001155 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001156
Denys Vlasenko4d8ad382013-11-26 12:12:27 +01001157 if (ENABLE_DEVFSD_VERBOSE)
1158 msg = "user";
1159
1160 if (flag == GID) {
1161 if ((grp_ent = getgrnam(string)) != NULL)
1162 return grp_ent->gr_gid;
1163 if (ENABLE_DEVFSD_VERBOSE)
1164 msg = "group";
1165 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001166
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001167 if (ENABLE_DEVFSD_VERBOSE)
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001168 msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001169 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001170}/* End Function get_uid_gid */
1171
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001172static mode_t get_mode(const char *string)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001173/* [SUMMARY] Convert a string to a mode value.
1174 <string> The string.
1175 [RETURNS] The mode value.
1176*/
1177{
1178 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001179 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001180
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001181 if (isdigit(string[0]))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001182 return strtoul(string, NULL, 8);
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001183 if (strlen(string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001184 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001185
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001186 mode = 0;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001187 i = S_IRUSR;
1188 while (i > 0) {
1189 if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
1190 mode += i;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001191 i = i / 2;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001192 string++;
1193 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001194 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195} /* End Function get_mode */
1196
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001197static void signal_handler(int sig)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001198{
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001199 caught_signal = TRUE;
1200 if (sig == SIGHUP)
1201 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001202
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001203 info_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001204} /* End Function signal_handler */
1205
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001206static const char *get_variable(const char *variable, void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001207{
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001208 static char *hostname;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001209
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001210 struct get_variable_info *gv_info = info;
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001211 const char *field_names[] = {
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001212 "hostname", "mntpt", "devpath", "devname", "uid", "gid", "mode",
1213 NULL, mount_point, gv_info->devpath, gv_info->devname, NULL
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001214 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001215 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001216
Denis Vlasenko6f1713f2008-02-25 23:23:58 +00001217 if (!hostname)
1218 hostname = safe_gethostname();
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001219 field_names[7] = hostname;
1220
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001221 /* index_in_str_array returns i>=0 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001222 i = index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001223
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001224 if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001225 return NULL;
1226 if (i >= 0 && i <= 3)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001227 return field_names[i + 7];
Eric Andersenf18bd892003-12-19 11:07:59 +00001228
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001229 if (i == 4)
Denys Vlasenko02859aa2015-10-09 18:16:40 +02001230 return auto_string(xasprintf("%u", gv_info->info->uid));
1231 if (i == 5)
1232 return auto_string(xasprintf("%u", gv_info->info->gid));
1233 /* i == 6 */
1234 return auto_string(xasprintf("%o", gv_info->info->mode));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001235} /* End Function get_variable */
1236
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001237static void service(struct stat statbuf, char *path)
1238{
1239 struct devfsd_notify_struct info;
1240
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001241 memset(&info, 0, sizeof info);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001242 info.type = DEVFSD_NOTIFY_REGISTERED;
1243 info.mode = statbuf.st_mode;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001244 info.major = major(statbuf.st_rdev);
1245 info.minor = minor(statbuf.st_rdev);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001246 info.uid = statbuf.st_uid;
1247 info.gid = statbuf.st_gid;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001248 snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
1249 info.namelen = strlen(info.devname);
1250 service_name(&info);
1251 if (S_ISDIR(statbuf.st_mode))
1252 dir_operation(SERVICE, path, 0, NULL);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001253}
1254
1255static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001256/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001257 <flag> To choose which function to perform
1258 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001259 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001260 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001261 [RETURNS] Nothing.
1262*/
1263{
1264 struct stat statbuf;
1265 DIR *dp;
1266 struct dirent *de;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001267 char *path;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001268
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001269 dp = warn_opendir(dir_name);
1270 if (dp == NULL)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001271 return;
1272
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001273 while ((de = readdir(dp)) != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001274
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001275 if (de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001276 continue;
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001277 path = concat_path_file(dir_name, de->d_name);
1278 if (lstat(path, &statbuf) == 0) {
1279 switch (type) {
1280 case SERVICE:
1281 service(statbuf, path);
1282 break;
1283 case RESTORE:
1284 restore(path, statbuf, var);
1285 break;
1286 case READ_CONFIG:
1287 read_config_file(path, var, event_mask);
1288 break;
1289 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001290 }
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001291 free(path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001292 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001293 closedir(dp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001294} /* End Function do_scan_and_service */
1295
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001296static int mksymlink(const char *oldpath, const char *newpath)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001297/* [SUMMARY] Create a symlink, creating intervening directories as required.
1298 <oldpath> The string contained in the symlink.
1299 <newpath> The name of the new symlink.
1300 [RETURNS] 0 on success, else -1.
1301*/
1302{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001303 if (!make_dir_tree(newpath))
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001304 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001305
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001306 if (symlink(oldpath, newpath) != 0) {
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001307 if (errno != EEXIST)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001308 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001309 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001310 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001311} /* End Function mksymlink */
1312
1313
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001314static int make_dir_tree(const char *path)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001315/* [SUMMARY] Creating intervening directories for a path as required.
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001316 <path> The full pathname(including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001317 [RETURNS] TRUE on success, else FALSE.
1318*/
1319{
Denis Vlasenkoc965f4b2007-06-27 00:20:38 +00001320 if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001321 return FALSE;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001322 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001323} /* End Function make_dir_tree */
1324
1325static int expand_expression(char *output, unsigned int outsize,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001326 const char *input,
1327 const char *(*get_variable_func)(const char *variable, void *info),
1328 void *info,
1329 const char *devname,
1330 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001331/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001332 <output> The output expanded expression is written here.
1333 <length> The size of the output buffer.
1334 <input> The input expression. This may equal <<output>>.
1335 <get_variable> A function which will be used to get variable values. If
1336 this returns NULL, the environment is searched instead. If this is NULL,
1337 only the environment is searched.
1338 <info> An arbitrary pointer passed to <<get_variable>>.
1339 <devname> Device name; specifically, this is the string that contains all
1340 of the regular subexpressions.
1341 <ex> Array of start / end offsets into info->devname for each subexpression
1342 <numexp> Number of regular subexpressions found in <<devname>>.
1343 [RETURNS] TRUE on success, else FALSE.
1344*/
1345{
1346 char temp[STRING_LENGTH];
1347
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001348 if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001349 return FALSE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001350 expand_regexp(output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001351 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001352} /* End Function expand_expression */
1353
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001354static void expand_regexp(char *output, size_t outsize, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001355 const char *devname,
1356 const regmatch_t *ex, unsigned int numex)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001357/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1358 <output> The output expanded expression is written here.
1359 <outsize> The size of the output buffer.
1360 <input> The input expression. This may NOT equal <<output>>, because
1361 supporting that would require yet another string-copy. However, it's not
1362 hard to write a simple wrapper function to add this functionality for those
1363 few cases that need it.
1364 <devname> Device name; specifically, this is the string that contains all
1365 of the regular subexpressions.
1366 <ex> An array of start and end offsets into <<devname>>, one for each
1367 subexpression
1368 <numex> Number of subexpressions in the offset-array <<ex>>.
1369 [RETURNS] Nothing.
1370*/
1371{
1372 const char last_exp = '0' - 1 + numex;
1373 int c = -1;
1374
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001375 /* Guarantee NULL termination by writing an explicit '\0' character into
1376 the very last byte */
1377 if (outsize)
1378 output[--outsize] = '\0';
1379 /* Copy the input string into the output buffer, replacing '\\' with '\'
1380 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1381 codes are deleted */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001382 while ((c != '\0') && (outsize != 0)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001383 c = *input;
1384 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001385 if (c == '\\') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001386 c = *input;
1387 ++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001388 if (c != '\\') {
1389 if ((c >= '0') && (c <= last_exp)) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001390 const regmatch_t *subexp = ex + (c - '0');
1391 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1392
1393 /* Range checking */
1394 if (sublen > outsize)
1395 sublen = outsize;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001396 strncpy(output, devname + subexp->rm_so, sublen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001397 output += sublen;
1398 outsize -= sublen;
1399 }
1400 continue;
1401 }
1402 }
1403 *output = c;
1404 ++output;
1405 --outsize;
1406 } /* while */
1407} /* End Function expand_regexp */
1408
1409
1410/* from compat_name.c */
1411
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +02001412struct translate_struct {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001413 const char *match; /* The string to match to(up to length) */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001414 const char *format; /* Format of output, "%s" takes data past match string,
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001415 NULL is effectively "%s"(just more efficient) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001416};
1417
1418static struct translate_struct translate_table[] =
1419{
1420 {"sound/", NULL},
1421 {"printers/", "lp%s"},
1422 {"v4l/", NULL},
1423 {"parports/", "parport%s"},
1424 {"fb/", "fb%s"},
1425 {"netlink/", NULL},
1426 {"loop/", "loop%s"},
1427 {"floppy/", "fd%s"},
1428 {"rd/", "ram%s"},
1429 {"md/", "md%s"}, /* Meta-devices */
1430 {"vc/", "tty%s"},
1431 {"misc/", NULL},
1432 {"isdn/", NULL},
1433 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1434 {"i2c/", "i2c-%s"},
1435 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1436 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1437 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1438 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1439 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1440 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1441 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1442 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1443 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1444 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1445 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1446 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1447 {"input/js", "js%s"}, /* Joystick driver */
1448 {NULL, NULL}
1449};
1450
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001451const char *get_old_name(const char *devname, unsigned int namelen,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001452 char *buffer, unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001453/* [SUMMARY] Translate a kernel-supplied name into an old name.
1454 <devname> The device name provided by the kernel.
1455 <namelen> The length of the name.
1456 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1457 <major> The major number for the device.
1458 <minor> The minor number for the device.
1459 [RETURNS] A pointer to the old name if known, else NULL.
1460*/
1461{
1462 const char *compat_name = NULL;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001463 const char *ptr;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001464 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001465 unsigned int i;
1466 char mode;
1467 int indexx;
1468 const char *pty1;
1469 const char *pty2;
Eric Andersenf18bd892003-12-19 11:07:59 +00001470 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Denys Vlasenko987be932022-02-06 20:07:12 +01001471 static const char *const fmt[] ALIGN_PTR = {
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001472 NULL ,
1473 "sg%u", /* scsi/generic */
1474 NULL, /* scsi/disc */
1475 "sr%u", /* scsi/cd */
1476 NULL, /* scsi/part */
1477 "nst%u%c", /* scsi/mt */
1478 "hd%c" , /* ide/host/disc */
1479 "hd%c" , /* ide/host/cd */
1480 "hd%c%s", /* ide/host/part */
1481 "%sht%d", /* ide/host/mt */
1482 "sbpcd%u", /* sbp/ */
1483 "vcs%s", /* vcc/ */
1484 "%cty%c%c", /* pty/ */
1485 NULL
1486 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001487
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001488 for (trans = translate_table; trans->match != NULL; ++trans) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001489 char *after_match = is_prefixed_with(devname, trans->match);
1490 if (after_match) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001491 if (trans->format == NULL)
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001492 return after_match;
1493 sprintf(buffer, trans->format, after_match);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001494 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001495 }
1496 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001497
Denis Vlasenkodc757aa2007-06-30 08:04:05 +00001498 ptr = bb_basename(devname);
Eric Andersenf18bd892003-12-19 11:07:59 +00001499 i = scan_dev_name(devname, namelen, ptr);
1500
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001501 if (i > 0 && i < 13)
Eric Andersenf18bd892003-12-19 11:07:59 +00001502 compat_name = buffer;
1503 else
1504 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001505
Eric Andersenf18bd892003-12-19 11:07:59 +00001506 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001507 if (i == 1 || i == 3 || i == 10)
1508 sprintf(buffer, fmt[i], minor);
Eric Andersenf18bd892003-12-19 11:07:59 +00001509
1510 /* 2 ==scsi/disc, 4 == scsi/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001511 if (i == 2 || i == 4)
Denys Vlasenko083e1722010-01-28 12:30:24 +01001512 compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
Eric Andersenf18bd892003-12-19 11:07:59 +00001513
1514 /* 5 == scsi/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001515 if (i == 5) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001516 mode = ptr[2];
1517 if (mode == 'n')
1518 mode = '\0';
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001519 sprintf(buffer, fmt[i], minor & 0x1f, mode);
Eric Andersenf18bd892003-12-19 11:07:59 +00001520 if (devname[namelen - 1] != 'n')
1521 ++compat_name;
1522 }
1523 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001524 if (i == 6 || i == 7 || i == 8)
Rob Landley1ba19d62005-10-08 17:42:35 +00001525 /* last arg should be ignored for i == 6 or i== 7 */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001526 sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001527
1528 /* 9 == ide/host/mt */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001529 if (i == 9)
1530 sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
Eric Andersenf18bd892003-12-19 11:07:59 +00001531
1532 /* 11 == vcc/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001533 if (i == 11) {
1534 sprintf(buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001535 if (buffer[3] == '0')
1536 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001537 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001538 /* 12 == pty/ */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001539 if (i == 12) {
Eric Andersenf18bd892003-12-19 11:07:59 +00001540 pty1 = "pqrstuvwxyzabcde";
1541 pty2 = "0123456789abcdef";
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001542 indexx = atoi(devname + 5);
1543 sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001544 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001545 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001546} /* End Function get_old_name */
1547
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001548static char get_old_ide_name(unsigned int major, unsigned int minor)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001549/* [SUMMARY] Get the old IDE name for a device.
1550 <major> The major number for the device.
1551 <minor> The minor number for the device.
1552 [RETURNS] The drive letter.
1553*/
1554{
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001555 char letter = 'y'; /* 121 */
1556 char c = 'a'; /* 97 */
1557 int i = IDE0_MAJOR;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001558
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001559 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1560 do {
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001561 if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
1562 || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
1563 || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
1564 || i == IDE9_MAJOR
1565 ) {
1566 if ((unsigned int)i == major) {
1567 letter = c;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001568 break;
1569 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001570 c += 2;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001571 }
1572 i++;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001573 } while (i <= IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001574
1575 if (minor > 63)
1576 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001577 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001578} /* End Function get_old_ide_name */
1579
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001580static char *write_old_sd_name(char *buffer,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001581 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001582 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001583/* [SUMMARY] Write the old SCSI disc name to a buffer.
1584 <buffer> The buffer to write to.
1585 <major> The major number for the device.
1586 <minor> The minor number for the device.
1587 <part> The partition string. Must be "" for a whole-disc entry.
1588 [RETURNS] A pointer to the buffer on success, else NULL.
1589*/
1590{
1591 unsigned int disc_index;
1592
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001593 if (major == 8) {
1594 sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001595 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001596 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001597 if ((major > 64) && (major < 72)) {
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +02001598 disc_index = ((major - 64) << 4) + (minor >> 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001599 if (disc_index < 26)
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001600 sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001601 else
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +02001602 sprintf(buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26, part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001603 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001604 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001605 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001606} /* End Function write_old_sd_name */
1607
1608
1609/* expression.c */
1610
1611/*EXPERIMENTAL_FUNCTION*/
1612
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001613int st_expr_expand(char *output, unsigned int length, const char *input,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001614 const char *(*get_variable_func)(const char *variable,
1615 void *info),
1616 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001617/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1618 <output> The output expanded expression is written here.
1619 <length> The size of the output buffer.
1620 <input> The input expression. This may equal <<output>>.
1621 <get_variable> A function which will be used to get variable values. If
1622 this returns NULL, the environment is searched instead. If this is NULL,
1623 only the environment is searched.
1624 <info> An arbitrary pointer passed to <<get_variable>>.
1625 [RETURNS] TRUE on success, else FALSE.
1626*/
1627{
1628 char ch;
1629 unsigned int len;
1630 unsigned int out_pos = 0;
1631 const char *env;
1632 const char *ptr;
1633 struct passwd *pwent;
1634 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1635
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001636 if (length > BUFFER_SIZE)
1637 length = BUFFER_SIZE;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001638 for (; TRUE; ++input) {
1639 switch (ch = *input) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001640 case '$':
1641 /* Variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001642 input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001643 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001644 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001645 break;
1646 case '~':
1647 /* Home directory expansion */
1648 ch = input[1];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001649 if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001650 /* User's own home directory: leave separator for next time */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001651 env = getenv("HOME");
1652 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001653 info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001654 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001655 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001656 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001657 if (len + out_pos >= length)
1658 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001659 memcpy(buffer + out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001660 out_pos += len;
1661 continue;
1662 }
1663 /* Someone else's home directory */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001664 for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001665 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001666 len = ptr - input;
1667 if (len >= sizeof tmp)
1668 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001669 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001670 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001671 pwent = getpwnam(tmp);
1672 if (pwent == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001673 info_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001674 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001675 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001676 len = strlen(pwent->pw_dir);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001677 if (len + out_pos >= length)
1678 goto st_expr_expand_out;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001679 memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001680 out_pos += len;
1681 break;
1682 case '\0':
1683 /* Falltrough */
1684 default:
1685 if (out_pos >= length)
1686 goto st_expr_expand_out;
1687 buffer[out_pos++] = ch;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001688 if (ch == '\0') {
1689 memcpy(output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001690 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001691 }
1692 break;
1693 /* esac */
1694 }
1695 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001696 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001697st_expr_expand_out:
James Byrne69374872019-07-02 11:35:03 +02001698 simple_info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001699 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001700} /* End Function st_expr_expand */
1701
1702
1703/* Private functions follow */
1704
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001705static const char *expand_variable(char *buffer, unsigned int length,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001706 unsigned int *out_pos, const char *input,
1707 const char *(*func)(const char *variable,
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001708 void *info),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001709 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001710/* [SUMMARY] Expand a variable.
1711 <buffer> The buffer to write to.
1712 <length> The length of the output buffer.
1713 <out_pos> The current output position. This is updated.
1714 <input> A pointer to the input character pointer.
1715 <func> A function which will be used to get variable values. If this
1716 returns NULL, the environment is searched instead. If this is NULL, only
1717 the environment is searched.
1718 <info> An arbitrary pointer passed to <<func>>.
1719 <errfp> Diagnostic messages are written here.
1720 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1721*/
1722{
1723 char ch;
1724 int len;
1725 unsigned int open_braces;
1726 const char *env, *ptr;
1727 char tmp[STRING_LENGTH];
1728
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001729 ch = input[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001730 if (ch == '$') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001731 /* Special case for "$$": PID */
Denys Vlasenko083e1722010-01-28 12:30:24 +01001732 sprintf(tmp, "%d", (int) getpid());
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001733 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001734 if (len + *out_pos >= length)
1735 goto expand_variable_out;
1736
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001737 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001738 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001739 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001740 }
1741 /* Ordinary variable expansion, possibly in braces */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001742 if (ch != '{') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001743 /* Simple variable expansion */
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001744 for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
Denis Vlasenkob71c6682007-07-21 15:08:09 +00001745 /* VOID */;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001746 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001747 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001748 goto expand_variable_out;
1749
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001750 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001751 input = ptr - 1;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001752 env = get_variable_v2(tmp, func, info);
1753 if (env == NULL) {
Denis Vlasenko10aea3e2007-07-01 22:25:33 +00001754 info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001755 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001756 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001757 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001758 if (len + *out_pos >= length)
1759 goto expand_variable_out;
1760
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001761 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001762 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001763 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001764 }
1765 /* Variable in braces: check for ':' tricks */
1766 ch = *++input;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001767 for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001768 /* VOID */;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001769 if (ch == '}') {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001770 /* Must be simple variable expansion with "${var}" */
1771 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001772 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001773 goto expand_variable_out;
1774
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001775 safe_memcpy(tmp, input, len);
1776 ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001777 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001778 return NULL;
1779 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001780 }
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001781 if (ch != ':' || ptr[1] != '-') {
James Byrne69374872019-07-02 11:35:03 +02001782 simple_info_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001783 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001784 }
1785 /* It's that handy "${var:-word}" expression. Check if var is defined */
1786 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001787 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001788 goto expand_variable_out;
1789
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001790 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001791 /* Move input pointer to ':' */
1792 input = ptr;
1793 /* First skip to closing brace, taking note of nested expressions */
1794 ptr += 2;
1795 ch = ptr[0];
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001796 for (open_braces = 1; open_braces > 0; ch = *++ptr) {
1797 switch (ch) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001798 case '{':
1799 ++open_braces;
1800 break;
1801 case '}':
1802 --open_braces;
1803 break;
1804 case '\0':
Denis Vlasenkof5d8c902008-06-26 14:32:57 +00001805 info_logger(LOG_INFO, "\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001806 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001807 default:
1808 break;
1809 }
1810 }
1811 --ptr;
1812 /* At this point ptr should point to closing brace of "${var:-word}" */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001813 env = get_variable_v2(tmp, func, info);
1814 if (env != NULL) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001815 /* Found environment variable, so skip the input to the closing brace
1816 and return the variable */
1817 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001818 len = strlen(env);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001819 if (len + *out_pos >= length)
1820 goto expand_variable_out;
1821
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001822 memcpy(buffer + *out_pos, env, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001823 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001824 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001825 }
1826 /* Environment variable was not found, so process word. Advance input
1827 pointer to start of word in "${var:-word}" */
1828 input += 2;
1829 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001830 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001831 goto expand_variable_out;
1832
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001833 safe_memcpy(tmp, input, len);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001834 input = ptr;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001835 if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001836 return NULL;
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001837 len = strlen(tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001838 if (len + *out_pos >= length)
1839 goto expand_variable_out;
1840
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001841 memcpy(buffer + *out_pos, tmp, len + 1);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001842 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001843 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001844expand_variable_out:
James Byrne69374872019-07-02 11:35:03 +02001845 simple_info_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001846 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001847} /* End Function expand_variable */
1848
1849
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001850static const char *get_variable_v2(const char *variable,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001851 const char *(*func)(const char *variable, void *info),
1852 void *info)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001853/* [SUMMARY] Get a variable from the environment or .
1854 <variable> The variable name.
1855 <func> A function which will be used to get the variable. If this returns
1856 NULL, the environment is searched instead. If this is NULL, only the
1857 environment is searched.
1858 [RETURNS] The value of the variable on success, else NULL.
1859*/
1860{
1861 const char *value;
1862
Denis Vlasenko1fc62382007-06-25 22:55:34 +00001863 if (func != NULL) {
1864 value = (*func)(variable, info);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001865 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001866 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001867 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001868 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001869} /* End Function get_variable */
1870
1871/* END OF CODE */