blob: ad934a3a487410d53cf12708a9ca3441c7b18b8e [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002/*
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6/*
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00007 devfsd implementation for busybox
8
9 Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
10
11 Busybox version is based on some previous work and ideas
12 Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
13
14 devfsd.c
15
16 Main file for devfsd (devfs daemon for Linux).
17
18 Copyright (C) 1998-2002 Richard Gooch
19
20 devfsd.h
21
22 Header file for devfsd (devfs daemon for Linux).
23
24 Copyright (C) 1998-2000 Richard Gooch
25
26 compat_name.c
27
28 Compatibility name file for devfsd (build compatibility names).
29
30 Copyright (C) 1998-2002 Richard Gooch
31
32 expression.c
33
34 This code provides Borne Shell-like expression expansion.
35
36 Copyright (C) 1997-1999 Richard Gooch
37
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2 of the License, or
41 (at your option) any later version.
42
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
47
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
51
52 Richard Gooch may be reached by email at rgooch@atnf.csiro.au
53 The postal address is:
54 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
55*/
56
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000057//#include <sys/wait.h>
58//#include <sys/ioctl.h>
59//#include <sys/socket.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000060#include <sys/un.h>
61#include <dirent.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000062#include <syslog.h>
Eric Andersen2af70002003-10-09 21:02:23 +000063#include <sys/sysmacros.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000064#include "libbb.h"
65#include "xregex.h"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000066
Eric Andersen2af70002003-10-09 21:02:23 +000067
68/* Various defines taken from linux/major.h */
69#define IDE0_MAJOR 3
70#define IDE1_MAJOR 22
71#define IDE2_MAJOR 33
72#define IDE3_MAJOR 34
73#define IDE4_MAJOR 56
74#define IDE5_MAJOR 57
75#define IDE6_MAJOR 88
76#define IDE7_MAJOR 89
77#define IDE8_MAJOR 90
78#define IDE9_MAJOR 91
79
80
81/* Various defines taken from linux/devfs_fs.h */
82#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
83#define DEVFSD_IOCTL_BASE 'd'
84/* These are the various ioctls */
85#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
86#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
87#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000088#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000089#define DEVFSD_NOTIFY_REGISTERED 0
90#define DEVFSD_NOTIFY_UNREGISTERED 1
91#define DEVFSD_NOTIFY_ASYNC_OPEN 2
92#define DEVFSD_NOTIFY_CLOSE 3
93#define DEVFSD_NOTIFY_LOOKUP 4
94#define DEVFSD_NOTIFY_CHANGE 5
95#define DEVFSD_NOTIFY_CREATE 6
96#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +000097#define DEVFS_PATHLEN 1024
98/* Never change this otherwise the binary interface will change */
99
Eric Andersen2af70002003-10-09 21:02:23 +0000100struct devfsd_notify_struct
101{ /* Use native C types to ensure same types in kernel and user space */
102 unsigned int type; /* DEVFSD_NOTIFY_* value */
103 unsigned int mode; /* Mode of the inode or device entry */
104 unsigned int major; /* Major number of device entry */
105 unsigned int minor; /* Minor number of device entry */
106 unsigned int uid; /* Uid of process, inode or device entry */
107 unsigned int gid; /* Gid of process, inode or device entry */
108 unsigned int overrun_count; /* Number of lost events */
109 unsigned int namelen; /* Number of characters not including '\0' */
110 /* The device name MUST come last */
111 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
112};
113
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000114#define BUFFER_SIZE 16384
115#define DEVFSD_VERSION "1.3.25"
116#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000117#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000118#define MODPROBE_SWITCH_1 "-k"
119#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000120#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000121#define MAX_ARGS (6 + 1)
122#define MAX_SUBEXPR 10
123#define STRING_LENGTH 255
124
125/* for get_uid_gid() */
126#define UID 0
127#define GID 1
128
Rob Landley1ba19d62005-10-08 17:42:35 +0000129/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000130# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000131# define NO_DIE 0
132
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000133/* for dir_operation() */
134#define RESTORE 0
135#define SERVICE 1
136#define READ_CONFIG 2
137
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000138/* Update only after changing code to reflect new protocol */
139#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
140
141/* Compile-time check */
142#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
143#error protocol version mismatch. Update your kernel headers
144#endif
145
146#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000147#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000148#define AC_EXECUTE 2
149#define AC_MFUNCTION 3 /* not supported by busybox */
150#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000151#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000152#define AC_IGNORE 6
153#define AC_MKOLDCOMPAT 7
154#define AC_MKNEWCOMPAT 8
155#define AC_RMOLDCOMPAT 9
156#define AC_RMNEWCOMPAT 10
157#define AC_RESTORE 11
158
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000159struct permissions_type
160{
161 mode_t mode;
162 uid_t uid;
163 gid_t gid;
164};
165
166struct execute_type
167{
168 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
169};
170
171struct copy_type
172{
173 const char *source;
174 const char *destination;
175};
176
177struct action_type
178{
179 unsigned int what;
180 unsigned int when;
181};
182
183struct config_entry_struct
184{
185 struct action_type action;
186 regex_t preg;
187 union
188 {
189 struct permissions_type permissions;
190 struct execute_type execute;
191 struct copy_type copy;
192 }
193 u;
194 struct config_entry_struct *next;
195};
196
197struct get_variable_info
198{
199 const struct devfsd_notify_struct *info;
200 const char *devname;
201 char devpath[STRING_LENGTH];
202};
203
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000204static void dir_operation(int , const char * , int, unsigned long* );
205static void service(struct stat statbuf, char *path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000206static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
207static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
208static int mksymlink (const char *oldpath, const char *newpath);
Eric Andersenf18bd892003-12-19 11:07:59 +0000209static void read_config_file (char *path, int optional, unsigned long *event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000210static void process_config_line (const char *, unsigned long *);
211static int do_servicing (int, unsigned long);
212static void service_name (const struct devfsd_notify_struct *);
213static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
214static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
215 const regmatch_t *, unsigned);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000216static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000217static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
218 const regmatch_t *, unsigned);
219static void action_compat (const struct devfsd_notify_struct *, unsigned);
220static void free_config (void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000221static void restore(char *spath, struct stat source_stat, int rootlen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000222static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
223static mode_t get_mode (const char *);
224static void signal_handler (int);
225static const char *get_variable (const char *, void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000226static int make_dir_tree (const char *);
227static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
228 const char *, const regmatch_t *, unsigned );
229static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
230static const char *expand_variable( char *, unsigned, unsigned *, const char *,
231 const char *(*) (const char *, void *), void * );
232static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
233static char get_old_ide_name (unsigned , unsigned);
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000234static char *write_old_sd_name (char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000235
236/* busybox functions */
Rob Landley1ba19d62005-10-08 17:42:35 +0000237static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3)));
238static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3)));
239static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000240static void fork_and_execute(int die, char *arg0, char **arg );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000241static int get_uid_gid ( int, const char *);
242static void safe_memcpy( char * dest, const char * src, int len);
Eric Andersenf18bd892003-12-19 11:07:59 +0000243static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
244static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000245
246/* Structs and vars */
247static struct config_entry_struct *first_config = NULL;
248static struct config_entry_struct *last_config = NULL;
249static const char *mount_point = NULL;
250static volatile int caught_signal = FALSE;
251static volatile int caught_sighup = FALSE;
252static struct initial_symlink_struct
253{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000254 const char *dest;
255 const char *name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000256} initial_symlinks[] =
257{
258 {"/proc/self/fd", "fd"},
259 {"fd/0", "stdin"},
260 {"fd/1", "stdout"},
261 {"fd/2", "stderr"},
262 {NULL, NULL},
263};
264
265static struct event_type
266{
267 unsigned int type; /* The DEVFSD_NOTIFY_* value */
268 const char *config_name; /* The name used in the config file */
269} event_types[] =
270{
271 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
272 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
273 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
274 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
275 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
276 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
277 {DEVFSD_NOTIFY_CREATE, "CREATE"},
278 {DEVFSD_NOTIFY_DELETE, "DELETE"},
279 {0xffffffff, NULL}
280};
281
Rob Landley1ba19d62005-10-08 17:42:35 +0000282/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000283
Rob Landley1ba19d62005-10-08 17:42:35 +0000284static const char * const bb_msg_proto_rev = "protocol revision";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000285static const char * const bb_msg_bad_config = "bad %s config file: %s";
Rob Landley1ba19d62005-10-08 17:42:35 +0000286static const char * const bb_msg_small_buffer = "buffer too small";
287static const char * const bb_msg_variable_not_found = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000288
Rob Landley1ba19d62005-10-08 17:42:35 +0000289/* Busybox functions */
290static void msg_logger(int pri, const char * fmt, ... )
291{
292 va_list ap;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000293 int ret;
294
Rob Landley1ba19d62005-10-08 17:42:35 +0000295 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000296 ret = access ("/dev/log", F_OK);
Rob Landley1ba19d62005-10-08 17:42:35 +0000297 if (ret == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000298 openlog(applet_name, 0, LOG_DAEMON);
Rob Landley1ba19d62005-10-08 17:42:35 +0000299 vsyslog( pri , fmt, ap);
300 /* Man: A trailing newline is added when needed. */
301 closelog();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000302 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000303 /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
304 if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
305 bb_error_msg(fmt, ap);
306 }
307 va_end(ap);
308}
309
310static void msg_logger_and_die(int pri, const char* fmt, ...)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000311{
312 va_list ap;
313
314 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000315 msg_logger(pri, fmt, ap);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000316 va_end(ap);
Rob Landley1ba19d62005-10-08 17:42:35 +0000317 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000318}
Rob Landley1ba19d62005-10-08 17:42:35 +0000319
320/* Busybox stuff */
321#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000322#define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args)
323#define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args)
324#define devfsd_error_msg_and_die(fmt, args...) bb_error_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000325#if defined(CONFIG_DEBUG)
326#define debug_msg_logger(x, fmt, args...) msg_logger(x, fmt, ## args)
327#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000328#define debug_msg_logger(x, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000329#endif
330#else
331#define debug_msg_logger(x, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000332#define msg_logger(p, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000333#define msg_logger_and_die(p, fmt, args...) exit(1)
334#define devfsd_perror_msg_and_die(fmt, args...) exit(1)
335#define devfsd_error_msg_and_die(fmt, args...) exit(1)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000336#define devfsd_error_msg(fmt, args...)
Eric Andersenf18bd892003-12-19 11:07:59 +0000337#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000338
Rob Landley1ba19d62005-10-08 17:42:35 +0000339static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000340{
Eric Andersenf18bd892003-12-19 11:07:59 +0000341 if (ioctl (fd, request, event_mask_flag) == -1)
Rob Landley1ba19d62005-10-08 17:42:35 +0000342 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000343}
344
345static void fork_and_execute(int die, char *arg0, char **arg )
346{
347 switch ( fork () )
348 {
349 case 0:
350 /* Child */
351 break;
352 case -1:
353 /* Parent: Error : die or return */
Rob Landley1ba19d62005-10-08 17:42:35 +0000354 msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted);
355 if(die)
Eric Andersenf18bd892003-12-19 11:07:59 +0000356 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000357 return;
358 default:
359 /* Parent : ok : return or exit */
360 if(arg0 != NULL)
361 {
362 wait (NULL);
363 return;
364 }
365 exit (EXIT_SUCCESS);
366 }
367 /* Child : if arg0 != NULL do execvp */
368 if(arg0 != NULL )
369 {
Denis Vlasenko1d76f432007-02-06 01:20:12 +0000370 BB_EXECVP(arg0, arg);
Rob Landley1ba19d62005-10-08 17:42:35 +0000371 msg_logger_and_die(LOG_ERR, "execvp");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000372 }
373}
374
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000375static void safe_memcpy( char *dest, const char *src, int len)
376{
377 memcpy (dest , src , len );
378 dest[len] = '\0';
379}
380
Eric Andersenf18bd892003-12-19 11:07:59 +0000381static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
382{
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000383 if(d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
384 return 2 + addendum;
385 if(d[n - 2]=='c' && d[n - 1]=='d')
386 return 3 + addendum;
387 if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
388 return 4 + addendum;
389 if(ptr[n - 2]=='m' && ptr[n - 1]=='t')
390 return 5 + addendum;
391 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000392}
393
394static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
395{
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000396 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/') {
Eric Andersenf18bd892003-12-19 11:07:59 +0000397 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
398 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
399 d[n - 1]=='c' )
400 return 1;
401 return scan_dev_name_common(d, n, 0, ptr);
402 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000403 if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
Eric Andersenf18bd892003-12-19 11:07:59 +0000404 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
Eric Andersenf18bd892003-12-19 11:07:59 +0000405 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000406 if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000407 return 10;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000408 if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000409 return 11;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000410 if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000411 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000412 return 0;
413}
414
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000415/* Public functions follow */
416
Denis Vlasenko06af2162007-02-03 17:28:39 +0000417int devfsd_main (int argc, char **argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000418int devfsd_main (int argc, char **argv)
419{
420 int print_version = FALSE;
421 int do_daemon = TRUE;
422 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000423 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000424 int fd, proto_rev, count;
425 unsigned long event_mask = 0;
426 struct sigaction new_action;
427 struct initial_symlink_struct *curr;
428
429 if (argc < 2)
430 bb_show_usage();
431
432 for (count = 2; count < argc; ++count)
433 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000434 if(argv[count][0] == '-')
435 {
436 if(argv[count][1]=='v' && !argv[count][2]) /* -v */
437 print_version = TRUE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000438 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f'
Rob Landley1ba19d62005-10-08 17:42:35 +0000439 && argv[count][2]=='g' && !argv[count][3]) /* -fg */
Eric Andersenf18bd892003-12-19 11:07:59 +0000440 do_daemon = FALSE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000441 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n'
Rob Landley1ba19d62005-10-08 17:42:35 +0000442 && argv[count][2]=='p' && !argv[count][3]) /* -np */
Eric Andersenf18bd892003-12-19 11:07:59 +0000443 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000444 else
445 bb_show_usage();
446 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000447 }
448
Eric Andersenf18bd892003-12-19 11:07:59 +0000449 /* strip last / from mount point, so we don't need to check for it later */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000450 while (argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
Eric Andersenf18bd892003-12-19 11:07:59 +0000451 argv[1][strlen(argv[1]) -1] = '\0';
452
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000453 mount_point = argv[1];
454
455 if (chdir (mount_point) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000456 devfsd_perror_msg_and_die(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000457
Rob Landleyd921b2e2006-08-03 15:41:12 +0000458 fd = xopen (".devfsd", O_RDONLY);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000459
460 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000461 devfsd_perror_msg_and_die("FD_CLOEXEC");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000462
Eric Andersena68ea1c2006-01-30 22:48:39 +0000463 if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
464 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000465
466 /*setup initial entries */
467 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
468 symlink (curr->dest, curr->name);
469
470 /* NB: The check for CONFIG_FILE is done in read_config_file() */
471
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000472 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
473 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000474 applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
475 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000476 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
477 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
478 exit(EXIT_SUCCESS); /* -v */
479 }
480 /* Tell kernel we are special (i.e. we get to see hidden entries) */
Rob Landley1ba19d62005-10-08 17:42:35 +0000481 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000482
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000483 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000484 new_action.sa_flags = 0;
485
486 /* Set up SIGHUP and SIGUSR1 handlers */
487 new_action.sa_handler = signal_handler;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000488 if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
Rob Landley1ba19d62005-10-08 17:42:35 +0000489 devfsd_error_msg_and_die( "sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000490
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000491 printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000492
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000493 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000494 umask(0);
495 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000496 /* Do the scan before forking, so that boot scripts see the finished product */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000497 dir_operation(SERVICE,mount_point,0,NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000498
Rob Landley1ba19d62005-10-08 17:42:35 +0000499 if (ENABLE_DEVFSD_FG_NP && no_polling)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000500 exit (0);
501 if (do_daemon)
502 {
503 /* Release so that the child can grab it */
Rob Landley1ba19d62005-10-08 17:42:35 +0000504 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000505 fork_and_execute(DIE, NULL, NULL);
506 setsid (); /* Prevent hangups and become pgrp leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000507 } else if(ENABLE_DEVFSD_FG_NP) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000508 setpgid (0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000509 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510
511 while (TRUE)
512 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000513 do_scan = do_servicing (fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000514
515 free_config ();
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000516 read_config_file ((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000517 if (do_scan)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000518 dir_operation(SERVICE,mount_point,0,NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000519 }
520} /* End Function main */
521
522
523/* Private functions follow */
524
Eric Andersenf18bd892003-12-19 11:07:59 +0000525static void read_config_file (char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000526/* [SUMMARY] Read a configuration database.
527 <path> The path to read the database from. If this is a directory, all
528 entries in that directory will be read (except hidden entries).
529 <optional> If TRUE, the routine will silently ignore a missing config file.
530 <event_mask> The event mask is written here. This is not initialised.
531 [RETURNS] Nothing.
532*/
533{
534 struct stat statbuf;
535 FILE *fp;
536 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000537 char *line = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000538
Rob Landley1ba19d62005-10-08 17:42:35 +0000539 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000540
Rob Landley06813d02005-06-07 03:47:00 +0000541 if (stat (path, &statbuf) == 0 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000542 {
Rob Landley06813d02005-06-07 03:47:00 +0000543 /* Don't read 0 length files: ignored */
544 /*if( statbuf.st_size == 0 )
545 return;*/
546 if ( S_ISDIR (statbuf.st_mode) )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000547 {
Rob Landley06813d02005-06-07 03:47:00 +0000548 /* strip last / from dirname so we don't need to check for it later */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000549 while (path && path[1]!='\0' && path[strlen(path)-1] == '/')
Rob Landley06813d02005-06-07 03:47:00 +0000550 path[strlen(path) -1] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000551
Rob Landley06813d02005-06-07 03:47:00 +0000552 dir_operation(READ_CONFIG, path, 0, event_mask);
553 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000554 }
Rob Landley06813d02005-06-07 03:47:00 +0000555 if ( ( fp = fopen (path, "r") ) != NULL )
556 {
557 while (fgets (buf, STRING_LENGTH, fp) != NULL)
558 {
559 /* Skip whitespace */
560 for (line = buf; isspace (*line); ++line)
561 /*VOID*/;
562 if (line[0] == '\0' || line[0] == '#' )
563 continue;
564 process_config_line (line, event_mask);
565 }
566 fclose (fp);
567 } else {
568 goto read_config_file_err;
569 }
570 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000571read_config_file_err:
Eric Andersenf18bd892003-12-19 11:07:59 +0000572 if(optional == 0 && errno == ENOENT)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000573 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
Rob Landley06813d02005-06-07 03:47:00 +0000574 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000575} /* End Function read_config_file */
576
577static void process_config_line (const char *line, unsigned long *event_mask)
578/* [SUMMARY] Process a line from a configuration file.
579 <line> The configuration line.
580 <event_mask> The event mask is written here. This is not initialised.
581 [RETURNS] Nothing.
582*/
583{
584 int num_args, count;
585 struct config_entry_struct *new;
586 char p[MAX_ARGS][STRING_LENGTH];
587 char when[STRING_LENGTH], what[STRING_LENGTH];
588 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000589 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000590 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000591 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000592
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000593 /* !!!! Only Uppercase Keywords in devsfd.conf */
594 static const char *const options[] = {
595 "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
596 "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
597 "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
598 "RMOLDCOMPAT", "RMNEWCOMPAT", 0
599 };
Rob Landley1ba19d62005-10-08 17:42:35 +0000600
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000601 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000602
603 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
604 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
605 when, name, what,
606 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000607
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000608 i = index_in_str_array(options, when );
Eric Andersenf18bd892003-12-19 11:07:59 +0000609
610 /*"CLEAR_CONFIG"*/
611 if( i == 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000612 {
613 free_config ();
614 *event_mask = 0;
615 return;
616 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000617
618 if ( num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000619 goto process_config_line_err;
620
Eric Andersenf18bd892003-12-19 11:07:59 +0000621 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
622 if( i == 1 || i == 2 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000623 {
624 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
Rob Landley1ba19d62005-10-08 17:42:35 +0000625 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000626 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
627 return;
628 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000629 /* "RESTORE" */
630 if( i == 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000631 {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000632 dir_operation(RESTORE,name, strlen (name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000633 return;
634 }
635 if (num_args < 3)
636 goto process_config_line_err;
637
638 new = xmalloc (sizeof *new);
639 memset (new, 0, sizeof *new);
640
641 for (count = 0; event_types[count].config_name != NULL; ++count)
642 {
643 if (strcasecmp (when, event_types[count].config_name) != 0)
644 continue;
645 new->action.when = event_types[count].type;
646 break;
647 }
648 if (event_types[count].config_name == NULL)
649 {
650 msg="WHEN in";
651 goto process_config_line_err;
652 }
653
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000654 i = index_in_str_array(options, what );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000655
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000656 switch (i)
Eric Andersenf18bd892003-12-19 11:07:59 +0000657 {
658 case 4: /* "PERMISSIONS" */
659 new->action.what = AC_PERMISSIONS;
660 /* Get user and group */
661 if ( ( ptr = strchr (p[0], '.') ) == NULL )
662 {
663 msg="UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000664 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000665 }
666
667 *ptr++ = '\0';
668 new->u.permissions.uid = get_uid_gid (UID, p[0]);
669 new->u.permissions.gid = get_uid_gid (GID, ptr);
670 /* Get mode */
671 new->u.permissions.mode = get_mode (p[1]);
672 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000673 case 5: /* MODLOAD */
674 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
675 the device name) to the module loading facility. In addition,
676 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000677 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000678 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000679 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000680 case 6: /* EXECUTE */
681 new->action.what = AC_EXECUTE;
682 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000683
Eric Andersenf18bd892003-12-19 11:07:59 +0000684 for (count = 0; count < num_args; ++count)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000685 new->u.execute.argv[count] = xstrdup (p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000686
Eric Andersenf18bd892003-12-19 11:07:59 +0000687 new->u.execute.argv[num_args] = NULL;
688 break;
689 case 7: /* COPY */
690 new->action.what = AC_COPY;
691 num_args -= 3;
692 if (num_args != 2)
693 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000694
Rob Landleyd921b2e2006-08-03 15:41:12 +0000695 new->u.copy.source = xstrdup (p[0]);
696 new->u.copy.destination = xstrdup (p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000697 break;
698 case 8: /* IGNORE */
699 /* FALLTROUGH */
700 case 9: /* MKOLDCOMPAT */
701 /* FALLTROUGH */
702 case 10: /* MKNEWCOMPAT */
703 /* FALLTROUGH */
704 case 11:/* RMOLDCOMPAT */
705 /* FALLTROUGH */
706 case 12: /* RMNEWCOMPAT */
707 /* AC_IGNORE 6
708 AC_MKOLDCOMPAT 7
709 AC_MKNEWCOMPAT 8
710 AC_RMOLDCOMPAT 9
711 AC_RMNEWCOMPAT 10*/
712 new->action.what = i - 2;
713 break;
714 default:
715 msg ="WHAT in";
716 goto process_config_line_err;
717 /*esac*/
718 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000719
720 xregcomp( &new->preg, name, REG_EXTENDED);
721
722 *event_mask |= 1 << new->action.when;
723 new->next = NULL;
724 if (first_config == NULL)
725 first_config = new;
726 else
727 last_config->next = new;
728 last_config = new;
729 return;
730process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000731 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000732} /* End Function process_config_line */
733
734static int do_servicing (int fd, unsigned long event_mask)
735/* [SUMMARY] Service devfs changes until a signal is received.
736 <fd> The open control file.
737 <event_mask> The event mask.
738 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
739*/
740{
741 ssize_t bytes;
742 struct devfsd_notify_struct info;
743 unsigned long tmp_event_mask;
744
Rob Landley1ba19d62005-10-08 17:42:35 +0000745 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000746
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000747 /* Tell devfs what events we care about */
748 tmp_event_mask = event_mask;
Rob Landley1ba19d62005-10-08 17:42:35 +0000749 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000750 while (!caught_signal)
751 {
752 errno = 0;
753 bytes = read (fd, (char *) &info, sizeof info);
754 if (caught_signal)
755 break; /* Must test for this first */
756 if (errno == EINTR)
757 continue; /* Yes, the order is important */
758 if (bytes < 1)
759 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000760 service_name (&info);
761 }
762 if (caught_signal)
763 {
764 int c_sighup = caught_sighup;
765
766 caught_signal = FALSE;
767 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000768 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000769 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000770 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000771} /* End Function do_servicing */
772
773static void service_name (const struct devfsd_notify_struct *info)
774/* [SUMMARY] Service a single devfs change.
775 <info> The devfs change.
776 [RETURNS] Nothing.
777*/
778{
779 unsigned int n;
780 regmatch_t mbuf[MAX_SUBEXPR];
781 struct config_entry_struct *entry;
782
Rob Landley1ba19d62005-10-08 17:42:35 +0000783 debug_msg_logger(LOG_INFO, __FUNCTION__);
784 if (ENABLE_DEBUG && info->overrun_count > 0)
785 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000786
787 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Eric Andersenf18bd892003-12-19 11:07:59 +0000788 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
789 ((info->devname[0]=='l' && info->devname[1]=='o' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000790 info->devname[2]=='g' && !info->devname[3]) ||
Eric Andersenf18bd892003-12-19 11:07:59 +0000791 ( info->devname[0]=='i' && info->devname[1]=='n' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000792 info->devname[2]=='i' && info->devname[3]=='t' &&
793 info->devname[4]=='c' && info->devname[5]=='t' &&
794 info->devname[6]=='l' && !info->devname[7])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000795 return;
796 for (entry = first_config; entry != NULL; entry = entry->next)
797 {
798 /* First check if action matches the type, then check if name matches */
799 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
800 continue;
801 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
802 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000803
Rob Landley1ba19d62005-10-08 17:42:35 +0000804 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000805
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000806 switch (entry->action.what)
807 {
808 case AC_PERMISSIONS:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000809 action_permissions (info, entry);
810 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000811 case AC_MODLOAD:
Rob Landley1ba19d62005-10-08 17:42:35 +0000812 if(ENABLE_DEVFSD_MODLOAD)
813 action_modload (info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000814 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000815 case AC_EXECUTE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000816 action_execute (info, entry, mbuf, n);
817 break;
818 case AC_COPY:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000819 action_copy (info, entry, mbuf, n);
820 break;
821 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000822 return;
823 /*break;*/
824 case AC_MKOLDCOMPAT:
825 case AC_MKNEWCOMPAT:
826 case AC_RMOLDCOMPAT:
827 case AC_RMNEWCOMPAT:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000828 action_compat (info, entry->action.what);
829 break;
830 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000831 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000832 }
833 }
834} /* End Function service_name */
835
836static void action_permissions (const struct devfsd_notify_struct *info,
837 const struct config_entry_struct *entry)
838/* [SUMMARY] Update permissions for a device entry.
839 <info> The devfs change.
840 <entry> The config file entry.
841 [RETURNS] Nothing.
842*/
843{
844 struct stat statbuf;
845
Rob Landley1ba19d62005-10-08 17:42:35 +0000846 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000847
848 if ( stat (info->devname, &statbuf) != 0 ||
849 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
850 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
851 {
Rob Landley1ba19d62005-10-08 17:42:35 +0000852 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000853 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000854} /* End Function action_permissions */
855
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000856static void action_modload (const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000857 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000858/* [SUMMARY] Load a module.
859 <info> The devfs change.
860 <entry> The config file entry.
861 [RETURNS] Nothing.
862*/
863{
864 char *argv[6];
865 char device[STRING_LENGTH];
866
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000867 argv[0] = (char*)MODPROBE;
868 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
869 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
870 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000871 argv[4] = device;
872 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000873
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000874 snprintf (device, sizeof (device), "/dev/%s", info->devname);
Rob Landley1ba19d62005-10-08 17:42:35 +0000875 debug_msg_logger(LOG_INFO, "%s: %s %s %s %s %s",__FUNCTION__, argv[0],argv[1],argv[2],argv[3],argv[4]);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000876 fork_and_execute(DIE, argv[0], argv);
877} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000878
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000879static void action_execute (const struct devfsd_notify_struct *info,
880 const struct config_entry_struct *entry,
881 const regmatch_t *regexpr, unsigned int numexpr)
882/* [SUMMARY] Execute a programme.
883 <info> The devfs change.
884 <entry> The config file entry.
885 <regexpr> The number of subexpression (start, end) offsets within the
886 device name.
887 <numexpr> The number of elements within <<regexpr>>.
888 [RETURNS] Nothing.
889*/
890{
891 unsigned int count;
892 struct get_variable_info gv_info;
893 char *argv[MAX_ARGS + 1];
894 char largv[MAX_ARGS + 1][STRING_LENGTH];
895
Rob Landley1ba19d62005-10-08 17:42:35 +0000896 debug_msg_logger(LOG_INFO ,__FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000897 gv_info.info = info;
898 gv_info.devname = info->devname;
899 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
900 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
901 {
902 expand_expression (largv[count], STRING_LENGTH,
903 entry->u.execute.argv[count],
904 get_variable, &gv_info,
905 gv_info.devname, regexpr, numexpr );
906 argv[count] = largv[count];
907 }
908 argv[count] = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000909 fork_and_execute(NO_DIE, argv[0], argv);
910} /* End Function action_execute */
911
912
913static void action_copy (const struct devfsd_notify_struct *info,
914 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000915 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000916/* [SUMMARY] Copy permissions.
917 <info> The devfs change.
918 <entry> The config file entry.
919 <regexpr> This list of subexpression (start, end) offsets within the
920 device name.
921 <numexpr> The number of elements in <<regexpr>>.
922 [RETURNS] Nothing.
923*/
924{
925 mode_t new_mode;
926 struct get_variable_info gv_info;
927 struct stat source_stat, dest_stat;
928 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000929 int ret = 0;
930
931 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000932
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000933 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000934
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000935 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
936 return;
937 gv_info.info = info;
938 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000939
Eric Andersenf18bd892003-12-19 11:07:59 +0000940 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000941 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
942 get_variable, &gv_info, gv_info.devname,
943 regexpr, numexpr);
944
945 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
946 get_variable, &gv_info, gv_info.devname,
947 regexpr, numexpr);
948
949 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000950 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000951 lstat (destination, &dest_stat);
952 new_mode = source_stat.st_mode & ~S_ISVTX;
953 if (info->type == DEVFSD_NOTIFY_CREATE)
954 new_mode |= S_ISVTX;
955 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
956 new_mode |= S_ISVTX;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000957 ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
Rob Landley1ba19d62005-10-08 17:42:35 +0000958 if (ENABLE_DEBUG && ret && (errno != EEXIST))
959 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000960} /* End Function action_copy */
961
Eric Andersenf18bd892003-12-19 11:07:59 +0000962static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000963/* [SUMMARY] Process a compatibility request.
964 <info> The devfs change.
965 <action> The action to take.
966 [RETURNS] Nothing.
967*/
968{
Rob Landley1ba19d62005-10-08 17:42:35 +0000969 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000970 const char *compat_name = NULL;
971 const char *dest_name = info->devname;
Eric Andersenf18bd892003-12-19 11:07:59 +0000972 char *ptr=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000973 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000974 int mode, host, bus, target, lun;
975 unsigned int i;
976 char rewind_;
977 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000978 static const char *const fmt[] = {
979 NULL ,
980 "sg/c%db%dt%du%d", /* scsi/generic */
981 "sd/c%db%dt%du%d", /* scsi/disc */
982 "sr/c%db%dt%du%d", /* scsi/cd */
983 "sd/c%db%dt%du%dp%d", /* scsi/part */
984 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
985 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
986 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
987 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
988 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
989 NULL
990 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000991
992 /* First construct compatibility name */
993 switch (action)
994 {
995 case AC_MKOLDCOMPAT:
996 case AC_RMOLDCOMPAT:
997 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
998 break;
999 case AC_MKNEWCOMPAT:
1000 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001001 ptr = strrchr (info->devname, '/') + 1;
1002 i=scan_dev_name(info->devname, info->namelen, ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001003
Rob Landley1ba19d62005-10-08 17:42:35 +00001004 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001005
1006 /* nothing found */
1007 if(i==0 || i > 9)
1008 return;
1009
1010 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1011 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001012 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +00001013 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001014
Eric Andersenf18bd892003-12-19 11:07:59 +00001015
1016 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1017 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1018 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1019
1020 /* 4 == scsi/part 8 == ide/host/part */
1021 if( i == 4 || i == 8)
1022 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1023
1024 /* 5 == scsi/mt */
1025 if( i == 5)
1026 {
1027 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001028 if (rewind_ != 'n')
1029 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001030 mode=0;
1031 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1032 mode = ptr[2] - 107; /* 1 or 2 */
1033 if(ptr[2] == 'a')
1034 mode = 3;
1035 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001036 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001037
Eric Andersenf18bd892003-12-19 11:07:59 +00001038 /* 9 == ide/host/mt */
1039 if( i == 9 )
1040 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1041 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001042 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001043
1044 if(compat_name == NULL )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001045 return;
Eric Andersenf18bd892003-12-19 11:07:59 +00001046
Rob Landley1ba19d62005-10-08 17:42:35 +00001047 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
Eric Andersenf18bd892003-12-19 11:07:59 +00001048
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001049 /* Now decide what to do with it */
1050 switch (action)
1051 {
1052 case AC_MKOLDCOMPAT:
1053 case AC_MKNEWCOMPAT:
1054 mksymlink (dest_name, compat_name);
1055 break;
1056 case AC_RMOLDCOMPAT:
1057 case AC_RMNEWCOMPAT:
Rob Landley1ba19d62005-10-08 17:42:35 +00001058 ret = unlink (compat_name);
1059 if (ENABLE_DEBUG && ret)
1060 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001061 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001062 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001063 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001064} /* End Function action_compat */
1065
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001066static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001067{
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001068 char dpath[STRING_LENGTH];
1069 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001070
Rob Landley1ba19d62005-10-08 17:42:35 +00001071 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001072
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001073 dest_stat.st_mode = 0;
1074 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1075 lstat (dpath, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001076
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001077 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1078 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001079
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001080 if ( S_ISDIR (source_stat.st_mode) )
1081 dir_operation(RESTORE, spath, rootlen,NULL);
1082}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001083
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001084
1085static int copy_inode (const char *destpath, const struct stat *dest_stat,
1086 mode_t new_mode,
1087 const char *sourcepath, const struct stat *source_stat)
1088/* [SUMMARY] Copy an inode.
1089 <destpath> The destination path. An existing inode may be deleted.
1090 <dest_stat> The destination stat(2) information.
1091 <new_mode> The desired new mode for the destination.
1092 <sourcepath> The source path.
1093 <source_stat> The source stat(2) information.
1094 [RETURNS] TRUE on success, else FALSE.
1095*/
1096{
Eric Andersenf18bd892003-12-19 11:07:59 +00001097 int source_len, dest_len;
1098 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1099 int fd, val;
1100 struct sockaddr_un un_addr;
1101 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001102
Rob Landley1ba19d62005-10-08 17:42:35 +00001103 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001104
1105 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1106 {
1107 /* Same type */
1108 if ( S_ISLNK (source_stat->st_mode) )
1109 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001110 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1111 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001112 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001113 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001114 dest_link[dest_len] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001115 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1116 {
1117 unlink (destpath);
1118 symlink (source_link, destpath);
1119 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001120 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001121 } /* Else not a symlink */
1122 chmod (destpath, new_mode & ~S_IFMT);
1123 chown (destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001124 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001125 }
1126 /* Different types: unlink and create */
1127 unlink (destpath);
1128 switch (source_stat->st_mode & S_IFMT)
1129 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001130 case S_IFSOCK:
1131 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1132 break;
1133 un_addr.sun_family = AF_UNIX;
1134 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1135 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1136 close (fd);
1137 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1138 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001139 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001140 case S_IFLNK:
1141 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1142 break;
1143 symlink_val[val] = '\0';
1144 if (symlink (symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001145 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001146 break;
1147 case S_IFREG:
1148 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1149 break;
1150 close (fd);
1151 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1152 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001153 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001154 case S_IFBLK:
1155 case S_IFCHR:
1156 case S_IFIFO:
1157 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1158 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001159 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001160 case S_IFDIR:
1161 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1162 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001163do_chown:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001164 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001165 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001166 /*break;*/
1167 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001168 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001169} /* End Function copy_inode */
1170
Bernhard Reutner-Fischerf0877982006-06-25 22:08:53 +00001171static void free_config (void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001172/* [SUMMARY] Free the configuration information.
1173 [RETURNS] Nothing.
1174*/
1175{
1176 struct config_entry_struct *c_entry;
1177 void *next;
1178
Rob Landley1ba19d62005-10-08 17:42:35 +00001179 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001180
1181 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1182 {
1183 unsigned int count;
1184
1185 next = c_entry->next;
1186 regfree (&c_entry->preg);
1187 if (c_entry->action.what == AC_EXECUTE)
1188 {
1189 for (count = 0; count < MAX_ARGS; ++count)
1190 {
1191 if (c_entry->u.execute.argv[count] == NULL)
1192 break;
1193 free (c_entry->u.execute.argv[count]);
1194 }
1195 }
1196 free (c_entry);
1197 }
1198 first_config = NULL;
1199 last_config = NULL;
1200} /* End Function free_config */
1201
1202static int get_uid_gid (int flag, const char *string)
1203/* [SUMMARY] Convert a string to a UID or GID value.
1204 <flag> "UID" or "GID".
1205 <string> The string.
1206 [RETURNS] The UID or GID value.
1207*/
1208{
1209 struct passwd *pw_ent;
1210 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001211 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001212
Rob Landley1ba19d62005-10-08 17:42:35 +00001213 if (ENABLE_DEVFSD_VERBOSE)
1214 msg="user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001215
Rob Landley1ba19d62005-10-08 17:42:35 +00001216 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001217
Rob Landley1ba19d62005-10-08 17:42:35 +00001218 if(ENABLE_DEBUG && flag != UID && flag != GID)
1219 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001220
1221 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001222 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001223
1224 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001225 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001226
1227 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001228 return grp_ent->gr_gid;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001229 else if(ENABLE_DEVFSD_VERBOSE)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001230 msg="group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001231
Rob Landley1ba19d62005-10-08 17:42:35 +00001232 if(ENABLE_DEVFSD_VERBOSE)
1233 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001234 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001235}/* End Function get_uid_gid */
1236
1237static mode_t get_mode (const char *string)
1238/* [SUMMARY] Convert a string to a mode value.
1239 <string> The string.
1240 [RETURNS] The mode value.
1241*/
1242{
1243 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001244 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001245
1246 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001247
1248 if ( isdigit (string[0]) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001249 return strtoul(string, NULL, 8);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001250 if (strlen (string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001251 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001252
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001253 mode = 0;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001254 i= S_IRUSR;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001255 while (i>0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001256 {
1257 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1258 mode+=i;
1259 i=i/2;
1260 string++;
1261 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001262 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001263} /* End Function get_mode */
1264
1265static void signal_handler (int sig)
1266{
Rob Landley1ba19d62005-10-08 17:42:35 +00001267 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001268
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001269 caught_signal = TRUE;
1270 if (sig == SIGHUP)
1271 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001272
1273 msg_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001274} /* End Function signal_handler */
1275
1276static const char *get_variable (const char *variable, void *info)
1277{
1278 struct get_variable_info *gv_info = info;
1279 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001280 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1281 "uid", "gid", "mode", hostname, mount_point,
1282 gv_info->devpath, gv_info->devname, 0 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001283 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001284
1285 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001286
1287 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +00001288 msg_logger_and_die(LOG_ERR, "gethostname: %m");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001289
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001290 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001291 hostname[STRING_LENGTH - 1] = '\0';
1292
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001293 /* index_in_str_array returns i>=0 */
1294 i=index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001295
Rob Landley176f2df2006-06-08 14:19:48 +00001296 if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001297 return NULL;
Rob Landley176f2df2006-06-08 14:19:48 +00001298 if( i >= 0 && i <= 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001299 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001300 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001301 return field_names[i+7];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001302 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001303
1304 if(i == 4 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001305 sprintf (sbuf, "%u", gv_info->info->uid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001306 else if(i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001307 sprintf (sbuf, "%u", gv_info->info->gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001308 else if(i == 6)
1309 sprintf (sbuf, "%o", gv_info->info->mode);
Rob Landley1ba19d62005-10-08 17:42:35 +00001310
1311 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
1312
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001313 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001314} /* End Function get_variable */
1315
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001316static void service(struct stat statbuf, char *path)
1317{
1318 struct devfsd_notify_struct info;
1319
Rob Landley1ba19d62005-10-08 17:42:35 +00001320 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001321
1322 memset (&info, 0, sizeof info);
1323 info.type = DEVFSD_NOTIFY_REGISTERED;
1324 info.mode = statbuf.st_mode;
1325 info.major = major (statbuf.st_rdev);
1326 info.minor = minor (statbuf.st_rdev);
1327 info.uid = statbuf.st_uid;
1328 info.gid = statbuf.st_gid;
1329 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1330 info.namelen = strlen (info.devname);
1331 service_name (&info);
1332 if ( S_ISDIR (statbuf.st_mode) )
1333 dir_operation(SERVICE,path,0,NULL);
1334}
1335
1336static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001337/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001338 <flag> To choose which function to perform
1339 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001340 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001341 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001342 [RETURNS] Nothing.
1343*/
1344{
1345 struct stat statbuf;
1346 DIR *dp;
1347 struct dirent *de;
1348 char path[STRING_LENGTH];
1349
Rob Landley1ba19d62005-10-08 17:42:35 +00001350 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001351
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001352 if((dp = opendir( dir_name))==NULL)
1353 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001354 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001355 return;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001356 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001357
1358 while ( (de = readdir (dp) ) != NULL )
1359 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001360
Denis Vlasenko16abcd92007-04-13 23:59:52 +00001361 if(de->d_name && DOT_OR_DOTDOT(de->d_name))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001362 continue;
Eric Andersenf18bd892003-12-19 11:07:59 +00001363 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001364 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001365
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001366 if (lstat (path, &statbuf) != 0)
1367 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001368 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001369 continue;
1370 }
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001371 switch (type)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001372 {
1373 case SERVICE:
1374 service(statbuf,path);
1375 break;
1376 case RESTORE:
1377 restore(path, statbuf, var);
1378 break;
1379 case READ_CONFIG:
1380 read_config_file (path, var, event_mask);
1381 break;
1382 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001383 }
1384 closedir (dp);
1385} /* End Function do_scan_and_service */
1386
1387static int mksymlink (const char *oldpath, const char *newpath)
1388/* [SUMMARY] Create a symlink, creating intervening directories as required.
1389 <oldpath> The string contained in the symlink.
1390 <newpath> The name of the new symlink.
1391 [RETURNS] 0 on success, else -1.
1392*/
1393{
Rob Landley1ba19d62005-10-08 17:42:35 +00001394 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001395
1396 if ( !make_dir_tree (newpath) )
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001397 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001398
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001399 if (symlink (oldpath, newpath) != 0) {
1400 if (errno != EEXIST) {
Rob Landley1ba19d62005-10-08 17:42:35 +00001401 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001402 return -1;
Eric Andersenf18bd892003-12-19 11:07:59 +00001403 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001404 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001405 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001406} /* End Function mksymlink */
1407
1408
1409static int make_dir_tree (const char *path)
1410/* [SUMMARY] Creating intervening directories for a path as required.
Eric Andersen52899692003-10-22 10:10:50 +00001411 <path> The full pathname (including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001412 [RETURNS] TRUE on success, else FALSE.
1413*/
1414{
Rob Landley1ba19d62005-10-08 17:42:35 +00001415 debug_msg_logger(LOG_INFO, __FUNCTION__);
1416
Eric Andersen0e020d12004-10-13 06:25:52 +00001417 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001418 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001419 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001420 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001421 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001422 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001423} /* End Function make_dir_tree */
1424
1425static int expand_expression(char *output, unsigned int outsize,
1426 const char *input,
1427 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001428 void *info,
1429 const char *devname,
1430 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001431/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001432 <output> The output expanded expression is written here.
1433 <length> The size of the output buffer.
1434 <input> The input expression. This may equal <<output>>.
1435 <get_variable> A function which will be used to get variable values. If
1436 this returns NULL, the environment is searched instead. If this is NULL,
1437 only the environment is searched.
1438 <info> An arbitrary pointer passed to <<get_variable>>.
1439 <devname> Device name; specifically, this is the string that contains all
1440 of the regular subexpressions.
1441 <ex> Array of start / end offsets into info->devname for each subexpression
1442 <numexp> Number of regular subexpressions found in <<devname>>.
1443 [RETURNS] TRUE on success, else FALSE.
1444*/
1445{
1446 char temp[STRING_LENGTH];
1447
Rob Landley1ba19d62005-10-08 17:42:35 +00001448 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001449
1450 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001451 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001452 expand_regexp (output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001453 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001454} /* End Function expand_expression */
1455
1456static void expand_regexp (char *output, size_t outsize, const char *input,
1457 const char *devname,
1458 const regmatch_t *ex, unsigned int numex )
1459/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1460 <output> The output expanded expression is written here.
1461 <outsize> The size of the output buffer.
1462 <input> The input expression. This may NOT equal <<output>>, because
1463 supporting that would require yet another string-copy. However, it's not
1464 hard to write a simple wrapper function to add this functionality for those
1465 few cases that need it.
1466 <devname> Device name; specifically, this is the string that contains all
1467 of the regular subexpressions.
1468 <ex> An array of start and end offsets into <<devname>>, one for each
1469 subexpression
1470 <numex> Number of subexpressions in the offset-array <<ex>>.
1471 [RETURNS] Nothing.
1472*/
1473{
1474 const char last_exp = '0' - 1 + numex;
1475 int c = -1;
1476
Rob Landley1ba19d62005-10-08 17:42:35 +00001477 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001478
1479 /* Guarantee NULL termination by writing an explicit '\0' character into
1480 the very last byte */
1481 if (outsize)
1482 output[--outsize] = '\0';
1483 /* Copy the input string into the output buffer, replacing '\\' with '\'
1484 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1485 codes are deleted */
1486 while ( (c != '\0') && (outsize != 0) )
1487 {
1488 c = *input;
1489 ++input;
1490 if (c == '\\')
1491 {
1492 c = *input;
1493 ++input;
1494 if (c != '\\')
1495 {
1496 if ((c >= '0') && (c <= last_exp))
1497 {
1498 const regmatch_t *subexp = ex + (c - '0');
1499 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1500
1501 /* Range checking */
1502 if (sublen > outsize)
1503 sublen = outsize;
1504 strncpy (output, devname + subexp->rm_so, sublen);
1505 output += sublen;
1506 outsize -= sublen;
1507 }
1508 continue;
1509 }
1510 }
1511 *output = c;
1512 ++output;
1513 --outsize;
1514 } /* while */
1515} /* End Function expand_regexp */
1516
1517
1518/* from compat_name.c */
1519
1520struct translate_struct
1521{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001522 const char *match; /* The string to match to (up to length) */
1523 const char *format; /* Format of output, "%s" takes data past match string,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001524 NULL is effectively "%s" (just more efficient) */
1525};
1526
1527static struct translate_struct translate_table[] =
1528{
1529 {"sound/", NULL},
1530 {"printers/", "lp%s"},
1531 {"v4l/", NULL},
1532 {"parports/", "parport%s"},
1533 {"fb/", "fb%s"},
1534 {"netlink/", NULL},
1535 {"loop/", "loop%s"},
1536 {"floppy/", "fd%s"},
1537 {"rd/", "ram%s"},
1538 {"md/", "md%s"}, /* Meta-devices */
1539 {"vc/", "tty%s"},
1540 {"misc/", NULL},
1541 {"isdn/", NULL},
1542 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1543 {"i2c/", "i2c-%s"},
1544 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1545 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1546 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1547 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1548 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1549 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1550 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1551 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1552 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1553 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1554 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1555 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1556 {"input/js", "js%s"}, /* Joystick driver */
1557 {NULL, NULL}
1558};
1559
1560const char *get_old_name (const char *devname, unsigned int namelen,
1561 char *buffer, unsigned int major, unsigned int minor)
1562/* [SUMMARY] Translate a kernel-supplied name into an old name.
1563 <devname> The device name provided by the kernel.
1564 <namelen> The length of the name.
1565 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1566 <major> The major number for the device.
1567 <minor> The minor number for the device.
1568 [RETURNS] A pointer to the old name if known, else NULL.
1569*/
1570{
1571 const char *compat_name = NULL;
1572 char *ptr;
1573 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001574 unsigned int i;
1575 char mode;
1576 int indexx;
1577 const char *pty1;
1578 const char *pty2;
1579 size_t len;
1580 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001581 static const char *const fmt[] = {
1582 NULL ,
1583 "sg%u", /* scsi/generic */
1584 NULL, /* scsi/disc */
1585 "sr%u", /* scsi/cd */
1586 NULL, /* scsi/part */
1587 "nst%u%c", /* scsi/mt */
1588 "hd%c" , /* ide/host/disc */
1589 "hd%c" , /* ide/host/cd */
1590 "hd%c%s", /* ide/host/part */
1591 "%sht%d", /* ide/host/mt */
1592 "sbpcd%u", /* sbp/ */
1593 "vcs%s", /* vcc/ */
1594 "%cty%c%c", /* pty/ */
1595 NULL
1596 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001597
Rob Landley1ba19d62005-10-08 17:42:35 +00001598 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001599
1600 for (trans = translate_table; trans->match != NULL; ++trans)
1601 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001602 len = strlen (trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001603
1604 if (strncmp (devname, trans->match, len) == 0)
1605 {
1606 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001607 return devname + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001608 sprintf (buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001609 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001610 }
1611 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001612
Eric Andersenf18bd892003-12-19 11:07:59 +00001613 ptr = (strrchr (devname, '/') + 1);
1614 i = scan_dev_name(devname, namelen, ptr);
1615
1616 if( i > 0 && i < 13)
1617 compat_name = buffer;
1618 else
1619 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001620
Rob Landley1ba19d62005-10-08 17:42:35 +00001621 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001622
1623 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1624 if( i == 1 || i == 3 || i == 10 )
1625 sprintf (buffer, fmt[i], minor);
1626
1627 /* 2 ==scsi/disc, 4 == scsi/part */
1628 if( i == 2 || i == 4)
1629 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1630
1631 /* 5 == scsi/mt */
1632 if( i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001633 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001634 mode = ptr[2];
1635 if (mode == 'n')
1636 mode = '\0';
1637 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1638 if (devname[namelen - 1] != 'n')
1639 ++compat_name;
1640 }
1641 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1642 if( i == 6 || i == 7 || i == 8 )
Rob Landley1ba19d62005-10-08 17:42:35 +00001643 /* last arg should be ignored for i == 6 or i== 7 */
1644 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001645
1646 /* 9 == ide/host/mt */
1647 if( i == 9 )
1648 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1649
1650 /* 11 == vcc/ */
1651 if( i == 11 )
1652 {
1653 sprintf (buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001654 if (buffer[3] == '0')
1655 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001656 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001657 /* 12 == pty/ */
1658 if( i == 12 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001659 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001660 pty1 = "pqrstuvwxyzabcde";
1661 pty2 = "0123456789abcdef";
1662 indexx = atoi (devname + 5);
1663 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001664 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001665
Rob Landley1ba19d62005-10-08 17:42:35 +00001666 if(ENABLE_DEBUG && compat_name!=NULL)
1667 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001668
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001669 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001670} /* End Function get_old_name */
1671
1672static char get_old_ide_name (unsigned int major, unsigned int minor)
1673/* [SUMMARY] Get the old IDE name for a device.
1674 <major> The major number for the device.
1675 <minor> The minor number for the device.
1676 [RETURNS] The drive letter.
1677*/
1678{
1679 char letter='y'; /* 121 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001680 char c='a'; /* 97 */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681 int i=IDE0_MAJOR;
1682
Rob Landley1ba19d62005-10-08 17:42:35 +00001683 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001684
1685 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1686 do {
1687 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1688 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1689 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1690 i==IDE9_MAJOR )
1691 {
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001692 if((unsigned int)i==major)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001693 {
1694 letter=c;
1695 break;
1696 }
1697 c+=2;
1698 }
1699 i++;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001700 } while (i<=IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001701
1702 if (minor > 63)
1703 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001704 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001705} /* End Function get_old_ide_name */
1706
1707static char *write_old_sd_name (char *buffer,
1708 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001709 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001710/* [SUMMARY] Write the old SCSI disc name to a buffer.
1711 <buffer> The buffer to write to.
1712 <major> The major number for the device.
1713 <minor> The minor number for the device.
1714 <part> The partition string. Must be "" for a whole-disc entry.
1715 [RETURNS] A pointer to the buffer on success, else NULL.
1716*/
1717{
1718 unsigned int disc_index;
1719
Rob Landley1ba19d62005-10-08 17:42:35 +00001720 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001721
1722 if (major == 8)
1723 {
1724 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001725 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001726 }
1727 if ( (major > 64) && (major < 72) )
1728 {
1729 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1730 if (disc_index < 26)
1731 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1732 else
1733 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001734 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001735 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001736 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001737} /* End Function write_old_sd_name */
1738
1739
1740/* expression.c */
1741
1742/*EXPERIMENTAL_FUNCTION*/
1743
1744int st_expr_expand (char *output, unsigned int length, const char *input,
1745 const char *(*get_variable_func) (const char *variable,
1746 void *info),
1747 void *info)
1748/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1749 <output> The output expanded expression is written here.
1750 <length> The size of the output buffer.
1751 <input> The input expression. This may equal <<output>>.
1752 <get_variable> A function which will be used to get variable values. If
1753 this returns NULL, the environment is searched instead. If this is NULL,
1754 only the environment is searched.
1755 <info> An arbitrary pointer passed to <<get_variable>>.
1756 [RETURNS] TRUE on success, else FALSE.
1757*/
1758{
1759 char ch;
1760 unsigned int len;
1761 unsigned int out_pos = 0;
1762 const char *env;
1763 const char *ptr;
1764 struct passwd *pwent;
1765 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1766
Rob Landley1ba19d62005-10-08 17:42:35 +00001767 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001768
1769 if (length > BUFFER_SIZE)
1770 length = BUFFER_SIZE;
1771 for (; TRUE; ++input)
1772 {
1773 switch (ch = *input)
1774 {
1775 case '$':
1776 /* Variable expansion */
1777 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1778 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001779 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001780 break;
1781 case '~':
1782 /* Home directory expansion */
1783 ch = input[1];
1784 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1785 {
1786 /* User's own home directory: leave separator for next time */
1787 if ( ( env = getenv ("HOME") ) == NULL )
1788 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001789 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001790 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001791 }
1792 len = strlen (env);
1793 if (len + out_pos >= length)
1794 goto st_expr_expand_out;
1795 memcpy (buffer + out_pos, env, len + 1);
1796 out_pos += len;
1797 continue;
1798 }
1799 /* Someone else's home directory */
1800 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1801 /* VOID */ ;
1802 len = ptr - input;
1803 if (len >= sizeof tmp)
1804 goto st_expr_expand_out;
1805 safe_memcpy (tmp, input, len);
1806 input = ptr - 1;
1807 if ( ( pwent = getpwnam (tmp) ) == NULL )
1808 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001809 msg_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001810 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001811 }
1812 len = strlen (pwent->pw_dir);
1813 if (len + out_pos >= length)
1814 goto st_expr_expand_out;
1815 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1816 out_pos += len;
1817 break;
1818 case '\0':
1819 /* Falltrough */
1820 default:
1821 if (out_pos >= length)
1822 goto st_expr_expand_out;
1823 buffer[out_pos++] = ch;
1824 if (ch == '\0')
1825 {
1826 memcpy (output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001827 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001828 }
1829 break;
1830 /* esac */
1831 }
1832 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001833 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001834st_expr_expand_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00001835 msg_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001836 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001837} /* End Function st_expr_expand */
1838
1839
1840/* Private functions follow */
1841
1842static const char *expand_variable (char *buffer, unsigned int length,
1843 unsigned int *out_pos, const char *input,
1844 const char *(*func) (const char *variable,
1845 void *info),
1846 void *info)
1847/* [SUMMARY] Expand a variable.
1848 <buffer> The buffer to write to.
1849 <length> The length of the output buffer.
1850 <out_pos> The current output position. This is updated.
1851 <input> A pointer to the input character pointer.
1852 <func> A function which will be used to get variable values. If this
1853 returns NULL, the environment is searched instead. If this is NULL, only
1854 the environment is searched.
1855 <info> An arbitrary pointer passed to <<func>>.
1856 <errfp> Diagnostic messages are written here.
1857 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1858*/
1859{
1860 char ch;
1861 int len;
1862 unsigned int open_braces;
1863 const char *env, *ptr;
1864 char tmp[STRING_LENGTH];
1865
Rob Landley1ba19d62005-10-08 17:42:35 +00001866 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001867
1868 ch = input[0];
1869 if (ch == '$')
1870 {
1871 /* Special case for "$$": PID */
1872 sprintf ( tmp, "%d", (int) getpid () );
1873 len = strlen (tmp);
1874 if (len + *out_pos >= length)
1875 goto expand_variable_out;
1876
1877 memcpy (buffer + *out_pos, tmp, len + 1);
1878 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001879 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001880 }
1881 /* Ordinary variable expansion, possibly in braces */
1882 if (ch != '{')
1883 {
1884 /* Simple variable expansion */
1885 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
1886 /* VOID */ ;
1887 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001888 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001889 goto expand_variable_out;
1890
1891 safe_memcpy (tmp, input, len);
1892 input = ptr - 1;
1893 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
1894 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001895 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001896 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001897 }
1898 len = strlen (env);
1899 if (len + *out_pos >= length)
1900 goto expand_variable_out;
1901
1902 memcpy (buffer + *out_pos, env, len + 1);
1903 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001904 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001905 }
1906 /* Variable in braces: check for ':' tricks */
1907 ch = *++input;
1908 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
1909 /* VOID */;
1910 if (ch == '}')
1911 {
1912 /* Must be simple variable expansion with "${var}" */
1913 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001914 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001915 goto expand_variable_out;
1916
1917 safe_memcpy (tmp, input, len);
1918 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
1919 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001920 return NULL;
1921 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001922 }
1923 if (ch != ':' || ptr[1] != '-' )
1924 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001925 msg_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001926 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001927 }
1928 /* It's that handy "${var:-word}" expression. Check if var is defined */
1929 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001930 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001931 goto expand_variable_out;
1932
1933 safe_memcpy (tmp, input, len);
1934 /* Move input pointer to ':' */
1935 input = ptr;
1936 /* First skip to closing brace, taking note of nested expressions */
1937 ptr += 2;
1938 ch = ptr[0];
1939 for (open_braces = 1; open_braces > 0; ch = *++ptr)
1940 {
1941 switch (ch)
1942 {
1943 case '{':
1944 ++open_braces;
1945 break;
1946 case '}':
1947 --open_braces;
1948 break;
1949 case '\0':
Rob Landley1ba19d62005-10-08 17:42:35 +00001950 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001951 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001952 default:
1953 break;
1954 }
1955 }
1956 --ptr;
1957 /* At this point ptr should point to closing brace of "${var:-word}" */
1958 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
1959 {
1960 /* Found environment variable, so skip the input to the closing brace
1961 and return the variable */
1962 input = ptr;
1963 len = strlen (env);
1964 if (len + *out_pos >= length)
1965 goto expand_variable_out;
1966
1967 memcpy (buffer + *out_pos, env, len + 1);
1968 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001969 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001970 }
1971 /* Environment variable was not found, so process word. Advance input
1972 pointer to start of word in "${var:-word}" */
1973 input += 2;
1974 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001975 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001976 goto expand_variable_out;
1977
1978 safe_memcpy (tmp, input, len);
1979 input = ptr;
1980 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001981 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001982 len = strlen (tmp);
1983 if (len + *out_pos >= length)
1984 goto expand_variable_out;
1985
1986 memcpy (buffer + *out_pos, tmp, len + 1);
1987 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001988 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001989expand_variable_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00001990 msg_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001991 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001992} /* End Function expand_variable */
1993
1994
1995static const char *get_variable_v2 (const char *variable,
1996 const char *(*func) (const char *variable, void *info),
1997 void *info)
1998/* [SUMMARY] Get a variable from the environment or .
1999 <variable> The variable name.
2000 <func> A function which will be used to get the variable. If this returns
2001 NULL, the environment is searched instead. If this is NULL, only the
2002 environment is searched.
2003 [RETURNS] The value of the variable on success, else NULL.
2004*/
2005{
2006 const char *value;
2007
Rob Landley1ba19d62005-10-08 17:42:35 +00002008 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002009
2010 if (func != NULL)
2011 {
2012 value = (*func) (variable, info);
2013 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002014 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002015 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002016 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002017} /* End Function get_variable */
2018
2019/* END OF CODE */