2 devfsd implementation for busybox
4 Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
6 Busybox version is based on some previous work and ideas
7 Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
11 Main file for devfsd (devfs daemon for Linux).
13 Copyright (C) 1998-2002 Richard Gooch
17 Header file for devfsd (devfs daemon for Linux).
19 Copyright (C) 1998-2000 Richard Gooch
23 Compatibility name file for devfsd (build compatibility names).
25 Copyright (C) 1998-2002 Richard Gooch
29 This code provides Borne Shell-like expression expansion.
31 Copyright (C) 1997-1999 Richard Gooch
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.
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.
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.
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.
62 #include <sys/types.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
73 #include <sys/sysmacros.h>
76 /* Various defines taken from linux/major.h */
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)
96 #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
97 #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
105 #define DEVFS_PATHLEN 1024
106 /* Never change this otherwise the binary interface will change */
108 struct 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 */
122 #define BUFFER_SIZE 16384
123 #define DEVFSD_VERSION "1.3.25"
124 #define CONFIG_FILE "/etc/devfsd.conf"
125 #define MODPROBE "/sbin/modprobe"
126 #define MODPROBE_SWITCH_1 "-k"
127 #define MODPROBE_SWITCH_2 "-C"
128 #define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
129 #define MAX_ARGS (6 + 1)
130 #define MAX_SUBEXPR 10
131 #define STRING_LENGTH 255
133 /* for get_uid_gid() */
137 /* for msg_logger(), do_ioctl(),
138 fork_and_execute() */
142 /* for dir_operation() */
145 #define READ_CONFIG 2
147 /* Update only after changing code to reflect new protocol */
148 #define DEVFSD_PROTOCOL_REVISION_DAEMON 5
150 /* Compile-time check */
151 #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
152 #error protocol version mismatch. Update your kernel headers
155 #define AC_PERMISSIONS 0
158 #define AC_MFUNCTION 3 /* not supported by busybox */
159 #define AC_CFUNCTION 4 /* not supported by busybox */
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
169 struct permissions_type
178 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
184 const char *destination;
193 struct config_entry_struct
195 struct action_type action;
199 struct permissions_type permissions;
200 struct execute_type execute;
201 struct copy_type copy;
204 struct config_entry_struct *next;
207 struct get_variable_info
209 const struct devfsd_notify_struct *info;
211 char devpath[STRING_LENGTH];
214 static void dir_operation(int , const char * , int, unsigned long* );
215 static void service(struct stat statbuf, char *path);
216 static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
217 static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
218 static int mksymlink (const char *oldpath, const char *newpath);
219 static void read_config_file (char *path, int optional, unsigned long *event_mask);
220 static void process_config_line (const char *, unsigned long *);
221 static int do_servicing (int, unsigned long);
222 static void service_name (const struct devfsd_notify_struct *);
223 static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
224 static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
225 const regmatch_t *, unsigned);
226 #ifdef CONFIG_DEVFSD_MODLOAD
227 static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
229 static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
230 const regmatch_t *, unsigned);
231 static void action_compat (const struct devfsd_notify_struct *, unsigned);
232 static void free_config (void);
233 static void restore(char *spath, struct stat source_stat, int rootlen);
234 static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
235 static mode_t get_mode (const char *);
236 static void signal_handler (int);
237 static const char *get_variable (const char *, void *);
238 static int make_dir_tree (const char *);
239 static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
240 const char *, const regmatch_t *, unsigned );
241 static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
242 static const char *expand_variable( char *, unsigned, unsigned *, const char *,
243 const char *(*) (const char *, void *), void * );
244 static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
245 static char get_old_ide_name (unsigned , unsigned);
246 static char *write_old_sd_name (char *, unsigned, unsigned, char *);
248 /* busybox functions */
249 #if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
250 static void msg_logger(int die, int pri, const char * fmt, ... );
252 static void do_ioctl(int die, int fd, int request, unsigned long event_mask_flag);
253 static void fork_and_execute(int die, char *arg0, char **arg );
254 static int get_uid_gid ( int, const char *);
255 static void safe_memcpy( char * dest, const char * src, int len);
256 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
257 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
259 /* Structs and vars */
260 static struct config_entry_struct *first_config = NULL;
261 static struct config_entry_struct *last_config = NULL;
262 static const char *mount_point = NULL;
263 static volatile int caught_signal = FALSE;
264 static volatile int caught_sighup = FALSE;
265 static struct initial_symlink_struct
269 } initial_symlinks[] =
271 {"/proc/self/fd", "fd"},
278 static struct event_type
280 unsigned int type; /* The DEVFSD_NOTIFY_* value */
281 const char *config_name; /* The name used in the config file */
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"},
295 /* busybox functions and messages */
297 extern void xregcomp(regex_t * preg, const char *regex, int cflags);
299 const char * const bb_msg_proto_rev = "protocol revision";
300 #ifdef CONFIG_DEVFSD_VERBOSE
301 const char * const bb_msg_bad_config = "bad %s config file: %s\n";
302 const char * const bb_msg_small_buffer = "buffer too small\n";
303 const char * const bb_msg_variable_not_found = "variable: %s not found\n";
306 #if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
307 static void msg_logger(int die, int pri, const char * fmt, ... )
312 if (access ("/dev/log", F_OK) == 0)
314 openlog(bb_applet_name, 0, LOG_DAEMON);
315 vsyslog( pri , fmt , ap);
321 bb_verror_msg(fmt, ap);
328 static void do_ioctl(int die, int fd, int request, unsigned long event_mask_flag)
330 #ifdef CONFIG_DEVFSD_VERBOSE
331 if (ioctl (fd, request, event_mask_flag) == -1)
332 msg_logger(die, LOG_ERR, "ioctl(): %m\n");
334 if (ioctl (fd, request, event_mask_flag) == -1)
339 static void fork_and_execute(int die, char *arg0, char **arg )
347 /* Parent: Error : die or return */
348 #ifdef CONFIG_DEVFSD_VERBOSE
349 msg_logger(die, LOG_ERR,(char *) bb_msg_memory_exhausted);
356 /* Parent : ok : return or exit */
364 /* Child : if arg0 != NULL do execvp */
368 #ifdef CONFIG_DEVFSD_VERBOSE
369 msg_logger(DIE, LOG_ERR, "execvp(): %s: %m\n", arg0);
376 static void safe_memcpy( char *dest, const char *src, int len)
378 memcpy (dest , src , len );
382 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
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);
396 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
398 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/')
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' &&
404 return scan_dev_name_common(d, n, 0, ptr);
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')
409 return scan_dev_name_common(d, n, 4, ptr);
411 else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
415 else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
419 else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
426 /* Public functions follow */
428 int devfsd_main (int argc, char **argv)
430 int print_version = FALSE;
431 #ifdef CONFIG_DEVFSD_FG_NP
432 int do_daemon = TRUE;
433 int no_polling = FALSE;
436 int fd, proto_rev, count;
437 unsigned long event_mask = 0;
438 struct sigaction new_action;
439 struct initial_symlink_struct *curr;
444 for (count = 2; count < argc; ++count)
446 if(argv[count][0] == '-')
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 */
453 else if(argv[count][1]=='n' && argv[count][2]=='p' && !argv[count][3]) /* -np */
461 /* 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';
465 mount_point = argv[1];
467 if (chdir (mount_point) != 0)
468 #ifdef CONFIG_DEVFSD_VERBOSE
469 bb_error_msg_and_die( " %s: %m", mount_point);
474 fd = bb_xopen (".devfsd", O_RDONLY);
476 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
477 #ifdef CONFIG_DEVFSD_VERBOSE
478 bb_error_msg( "FD_CLOEXEC");
483 do_ioctl(DIE, fd, DEVFSDIOC_GET_PROTO_REV,(int )&proto_rev);
485 /*setup initial entries */
486 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
487 symlink (curr->dest, curr->name);
489 /* NB: The check for CONFIG_FILE is done in read_config_file() */
491 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
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 */
500 /* Tell kernel we are special (i.e. we get to see hidden entries) */
501 do_ioctl(DIE, fd, DEVFSDIOC_SET_EVENT_MASK, 0);
503 sigemptyset (&new_action.sa_mask);
504 new_action.sa_flags = 0;
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 )
509 #ifdef CONFIG_DEVFSD_VERBOSE
510 bb_error_msg_and_die( "sigaction()");
515 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
517 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
519 read_config_file (CONFIG_FILE, FALSE, &event_mask);
520 /* Do the scan before forking, so that boot scripts see the finished product */
521 dir_operation(SERVICE,mount_point,0,NULL);
522 #ifdef CONFIG_DEVFSD_FG_NP
528 /* 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 */
532 #ifdef CONFIG_DEVFSD_FG_NP
535 setpgid (0, 0); /* Become process group leader */
540 do_scan = do_servicing (fd, event_mask);
543 read_config_file (CONFIG_FILE, FALSE, &event_mask);
545 dir_operation(SERVICE,mount_point,0,NULL);
547 } /* End Function main */
550 /* Private functions follow */
552 static void read_config_file (char *path, int optional, unsigned long *event_mask)
553 /* [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.
563 char buf[STRING_LENGTH];
567 msg_logger( NO_DIE, LOG_INFO, "read_config_file(): %s\n", path);
569 if (stat (path, &statbuf) == 0 )
571 /* Don't read 0 length files: ignored */
572 /*if( statbuf.st_size == 0 )
574 if ( S_ISDIR (statbuf.st_mode) )
576 /* 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';
580 dir_operation(READ_CONFIG, path, 0, event_mask);
583 if ( ( fp = fopen (path, "r") ) != NULL )
585 while (fgets (buf, STRING_LENGTH, fp) != NULL)
587 /* Skip whitespace */
588 for (line = buf; isspace (*line); ++line)
590 if (line[0] == '\0' || line[0] == '#' )
592 process_config_line (line, event_mask);
596 goto read_config_file_err;
599 read_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);
603 if(optional == 0 && errno == ENOENT)
608 } /* End Function read_config_file */
610 static 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.
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];
625 /* !!!! 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 };
633 msg_logger( NO_DIE, LOG_INFO, "process_config_line()\n");
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",
639 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
641 i = compare_string_array(options, when );
652 goto process_config_line_err;
654 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
655 if( i == 1 || i == 2 )
657 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
659 msg_logger( NO_DIE, LOG_INFO, "%sinclude: %s\n",(toupper (when[0]) == 'I') ? "": "optional_", name);
661 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
667 dir_operation(RESTORE,name, strlen (name),NULL);
671 goto process_config_line_err;
673 new = xmalloc (sizeof *new);
674 memset (new, 0, sizeof *new);
676 for (count = 0; event_types[count].config_name != NULL; ++count)
678 if (strcasecmp (when, event_types[count].config_name) != 0)
680 new->action.when = event_types[count].type;
683 if (event_types[count].config_name == NULL)
686 goto process_config_line_err;
689 i = compare_string_array(options, what );
693 case 4: /* "PERMISSIONS" */
694 new->action.what = AC_PERMISSIONS;
695 /* Get user and group */
696 if ( ( ptr = strchr (p[0], '.') ) == NULL )
699 goto process_config_line_err; /*"missing '.' in UID.GID */
703 new->u.permissions.uid = get_uid_gid (UID, p[0]);
704 new->u.permissions.gid = get_uid_gid (GID, ptr);
706 new->u.permissions.mode = get_mode (p[1]);
708 #ifdef CONFIG_DEVFSD_MODLOAD
709 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;
716 case 6: /* EXECUTE */
717 new->action.what = AC_EXECUTE;
720 for (count = 0; count < num_args; ++count)
721 new->u.execute.argv[count] = bb_xstrdup (p[count]);
723 new->u.execute.argv[num_args] = NULL;
726 new->action.what = AC_COPY;
729 goto process_config_line_err; /* missing path and function in line */
731 new->u.copy.source = bb_xstrdup (p[0]);
732 new->u.copy.destination = bb_xstrdup (p[1]);
736 case 9: /* MKOLDCOMPAT */
738 case 10: /* MKNEWCOMPAT */
740 case 11:/* RMOLDCOMPAT */
742 case 12: /* RMNEWCOMPAT */
748 new->action.what = i - 2;
752 goto process_config_line_err;
756 xregcomp( &new->preg, name, REG_EXTENDED);
758 *event_mask |= 1 << new->action.when;
760 if (first_config == NULL)
763 last_config->next = new;
766 process_config_line_err:
767 #ifdef CONFIG_DEVFSD_VERBOSE
768 msg_logger( DIE, LOG_ERR, bb_msg_bad_config, msg , line);
772 } /* End Function process_config_line */
774 static 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.
782 struct devfsd_notify_struct info;
783 unsigned long tmp_event_mask;
786 msg_logger( NO_DIE, LOG_INFO, "do_servicing()\n");
788 /* Tell devfs what events we care about */
789 tmp_event_mask = event_mask;
790 do_ioctl(DIE, fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
791 while (!caught_signal)
794 bytes = read (fd, (char *) &info, sizeof info);
796 break; /* Must test for this first */
798 continue; /* Yes, the order is important */
801 service_name (&info);
805 int c_sighup = caught_sighup;
807 caught_signal = FALSE;
808 caught_sighup = FALSE;
811 #ifdef CONFIG_DEVFSD_VERBOSE
812 msg_logger( NO_DIE, LOG_ERR, "read error on control file: %m\n");
814 /* This is to shut up a compiler warning */
816 } /* End Function do_servicing */
818 static void service_name (const struct devfsd_notify_struct *info)
819 /* [SUMMARY] Service a single devfs change.
820 <info> The devfs change.
825 regmatch_t mbuf[MAX_SUBEXPR];
826 struct config_entry_struct *entry;
829 msg_logger( NO_DIE, LOG_INFO, "service_name()\n");
830 if (info->overrun_count > 0)
831 msg_logger( NO_DIE, LOG_ERR, "lost %u events\n", info->overrun_count);
834 /* Discard lookups on "/dev/log" and "/dev/initctl" */
835 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
836 ((info->devname[0]=='l' && info->devname[1]=='o' &&
837 info->devname[2]=='g' && !info->devname[3]) ||
838 ( info->devname[0]=='i' && info->devname[1]=='n' &&
839 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])))
843 for (entry = first_config; entry != NULL; entry = entry->next)
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 )
848 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
851 msg_logger( NO_DIE, LOG_INFO, "service_name(): action.what %d\n", entry->action.what);
853 switch (entry->action.what)
856 action_permissions (info, entry);
858 #ifdef CONFIG_DEVFSD_MODLOAD
860 action_modload (info, entry);
864 action_execute (info, entry, mbuf, n);
867 action_copy (info, entry, mbuf, n);
876 action_compat (info, entry->action.what);
879 #ifdef CONFIG_DEVFSD_VERBOSE
880 msg_logger( DIE, LOG_ERR, "Unknown action\n");
887 } /* End Function service_name */
889 static 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.
900 msg_logger( NO_DIE, LOG_INFO, "action_permission()\n");
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)
907 #ifdef CONFIG_DEVFSD_VERBOSE
908 msg_logger( NO_DIE, LOG_ERR, "chmod() or chown(): %s: %m\n",info->devname);
913 } /* End Function action_permissions */
915 #ifdef CONFIG_DEVFSD_MODLOAD
916 static 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.
925 char device[STRING_LENGTH];
928 argv[1] = MODPROBE_SWITCH_1; /* "-k" */
929 argv[2] = MODPROBE_SWITCH_2; /* "-C" */
930 argv[3] = CONFIG_MODULES_DEVFS;
934 snprintf (device, sizeof (device), "/dev/%s", info->devname);
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]);
938 fork_and_execute(DIE, argv[0], argv);
939 } /* End Function action_modload */
942 static 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
950 <numexpr> The number of elements within <<regexpr>>.
955 struct get_variable_info gv_info;
956 char *argv[MAX_ARGS + 1];
957 char largv[MAX_ARGS + 1][STRING_LENGTH];
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)
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];
979 for(i=0;argv[i]!=NULL;i++) /* argv[i] < MAX_ARGS + 1 */
982 if( (strlen(buff)+ 1 + strlen(argv[i])) >= 512)
984 strcat(buff,argv[i]);
987 msg_logger( NO_DIE, LOG_INFO, "action_execute(): %s",buff);
990 fork_and_execute(NO_DIE, argv[0], argv);
991 } /* End Function action_execute */
994 static 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
1002 <numexpr> The number of elements in <<regexpr>>.
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;
1013 msg_logger( NO_DIE, LOG_INFO, "action_copy()\n");
1016 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
1018 gv_info.info = info;
1019 gv_info.devname = info->devname;
1021 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
1022 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
1023 get_variable, &gv_info, gv_info.devname,
1026 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
1027 get_variable, &gv_info, gv_info.devname,
1030 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
1032 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;
1039 if ( !copy_inode (destination, &dest_stat, new_mode, source, &source_stat) && (errno != EEXIST))
1040 msg_logger( NO_DIE, LOG_ERR, "copy_inode(): %s to %s: %m\n", source, destination);
1042 copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
1045 } /* End Function action_copy */
1047 static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
1048 /* [SUMMARY] Process a compatibility request.
1049 <info> The devfs change.
1050 <action> The action to take.
1054 const char *compat_name = NULL;
1055 const char *dest_name = info->devname;
1057 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
1058 int mode, host, bus, target, lun;
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 */
1074 /* First construct compatibility name */
1077 case AC_MKOLDCOMPAT:
1078 case AC_RMOLDCOMPAT:
1079 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1081 case AC_MKNEWCOMPAT:
1082 case AC_RMNEWCOMPAT:
1083 ptr = strrchr (info->devname, '/') + 1;
1084 i=scan_dev_name(info->devname, info->namelen, ptr);
1087 msg_logger( NO_DIE, LOG_INFO, "action_compat(): scan_dev_name() returned %d\n", i);
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));
1096 dest_name = dest_buf;
1097 compat_name = compat_buf;
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);
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) );
1111 rewind_ = info->devname[info->namelen - 1];
1115 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1116 mode = ptr[2] - 107; /* 1 or 2 */
1119 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
1122 /* 9 == ide/host/mt */
1124 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1126 } /* switch(action) */
1128 if(compat_name == NULL )
1132 msg_logger( NO_DIE, LOG_INFO, "action_compat(): %s\n", compat_name);
1135 /* Now decide what to do with it */
1138 case AC_MKOLDCOMPAT:
1139 case AC_MKNEWCOMPAT:
1140 mksymlink (dest_name, compat_name);
1142 case AC_RMOLDCOMPAT:
1143 case AC_RMNEWCOMPAT:
1145 if (unlink (compat_name) != 0)
1146 msg_logger( NO_DIE, LOG_ERR, "unlink(): %s: %m\n", compat_name);
1148 unlink (compat_name);
1152 } /* switch(action) */
1153 } /* End Function action_compat */
1155 static void restore(char *spath, struct stat source_stat, int rootlen)
1157 char dpath[STRING_LENGTH];
1158 struct stat dest_stat;
1161 msg_logger( NO_DIE, LOG_INFO, "restore()\n");
1164 dest_stat.st_mode = 0;
1165 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1166 lstat (dpath, &dest_stat);
1168 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);
1171 if ( S_ISDIR (source_stat.st_mode) )
1172 dir_operation(RESTORE, spath, rootlen,NULL);
1176 static int copy_inode (const char *destpath, const struct stat *dest_stat,
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.
1188 int source_len, dest_len;
1189 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1191 struct sockaddr_un un_addr;
1192 char symlink_val[STRING_LENGTH];
1195 msg_logger( NO_DIE, LOG_INFO, "copy_inode()\n");
1198 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1201 if ( S_ISLNK (source_stat->st_mode) )
1203 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1204 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
1206 source_link[source_len] = '\0';
1207 dest_link[dest_len] = '\0';
1208 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1211 symlink (source_link, destpath);
1214 } /* Else not a symlink */
1215 chmod (destpath, new_mode & ~S_IFMT);
1216 chown (destpath, source_stat->st_uid, source_stat->st_gid);
1219 /* Different types: unlink and create */
1221 switch (source_stat->st_mode & S_IFMT)
1224 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
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);
1230 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1234 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1236 symlink_val[val] = '\0';
1237 if (symlink (symlink_val, destpath) == 0)
1241 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1244 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1250 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1254 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1257 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1262 } /* End Function copy_inode */
1264 static void free_config ()
1265 /* [SUMMARY] Free the configuration information.
1269 struct config_entry_struct *c_entry;
1273 msg_logger( NO_DIE, LOG_INFO, "free_config()\n");
1276 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1280 next = c_entry->next;
1281 regfree (&c_entry->preg);
1282 if (c_entry->action.what == AC_EXECUTE)
1284 for (count = 0; count < MAX_ARGS; ++count)
1286 if (c_entry->u.execute.argv[count] == NULL)
1288 free (c_entry->u.execute.argv[count]);
1293 first_config = NULL;
1295 } /* End Function free_config */
1297 static 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.
1304 struct passwd *pw_ent;
1305 struct group *grp_ent;
1306 #ifdef CONFIG_DEVFSD_VERBOSE
1311 msg_logger( NO_DIE, LOG_INFO, "get_uid_gid()\n");
1314 if(flag != UID && flag != GID )
1315 msg_logger( DIE, LOG_ERR,"get_uid_gid(): flag != UID && flag != GID\n");
1318 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
1319 return atoi (string);
1321 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
1322 return (pw_ent->pw_uid);
1324 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
1325 return (grp_ent->gr_gid);
1326 #ifdef CONFIG_DEVFSD_VERBOSE
1330 msg_logger( NO_DIE, LOG_ERR,"unknown %s: %s, defaulting to %cID=0\n", msg, string, msg[0] - 32);
1333 }/* End Function get_uid_gid */
1335 static 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.
1344 msg_logger( NO_DIE, LOG_INFO, "get_mode()\n");
1347 if ( isdigit (string[0]) )
1348 return strtoul (string, NULL, 8);
1349 if (strlen (string) != 9)
1350 #ifdef CONFIG_DEVFSD_VERBOSE
1351 msg_logger( DIE, LOG_ERR, "bad mode: %s\n", string);
1359 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1365 } /* End Function get_mode */
1367 static void signal_handler (int sig)
1370 msg_logger( NO_DIE, LOG_INFO, "signal_handler()\n");
1373 caught_signal = TRUE;
1375 caught_sighup = TRUE;
1376 #ifdef CONFIG_DEVFSD_VERBOSE
1377 msg_logger( NO_DIE, LOG_INFO, "Caught %s\n",(sig == SIGHUP)?"SIGHUP" : "SIGUSR1");
1379 } /* End Function signal_handler */
1381 static const char *get_variable (const char *variable, void *info)
1383 struct get_variable_info *gv_info = info;
1384 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
1385 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1386 "uid", "gid", "mode", hostname, mount_point,
1387 gv_info->devpath, gv_info->devname, 0 };
1390 msg_logger( NO_DIE, LOG_INFO, "get_variable()\n");
1393 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
1394 #ifdef CONFIG_DEVFSD_VERBOSE
1395 msg_logger( DIE, LOG_ERR, "gethostname(): %m\n");
1399 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
1400 hostname[STRING_LENGTH - 1] = '\0';
1402 /* compare_string_array returns i>=0 */
1403 i=compare_string_array(field_names, variable);
1405 if ( i > 6 && (i > 1 && gv_info == NULL))
1407 if( i >= 0 || i <= 3)
1410 msg_logger( NO_DIE, LOG_INFO, "get_variable(): i=%d %s\n",i ,field_names[i+7]);
1412 return(field_names[i+7]);
1416 sprintf (sbuf, "%u", gv_info->info->uid);
1418 sprintf (sbuf, "%u", gv_info->info->gid);
1420 sprintf (sbuf, "%o", gv_info->info->mode);
1422 msg_logger( NO_DIE, LOG_INFO, "get_variable(): %s\n", sbuf);
1425 } /* End Function get_variable */
1427 static void service(struct stat statbuf, char *path)
1429 struct devfsd_notify_struct info;
1432 msg_logger( NO_DIE, LOG_INFO, "service()\n");
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);
1449 static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
1450 /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
1451 <flag> To choose which function to perform
1452 <dp> The directory pointer. This is closed upon completion.
1453 <dir_name> The name of the directory.
1454 <rootlen> string length parameter.
1458 struct stat statbuf;
1461 char path[STRING_LENGTH];
1465 msg_logger( NO_DIE, LOG_INFO, "dir_operation()\n");
1468 if((dp = opendir( dir_name))==NULL)
1471 msg_logger( NO_DIE, LOG_ERR, "opendir(): %s: %m\n", dir_name);
1476 while ( (de = readdir (dp) ) != NULL )
1479 if(de->d_name && *de->d_name == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])))
1481 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
1483 msg_logger( NO_DIE, LOG_ERR, "dir_operation(): %s\n", path);
1486 if (lstat (path, &statbuf) != 0)
1489 msg_logger( NO_DIE, LOG_ERR, "%s: %m\n", path);
1496 service(statbuf,path);
1499 restore(path, statbuf, var);
1502 read_config_file (path, var, event_mask);
1507 } /* End Function do_scan_and_service */
1509 static 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.
1517 msg_logger( NO_DIE, LOG_INFO, "mksymlink()\n", newpath);
1521 if ( !make_dir_tree (newpath) )
1524 if (symlink (oldpath, newpath) != 0)
1526 if (errno != EEXIST)
1529 msg_logger( NO_DIE, LOG_ERR, "mksymlink(): %s to %s: %m\n", oldpath, newpath);
1535 } /* End Function mksymlink */
1538 static int make_dir_tree (const char *path)
1539 /* [SUMMARY] Creating intervening directories for a path as required.
1540 <path> The full pathname (including the leaf node).
1541 [RETURNS] TRUE on success, else FALSE.
1545 msg_logger( NO_DIE, LOG_INFO, "make_dir_tree()\n");
1547 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
1550 msg_logger( NO_DIE, LOG_ERR, "make_dir_tree(): %s: %m\n", path);
1555 } /* End Function make_dir_tree */
1557 static int expand_expression(char *output, unsigned int outsize,
1559 const char *(*get_variable_func)(const char *variable, void *info),
1561 const char *devname,
1562 const regmatch_t *ex, unsigned int numexp)
1563 /* [SUMMARY] Expand environment variables and regular subexpressions in string.
1564 <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.
1578 char temp[STRING_LENGTH];
1581 msg_logger( NO_DIE, LOG_INFO, "expand_expression()\n");
1584 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
1586 expand_regexp (output, outsize, temp, devname, ex, numexp);
1588 } /* End Function expand_expression */
1590 static 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
1604 <numex> Number of subexpressions in the offset-array <<ex>>.
1608 const char last_exp = '0' - 1 + numex;
1612 msg_logger( NO_DIE, LOG_INFO, "expand_regexp()\n");
1615 /* Guarantee NULL termination by writing an explicit '\0' character into
1616 the very last byte */
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) )
1632 if ((c >= '0') && (c <= last_exp))
1634 const regmatch_t *subexp = ex + (c - '0');
1635 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1637 /* Range checking */
1638 if (sublen > outsize)
1640 strncpy (output, devname + subexp->rm_so, sublen);
1651 } /* End Function expand_regexp */
1654 /* from compat_name.c */
1656 struct translate_struct
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) */
1663 static struct translate_struct translate_table[] =
1666 {"printers/", "lp%s"},
1668 {"parports/", "parport%s"},
1671 {"loop/", "loop%s"},
1672 {"floppy/", "fd%s"},
1674 {"md/", "md%s"}, /* Meta-devices */
1678 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
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 */
1696 const 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.
1707 const char *compat_name = NULL;
1709 struct translate_struct *trans;
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/ */
1729 "%cty%c%c", /* pty/ */
1733 msg_logger( NO_DIE, LOG_INFO, "get_old_name()\n");
1736 for (trans = translate_table; trans->match != NULL; ++trans)
1738 len = strlen (trans->match);
1740 if (strncmp (devname, trans->match, len) == 0)
1742 if (trans->format == NULL)
1743 return (devname + len);
1744 sprintf (buffer, trans->format, devname + len);
1749 ptr = (strrchr (devname, '/') + 1);
1750 i = scan_dev_name(devname, namelen, ptr);
1752 if( i > 0 && i < 13)
1753 compat_name = buffer;
1758 msg_logger( NO_DIE, LOG_INFO, "get_old_name(): scan_dev_name() returned %d\n", i);
1761 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1762 if( i == 1 || i == 3 || i == 10 )
1763 sprintf (buffer, fmt[i], minor);
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)));
1775 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1776 if (devname[namelen - 1] != 'n')
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 */
1783 /* 9 == ide/host/mt */
1785 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1790 sprintf (buffer, fmt[i], devname + 4);
1791 if (buffer[3] == '0')
1797 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]);
1803 if(compat_name!=NULL)
1804 msg_logger( NO_DIE, LOG_INFO, "get_old_name(): compat_name %s\n", compat_name);
1806 return (compat_name);
1807 } /* End Function get_old_name */
1809 static 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.
1816 char letter='y'; /* 121 */
1817 char c='a'; /* 97 */
1821 msg_logger( NO_DIE, LOG_INFO, "get_old_ide_name()\n");
1824 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
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 ||
1839 } while(i<=IDE9_MAJOR);
1844 } /* End Function get_old_ide_name */
1846 static char *write_old_sd_name (char *buffer,
1847 unsigned int major, unsigned int minor,
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.
1857 unsigned int disc_index;
1860 msg_logger( NO_DIE, LOG_INFO, "write_old_sd_name()\n");
1865 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
1868 if ( (major > 64) && (major < 72) )
1870 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1871 if (disc_index < 26)
1872 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1874 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
1878 } /* End Function write_old_sd_name */
1883 /*EXPERIMENTAL_FUNCTION*/
1885 int st_expr_expand (char *output, unsigned int length, const char *input,
1886 const char *(*get_variable_func) (const char *variable,
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.
1902 unsigned int out_pos = 0;
1905 struct passwd *pwent;
1906 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1909 msg_logger( NO_DIE, LOG_INFO, "st_expr_expand()\n");
1912 if (length > BUFFER_SIZE)
1913 length = BUFFER_SIZE;
1914 for (; TRUE; ++input)
1916 switch (ch = *input)
1919 /* Variable expansion */
1920 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1925 /* Home directory expansion */
1927 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1929 /* User's own home directory: leave separator for next time */
1930 if ( ( env = getenv ("HOME") ) == NULL )
1932 #ifdef CONFIG_DEVFSD_VERBOSE
1933 msg_logger( NO_DIE, LOG_INFO, bb_msg_variable_not_found, "HOME");
1938 if (len + out_pos >= length)
1939 goto st_expr_expand_out;
1940 memcpy (buffer + out_pos, env, len + 1);
1944 /* Someone else's home directory */
1945 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1948 if (len >= sizeof tmp)
1949 goto st_expr_expand_out;
1950 safe_memcpy (tmp, input, len);
1952 if ( ( pwent = getpwnam (tmp) ) == NULL )
1954 #ifdef CONFIG_DEVFSD_VERBOSE
1955 msg_logger( NO_DIE, LOG_INFO, "no pwent for: %s\n", tmp);
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);
1968 if (out_pos >= length)
1969 goto st_expr_expand_out;
1970 buffer[out_pos++] = ch;
1973 memcpy (output, buffer, out_pos);
1982 #ifdef CONFIG_DEVFSD_VERBOSE
1983 msg_logger( NO_DIE, LOG_INFO, bb_msg_small_buffer);
1986 } /* End Function st_expr_expand */
1989 /* Private functions follow */
1991 static const char *expand_variable (char *buffer, unsigned int length,
1992 unsigned int *out_pos, const char *input,
1993 const char *(*func) (const char *variable,
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.
2011 unsigned int open_braces;
2012 const char *env, *ptr;
2013 char tmp[STRING_LENGTH];
2016 msg_logger( NO_DIE, LOG_INFO, "expand_variable()\n");
2022 /* Special case for "$$": PID */
2023 sprintf ( tmp, "%d", (int) getpid () );
2025 if (len + *out_pos >= length)
2026 goto expand_variable_out;
2028 memcpy (buffer + *out_pos, tmp, len + 1);
2032 /* Ordinary variable expansion, possibly in braces */
2035 /* Simple variable expansion */
2036 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
2039 if (len >= sizeof tmp)
2040 goto expand_variable_out;
2042 safe_memcpy (tmp, input, len);
2044 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
2046 #ifdef CONFIG_DEVFSD_VERBOSE
2047 msg_logger( NO_DIE, LOG_INFO, bb_msg_variable_not_found, tmp);
2052 if (len + *out_pos >= length)
2053 goto expand_variable_out;
2055 memcpy (buffer + *out_pos, env, len + 1);
2059 /* Variable in braces: check for ':' tricks */
2061 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
2065 /* Must be simple variable expansion with "${var}" */
2067 if (len >= sizeof tmp)
2068 goto expand_variable_out;
2070 safe_memcpy (tmp, input, len);
2071 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
2074 return (input + len);
2076 if (ch != ':' || ptr[1] != '-' )
2078 #ifdef CONFIG_DEVFSD_VERBOSE
2079 msg_logger( NO_DIE, LOG_INFO,"illegal char in var name\n");
2083 /* It's that handy "${var:-word}" expression. Check if var is defined */
2085 if (len >= sizeof tmp)
2086 goto expand_variable_out;
2088 safe_memcpy (tmp, input, len);
2089 /* Move input pointer to ':' */
2091 /* First skip to closing brace, taking note of nested expressions */
2094 for (open_braces = 1; open_braces > 0; ch = *++ptr)
2105 #ifdef CONFIG_DEVFSD_VERBOSE
2106 msg_logger( NO_DIE, LOG_INFO,"\"}\" not found in: %s\n", input);
2114 /* At this point ptr should point to closing brace of "${var:-word}" */
2115 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
2117 /* Found environment variable, so skip the input to the closing brace
2118 and return the variable */
2121 if (len + *out_pos >= length)
2122 goto expand_variable_out;
2124 memcpy (buffer + *out_pos, env, len + 1);
2128 /* Environment variable was not found, so process word. Advance input
2129 pointer to start of word in "${var:-word}" */
2132 if (len >= sizeof tmp)
2133 goto expand_variable_out;
2135 safe_memcpy (tmp, input, len);
2137 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
2140 if (len + *out_pos >= length)
2141 goto expand_variable_out;
2143 memcpy (buffer + *out_pos, tmp, len + 1);
2146 expand_variable_out:
2147 #ifdef CONFIG_DEVFSD_VERBOSE
2148 msg_logger( NO_DIE, LOG_INFO, bb_msg_small_buffer);
2151 } /* End Function expand_variable */
2154 static const char *get_variable_v2 (const char *variable,
2155 const char *(*func) (const char *variable, 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.
2168 msg_logger( NO_DIE, LOG_INFO, "get_variable_v2()\n");
2173 value = (*func) (variable, info);
2177 return getenv (variable);
2178 } /* End Function get_variable */