blob: d8a630c043e7310b45d16d368cf97611d682a6d5 [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
Rob Landley1ba19d62005-10-08 17:42:35 +0000479 do_ioctl_and_die(fd, DEVFSDIOC_GET_PROTO_REV,(int )&proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000480
481 /*setup initial entries */
482 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
483 symlink (curr->dest, curr->name);
484
485 /* NB: The check for CONFIG_FILE is done in read_config_file() */
486
487 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
488 {
489 bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
490 bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
491 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
492 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
493 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
494 exit(EXIT_SUCCESS); /* -v */
495 }
496 /* Tell kernel we are special (i.e. we get to see hidden entries) */
Rob Landley1ba19d62005-10-08 17:42:35 +0000497 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000498
499 sigemptyset (&new_action.sa_mask);
500 new_action.sa_flags = 0;
501
502 /* Set up SIGHUP and SIGUSR1 handlers */
503 new_action.sa_handler = signal_handler;
504 if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
Rob Landley1ba19d62005-10-08 17:42:35 +0000505 devfsd_error_msg_and_die( "sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000506
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000507 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
508
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000509 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510 umask (0);
511 read_config_file (CONFIG_FILE, FALSE, &event_mask);
512 /* Do the scan before forking, so that boot scripts see the finished product */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000513 dir_operation(SERVICE,mount_point,0,NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000514
Rob Landley1ba19d62005-10-08 17:42:35 +0000515 if (ENABLE_DEVFSD_FG_NP && no_polling)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000516 exit (0);
517 if (do_daemon)
518 {
519 /* Release so that the child can grab it */
Rob Landley1ba19d62005-10-08 17:42:35 +0000520 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000521 fork_and_execute(DIE, NULL, NULL);
522 setsid (); /* Prevent hangups and become pgrp leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000523 } else if(ENABLE_DEVFSD_FG_NP) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000524 setpgid (0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000525 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000526
527 while (TRUE)
528 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000529 do_scan = do_servicing (fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000530
531 free_config ();
532 read_config_file (CONFIG_FILE, FALSE, &event_mask);
533 if (do_scan)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000534 dir_operation(SERVICE,mount_point,0,NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000535 }
536} /* End Function main */
537
538
539/* Private functions follow */
540
Eric Andersenf18bd892003-12-19 11:07:59 +0000541static void read_config_file (char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000542/* [SUMMARY] Read a configuration database.
543 <path> The path to read the database from. If this is a directory, all
544 entries in that directory will be read (except hidden entries).
545 <optional> If TRUE, the routine will silently ignore a missing config file.
546 <event_mask> The event mask is written here. This is not initialised.
547 [RETURNS] Nothing.
548*/
549{
550 struct stat statbuf;
551 FILE *fp;
552 char buf[STRING_LENGTH];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000553 char *line=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000554
Rob Landley1ba19d62005-10-08 17:42:35 +0000555 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000556
Rob Landley06813d02005-06-07 03:47:00 +0000557 if (stat (path, &statbuf) == 0 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000558 {
Rob Landley06813d02005-06-07 03:47:00 +0000559 /* Don't read 0 length files: ignored */
560 /*if( statbuf.st_size == 0 )
561 return;*/
562 if ( S_ISDIR (statbuf.st_mode) )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000563 {
Rob Landley06813d02005-06-07 03:47:00 +0000564 /* strip last / from dirname so we don't need to check for it later */
565 while( path && path[1]!='\0' && path[strlen(path)-1] == '/')
566 path[strlen(path) -1] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000567
Rob Landley06813d02005-06-07 03:47:00 +0000568 dir_operation(READ_CONFIG, path, 0, event_mask);
569 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000570 }
Rob Landley06813d02005-06-07 03:47:00 +0000571 if ( ( fp = fopen (path, "r") ) != NULL )
572 {
573 while (fgets (buf, STRING_LENGTH, fp) != NULL)
574 {
575 /* Skip whitespace */
576 for (line = buf; isspace (*line); ++line)
577 /*VOID*/;
578 if (line[0] == '\0' || line[0] == '#' )
579 continue;
580 process_config_line (line, event_mask);
581 }
582 fclose (fp);
583 } else {
584 goto read_config_file_err;
585 }
586 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000587read_config_file_err:
Eric Andersenf18bd892003-12-19 11:07:59 +0000588 if(optional == 0 && errno == ENOENT)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000589 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
Rob Landley06813d02005-06-07 03:47:00 +0000590 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000591 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000592} /* End Function read_config_file */
593
594static void process_config_line (const char *line, unsigned long *event_mask)
595/* [SUMMARY] Process a line from a configuration file.
596 <line> The configuration line.
597 <event_mask> The event mask is written here. This is not initialised.
598 [RETURNS] Nothing.
599*/
600{
601 int num_args, count;
602 struct config_entry_struct *new;
603 char p[MAX_ARGS][STRING_LENGTH];
604 char when[STRING_LENGTH], what[STRING_LENGTH];
605 char name[STRING_LENGTH];
606 char * msg="";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000607 char *ptr;
608
Eric Andersenf18bd892003-12-19 11:07:59 +0000609 /* !!!! Only Uppercase Keywords in devsfd.conf */
610 const char *options[] = { "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
611 "PERMISSIONS", "MODLOAD", "EXECUTE", "COPY", "IGNORE",
612 "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT", 0 };
613
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000614 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000615
Rob Landley1ba19d62005-10-08 17:42:35 +0000616 debug_msg_logger(LOG_INFO, __FUNCTION__);
617
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000618
619 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
620 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
621 when, name, what,
622 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000623
624 i = compare_string_array(options, when );
625
626 /*"CLEAR_CONFIG"*/
627 if( i == 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000628 {
629 free_config ();
630 *event_mask = 0;
631 return;
632 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000633
634 if ( num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000635 goto process_config_line_err;
636
Eric Andersenf18bd892003-12-19 11:07:59 +0000637 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
638 if( i == 1 || i == 2 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000639 {
640 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
Rob Landley1ba19d62005-10-08 17:42:35 +0000641 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000642 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
643 return;
644 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000645 /* "RESTORE" */
646 if( i == 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000647 {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000648 dir_operation(RESTORE,name, strlen (name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000649 return;
650 }
651 if (num_args < 3)
652 goto process_config_line_err;
653
654 new = xmalloc (sizeof *new);
655 memset (new, 0, sizeof *new);
656
657 for (count = 0; event_types[count].config_name != NULL; ++count)
658 {
659 if (strcasecmp (when, event_types[count].config_name) != 0)
660 continue;
661 new->action.when = event_types[count].type;
662 break;
663 }
664 if (event_types[count].config_name == NULL)
665 {
666 msg="WHEN in";
667 goto process_config_line_err;
668 }
669
Eric Andersenf18bd892003-12-19 11:07:59 +0000670 i = compare_string_array(options, what );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000671
Eric Andersenf18bd892003-12-19 11:07:59 +0000672 switch(i)
673 {
674 case 4: /* "PERMISSIONS" */
675 new->action.what = AC_PERMISSIONS;
676 /* Get user and group */
677 if ( ( ptr = strchr (p[0], '.') ) == NULL )
678 {
679 msg="UID.GID";
680 goto process_config_line_err; /*"missing '.' in UID.GID */
681 }
682
683 *ptr++ = '\0';
684 new->u.permissions.uid = get_uid_gid (UID, p[0]);
685 new->u.permissions.gid = get_uid_gid (GID, ptr);
686 /* Get mode */
687 new->u.permissions.mode = get_mode (p[1]);
688 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000689 case 5: /* MODLOAD */
690 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
691 the device name) to the module loading facility. In addition,
692 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000693 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000694 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000695 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000696 case 6: /* EXECUTE */
697 new->action.what = AC_EXECUTE;
698 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000699
Eric Andersenf18bd892003-12-19 11:07:59 +0000700 for (count = 0; count < num_args; ++count)
701 new->u.execute.argv[count] = bb_xstrdup (p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000702
Eric Andersenf18bd892003-12-19 11:07:59 +0000703 new->u.execute.argv[num_args] = NULL;
704 break;
705 case 7: /* COPY */
706 new->action.what = AC_COPY;
707 num_args -= 3;
708 if (num_args != 2)
709 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000710
Eric Andersenf18bd892003-12-19 11:07:59 +0000711 new->u.copy.source = bb_xstrdup (p[0]);
712 new->u.copy.destination = bb_xstrdup (p[1]);
713 break;
714 case 8: /* IGNORE */
715 /* FALLTROUGH */
716 case 9: /* MKOLDCOMPAT */
717 /* FALLTROUGH */
718 case 10: /* MKNEWCOMPAT */
719 /* FALLTROUGH */
720 case 11:/* RMOLDCOMPAT */
721 /* FALLTROUGH */
722 case 12: /* RMNEWCOMPAT */
723 /* AC_IGNORE 6
724 AC_MKOLDCOMPAT 7
725 AC_MKNEWCOMPAT 8
726 AC_RMOLDCOMPAT 9
727 AC_RMNEWCOMPAT 10*/
728 new->action.what = i - 2;
729 break;
730 default:
731 msg ="WHAT in";
732 goto process_config_line_err;
733 /*esac*/
734 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000735
736 xregcomp( &new->preg, name, REG_EXTENDED);
737
738 *event_mask |= 1 << new->action.when;
739 new->next = NULL;
740 if (first_config == NULL)
741 first_config = new;
742 else
743 last_config->next = new;
744 last_config = new;
745 return;
746process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000747 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000748} /* End Function process_config_line */
749
750static int do_servicing (int fd, unsigned long event_mask)
751/* [SUMMARY] Service devfs changes until a signal is received.
752 <fd> The open control file.
753 <event_mask> The event mask.
754 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
755*/
756{
757 ssize_t bytes;
758 struct devfsd_notify_struct info;
759 unsigned long tmp_event_mask;
760
Rob Landley1ba19d62005-10-08 17:42:35 +0000761 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000762
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000763 /* Tell devfs what events we care about */
764 tmp_event_mask = event_mask;
Rob Landley1ba19d62005-10-08 17:42:35 +0000765 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000766 while (!caught_signal)
767 {
768 errno = 0;
769 bytes = read (fd, (char *) &info, sizeof info);
770 if (caught_signal)
771 break; /* Must test for this first */
772 if (errno == EINTR)
773 continue; /* Yes, the order is important */
774 if (bytes < 1)
775 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000776 service_name (&info);
777 }
778 if (caught_signal)
779 {
780 int c_sighup = caught_sighup;
781
782 caught_signal = FALSE;
783 caught_sighup = FALSE;
784 return (c_sighup);
785 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000786 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000787} /* End Function do_servicing */
788
789static void service_name (const struct devfsd_notify_struct *info)
790/* [SUMMARY] Service a single devfs change.
791 <info> The devfs change.
792 [RETURNS] Nothing.
793*/
794{
795 unsigned int n;
796 regmatch_t mbuf[MAX_SUBEXPR];
797 struct config_entry_struct *entry;
798
Rob Landley1ba19d62005-10-08 17:42:35 +0000799 debug_msg_logger(LOG_INFO, __FUNCTION__);
800 if (ENABLE_DEBUG && info->overrun_count > 0)
801 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000802
803 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Eric Andersenf18bd892003-12-19 11:07:59 +0000804 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
805 ((info->devname[0]=='l' && info->devname[1]=='o' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000806 info->devname[2]=='g' && !info->devname[3]) ||
Eric Andersenf18bd892003-12-19 11:07:59 +0000807 ( info->devname[0]=='i' && info->devname[1]=='n' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000808 info->devname[2]=='i' && info->devname[3]=='t' &&
809 info->devname[4]=='c' && info->devname[5]=='t' &&
810 info->devname[6]=='l' && !info->devname[7])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000811 return;
812 for (entry = first_config; entry != NULL; entry = entry->next)
813 {
814 /* First check if action matches the type, then check if name matches */
815 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
816 continue;
817 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
818 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000819
Rob Landley1ba19d62005-10-08 17:42:35 +0000820 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000821
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000822 switch (entry->action.what)
823 {
824 case AC_PERMISSIONS:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000825 action_permissions (info, entry);
826 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000827 case AC_MODLOAD:
Rob Landley1ba19d62005-10-08 17:42:35 +0000828 if(ENABLE_DEVFSD_MODLOAD)
829 action_modload (info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000830 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000831 case AC_EXECUTE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000832 action_execute (info, entry, mbuf, n);
833 break;
834 case AC_COPY:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000835 action_copy (info, entry, mbuf, n);
836 break;
837 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000838 return;
839 /*break;*/
840 case AC_MKOLDCOMPAT:
841 case AC_MKNEWCOMPAT:
842 case AC_RMOLDCOMPAT:
843 case AC_RMNEWCOMPAT:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000844 action_compat (info, entry->action.what);
845 break;
846 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000847 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000848 }
849 }
850} /* End Function service_name */
851
852static void action_permissions (const struct devfsd_notify_struct *info,
853 const struct config_entry_struct *entry)
854/* [SUMMARY] Update permissions for a device entry.
855 <info> The devfs change.
856 <entry> The config file entry.
857 [RETURNS] Nothing.
858*/
859{
860 struct stat statbuf;
861
Rob Landley1ba19d62005-10-08 17:42:35 +0000862 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000863
864 if ( stat (info->devname, &statbuf) != 0 ||
865 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
866 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
867 {
Rob Landley1ba19d62005-10-08 17:42:35 +0000868 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000869 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000870} /* End Function action_permissions */
871
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000872static void action_modload (const struct devfsd_notify_struct *info,
873 const struct config_entry_struct *entry)
874/* [SUMMARY] Load a module.
875 <info> The devfs change.
876 <entry> The config file entry.
877 [RETURNS] Nothing.
878*/
879{
880 char *argv[6];
881 char device[STRING_LENGTH];
882
883 argv[0] = MODPROBE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000884 argv[1] = MODPROBE_SWITCH_1; /* "-k" */
885 argv[2] = MODPROBE_SWITCH_2; /* "-C" */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000886 argv[3] = CONFIG_MODULES_DEVFS;
887 argv[4] = device;
888 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000889
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000890 snprintf (device, sizeof (device), "/dev/%s", info->devname);
Rob Landley1ba19d62005-10-08 17:42:35 +0000891 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 +0000892 fork_and_execute(DIE, argv[0], argv);
893} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000894
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000895static void action_execute (const struct devfsd_notify_struct *info,
896 const struct config_entry_struct *entry,
897 const regmatch_t *regexpr, unsigned int numexpr)
898/* [SUMMARY] Execute a programme.
899 <info> The devfs change.
900 <entry> The config file entry.
901 <regexpr> The number of subexpression (start, end) offsets within the
902 device name.
903 <numexpr> The number of elements within <<regexpr>>.
904 [RETURNS] Nothing.
905*/
906{
907 unsigned int count;
908 struct get_variable_info gv_info;
909 char *argv[MAX_ARGS + 1];
910 char largv[MAX_ARGS + 1][STRING_LENGTH];
911
Rob Landley1ba19d62005-10-08 17:42:35 +0000912 debug_msg_logger(LOG_INFO ,__FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000913 gv_info.info = info;
914 gv_info.devname = info->devname;
915 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
916 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
917 {
918 expand_expression (largv[count], STRING_LENGTH,
919 entry->u.execute.argv[count],
920 get_variable, &gv_info,
921 gv_info.devname, regexpr, numexpr );
922 argv[count] = largv[count];
923 }
924 argv[count] = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000925 fork_and_execute(NO_DIE, argv[0], argv);
926} /* End Function action_execute */
927
928
929static void action_copy (const struct devfsd_notify_struct *info,
930 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000931 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000932/* [SUMMARY] Copy permissions.
933 <info> The devfs change.
934 <entry> The config file entry.
935 <regexpr> This list of subexpression (start, end) offsets within the
936 device name.
937 <numexpr> The number of elements in <<regexpr>>.
938 [RETURNS] Nothing.
939*/
940{
941 mode_t new_mode;
942 struct get_variable_info gv_info;
943 struct stat source_stat, dest_stat;
944 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000945 int ret = 0;
946
947 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000948
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000949 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000950
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000951 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
952 return;
953 gv_info.info = info;
954 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000955
Eric Andersenf18bd892003-12-19 11:07:59 +0000956 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000957 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
958 get_variable, &gv_info, gv_info.devname,
959 regexpr, numexpr);
960
961 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
962 get_variable, &gv_info, gv_info.devname,
963 regexpr, numexpr);
964
965 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000966 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000967 lstat (destination, &dest_stat);
968 new_mode = source_stat.st_mode & ~S_ISVTX;
969 if (info->type == DEVFSD_NOTIFY_CREATE)
970 new_mode |= S_ISVTX;
971 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
972 new_mode |= S_ISVTX;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000973 ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
Rob Landley1ba19d62005-10-08 17:42:35 +0000974 if (ENABLE_DEBUG && ret && (errno != EEXIST))
975 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000976 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000977} /* End Function action_copy */
978
Eric Andersenf18bd892003-12-19 11:07:59 +0000979static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000980/* [SUMMARY] Process a compatibility request.
981 <info> The devfs change.
982 <action> The action to take.
983 [RETURNS] Nothing.
984*/
985{
Rob Landley1ba19d62005-10-08 17:42:35 +0000986 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000987 const char *compat_name = NULL;
988 const char *dest_name = info->devname;
Eric Andersenf18bd892003-12-19 11:07:59 +0000989 char *ptr=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000990 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000991 int mode, host, bus, target, lun;
992 unsigned int i;
993 char rewind_;
994 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
995 const char *fmt[] = { NULL ,
996 "sg/c%db%dt%du%d", /* scsi/generic */
997 "sd/c%db%dt%du%d", /* scsi/disc */
998 "sr/c%db%dt%du%d", /* scsi/cd */
999 "sd/c%db%dt%du%dp%d", /* scsi/part */
1000 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
1001 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
1002 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
1003 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
1004 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
1005 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001006
1007 /* First construct compatibility name */
1008 switch (action)
1009 {
1010 case AC_MKOLDCOMPAT:
1011 case AC_RMOLDCOMPAT:
1012 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1013 break;
1014 case AC_MKNEWCOMPAT:
1015 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001016 ptr = strrchr (info->devname, '/') + 1;
1017 i=scan_dev_name(info->devname, info->namelen, ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001018
Rob Landley1ba19d62005-10-08 17:42:35 +00001019 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001020
1021 /* nothing found */
1022 if(i==0 || i > 9)
1023 return;
1024
1025 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1026 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001027 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +00001028 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001029
Eric Andersenf18bd892003-12-19 11:07:59 +00001030
1031 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1032 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1033 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1034
1035 /* 4 == scsi/part 8 == ide/host/part */
1036 if( i == 4 || i == 8)
1037 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1038
1039 /* 5 == scsi/mt */
1040 if( i == 5)
1041 {
1042 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001043 if (rewind_ != 'n')
1044 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001045 mode=0;
1046 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1047 mode = ptr[2] - 107; /* 1 or 2 */
1048 if(ptr[2] == 'a')
1049 mode = 3;
1050 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001051 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001052
Eric Andersenf18bd892003-12-19 11:07:59 +00001053 /* 9 == ide/host/mt */
1054 if( i == 9 )
1055 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1056 /* esac */
1057 } /* switch(action) */
1058
1059 if(compat_name == NULL )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001060 return;
Eric Andersenf18bd892003-12-19 11:07:59 +00001061
Rob Landley1ba19d62005-10-08 17:42:35 +00001062 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
Eric Andersenf18bd892003-12-19 11:07:59 +00001063
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001064 /* Now decide what to do with it */
1065 switch (action)
1066 {
1067 case AC_MKOLDCOMPAT:
1068 case AC_MKNEWCOMPAT:
1069 mksymlink (dest_name, compat_name);
1070 break;
1071 case AC_RMOLDCOMPAT:
1072 case AC_RMNEWCOMPAT:
Rob Landley1ba19d62005-10-08 17:42:35 +00001073 ret = unlink (compat_name);
1074 if (ENABLE_DEBUG && ret)
1075 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001076 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001077 /*esac*/
1078 } /* switch(action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001079} /* End Function action_compat */
1080
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001081static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001082{
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001083 char dpath[STRING_LENGTH];
1084 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001085
Rob Landley1ba19d62005-10-08 17:42:35 +00001086 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001087
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001088 dest_stat.st_mode = 0;
1089 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1090 lstat (dpath, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001091
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001092 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1093 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001094
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001095 if ( S_ISDIR (source_stat.st_mode) )
1096 dir_operation(RESTORE, spath, rootlen,NULL);
1097}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001098
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001099
1100static int copy_inode (const char *destpath, const struct stat *dest_stat,
1101 mode_t new_mode,
1102 const char *sourcepath, const struct stat *source_stat)
1103/* [SUMMARY] Copy an inode.
1104 <destpath> The destination path. An existing inode may be deleted.
1105 <dest_stat> The destination stat(2) information.
1106 <new_mode> The desired new mode for the destination.
1107 <sourcepath> The source path.
1108 <source_stat> The source stat(2) information.
1109 [RETURNS] TRUE on success, else FALSE.
1110*/
1111{
Eric Andersenf18bd892003-12-19 11:07:59 +00001112 int source_len, dest_len;
1113 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1114 int fd, val;
1115 struct sockaddr_un un_addr;
1116 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001117
Rob Landley1ba19d62005-10-08 17:42:35 +00001118 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001119
1120 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1121 {
1122 /* Same type */
1123 if ( S_ISLNK (source_stat->st_mode) )
1124 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001125 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1126 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
1127 return (FALSE);
1128 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001129 dest_link[dest_len] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001130 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1131 {
1132 unlink (destpath);
1133 symlink (source_link, destpath);
1134 }
1135 return (TRUE);
1136 } /* Else not a symlink */
1137 chmod (destpath, new_mode & ~S_IFMT);
1138 chown (destpath, source_stat->st_uid, source_stat->st_gid);
1139 return (TRUE);
1140 }
1141 /* Different types: unlink and create */
1142 unlink (destpath);
1143 switch (source_stat->st_mode & S_IFMT)
1144 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001145 case S_IFSOCK:
1146 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1147 break;
1148 un_addr.sun_family = AF_UNIX;
1149 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1150 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1151 close (fd);
1152 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1153 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001154 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001155 case S_IFLNK:
1156 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1157 break;
1158 symlink_val[val] = '\0';
1159 if (symlink (symlink_val, destpath) == 0)
1160 return (TRUE);
1161 break;
1162 case S_IFREG:
1163 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1164 break;
1165 close (fd);
1166 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1167 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001168 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001169 case S_IFBLK:
1170 case S_IFCHR:
1171 case S_IFIFO:
1172 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1173 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001174 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001175 case S_IFDIR:
1176 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1177 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001178do_chown:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001179 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1180 return (TRUE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001181 /*break;*/
1182 }
1183 return (FALSE);
1184} /* End Function copy_inode */
1185
1186static void free_config ()
1187/* [SUMMARY] Free the configuration information.
1188 [RETURNS] Nothing.
1189*/
1190{
1191 struct config_entry_struct *c_entry;
1192 void *next;
1193
Rob Landley1ba19d62005-10-08 17:42:35 +00001194 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195
1196 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1197 {
1198 unsigned int count;
1199
1200 next = c_entry->next;
1201 regfree (&c_entry->preg);
1202 if (c_entry->action.what == AC_EXECUTE)
1203 {
1204 for (count = 0; count < MAX_ARGS; ++count)
1205 {
1206 if (c_entry->u.execute.argv[count] == NULL)
1207 break;
1208 free (c_entry->u.execute.argv[count]);
1209 }
1210 }
1211 free (c_entry);
1212 }
1213 first_config = NULL;
1214 last_config = NULL;
1215} /* End Function free_config */
1216
1217static int get_uid_gid (int flag, const char *string)
1218/* [SUMMARY] Convert a string to a UID or GID value.
1219 <flag> "UID" or "GID".
1220 <string> The string.
1221 [RETURNS] The UID or GID value.
1222*/
1223{
1224 struct passwd *pw_ent;
1225 struct group *grp_ent;
Rob Landley1ba19d62005-10-08 17:42:35 +00001226 static char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001227
Rob Landley1ba19d62005-10-08 17:42:35 +00001228 if (ENABLE_DEVFSD_VERBOSE)
1229 msg="user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001230
Rob Landley1ba19d62005-10-08 17:42:35 +00001231 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001232
Rob Landley1ba19d62005-10-08 17:42:35 +00001233 if(ENABLE_DEBUG && flag != UID && flag != GID)
1234 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001235
1236 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
1237 return atoi (string);
1238
1239 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
1240 return (pw_ent->pw_uid);
1241
1242 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
1243 return (grp_ent->gr_gid);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001244 else if(ENABLE_DEVFSD_VERBOSE)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001245 msg="group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001246
Rob Landley1ba19d62005-10-08 17:42:35 +00001247 if(ENABLE_DEVFSD_VERBOSE)
1248 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001249 return (0);
1250}/* End Function get_uid_gid */
1251
1252static mode_t get_mode (const char *string)
1253/* [SUMMARY] Convert a string to a mode value.
1254 <string> The string.
1255 [RETURNS] The mode value.
1256*/
1257{
1258 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001259 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001260
1261 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001262
1263 if ( isdigit (string[0]) )
1264 return strtoul (string, NULL, 8);
1265 if (strlen (string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001266 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001267
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001268 mode = 0;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001269 i= S_IRUSR;
1270 while(i>0)
1271 {
1272 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1273 mode+=i;
1274 i=i/2;
1275 string++;
1276 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001277 return (mode);
1278} /* End Function get_mode */
1279
1280static void signal_handler (int sig)
1281{
Rob Landley1ba19d62005-10-08 17:42:35 +00001282 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001283
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001284 caught_signal = TRUE;
1285 if (sig == SIGHUP)
1286 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001287
1288 msg_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001289} /* End Function signal_handler */
1290
1291static const char *get_variable (const char *variable, void *info)
1292{
1293 struct get_variable_info *gv_info = info;
1294 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001295 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1296 "uid", "gid", "mode", hostname, mount_point,
1297 gv_info->devpath, gv_info->devname, 0 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001298 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001299
1300 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001301
1302 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +00001303 msg_logger_and_die(LOG_ERR, "gethostname: %m");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001304
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001305 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001306 hostname[STRING_LENGTH - 1] = '\0';
1307
1308 /* compare_string_array returns i>=0 */
1309 i=compare_string_array(field_names, variable);
1310
1311 if ( i > 6 && (i > 1 && gv_info == NULL))
1312 return (NULL);
1313 if( i >= 0 || i <= 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001314 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001315 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
Eric Andersenf18bd892003-12-19 11:07:59 +00001316 return(field_names[i+7]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001317 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001318
1319 if(i == 4 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001320 sprintf (sbuf, "%u", gv_info->info->uid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001321 else if(i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001322 sprintf (sbuf, "%u", gv_info->info->gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001323 else if(i == 6)
1324 sprintf (sbuf, "%o", gv_info->info->mode);
Rob Landley1ba19d62005-10-08 17:42:35 +00001325
1326 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
1327
Eric Andersenf18bd892003-12-19 11:07:59 +00001328 return (sbuf);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001329} /* End Function get_variable */
1330
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001331static void service(struct stat statbuf, char *path)
1332{
1333 struct devfsd_notify_struct info;
1334
Rob Landley1ba19d62005-10-08 17:42:35 +00001335 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001336
1337 memset (&info, 0, sizeof info);
1338 info.type = DEVFSD_NOTIFY_REGISTERED;
1339 info.mode = statbuf.st_mode;
1340 info.major = major (statbuf.st_rdev);
1341 info.minor = minor (statbuf.st_rdev);
1342 info.uid = statbuf.st_uid;
1343 info.gid = statbuf.st_gid;
1344 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1345 info.namelen = strlen (info.devname);
1346 service_name (&info);
1347 if ( S_ISDIR (statbuf.st_mode) )
1348 dir_operation(SERVICE,path,0,NULL);
1349}
1350
1351static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001352/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001353 <flag> To choose which function to perform
1354 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001355 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001356 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001357 [RETURNS] Nothing.
1358*/
1359{
1360 struct stat statbuf;
1361 DIR *dp;
1362 struct dirent *de;
1363 char path[STRING_LENGTH];
1364
Rob Landley1ba19d62005-10-08 17:42:35 +00001365 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001366
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001367 if((dp = opendir( dir_name))==NULL)
1368 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001369 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001370 return;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001371 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001372
1373 while ( (de = readdir (dp) ) != NULL )
1374 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001375
Eric Andersenf18bd892003-12-19 11:07:59 +00001376 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 +00001377 continue;
Eric Andersenf18bd892003-12-19 11:07:59 +00001378 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001379 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001380
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001381 if (lstat (path, &statbuf) != 0)
1382 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001383 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001384 continue;
1385 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001386 switch(type)
1387 {
1388 case SERVICE:
1389 service(statbuf,path);
1390 break;
1391 case RESTORE:
1392 restore(path, statbuf, var);
1393 break;
1394 case READ_CONFIG:
1395 read_config_file (path, var, event_mask);
1396 break;
1397 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001398 }
1399 closedir (dp);
1400} /* End Function do_scan_and_service */
1401
1402static int mksymlink (const char *oldpath, const char *newpath)
1403/* [SUMMARY] Create a symlink, creating intervening directories as required.
1404 <oldpath> The string contained in the symlink.
1405 <newpath> The name of the new symlink.
1406 [RETURNS] 0 on success, else -1.
1407*/
1408{
Rob Landley1ba19d62005-10-08 17:42:35 +00001409 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001410
1411 if ( !make_dir_tree (newpath) )
1412 return (-1);
1413
1414 if (symlink (oldpath, newpath) != 0)
1415 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001416 if (errno != EEXIST)
Eric Andersenf18bd892003-12-19 11:07:59 +00001417 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001418 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001419 return (-1);
Eric Andersenf18bd892003-12-19 11:07:59 +00001420 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001421 }
1422 return (0);
1423} /* End Function mksymlink */
1424
1425
1426static int make_dir_tree (const char *path)
1427/* [SUMMARY] Creating intervening directories for a path as required.
Eric Andersen52899692003-10-22 10:10:50 +00001428 <path> The full pathname (including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001429 [RETURNS] TRUE on success, else FALSE.
1430*/
1431{
Rob Landley1ba19d62005-10-08 17:42:35 +00001432 debug_msg_logger(LOG_INFO, __FUNCTION__);
1433
Eric Andersen0e020d12004-10-13 06:25:52 +00001434 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001435 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001436 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001437 return (FALSE);
1438 }
1439 return(TRUE);
1440} /* End Function make_dir_tree */
1441
1442static int expand_expression(char *output, unsigned int outsize,
1443 const char *input,
1444 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001445 void *info,
1446 const char *devname,
1447 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001448/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001449 <output> The output expanded expression is written here.
1450 <length> The size of the output buffer.
1451 <input> The input expression. This may equal <<output>>.
1452 <get_variable> A function which will be used to get variable values. If
1453 this returns NULL, the environment is searched instead. If this is NULL,
1454 only the environment is searched.
1455 <info> An arbitrary pointer passed to <<get_variable>>.
1456 <devname> Device name; specifically, this is the string that contains all
1457 of the regular subexpressions.
1458 <ex> Array of start / end offsets into info->devname for each subexpression
1459 <numexp> Number of regular subexpressions found in <<devname>>.
1460 [RETURNS] TRUE on success, else FALSE.
1461*/
1462{
1463 char temp[STRING_LENGTH];
1464
Rob Landley1ba19d62005-10-08 17:42:35 +00001465 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001466
1467 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
1468 return (FALSE);
1469 expand_regexp (output, outsize, temp, devname, ex, numexp);
1470 return (TRUE);
1471} /* End Function expand_expression */
1472
1473static void expand_regexp (char *output, size_t outsize, const char *input,
1474 const char *devname,
1475 const regmatch_t *ex, unsigned int numex )
1476/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1477 <output> The output expanded expression is written here.
1478 <outsize> The size of the output buffer.
1479 <input> The input expression. This may NOT equal <<output>>, because
1480 supporting that would require yet another string-copy. However, it's not
1481 hard to write a simple wrapper function to add this functionality for those
1482 few cases that need it.
1483 <devname> Device name; specifically, this is the string that contains all
1484 of the regular subexpressions.
1485 <ex> An array of start and end offsets into <<devname>>, one for each
1486 subexpression
1487 <numex> Number of subexpressions in the offset-array <<ex>>.
1488 [RETURNS] Nothing.
1489*/
1490{
1491 const char last_exp = '0' - 1 + numex;
1492 int c = -1;
1493
Rob Landley1ba19d62005-10-08 17:42:35 +00001494 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001495
1496 /* Guarantee NULL termination by writing an explicit '\0' character into
1497 the very last byte */
1498 if (outsize)
1499 output[--outsize] = '\0';
1500 /* Copy the input string into the output buffer, replacing '\\' with '\'
1501 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1502 codes are deleted */
1503 while ( (c != '\0') && (outsize != 0) )
1504 {
1505 c = *input;
1506 ++input;
1507 if (c == '\\')
1508 {
1509 c = *input;
1510 ++input;
1511 if (c != '\\')
1512 {
1513 if ((c >= '0') && (c <= last_exp))
1514 {
1515 const regmatch_t *subexp = ex + (c - '0');
1516 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1517
1518 /* Range checking */
1519 if (sublen > outsize)
1520 sublen = outsize;
1521 strncpy (output, devname + subexp->rm_so, sublen);
1522 output += sublen;
1523 outsize -= sublen;
1524 }
1525 continue;
1526 }
1527 }
1528 *output = c;
1529 ++output;
1530 --outsize;
1531 } /* while */
1532} /* End Function expand_regexp */
1533
1534
1535/* from compat_name.c */
1536
1537struct translate_struct
1538{
1539 char *match; /* The string to match to (up to length) */
1540 char *format; /* Format of output, "%s" takes data past match string,
1541 NULL is effectively "%s" (just more efficient) */
1542};
1543
1544static struct translate_struct translate_table[] =
1545{
1546 {"sound/", NULL},
1547 {"printers/", "lp%s"},
1548 {"v4l/", NULL},
1549 {"parports/", "parport%s"},
1550 {"fb/", "fb%s"},
1551 {"netlink/", NULL},
1552 {"loop/", "loop%s"},
1553 {"floppy/", "fd%s"},
1554 {"rd/", "ram%s"},
1555 {"md/", "md%s"}, /* Meta-devices */
1556 {"vc/", "tty%s"},
1557 {"misc/", NULL},
1558 {"isdn/", NULL},
1559 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1560 {"i2c/", "i2c-%s"},
1561 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1562 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1563 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1564 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1565 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1566 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1567 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1568 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1569 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1570 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1571 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1572 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1573 {"input/js", "js%s"}, /* Joystick driver */
1574 {NULL, NULL}
1575};
1576
1577const char *get_old_name (const char *devname, unsigned int namelen,
1578 char *buffer, unsigned int major, unsigned int minor)
1579/* [SUMMARY] Translate a kernel-supplied name into an old name.
1580 <devname> The device name provided by the kernel.
1581 <namelen> The length of the name.
1582 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1583 <major> The major number for the device.
1584 <minor> The minor number for the device.
1585 [RETURNS] A pointer to the old name if known, else NULL.
1586*/
1587{
1588 const char *compat_name = NULL;
1589 char *ptr;
1590 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001591 unsigned int i;
1592 char mode;
1593 int indexx;
1594 const char *pty1;
1595 const char *pty2;
1596 size_t len;
1597 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
1598 const char *fmt[] = { NULL ,
1599 "sg%u", /* scsi/generic */
1600 NULL, /* scsi/disc */
1601 "sr%u", /* scsi/cd */
1602 NULL, /* scsi/part */
1603 "nst%u%c", /* scsi/mt */
1604 "hd%c" , /* ide/host/disc */
1605 "hd%c" , /* ide/host/cd */
1606 "hd%c%s", /* ide/host/part */
1607 "%sht%d", /* ide/host/mt */
1608 "sbpcd%u", /* sbp/ */
1609 "vcs%s", /* vcc/ */
1610 "%cty%c%c", /* pty/ */
1611 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001612
Rob Landley1ba19d62005-10-08 17:42:35 +00001613 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001614
1615 for (trans = translate_table; trans->match != NULL; ++trans)
1616 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001617 len = strlen (trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001618
1619 if (strncmp (devname, trans->match, len) == 0)
1620 {
1621 if (trans->format == NULL)
1622 return (devname + len);
1623 sprintf (buffer, trans->format, devname + len);
1624 return (buffer);
1625 }
1626 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001627
Eric Andersenf18bd892003-12-19 11:07:59 +00001628 ptr = (strrchr (devname, '/') + 1);
1629 i = scan_dev_name(devname, namelen, ptr);
1630
1631 if( i > 0 && i < 13)
1632 compat_name = buffer;
1633 else
1634 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001635
Rob Landley1ba19d62005-10-08 17:42:35 +00001636 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001637
1638 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1639 if( i == 1 || i == 3 || i == 10 )
1640 sprintf (buffer, fmt[i], minor);
1641
1642 /* 2 ==scsi/disc, 4 == scsi/part */
1643 if( i == 2 || i == 4)
1644 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1645
1646 /* 5 == scsi/mt */
1647 if( i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001648 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001649 mode = ptr[2];
1650 if (mode == 'n')
1651 mode = '\0';
1652 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1653 if (devname[namelen - 1] != 'n')
1654 ++compat_name;
1655 }
1656 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1657 if( i == 6 || i == 7 || i == 8 )
Rob Landley1ba19d62005-10-08 17:42:35 +00001658 /* last arg should be ignored for i == 6 or i== 7 */
1659 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001660
1661 /* 9 == ide/host/mt */
1662 if( i == 9 )
1663 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1664
1665 /* 11 == vcc/ */
1666 if( i == 11 )
1667 {
1668 sprintf (buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001669 if (buffer[3] == '0')
1670 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001671 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001672 /* 12 == pty/ */
1673 if( i == 12 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001674 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001675 pty1 = "pqrstuvwxyzabcde";
1676 pty2 = "0123456789abcdef";
1677 indexx = atoi (devname + 5);
1678 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001679 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001680
Rob Landley1ba19d62005-10-08 17:42:35 +00001681 if(ENABLE_DEBUG && compat_name!=NULL)
1682 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001683
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001684 return (compat_name);
1685} /* End Function get_old_name */
1686
1687static char get_old_ide_name (unsigned int major, unsigned int minor)
1688/* [SUMMARY] Get the old IDE name for a device.
1689 <major> The major number for the device.
1690 <minor> The minor number for the device.
1691 [RETURNS] The drive letter.
1692*/
1693{
1694 char letter='y'; /* 121 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001695 char c='a'; /* 97 */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001696 int i=IDE0_MAJOR;
1697
Rob Landley1ba19d62005-10-08 17:42:35 +00001698 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001699
1700 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1701 do {
1702 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1703 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1704 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1705 i==IDE9_MAJOR )
1706 {
1707 if(i==major)
1708 {
1709 letter=c;
1710 break;
1711 }
1712 c+=2;
1713 }
1714 i++;
1715 } while(i<=IDE9_MAJOR);
1716
1717 if (minor > 63)
1718 ++letter;
1719 return (letter);
1720} /* End Function get_old_ide_name */
1721
1722static char *write_old_sd_name (char *buffer,
1723 unsigned int major, unsigned int minor,
1724 char *part)
1725/* [SUMMARY] Write the old SCSI disc name to a buffer.
1726 <buffer> The buffer to write to.
1727 <major> The major number for the device.
1728 <minor> The minor number for the device.
1729 <part> The partition string. Must be "" for a whole-disc entry.
1730 [RETURNS] A pointer to the buffer on success, else NULL.
1731*/
1732{
1733 unsigned int disc_index;
1734
Rob Landley1ba19d62005-10-08 17:42:35 +00001735 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001736
1737 if (major == 8)
1738 {
1739 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
1740 return (buffer);
1741 }
1742 if ( (major > 64) && (major < 72) )
1743 {
1744 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1745 if (disc_index < 26)
1746 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1747 else
1748 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
1749 return (buffer);
1750 }
1751 return (NULL);
1752} /* End Function write_old_sd_name */
1753
1754
1755/* expression.c */
1756
1757/*EXPERIMENTAL_FUNCTION*/
1758
1759int st_expr_expand (char *output, unsigned int length, const char *input,
1760 const char *(*get_variable_func) (const char *variable,
1761 void *info),
1762 void *info)
1763/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1764 <output> The output expanded expression is written here.
1765 <length> The size of the output buffer.
1766 <input> The input expression. This may equal <<output>>.
1767 <get_variable> A function which will be used to get variable values. If
1768 this returns NULL, the environment is searched instead. If this is NULL,
1769 only the environment is searched.
1770 <info> An arbitrary pointer passed to <<get_variable>>.
1771 [RETURNS] TRUE on success, else FALSE.
1772*/
1773{
1774 char ch;
1775 unsigned int len;
1776 unsigned int out_pos = 0;
1777 const char *env;
1778 const char *ptr;
1779 struct passwd *pwent;
1780 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1781
Rob Landley1ba19d62005-10-08 17:42:35 +00001782 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001783
1784 if (length > BUFFER_SIZE)
1785 length = BUFFER_SIZE;
1786 for (; TRUE; ++input)
1787 {
1788 switch (ch = *input)
1789 {
1790 case '$':
1791 /* Variable expansion */
1792 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1793 if (input == NULL)
1794 return (FALSE);
1795 break;
1796 case '~':
1797 /* Home directory expansion */
1798 ch = input[1];
1799 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1800 {
1801 /* User's own home directory: leave separator for next time */
1802 if ( ( env = getenv ("HOME") ) == NULL )
1803 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001804 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001805 return (FALSE);
1806 }
1807 len = strlen (env);
1808 if (len + out_pos >= length)
1809 goto st_expr_expand_out;
1810 memcpy (buffer + out_pos, env, len + 1);
1811 out_pos += len;
1812 continue;
1813 }
1814 /* Someone else's home directory */
1815 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1816 /* VOID */ ;
1817 len = ptr - input;
1818 if (len >= sizeof tmp)
1819 goto st_expr_expand_out;
1820 safe_memcpy (tmp, input, len);
1821 input = ptr - 1;
1822 if ( ( pwent = getpwnam (tmp) ) == NULL )
1823 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001824 msg_logger(LOG_INFO, "no pwent for: %s", tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001825 return (FALSE);
1826 }
1827 len = strlen (pwent->pw_dir);
1828 if (len + out_pos >= length)
1829 goto st_expr_expand_out;
1830 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1831 out_pos += len;
1832 break;
1833 case '\0':
1834 /* Falltrough */
1835 default:
1836 if (out_pos >= length)
1837 goto st_expr_expand_out;
1838 buffer[out_pos++] = ch;
1839 if (ch == '\0')
1840 {
1841 memcpy (output, buffer, out_pos);
1842 return (TRUE);
1843 }
1844 break;
1845 /* esac */
1846 }
1847 }
1848 return (FALSE);
1849st_expr_expand_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00001850 msg_logger(LOG_INFO, bb_msg_small_buffer);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001851 return (FALSE);
1852} /* End Function st_expr_expand */
1853
1854
1855/* Private functions follow */
1856
1857static const char *expand_variable (char *buffer, unsigned int length,
1858 unsigned int *out_pos, const char *input,
1859 const char *(*func) (const char *variable,
1860 void *info),
1861 void *info)
1862/* [SUMMARY] Expand a variable.
1863 <buffer> The buffer to write to.
1864 <length> The length of the output buffer.
1865 <out_pos> The current output position. This is updated.
1866 <input> A pointer to the input character pointer.
1867 <func> A function which will be used to get variable values. If this
1868 returns NULL, the environment is searched instead. If this is NULL, only
1869 the environment is searched.
1870 <info> An arbitrary pointer passed to <<func>>.
1871 <errfp> Diagnostic messages are written here.
1872 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1873*/
1874{
1875 char ch;
1876 int len;
1877 unsigned int open_braces;
1878 const char *env, *ptr;
1879 char tmp[STRING_LENGTH];
1880
Rob Landley1ba19d62005-10-08 17:42:35 +00001881 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001882
1883 ch = input[0];
1884 if (ch == '$')
1885 {
1886 /* Special case for "$$": PID */
1887 sprintf ( tmp, "%d", (int) getpid () );
1888 len = strlen (tmp);
1889 if (len + *out_pos >= length)
1890 goto expand_variable_out;
1891
1892 memcpy (buffer + *out_pos, tmp, len + 1);
1893 out_pos += len;
1894 return (input);
1895 }
1896 /* Ordinary variable expansion, possibly in braces */
1897 if (ch != '{')
1898 {
1899 /* Simple variable expansion */
1900 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
1901 /* VOID */ ;
1902 len = ptr - input;
1903 if (len >= sizeof tmp)
1904 goto expand_variable_out;
1905
1906 safe_memcpy (tmp, input, len);
1907 input = ptr - 1;
1908 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
1909 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001910 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001911 return (NULL);
1912 }
1913 len = strlen (env);
1914 if (len + *out_pos >= length)
1915 goto expand_variable_out;
1916
1917 memcpy (buffer + *out_pos, env, len + 1);
1918 *out_pos += len;
1919 return (input);
1920 }
1921 /* Variable in braces: check for ':' tricks */
1922 ch = *++input;
1923 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
1924 /* VOID */;
1925 if (ch == '}')
1926 {
1927 /* Must be simple variable expansion with "${var}" */
1928 len = ptr - input;
1929 if (len >= sizeof tmp)
1930 goto expand_variable_out;
1931
1932 safe_memcpy (tmp, input, len);
1933 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
1934 if (ptr == NULL)
1935 return (NULL);
1936 return (input + len);
1937 }
1938 if (ch != ':' || ptr[1] != '-' )
1939 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001940 msg_logger(LOG_INFO, "illegal char in var name");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001941 return (NULL);
1942 }
1943 /* It's that handy "${var:-word}" expression. Check if var is defined */
1944 len = ptr - input;
1945 if (len >= sizeof tmp)
1946 goto expand_variable_out;
1947
1948 safe_memcpy (tmp, input, len);
1949 /* Move input pointer to ':' */
1950 input = ptr;
1951 /* First skip to closing brace, taking note of nested expressions */
1952 ptr += 2;
1953 ch = ptr[0];
1954 for (open_braces = 1; open_braces > 0; ch = *++ptr)
1955 {
1956 switch (ch)
1957 {
1958 case '{':
1959 ++open_braces;
1960 break;
1961 case '}':
1962 --open_braces;
1963 break;
1964 case '\0':
Rob Landley1ba19d62005-10-08 17:42:35 +00001965 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001966 return (NULL);
1967 default:
1968 break;
1969 }
1970 }
1971 --ptr;
1972 /* At this point ptr should point to closing brace of "${var:-word}" */
1973 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
1974 {
1975 /* Found environment variable, so skip the input to the closing brace
1976 and return the variable */
1977 input = ptr;
1978 len = strlen (env);
1979 if (len + *out_pos >= length)
1980 goto expand_variable_out;
1981
1982 memcpy (buffer + *out_pos, env, len + 1);
1983 *out_pos += len;
1984 return (input);
1985 }
1986 /* Environment variable was not found, so process word. Advance input
1987 pointer to start of word in "${var:-word}" */
1988 input += 2;
1989 len = ptr - input;
1990 if (len >= sizeof tmp)
1991 goto expand_variable_out;
1992
1993 safe_memcpy (tmp, input, len);
1994 input = ptr;
1995 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
1996 return (NULL);
1997 len = strlen (tmp);
1998 if (len + *out_pos >= length)
1999 goto expand_variable_out;
2000
2001 memcpy (buffer + *out_pos, tmp, len + 1);
2002 *out_pos += len;
2003 return (input);
2004expand_variable_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00002005 msg_logger(LOG_INFO, bb_msg_small_buffer);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002006 return (NULL);
2007} /* End Function expand_variable */
2008
2009
2010static const char *get_variable_v2 (const char *variable,
2011 const char *(*func) (const char *variable, void *info),
2012 void *info)
2013/* [SUMMARY] Get a variable from the environment or .
2014 <variable> The variable name.
2015 <func> A function which will be used to get the variable. If this returns
2016 NULL, the environment is searched instead. If this is NULL, only the
2017 environment is searched.
2018 [RETURNS] The value of the variable on success, else NULL.
2019*/
2020{
2021 const char *value;
2022
Rob Landley1ba19d62005-10-08 17:42:35 +00002023 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002024
2025 if (func != NULL)
2026 {
2027 value = (*func) (variable, info);
2028 if (value != NULL)
2029 return (value);
2030 }
2031 return getenv (variable);
2032} /* End Function get_variable */
2033
2034/* END OF CODE */