blob: 34945c7b2e763c3fa0a88bf1a4dcb8fc8873383e [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
52#include "libbb.h"
53#include "busybox.h"
54#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>
71#include <regex.h>
72#include <errno.h>
Eric Andersen2af70002003-10-09 21:02:23 +000073#include <sys/sysmacros.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000074
Eric Andersen2af70002003-10-09 21:02:23 +000075
76/* Various defines taken from linux/major.h */
77#define IDE0_MAJOR 3
78#define IDE1_MAJOR 22
79#define IDE2_MAJOR 33
80#define IDE3_MAJOR 34
81#define IDE4_MAJOR 56
82#define IDE5_MAJOR 57
83#define IDE6_MAJOR 88
84#define IDE7_MAJOR 89
85#define IDE8_MAJOR 90
86#define IDE9_MAJOR 91
87
88
89/* Various defines taken from linux/devfs_fs.h */
90#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
91#define DEVFSD_IOCTL_BASE 'd'
92/* These are the various ioctls */
93#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
94#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
95#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000096#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +000097#define DEVFSD_NOTIFY_REGISTERED 0
98#define DEVFSD_NOTIFY_UNREGISTERED 1
99#define DEVFSD_NOTIFY_ASYNC_OPEN 2
100#define DEVFSD_NOTIFY_CLOSE 3
101#define DEVFSD_NOTIFY_LOOKUP 4
102#define DEVFSD_NOTIFY_CHANGE 5
103#define DEVFSD_NOTIFY_CREATE 6
104#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000105#define DEVFS_PATHLEN 1024
106/* Never change this otherwise the binary interface will change */
107
Eric Andersen2af70002003-10-09 21:02:23 +0000108struct devfsd_notify_struct
109{ /* Use native C types to ensure same types in kernel and user space */
110 unsigned int type; /* DEVFSD_NOTIFY_* value */
111 unsigned int mode; /* Mode of the inode or device entry */
112 unsigned int major; /* Major number of device entry */
113 unsigned int minor; /* Minor number of device entry */
114 unsigned int uid; /* Uid of process, inode or device entry */
115 unsigned int gid; /* Gid of process, inode or device entry */
116 unsigned int overrun_count; /* Number of lost events */
117 unsigned int namelen; /* Number of characters not including '\0' */
118 /* The device name MUST come last */
119 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
120};
121
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000122#define BUFFER_SIZE 16384
123#define DEVFSD_VERSION "1.3.25"
124#define CONFIG_FILE "/etc/devfsd.conf"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000125#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000126#define MODPROBE_SWITCH_1 "-k"
127#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000128#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000129#define MAX_ARGS (6 + 1)
130#define MAX_SUBEXPR 10
131#define STRING_LENGTH 255
132
133/* for get_uid_gid() */
134#define UID 0
135#define GID 1
136
137/* for msg_logger(), do_ioctl(),
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000138 fork_and_execute() */
139# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000140# define NO_DIE 0
141
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000142/* for dir_operation() */
143#define RESTORE 0
144#define SERVICE 1
145#define READ_CONFIG 2
146
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000147/* Update only after changing code to reflect new protocol */
148#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
149
150/* Compile-time check */
151#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
152#error protocol version mismatch. Update your kernel headers
153#endif
154
155#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000156#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000157#define AC_EXECUTE 2
158#define AC_MFUNCTION 3 /* not supported by busybox */
159#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000160#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000161#define AC_IGNORE 6
162#define AC_MKOLDCOMPAT 7
163#define AC_MKNEWCOMPAT 8
164#define AC_RMOLDCOMPAT 9
165#define AC_RMNEWCOMPAT 10
166#define AC_RESTORE 11
167
168
169struct permissions_type
170{
171 mode_t mode;
172 uid_t uid;
173 gid_t gid;
174};
175
176struct execute_type
177{
178 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
179};
180
181struct copy_type
182{
183 const char *source;
184 const char *destination;
185};
186
187struct action_type
188{
189 unsigned int what;
190 unsigned int when;
191};
192
193struct config_entry_struct
194{
195 struct action_type action;
196 regex_t preg;
197 union
198 {
199 struct permissions_type permissions;
200 struct execute_type execute;
201 struct copy_type copy;
202 }
203 u;
204 struct config_entry_struct *next;
205};
206
207struct get_variable_info
208{
209 const struct devfsd_notify_struct *info;
210 const char *devname;
211 char devpath[STRING_LENGTH];
212};
213
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000214static void dir_operation(int , const char * , int, unsigned long* );
215static void service(struct stat statbuf, char *path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000216static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
217static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
218static int mksymlink (const char *oldpath, const char *newpath);
Eric Andersenf18bd892003-12-19 11:07:59 +0000219static void read_config_file (char *path, int optional, unsigned long *event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000220static void process_config_line (const char *, unsigned long *);
221static int do_servicing (int, unsigned long);
222static void service_name (const struct devfsd_notify_struct *);
223static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
224static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
225 const regmatch_t *, unsigned);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000226#ifdef CONFIG_DEVFSD_MODLOAD
227static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
228#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000229static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
230 const regmatch_t *, unsigned);
231static void action_compat (const struct devfsd_notify_struct *, unsigned);
232static void free_config (void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000233static void restore(char *spath, struct stat source_stat, int rootlen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000234static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
235static mode_t get_mode (const char *);
236static void signal_handler (int);
237static const char *get_variable (const char *, void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000238static int make_dir_tree (const char *);
239static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
240 const char *, const regmatch_t *, unsigned );
241static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
242static const char *expand_variable( char *, unsigned, unsigned *, const char *,
243 const char *(*) (const char *, void *), void * );
244static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
245static char get_old_ide_name (unsigned , unsigned);
246static char *write_old_sd_name (char *, unsigned, unsigned, char *);
247
248/* busybox functions */
Glenn L McGrathfa134cd2004-02-22 07:38:36 +0000249#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000250static void msg_logger(int die, int pri, const char * fmt, ... );
Eric Andersenf18bd892003-12-19 11:07:59 +0000251#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000252static void do_ioctl(int die, int fd, int request, unsigned long event_mask_flag);
253static void fork_and_execute(int die, char *arg0, char **arg );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000254static int get_uid_gid ( int, const char *);
255static void safe_memcpy( char * dest, const char * src, int len);
Eric Andersenf18bd892003-12-19 11:07:59 +0000256static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
257static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000258
259/* Structs and vars */
260static struct config_entry_struct *first_config = NULL;
261static struct config_entry_struct *last_config = NULL;
262static const char *mount_point = NULL;
263static volatile int caught_signal = FALSE;
264static volatile int caught_sighup = FALSE;
265static struct initial_symlink_struct
266{
267 char *dest;
268 char *name;
269} initial_symlinks[] =
270{
271 {"/proc/self/fd", "fd"},
272 {"fd/0", "stdin"},
273 {"fd/1", "stdout"},
274 {"fd/2", "stderr"},
275 {NULL, NULL},
276};
277
278static struct event_type
279{
280 unsigned int type; /* The DEVFSD_NOTIFY_* value */
281 const char *config_name; /* The name used in the config file */
282} event_types[] =
283{
284 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
285 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
286 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
287 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
288 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
289 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
290 {DEVFSD_NOTIFY_CREATE, "CREATE"},
291 {DEVFSD_NOTIFY_DELETE, "DELETE"},
292 {0xffffffff, NULL}
293};
294
295/* busybox functions and messages */
296
297extern void xregcomp(regex_t * preg, const char *regex, int cflags);
298
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000299const char * const bb_msg_proto_rev = "protocol revision";
300#ifdef CONFIG_DEVFSD_VERBOSE
Eric Andersenf18bd892003-12-19 11:07:59 +0000301const char * const bb_msg_bad_config = "bad %s config file: %s\n";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000302const char * const bb_msg_small_buffer = "buffer too small\n";
Eric Andersenf18bd892003-12-19 11:07:59 +0000303const char * const bb_msg_variable_not_found = "variable: %s not found\n";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000304#endif
305
Glenn L McGrathfa134cd2004-02-22 07:38:36 +0000306#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000307static void msg_logger(int die, int pri, const char * fmt, ... )
308{
309 va_list ap;
310
311 va_start(ap, fmt);
312 if (access ("/dev/log", F_OK) == 0)
313 {
314 openlog(bb_applet_name, 0, LOG_DAEMON);
315 vsyslog( pri , fmt , ap);
316 closelog();
317 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000318#ifndef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000319 else
320#endif
321 bb_verror_msg(fmt, ap);
322 va_end(ap);
323 if(die==DIE)
324 exit(EXIT_FAILURE);
325}
Eric Andersenf18bd892003-12-19 11:07:59 +0000326#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000327
328static void do_ioctl(int die, int fd, int request, unsigned long event_mask_flag)
329{
Eric Andersenf18bd892003-12-19 11:07:59 +0000330#ifdef CONFIG_DEVFSD_VERBOSE
331 if (ioctl (fd, request, event_mask_flag) == -1)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000332 msg_logger(die, LOG_ERR, "ioctl(): %m\n");
Eric Andersenf18bd892003-12-19 11:07:59 +0000333#else
334 if (ioctl (fd, request, event_mask_flag) == -1)
335 exit(EXIT_FAILURE);
336#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000337}
338
339static void fork_and_execute(int die, char *arg0, char **arg )
340{
341 switch ( fork () )
342 {
343 case 0:
344 /* Child */
345 break;
346 case -1:
347 /* Parent: Error : die or return */
Eric Andersenf18bd892003-12-19 11:07:59 +0000348#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000349 msg_logger(die, LOG_ERR,(char *) bb_msg_memory_exhausted);
Eric Andersenf18bd892003-12-19 11:07:59 +0000350#else
351 if(die == DIE)
352 exit(EXIT_FAILURE);
353#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000354 return;
355 default:
356 /* Parent : ok : return or exit */
357 if(arg0 != NULL)
358 {
359 wait (NULL);
360 return;
361 }
362 exit (EXIT_SUCCESS);
363 }
364 /* Child : if arg0 != NULL do execvp */
365 if(arg0 != NULL )
366 {
367 execvp (arg0, arg);
Eric Andersenf18bd892003-12-19 11:07:59 +0000368#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000369 msg_logger(DIE, LOG_ERR, "execvp(): %s: %m\n", arg0);
Eric Andersenf18bd892003-12-19 11:07:59 +0000370#else
371 exit(EXIT_FAILURE);
372#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000373 }
374}
375
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000376static void safe_memcpy( char *dest, const char *src, int len)
377{
378 memcpy (dest , src , len );
379 dest[len] = '\0';
380}
381
Eric Andersenf18bd892003-12-19 11:07:59 +0000382static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
383{
384 if( d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
385 return (2 + addendum);
386 else if( d[n - 2]=='c' && d[n - 1]=='d')
387 return (3 + addendum);
388 else if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
389 return (4 + addendum);
390 else if( ptr[n - 2]=='m' && ptr[n - 1]=='t')
391 return (5 + addendum);
392 else
393 return 0;
394}
395
396static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
397{
398 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/')
399 {
400 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
401 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
402 d[n - 1]=='c' )
403 return 1;
404 return scan_dev_name_common(d, n, 0, ptr);
405 }
406 else if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
407 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
408 {
409 return scan_dev_name_common(d, n, 4, ptr);
410 }
411 else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
412 {
413 return 10;
414 }
415 else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
416 {
417 return 11;
418 }
419 else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
420 {
421 return 12;
422 }
423 return 0;
424}
425
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000426/* Public functions follow */
427
428int devfsd_main (int argc, char **argv)
429{
430 int print_version = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000431#ifdef CONFIG_DEVFSD_FG_NP
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000432 int do_daemon = TRUE;
433 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000434#endif
435 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000436 int fd, proto_rev, count;
437 unsigned long event_mask = 0;
438 struct sigaction new_action;
439 struct initial_symlink_struct *curr;
440
441 if (argc < 2)
442 bb_show_usage();
443
444 for (count = 2; count < argc; ++count)
445 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000446 if(argv[count][0] == '-')
447 {
448 if(argv[count][1]=='v' && !argv[count][2]) /* -v */
449 print_version = TRUE;
450#ifdef CONFIG_DEVFSD_FG_NP
451 else if(argv[count][1]=='f' && argv[count][2]=='g' && !argv[count][3]) /* -fg */
452 do_daemon = FALSE;
453 else if(argv[count][1]=='n' && argv[count][2]=='p' && !argv[count][3]) /* -np */
454 no_polling = TRUE;
455#endif
456 else
457 bb_show_usage();
458 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000459 }
460
Eric Andersenf18bd892003-12-19 11:07:59 +0000461 /* strip last / from mount point, so we don't need to check for it later */
462 while( argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
463 argv[1][strlen(argv[1]) -1] = '\0';
464
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000465 mount_point = argv[1];
466
467 if (chdir (mount_point) != 0)
Eric Andersenf18bd892003-12-19 11:07:59 +0000468#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000469 bb_error_msg_and_die( " %s: %m", mount_point);
Eric Andersenf18bd892003-12-19 11:07:59 +0000470#else
471 exit(EXIT_FAILURE);
472#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000473
474 fd = bb_xopen (".devfsd", O_RDONLY);
475
476 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
Eric Andersenf18bd892003-12-19 11:07:59 +0000477#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000478 bb_error_msg( "FD_CLOEXEC");
Eric Andersenf18bd892003-12-19 11:07:59 +0000479#else
480 exit(EXIT_FAILURE);
481#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000482
483 do_ioctl(DIE, fd, DEVFSDIOC_GET_PROTO_REV,(int )&proto_rev);
484
485 /*setup initial entries */
486 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
487 symlink (curr->dest, curr->name);
488
489 /* NB: The check for CONFIG_FILE is done in read_config_file() */
490
491 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
492 {
493 bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
494 bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
495 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
496 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
497 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
498 exit(EXIT_SUCCESS); /* -v */
499 }
500 /* Tell kernel we are special (i.e. we get to see hidden entries) */
501 do_ioctl(DIE, fd, DEVFSDIOC_SET_EVENT_MASK, 0);
502
503 sigemptyset (&new_action.sa_mask);
504 new_action.sa_flags = 0;
505
506 /* Set up SIGHUP and SIGUSR1 handlers */
507 new_action.sa_handler = signal_handler;
508 if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
Eric Andersenf18bd892003-12-19 11:07:59 +0000509#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000510 bb_error_msg_and_die( "sigaction()");
Eric Andersenf18bd892003-12-19 11:07:59 +0000511#else
512 exit(EXIT_FAILURE);
513#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000514
515 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
516
517 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
518 umask (0);
519 read_config_file (CONFIG_FILE, FALSE, &event_mask);
520 /* Do the scan before forking, so that boot scripts see the finished product */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000521 dir_operation(SERVICE,mount_point,0,NULL);
Eric Andersenf18bd892003-12-19 11:07:59 +0000522#ifdef CONFIG_DEVFSD_FG_NP
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000523 if (no_polling)
524 exit (0);
525 if (do_daemon)
526 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000527#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000528 /* Release so that the child can grab it */
529 do_ioctl(DIE, fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
530 fork_and_execute(DIE, NULL, NULL);
531 setsid (); /* Prevent hangups and become pgrp leader */
Eric Andersenf18bd892003-12-19 11:07:59 +0000532#ifdef CONFIG_DEVFSD_FG_NP
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000533 }
534 else
535 setpgid (0, 0); /* Become process group leader */
Eric Andersenf18bd892003-12-19 11:07:59 +0000536#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000537
538 while (TRUE)
539 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000540 do_scan = do_servicing (fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000541
542 free_config ();
543 read_config_file (CONFIG_FILE, FALSE, &event_mask);
544 if (do_scan)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000545 dir_operation(SERVICE,mount_point,0,NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000546 }
547} /* End Function main */
548
549
550/* Private functions follow */
551
Eric Andersenf18bd892003-12-19 11:07:59 +0000552static void read_config_file (char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000553/* [SUMMARY] Read a configuration database.
554 <path> The path to read the database from. If this is a directory, all
555 entries in that directory will be read (except hidden entries).
556 <optional> If TRUE, the routine will silently ignore a missing config file.
557 <event_mask> The event mask is written here. This is not initialised.
558 [RETURNS] Nothing.
559*/
560{
561 struct stat statbuf;
562 FILE *fp;
563 char buf[STRING_LENGTH];
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000564 char *line=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000565
Eric Andersenf18bd892003-12-19 11:07:59 +0000566#ifdef CONFIG_DEBUG
567 msg_logger( NO_DIE, LOG_INFO, "read_config_file(): %s\n", path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000568#endif
Rob Landley06813d02005-06-07 03:47:00 +0000569 if (stat (path, &statbuf) == 0 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000570 {
Rob Landley06813d02005-06-07 03:47:00 +0000571 /* Don't read 0 length files: ignored */
572 /*if( statbuf.st_size == 0 )
573 return;*/
574 if ( S_ISDIR (statbuf.st_mode) )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000575 {
Rob Landley06813d02005-06-07 03:47:00 +0000576 /* strip last / from dirname so we don't need to check for it later */
577 while( path && path[1]!='\0' && path[strlen(path)-1] == '/')
578 path[strlen(path) -1] = '\0';
579
580 dir_operation(READ_CONFIG, path, 0, event_mask);
581 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000582 }
Rob Landley06813d02005-06-07 03:47:00 +0000583 if ( ( fp = fopen (path, "r") ) != NULL )
584 {
585 while (fgets (buf, STRING_LENGTH, fp) != NULL)
586 {
587 /* Skip whitespace */
588 for (line = buf; isspace (*line); ++line)
589 /*VOID*/;
590 if (line[0] == '\0' || line[0] == '#' )
591 continue;
592 process_config_line (line, event_mask);
593 }
594 fclose (fp);
595 } else {
596 goto read_config_file_err;
597 }
598 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000599read_config_file_err:
600#ifdef CONFIG_DEVFSD_VERBOSE
601 msg_logger(((optional == 0 ) && (errno == ENOENT))? DIE : NO_DIE, LOG_ERR, "read config file: %s: %m\n", path);
Eric Andersenf18bd892003-12-19 11:07:59 +0000602#else
603 if(optional == 0 && errno == ENOENT)
604 exit(EXIT_FAILURE);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000605#endif
Rob Landley06813d02005-06-07 03:47:00 +0000606 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000607 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000608} /* End Function read_config_file */
609
610static void process_config_line (const char *line, unsigned long *event_mask)
611/* [SUMMARY] Process a line from a configuration file.
612 <line> The configuration line.
613 <event_mask> The event mask is written here. This is not initialised.
614 [RETURNS] Nothing.
615*/
616{
617 int num_args, count;
618 struct config_entry_struct *new;
619 char p[MAX_ARGS][STRING_LENGTH];
620 char when[STRING_LENGTH], what[STRING_LENGTH];
621 char name[STRING_LENGTH];
622 char * msg="";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000623 char *ptr;
624
Eric Andersenf18bd892003-12-19 11:07:59 +0000625 /* !!!! Only Uppercase Keywords in devsfd.conf */
626 const char *options[] = { "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
627 "PERMISSIONS", "MODLOAD", "EXECUTE", "COPY", "IGNORE",
628 "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT", 0 };
629
630 short int i;
631
632#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000633 msg_logger( NO_DIE, LOG_INFO, "process_config_line()\n");
634#endif
635
636 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
637 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
638 when, name, what,
639 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000640
641 i = compare_string_array(options, when );
642
643 /*"CLEAR_CONFIG"*/
644 if( i == 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000645 {
646 free_config ();
647 *event_mask = 0;
648 return;
649 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000650
651 if ( num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000652 goto process_config_line_err;
653
Eric Andersenf18bd892003-12-19 11:07:59 +0000654 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
655 if( i == 1 || i == 2 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000656 {
657 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
Eric Andersenf18bd892003-12-19 11:07:59 +0000658#ifdef CONFIG_DEBUG
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000659 msg_logger( NO_DIE, LOG_INFO, "%sinclude: %s\n",(toupper (when[0]) == 'I') ? "": "optional_", name);
660#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000661 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
662 return;
663 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000664 /* "RESTORE" */
665 if( i == 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000666 {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000667 dir_operation(RESTORE,name, strlen (name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000668 return;
669 }
670 if (num_args < 3)
671 goto process_config_line_err;
672
673 new = xmalloc (sizeof *new);
674 memset (new, 0, sizeof *new);
675
676 for (count = 0; event_types[count].config_name != NULL; ++count)
677 {
678 if (strcasecmp (when, event_types[count].config_name) != 0)
679 continue;
680 new->action.when = event_types[count].type;
681 break;
682 }
683 if (event_types[count].config_name == NULL)
684 {
685 msg="WHEN in";
686 goto process_config_line_err;
687 }
688
Eric Andersenf18bd892003-12-19 11:07:59 +0000689 i = compare_string_array(options, what );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000690
Eric Andersenf18bd892003-12-19 11:07:59 +0000691 switch(i)
692 {
693 case 4: /* "PERMISSIONS" */
694 new->action.what = AC_PERMISSIONS;
695 /* Get user and group */
696 if ( ( ptr = strchr (p[0], '.') ) == NULL )
697 {
698 msg="UID.GID";
699 goto process_config_line_err; /*"missing '.' in UID.GID */
700 }
701
702 *ptr++ = '\0';
703 new->u.permissions.uid = get_uid_gid (UID, p[0]);
704 new->u.permissions.gid = get_uid_gid (GID, ptr);
705 /* Get mode */
706 new->u.permissions.mode = get_mode (p[1]);
707 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000708#ifdef CONFIG_DEVFSD_MODLOAD
Eric Andersenf18bd892003-12-19 11:07:59 +0000709 case 5: /* MODLOAD */
710 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
711 the device name) to the module loading facility. In addition,
712 the /etc/modules.devfs configuration file is used.*/
713 new->action.what = AC_MODLOAD;
714 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000715#endif
Eric Andersenf18bd892003-12-19 11:07:59 +0000716 case 6: /* EXECUTE */
717 new->action.what = AC_EXECUTE;
718 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000719
Eric Andersenf18bd892003-12-19 11:07:59 +0000720 for (count = 0; count < num_args; ++count)
721 new->u.execute.argv[count] = bb_xstrdup (p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000722
Eric Andersenf18bd892003-12-19 11:07:59 +0000723 new->u.execute.argv[num_args] = NULL;
724 break;
725 case 7: /* COPY */
726 new->action.what = AC_COPY;
727 num_args -= 3;
728 if (num_args != 2)
729 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000730
Eric Andersenf18bd892003-12-19 11:07:59 +0000731 new->u.copy.source = bb_xstrdup (p[0]);
732 new->u.copy.destination = bb_xstrdup (p[1]);
733 break;
734 case 8: /* IGNORE */
735 /* FALLTROUGH */
736 case 9: /* MKOLDCOMPAT */
737 /* FALLTROUGH */
738 case 10: /* MKNEWCOMPAT */
739 /* FALLTROUGH */
740 case 11:/* RMOLDCOMPAT */
741 /* FALLTROUGH */
742 case 12: /* RMNEWCOMPAT */
743 /* AC_IGNORE 6
744 AC_MKOLDCOMPAT 7
745 AC_MKNEWCOMPAT 8
746 AC_RMOLDCOMPAT 9
747 AC_RMNEWCOMPAT 10*/
748 new->action.what = i - 2;
749 break;
750 default:
751 msg ="WHAT in";
752 goto process_config_line_err;
753 /*esac*/
754 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000755
756 xregcomp( &new->preg, name, REG_EXTENDED);
757
758 *event_mask |= 1 << new->action.when;
759 new->next = NULL;
760 if (first_config == NULL)
761 first_config = new;
762 else
763 last_config->next = new;
764 last_config = new;
765 return;
766process_config_line_err:
Eric Andersenf18bd892003-12-19 11:07:59 +0000767#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000768 msg_logger( DIE, LOG_ERR, bb_msg_bad_config, msg , line);
Eric Andersenf18bd892003-12-19 11:07:59 +0000769#else
770 exit(EXIT_FAILURE);
771#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000772} /* End Function process_config_line */
773
774static int do_servicing (int fd, unsigned long event_mask)
775/* [SUMMARY] Service devfs changes until a signal is received.
776 <fd> The open control file.
777 <event_mask> The event mask.
778 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
779*/
780{
781 ssize_t bytes;
782 struct devfsd_notify_struct info;
783 unsigned long tmp_event_mask;
784
Eric Andersenf18bd892003-12-19 11:07:59 +0000785#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000786 msg_logger( NO_DIE, LOG_INFO, "do_servicing()\n");
787#endif
788 /* Tell devfs what events we care about */
789 tmp_event_mask = event_mask;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000790 do_ioctl(DIE, fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
791 while (!caught_signal)
792 {
793 errno = 0;
794 bytes = read (fd, (char *) &info, sizeof info);
795 if (caught_signal)
796 break; /* Must test for this first */
797 if (errno == EINTR)
798 continue; /* Yes, the order is important */
799 if (bytes < 1)
800 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000801 service_name (&info);
802 }
803 if (caught_signal)
804 {
805 int c_sighup = caught_sighup;
806
807 caught_signal = FALSE;
808 caught_sighup = FALSE;
809 return (c_sighup);
810 }
811#ifdef CONFIG_DEVFSD_VERBOSE
812 msg_logger( NO_DIE, LOG_ERR, "read error on control file: %m\n");
813#endif
814 /* This is to shut up a compiler warning */
Eric Andersenf18bd892003-12-19 11:07:59 +0000815 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000816} /* End Function do_servicing */
817
818static void service_name (const struct devfsd_notify_struct *info)
819/* [SUMMARY] Service a single devfs change.
820 <info> The devfs change.
821 [RETURNS] Nothing.
822*/
823{
824 unsigned int n;
825 regmatch_t mbuf[MAX_SUBEXPR];
826 struct config_entry_struct *entry;
827
Eric Andersenf18bd892003-12-19 11:07:59 +0000828#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000829 msg_logger( NO_DIE, LOG_INFO, "service_name()\n");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000830 if (info->overrun_count > 0)
831 msg_logger( NO_DIE, LOG_ERR, "lost %u events\n", info->overrun_count);
832#endif
833
834 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Eric Andersenf18bd892003-12-19 11:07:59 +0000835 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
836 ((info->devname[0]=='l' && info->devname[1]=='o' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000837 info->devname[2]=='g' && !info->devname[3]) ||
Eric Andersenf18bd892003-12-19 11:07:59 +0000838 ( info->devname[0]=='i' && info->devname[1]=='n' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000839 info->devname[2]=='i' && info->devname[3]=='t' &&
840 info->devname[4]=='c' && info->devname[5]=='t' &&
841 info->devname[6]=='l' && !info->devname[7])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000842 return;
843 for (entry = first_config; entry != NULL; entry = entry->next)
844 {
845 /* First check if action matches the type, then check if name matches */
846 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
847 continue;
848 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
849 /* VOID */;
Eric Andersenf18bd892003-12-19 11:07:59 +0000850#ifdef CONFIG_DEBUG
851 msg_logger( NO_DIE, LOG_INFO, "service_name(): action.what %d\n", entry->action.what);
852#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000853 switch (entry->action.what)
854 {
855 case AC_PERMISSIONS:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000856 action_permissions (info, entry);
857 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000858#ifdef CONFIG_DEVFSD_MODLOAD
859 case AC_MODLOAD:
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000860 action_modload (info, entry);
861 break;
862#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000863 case AC_EXECUTE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000864 action_execute (info, entry, mbuf, n);
865 break;
866 case AC_COPY:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000867 action_copy (info, entry, mbuf, n);
868 break;
869 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000870 return;
871 /*break;*/
872 case AC_MKOLDCOMPAT:
873 case AC_MKNEWCOMPAT:
874 case AC_RMOLDCOMPAT:
875 case AC_RMNEWCOMPAT:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000876 action_compat (info, entry->action.what);
877 break;
878 default:
Eric Andersenf18bd892003-12-19 11:07:59 +0000879#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000880 msg_logger( DIE, LOG_ERR, "Unknown action\n");
Eric Andersenf18bd892003-12-19 11:07:59 +0000881#else
882 exit(EXIT_FAILURE);
883#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000884 /*break;*/
885 }
886 }
887} /* End Function service_name */
888
889static void action_permissions (const struct devfsd_notify_struct *info,
890 const struct config_entry_struct *entry)
891/* [SUMMARY] Update permissions for a device entry.
892 <info> The devfs change.
893 <entry> The config file entry.
894 [RETURNS] Nothing.
895*/
896{
897 struct stat statbuf;
898
Eric Andersenf18bd892003-12-19 11:07:59 +0000899#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000900 msg_logger( NO_DIE, LOG_INFO, "action_permission()\n");
901#endif
902
903 if ( stat (info->devname, &statbuf) != 0 ||
904 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
905 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
906 {
907#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000908 msg_logger( NO_DIE, LOG_ERR, "chmod() or chown(): %s: %m\n",info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000909#endif
910 return;
911 }
912
913} /* End Function action_permissions */
914
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000915#ifdef CONFIG_DEVFSD_MODLOAD
916static void action_modload (const struct devfsd_notify_struct *info,
917 const struct config_entry_struct *entry)
918/* [SUMMARY] Load a module.
919 <info> The devfs change.
920 <entry> The config file entry.
921 [RETURNS] Nothing.
922*/
923{
924 char *argv[6];
925 char device[STRING_LENGTH];
926
927 argv[0] = MODPROBE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000928 argv[1] = MODPROBE_SWITCH_1; /* "-k" */
929 argv[2] = MODPROBE_SWITCH_2; /* "-C" */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000930 argv[3] = CONFIG_MODULES_DEVFS;
931 argv[4] = device;
932 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000933
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000934 snprintf (device, sizeof (device), "/dev/%s", info->devname);
Eric Andersenf18bd892003-12-19 11:07:59 +0000935 #ifdef CONFIG_DEBUG
936 msg_logger( NO_DIE, LOG_INFO, "action_modload():%s %s %s %s %s\n",argv[0],argv[1],argv[2],argv[3],argv[4]);
937 #endif
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000938 fork_and_execute(DIE, argv[0], argv);
939} /* End Function action_modload */
940#endif
941
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000942static void action_execute (const struct devfsd_notify_struct *info,
943 const struct config_entry_struct *entry,
944 const regmatch_t *regexpr, unsigned int numexpr)
945/* [SUMMARY] Execute a programme.
946 <info> The devfs change.
947 <entry> The config file entry.
948 <regexpr> The number of subexpression (start, end) offsets within the
949 device name.
950 <numexpr> The number of elements within <<regexpr>>.
951 [RETURNS] Nothing.
952*/
953{
954 unsigned int count;
955 struct get_variable_info gv_info;
956 char *argv[MAX_ARGS + 1];
957 char largv[MAX_ARGS + 1][STRING_LENGTH];
958
Eric Andersenf18bd892003-12-19 11:07:59 +0000959#ifdef CONFIG_DEBUG
Tim Riker6fe55ae2003-10-17 18:54:55 +0000960 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000961 char buff[512];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000962#endif
963
964 gv_info.info = info;
965 gv_info.devname = info->devname;
966 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
967 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
968 {
969 expand_expression (largv[count], STRING_LENGTH,
970 entry->u.execute.argv[count],
971 get_variable, &gv_info,
972 gv_info.devname, regexpr, numexpr );
973 argv[count] = largv[count];
974 }
975 argv[count] = NULL;
976
Eric Andersenf18bd892003-12-19 11:07:59 +0000977#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000978 buff[0]='\0';
979 for(i=0;argv[i]!=NULL;i++) /* argv[i] < MAX_ARGS + 1 */
980 {
981 strcat(buff," ");
Eric Andersenf18bd892003-12-19 11:07:59 +0000982 if( (strlen(buff)+ 1 + strlen(argv[i])) >= 512)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000983 break;
984 strcat(buff,argv[i]);
985 }
986 strcat(buff,"\n");
987 msg_logger( NO_DIE, LOG_INFO, "action_execute(): %s",buff);
988#endif
989
990 fork_and_execute(NO_DIE, argv[0], argv);
991} /* End Function action_execute */
992
993
994static void action_copy (const struct devfsd_notify_struct *info,
995 const struct config_entry_struct *entry,
996 const regmatch_t *regexpr, unsigned int numexpr)
997/* [SUMMARY] Copy permissions.
998 <info> The devfs change.
999 <entry> The config file entry.
1000 <regexpr> This list of subexpression (start, end) offsets within the
1001 device name.
1002 <numexpr> The number of elements in <<regexpr>>.
1003 [RETURNS] Nothing.
1004*/
1005{
1006 mode_t new_mode;
1007 struct get_variable_info gv_info;
1008 struct stat source_stat, dest_stat;
1009 char source[STRING_LENGTH], destination[STRING_LENGTH];
1010 dest_stat.st_mode = 0;
1011
Eric Andersenf18bd892003-12-19 11:07:59 +00001012#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001013 msg_logger( NO_DIE, LOG_INFO, "action_copy()\n");
1014#endif
1015
1016 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
1017 return;
1018 gv_info.info = info;
1019 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001020
Eric Andersenf18bd892003-12-19 11:07:59 +00001021 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001022 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
1023 get_variable, &gv_info, gv_info.devname,
1024 regexpr, numexpr);
1025
1026 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
1027 get_variable, &gv_info, gv_info.devname,
1028 regexpr, numexpr);
1029
1030 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001031 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001032 lstat (destination, &dest_stat);
1033 new_mode = source_stat.st_mode & ~S_ISVTX;
1034 if (info->type == DEVFSD_NOTIFY_CREATE)
1035 new_mode |= S_ISVTX;
1036 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
1037 new_mode |= S_ISVTX;
Eric Andersenf18bd892003-12-19 11:07:59 +00001038#ifdef CONFIG_DEBUG
1039 if ( !copy_inode (destination, &dest_stat, new_mode, source, &source_stat) && (errno != EEXIST))
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001040 msg_logger( NO_DIE, LOG_ERR, "copy_inode(): %s to %s: %m\n", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001041#else
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001042 copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001043#endif
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001044 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001045} /* End Function action_copy */
1046
Eric Andersenf18bd892003-12-19 11:07:59 +00001047static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001048/* [SUMMARY] Process a compatibility request.
1049 <info> The devfs change.
1050 <action> The action to take.
1051 [RETURNS] Nothing.
1052*/
1053{
1054 const char *compat_name = NULL;
1055 const char *dest_name = info->devname;
Eric Andersenf18bd892003-12-19 11:07:59 +00001056 char *ptr=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001057 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001058 int mode, host, bus, target, lun;
1059 unsigned int i;
1060 char rewind_;
1061 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
1062 const char *fmt[] = { NULL ,
1063 "sg/c%db%dt%du%d", /* scsi/generic */
1064 "sd/c%db%dt%du%d", /* scsi/disc */
1065 "sr/c%db%dt%du%d", /* scsi/cd */
1066 "sd/c%db%dt%du%dp%d", /* scsi/part */
1067 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
1068 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
1069 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
1070 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
1071 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
1072 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001073
1074 /* First construct compatibility name */
1075 switch (action)
1076 {
1077 case AC_MKOLDCOMPAT:
1078 case AC_RMOLDCOMPAT:
1079 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1080 break;
1081 case AC_MKNEWCOMPAT:
1082 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001083 ptr = strrchr (info->devname, '/') + 1;
1084 i=scan_dev_name(info->devname, info->namelen, ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001085
Eric Andersenf18bd892003-12-19 11:07:59 +00001086#ifdef CONFIG_DEBUG
1087 msg_logger( NO_DIE, LOG_INFO, "action_compat(): scan_dev_name() returned %d\n", i);
1088#endif
1089
1090 /* nothing found */
1091 if(i==0 || i > 9)
1092 return;
1093
1094 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1095 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001096 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +00001097 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001098
Eric Andersenf18bd892003-12-19 11:07:59 +00001099
1100 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1101 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1102 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1103
1104 /* 4 == scsi/part 8 == ide/host/part */
1105 if( i == 4 || i == 8)
1106 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1107
1108 /* 5 == scsi/mt */
1109 if( i == 5)
1110 {
1111 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001112 if (rewind_ != 'n')
1113 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001114 mode=0;
1115 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1116 mode = ptr[2] - 107; /* 1 or 2 */
1117 if(ptr[2] == 'a')
1118 mode = 3;
1119 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001120 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001121
Eric Andersenf18bd892003-12-19 11:07:59 +00001122 /* 9 == ide/host/mt */
1123 if( i == 9 )
1124 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1125 /* esac */
1126 } /* switch(action) */
1127
1128 if(compat_name == NULL )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001129 return;
Eric Andersenf18bd892003-12-19 11:07:59 +00001130
1131#ifdef CONFIG_DEBUG
1132 msg_logger( NO_DIE, LOG_INFO, "action_compat(): %s\n", compat_name);
1133#endif
1134
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001135 /* Now decide what to do with it */
1136 switch (action)
1137 {
1138 case AC_MKOLDCOMPAT:
1139 case AC_MKNEWCOMPAT:
1140 mksymlink (dest_name, compat_name);
1141 break;
1142 case AC_RMOLDCOMPAT:
1143 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001144#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001145 if (unlink (compat_name) != 0)
1146 msg_logger( NO_DIE, LOG_ERR, "unlink(): %s: %m\n", compat_name);
1147#else
1148 unlink (compat_name);
1149#endif
1150 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001151 /*esac*/
1152 } /* switch(action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001153} /* End Function action_compat */
1154
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001155static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001156{
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001157 char dpath[STRING_LENGTH];
1158 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001159
Eric Andersenf18bd892003-12-19 11:07:59 +00001160#ifdef CONFIG_DEBUG
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001161 msg_logger( NO_DIE, LOG_INFO, "restore()\n");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001162#endif
1163
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001164 dest_stat.st_mode = 0;
1165 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1166 lstat (dpath, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001167
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001168 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1169 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001170
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001171 if ( S_ISDIR (source_stat.st_mode) )
1172 dir_operation(RESTORE, spath, rootlen,NULL);
1173}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001174
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001175
1176static int copy_inode (const char *destpath, const struct stat *dest_stat,
1177 mode_t new_mode,
1178 const char *sourcepath, const struct stat *source_stat)
1179/* [SUMMARY] Copy an inode.
1180 <destpath> The destination path. An existing inode may be deleted.
1181 <dest_stat> The destination stat(2) information.
1182 <new_mode> The desired new mode for the destination.
1183 <sourcepath> The source path.
1184 <source_stat> The source stat(2) information.
1185 [RETURNS] TRUE on success, else FALSE.
1186*/
1187{
Eric Andersenf18bd892003-12-19 11:07:59 +00001188 int source_len, dest_len;
1189 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1190 int fd, val;
1191 struct sockaddr_un un_addr;
1192 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001193
Eric Andersenf18bd892003-12-19 11:07:59 +00001194#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001195 msg_logger( NO_DIE, LOG_INFO, "copy_inode()\n");
1196#endif
1197
1198 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1199 {
1200 /* Same type */
1201 if ( S_ISLNK (source_stat->st_mode) )
1202 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001203 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1204 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
1205 return (FALSE);
1206 source_link[source_len] = '\0';
1207 dest_link[dest_len] = '\0';
1208 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1209 {
1210 unlink (destpath);
1211 symlink (source_link, destpath);
1212 }
1213 return (TRUE);
1214 } /* Else not a symlink */
1215 chmod (destpath, new_mode & ~S_IFMT);
1216 chown (destpath, source_stat->st_uid, source_stat->st_gid);
1217 return (TRUE);
1218 }
1219 /* Different types: unlink and create */
1220 unlink (destpath);
1221 switch (source_stat->st_mode & S_IFMT)
1222 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001223 case S_IFSOCK:
1224 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1225 break;
1226 un_addr.sun_family = AF_UNIX;
1227 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1228 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1229 close (fd);
1230 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1231 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001232 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001233 case S_IFLNK:
1234 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1235 break;
1236 symlink_val[val] = '\0';
1237 if (symlink (symlink_val, destpath) == 0)
1238 return (TRUE);
1239 break;
1240 case S_IFREG:
1241 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1242 break;
1243 close (fd);
1244 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1245 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001246 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001247 case S_IFBLK:
1248 case S_IFCHR:
1249 case S_IFIFO:
1250 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1251 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001252 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001253 case S_IFDIR:
1254 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1255 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001256do_chown:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001257 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1258 return (TRUE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001259 /*break;*/
1260 }
1261 return (FALSE);
1262} /* End Function copy_inode */
1263
1264static void free_config ()
1265/* [SUMMARY] Free the configuration information.
1266 [RETURNS] Nothing.
1267*/
1268{
1269 struct config_entry_struct *c_entry;
1270 void *next;
1271
Eric Andersenf18bd892003-12-19 11:07:59 +00001272#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001273 msg_logger( NO_DIE, LOG_INFO, "free_config()\n");
1274#endif
1275
1276 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1277 {
1278 unsigned int count;
1279
1280 next = c_entry->next;
1281 regfree (&c_entry->preg);
1282 if (c_entry->action.what == AC_EXECUTE)
1283 {
1284 for (count = 0; count < MAX_ARGS; ++count)
1285 {
1286 if (c_entry->u.execute.argv[count] == NULL)
1287 break;
1288 free (c_entry->u.execute.argv[count]);
1289 }
1290 }
1291 free (c_entry);
1292 }
1293 first_config = NULL;
1294 last_config = NULL;
1295} /* End Function free_config */
1296
1297static int get_uid_gid (int flag, const char *string)
1298/* [SUMMARY] Convert a string to a UID or GID value.
1299 <flag> "UID" or "GID".
1300 <string> The string.
1301 [RETURNS] The UID or GID value.
1302*/
1303{
1304 struct passwd *pw_ent;
1305 struct group *grp_ent;
Eric Andersenf18bd892003-12-19 11:07:59 +00001306#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001307 char * msg="user";
Eric Andersenf18bd892003-12-19 11:07:59 +00001308#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001309
Eric Andersenf18bd892003-12-19 11:07:59 +00001310#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001311 msg_logger( NO_DIE, LOG_INFO, "get_uid_gid()\n");
1312
1313
1314 if(flag != UID && flag != GID )
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001315 msg_logger( DIE, LOG_ERR,"get_uid_gid(): flag != UID && flag != GID\n");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001316#endif
1317
1318 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
1319 return atoi (string);
1320
1321 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
1322 return (pw_ent->pw_uid);
1323
1324 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
1325 return (grp_ent->gr_gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001326#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001327 else
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001328 msg="group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001329
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001330 msg_logger( NO_DIE, LOG_ERR,"unknown %s: %s, defaulting to %cID=0\n", msg, string, msg[0] - 32);
Eric Andersenf18bd892003-12-19 11:07:59 +00001331#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001332 return (0);
1333}/* End Function get_uid_gid */
1334
1335static mode_t get_mode (const char *string)
1336/* [SUMMARY] Convert a string to a mode value.
1337 <string> The string.
1338 [RETURNS] The mode value.
1339*/
1340{
1341 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001342 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +00001343#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001344 msg_logger( NO_DIE, LOG_INFO, "get_mode()\n");
1345#endif
1346
1347 if ( isdigit (string[0]) )
1348 return strtoul (string, NULL, 8);
1349 if (strlen (string) != 9)
Eric Andersenf18bd892003-12-19 11:07:59 +00001350#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001351 msg_logger( DIE, LOG_ERR, "bad mode: %s\n", string);
Eric Andersenf18bd892003-12-19 11:07:59 +00001352#else
1353 exit(EXIT_FAILURE);
1354#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001355 mode = 0;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001356 i= S_IRUSR;
1357 while(i>0)
1358 {
1359 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1360 mode+=i;
1361 i=i/2;
1362 string++;
1363 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001364 return (mode);
1365} /* End Function get_mode */
1366
1367static void signal_handler (int sig)
1368{
Eric Andersenf18bd892003-12-19 11:07:59 +00001369#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001370 msg_logger( NO_DIE, LOG_INFO, "signal_handler()\n");
1371#endif
1372
1373 caught_signal = TRUE;
1374 if (sig == SIGHUP)
1375 caught_sighup = TRUE;
1376#ifdef CONFIG_DEVFSD_VERBOSE
1377 msg_logger( NO_DIE, LOG_INFO, "Caught %s\n",(sig == SIGHUP)?"SIGHUP" : "SIGUSR1");
1378#endif
1379} /* End Function signal_handler */
1380
1381static const char *get_variable (const char *variable, void *info)
1382{
1383 struct get_variable_info *gv_info = info;
1384 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001385 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1386 "uid", "gid", "mode", hostname, mount_point,
1387 gv_info->devpath, gv_info->devname, 0 };
1388 short int i;
1389#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001390 msg_logger( NO_DIE, LOG_INFO, "get_variable()\n");
1391#endif
1392
1393 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
Eric Andersenf18bd892003-12-19 11:07:59 +00001394#ifdef CONFIG_DEVFSD_VERBOSE
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001395 msg_logger( DIE, LOG_ERR, "gethostname(): %m\n");
Eric Andersenf18bd892003-12-19 11:07:59 +00001396#else
1397 exit(EXIT_FAILURE);
1398#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001399 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001400 hostname[STRING_LENGTH - 1] = '\0';
1401
1402 /* compare_string_array returns i>=0 */
1403 i=compare_string_array(field_names, variable);
1404
1405 if ( i > 6 && (i > 1 && gv_info == NULL))
1406 return (NULL);
1407 if( i >= 0 || i <= 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001408 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001409#ifdef CONFIG_DEBUG
1410 msg_logger( NO_DIE, LOG_INFO, "get_variable(): i=%d %s\n",i ,field_names[i+7]);
1411#endif
1412 return(field_names[i+7]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001413 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001414
1415 if(i == 4 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001416 sprintf (sbuf, "%u", gv_info->info->uid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001417 else if(i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001418 sprintf (sbuf, "%u", gv_info->info->gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001419 else if(i == 6)
1420 sprintf (sbuf, "%o", gv_info->info->mode);
1421#ifdef CONFIG_DEBUG
1422 msg_logger( NO_DIE, LOG_INFO, "get_variable(): %s\n", sbuf);
1423#endif
1424 return (sbuf);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001425} /* End Function get_variable */
1426
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001427static void service(struct stat statbuf, char *path)
1428{
1429 struct devfsd_notify_struct info;
1430
Eric Andersenf18bd892003-12-19 11:07:59 +00001431#ifdef CONFIG_DEBUG
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001432 msg_logger( NO_DIE, LOG_INFO, "service()\n");
1433#endif
1434
1435 memset (&info, 0, sizeof info);
1436 info.type = DEVFSD_NOTIFY_REGISTERED;
1437 info.mode = statbuf.st_mode;
1438 info.major = major (statbuf.st_rdev);
1439 info.minor = minor (statbuf.st_rdev);
1440 info.uid = statbuf.st_uid;
1441 info.gid = statbuf.st_gid;
1442 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1443 info.namelen = strlen (info.devname);
1444 service_name (&info);
1445 if ( S_ISDIR (statbuf.st_mode) )
1446 dir_operation(SERVICE,path,0,NULL);
1447}
1448
1449static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001450/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001451 <flag> To choose which function to perform
1452 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001453 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001454 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001455 [RETURNS] Nothing.
1456*/
1457{
1458 struct stat statbuf;
1459 DIR *dp;
1460 struct dirent *de;
1461 char path[STRING_LENGTH];
1462
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001463
Eric Andersenf18bd892003-12-19 11:07:59 +00001464#ifdef CONFIG_DEBUG
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001465 msg_logger( NO_DIE, LOG_INFO, "dir_operation()\n");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001466#endif
1467
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001468 if((dp = opendir( dir_name))==NULL)
1469 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001470#ifdef CONFIG_DEBUG
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001471 msg_logger( NO_DIE, LOG_ERR, "opendir(): %s: %m\n", dir_name);
Eric Andersenf18bd892003-12-19 11:07:59 +00001472#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001473 return;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001474 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001475
1476 while ( (de = readdir (dp) ) != NULL )
1477 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001478
Eric Andersenf18bd892003-12-19 11:07:59 +00001479 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 +00001480 continue;
Eric Andersenf18bd892003-12-19 11:07:59 +00001481 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
1482#ifdef CONFIG_DEBUG
1483 msg_logger( NO_DIE, LOG_ERR, "dir_operation(): %s\n", path);
1484#endif
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001485
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001486 if (lstat (path, &statbuf) != 0)
1487 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001488#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001489 msg_logger( NO_DIE, LOG_ERR, "%s: %m\n", path);
1490#endif
1491 continue;
1492 }
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001493 switch(type)
1494 {
1495 case SERVICE:
1496 service(statbuf,path);
1497 break;
1498 case RESTORE:
1499 restore(path, statbuf, var);
1500 break;
1501 case READ_CONFIG:
1502 read_config_file (path, var, event_mask);
1503 break;
1504 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001505 }
1506 closedir (dp);
1507} /* End Function do_scan_and_service */
1508
1509static int mksymlink (const char *oldpath, const char *newpath)
1510/* [SUMMARY] Create a symlink, creating intervening directories as required.
1511 <oldpath> The string contained in the symlink.
1512 <newpath> The name of the new symlink.
1513 [RETURNS] 0 on success, else -1.
1514*/
1515{
Eric Andersenf18bd892003-12-19 11:07:59 +00001516#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001517 msg_logger( NO_DIE, LOG_INFO, "mksymlink()\n", newpath);
1518#endif
1519
1520
1521 if ( !make_dir_tree (newpath) )
1522 return (-1);
1523
1524 if (symlink (oldpath, newpath) != 0)
1525 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001526 if (errno != EEXIST)
Eric Andersenf18bd892003-12-19 11:07:59 +00001527 {
1528#ifdef CONFIG_DEBUG
1529 msg_logger( NO_DIE, LOG_ERR, "mksymlink(): %s to %s: %m\n", oldpath, newpath);
1530#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001531 return (-1);
Eric Andersenf18bd892003-12-19 11:07:59 +00001532 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001533 }
1534 return (0);
1535} /* End Function mksymlink */
1536
1537
1538static int make_dir_tree (const char *path)
1539/* [SUMMARY] Creating intervening directories for a path as required.
Eric Andersen52899692003-10-22 10:10:50 +00001540 <path> The full pathname (including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001541 [RETURNS] TRUE on success, else FALSE.
1542*/
1543{
Eric Andersenf18bd892003-12-19 11:07:59 +00001544#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001545 msg_logger( NO_DIE, LOG_INFO, "make_dir_tree()\n");
1546#endif
Eric Andersen0e020d12004-10-13 06:25:52 +00001547 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001548 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001549#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001550 msg_logger( NO_DIE, LOG_ERR, "make_dir_tree(): %s: %m\n", path);
1551#endif
1552 return (FALSE);
1553 }
1554 return(TRUE);
1555} /* End Function make_dir_tree */
1556
1557static int expand_expression(char *output, unsigned int outsize,
1558 const char *input,
1559 const char *(*get_variable_func)(const char *variable, void *info),
1560 void *info,
1561 const char *devname,
1562 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001563/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001564 <output> The output expanded expression is written here.
1565 <length> The size of the output buffer.
1566 <input> The input expression. This may equal <<output>>.
1567 <get_variable> A function which will be used to get variable values. If
1568 this returns NULL, the environment is searched instead. If this is NULL,
1569 only the environment is searched.
1570 <info> An arbitrary pointer passed to <<get_variable>>.
1571 <devname> Device name; specifically, this is the string that contains all
1572 of the regular subexpressions.
1573 <ex> Array of start / end offsets into info->devname for each subexpression
1574 <numexp> Number of regular subexpressions found in <<devname>>.
1575 [RETURNS] TRUE on success, else FALSE.
1576*/
1577{
1578 char temp[STRING_LENGTH];
1579
Eric Andersenf18bd892003-12-19 11:07:59 +00001580#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001581 msg_logger( NO_DIE, LOG_INFO, "expand_expression()\n");
1582#endif
1583
1584 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
1585 return (FALSE);
1586 expand_regexp (output, outsize, temp, devname, ex, numexp);
1587 return (TRUE);
1588} /* End Function expand_expression */
1589
1590static void expand_regexp (char *output, size_t outsize, const char *input,
1591 const char *devname,
1592 const regmatch_t *ex, unsigned int numex )
1593/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1594 <output> The output expanded expression is written here.
1595 <outsize> The size of the output buffer.
1596 <input> The input expression. This may NOT equal <<output>>, because
1597 supporting that would require yet another string-copy. However, it's not
1598 hard to write a simple wrapper function to add this functionality for those
1599 few cases that need it.
1600 <devname> Device name; specifically, this is the string that contains all
1601 of the regular subexpressions.
1602 <ex> An array of start and end offsets into <<devname>>, one for each
1603 subexpression
1604 <numex> Number of subexpressions in the offset-array <<ex>>.
1605 [RETURNS] Nothing.
1606*/
1607{
1608 const char last_exp = '0' - 1 + numex;
1609 int c = -1;
1610
Eric Andersenf18bd892003-12-19 11:07:59 +00001611#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001612 msg_logger( NO_DIE, LOG_INFO, "expand_regexp()\n");
1613#endif
1614
1615 /* Guarantee NULL termination by writing an explicit '\0' character into
1616 the very last byte */
1617 if (outsize)
1618 output[--outsize] = '\0';
1619 /* Copy the input string into the output buffer, replacing '\\' with '\'
1620 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1621 codes are deleted */
1622 while ( (c != '\0') && (outsize != 0) )
1623 {
1624 c = *input;
1625 ++input;
1626 if (c == '\\')
1627 {
1628 c = *input;
1629 ++input;
1630 if (c != '\\')
1631 {
1632 if ((c >= '0') && (c <= last_exp))
1633 {
1634 const regmatch_t *subexp = ex + (c - '0');
1635 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1636
1637 /* Range checking */
1638 if (sublen > outsize)
1639 sublen = outsize;
1640 strncpy (output, devname + subexp->rm_so, sublen);
1641 output += sublen;
1642 outsize -= sublen;
1643 }
1644 continue;
1645 }
1646 }
1647 *output = c;
1648 ++output;
1649 --outsize;
1650 } /* while */
1651} /* End Function expand_regexp */
1652
1653
1654/* from compat_name.c */
1655
1656struct translate_struct
1657{
1658 char *match; /* The string to match to (up to length) */
1659 char *format; /* Format of output, "%s" takes data past match string,
1660 NULL is effectively "%s" (just more efficient) */
1661};
1662
1663static struct translate_struct translate_table[] =
1664{
1665 {"sound/", NULL},
1666 {"printers/", "lp%s"},
1667 {"v4l/", NULL},
1668 {"parports/", "parport%s"},
1669 {"fb/", "fb%s"},
1670 {"netlink/", NULL},
1671 {"loop/", "loop%s"},
1672 {"floppy/", "fd%s"},
1673 {"rd/", "ram%s"},
1674 {"md/", "md%s"}, /* Meta-devices */
1675 {"vc/", "tty%s"},
1676 {"misc/", NULL},
1677 {"isdn/", NULL},
1678 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1679 {"i2c/", "i2c-%s"},
1680 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1681 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1682 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1683 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1684 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1685 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1686 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1687 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1688 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1689 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1690 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1691 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1692 {"input/js", "js%s"}, /* Joystick driver */
1693 {NULL, NULL}
1694};
1695
1696const char *get_old_name (const char *devname, unsigned int namelen,
1697 char *buffer, unsigned int major, unsigned int minor)
1698/* [SUMMARY] Translate a kernel-supplied name into an old name.
1699 <devname> The device name provided by the kernel.
1700 <namelen> The length of the name.
1701 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1702 <major> The major number for the device.
1703 <minor> The minor number for the device.
1704 [RETURNS] A pointer to the old name if known, else NULL.
1705*/
1706{
1707 const char *compat_name = NULL;
1708 char *ptr;
1709 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001710 unsigned int i;
1711 char mode;
1712 int indexx;
1713 const char *pty1;
1714 const char *pty2;
1715 size_t len;
1716 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
1717 const char *fmt[] = { NULL ,
1718 "sg%u", /* scsi/generic */
1719 NULL, /* scsi/disc */
1720 "sr%u", /* scsi/cd */
1721 NULL, /* scsi/part */
1722 "nst%u%c", /* scsi/mt */
1723 "hd%c" , /* ide/host/disc */
1724 "hd%c" , /* ide/host/cd */
1725 "hd%c%s", /* ide/host/part */
1726 "%sht%d", /* ide/host/mt */
1727 "sbpcd%u", /* sbp/ */
1728 "vcs%s", /* vcc/ */
1729 "%cty%c%c", /* pty/ */
1730 NULL };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001731
Eric Andersenf18bd892003-12-19 11:07:59 +00001732#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001733 msg_logger( NO_DIE, LOG_INFO, "get_old_name()\n");
1734#endif
1735
1736 for (trans = translate_table; trans->match != NULL; ++trans)
1737 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001738 len = strlen (trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001739
1740 if (strncmp (devname, trans->match, len) == 0)
1741 {
1742 if (trans->format == NULL)
1743 return (devname + len);
1744 sprintf (buffer, trans->format, devname + len);
1745 return (buffer);
1746 }
1747 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001748
Eric Andersenf18bd892003-12-19 11:07:59 +00001749 ptr = (strrchr (devname, '/') + 1);
1750 i = scan_dev_name(devname, namelen, ptr);
1751
1752 if( i > 0 && i < 13)
1753 compat_name = buffer;
1754 else
1755 return NULL;
1756
1757#ifdef CONFIG_DEBUG
1758 msg_logger( NO_DIE, LOG_INFO, "get_old_name(): scan_dev_name() returned %d\n", i);
1759#endif
1760
1761 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1762 if( i == 1 || i == 3 || i == 10 )
1763 sprintf (buffer, fmt[i], minor);
1764
1765 /* 2 ==scsi/disc, 4 == scsi/part */
1766 if( i == 2 || i == 4)
1767 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1768
1769 /* 5 == scsi/mt */
1770 if( i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001771 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001772 mode = ptr[2];
1773 if (mode == 'n')
1774 mode = '\0';
1775 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1776 if (devname[namelen - 1] != 'n')
1777 ++compat_name;
1778 }
1779 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1780 if( i == 6 || i == 7 || i == 8 )
1781 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4); /* last arg should be ignored for i == 6 or i== 7 */
1782
1783 /* 9 == ide/host/mt */
1784 if( i == 9 )
1785 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1786
1787 /* 11 == vcc/ */
1788 if( i == 11 )
1789 {
1790 sprintf (buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001791 if (buffer[3] == '0')
1792 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001793 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001794 /* 12 == pty/ */
1795 if( i == 12 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001796 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001797 pty1 = "pqrstuvwxyzabcde";
1798 pty2 = "0123456789abcdef";
1799 indexx = atoi (devname + 5);
1800 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001801 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001802#ifdef CONFIG_DEBUG
1803 if(compat_name!=NULL)
1804 msg_logger( NO_DIE, LOG_INFO, "get_old_name(): compat_name %s\n", compat_name);
1805#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001806 return (compat_name);
1807} /* End Function get_old_name */
1808
1809static char get_old_ide_name (unsigned int major, unsigned int minor)
1810/* [SUMMARY] Get the old IDE name for a device.
1811 <major> The major number for the device.
1812 <minor> The minor number for the device.
1813 [RETURNS] The drive letter.
1814*/
1815{
1816 char letter='y'; /* 121 */
1817 char c='a'; /* 97 */
1818 int i=IDE0_MAJOR;
1819
Eric Andersenf18bd892003-12-19 11:07:59 +00001820#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001821 msg_logger( NO_DIE, LOG_INFO, "get_old_ide_name()\n");
1822#endif
1823
1824 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1825 do {
1826 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1827 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1828 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1829 i==IDE9_MAJOR )
1830 {
1831 if(i==major)
1832 {
1833 letter=c;
1834 break;
1835 }
1836 c+=2;
1837 }
1838 i++;
1839 } while(i<=IDE9_MAJOR);
1840
1841 if (minor > 63)
1842 ++letter;
1843 return (letter);
1844} /* End Function get_old_ide_name */
1845
1846static char *write_old_sd_name (char *buffer,
1847 unsigned int major, unsigned int minor,
1848 char *part)
1849/* [SUMMARY] Write the old SCSI disc name to a buffer.
1850 <buffer> The buffer to write to.
1851 <major> The major number for the device.
1852 <minor> The minor number for the device.
1853 <part> The partition string. Must be "" for a whole-disc entry.
1854 [RETURNS] A pointer to the buffer on success, else NULL.
1855*/
1856{
1857 unsigned int disc_index;
1858
Eric Andersenf18bd892003-12-19 11:07:59 +00001859#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001860 msg_logger( NO_DIE, LOG_INFO, "write_old_sd_name()\n");
1861#endif
1862
1863 if (major == 8)
1864 {
1865 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
1866 return (buffer);
1867 }
1868 if ( (major > 64) && (major < 72) )
1869 {
1870 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1871 if (disc_index < 26)
1872 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1873 else
1874 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
1875 return (buffer);
1876 }
1877 return (NULL);
1878} /* End Function write_old_sd_name */
1879
1880
1881/* expression.c */
1882
1883/*EXPERIMENTAL_FUNCTION*/
1884
1885int st_expr_expand (char *output, unsigned int length, const char *input,
1886 const char *(*get_variable_func) (const char *variable,
1887 void *info),
1888 void *info)
1889/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1890 <output> The output expanded expression is written here.
1891 <length> The size of the output buffer.
1892 <input> The input expression. This may equal <<output>>.
1893 <get_variable> A function which will be used to get variable values. If
1894 this returns NULL, the environment is searched instead. If this is NULL,
1895 only the environment is searched.
1896 <info> An arbitrary pointer passed to <<get_variable>>.
1897 [RETURNS] TRUE on success, else FALSE.
1898*/
1899{
1900 char ch;
1901 unsigned int len;
1902 unsigned int out_pos = 0;
1903 const char *env;
1904 const char *ptr;
1905 struct passwd *pwent;
1906 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1907
Eric Andersenf18bd892003-12-19 11:07:59 +00001908#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001909 msg_logger( NO_DIE, LOG_INFO, "st_expr_expand()\n");
1910#endif
1911
1912 if (length > BUFFER_SIZE)
1913 length = BUFFER_SIZE;
1914 for (; TRUE; ++input)
1915 {
1916 switch (ch = *input)
1917 {
1918 case '$':
1919 /* Variable expansion */
1920 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1921 if (input == NULL)
1922 return (FALSE);
1923 break;
1924 case '~':
1925 /* Home directory expansion */
1926 ch = input[1];
1927 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1928 {
1929 /* User's own home directory: leave separator for next time */
1930 if ( ( env = getenv ("HOME") ) == NULL )
1931 {
1932#ifdef CONFIG_DEVFSD_VERBOSE
1933 msg_logger( NO_DIE, LOG_INFO, bb_msg_variable_not_found, "HOME");
1934#endif
1935 return (FALSE);
1936 }
1937 len = strlen (env);
1938 if (len + out_pos >= length)
1939 goto st_expr_expand_out;
1940 memcpy (buffer + out_pos, env, len + 1);
1941 out_pos += len;
1942 continue;
1943 }
1944 /* Someone else's home directory */
1945 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1946 /* VOID */ ;
1947 len = ptr - input;
1948 if (len >= sizeof tmp)
1949 goto st_expr_expand_out;
1950 safe_memcpy (tmp, input, len);
1951 input = ptr - 1;
1952 if ( ( pwent = getpwnam (tmp) ) == NULL )
1953 {
1954#ifdef CONFIG_DEVFSD_VERBOSE
Eric Andersenf18bd892003-12-19 11:07:59 +00001955 msg_logger( NO_DIE, LOG_INFO, "no pwent for: %s\n", tmp);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001956#endif
1957 return (FALSE);
1958 }
1959 len = strlen (pwent->pw_dir);
1960 if (len + out_pos >= length)
1961 goto st_expr_expand_out;
1962 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1963 out_pos += len;
1964 break;
1965 case '\0':
1966 /* Falltrough */
1967 default:
1968 if (out_pos >= length)
1969 goto st_expr_expand_out;
1970 buffer[out_pos++] = ch;
1971 if (ch == '\0')
1972 {
1973 memcpy (output, buffer, out_pos);
1974 return (TRUE);
1975 }
1976 break;
1977 /* esac */
1978 }
1979 }
1980 return (FALSE);
1981st_expr_expand_out:
1982#ifdef CONFIG_DEVFSD_VERBOSE
1983 msg_logger( NO_DIE, LOG_INFO, bb_msg_small_buffer);
1984#endif
1985 return (FALSE);
1986} /* End Function st_expr_expand */
1987
1988
1989/* Private functions follow */
1990
1991static const char *expand_variable (char *buffer, unsigned int length,
1992 unsigned int *out_pos, const char *input,
1993 const char *(*func) (const char *variable,
1994 void *info),
1995 void *info)
1996/* [SUMMARY] Expand a variable.
1997 <buffer> The buffer to write to.
1998 <length> The length of the output buffer.
1999 <out_pos> The current output position. This is updated.
2000 <input> A pointer to the input character pointer.
2001 <func> A function which will be used to get variable values. If this
2002 returns NULL, the environment is searched instead. If this is NULL, only
2003 the environment is searched.
2004 <info> An arbitrary pointer passed to <<func>>.
2005 <errfp> Diagnostic messages are written here.
2006 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
2007*/
2008{
2009 char ch;
2010 int len;
2011 unsigned int open_braces;
2012 const char *env, *ptr;
2013 char tmp[STRING_LENGTH];
2014
Eric Andersenf18bd892003-12-19 11:07:59 +00002015#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002016 msg_logger( NO_DIE, LOG_INFO, "expand_variable()\n");
2017#endif
2018
2019 ch = input[0];
2020 if (ch == '$')
2021 {
2022 /* Special case for "$$": PID */
2023 sprintf ( tmp, "%d", (int) getpid () );
2024 len = strlen (tmp);
2025 if (len + *out_pos >= length)
2026 goto expand_variable_out;
2027
2028 memcpy (buffer + *out_pos, tmp, len + 1);
2029 out_pos += len;
2030 return (input);
2031 }
2032 /* Ordinary variable expansion, possibly in braces */
2033 if (ch != '{')
2034 {
2035 /* Simple variable expansion */
2036 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
2037 /* VOID */ ;
2038 len = ptr - input;
2039 if (len >= sizeof tmp)
2040 goto expand_variable_out;
2041
2042 safe_memcpy (tmp, input, len);
2043 input = ptr - 1;
2044 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
2045 {
2046#ifdef CONFIG_DEVFSD_VERBOSE
2047 msg_logger( NO_DIE, LOG_INFO, bb_msg_variable_not_found, tmp);
2048#endif
2049 return (NULL);
2050 }
2051 len = strlen (env);
2052 if (len + *out_pos >= length)
2053 goto expand_variable_out;
2054
2055 memcpy (buffer + *out_pos, env, len + 1);
2056 *out_pos += len;
2057 return (input);
2058 }
2059 /* Variable in braces: check for ':' tricks */
2060 ch = *++input;
2061 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
2062 /* VOID */;
2063 if (ch == '}')
2064 {
2065 /* Must be simple variable expansion with "${var}" */
2066 len = ptr - input;
2067 if (len >= sizeof tmp)
2068 goto expand_variable_out;
2069
2070 safe_memcpy (tmp, input, len);
2071 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
2072 if (ptr == NULL)
2073 return (NULL);
2074 return (input + len);
2075 }
2076 if (ch != ':' || ptr[1] != '-' )
2077 {
2078#ifdef CONFIG_DEVFSD_VERBOSE
2079 msg_logger( NO_DIE, LOG_INFO,"illegal char in var name\n");
2080#endif
2081 return (NULL);
2082 }
2083 /* It's that handy "${var:-word}" expression. Check if var is defined */
2084 len = ptr - input;
2085 if (len >= sizeof tmp)
2086 goto expand_variable_out;
2087
2088 safe_memcpy (tmp, input, len);
2089 /* Move input pointer to ':' */
2090 input = ptr;
2091 /* First skip to closing brace, taking note of nested expressions */
2092 ptr += 2;
2093 ch = ptr[0];
2094 for (open_braces = 1; open_braces > 0; ch = *++ptr)
2095 {
2096 switch (ch)
2097 {
2098 case '{':
2099 ++open_braces;
2100 break;
2101 case '}':
2102 --open_braces;
2103 break;
2104 case '\0':
2105#ifdef CONFIG_DEVFSD_VERBOSE
2106 msg_logger( NO_DIE, LOG_INFO,"\"}\" not found in: %s\n", input);
2107#endif
2108 return (NULL);
2109 default:
2110 break;
2111 }
2112 }
2113 --ptr;
2114 /* At this point ptr should point to closing brace of "${var:-word}" */
2115 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
2116 {
2117 /* Found environment variable, so skip the input to the closing brace
2118 and return the variable */
2119 input = ptr;
2120 len = strlen (env);
2121 if (len + *out_pos >= length)
2122 goto expand_variable_out;
2123
2124 memcpy (buffer + *out_pos, env, len + 1);
2125 *out_pos += len;
2126 return (input);
2127 }
2128 /* Environment variable was not found, so process word. Advance input
2129 pointer to start of word in "${var:-word}" */
2130 input += 2;
2131 len = ptr - input;
2132 if (len >= sizeof tmp)
2133 goto expand_variable_out;
2134
2135 safe_memcpy (tmp, input, len);
2136 input = ptr;
2137 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
2138 return (NULL);
2139 len = strlen (tmp);
2140 if (len + *out_pos >= length)
2141 goto expand_variable_out;
2142
2143 memcpy (buffer + *out_pos, tmp, len + 1);
2144 *out_pos += len;
2145 return (input);
2146expand_variable_out:
2147#ifdef CONFIG_DEVFSD_VERBOSE
2148 msg_logger( NO_DIE, LOG_INFO, bb_msg_small_buffer);
2149#endif
2150 return (NULL);
2151} /* End Function expand_variable */
2152
2153
2154static const char *get_variable_v2 (const char *variable,
2155 const char *(*func) (const char *variable, void *info),
2156 void *info)
2157/* [SUMMARY] Get a variable from the environment or .
2158 <variable> The variable name.
2159 <func> A function which will be used to get the variable. If this returns
2160 NULL, the environment is searched instead. If this is NULL, only the
2161 environment is searched.
2162 [RETURNS] The value of the variable on success, else NULL.
2163*/
2164{
2165 const char *value;
2166
Eric Andersenf18bd892003-12-19 11:07:59 +00002167#ifdef CONFIG_DEBUG
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002168 msg_logger( NO_DIE, LOG_INFO, "get_variable_v2()\n");
2169#endif
2170
2171 if (func != NULL)
2172 {
2173 value = (*func) (variable, info);
2174 if (value != NULL)
2175 return (value);
2176 }
2177 return getenv (variable);
2178} /* End Function get_variable */
2179
2180/* END OF CODE */