blob: 0069b280e9169ec5e0607fdbc42d1f08914a4a7d [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002/*
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6/*
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00007 devfsd implementation for busybox
8
9 Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
10
11 Busybox version is based on some previous work and ideas
12 Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
13
14 devfsd.c
15
16 Main file for devfsd (devfs daemon for Linux).
17
18 Copyright (C) 1998-2002 Richard Gooch
19
20 devfsd.h
21
22 Header file for devfsd (devfs daemon for Linux).
23
24 Copyright (C) 1998-2000 Richard Gooch
25
26 compat_name.c
27
28 Compatibility name file for devfsd (build compatibility names).
29
30 Copyright (C) 1998-2002 Richard Gooch
31
32 expression.c
33
34 This code provides Borne Shell-like expression expansion.
35
36 Copyright (C) 1997-1999 Richard Gooch
37
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2 of the License, or
41 (at your option) any later version.
42
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
47
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
51
52 Richard Gooch may be reached by email at rgooch@atnf.csiro.au
53 The postal address is:
54 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
55*/
56
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000057#include "busybox.h"
"Vladimir N. Oleynik"23f62fc2005-09-14 16:59:11 +000058#include "xregex.h"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000059#include <unistd.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <stdarg.h>
63#include <string.h>
64#include <ctype.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000065#include <sys/stat.h>
66#include <sys/types.h>
67#include <sys/wait.h>
68#include <sys/ioctl.h>
69#include <sys/socket.h>
70#include <sys/un.h>
71#include <dirent.h>
72#include <fcntl.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000073#include <syslog.h>
74#include <signal.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000075#include <errno.h>
Eric Andersen2af70002003-10-09 21:02:23 +000076#include <sys/sysmacros.h>
Glenn L McGrath17d21fa2003-10-09 11:46:23 +000077
Eric Andersen2af70002003-10-09 21:02:23 +000078
79/* Various defines taken from linux/major.h */
80#define IDE0_MAJOR 3
81#define IDE1_MAJOR 22
82#define IDE2_MAJOR 33
83#define IDE3_MAJOR 34
84#define IDE4_MAJOR 56
85#define IDE5_MAJOR 57
86#define IDE6_MAJOR 88
87#define IDE7_MAJOR 89
88#define IDE8_MAJOR 90
89#define IDE9_MAJOR 91
90
91
92/* Various defines taken from linux/devfs_fs.h */
93#define DEVFSD_PROTOCOL_REVISION_KERNEL 5
94#define DEVFSD_IOCTL_BASE 'd'
95/* These are the various ioctls */
96#define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
97#define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
98#define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
Eric Andersenf18bd892003-12-19 11:07:59 +000099#define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
Eric Andersen2af70002003-10-09 21:02:23 +0000100#define DEVFSD_NOTIFY_REGISTERED 0
101#define DEVFSD_NOTIFY_UNREGISTERED 1
102#define DEVFSD_NOTIFY_ASYNC_OPEN 2
103#define DEVFSD_NOTIFY_CLOSE 3
104#define DEVFSD_NOTIFY_LOOKUP 4
105#define DEVFSD_NOTIFY_CHANGE 5
106#define DEVFSD_NOTIFY_CREATE 6
107#define DEVFSD_NOTIFY_DELETE 7
Eric Andersenf18bd892003-12-19 11:07:59 +0000108#define DEVFS_PATHLEN 1024
109/* Never change this otherwise the binary interface will change */
110
Eric Andersen2af70002003-10-09 21:02:23 +0000111struct devfsd_notify_struct
112{ /* Use native C types to ensure same types in kernel and user space */
113 unsigned int type; /* DEVFSD_NOTIFY_* value */
114 unsigned int mode; /* Mode of the inode or device entry */
115 unsigned int major; /* Major number of device entry */
116 unsigned int minor; /* Minor number of device entry */
117 unsigned int uid; /* Uid of process, inode or device entry */
118 unsigned int gid; /* Gid of process, inode or device entry */
119 unsigned int overrun_count; /* Number of lost events */
120 unsigned int namelen; /* Number of characters not including '\0' */
121 /* The device name MUST come last */
122 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
123};
124
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000125#define BUFFER_SIZE 16384
126#define DEVFSD_VERSION "1.3.25"
127#define CONFIG_FILE "/etc/devfsd.conf"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000128#define MODPROBE "/sbin/modprobe"
Eric Andersenf18bd892003-12-19 11:07:59 +0000129#define MODPROBE_SWITCH_1 "-k"
130#define MODPROBE_SWITCH_2 "-C"
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000131#define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000132#define MAX_ARGS (6 + 1)
133#define MAX_SUBEXPR 10
134#define STRING_LENGTH 255
135
136/* for get_uid_gid() */
137#define UID 0
138#define GID 1
139
Rob Landley1ba19d62005-10-08 17:42:35 +0000140/* fork_and_execute() */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000141# define DIE 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000142# define NO_DIE 0
143
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000144/* for dir_operation() */
145#define RESTORE 0
146#define SERVICE 1
147#define READ_CONFIG 2
148
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000149/* Update only after changing code to reflect new protocol */
150#define DEVFSD_PROTOCOL_REVISION_DAEMON 5
151
152/* Compile-time check */
153#if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
154#error protocol version mismatch. Update your kernel headers
155#endif
156
157#define AC_PERMISSIONS 0
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000158#define AC_MODLOAD 1
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000159#define AC_EXECUTE 2
160#define AC_MFUNCTION 3 /* not supported by busybox */
161#define AC_CFUNCTION 4 /* not supported by busybox */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000162#define AC_COPY 5
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000163#define AC_IGNORE 6
164#define AC_MKOLDCOMPAT 7
165#define AC_MKNEWCOMPAT 8
166#define AC_RMOLDCOMPAT 9
167#define AC_RMNEWCOMPAT 10
168#define AC_RESTORE 11
169
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000170struct permissions_type
171{
172 mode_t mode;
173 uid_t uid;
174 gid_t gid;
175};
176
177struct execute_type
178{
179 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
180};
181
182struct copy_type
183{
184 const char *source;
185 const char *destination;
186};
187
188struct action_type
189{
190 unsigned int what;
191 unsigned int when;
192};
193
194struct config_entry_struct
195{
196 struct action_type action;
197 regex_t preg;
198 union
199 {
200 struct permissions_type permissions;
201 struct execute_type execute;
202 struct copy_type copy;
203 }
204 u;
205 struct config_entry_struct *next;
206};
207
208struct get_variable_info
209{
210 const struct devfsd_notify_struct *info;
211 const char *devname;
212 char devpath[STRING_LENGTH];
213};
214
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000215static void dir_operation(int , const char * , int, unsigned long* );
216static void service(struct stat statbuf, char *path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000217static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
218static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
219static int mksymlink (const char *oldpath, const char *newpath);
Eric Andersenf18bd892003-12-19 11:07:59 +0000220static void read_config_file (char *path, int optional, unsigned long *event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000221static void process_config_line (const char *, unsigned long *);
222static int do_servicing (int, unsigned long);
223static void service_name (const struct devfsd_notify_struct *);
224static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
225static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
226 const regmatch_t *, unsigned);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000227static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000228static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
229 const regmatch_t *, unsigned);
230static void action_compat (const struct devfsd_notify_struct *, unsigned);
231static void free_config (void);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000232static void restore(char *spath, struct stat source_stat, int rootlen);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000233static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
234static mode_t get_mode (const char *);
235static void signal_handler (int);
236static const char *get_variable (const char *, void *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000237static int make_dir_tree (const char *);
238static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
239 const char *, const regmatch_t *, unsigned );
240static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
241static const char *expand_variable( char *, unsigned, unsigned *, const char *,
242 const char *(*) (const char *, void *), void * );
243static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
244static char get_old_ide_name (unsigned , unsigned);
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000245static char *write_old_sd_name (char *, unsigned, unsigned, const char *);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000246
247/* busybox functions */
Rob Landley1ba19d62005-10-08 17:42:35 +0000248static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3)));
249static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3)));
250static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000251static void fork_and_execute(int die, char *arg0, char **arg );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000252static int get_uid_gid ( int, const char *);
253static void safe_memcpy( char * dest, const char * src, int len);
Eric Andersenf18bd892003-12-19 11:07:59 +0000254static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
255static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000256
257/* Structs and vars */
258static struct config_entry_struct *first_config = NULL;
259static struct config_entry_struct *last_config = NULL;
260static const char *mount_point = NULL;
261static volatile int caught_signal = FALSE;
262static volatile int caught_sighup = FALSE;
263static struct initial_symlink_struct
264{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000265 const char *dest;
266 const char *name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000267} initial_symlinks[] =
268{
269 {"/proc/self/fd", "fd"},
270 {"fd/0", "stdin"},
271 {"fd/1", "stdout"},
272 {"fd/2", "stderr"},
273 {NULL, NULL},
274};
275
276static struct event_type
277{
278 unsigned int type; /* The DEVFSD_NOTIFY_* value */
279 const char *config_name; /* The name used in the config file */
280} event_types[] =
281{
282 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
283 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
284 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
285 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
286 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
287 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
288 {DEVFSD_NOTIFY_CREATE, "CREATE"},
289 {DEVFSD_NOTIFY_DELETE, "DELETE"},
290 {0xffffffff, NULL}
291};
292
Rob Landley1ba19d62005-10-08 17:42:35 +0000293/* Busybox messages */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000294
Rob Landley1ba19d62005-10-08 17:42:35 +0000295static const char * const bb_msg_proto_rev = "protocol revision";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000296static const char * const bb_msg_bad_config = "bad %s config file: %s";
Rob Landley1ba19d62005-10-08 17:42:35 +0000297static const char * const bb_msg_small_buffer = "buffer too small";
298static const char * const bb_msg_variable_not_found = "variable: %s not found";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000299
Rob Landley1ba19d62005-10-08 17:42:35 +0000300/* Busybox functions */
301static void msg_logger(int pri, const char * fmt, ... )
302{
303 va_list ap;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000304 int ret;
305
Rob Landley1ba19d62005-10-08 17:42:35 +0000306 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000307 ret = access ("/dev/log", F_OK);
Rob Landley1ba19d62005-10-08 17:42:35 +0000308 if (ret == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000309 openlog(applet_name, 0, LOG_DAEMON);
Rob Landley1ba19d62005-10-08 17:42:35 +0000310 vsyslog( pri , fmt, ap);
311 /* Man: A trailing newline is added when needed. */
312 closelog();
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000313 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000314 /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
315 if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
316 bb_error_msg(fmt, ap);
317 }
318 va_end(ap);
319}
320
321static void msg_logger_and_die(int pri, const char* fmt, ...)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000322{
323 va_list ap;
324
325 va_start(ap, fmt);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000326 msg_logger(pri, fmt, ap);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000327 va_end(ap);
Rob Landley1ba19d62005-10-08 17:42:35 +0000328 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000329}
Rob Landley1ba19d62005-10-08 17:42:35 +0000330
331/* Busybox stuff */
332#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000333#define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args)
334#define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args)
335#define devfsd_error_msg_and_die(fmt, args...) bb_error_msg_and_die(fmt, ## args)
Rob Landley1ba19d62005-10-08 17:42:35 +0000336#if defined(CONFIG_DEBUG)
337#define debug_msg_logger(x, fmt, args...) msg_logger(x, fmt, ## args)
338#else
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000339#define debug_msg_logger(x, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000340#endif
341#else
342#define debug_msg_logger(x, fmt, args...)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000343#define msg_logger(p, fmt, args...)
Rob Landley1ba19d62005-10-08 17:42:35 +0000344#define msg_logger_and_die(p, fmt, args...) exit(1)
345#define devfsd_perror_msg_and_die(fmt, args...) exit(1)
346#define devfsd_error_msg_and_die(fmt, args...) exit(1)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000347#define devfsd_error_msg(fmt, args...)
Eric Andersenf18bd892003-12-19 11:07:59 +0000348#endif
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000349
Rob Landley1ba19d62005-10-08 17:42:35 +0000350static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000351{
Eric Andersenf18bd892003-12-19 11:07:59 +0000352 if (ioctl (fd, request, event_mask_flag) == -1)
Rob Landley1ba19d62005-10-08 17:42:35 +0000353 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000354}
355
356static void fork_and_execute(int die, char *arg0, char **arg )
357{
358 switch ( fork () )
359 {
360 case 0:
361 /* Child */
362 break;
363 case -1:
364 /* Parent: Error : die or return */
Rob Landley1ba19d62005-10-08 17:42:35 +0000365 msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted);
366 if(die)
Eric Andersenf18bd892003-12-19 11:07:59 +0000367 exit(EXIT_FAILURE);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000368 return;
369 default:
370 /* Parent : ok : return or exit */
371 if(arg0 != NULL)
372 {
373 wait (NULL);
374 return;
375 }
376 exit (EXIT_SUCCESS);
377 }
378 /* Child : if arg0 != NULL do execvp */
379 if(arg0 != NULL )
380 {
Denis Vlasenko1d76f432007-02-06 01:20:12 +0000381 BB_EXECVP(arg0, arg);
Rob Landley1ba19d62005-10-08 17:42:35 +0000382 msg_logger_and_die(LOG_ERR, "execvp");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000383 }
384}
385
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000386static void safe_memcpy( char *dest, const char *src, int len)
387{
388 memcpy (dest , src , len );
389 dest[len] = '\0';
390}
391
Eric Andersenf18bd892003-12-19 11:07:59 +0000392static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
393{
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000394 if(d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
395 return 2 + addendum;
396 if(d[n - 2]=='c' && d[n - 1]=='d')
397 return 3 + addendum;
398 if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
399 return 4 + addendum;
400 if(ptr[n - 2]=='m' && ptr[n - 1]=='t')
401 return 5 + addendum;
402 return 0;
Eric Andersenf18bd892003-12-19 11:07:59 +0000403}
404
405static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
406{
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000407 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/') {
Eric Andersenf18bd892003-12-19 11:07:59 +0000408 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
409 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
410 d[n - 1]=='c' )
411 return 1;
412 return scan_dev_name_common(d, n, 0, ptr);
413 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000414 if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
Eric Andersenf18bd892003-12-19 11:07:59 +0000415 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
Eric Andersenf18bd892003-12-19 11:07:59 +0000416 return scan_dev_name_common(d, n, 4, ptr);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000417 if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000418 return 10;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000419 if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000420 return 11;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000421 if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
Eric Andersenf18bd892003-12-19 11:07:59 +0000422 return 12;
Eric Andersenf18bd892003-12-19 11:07:59 +0000423 return 0;
424}
425
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000426/* Public functions follow */
427
Denis Vlasenko06af2162007-02-03 17:28:39 +0000428int devfsd_main (int argc, char **argv);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000429int devfsd_main (int argc, char **argv)
430{
431 int print_version = FALSE;
432 int do_daemon = TRUE;
433 int no_polling = FALSE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000434 int do_scan;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000435 int fd, proto_rev, count;
436 unsigned long event_mask = 0;
437 struct sigaction new_action;
438 struct initial_symlink_struct *curr;
439
440 if (argc < 2)
441 bb_show_usage();
442
443 for (count = 2; count < argc; ++count)
444 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000445 if(argv[count][0] == '-')
446 {
447 if(argv[count][1]=='v' && !argv[count][2]) /* -v */
448 print_version = TRUE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000449 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f'
Rob Landley1ba19d62005-10-08 17:42:35 +0000450 && argv[count][2]=='g' && !argv[count][3]) /* -fg */
Eric Andersenf18bd892003-12-19 11:07:59 +0000451 do_daemon = FALSE;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000452 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n'
Rob Landley1ba19d62005-10-08 17:42:35 +0000453 && argv[count][2]=='p' && !argv[count][3]) /* -np */
Eric Andersenf18bd892003-12-19 11:07:59 +0000454 no_polling = TRUE;
Eric Andersenf18bd892003-12-19 11:07:59 +0000455 else
456 bb_show_usage();
457 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000458 }
459
Eric Andersenf18bd892003-12-19 11:07:59 +0000460 /* strip last / from mount point, so we don't need to check for it later */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000461 while (argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
Eric Andersenf18bd892003-12-19 11:07:59 +0000462 argv[1][strlen(argv[1]) -1] = '\0';
463
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000464 mount_point = argv[1];
465
466 if (chdir (mount_point) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000467 devfsd_perror_msg_and_die(mount_point);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000468
Rob Landleyd921b2e2006-08-03 15:41:12 +0000469 fd = xopen (".devfsd", O_RDONLY);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000470
471 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +0000472 devfsd_perror_msg_and_die("FD_CLOEXEC");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000473
Eric Andersena68ea1c2006-01-30 22:48:39 +0000474 if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
475 msg_logger_and_die(LOG_ERR, "ioctl");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000476
477 /*setup initial entries */
478 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
479 symlink (curr->dest, curr->name);
480
481 /* NB: The check for CONFIG_FILE is done in read_config_file() */
482
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000483 if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
484 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000485 applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
486 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000487 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
488 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
489 exit(EXIT_SUCCESS); /* -v */
490 }
491 /* Tell kernel we are special (i.e. we get to see hidden entries) */
Rob Landley1ba19d62005-10-08 17:42:35 +0000492 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000493
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000494 sigemptyset(&new_action.sa_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000495 new_action.sa_flags = 0;
496
497 /* Set up SIGHUP and SIGUSR1 handlers */
498 new_action.sa_handler = signal_handler;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000499 if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
Rob Landley1ba19d62005-10-08 17:42:35 +0000500 devfsd_error_msg_and_die( "sigaction");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000501
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000502 printf("%s v%s started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000503
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000504 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000505 umask(0);
506 read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000507 /* Do the scan before forking, so that boot scripts see the finished product */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000508 dir_operation(SERVICE,mount_point,0,NULL);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000509
Rob Landley1ba19d62005-10-08 17:42:35 +0000510 if (ENABLE_DEVFSD_FG_NP && no_polling)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000511 exit (0);
512 if (do_daemon)
513 {
514 /* Release so that the child can grab it */
Rob Landley1ba19d62005-10-08 17:42:35 +0000515 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000516 fork_and_execute(DIE, NULL, NULL);
517 setsid (); /* Prevent hangups and become pgrp leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000518 } else if(ENABLE_DEVFSD_FG_NP) {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000519 setpgid (0, 0); /* Become process group leader */
Rob Landley1ba19d62005-10-08 17:42:35 +0000520 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000521
522 while (TRUE)
523 {
Eric Andersenf18bd892003-12-19 11:07:59 +0000524 do_scan = do_servicing (fd, event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000525
526 free_config ();
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000527 read_config_file ((char*)CONFIG_FILE, FALSE, &event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000528 if (do_scan)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000529 dir_operation(SERVICE,mount_point,0,NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000530 }
531} /* End Function main */
532
533
534/* Private functions follow */
535
Eric Andersenf18bd892003-12-19 11:07:59 +0000536static void read_config_file (char *path, int optional, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000537/* [SUMMARY] Read a configuration database.
538 <path> The path to read the database from. If this is a directory, all
539 entries in that directory will be read (except hidden entries).
540 <optional> If TRUE, the routine will silently ignore a missing config file.
541 <event_mask> The event mask is written here. This is not initialised.
542 [RETURNS] Nothing.
543*/
544{
545 struct stat statbuf;
546 FILE *fp;
547 char buf[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000548 char *line = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000549
Rob Landley1ba19d62005-10-08 17:42:35 +0000550 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000551
Rob Landley06813d02005-06-07 03:47:00 +0000552 if (stat (path, &statbuf) == 0 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000553 {
Rob Landley06813d02005-06-07 03:47:00 +0000554 /* Don't read 0 length files: ignored */
555 /*if( statbuf.st_size == 0 )
556 return;*/
557 if ( S_ISDIR (statbuf.st_mode) )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000558 {
Rob Landley06813d02005-06-07 03:47:00 +0000559 /* strip last / from dirname so we don't need to check for it later */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000560 while (path && path[1]!='\0' && path[strlen(path)-1] == '/')
Rob Landley06813d02005-06-07 03:47:00 +0000561 path[strlen(path) -1] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000562
Rob Landley06813d02005-06-07 03:47:00 +0000563 dir_operation(READ_CONFIG, path, 0, event_mask);
564 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000565 }
Rob Landley06813d02005-06-07 03:47:00 +0000566 if ( ( fp = fopen (path, "r") ) != NULL )
567 {
568 while (fgets (buf, STRING_LENGTH, fp) != NULL)
569 {
570 /* Skip whitespace */
571 for (line = buf; isspace (*line); ++line)
572 /*VOID*/;
573 if (line[0] == '\0' || line[0] == '#' )
574 continue;
575 process_config_line (line, event_mask);
576 }
577 fclose (fp);
578 } else {
579 goto read_config_file_err;
580 }
581 } else {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000582read_config_file_err:
Eric Andersenf18bd892003-12-19 11:07:59 +0000583 if(optional == 0 && errno == ENOENT)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000584 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
Rob Landley06813d02005-06-07 03:47:00 +0000585 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000586} /* End Function read_config_file */
587
588static void process_config_line (const char *line, unsigned long *event_mask)
589/* [SUMMARY] Process a line from a configuration file.
590 <line> The configuration line.
591 <event_mask> The event mask is written here. This is not initialised.
592 [RETURNS] Nothing.
593*/
594{
595 int num_args, count;
596 struct config_entry_struct *new;
597 char p[MAX_ARGS][STRING_LENGTH];
598 char when[STRING_LENGTH], what[STRING_LENGTH];
599 char name[STRING_LENGTH];
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000600 const char *msg = "";
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000601 char *ptr;
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000602 int i;
Eric Andersenf18bd892003-12-19 11:07:59 +0000603
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000604 /* !!!! Only Uppercase Keywords in devsfd.conf */
605 static const char *const options[] = {
606 "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
607 "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
608 "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
609 "RMOLDCOMPAT", "RMNEWCOMPAT", 0
610 };
Rob Landley1ba19d62005-10-08 17:42:35 +0000611
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000612 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000613
614 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
615 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
616 when, name, what,
617 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000618
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000619 i = index_in_str_array(options, when );
Eric Andersenf18bd892003-12-19 11:07:59 +0000620
621 /*"CLEAR_CONFIG"*/
622 if( i == 0)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000623 {
624 free_config ();
625 *event_mask = 0;
626 return;
627 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000628
629 if ( num_args < 2)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000630 goto process_config_line_err;
631
Eric Andersenf18bd892003-12-19 11:07:59 +0000632 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
633 if( i == 1 || i == 2 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000634 {
635 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
Rob Landley1ba19d62005-10-08 17:42:35 +0000636 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000637 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
638 return;
639 }
Eric Andersenf18bd892003-12-19 11:07:59 +0000640 /* "RESTORE" */
641 if( i == 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000642 {
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000643 dir_operation(RESTORE,name, strlen (name),NULL);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000644 return;
645 }
646 if (num_args < 3)
647 goto process_config_line_err;
648
649 new = xmalloc (sizeof *new);
650 memset (new, 0, sizeof *new);
651
652 for (count = 0; event_types[count].config_name != NULL; ++count)
653 {
654 if (strcasecmp (when, event_types[count].config_name) != 0)
655 continue;
656 new->action.when = event_types[count].type;
657 break;
658 }
659 if (event_types[count].config_name == NULL)
660 {
661 msg="WHEN in";
662 goto process_config_line_err;
663 }
664
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000665 i = index_in_str_array(options, what );
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000666
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000667 switch (i)
Eric Andersenf18bd892003-12-19 11:07:59 +0000668 {
669 case 4: /* "PERMISSIONS" */
670 new->action.what = AC_PERMISSIONS;
671 /* Get user and group */
672 if ( ( ptr = strchr (p[0], '.') ) == NULL )
673 {
674 msg="UID.GID";
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000675 goto process_config_line_err; /*"missing '.' in UID.GID"*/
Eric Andersenf18bd892003-12-19 11:07:59 +0000676 }
677
678 *ptr++ = '\0';
679 new->u.permissions.uid = get_uid_gid (UID, p[0]);
680 new->u.permissions.gid = get_uid_gid (GID, ptr);
681 /* Get mode */
682 new->u.permissions.mode = get_mode (p[1]);
683 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000684 case 5: /* MODLOAD */
685 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
686 the device name) to the module loading facility. In addition,
687 the /etc/modules.devfs configuration file is used.*/
Rob Landley1ba19d62005-10-08 17:42:35 +0000688 if (ENABLE_DEVFSD_MODLOAD)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000689 new->action.what = AC_MODLOAD;
Eric Andersenf18bd892003-12-19 11:07:59 +0000690 break;
Eric Andersenf18bd892003-12-19 11:07:59 +0000691 case 6: /* EXECUTE */
692 new->action.what = AC_EXECUTE;
693 num_args -= 3;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000694
Eric Andersenf18bd892003-12-19 11:07:59 +0000695 for (count = 0; count < num_args; ++count)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000696 new->u.execute.argv[count] = xstrdup (p[count]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000697
Eric Andersenf18bd892003-12-19 11:07:59 +0000698 new->u.execute.argv[num_args] = NULL;
699 break;
700 case 7: /* COPY */
701 new->action.what = AC_COPY;
702 num_args -= 3;
703 if (num_args != 2)
704 goto process_config_line_err; /* missing path and function in line */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000705
Rob Landleyd921b2e2006-08-03 15:41:12 +0000706 new->u.copy.source = xstrdup (p[0]);
707 new->u.copy.destination = xstrdup (p[1]);
Eric Andersenf18bd892003-12-19 11:07:59 +0000708 break;
709 case 8: /* IGNORE */
710 /* FALLTROUGH */
711 case 9: /* MKOLDCOMPAT */
712 /* FALLTROUGH */
713 case 10: /* MKNEWCOMPAT */
714 /* FALLTROUGH */
715 case 11:/* RMOLDCOMPAT */
716 /* FALLTROUGH */
717 case 12: /* RMNEWCOMPAT */
718 /* AC_IGNORE 6
719 AC_MKOLDCOMPAT 7
720 AC_MKNEWCOMPAT 8
721 AC_RMOLDCOMPAT 9
722 AC_RMNEWCOMPAT 10*/
723 new->action.what = i - 2;
724 break;
725 default:
726 msg ="WHAT in";
727 goto process_config_line_err;
728 /*esac*/
729 } /* switch (i) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000730
731 xregcomp( &new->preg, name, REG_EXTENDED);
732
733 *event_mask |= 1 << new->action.when;
734 new->next = NULL;
735 if (first_config == NULL)
736 first_config = new;
737 else
738 last_config->next = new;
739 last_config = new;
740 return;
741process_config_line_err:
Rob Landley1ba19d62005-10-08 17:42:35 +0000742 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000743} /* End Function process_config_line */
744
745static int do_servicing (int fd, unsigned long event_mask)
746/* [SUMMARY] Service devfs changes until a signal is received.
747 <fd> The open control file.
748 <event_mask> The event mask.
749 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
750*/
751{
752 ssize_t bytes;
753 struct devfsd_notify_struct info;
754 unsigned long tmp_event_mask;
755
Rob Landley1ba19d62005-10-08 17:42:35 +0000756 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000757
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000758 /* Tell devfs what events we care about */
759 tmp_event_mask = event_mask;
Rob Landley1ba19d62005-10-08 17:42:35 +0000760 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000761 while (!caught_signal)
762 {
763 errno = 0;
764 bytes = read (fd, (char *) &info, sizeof info);
765 if (caught_signal)
766 break; /* Must test for this first */
767 if (errno == EINTR)
768 continue; /* Yes, the order is important */
769 if (bytes < 1)
770 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000771 service_name (&info);
772 }
773 if (caught_signal)
774 {
775 int c_sighup = caught_sighup;
776
777 caught_signal = FALSE;
778 caught_sighup = FALSE;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000779 return c_sighup;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000780 }
Rob Landley1ba19d62005-10-08 17:42:35 +0000781 msg_logger_and_die(LOG_ERR, "read error on control file");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000782} /* End Function do_servicing */
783
784static void service_name (const struct devfsd_notify_struct *info)
785/* [SUMMARY] Service a single devfs change.
786 <info> The devfs change.
787 [RETURNS] Nothing.
788*/
789{
790 unsigned int n;
791 regmatch_t mbuf[MAX_SUBEXPR];
792 struct config_entry_struct *entry;
793
Rob Landley1ba19d62005-10-08 17:42:35 +0000794 debug_msg_logger(LOG_INFO, __FUNCTION__);
795 if (ENABLE_DEBUG && info->overrun_count > 0)
796 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000797
798 /* Discard lookups on "/dev/log" and "/dev/initctl" */
Eric Andersenf18bd892003-12-19 11:07:59 +0000799 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
800 ((info->devname[0]=='l' && info->devname[1]=='o' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000801 info->devname[2]=='g' && !info->devname[3]) ||
Eric Andersenf18bd892003-12-19 11:07:59 +0000802 ( info->devname[0]=='i' && info->devname[1]=='n' &&
Glenn L McGrath5b0d7de2004-02-04 08:27:57 +0000803 info->devname[2]=='i' && info->devname[3]=='t' &&
804 info->devname[4]=='c' && info->devname[5]=='t' &&
805 info->devname[6]=='l' && !info->devname[7])))
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000806 return;
807 for (entry = first_config; entry != NULL; entry = entry->next)
808 {
809 /* First check if action matches the type, then check if name matches */
810 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
811 continue;
812 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
813 /* VOID */;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000814
Rob Landley1ba19d62005-10-08 17:42:35 +0000815 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000816
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000817 switch (entry->action.what)
818 {
819 case AC_PERMISSIONS:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000820 action_permissions (info, entry);
821 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000822 case AC_MODLOAD:
Rob Landley1ba19d62005-10-08 17:42:35 +0000823 if(ENABLE_DEVFSD_MODLOAD)
824 action_modload (info, entry);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000825 break;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000826 case AC_EXECUTE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000827 action_execute (info, entry, mbuf, n);
828 break;
829 case AC_COPY:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000830 action_copy (info, entry, mbuf, n);
831 break;
832 case AC_IGNORE:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000833 return;
834 /*break;*/
835 case AC_MKOLDCOMPAT:
836 case AC_MKNEWCOMPAT:
837 case AC_RMOLDCOMPAT:
838 case AC_RMNEWCOMPAT:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000839 action_compat (info, entry->action.what);
840 break;
841 default:
Rob Landley1ba19d62005-10-08 17:42:35 +0000842 msg_logger_and_die(LOG_ERR, "Unknown action");
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000843 }
844 }
845} /* End Function service_name */
846
847static void action_permissions (const struct devfsd_notify_struct *info,
848 const struct config_entry_struct *entry)
849/* [SUMMARY] Update permissions for a device entry.
850 <info> The devfs change.
851 <entry> The config file entry.
852 [RETURNS] Nothing.
853*/
854{
855 struct stat statbuf;
856
Rob Landley1ba19d62005-10-08 17:42:35 +0000857 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000858
859 if ( stat (info->devname, &statbuf) != 0 ||
860 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
861 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
862 {
Rob Landley1ba19d62005-10-08 17:42:35 +0000863 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000864 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000865} /* End Function action_permissions */
866
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000867static void action_modload (const struct devfsd_notify_struct *info,
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +0000868 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000869/* [SUMMARY] Load a module.
870 <info> The devfs change.
871 <entry> The config file entry.
872 [RETURNS] Nothing.
873*/
874{
875 char *argv[6];
876 char device[STRING_LENGTH];
877
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000878 argv[0] = (char*)MODPROBE;
879 argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
880 argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
881 argv[3] = (char*)CONFIG_MODULES_DEVFS;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000882 argv[4] = device;
883 argv[5] = NULL;
Eric Andersenf18bd892003-12-19 11:07:59 +0000884
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000885 snprintf (device, sizeof (device), "/dev/%s", info->devname);
Rob Landley1ba19d62005-10-08 17:42:35 +0000886 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 +0000887 fork_and_execute(DIE, argv[0], argv);
888} /* End Function action_modload */
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000889
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000890static void action_execute (const struct devfsd_notify_struct *info,
891 const struct config_entry_struct *entry,
892 const regmatch_t *regexpr, unsigned int numexpr)
893/* [SUMMARY] Execute a programme.
894 <info> The devfs change.
895 <entry> The config file entry.
896 <regexpr> The number of subexpression (start, end) offsets within the
897 device name.
898 <numexpr> The number of elements within <<regexpr>>.
899 [RETURNS] Nothing.
900*/
901{
902 unsigned int count;
903 struct get_variable_info gv_info;
904 char *argv[MAX_ARGS + 1];
905 char largv[MAX_ARGS + 1][STRING_LENGTH];
906
Rob Landley1ba19d62005-10-08 17:42:35 +0000907 debug_msg_logger(LOG_INFO ,__FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000908 gv_info.info = info;
909 gv_info.devname = info->devname;
910 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
911 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
912 {
913 expand_expression (largv[count], STRING_LENGTH,
914 entry->u.execute.argv[count],
915 get_variable, &gv_info,
916 gv_info.devname, regexpr, numexpr );
917 argv[count] = largv[count];
918 }
919 argv[count] = NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000920 fork_and_execute(NO_DIE, argv[0], argv);
921} /* End Function action_execute */
922
923
924static void action_copy (const struct devfsd_notify_struct *info,
925 const struct config_entry_struct *entry,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000926 const regmatch_t *regexpr, unsigned int numexpr)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000927/* [SUMMARY] Copy permissions.
928 <info> The devfs change.
929 <entry> The config file entry.
930 <regexpr> This list of subexpression (start, end) offsets within the
931 device name.
932 <numexpr> The number of elements in <<regexpr>>.
933 [RETURNS] Nothing.
934*/
935{
936 mode_t new_mode;
937 struct get_variable_info gv_info;
938 struct stat source_stat, dest_stat;
939 char source[STRING_LENGTH], destination[STRING_LENGTH];
Rob Landley1ba19d62005-10-08 17:42:35 +0000940 int ret = 0;
941
942 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000943
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000944 dest_stat.st_mode = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000945
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000946 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
947 return;
948 gv_info.info = info;
949 gv_info.devname = info->devname;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000950
Eric Andersenf18bd892003-12-19 11:07:59 +0000951 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000952 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
953 get_variable, &gv_info, gv_info.devname,
954 regexpr, numexpr);
955
956 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
957 get_variable, &gv_info, gv_info.devname,
958 regexpr, numexpr);
959
960 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +0000961 return;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000962 lstat (destination, &dest_stat);
963 new_mode = source_stat.st_mode & ~S_ISVTX;
964 if (info->type == DEVFSD_NOTIFY_CREATE)
965 new_mode |= S_ISVTX;
966 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
967 new_mode |= S_ISVTX;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000968 ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
Rob Landley1ba19d62005-10-08 17:42:35 +0000969 if (ENABLE_DEBUG && ret && (errno != EEXIST))
970 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000971} /* End Function action_copy */
972
Eric Andersenf18bd892003-12-19 11:07:59 +0000973static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000974/* [SUMMARY] Process a compatibility request.
975 <info> The devfs change.
976 <action> The action to take.
977 [RETURNS] Nothing.
978*/
979{
Rob Landley1ba19d62005-10-08 17:42:35 +0000980 int ret;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000981 const char *compat_name = NULL;
982 const char *dest_name = info->devname;
Eric Andersenf18bd892003-12-19 11:07:59 +0000983 char *ptr=NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +0000984 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +0000985 int mode, host, bus, target, lun;
986 unsigned int i;
987 char rewind_;
988 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
Rob Landley0a7c8ef2006-02-22 17:01:00 +0000989 static const char *const fmt[] = {
990 NULL ,
991 "sg/c%db%dt%du%d", /* scsi/generic */
992 "sd/c%db%dt%du%d", /* scsi/disc */
993 "sr/c%db%dt%du%d", /* scsi/cd */
994 "sd/c%db%dt%du%dp%d", /* scsi/part */
995 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
996 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
997 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
998 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
999 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
1000 NULL
1001 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001002
1003 /* First construct compatibility name */
1004 switch (action)
1005 {
1006 case AC_MKOLDCOMPAT:
1007 case AC_RMOLDCOMPAT:
1008 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1009 break;
1010 case AC_MKNEWCOMPAT:
1011 case AC_RMNEWCOMPAT:
Eric Andersenf18bd892003-12-19 11:07:59 +00001012 ptr = strrchr (info->devname, '/') + 1;
1013 i=scan_dev_name(info->devname, info->namelen, ptr);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001014
Rob Landley1ba19d62005-10-08 17:42:35 +00001015 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001016
1017 /* nothing found */
1018 if(i==0 || i > 9)
1019 return;
1020
1021 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1022 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001023 dest_name = dest_buf;
Eric Andersenf18bd892003-12-19 11:07:59 +00001024 compat_name = compat_buf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001025
Eric Andersenf18bd892003-12-19 11:07:59 +00001026
1027 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1028 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1029 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1030
1031 /* 4 == scsi/part 8 == ide/host/part */
1032 if( i == 4 || i == 8)
1033 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1034
1035 /* 5 == scsi/mt */
1036 if( i == 5)
1037 {
1038 rewind_ = info->devname[info->namelen - 1];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001039 if (rewind_ != 'n')
1040 rewind_ = '\0';
Eric Andersenf18bd892003-12-19 11:07:59 +00001041 mode=0;
1042 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1043 mode = ptr[2] - 107; /* 1 or 2 */
1044 if(ptr[2] == 'a')
1045 mode = 3;
1046 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001047 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001048
Eric Andersenf18bd892003-12-19 11:07:59 +00001049 /* 9 == ide/host/mt */
1050 if( i == 9 )
1051 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1052 /* esac */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001053 } /* switch (action) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001054
1055 if(compat_name == NULL )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001056 return;
Eric Andersenf18bd892003-12-19 11:07:59 +00001057
Rob Landley1ba19d62005-10-08 17:42:35 +00001058 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
Eric Andersenf18bd892003-12-19 11:07:59 +00001059
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001060 /* Now decide what to do with it */
1061 switch (action)
1062 {
1063 case AC_MKOLDCOMPAT:
1064 case AC_MKNEWCOMPAT:
1065 mksymlink (dest_name, compat_name);
1066 break;
1067 case AC_RMOLDCOMPAT:
1068 case AC_RMNEWCOMPAT:
Rob Landley1ba19d62005-10-08 17:42:35 +00001069 ret = unlink (compat_name);
1070 if (ENABLE_DEBUG && ret)
1071 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001072 break;
Eric Andersenf18bd892003-12-19 11:07:59 +00001073 /*esac*/
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001074 } /* switch (action) */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001075} /* End Function action_compat */
1076
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001077static void restore(char *spath, struct stat source_stat, int rootlen)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001078{
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001079 char dpath[STRING_LENGTH];
1080 struct stat dest_stat;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001081
Rob Landley1ba19d62005-10-08 17:42:35 +00001082 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001083
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001084 dest_stat.st_mode = 0;
1085 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1086 lstat (dpath, &dest_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001087
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001088 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1089 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001090
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001091 if ( S_ISDIR (source_stat.st_mode) )
1092 dir_operation(RESTORE, spath, rootlen,NULL);
1093}
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001094
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001095
1096static int copy_inode (const char *destpath, const struct stat *dest_stat,
1097 mode_t new_mode,
1098 const char *sourcepath, const struct stat *source_stat)
1099/* [SUMMARY] Copy an inode.
1100 <destpath> The destination path. An existing inode may be deleted.
1101 <dest_stat> The destination stat(2) information.
1102 <new_mode> The desired new mode for the destination.
1103 <sourcepath> The source path.
1104 <source_stat> The source stat(2) information.
1105 [RETURNS] TRUE on success, else FALSE.
1106*/
1107{
Eric Andersenf18bd892003-12-19 11:07:59 +00001108 int source_len, dest_len;
1109 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1110 int fd, val;
1111 struct sockaddr_un un_addr;
1112 char symlink_val[STRING_LENGTH];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001113
Rob Landley1ba19d62005-10-08 17:42:35 +00001114 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001115
1116 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1117 {
1118 /* Same type */
1119 if ( S_ISLNK (source_stat->st_mode) )
1120 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001121 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1122 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001123 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001124 source_link[source_len] = '\0';
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001125 dest_link[dest_len] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001126 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1127 {
1128 unlink (destpath);
1129 symlink (source_link, destpath);
1130 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001131 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001132 } /* Else not a symlink */
1133 chmod (destpath, new_mode & ~S_IFMT);
1134 chown (destpath, source_stat->st_uid, source_stat->st_gid);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001135 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001136 }
1137 /* Different types: unlink and create */
1138 unlink (destpath);
1139 switch (source_stat->st_mode & S_IFMT)
1140 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001141 case S_IFSOCK:
1142 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1143 break;
1144 un_addr.sun_family = AF_UNIX;
1145 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1146 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1147 close (fd);
1148 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1149 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001150 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001151 case S_IFLNK:
1152 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1153 break;
1154 symlink_val[val] = '\0';
1155 if (symlink (symlink_val, destpath) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001156 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001157 break;
1158 case S_IFREG:
1159 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1160 break;
1161 close (fd);
1162 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1163 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001164 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001165 case S_IFBLK:
1166 case S_IFCHR:
1167 case S_IFIFO:
1168 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1169 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001170 goto do_chown;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001171 case S_IFDIR:
1172 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1173 break;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001174do_chown:
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001175 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001176 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001177 /*break;*/
1178 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001179 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001180} /* End Function copy_inode */
1181
Bernhard Reutner-Fischerf0877982006-06-25 22:08:53 +00001182static void free_config (void)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001183/* [SUMMARY] Free the configuration information.
1184 [RETURNS] Nothing.
1185*/
1186{
1187 struct config_entry_struct *c_entry;
1188 void *next;
1189
Rob Landley1ba19d62005-10-08 17:42:35 +00001190 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001191
1192 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1193 {
1194 unsigned int count;
1195
1196 next = c_entry->next;
1197 regfree (&c_entry->preg);
1198 if (c_entry->action.what == AC_EXECUTE)
1199 {
1200 for (count = 0; count < MAX_ARGS; ++count)
1201 {
1202 if (c_entry->u.execute.argv[count] == NULL)
1203 break;
1204 free (c_entry->u.execute.argv[count]);
1205 }
1206 }
1207 free (c_entry);
1208 }
1209 first_config = NULL;
1210 last_config = NULL;
1211} /* End Function free_config */
1212
1213static int get_uid_gid (int flag, const char *string)
1214/* [SUMMARY] Convert a string to a UID or GID value.
1215 <flag> "UID" or "GID".
1216 <string> The string.
1217 [RETURNS] The UID or GID value.
1218*/
1219{
1220 struct passwd *pw_ent;
1221 struct group *grp_ent;
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001222 static const char *msg;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001223
Rob Landley1ba19d62005-10-08 17:42:35 +00001224 if (ENABLE_DEVFSD_VERBOSE)
1225 msg="user";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001226
Rob Landley1ba19d62005-10-08 17:42:35 +00001227 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001228
Rob Landley1ba19d62005-10-08 17:42:35 +00001229 if(ENABLE_DEBUG && flag != UID && flag != GID)
1230 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001231
1232 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001233 return atoi(string);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001234
1235 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001236 return pw_ent->pw_uid;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001237
1238 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001239 return grp_ent->gr_gid;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001240 else if(ENABLE_DEVFSD_VERBOSE)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001241 msg="group";
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001242
Rob Landley1ba19d62005-10-08 17:42:35 +00001243 if(ENABLE_DEVFSD_VERBOSE)
1244 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001245 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001246}/* End Function get_uid_gid */
1247
1248static mode_t get_mode (const char *string)
1249/* [SUMMARY] Convert a string to a mode value.
1250 <string> The string.
1251 [RETURNS] The mode value.
1252*/
1253{
1254 mode_t mode;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001255 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001256
1257 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001258
1259 if ( isdigit (string[0]) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001260 return strtoul(string, NULL, 8);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001261 if (strlen (string) != 9)
Rob Landley1ba19d62005-10-08 17:42:35 +00001262 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001263
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001264 mode = 0;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001265 i= S_IRUSR;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001266 while (i>0)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001267 {
1268 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1269 mode+=i;
1270 i=i/2;
1271 string++;
1272 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001273 return mode;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001274} /* End Function get_mode */
1275
1276static void signal_handler (int sig)
1277{
Rob Landley1ba19d62005-10-08 17:42:35 +00001278 debug_msg_logger(LOG_INFO, __FUNCTION__);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001279
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001280 caught_signal = TRUE;
1281 if (sig == SIGHUP)
1282 caught_sighup = TRUE;
Rob Landley1ba19d62005-10-08 17:42:35 +00001283
1284 msg_logger(LOG_INFO, "Caught signal %d", sig);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001285} /* End Function signal_handler */
1286
1287static const char *get_variable (const char *variable, void *info)
1288{
1289 struct get_variable_info *gv_info = info;
1290 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
Eric Andersenf18bd892003-12-19 11:07:59 +00001291 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1292 "uid", "gid", "mode", hostname, mount_point,
1293 gv_info->devpath, gv_info->devname, 0 };
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +00001294 int i;
Rob Landley1ba19d62005-10-08 17:42:35 +00001295
1296 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001297
1298 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
Rob Landley1ba19d62005-10-08 17:42:35 +00001299 msg_logger_and_die(LOG_ERR, "gethostname: %m");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001300
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001301 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
Eric Andersenf18bd892003-12-19 11:07:59 +00001302 hostname[STRING_LENGTH - 1] = '\0';
1303
Denis Vlasenko5af906e2006-11-05 18:05:09 +00001304 /* index_in_str_array returns i>=0 */
1305 i=index_in_str_array(field_names, variable);
Eric Andersenf18bd892003-12-19 11:07:59 +00001306
Rob Landley176f2df2006-06-08 14:19:48 +00001307 if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL))
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001308 return NULL;
Rob Landley176f2df2006-06-08 14:19:48 +00001309 if( i >= 0 && i <= 3)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001310 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001311 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001312 return field_names[i+7];
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001313 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001314
1315 if(i == 4 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001316 sprintf (sbuf, "%u", gv_info->info->uid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001317 else if(i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001318 sprintf (sbuf, "%u", gv_info->info->gid);
Eric Andersenf18bd892003-12-19 11:07:59 +00001319 else if(i == 6)
1320 sprintf (sbuf, "%o", gv_info->info->mode);
Rob Landley1ba19d62005-10-08 17:42:35 +00001321
1322 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
1323
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001324 return sbuf;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001325} /* End Function get_variable */
1326
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001327static void service(struct stat statbuf, char *path)
1328{
1329 struct devfsd_notify_struct info;
1330
Rob Landley1ba19d62005-10-08 17:42:35 +00001331 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001332
1333 memset (&info, 0, sizeof info);
1334 info.type = DEVFSD_NOTIFY_REGISTERED;
1335 info.mode = statbuf.st_mode;
1336 info.major = major (statbuf.st_rdev);
1337 info.minor = minor (statbuf.st_rdev);
1338 info.uid = statbuf.st_uid;
1339 info.gid = statbuf.st_gid;
1340 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1341 info.namelen = strlen (info.devname);
1342 service_name (&info);
1343 if ( S_ISDIR (statbuf.st_mode) )
1344 dir_operation(SERVICE,path,0,NULL);
1345}
1346
1347static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001348/* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001349 <flag> To choose which function to perform
1350 <dp> The directory pointer. This is closed upon completion.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001351 <dir_name> The name of the directory.
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001352 <rootlen> string length parameter.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001353 [RETURNS] Nothing.
1354*/
1355{
1356 struct stat statbuf;
1357 DIR *dp;
1358 struct dirent *de;
1359 char path[STRING_LENGTH];
1360
Rob Landley1ba19d62005-10-08 17:42:35 +00001361 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001362
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001363 if((dp = opendir( dir_name))==NULL)
1364 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001365 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001366 return;
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001367 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001368
1369 while ( (de = readdir (dp) ) != NULL )
1370 {
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001371
Eric Andersenf18bd892003-12-19 11:07:59 +00001372 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 +00001373 continue;
Eric Andersenf18bd892003-12-19 11:07:59 +00001374 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
Rob Landley1ba19d62005-10-08 17:42:35 +00001375 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001376
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001377 if (lstat (path, &statbuf) != 0)
1378 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001379 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001380 continue;
1381 }
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001382 switch (type)
Glenn L McGrath3860b2e2003-11-30 23:46:06 +00001383 {
1384 case SERVICE:
1385 service(statbuf,path);
1386 break;
1387 case RESTORE:
1388 restore(path, statbuf, var);
1389 break;
1390 case READ_CONFIG:
1391 read_config_file (path, var, event_mask);
1392 break;
1393 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001394 }
1395 closedir (dp);
1396} /* End Function do_scan_and_service */
1397
1398static int mksymlink (const char *oldpath, const char *newpath)
1399/* [SUMMARY] Create a symlink, creating intervening directories as required.
1400 <oldpath> The string contained in the symlink.
1401 <newpath> The name of the new symlink.
1402 [RETURNS] 0 on success, else -1.
1403*/
1404{
Rob Landley1ba19d62005-10-08 17:42:35 +00001405 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001406
1407 if ( !make_dir_tree (newpath) )
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001408 return -1;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001409
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001410 if (symlink (oldpath, newpath) != 0) {
1411 if (errno != EEXIST) {
Rob Landley1ba19d62005-10-08 17:42:35 +00001412 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001413 return -1;
Eric Andersenf18bd892003-12-19 11:07:59 +00001414 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001415 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001416 return 0;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001417} /* End Function mksymlink */
1418
1419
1420static int make_dir_tree (const char *path)
1421/* [SUMMARY] Creating intervening directories for a path as required.
Eric Andersen52899692003-10-22 10:10:50 +00001422 <path> The full pathname (including the leaf node).
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001423 [RETURNS] TRUE on success, else FALSE.
1424*/
1425{
Rob Landley1ba19d62005-10-08 17:42:35 +00001426 debug_msg_logger(LOG_INFO, __FUNCTION__);
1427
Eric Andersen0e020d12004-10-13 06:25:52 +00001428 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001429 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001430 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001431 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001432 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001433 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001434} /* End Function make_dir_tree */
1435
1436static int expand_expression(char *output, unsigned int outsize,
1437 const char *input,
1438 const char *(*get_variable_func)(const char *variable, void *info),
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001439 void *info,
1440 const char *devname,
1441 const regmatch_t *ex, unsigned int numexp)
Eric Andersenaff114c2004-04-14 17:51:38 +00001442/* [SUMMARY] Expand environment variables and regular subexpressions in string.
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001443 <output> The output expanded expression is written here.
1444 <length> The size of the output buffer.
1445 <input> The input expression. This may equal <<output>>.
1446 <get_variable> A function which will be used to get variable values. If
1447 this returns NULL, the environment is searched instead. If this is NULL,
1448 only the environment is searched.
1449 <info> An arbitrary pointer passed to <<get_variable>>.
1450 <devname> Device name; specifically, this is the string that contains all
1451 of the regular subexpressions.
1452 <ex> Array of start / end offsets into info->devname for each subexpression
1453 <numexp> Number of regular subexpressions found in <<devname>>.
1454 [RETURNS] TRUE on success, else FALSE.
1455*/
1456{
1457 char temp[STRING_LENGTH];
1458
Rob Landley1ba19d62005-10-08 17:42:35 +00001459 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001460
1461 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001462 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001463 expand_regexp (output, outsize, temp, devname, ex, numexp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001464 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001465} /* End Function expand_expression */
1466
1467static void expand_regexp (char *output, size_t outsize, const char *input,
1468 const char *devname,
1469 const regmatch_t *ex, unsigned int numex )
1470/* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1471 <output> The output expanded expression is written here.
1472 <outsize> The size of the output buffer.
1473 <input> The input expression. This may NOT equal <<output>>, because
1474 supporting that would require yet another string-copy. However, it's not
1475 hard to write a simple wrapper function to add this functionality for those
1476 few cases that need it.
1477 <devname> Device name; specifically, this is the string that contains all
1478 of the regular subexpressions.
1479 <ex> An array of start and end offsets into <<devname>>, one for each
1480 subexpression
1481 <numex> Number of subexpressions in the offset-array <<ex>>.
1482 [RETURNS] Nothing.
1483*/
1484{
1485 const char last_exp = '0' - 1 + numex;
1486 int c = -1;
1487
Rob Landley1ba19d62005-10-08 17:42:35 +00001488 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001489
1490 /* Guarantee NULL termination by writing an explicit '\0' character into
1491 the very last byte */
1492 if (outsize)
1493 output[--outsize] = '\0';
1494 /* Copy the input string into the output buffer, replacing '\\' with '\'
1495 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1496 codes are deleted */
1497 while ( (c != '\0') && (outsize != 0) )
1498 {
1499 c = *input;
1500 ++input;
1501 if (c == '\\')
1502 {
1503 c = *input;
1504 ++input;
1505 if (c != '\\')
1506 {
1507 if ((c >= '0') && (c <= last_exp))
1508 {
1509 const regmatch_t *subexp = ex + (c - '0');
1510 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1511
1512 /* Range checking */
1513 if (sublen > outsize)
1514 sublen = outsize;
1515 strncpy (output, devname + subexp->rm_so, sublen);
1516 output += sublen;
1517 outsize -= sublen;
1518 }
1519 continue;
1520 }
1521 }
1522 *output = c;
1523 ++output;
1524 --outsize;
1525 } /* while */
1526} /* End Function expand_regexp */
1527
1528
1529/* from compat_name.c */
1530
1531struct translate_struct
1532{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001533 const char *match; /* The string to match to (up to length) */
1534 const char *format; /* Format of output, "%s" takes data past match string,
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001535 NULL is effectively "%s" (just more efficient) */
1536};
1537
1538static struct translate_struct translate_table[] =
1539{
1540 {"sound/", NULL},
1541 {"printers/", "lp%s"},
1542 {"v4l/", NULL},
1543 {"parports/", "parport%s"},
1544 {"fb/", "fb%s"},
1545 {"netlink/", NULL},
1546 {"loop/", "loop%s"},
1547 {"floppy/", "fd%s"},
1548 {"rd/", "ram%s"},
1549 {"md/", "md%s"}, /* Meta-devices */
1550 {"vc/", "tty%s"},
1551 {"misc/", NULL},
1552 {"isdn/", NULL},
1553 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1554 {"i2c/", "i2c-%s"},
1555 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1556 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1557 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1558 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1559 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1560 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1561 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1562 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1563 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1564 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1565 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1566 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1567 {"input/js", "js%s"}, /* Joystick driver */
1568 {NULL, NULL}
1569};
1570
1571const char *get_old_name (const char *devname, unsigned int namelen,
1572 char *buffer, unsigned int major, unsigned int minor)
1573/* [SUMMARY] Translate a kernel-supplied name into an old name.
1574 <devname> The device name provided by the kernel.
1575 <namelen> The length of the name.
1576 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1577 <major> The major number for the device.
1578 <minor> The minor number for the device.
1579 [RETURNS] A pointer to the old name if known, else NULL.
1580*/
1581{
1582 const char *compat_name = NULL;
1583 char *ptr;
1584 struct translate_struct *trans;
Eric Andersenf18bd892003-12-19 11:07:59 +00001585 unsigned int i;
1586 char mode;
1587 int indexx;
1588 const char *pty1;
1589 const char *pty2;
1590 size_t len;
1591 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001592 static const char *const fmt[] = {
1593 NULL ,
1594 "sg%u", /* scsi/generic */
1595 NULL, /* scsi/disc */
1596 "sr%u", /* scsi/cd */
1597 NULL, /* scsi/part */
1598 "nst%u%c", /* scsi/mt */
1599 "hd%c" , /* ide/host/disc */
1600 "hd%c" , /* ide/host/cd */
1601 "hd%c%s", /* ide/host/part */
1602 "%sht%d", /* ide/host/mt */
1603 "sbpcd%u", /* sbp/ */
1604 "vcs%s", /* vcc/ */
1605 "%cty%c%c", /* pty/ */
1606 NULL
1607 };
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001608
Rob Landley1ba19d62005-10-08 17:42:35 +00001609 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001610
1611 for (trans = translate_table; trans->match != NULL; ++trans)
1612 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001613 len = strlen (trans->match);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001614
1615 if (strncmp (devname, trans->match, len) == 0)
1616 {
1617 if (trans->format == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001618 return devname + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001619 sprintf (buffer, trans->format, devname + len);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001620 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001621 }
1622 }
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001623
Eric Andersenf18bd892003-12-19 11:07:59 +00001624 ptr = (strrchr (devname, '/') + 1);
1625 i = scan_dev_name(devname, namelen, ptr);
1626
1627 if( i > 0 && i < 13)
1628 compat_name = buffer;
1629 else
1630 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001631
Rob Landley1ba19d62005-10-08 17:42:35 +00001632 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
Eric Andersenf18bd892003-12-19 11:07:59 +00001633
1634 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1635 if( i == 1 || i == 3 || i == 10 )
1636 sprintf (buffer, fmt[i], minor);
1637
1638 /* 2 ==scsi/disc, 4 == scsi/part */
1639 if( i == 2 || i == 4)
1640 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1641
1642 /* 5 == scsi/mt */
1643 if( i == 5)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001644 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001645 mode = ptr[2];
1646 if (mode == 'n')
1647 mode = '\0';
1648 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1649 if (devname[namelen - 1] != 'n')
1650 ++compat_name;
1651 }
1652 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1653 if( i == 6 || i == 7 || i == 8 )
Rob Landley1ba19d62005-10-08 17:42:35 +00001654 /* last arg should be ignored for i == 6 or i== 7 */
1655 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
Eric Andersenf18bd892003-12-19 11:07:59 +00001656
1657 /* 9 == ide/host/mt */
1658 if( i == 9 )
1659 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1660
1661 /* 11 == vcc/ */
1662 if( i == 11 )
1663 {
1664 sprintf (buffer, fmt[i], devname + 4);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001665 if (buffer[3] == '0')
1666 buffer[3] = '\0';
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001667 }
Eric Andersenf18bd892003-12-19 11:07:59 +00001668 /* 12 == pty/ */
1669 if( i == 12 )
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001670 {
Eric Andersenf18bd892003-12-19 11:07:59 +00001671 pty1 = "pqrstuvwxyzabcde";
1672 pty2 = "0123456789abcdef";
1673 indexx = atoi (devname + 5);
1674 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001675 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001676
Rob Landley1ba19d62005-10-08 17:42:35 +00001677 if(ENABLE_DEBUG && compat_name!=NULL)
1678 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001679
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001680 return compat_name;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001681} /* End Function get_old_name */
1682
1683static char get_old_ide_name (unsigned int major, unsigned int minor)
1684/* [SUMMARY] Get the old IDE name for a device.
1685 <major> The major number for the device.
1686 <minor> The minor number for the device.
1687 [RETURNS] The drive letter.
1688*/
1689{
1690 char letter='y'; /* 121 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001691 char c='a'; /* 97 */
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001692 int i=IDE0_MAJOR;
1693
Rob Landley1ba19d62005-10-08 17:42:35 +00001694 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001695
1696 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1697 do {
1698 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1699 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1700 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1701 i==IDE9_MAJOR )
1702 {
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001703 if((unsigned int)i==major)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001704 {
1705 letter=c;
1706 break;
1707 }
1708 c+=2;
1709 }
1710 i++;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001711 } while (i<=IDE9_MAJOR);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001712
1713 if (minor > 63)
1714 ++letter;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001715 return letter;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001716} /* End Function get_old_ide_name */
1717
1718static char *write_old_sd_name (char *buffer,
1719 unsigned int major, unsigned int minor,
Denis Vlasenko89ef65f2007-01-29 23:43:18 +00001720 const char *part)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001721/* [SUMMARY] Write the old SCSI disc name to a buffer.
1722 <buffer> The buffer to write to.
1723 <major> The major number for the device.
1724 <minor> The minor number for the device.
1725 <part> The partition string. Must be "" for a whole-disc entry.
1726 [RETURNS] A pointer to the buffer on success, else NULL.
1727*/
1728{
1729 unsigned int disc_index;
1730
Rob Landley1ba19d62005-10-08 17:42:35 +00001731 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001732
1733 if (major == 8)
1734 {
1735 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001736 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001737 }
1738 if ( (major > 64) && (major < 72) )
1739 {
1740 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1741 if (disc_index < 26)
1742 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1743 else
1744 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001745 return buffer;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001746 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001747 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001748} /* End Function write_old_sd_name */
1749
1750
1751/* expression.c */
1752
1753/*EXPERIMENTAL_FUNCTION*/
1754
1755int st_expr_expand (char *output, unsigned int length, const char *input,
1756 const char *(*get_variable_func) (const char *variable,
1757 void *info),
1758 void *info)
1759/* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1760 <output> The output expanded expression is written here.
1761 <length> The size of the output buffer.
1762 <input> The input expression. This may equal <<output>>.
1763 <get_variable> A function which will be used to get variable values. If
1764 this returns NULL, the environment is searched instead. If this is NULL,
1765 only the environment is searched.
1766 <info> An arbitrary pointer passed to <<get_variable>>.
1767 [RETURNS] TRUE on success, else FALSE.
1768*/
1769{
1770 char ch;
1771 unsigned int len;
1772 unsigned int out_pos = 0;
1773 const char *env;
1774 const char *ptr;
1775 struct passwd *pwent;
1776 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1777
Rob Landley1ba19d62005-10-08 17:42:35 +00001778 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001779
1780 if (length > BUFFER_SIZE)
1781 length = BUFFER_SIZE;
1782 for (; TRUE; ++input)
1783 {
1784 switch (ch = *input)
1785 {
1786 case '$':
1787 /* Variable expansion */
1788 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1789 if (input == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001790 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001791 break;
1792 case '~':
1793 /* Home directory expansion */
1794 ch = input[1];
1795 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1796 {
1797 /* User's own home directory: leave separator for next time */
1798 if ( ( env = getenv ("HOME") ) == NULL )
1799 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001800 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001801 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001802 }
1803 len = strlen (env);
1804 if (len + out_pos >= length)
1805 goto st_expr_expand_out;
1806 memcpy (buffer + out_pos, env, len + 1);
1807 out_pos += len;
1808 continue;
1809 }
1810 /* Someone else's home directory */
1811 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1812 /* VOID */ ;
1813 len = ptr - input;
1814 if (len >= sizeof tmp)
1815 goto st_expr_expand_out;
1816 safe_memcpy (tmp, input, len);
1817 input = ptr - 1;
1818 if ( ( pwent = getpwnam (tmp) ) == NULL )
1819 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001820 msg_logger(LOG_INFO, "no pwent for: %s", tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001821 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001822 }
1823 len = strlen (pwent->pw_dir);
1824 if (len + out_pos >= length)
1825 goto st_expr_expand_out;
1826 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1827 out_pos += len;
1828 break;
1829 case '\0':
1830 /* Falltrough */
1831 default:
1832 if (out_pos >= length)
1833 goto st_expr_expand_out;
1834 buffer[out_pos++] = ch;
1835 if (ch == '\0')
1836 {
1837 memcpy (output, buffer, out_pos);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001838 return TRUE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001839 }
1840 break;
1841 /* esac */
1842 }
1843 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001844 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001845st_expr_expand_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00001846 msg_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001847 return FALSE;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001848} /* End Function st_expr_expand */
1849
1850
1851/* Private functions follow */
1852
1853static const char *expand_variable (char *buffer, unsigned int length,
1854 unsigned int *out_pos, const char *input,
1855 const char *(*func) (const char *variable,
1856 void *info),
1857 void *info)
1858/* [SUMMARY] Expand a variable.
1859 <buffer> The buffer to write to.
1860 <length> The length of the output buffer.
1861 <out_pos> The current output position. This is updated.
1862 <input> A pointer to the input character pointer.
1863 <func> A function which will be used to get variable values. If this
1864 returns NULL, the environment is searched instead. If this is NULL, only
1865 the environment is searched.
1866 <info> An arbitrary pointer passed to <<func>>.
1867 <errfp> Diagnostic messages are written here.
1868 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1869*/
1870{
1871 char ch;
1872 int len;
1873 unsigned int open_braces;
1874 const char *env, *ptr;
1875 char tmp[STRING_LENGTH];
1876
Rob Landley1ba19d62005-10-08 17:42:35 +00001877 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001878
1879 ch = input[0];
1880 if (ch == '$')
1881 {
1882 /* Special case for "$$": PID */
1883 sprintf ( tmp, "%d", (int) getpid () );
1884 len = strlen (tmp);
1885 if (len + *out_pos >= length)
1886 goto expand_variable_out;
1887
1888 memcpy (buffer + *out_pos, tmp, len + 1);
1889 out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001890 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001891 }
1892 /* Ordinary variable expansion, possibly in braces */
1893 if (ch != '{')
1894 {
1895 /* Simple variable expansion */
1896 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
1897 /* VOID */ ;
1898 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001899 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001900 goto expand_variable_out;
1901
1902 safe_memcpy (tmp, input, len);
1903 input = ptr - 1;
1904 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
1905 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001906 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001907 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001908 }
1909 len = strlen (env);
1910 if (len + *out_pos >= length)
1911 goto expand_variable_out;
1912
1913 memcpy (buffer + *out_pos, env, len + 1);
1914 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001915 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001916 }
1917 /* Variable in braces: check for ':' tricks */
1918 ch = *++input;
1919 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
1920 /* VOID */;
1921 if (ch == '}')
1922 {
1923 /* Must be simple variable expansion with "${var}" */
1924 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001925 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001926 goto expand_variable_out;
1927
1928 safe_memcpy (tmp, input, len);
1929 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
1930 if (ptr == NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001931 return NULL;
1932 return input + len;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001933 }
1934 if (ch != ':' || ptr[1] != '-' )
1935 {
Rob Landley1ba19d62005-10-08 17:42:35 +00001936 msg_logger(LOG_INFO, "illegal char in var name");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001937 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001938 }
1939 /* It's that handy "${var:-word}" expression. Check if var is defined */
1940 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001941 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001942 goto expand_variable_out;
1943
1944 safe_memcpy (tmp, input, len);
1945 /* Move input pointer to ':' */
1946 input = ptr;
1947 /* First skip to closing brace, taking note of nested expressions */
1948 ptr += 2;
1949 ch = ptr[0];
1950 for (open_braces = 1; open_braces > 0; ch = *++ptr)
1951 {
1952 switch (ch)
1953 {
1954 case '{':
1955 ++open_braces;
1956 break;
1957 case '}':
1958 --open_braces;
1959 break;
1960 case '\0':
Rob Landley1ba19d62005-10-08 17:42:35 +00001961 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001962 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001963 default:
1964 break;
1965 }
1966 }
1967 --ptr;
1968 /* At this point ptr should point to closing brace of "${var:-word}" */
1969 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
1970 {
1971 /* Found environment variable, so skip the input to the closing brace
1972 and return the variable */
1973 input = ptr;
1974 len = strlen (env);
1975 if (len + *out_pos >= length)
1976 goto expand_variable_out;
1977
1978 memcpy (buffer + *out_pos, env, len + 1);
1979 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001980 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001981 }
1982 /* Environment variable was not found, so process word. Advance input
1983 pointer to start of word in "${var:-word}" */
1984 input += 2;
1985 len = ptr - input;
"Vladimir N. Oleynik"73ffd762006-02-01 12:56:19 +00001986 if ((size_t)len >= sizeof tmp)
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001987 goto expand_variable_out;
1988
1989 safe_memcpy (tmp, input, len);
1990 input = ptr;
1991 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001992 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00001993 len = strlen (tmp);
1994 if (len + *out_pos >= length)
1995 goto expand_variable_out;
1996
1997 memcpy (buffer + *out_pos, tmp, len + 1);
1998 *out_pos += len;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001999 return input;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002000expand_variable_out:
Rob Landley1ba19d62005-10-08 17:42:35 +00002001 msg_logger(LOG_INFO, bb_msg_small_buffer);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002002 return NULL;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002003} /* End Function expand_variable */
2004
2005
2006static const char *get_variable_v2 (const char *variable,
2007 const char *(*func) (const char *variable, void *info),
2008 void *info)
2009/* [SUMMARY] Get a variable from the environment or .
2010 <variable> The variable name.
2011 <func> A function which will be used to get the variable. If this returns
2012 NULL, the environment is searched instead. If this is NULL, only the
2013 environment is searched.
2014 [RETURNS] The value of the variable on success, else NULL.
2015*/
2016{
2017 const char *value;
2018
Rob Landley1ba19d62005-10-08 17:42:35 +00002019 debug_msg_logger(LOG_INFO, __FUNCTION__);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002020
2021 if (func != NULL)
2022 {
2023 value = (*func) (variable, info);
2024 if (value != NULL)
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002025 return value;
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002026 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002027 return getenv(variable);
Glenn L McGrath17d21fa2003-10-09 11:46:23 +00002028} /* End Function get_variable */
2029
2030/* END OF CODE */