blob: fd5f54763e7bfe55d746aebb32cb978b6a7c2e93 [file] [log] [blame]
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001/*
2 devfsd implementation for busybox
3
4 Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
5
6 Busybox version is based on some previous work and ideas
7 Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
8
9 devfsd.c
10
11 Main file for devfsd (devfs daemon for Linux).
12
13 Copyright (C) 1998-2002 Richard Gooch
14
15 devfsd.h
16
17 Header file for devfsd (devfs daemon for Linux).
18
19 Copyright (C) 1998-2000 Richard Gooch
20
21 compat_name.c
22
23 Compatibility name file for devfsd (build compatibility names).
24
25 Copyright (C) 1998-2002 Richard Gooch
26
27 expression.c
28
29 This code provides Borne Shell-like expression expansion.
30
31 Copyright (C) 1997-1999 Richard Gooch
32
33 This program is free software; you can redistribute it and/or modify
34 it under the terms of the GNU General Public License as published by
35 the Free Software Foundation; either version 2 of the License, or
36 (at your option) any later version.
37
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
42
43 You should have received a copy of the GNU General Public License
44 along with this program; if not, write to the Free Software
45 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46
47 Richard Gooch may be reached by email at rgooch@atnf.csiro.au
48 The postal address is:
49 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
50*/
51
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000052#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000053#include "xregex.h"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000054#include <unistd.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <stdarg.h>
58#include <string.h>
59#include <ctype.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000060#include <sys/time.h>
61#include <sys/stat.h>
62#include <sys/types.h>
63#include <sys/wait.h>
64#include <sys/ioctl.h>
65#include <sys/socket.h>
66#include <sys/un.h>
67#include <dirent.h>
68#include <fcntl.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000069#include <syslog.h>
70#include <signal.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000071#include <errno.h>
Eric Andersen2af70002003-10-09 21:02:23 +000072#include <sys/sysmacros.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000073
Eric Andersen2af70002003-10-09 21:02:23 +000074
75/* Various defines taken from linux/major.h */
76#define IDE0_MAJOR 3
77#define IDE1_MAJOR 22
78#define IDE2_MAJOR 33
79#define IDE3_MAJOR 34
80#define IDE4_MAJOR 56
81#define IDE5_MAJOR 57
82#define IDE6_MAJOR 88
83#define IDE7_MAJOR 89
84#define IDE8_MAJOR 90
85#define IDE9_MAJOR 91
86
87
88/* Various defines taken from linux/devfs_fs.h */
89#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
90#define DEVFSD_IOCTL_BASE 'd'
91/* These are the various ioctls */
92#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
93#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
94#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000095#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000096#define DEVFSD_NOTIFY_REGISTERED 0
97#define DEVFSD_NOTIFY_UNREGISTERED 1
98#define DEVFSD_NOTIFY_ASYNC_OPEN 2
99#define DEVFSD_NOTIFY_CLOSE 3
100#define DEVFSD_NOTIFY_LOOKUP 4
101#define DEVFSD_NOTIFY_CHANGE 5
102#define DEVFSD_NOTIFY_CREATE 6
103#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000104#define DEVFS_PATHLEN 1024
105/* Never change this otherwise the binary interface will change */
106
Eric Andersen2af70002003-10-09 21:02:23 +0000107struct devfsd_notify_struct
108{ /* Use native C types to ensure same types in kernel and user space */
109 unsigned int type; /* DEVFSD_NOTIFY_* value */
110 unsigned int mode; /* Mode of the inode or device entry */
111 unsigned int major; /* Major number of device entry */
112 unsigned int minor; /* Minor number of device entry */
113 unsigned int uid; /* Uid of process, inode or device entry */
114 unsigned int gid; /* Gid of process, inode or device entry */
115 unsigned int overrun_count; /* Number of lost events */
116 unsigned int namelen; /* Number of characters not including '\0' */
117 /* The device name MUST come last */
118 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
119};
120
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000121#define BUFFER_SIZE 16384
122#define DEVFSD_VERSION "1.3.25"
123#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000124#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000125#define MODPROBE_SWITCH_1 "-k"
126#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000127#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000128#define MAX_ARGS (6 + 1)
129#define MAX_SUBEXPR 10
130#define STRING_LENGTH 255
131
132/* for get_uid_gid() */
133#define UID 0
134#define GID 1
135
Rob Landley1ba19d62005-10-08 17:42:35 +0000136/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000137# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000138# define NO_DIE 0
139
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000140/* for dir_operation() */
141#define RESTORE 0
142#define SERVICE 1
143#define READ_CONFIG 2
144
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000145/* Update only after changing code to reflect new protocol */
146#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
147
148/* Compile-time check */
149#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
150#error protocol version mismatch. Update your kernel headers
151#endif
152
153#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000154#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000155#define AC_EXECUTE 2
156#define AC_MFUNCTION 3 /* not supported by busybox */
157#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000158#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000159#define AC_IGNORE 6
160#define AC_MKOLDCOMPAT 7
161#define AC_MKNEWCOMPAT 8
162#define AC_RMOLDCOMPAT 9
163#define AC_RMNEWCOMPAT 10
164#define AC_RESTORE 11
165
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000166struct permissions_type
167{
168 mode_t mode;
169 uid_t uid;
170 gid_t gid;
171};
172
173struct execute_type
174{
175 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
176};
177
178struct copy_type
179{
180 const char *source;
181 const char *destination;
182};
183
184struct action_type
185{
186 unsigned int what;
187 unsigned int when;
188};
189
190struct config_entry_struct
191{
192 struct action_type action;
193 regex_t preg;
194 union
195 {
196 struct permissions_type permissions;
197 struct execute_type execute;
198 struct copy_type copy;
199 }
200 u;
201 struct config_entry_struct *next;
202};
203
204struct get_variable_info
205{
206 const struct devfsd_notify_struct *info;
207 const char *devname;
208 char devpath[STRING_LENGTH];
209};
210
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000211static void dir_operation(int , const char * , int, unsigned long* );
212static void service(struct stat statbuf, char *path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000213static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
214static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
215static int mksymlink (const char *oldpath, const char *newpath);
Eric Andersenf18bd892003-12-19 11:07:59 +0000216static void read_config_file (char *path, int optional, unsigned long *event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000217static void process_config_line (const char *, unsigned long *);
218static int do_servicing (int, unsigned long);
219static void service_name (const struct devfsd_notify_struct *);
220static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
221static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
222 const regmatch_t *, unsigned);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000223static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000224static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
225 const regmatch_t *, unsigned);
226static void action_compat (const struct devfsd_notify_struct *, unsigned);
227static void free_config (void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000228static void restore(char *spath, struct stat source_stat, int rootlen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000229static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
230static mode_t get_mode (const char *);
231static void signal_handler (int);
232static const char *get_variable (const char *, void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000233static int make_dir_tree (const char *);
234static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
235 const char *, const regmatch_t *, unsigned );
236static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
237static const char *expand_variable( char *, unsigned, unsigned *, const char *,
238 const char *(*) (const char *, void *), void * );
239static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
240static char get_old_ide_name (unsigned , unsigned);
241static char *write_old_sd_name (char *, unsigned, unsigned, char *);
242
243/* busybox functions */
Rob Landley1ba19d62005-10-08 17:42:35 +0000244static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3)));
245static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3)));
246static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000247static void fork_and_execute(int die, char *arg0, char **arg );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000248static int get_uid_gid ( int, const char *);
249static void safe_memcpy( char * dest, const char * src, int len);
Eric Andersenf18bd892003-12-19 11:07:59 +0000250static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
251static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000252
253/* Structs and vars */
254static struct config_entry_struct *first_config = NULL;
255static struct config_entry_struct *last_config = NULL;
256static const char *mount_point = NULL;
257static volatile int caught_signal = FALSE;
258static volatile int caught_sighup = FALSE;
259static struct initial_symlink_struct
260{
261 char *dest;
262 char *name;
263} initial_symlinks[] =
264{
265 {"/proc/self/fd", "fd"},
266 {"fd/0", "stdin"},
267 {"fd/1", "stdout"},
268 {"fd/2", "stderr"},
269 {NULL, NULL},
270};
271
272static struct event_type
273{
274 unsigned int type; /* The DEVFSD_NOTIFY_* value */
275 const char *config_name; /* The name used in the config file */
276} event_types[] =
277{
278 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
279 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
280 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
281 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
282 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
283 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
284 {DEVFSD_NOTIFY_CREATE, "CREATE"},
285 {DEVFSD_NOTIFY_DELETE, "DELETE"},
286 {0xffffffff, NULL}
287};
288
Rob Landley1ba19d62005-10-08 17:42:35 +0000289/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000290
Rob Landley1ba19d62005-10-08 17:42:35 +0000291static const char * const bb_msg_proto_rev = "protocol revision";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000292static const char * const bb_msg_bad_config = "bad %s config file: %s";
Rob Landley1ba19d62005-10-08 17:42:35 +0000293static const char * const bb_msg_small_buffer = "buffer too small";
294static const char * const bb_msg_variable_not_found = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000295
Rob Landley1ba19d62005-10-08 17:42:35 +0000296/* Busybox functions */
297static void msg_logger(int pri, const char * fmt, ... )
298{
299 va_list ap;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000300 int ret;
301
Rob Landley1ba19d62005-10-08 17:42:35 +0000302 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000303 ret = access ("/dev/log", F_OK);
Rob Landley1ba19d62005-10-08 17:42:35 +0000304 if (ret == 0) {
305 openlog(bb_applet_name, 0, LOG_DAEMON);
306 vsyslog( pri , fmt, ap);
307 /* Man: A trailing newline is added when needed. */
308 closelog();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000309 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000310 /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
311 if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
312 bb_error_msg(fmt, ap);
313 }
314 va_end(ap);
315}
316
317static void msg_logger_and_die(int pri, const char* fmt, ...)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000318{
319 va_list ap;
320
321 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000322 msg_logger(pri, fmt, ap);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000323 va_end(ap);
Rob Landley1ba19d62005-10-08 17:42:35 +0000324 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000325}
Rob Landley1ba19d62005-10-08 17:42:35 +0000326
327/* Busybox stuff */
328#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000329#define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args)
330#define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args)
331#define devfsd_error_msg_and_die(fmt, args...) bb_error_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000332#if defined(CONFIG_DEBUG)
333#define debug_msg_logger(x, fmt, args...) msg_logger(x, fmt, ## args)
334#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000335#define debug_msg_logger(x, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000336#endif
337#else
338#define debug_msg_logger(x, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000339#define msg_logger(p, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000340#define msg_logger_and_die(p, fmt, args...) exit(1)
341#define devfsd_perror_msg_and_die(fmt, args...) exit(1)
342#define devfsd_error_msg_and_die(fmt, args...) exit(1)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000343#define devfsd_error_msg(fmt, args...)
Eric Andersenf18bd892003-12-19 11:07:59 +0000344#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000345
Rob Landley1ba19d62005-10-08 17:42:35 +0000346static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000347{
Eric Andersenf18bd892003-12-19 11:07:59 +0000348 if (ioctl (fd, request, event_mask_flag) == -1)
Rob Landley1ba19d62005-10-08 17:42:35 +0000349 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000350}
351
352static void fork_and_execute(int die, char *arg0, char **arg )
353{
354 switch ( fork () )
355 {
356 case 0:
357 /* Child */
358 break;
359 case -1:
360 /* Parent: Error : die or return */
Rob Landley1ba19d62005-10-08 17:42:35 +0000361 msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted);
362 if(die)
Eric Andersenf18bd892003-12-19 11:07:59 +0000363 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000364 return;
365 default:
366 /* Parent : ok : return or exit */
367 if(arg0 != NULL)
368 {
369 wait (NULL);
370 return;
371 }
372 exit (EXIT_SUCCESS);
373 }
374 /* Child : if arg0 != NULL do execvp */
375 if(arg0 != NULL )
376 {
377 execvp (arg0, arg);
Rob Landley1ba19d62005-10-08 17:42:35 +0000378 msg_logger_and_die(LOG_ERR, "execvp");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000379 }
380}
381
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000382static void safe_memcpy( char *dest, const char *src, int len)
383{
384 memcpy (dest , src , len );
385 dest[len] = '\0';
386}
387
Eric Andersenf18bd892003-12-19 11:07:59 +0000388static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
389{
390 if( d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
391 return (2 + addendum);
392 else if( d[n - 2]=='c' && d[n - 1]=='d')
393 return (3 + addendum);
394 else if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
395 return (4 + addendum);
396 else if( ptr[n - 2]=='m' && ptr[n - 1]=='t')
397 return (5 + addendum);
398 else
399 return 0;
400}
401
402static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
403{
404 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/')
405 {
406 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
407 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
408 d[n - 1]=='c' )
409 return 1;
410 return scan_dev_name_common(d, n, 0, ptr);
411 }
412 else if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
413 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
414 {
415 return scan_dev_name_common(d, n, 4, ptr);
416 }
417 else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
418 {
419 return 10;
420 }
421 else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
422 {
423 return 11;
424 }
425 else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
426 {
427 return 12;
428 }
429 return 0;
430}
431
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000432/* Public functions follow */
433
434int devfsd_main (int argc, char **argv)
435{
436 int print_version = FALSE;
437 int do_daemon = TRUE;
438 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000439 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000440 int fd, proto_rev, count;
441 unsigned long event_mask = 0;
442 struct sigaction new_action;
443 struct initial_symlink_struct *curr;
444
445 if (argc < 2)
446 bb_show_usage();
447
448 for (count = 2; count < argc; ++count)
449 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000450 if(argv[count][0] == '-')
451 {
452 if(argv[count][1]=='v' && !argv[count][2]) /* -v */
453 print_version = TRUE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000454 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f'
Rob Landley1ba19d62005-10-08 17:42:35 +0000455 && argv[count][2]=='g' && !argv[count][3]) /* -fg */
Eric Andersenf18bd892003-12-19 11:07:59 +0000456 do_daemon = FALSE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000457 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n'
Rob Landley1ba19d62005-10-08 17:42:35 +0000458 && argv[count][2]=='p' && !argv[count][3]) /* -np */
Eric Andersenf18bd892003-12-19 11:07:59 +0000459 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000460 else
461 bb_show_usage();
462 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000463 }
464
Eric Andersenf18bd892003-12-19 11:07:59 +0000465 /* strip last / from mount point, so we don't need to check for it later */
466 while( argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
467 argv[1][strlen(argv[1]) -1] = '\0';
468
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000469 mount_point = argv[1];
470
471 if (chdir (mount_point) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000472 devfsd_perror_msg_and_die(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000473
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000474 fd = bb_xopen (".devfsd", O_RDONLY);
475
476 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000477 devfsd_perror_msg_and_die("FD_CLOEXEC");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000478
Eric Andersena68ea1c2006-01-30 22:48:39 +0000479 if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
480 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000481
482 /*setup initial entries */
483 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
484 symlink (curr->dest, curr->name);
485
486 /* NB: The check for CONFIG_FILE is done in read_config_file() */
487
488 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
489 {
490 bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
491 bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
492 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
493 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
494 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
495 exit(EXIT_SUCCESS); /* -v */
496 }
497 /* Tell kernel we are special (i.e. we get to see hidden entries) */
Rob Landley1ba19d62005-10-08 17:42:35 +0000498 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000499
500 sigemptyset (&new_action.sa_mask);
501 new_action.sa_flags = 0;
502
503 /* Set up SIGHUP and SIGUSR1 handlers */
504 new_action.sa_handler = signal_handler;
505 if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
Rob Landley1ba19d62005-10-08 17:42:35 +0000506 devfsd_error_msg_and_die( "sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000507
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000508 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
509
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000510 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000511 umask (0);
512 read_config_file (CONFIG_FILE, FALSE, &event_mask);
513 /* Do the scan before forking, so that boot scripts see the finished product */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000514 dir_operation(SERVICE,mount_point,0,NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000515
Rob Landley1ba19d62005-10-08 17:42:35 +0000516 if (ENABLE_DEVFSD_FG_NP && no_polling)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000517 exit (0);
518 if (do_daemon)
519 {
520 /* Release so that the child can grab it */
Rob Landley1ba19d62005-10-08 17:42:35 +0000521 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000522 fork_and_execute(DIE, NULL, NULL);
523 setsid (); /* Prevent hangups and become pgrp leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000524 } else if(ENABLE_DEVFSD_FG_NP) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000525 setpgid (0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000526 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000527
528 while (TRUE)
529 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000530 do_scan = do_servicing (fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000531
532 free_config ();
533 read_config_file (CONFIG_FILE, FALSE, &event_mask);
534 if (do_scan)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000535 dir_operation(SERVICE,mount_point,0,NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000536 }
537} /* End Function main */
538
539
540/* Private functions follow */
541
Eric Andersenf18bd892003-12-19 11:07:59 +0000542static void read_config_file (char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000543/* [SUMMARY] Read a configuration database.
544 <path> The path to read the database from. If this is a directory, all
545 entries in that directory will be read (except hidden entries).
546 <optional> If TRUE, the routine will silently ignore a missing config file.
547 <event_mask> The event mask is written here. This is not initialised.
548 [RETURNS] Nothing.
549*/
550{
551 struct stat statbuf;
552 FILE *fp;
553 char buf[STRING_LENGTH];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000554 char *line=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000555
Rob Landley1ba19d62005-10-08 17:42:35 +0000556 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000557
Rob Landley06813d02005-06-07 03:47:00 +0000558 if (stat (path, &statbuf) == 0 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000559 {
Rob Landley06813d02005-06-07 03:47:00 +0000560 /* Don't read 0 length files: ignored */
561 /*if( statbuf.st_size == 0 )
562 return;*/
563 if ( S_ISDIR (statbuf.st_mode) )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000564 {
Rob Landley06813d02005-06-07 03:47:00 +0000565 /* strip last / from dirname so we don't need to check for it later */
566 while( path && path[1]!='\0' && path[strlen(path)-1] == '/')
567 path[strlen(path) -1] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000568
Rob Landley06813d02005-06-07 03:47:00 +0000569 dir_operation(READ_CONFIG, path, 0, event_mask);
570 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000571 }
Rob Landley06813d02005-06-07 03:47:00 +0000572 if ( ( fp = fopen (path, "r") ) != NULL )
573 {
574 while (fgets (buf, STRING_LENGTH, fp) != NULL)
575 {
576 /* Skip whitespace */
577 for (line = buf; isspace (*line); ++line)
578 /*VOID*/;
579 if (line[0] == '\0' || line[0] == '#' )
580 continue;
581 process_config_line (line, event_mask);
582 }
583 fclose (fp);
584 } else {
585 goto read_config_file_err;
586 }
587 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000588read_config_file_err:
Eric Andersenf18bd892003-12-19 11:07:59 +0000589 if(optional == 0 && errno == ENOENT)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000590 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
Rob Landley06813d02005-06-07 03:47:00 +0000591 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000592 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000593} /* End Function read_config_file */
594
595static void process_config_line (const char *line, unsigned long *event_mask)
596/* [SUMMARY] Process a line from a configuration file.
597 <line> The configuration line.
598 <event_mask> The event mask is written here. This is not initialised.
599 [RETURNS] Nothing.
600*/
601{
602 int num_args, count;
603 struct config_entry_struct *new;
604 char p[MAX_ARGS][STRING_LENGTH];
605 char when[STRING_LENGTH], what[STRING_LENGTH];
606 char name[STRING_LENGTH];
607 char * msg="";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000608 char *ptr;
609
Eric Andersenf18bd892003-12-19 11:07:59 +0000610 /* !!!! Only Uppercase Keywords in devsfd.conf */
611 const char *options[] = { "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
612 "PERMISSIONS", "MODLOAD", "EXECUTE", "COPY", "IGNORE",
613 "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT", 0 };
614
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000615 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000616
Rob Landley1ba19d62005-10-08 17:42:35 +0000617 debug_msg_logger(LOG_INFO, __FUNCTION__);
618
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000619
620 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
621 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
622 when, name, what,
623 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000624
625 i = compare_string_array(options, when );
626
627 /*"CLEAR_CONFIG"*/
628 if( i == 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000629 {
630 free_config ();
631 *event_mask = 0;
632 return;
633 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000634
635 if ( num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000636 goto process_config_line_err;
637
Eric Andersenf18bd892003-12-19 11:07:59 +0000638 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
639 if( i == 1 || i == 2 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000640 {
641 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
Rob Landley1ba19d62005-10-08 17:42:35 +0000642 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000643 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
644 return;
645 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000646 /* "RESTORE" */
647 if( i == 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000648 {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000649 dir_operation(RESTORE,name, strlen (name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000650 return;
651 }
652 if (num_args < 3)
653 goto process_config_line_err;
654
655 new = xmalloc (sizeof *new);
656 memset (new, 0, sizeof *new);
657
658 for (count = 0; event_types[count].config_name != NULL; ++count)
659 {
660 if (strcasecmp (when, event_types[count].config_name) != 0)
661 continue;
662 new->action.when = event_types[count].type;
663 break;
664 }
665 if (event_types[count].config_name == NULL)
666 {
667 msg="WHEN in";
668 goto process_config_line_err;
669 }
670
Eric Andersenf18bd892003-12-19 11:07:59 +0000671 i = compare_string_array(options, what );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000672
Eric Andersenf18bd892003-12-19 11:07:59 +0000673 switch(i)
674 {
675 case 4: /* "PERMISSIONS" */
676 new->action.what = AC_PERMISSIONS;
677 /* Get user and group */
678 if ( ( ptr = strchr (p[0], '.') ) == NULL )
679 {
680 msg="UID.GID";
681 goto process_config_line_err; /*"missing '.' in UID.GID */
682 }
683
684 *ptr++ = '\0';
685 new->u.permissions.uid = get_uid_gid (UID, p[0]);
686 new->u.permissions.gid = get_uid_gid (GID, ptr);
687 /* Get mode */
688 new->u.permissions.mode = get_mode (p[1]);
689 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000690 case 5: /* MODLOAD */
691 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
692 the device name) to the module loading facility. In addition,
693 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000694 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000695 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000696 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000697 case 6: /* EXECUTE */
698 new->action.what = AC_EXECUTE;
699 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000700
Eric Andersenf18bd892003-12-19 11:07:59 +0000701 for (count = 0; count < num_args; ++count)
702 new->u.execute.argv[count] = bb_xstrdup (p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000703
Eric Andersenf18bd892003-12-19 11:07:59 +0000704 new->u.execute.argv[num_args] = NULL;
705 break;
706 case 7: /* COPY */
707 new->action.what = AC_COPY;
708 num_args -= 3;
709 if (num_args != 2)
710 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000711
Eric Andersenf18bd892003-12-19 11:07:59 +0000712 new->u.copy.source = bb_xstrdup (p[0]);
713 new->u.copy.destination = bb_xstrdup (p[1]);
714 break;
715 case 8: /* IGNORE */
716 /* FALLTROUGH */
717 case 9: /* MKOLDCOMPAT */
718 /* FALLTROUGH */
719 case 10: /* MKNEWCOMPAT */
720 /* FALLTROUGH */
721 case 11:/* RMOLDCOMPAT */
722 /* FALLTROUGH */
723 case 12: /* RMNEWCOMPAT */
724 /* AC_IGNORE 6
725 AC_MKOLDCOMPAT 7
726 AC_MKNEWCOMPAT 8
727 AC_RMOLDCOMPAT 9
728 AC_RMNEWCOMPAT 10*/
729 new->action.what = i - 2;
730 break;
731 default:
732 msg ="WHAT in";
733 goto process_config_line_err;
734 /*esac*/
735 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000736
737 xregcomp( &new->preg, name, REG_EXTENDED);
738
739 *event_mask |= 1 << new->action.when;
740 new->next = NULL;
741 if (first_config == NULL)
742 first_config = new;
743 else
744 last_config->next = new;
745 last_config = new;
746 return;
747process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000748 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000749} /* End Function process_config_line */
750
751static int do_servicing (int fd, unsigned long event_mask)
752/* [SUMMARY] Service devfs changes until a signal is received.
753 <fd> The open control file.
754 <event_mask> The event mask.
755 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
756*/
757{
758 ssize_t bytes;
759 struct devfsd_notify_struct info;
760 unsigned long tmp_event_mask;
761
Rob Landley1ba19d62005-10-08 17:42:35 +0000762 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000763
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000764 /* Tell devfs what events we care about */
765 tmp_event_mask = event_mask;
Rob Landley1ba19d62005-10-08 17:42:35 +0000766 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000767 while (!caught_signal)
768 {
769 errno = 0;
770 bytes = read (fd, (char *) &info, sizeof info);
771 if (caught_signal)
772 break; /* Must test for this first */
773 if (errno == EINTR)
774 continue; /* Yes, the order is important */
775 if (bytes < 1)
776 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000777 service_name (&info);
778 }
779 if (caught_signal)
780 {
781 int c_sighup = caught_sighup;
782
783 caught_signal = FALSE;
784 caught_sighup = FALSE;
785 return (c_sighup);
786 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000787 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000788} /* End Function do_servicing */
789
790static void service_name (const struct devfsd_notify_struct *info)
791/* [SUMMARY] Service a single devfs change.
792 <info> The devfs change.
793 [RETURNS] Nothing.
794*/
795{
796 unsigned int n;
797 regmatch_t mbuf[MAX_SUBEXPR];
798 struct config_entry_struct *entry;
799
Rob Landley1ba19d62005-10-08 17:42:35 +0000800 debug_msg_logger(LOG_INFO, __FUNCTION__);
801 if (ENABLE_DEBUG && info->overrun_count > 0)
802 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000803
804 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Eric Andersenf18bd892003-12-19 11:07:59 +0000805 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
806 ((info->devname[0]=='l' && info->devname[1]=='o' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000807 info->devname[2]=='g' && !info->devname[3]) ||
Eric Andersenf18bd892003-12-19 11:07:59 +0000808 ( info->devname[0]=='i' && info->devname[1]=='n' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000809 info->devname[2]=='i' && info->devname[3]=='t' &&
810 info->devname[4]=='c' && info->devname[5]=='t' &&
811 info->devname[6]=='l' && !info->devname[7])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000812 return;
813 for (entry = first_config; entry != NULL; entry = entry->next)
814 {
815 /* First check if action matches the type, then check if name matches */
816 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
817 continue;
818 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
819 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000820
Rob Landley1ba19d62005-10-08 17:42:35 +0000821 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000822
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000823 switch (entry->action.what)
824 {
825 case AC_PERMISSIONS:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000826 action_permissions (info, entry);
827 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000828 case AC_MODLOAD:
Rob Landley1ba19d62005-10-08 17:42:35 +0000829 if(ENABLE_DEVFSD_MODLOAD)
830 action_modload (info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000831 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000832 case AC_EXECUTE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000833 action_execute (info, entry, mbuf, n);
834 break;
835 case AC_COPY:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000836 action_copy (info, entry, mbuf, n);
837 break;
838 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000839 return;
840 /*break;*/
841 case AC_MKOLDCOMPAT:
842 case AC_MKNEWCOMPAT:
843 case AC_RMOLDCOMPAT:
844 case AC_RMNEWCOMPAT:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000845 action_compat (info, entry->action.what);
846 break;
847 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000848 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000849 }
850 }
851} /* End Function service_name */
852
853static void action_permissions (const struct devfsd_notify_struct *info,
854 const struct config_entry_struct *entry)
855/* [SUMMARY] Update permissions for a device entry.
856 <info> The devfs change.
857 <entry> The config file entry.
858 [RETURNS] Nothing.
859*/
860{
861 struct stat statbuf;
862
Rob Landley1ba19d62005-10-08 17:42:35 +0000863 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000864
865 if ( stat (info->devname, &statbuf) != 0 ||
866 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
867 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
868 {
Rob Landley1ba19d62005-10-08 17:42:35 +0000869 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000870 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000871} /* End Function action_permissions */
872
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000873static void action_modload (const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000874 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000875/* [SUMMARY] Load a module.
876 <info> The devfs change.
877 <entry> The config file entry.
878 [RETURNS] Nothing.
879*/
880{
881 char *argv[6];
882 char device[STRING_LENGTH];
883
884 argv[0] = MODPROBE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000885 argv[1] = MODPROBE_SWITCH_1; /* "-k" */
886 argv[2] = MODPROBE_SWITCH_2; /* "-C" */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000887 argv[3] = CONFIG_MODULES_DEVFS;
888 argv[4] = device;
889 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000890
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000891 snprintf (device, sizeof (device), "/dev/%s", info->devname);
Rob Landley1ba19d62005-10-08 17:42:35 +0000892 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 +0000893 fork_and_execute(DIE, argv[0], argv);
894} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000895
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000896static void action_execute (const struct devfsd_notify_struct *info,
897 const struct config_entry_struct *entry,
898 const regmatch_t *regexpr, unsigned int numexpr)
899/* [SUMMARY] Execute a programme.
900 <info> The devfs change.
901 <entry> The config file entry.
902 <regexpr> The number of subexpression (start, end) offsets within the
903 device name.
904 <numexpr> The number of elements within <<regexpr>>.
905 [RETURNS] Nothing.
906*/
907{
908 unsigned int count;
909 struct get_variable_info gv_info;
910 char *argv[MAX_ARGS + 1];
911 char largv[MAX_ARGS + 1][STRING_LENGTH];
912
Rob Landley1ba19d62005-10-08 17:42:35 +0000913 debug_msg_logger(LOG_INFO ,__FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000914 gv_info.info = info;
915 gv_info.devname = info->devname;
916 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
917 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
918 {
919 expand_expression (largv[count], STRING_LENGTH,
920 entry->u.execute.argv[count],
921 get_variable, &gv_info,
922 gv_info.devname, regexpr, numexpr );
923 argv[count] = largv[count];
924 }
925 argv[count] = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000926 fork_and_execute(NO_DIE, argv[0], argv);
927} /* End Function action_execute */
928
929
930static void action_copy (const struct devfsd_notify_struct *info,
931 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000932 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000933/* [SUMMARY] Copy permissions.
934 <info> The devfs change.
935 <entry> The config file entry.
936 <regexpr> This list of subexpression (start, end) offsets within the
937 device name.
938 <numexpr> The number of elements in <<regexpr>>.
939 [RETURNS] Nothing.
940*/
941{
942 mode_t new_mode;
943 struct get_variable_info gv_info;
944 struct stat source_stat, dest_stat;
945 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000946 int ret = 0;
947
948 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000949
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000951
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000952 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
953 return;
954 gv_info.info = info;
955 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000956
Eric Andersenf18bd892003-12-19 11:07:59 +0000957 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000958 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
959 get_variable, &gv_info, gv_info.devname,
960 regexpr, numexpr);
961
962 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
963 get_variable, &gv_info, gv_info.devname,
964 regexpr, numexpr);
965
966 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000967 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000968 lstat (destination, &dest_stat);
969 new_mode = source_stat.st_mode & ~S_ISVTX;
970 if (info->type == DEVFSD_NOTIFY_CREATE)
971 new_mode |= S_ISVTX;
972 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
973 new_mode |= S_ISVTX;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000974 ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
Rob Landley1ba19d62005-10-08 17:42:35 +0000975 if (ENABLE_DEBUG && ret && (errno != EEXIST))
976 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000977 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000978} /* End Function action_copy */
979
Eric Andersenf18bd892003-12-19 11:07:59 +0000980static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000981/* [SUMMARY] Process a compatibility request.
982 <info> The devfs change.
983 <action> The action to take.
984 [RETURNS] Nothing.
985*/
986{
Rob Landley1ba19d62005-10-08 17:42:35 +0000987 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000988 const char *compat_name = NULL;
989 const char *dest_name = info->devname;
Eric Andersenf18bd892003-12-19 11:07:59 +0000990 char *ptr=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000991 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000992 int mode, host, bus, target, lun;
993 unsigned int i;
994 char rewind_;
995 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
996 const char *fmt[] = { NULL ,
997 "sg/c%db%dt%du%d", /* scsi/generic */
998 "sd/c%db%dt%du%d", /* scsi/disc */
999 "sr/c%db%dt%du%d", /* scsi/cd */
1000 "sd/c%db%dt%du%dp%d", /* scsi/part */
1001 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
1002 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
1003 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
1004 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
1005 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
1006 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001007
1008 /* First construct compatibility name */
1009 switch (action)
1010 {
1011 case AC_MKOLDCOMPAT:
1012 case AC_RMOLDCOMPAT:
1013 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1014 break;
1015 case AC_MKNEWCOMPAT:
1016 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001017 ptr = strrchr (info->devname, '/') + 1;
1018 i=scan_dev_name(info->devname, info->namelen, ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001019
Rob Landley1ba19d62005-10-08 17:42:35 +00001020 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001021
1022 /* nothing found */
1023 if(i==0 || i > 9)
1024 return;
1025
1026 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1027 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001028 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +00001029 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001030
Eric Andersenf18bd892003-12-19 11:07:59 +00001031
1032 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1033 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1034 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1035
1036 /* 4 == scsi/part 8 == ide/host/part */
1037 if( i == 4 || i == 8)
1038 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1039
1040 /* 5 == scsi/mt */
1041 if( i == 5)
1042 {
1043 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001044 if (rewind_ != 'n')
1045 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001046 mode=0;
1047 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1048 mode = ptr[2] - 107; /* 1 or 2 */
1049 if(ptr[2] == 'a')
1050 mode = 3;
1051 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001052 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001053
Eric Andersenf18bd892003-12-19 11:07:59 +00001054 /* 9 == ide/host/mt */
1055 if( i == 9 )
1056 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1057 /* esac */
1058 } /* switch(action) */
1059
1060 if(compat_name == NULL )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001061 return;
Eric Andersenf18bd892003-12-19 11:07:59 +00001062
Rob Landley1ba19d62005-10-08 17:42:35 +00001063 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
Eric Andersenf18bd892003-12-19 11:07:59 +00001064
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001065 /* Now decide what to do with it */
1066 switch (action)
1067 {
1068 case AC_MKOLDCOMPAT:
1069 case AC_MKNEWCOMPAT:
1070 mksymlink (dest_name, compat_name);
1071 break;
1072 case AC_RMOLDCOMPAT:
1073 case AC_RMNEWCOMPAT:
Rob Landley1ba19d62005-10-08 17:42:35 +00001074 ret = unlink (compat_name);
1075 if (ENABLE_DEBUG && ret)
1076 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001077 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001078 /*esac*/
1079 } /* switch(action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001080} /* End Function action_compat */
1081
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001082static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001083{
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001084 char dpath[STRING_LENGTH];
1085 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001086
Rob Landley1ba19d62005-10-08 17:42:35 +00001087 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001088
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001089 dest_stat.st_mode = 0;
1090 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1091 lstat (dpath, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001092
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001093 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1094 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001095
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001096 if ( S_ISDIR (source_stat.st_mode) )
1097 dir_operation(RESTORE, spath, rootlen,NULL);
1098}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001099
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001100
1101static int copy_inode (const char *destpath, const struct stat *dest_stat,
1102 mode_t new_mode,
1103 const char *sourcepath, const struct stat *source_stat)
1104/* [SUMMARY] Copy an inode.
1105 <destpath> The destination path. An existing inode may be deleted.
1106 <dest_stat> The destination stat(2) information.
1107 <new_mode> The desired new mode for the destination.
1108 <sourcepath> The source path.
1109 <source_stat> The source stat(2) information.
1110 [RETURNS] TRUE on success, else FALSE.
1111*/
1112{
Eric Andersenf18bd892003-12-19 11:07:59 +00001113 int source_len, dest_len;
1114 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1115 int fd, val;
1116 struct sockaddr_un un_addr;
1117 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001118
Rob Landley1ba19d62005-10-08 17:42:35 +00001119 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001120
1121 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1122 {
1123 /* Same type */
1124 if ( S_ISLNK (source_stat->st_mode) )
1125 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001126 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1127 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
1128 return (FALSE);
1129 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001130 dest_link[dest_len] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001131 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1132 {
1133 unlink (destpath);
1134 symlink (source_link, destpath);
1135 }
1136 return (TRUE);
1137 } /* Else not a symlink */
1138 chmod (destpath, new_mode & ~S_IFMT);
1139 chown (destpath, source_stat->st_uid, source_stat->st_gid);
1140 return (TRUE);
1141 }
1142 /* Different types: unlink and create */
1143 unlink (destpath);
1144 switch (source_stat->st_mode & S_IFMT)
1145 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001146 case S_IFSOCK:
1147 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1148 break;
1149 un_addr.sun_family = AF_UNIX;
1150 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1151 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1152 close (fd);
1153 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1154 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001155 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001156 case S_IFLNK:
1157 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1158 break;
1159 symlink_val[val] = '\0';
1160 if (symlink (symlink_val, destpath) == 0)
1161 return (TRUE);
1162 break;
1163 case S_IFREG:
1164 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1165 break;
1166 close (fd);
1167 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1168 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001169 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001170 case S_IFBLK:
1171 case S_IFCHR:
1172 case S_IFIFO:
1173 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1174 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001175 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001176 case S_IFDIR:
1177 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1178 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001179do_chown:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001180 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1181 return (TRUE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001182 /*break;*/
1183 }
1184 return (FALSE);
1185} /* End Function copy_inode */
1186
1187static void free_config ()
1188/* [SUMMARY] Free the configuration information.
1189 [RETURNS] Nothing.
1190*/
1191{
1192 struct config_entry_struct *c_entry;
1193 void *next;
1194
Rob Landley1ba19d62005-10-08 17:42:35 +00001195 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001196
1197 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1198 {
1199 unsigned int count;
1200
1201 next = c_entry->next;
1202 regfree (&c_entry->preg);
1203 if (c_entry->action.what == AC_EXECUTE)
1204 {
1205 for (count = 0; count < MAX_ARGS; ++count)
1206 {
1207 if (c_entry->u.execute.argv[count] == NULL)
1208 break;
1209 free (c_entry->u.execute.argv[count]);
1210 }
1211 }
1212 free (c_entry);
1213 }
1214 first_config = NULL;
1215 last_config = NULL;
1216} /* End Function free_config */
1217
1218static int get_uid_gid (int flag, const char *string)
1219/* [SUMMARY] Convert a string to a UID or GID value.
1220 <flag> "UID" or "GID".
1221 <string> The string.
1222 [RETURNS] The UID or GID value.
1223*/
1224{
1225 struct passwd *pw_ent;
1226 struct group *grp_ent;
Rob Landley1ba19d62005-10-08 17:42:35 +00001227 static char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001228
Rob Landley1ba19d62005-10-08 17:42:35 +00001229 if (ENABLE_DEVFSD_VERBOSE)
1230 msg="user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001231
Rob Landley1ba19d62005-10-08 17:42:35 +00001232 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001233
Rob Landley1ba19d62005-10-08 17:42:35 +00001234 if(ENABLE_DEBUG && flag != UID && flag != GID)
1235 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001236
1237 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
1238 return atoi (string);
1239
1240 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
1241 return (pw_ent->pw_uid);
1242
1243 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
1244 return (grp_ent->gr_gid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001245 else if(ENABLE_DEVFSD_VERBOSE)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001246 msg="group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001247
Rob Landley1ba19d62005-10-08 17:42:35 +00001248 if(ENABLE_DEVFSD_VERBOSE)
1249 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001250 return (0);
1251}/* End Function get_uid_gid */
1252
1253static mode_t get_mode (const char *string)
1254/* [SUMMARY] Convert a string to a mode value.
1255 <string> The string.
1256 [RETURNS] The mode value.
1257*/
1258{
1259 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001260 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001261
1262 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001263
1264 if ( isdigit (string[0]) )
1265 return strtoul (string, NULL, 8);
1266 if (strlen (string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001267 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001268
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001269 mode = 0;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001270 i= S_IRUSR;
1271 while(i>0)
1272 {
1273 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1274 mode+=i;
1275 i=i/2;
1276 string++;
1277 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001278 return (mode);
1279} /* End Function get_mode */
1280
1281static void signal_handler (int sig)
1282{
Rob Landley1ba19d62005-10-08 17:42:35 +00001283 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001284
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001285 caught_signal = TRUE;
1286 if (sig == SIGHUP)
1287 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001288
1289 msg_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001290} /* End Function signal_handler */
1291
1292static const char *get_variable (const char *variable, void *info)
1293{
1294 struct get_variable_info *gv_info = info;
1295 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001296 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1297 "uid", "gid", "mode", hostname, mount_point,
1298 gv_info->devpath, gv_info->devname, 0 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001299 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001300
1301 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001302
1303 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +00001304 msg_logger_and_die(LOG_ERR, "gethostname: %m");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001305
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001306 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001307 hostname[STRING_LENGTH - 1] = '\0';
1308
1309 /* compare_string_array returns i>=0 */
1310 i=compare_string_array(field_names, variable);
1311
1312 if ( i > 6 && (i > 1 && gv_info == NULL))
1313 return (NULL);
1314 if( i >= 0 || i <= 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001315 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001316 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
Eric Andersenf18bd892003-12-19 11:07:59 +00001317 return(field_names[i+7]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001318 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001319
1320 if(i == 4 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001321 sprintf (sbuf, "%u", gv_info->info->uid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001322 else if(i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001323 sprintf (sbuf, "%u", gv_info->info->gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001324 else if(i == 6)
1325 sprintf (sbuf, "%o", gv_info->info->mode);
Rob Landley1ba19d62005-10-08 17:42:35 +00001326
1327 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
1328
Eric Andersenf18bd892003-12-19 11:07:59 +00001329 return (sbuf);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001330} /* End Function get_variable */
1331
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001332static void service(struct stat statbuf, char *path)
1333{
1334 struct devfsd_notify_struct info;
1335
Rob Landley1ba19d62005-10-08 17:42:35 +00001336 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001337
1338 memset (&info, 0, sizeof info);
1339 info.type = DEVFSD_NOTIFY_REGISTERED;
1340 info.mode = statbuf.st_mode;
1341 info.major = major (statbuf.st_rdev);
1342 info.minor = minor (statbuf.st_rdev);
1343 info.uid = statbuf.st_uid;
1344 info.gid = statbuf.st_gid;
1345 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1346 info.namelen = strlen (info.devname);
1347 service_name (&info);
1348 if ( S_ISDIR (statbuf.st_mode) )
1349 dir_operation(SERVICE,path,0,NULL);
1350}
1351
1352static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001353/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001354 <flag> To choose which function to perform
1355 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001356 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001357 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001358 [RETURNS] Nothing.
1359*/
1360{
1361 struct stat statbuf;
1362 DIR *dp;
1363 struct dirent *de;
1364 char path[STRING_LENGTH];
1365
Rob Landley1ba19d62005-10-08 17:42:35 +00001366 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001367
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001368 if((dp = opendir( dir_name))==NULL)
1369 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001370 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001371 return;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001372 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001373
1374 while ( (de = readdir (dp) ) != NULL )
1375 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001376
Eric Andersenf18bd892003-12-19 11:07:59 +00001377 if(de->d_name && *de->d_name == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001378 continue;
Eric Andersenf18bd892003-12-19 11:07:59 +00001379 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001380 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001381
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001382 if (lstat (path, &statbuf) != 0)
1383 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001384 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001385 continue;
1386 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001387 switch(type)
1388 {
1389 case SERVICE:
1390 service(statbuf,path);
1391 break;
1392 case RESTORE:
1393 restore(path, statbuf, var);
1394 break;
1395 case READ_CONFIG:
1396 read_config_file (path, var, event_mask);
1397 break;
1398 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001399 }
1400 closedir (dp);
1401} /* End Function do_scan_and_service */
1402
1403static int mksymlink (const char *oldpath, const char *newpath)
1404/* [SUMMARY] Create a symlink, creating intervening directories as required.
1405 <oldpath> The string contained in the symlink.
1406 <newpath> The name of the new symlink.
1407 [RETURNS] 0 on success, else -1.
1408*/
1409{
Rob Landley1ba19d62005-10-08 17:42:35 +00001410 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001411
1412 if ( !make_dir_tree (newpath) )
1413 return (-1);
1414
1415 if (symlink (oldpath, newpath) != 0)
1416 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001417 if (errno != EEXIST)
Eric Andersenf18bd892003-12-19 11:07:59 +00001418 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001419 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001420 return (-1);
Eric Andersenf18bd892003-12-19 11:07:59 +00001421 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001422 }
1423 return (0);
1424} /* End Function mksymlink */
1425
1426
1427static int make_dir_tree (const char *path)
1428/* [SUMMARY] Creating intervening directories for a path as required.
Eric Andersen52899692003-10-22 10:10:50 +00001429 <path> The full pathname (including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001430 [RETURNS] TRUE on success, else FALSE.
1431*/
1432{
Rob Landley1ba19d62005-10-08 17:42:35 +00001433 debug_msg_logger(LOG_INFO, __FUNCTION__);
1434
Eric Andersen0e020d12004-10-13 06:25:52 +00001435 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001436 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001437 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001438 return (FALSE);
1439 }
1440 return(TRUE);
1441} /* End Function make_dir_tree */
1442
1443static int expand_expression(char *output, unsigned int outsize,
1444 const char *input,
1445 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001446 void *info,
1447 const char *devname,
1448 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001449/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001450 <output> The output expanded expression is written here.
1451 <length> The size of the output buffer.
1452 <input> The input expression. This may equal <<output>>.
1453 <get_variable> A function which will be used to get variable values. If
1454 this returns NULL, the environment is searched instead. If this is NULL,
1455 only the environment is searched.
1456 <info> An arbitrary pointer passed to <<get_variable>>.
1457 <devname> Device name; specifically, this is the string that contains all
1458 of the regular subexpressions.
1459 <ex> Array of start / end offsets into info->devname for each subexpression
1460 <numexp> Number of regular subexpressions found in <<devname>>.
1461 [RETURNS] TRUE on success, else FALSE.
1462*/
1463{
1464 char temp[STRING_LENGTH];
1465
Rob Landley1ba19d62005-10-08 17:42:35 +00001466 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001467
1468 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
1469 return (FALSE);
1470 expand_regexp (output, outsize, temp, devname, ex, numexp);
1471 return (TRUE);
1472} /* End Function expand_expression */
1473
1474static void expand_regexp (char *output, size_t outsize, const char *input,
1475 const char *devname,
1476 const regmatch_t *ex, unsigned int numex )
1477/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1478 <output> The output expanded expression is written here.
1479 <outsize> The size of the output buffer.
1480 <input> The input expression. This may NOT equal <<output>>, because
1481 supporting that would require yet another string-copy. However, it's not
1482 hard to write a simple wrapper function to add this functionality for those
1483 few cases that need it.
1484 <devname> Device name; specifically, this is the string that contains all
1485 of the regular subexpressions.
1486 <ex> An array of start and end offsets into <<devname>>, one for each
1487 subexpression
1488 <numex> Number of subexpressions in the offset-array <<ex>>.
1489 [RETURNS] Nothing.
1490*/
1491{
1492 const char last_exp = '0' - 1 + numex;
1493 int c = -1;
1494
Rob Landley1ba19d62005-10-08 17:42:35 +00001495 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001496
1497 /* Guarantee NULL termination by writing an explicit '\0' character into
1498 the very last byte */
1499 if (outsize)
1500 output[--outsize] = '\0';
1501 /* Copy the input string into the output buffer, replacing '\\' with '\'
1502 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1503 codes are deleted */
1504 while ( (c != '\0') && (outsize != 0) )
1505 {
1506 c = *input;
1507 ++input;
1508 if (c == '\\')
1509 {
1510 c = *input;
1511 ++input;
1512 if (c != '\\')
1513 {
1514 if ((c >= '0') && (c <= last_exp))
1515 {
1516 const regmatch_t *subexp = ex + (c - '0');
1517 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1518
1519 /* Range checking */
1520 if (sublen > outsize)
1521 sublen = outsize;
1522 strncpy (output, devname + subexp->rm_so, sublen);
1523 output += sublen;
1524 outsize -= sublen;
1525 }
1526 continue;
1527 }
1528 }
1529 *output = c;
1530 ++output;
1531 --outsize;
1532 } /* while */
1533} /* End Function expand_regexp */
1534
1535
1536/* from compat_name.c */
1537
1538struct translate_struct
1539{
1540 char *match; /* The string to match to (up to length) */
1541 char *format; /* Format of output, "%s" takes data past match string,
1542 NULL is effectively "%s" (just more efficient) */
1543};
1544
1545static struct translate_struct translate_table[] =
1546{
1547 {"sound/", NULL},
1548 {"printers/", "lp%s"},
1549 {"v4l/", NULL},
1550 {"parports/", "parport%s"},
1551 {"fb/", "fb%s"},
1552 {"netlink/", NULL},
1553 {"loop/", "loop%s"},
1554 {"floppy/", "fd%s"},
1555 {"rd/", "ram%s"},
1556 {"md/", "md%s"}, /* Meta-devices */
1557 {"vc/", "tty%s"},
1558 {"misc/", NULL},
1559 {"isdn/", NULL},
1560 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1561 {"i2c/", "i2c-%s"},
1562 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1563 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1564 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1565 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1566 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1567 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1568 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1569 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1570 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1571 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1572 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1573 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1574 {"input/js", "js%s"}, /* Joystick driver */
1575 {NULL, NULL}
1576};
1577
1578const char *get_old_name (const char *devname, unsigned int namelen,
1579 char *buffer, unsigned int major, unsigned int minor)
1580/* [SUMMARY] Translate a kernel-supplied name into an old name.
1581 <devname> The device name provided by the kernel.
1582 <namelen> The length of the name.
1583 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1584 <major> The major number for the device.
1585 <minor> The minor number for the device.
1586 [RETURNS] A pointer to the old name if known, else NULL.
1587*/
1588{
1589 const char *compat_name = NULL;
1590 char *ptr;
1591 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001592 unsigned int i;
1593 char mode;
1594 int indexx;
1595 const char *pty1;
1596 const char *pty2;
1597 size_t len;
1598 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
1599 const char *fmt[] = { NULL ,
1600 "sg%u", /* scsi/generic */
1601 NULL, /* scsi/disc */
1602 "sr%u", /* scsi/cd */
1603 NULL, /* scsi/part */
1604 "nst%u%c", /* scsi/mt */
1605 "hd%c" , /* ide/host/disc */
1606 "hd%c" , /* ide/host/cd */
1607 "hd%c%s", /* ide/host/part */
1608 "%sht%d", /* ide/host/mt */
1609 "sbpcd%u", /* sbp/ */
1610 "vcs%s", /* vcc/ */
1611 "%cty%c%c", /* pty/ */
1612 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001613
Rob Landley1ba19d62005-10-08 17:42:35 +00001614 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001615
1616 for (trans = translate_table; trans->match != NULL; ++trans)
1617 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001618 len = strlen (trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001619
1620 if (strncmp (devname, trans->match, len) == 0)
1621 {
1622 if (trans->format == NULL)
1623 return (devname + len);
1624 sprintf (buffer, trans->format, devname + len);
1625 return (buffer);
1626 }
1627 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001628
Eric Andersenf18bd892003-12-19 11:07:59 +00001629 ptr = (strrchr (devname, '/') + 1);
1630 i = scan_dev_name(devname, namelen, ptr);
1631
1632 if( i > 0 && i < 13)
1633 compat_name = buffer;
1634 else
1635 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001636
Rob Landley1ba19d62005-10-08 17:42:35 +00001637 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001638
1639 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1640 if( i == 1 || i == 3 || i == 10 )
1641 sprintf (buffer, fmt[i], minor);
1642
1643 /* 2 ==scsi/disc, 4 == scsi/part */
1644 if( i == 2 || i == 4)
1645 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1646
1647 /* 5 == scsi/mt */
1648 if( i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001649 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001650 mode = ptr[2];
1651 if (mode == 'n')
1652 mode = '\0';
1653 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1654 if (devname[namelen - 1] != 'n')
1655 ++compat_name;
1656 }
1657 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1658 if( i == 6 || i == 7 || i == 8 )
Rob Landley1ba19d62005-10-08 17:42:35 +00001659 /* last arg should be ignored for i == 6 or i== 7 */
1660 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001661
1662 /* 9 == ide/host/mt */
1663 if( i == 9 )
1664 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1665
1666 /* 11 == vcc/ */
1667 if( i == 11 )
1668 {
1669 sprintf (buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001670 if (buffer[3] == '0')
1671 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001672 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001673 /* 12 == pty/ */
1674 if( i == 12 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001675 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001676 pty1 = "pqrstuvwxyzabcde";
1677 pty2 = "0123456789abcdef";
1678 indexx = atoi (devname + 5);
1679 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001680 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001681
Rob Landley1ba19d62005-10-08 17:42:35 +00001682 if(ENABLE_DEBUG && compat_name!=NULL)
1683 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001684
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001685 return (compat_name);
1686} /* End Function get_old_name */
1687
1688static char get_old_ide_name (unsigned int major, unsigned int minor)
1689/* [SUMMARY] Get the old IDE name for a device.
1690 <major> The major number for the device.
1691 <minor> The minor number for the device.
1692 [RETURNS] The drive letter.
1693*/
1694{
1695 char letter='y'; /* 121 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001696 char c='a'; /* 97 */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001697 int i=IDE0_MAJOR;
1698
Rob Landley1ba19d62005-10-08 17:42:35 +00001699 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001700
1701 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1702 do {
1703 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1704 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1705 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1706 i==IDE9_MAJOR )
1707 {
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001708 if((unsigned int)i==major)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001709 {
1710 letter=c;
1711 break;
1712 }
1713 c+=2;
1714 }
1715 i++;
1716 } while(i<=IDE9_MAJOR);
1717
1718 if (minor > 63)
1719 ++letter;
1720 return (letter);
1721} /* End Function get_old_ide_name */
1722
1723static char *write_old_sd_name (char *buffer,
1724 unsigned int major, unsigned int minor,
1725 char *part)
1726/* [SUMMARY] Write the old SCSI disc name to a buffer.
1727 <buffer> The buffer to write to.
1728 <major> The major number for the device.
1729 <minor> The minor number for the device.
1730 <part> The partition string. Must be "" for a whole-disc entry.
1731 [RETURNS] A pointer to the buffer on success, else NULL.
1732*/
1733{
1734 unsigned int disc_index;
1735
Rob Landley1ba19d62005-10-08 17:42:35 +00001736 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001737
1738 if (major == 8)
1739 {
1740 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
1741 return (buffer);
1742 }
1743 if ( (major > 64) && (major < 72) )
1744 {
1745 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1746 if (disc_index < 26)
1747 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1748 else
1749 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
1750 return (buffer);
1751 }
1752 return (NULL);
1753} /* End Function write_old_sd_name */
1754
1755
1756/* expression.c */
1757
1758/*EXPERIMENTAL_FUNCTION*/
1759
1760int st_expr_expand (char *output, unsigned int length, const char *input,
1761 const char *(*get_variable_func) (const char *variable,
1762 void *info),
1763 void *info)
1764/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1765 <output> The output expanded expression is written here.
1766 <length> The size of the output buffer.
1767 <input> The input expression. This may equal <<output>>.
1768 <get_variable> A function which will be used to get variable values. If
1769 this returns NULL, the environment is searched instead. If this is NULL,
1770 only the environment is searched.
1771 <info> An arbitrary pointer passed to <<get_variable>>.
1772 [RETURNS] TRUE on success, else FALSE.
1773*/
1774{
1775 char ch;
1776 unsigned int len;
1777 unsigned int out_pos = 0;
1778 const char *env;
1779 const char *ptr;
1780 struct passwd *pwent;
1781 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1782
Rob Landley1ba19d62005-10-08 17:42:35 +00001783 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001784
1785 if (length > BUFFER_SIZE)
1786 length = BUFFER_SIZE;
1787 for (; TRUE; ++input)
1788 {
1789 switch (ch = *input)
1790 {
1791 case '$':
1792 /* Variable expansion */
1793 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1794 if (input == NULL)
1795 return (FALSE);
1796 break;
1797 case '~':
1798 /* Home directory expansion */
1799 ch = input[1];
1800 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1801 {
1802 /* User's own home directory: leave separator for next time */
1803 if ( ( env = getenv ("HOME") ) == NULL )
1804 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001805 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001806 return (FALSE);
1807 }
1808 len = strlen (env);
1809 if (len + out_pos >= length)
1810 goto st_expr_expand_out;
1811 memcpy (buffer + out_pos, env, len + 1);
1812 out_pos += len;
1813 continue;
1814 }
1815 /* Someone else's home directory */
1816 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1817 /* VOID */ ;
1818 len = ptr - input;
1819 if (len >= sizeof tmp)
1820 goto st_expr_expand_out;
1821 safe_memcpy (tmp, input, len);
1822 input = ptr - 1;
1823 if ( ( pwent = getpwnam (tmp) ) == NULL )
1824 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001825 msg_logger(LOG_INFO, "no pwent for: %s", tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001826 return (FALSE);
1827 }
1828 len = strlen (pwent->pw_dir);
1829 if (len + out_pos >= length)
1830 goto st_expr_expand_out;
1831 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1832 out_pos += len;
1833 break;
1834 case '\0':
1835 /* Falltrough */
1836 default:
1837 if (out_pos >= length)
1838 goto st_expr_expand_out;
1839 buffer[out_pos++] = ch;
1840 if (ch == '\0')
1841 {
1842 memcpy (output, buffer, out_pos);
1843 return (TRUE);
1844 }
1845 break;
1846 /* esac */
1847 }
1848 }
1849 return (FALSE);
1850st_expr_expand_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00001851 msg_logger(LOG_INFO, bb_msg_small_buffer);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001852 return (FALSE);
1853} /* End Function st_expr_expand */
1854
1855
1856/* Private functions follow */
1857
1858static const char *expand_variable (char *buffer, unsigned int length,
1859 unsigned int *out_pos, const char *input,
1860 const char *(*func) (const char *variable,
1861 void *info),
1862 void *info)
1863/* [SUMMARY] Expand a variable.
1864 <buffer> The buffer to write to.
1865 <length> The length of the output buffer.
1866 <out_pos> The current output position. This is updated.
1867 <input> A pointer to the input character pointer.
1868 <func> A function which will be used to get variable values. If this
1869 returns NULL, the environment is searched instead. If this is NULL, only
1870 the environment is searched.
1871 <info> An arbitrary pointer passed to <<func>>.
1872 <errfp> Diagnostic messages are written here.
1873 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1874*/
1875{
1876 char ch;
1877 int len;
1878 unsigned int open_braces;
1879 const char *env, *ptr;
1880 char tmp[STRING_LENGTH];
1881
Rob Landley1ba19d62005-10-08 17:42:35 +00001882 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001883
1884 ch = input[0];
1885 if (ch == '$')
1886 {
1887 /* Special case for "$$": PID */
1888 sprintf ( tmp, "%d", (int) getpid () );
1889 len = strlen (tmp);
1890 if (len + *out_pos >= length)
1891 goto expand_variable_out;
1892
1893 memcpy (buffer + *out_pos, tmp, len + 1);
1894 out_pos += len;
1895 return (input);
1896 }
1897 /* Ordinary variable expansion, possibly in braces */
1898 if (ch != '{')
1899 {
1900 /* Simple variable expansion */
1901 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
1902 /* VOID */ ;
1903 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001904 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001905 goto expand_variable_out;
1906
1907 safe_memcpy (tmp, input, len);
1908 input = ptr - 1;
1909 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
1910 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001911 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001912 return (NULL);
1913 }
1914 len = strlen (env);
1915 if (len + *out_pos >= length)
1916 goto expand_variable_out;
1917
1918 memcpy (buffer + *out_pos, env, len + 1);
1919 *out_pos += len;
1920 return (input);
1921 }
1922 /* Variable in braces: check for ':' tricks */
1923 ch = *++input;
1924 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
1925 /* VOID */;
1926 if (ch == '}')
1927 {
1928 /* Must be simple variable expansion with "${var}" */
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 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
1935 if (ptr == NULL)
1936 return (NULL);
1937 return (input + len);
1938 }
1939 if (ch != ':' || ptr[1] != '-' )
1940 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001941 msg_logger(LOG_INFO, "illegal char in var name");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001942 return (NULL);
1943 }
1944 /* It's that handy "${var:-word}" expression. Check if var is defined */
1945 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001946 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001947 goto expand_variable_out;
1948
1949 safe_memcpy (tmp, input, len);
1950 /* Move input pointer to ':' */
1951 input = ptr;
1952 /* First skip to closing brace, taking note of nested expressions */
1953 ptr += 2;
1954 ch = ptr[0];
1955 for (open_braces = 1; open_braces > 0; ch = *++ptr)
1956 {
1957 switch (ch)
1958 {
1959 case '{':
1960 ++open_braces;
1961 break;
1962 case '}':
1963 --open_braces;
1964 break;
1965 case '\0':
Rob Landley1ba19d62005-10-08 17:42:35 +00001966 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001967 return (NULL);
1968 default:
1969 break;
1970 }
1971 }
1972 --ptr;
1973 /* At this point ptr should point to closing brace of "${var:-word}" */
1974 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
1975 {
1976 /* Found environment variable, so skip the input to the closing brace
1977 and return the variable */
1978 input = ptr;
1979 len = strlen (env);
1980 if (len + *out_pos >= length)
1981 goto expand_variable_out;
1982
1983 memcpy (buffer + *out_pos, env, len + 1);
1984 *out_pos += len;
1985 return (input);
1986 }
1987 /* Environment variable was not found, so process word. Advance input
1988 pointer to start of word in "${var:-word}" */
1989 input += 2;
1990 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001991 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001992 goto expand_variable_out;
1993
1994 safe_memcpy (tmp, input, len);
1995 input = ptr;
1996 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
1997 return (NULL);
1998 len = strlen (tmp);
1999 if (len + *out_pos >= length)
2000 goto expand_variable_out;
2001
2002 memcpy (buffer + *out_pos, tmp, len + 1);
2003 *out_pos += len;
2004 return (input);
2005expand_variable_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00002006 msg_logger(LOG_INFO, bb_msg_small_buffer);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002007 return (NULL);
2008} /* End Function expand_variable */
2009
2010
2011static const char *get_variable_v2 (const char *variable,
2012 const char *(*func) (const char *variable, void *info),
2013 void *info)
2014/* [SUMMARY] Get a variable from the environment or .
2015 <variable> The variable name.
2016 <func> A function which will be used to get the variable. If this returns
2017 NULL, the environment is searched instead. If this is NULL, only the
2018 environment is searched.
2019 [RETURNS] The value of the variable on success, else NULL.
2020*/
2021{
2022 const char *value;
2023
Rob Landley1ba19d62005-10-08 17:42:35 +00002024 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002025
2026 if (func != NULL)
2027 {
2028 value = (*func) (variable, info);
2029 if (value != NULL)
2030 return (value);
2031 }
2032 return getenv (variable);
2033} /* End Function get_variable */
2034
2035/* END OF CODE */