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.
61 #include <sys/types.h>
63 #include <sys/ioctl.h>
64 #include <sys/socket.h>
71 #include <sys/sysmacros.h>
74 /* Various defines taken from linux/major.h */
87 /* Various defines taken from linux/devfs_fs.h */
88 #define DEVFSD_PROTOCOL_REVISION_KERNEL 5
89 #define DEVFSD_IOCTL_BASE 'd'
90 /* These are the various ioctls */
91 #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
92 #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
93 #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
94 #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
95 #define DEVFSD_NOTIFY_REGISTERED 0
96 #define DEVFSD_NOTIFY_UNREGISTERED 1
97 #define DEVFSD_NOTIFY_ASYNC_OPEN 2
98 #define DEVFSD_NOTIFY_CLOSE 3
99 #define DEVFSD_NOTIFY_LOOKUP 4
100 #define DEVFSD_NOTIFY_CHANGE 5
101 #define DEVFSD_NOTIFY_CREATE 6
102 #define DEVFSD_NOTIFY_DELETE 7
103 #define DEVFS_PATHLEN 1024
104 /* Never change this otherwise the binary interface will change */
106 struct devfsd_notify_struct
107 { /* Use native C types to ensure same types in kernel and user space */
108 unsigned int type; /* DEVFSD_NOTIFY_* value */
109 unsigned int mode; /* Mode of the inode or device entry */
110 unsigned int major; /* Major number of device entry */
111 unsigned int minor; /* Minor number of device entry */
112 unsigned int uid; /* Uid of process, inode or device entry */
113 unsigned int gid; /* Gid of process, inode or device entry */
114 unsigned int overrun_count; /* Number of lost events */
115 unsigned int namelen; /* Number of characters not including '\0' */
116 /* The device name MUST come last */
117 char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */
120 #define BUFFER_SIZE 16384
121 #define DEVFSD_VERSION "1.3.25"
122 #define CONFIG_FILE "/etc/devfsd.conf"
123 #define MODPROBE "/sbin/modprobe"
124 #define MODPROBE_SWITCH_1 "-k"
125 #define MODPROBE_SWITCH_2 "-C"
126 #define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
127 #define MAX_ARGS (6 + 1)
128 #define MAX_SUBEXPR 10
129 #define STRING_LENGTH 255
131 /* for get_uid_gid() */
135 /* fork_and_execute() */
139 /* for dir_operation() */
142 #define READ_CONFIG 2
144 /* Update only after changing code to reflect new protocol */
145 #define DEVFSD_PROTOCOL_REVISION_DAEMON 5
147 /* Compile-time check */
148 #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
149 #error protocol version mismatch. Update your kernel headers
152 #define AC_PERMISSIONS 0
155 #define AC_MFUNCTION 3 /* not supported by busybox */
156 #define AC_CFUNCTION 4 /* not supported by busybox */
159 #define AC_MKOLDCOMPAT 7
160 #define AC_MKNEWCOMPAT 8
161 #define AC_RMOLDCOMPAT 9
162 #define AC_RMNEWCOMPAT 10
163 #define AC_RESTORE 11
165 struct permissions_type
174 char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */
180 const char *destination;
189 struct config_entry_struct
191 struct action_type action;
195 struct permissions_type permissions;
196 struct execute_type execute;
197 struct copy_type copy;
200 struct config_entry_struct *next;
203 struct get_variable_info
205 const struct devfsd_notify_struct *info;
207 char devpath[STRING_LENGTH];
210 static void dir_operation(int , const char * , int, unsigned long* );
211 static void service(struct stat statbuf, char *path);
212 static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
213 static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
214 static int mksymlink (const char *oldpath, const char *newpath);
215 static void read_config_file (char *path, int optional, unsigned long *event_mask);
216 static void process_config_line (const char *, unsigned long *);
217 static int do_servicing (int, unsigned long);
218 static void service_name (const struct devfsd_notify_struct *);
219 static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
220 static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
221 const regmatch_t *, unsigned);
222 static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
223 static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
224 const regmatch_t *, unsigned);
225 static void action_compat (const struct devfsd_notify_struct *, unsigned);
226 static void free_config (void);
227 static void restore(char *spath, struct stat source_stat, int rootlen);
228 static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
229 static mode_t get_mode (const char *);
230 static void signal_handler (int);
231 static const char *get_variable (const char *, void *);
232 static int make_dir_tree (const char *);
233 static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
234 const char *, const regmatch_t *, unsigned );
235 static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
236 static const char *expand_variable( char *, unsigned, unsigned *, const char *,
237 const char *(*) (const char *, void *), void * );
238 static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
239 static char get_old_ide_name (unsigned , unsigned);
240 static char *write_old_sd_name (char *, unsigned, unsigned, char *);
242 /* busybox functions */
243 static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3)));
244 static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3)));
245 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
246 static void fork_and_execute(int die, char *arg0, char **arg );
247 static int get_uid_gid ( int, const char *);
248 static void safe_memcpy( char * dest, const char * src, int len);
249 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
250 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
252 /* Structs and vars */
253 static struct config_entry_struct *first_config = NULL;
254 static struct config_entry_struct *last_config = NULL;
255 static const char *mount_point = NULL;
256 static volatile int caught_signal = FALSE;
257 static volatile int caught_sighup = FALSE;
258 static struct initial_symlink_struct
262 } initial_symlinks[] =
264 {"/proc/self/fd", "fd"},
271 static struct event_type
273 unsigned int type; /* The DEVFSD_NOTIFY_* value */
274 const char *config_name; /* The name used in the config file */
277 {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
278 {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
279 {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
280 {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
281 {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
282 {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
283 {DEVFSD_NOTIFY_CREATE, "CREATE"},
284 {DEVFSD_NOTIFY_DELETE, "DELETE"},
288 /* Busybox messages */
290 static const char * const bb_msg_proto_rev = "protocol revision";
291 static const char * const bb_msg_bad_config = "bad %s config file: %s";
292 static const char * const bb_msg_small_buffer = "buffer too small";
293 static const char * const bb_msg_variable_not_found = "variable: %s not found";
295 /* Busybox functions */
296 static void msg_logger(int pri, const char * fmt, ... )
302 ret = access ("/dev/log", F_OK);
304 openlog(bb_applet_name, 0, LOG_DAEMON);
305 vsyslog( pri , fmt, ap);
306 /* Man: A trailing newline is added when needed. */
309 /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
310 if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
311 bb_error_msg(fmt, ap);
316 static void msg_logger_and_die(int pri, const char* fmt, ...)
321 msg_logger(pri, fmt, ap);
327 #if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
328 #define devfsd_error_msg(fmt, args...) bb_error_msg(fmt, ## args)
329 #define devfsd_perror_msg_and_die(fmt, args...) bb_perror_msg_and_die(fmt, ## args)
330 #define devfsd_error_msg_and_die(fmt, args...) bb_error_msg_and_die(fmt, ## args)
331 #if defined(CONFIG_DEBUG)
332 #define debug_msg_logger(x, fmt, args...) msg_logger(x, fmt, ## args)
334 #define debug_msg_logger(x, fmt, args...)
337 #define debug_msg_logger(x, fmt, args...)
338 #define msg_logger(p, fmt, args...)
339 #define msg_logger_and_die(p, fmt, args...) exit(1)
340 #define devfsd_perror_msg_and_die(fmt, args...) exit(1)
341 #define devfsd_error_msg_and_die(fmt, args...) exit(1)
342 #define devfsd_error_msg(fmt, args...)
345 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
347 if (ioctl (fd, request, event_mask_flag) == -1)
348 msg_logger_and_die(LOG_ERR, "ioctl");
351 static void fork_and_execute(int die, char *arg0, char **arg )
359 /* Parent: Error : die or return */
360 msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted);
365 /* Parent : ok : return or exit */
373 /* Child : if arg0 != NULL do execvp */
377 msg_logger_and_die(LOG_ERR, "execvp");
381 static void safe_memcpy( char *dest, const char *src, int len)
383 memcpy (dest , src , len );
387 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
389 if( d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
390 return (2 + addendum);
391 else if( d[n - 2]=='c' && d[n - 1]=='d')
392 return (3 + addendum);
393 else if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
394 return (4 + addendum);
395 else if( ptr[n - 2]=='m' && ptr[n - 1]=='t')
396 return (5 + addendum);
401 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
403 if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/')
405 if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
406 d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
409 return scan_dev_name_common(d, n, 0, ptr);
411 else if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
412 d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
414 return scan_dev_name_common(d, n, 4, ptr);
416 else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
420 else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
424 else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
431 /* Public functions follow */
433 int devfsd_main (int argc, char **argv)
435 int print_version = FALSE;
436 int do_daemon = TRUE;
437 int no_polling = FALSE;
439 int fd, proto_rev, count;
440 unsigned long event_mask = 0;
441 struct sigaction new_action;
442 struct initial_symlink_struct *curr;
447 for (count = 2; count < argc; ++count)
449 if(argv[count][0] == '-')
451 if(argv[count][1]=='v' && !argv[count][2]) /* -v */
452 print_version = TRUE;
453 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f'
454 && argv[count][2]=='g' && !argv[count][3]) /* -fg */
456 else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n'
457 && argv[count][2]=='p' && !argv[count][3]) /* -np */
464 /* strip last / from mount point, so we don't need to check for it later */
465 while( argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
466 argv[1][strlen(argv[1]) -1] = '\0';
468 mount_point = argv[1];
470 if (chdir (mount_point) != 0)
471 devfsd_perror_msg_and_die(mount_point);
473 fd = bb_xopen (".devfsd", O_RDONLY);
475 if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
476 devfsd_perror_msg_and_die("FD_CLOEXEC");
478 if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
479 msg_logger_and_die(LOG_ERR, "ioctl");
481 /*setup initial entries */
482 for (curr = initial_symlinks; curr->dest != NULL; ++curr)
483 symlink (curr->dest, curr->name);
485 /* NB: The check for CONFIG_FILE is done in read_config_file() */
487 if ( print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
489 bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
490 bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
491 DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
492 if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
493 bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
494 exit(EXIT_SUCCESS); /* -v */
496 /* Tell kernel we are special (i.e. we get to see hidden entries) */
497 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
499 sigemptyset (&new_action.sa_mask);
500 new_action.sa_flags = 0;
502 /* Set up SIGHUP and SIGUSR1 handlers */
503 new_action.sa_handler = signal_handler;
504 if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
505 devfsd_error_msg_and_die( "sigaction");
507 bb_printf("%s v%s started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
509 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
511 read_config_file (CONFIG_FILE, FALSE, &event_mask);
512 /* Do the scan before forking, so that boot scripts see the finished product */
513 dir_operation(SERVICE,mount_point,0,NULL);
515 if (ENABLE_DEVFSD_FG_NP && no_polling)
519 /* Release so that the child can grab it */
520 do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
521 fork_and_execute(DIE, NULL, NULL);
522 setsid (); /* Prevent hangups and become pgrp leader */
523 } else if(ENABLE_DEVFSD_FG_NP) {
524 setpgid (0, 0); /* Become process group leader */
529 do_scan = do_servicing (fd, event_mask);
532 read_config_file (CONFIG_FILE, FALSE, &event_mask);
534 dir_operation(SERVICE,mount_point,0,NULL);
536 } /* End Function main */
539 /* Private functions follow */
541 static void read_config_file (char *path, int optional, unsigned long *event_mask)
542 /* [SUMMARY] Read a configuration database.
543 <path> The path to read the database from. If this is a directory, all
544 entries in that directory will be read (except hidden entries).
545 <optional> If TRUE, the routine will silently ignore a missing config file.
546 <event_mask> The event mask is written here. This is not initialised.
552 char buf[STRING_LENGTH];
555 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
557 if (stat (path, &statbuf) == 0 )
559 /* Don't read 0 length files: ignored */
560 /*if( statbuf.st_size == 0 )
562 if ( S_ISDIR (statbuf.st_mode) )
564 /* strip last / from dirname so we don't need to check for it later */
565 while( path && path[1]!='\0' && path[strlen(path)-1] == '/')
566 path[strlen(path) -1] = '\0';
568 dir_operation(READ_CONFIG, path, 0, event_mask);
571 if ( ( fp = fopen (path, "r") ) != NULL )
573 while (fgets (buf, STRING_LENGTH, fp) != NULL)
575 /* Skip whitespace */
576 for (line = buf; isspace (*line); ++line)
578 if (line[0] == '\0' || line[0] == '#' )
580 process_config_line (line, event_mask);
584 goto read_config_file_err;
587 read_config_file_err:
588 if(optional == 0 && errno == ENOENT)
589 msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
592 } /* End Function read_config_file */
594 static void process_config_line (const char *line, unsigned long *event_mask)
595 /* [SUMMARY] Process a line from a configuration file.
596 <line> The configuration line.
597 <event_mask> The event mask is written here. This is not initialised.
602 struct config_entry_struct *new;
603 char p[MAX_ARGS][STRING_LENGTH];
604 char when[STRING_LENGTH], what[STRING_LENGTH];
605 char name[STRING_LENGTH];
610 /* !!!! Only Uppercase Keywords in devsfd.conf */
611 static const char *const options[] = {
612 "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
613 "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
614 "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
615 "RMOLDCOMPAT", "RMNEWCOMPAT", 0
618 debug_msg_logger(LOG_INFO, __FUNCTION__);
620 for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
621 num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
623 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
625 i = compare_string_array(options, when );
636 goto process_config_line_err;
638 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
639 if( i == 1 || i == 2 )
641 st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
642 msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
643 read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
649 dir_operation(RESTORE,name, strlen (name),NULL);
653 goto process_config_line_err;
655 new = xmalloc (sizeof *new);
656 memset (new, 0, sizeof *new);
658 for (count = 0; event_types[count].config_name != NULL; ++count)
660 if (strcasecmp (when, event_types[count].config_name) != 0)
662 new->action.when = event_types[count].type;
665 if (event_types[count].config_name == NULL)
668 goto process_config_line_err;
671 i = compare_string_array(options, what );
675 case 4: /* "PERMISSIONS" */
676 new->action.what = AC_PERMISSIONS;
677 /* Get user and group */
678 if ( ( ptr = strchr (p[0], '.') ) == NULL )
681 goto process_config_line_err; /*"missing '.' in UID.GID"*/
685 new->u.permissions.uid = get_uid_gid (UID, p[0]);
686 new->u.permissions.gid = get_uid_gid (GID, ptr);
688 new->u.permissions.mode = get_mode (p[1]);
690 case 5: /* MODLOAD */
691 /*This action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
692 the device name) to the module loading facility. In addition,
693 the /etc/modules.devfs configuration file is used.*/
694 if (ENABLE_DEVFSD_MODLOAD)
695 new->action.what = AC_MODLOAD;
697 case 6: /* EXECUTE */
698 new->action.what = AC_EXECUTE;
701 for (count = 0; count < num_args; ++count)
702 new->u.execute.argv[count] = bb_xstrdup (p[count]);
704 new->u.execute.argv[num_args] = NULL;
707 new->action.what = AC_COPY;
710 goto process_config_line_err; /* missing path and function in line */
712 new->u.copy.source = bb_xstrdup (p[0]);
713 new->u.copy.destination = bb_xstrdup (p[1]);
717 case 9: /* MKOLDCOMPAT */
719 case 10: /* MKNEWCOMPAT */
721 case 11:/* RMOLDCOMPAT */
723 case 12: /* RMNEWCOMPAT */
729 new->action.what = i - 2;
733 goto process_config_line_err;
737 xregcomp( &new->preg, name, REG_EXTENDED);
739 *event_mask |= 1 << new->action.when;
741 if (first_config == NULL)
744 last_config->next = new;
747 process_config_line_err:
748 msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
749 } /* End Function process_config_line */
751 static int do_servicing (int fd, unsigned long event_mask)
752 /* [SUMMARY] Service devfs changes until a signal is received.
753 <fd> The open control file.
754 <event_mask> The event mask.
755 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
759 struct devfsd_notify_struct info;
760 unsigned long tmp_event_mask;
762 debug_msg_logger(LOG_INFO, __FUNCTION__);
764 /* Tell devfs what events we care about */
765 tmp_event_mask = event_mask;
766 do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
767 while (!caught_signal)
770 bytes = read (fd, (char *) &info, sizeof info);
772 break; /* Must test for this first */
774 continue; /* Yes, the order is important */
777 service_name (&info);
781 int c_sighup = caught_sighup;
783 caught_signal = FALSE;
784 caught_sighup = FALSE;
787 msg_logger_and_die(LOG_ERR, "read error on control file");
788 } /* End Function do_servicing */
790 static void service_name (const struct devfsd_notify_struct *info)
791 /* [SUMMARY] Service a single devfs change.
792 <info> The devfs change.
797 regmatch_t mbuf[MAX_SUBEXPR];
798 struct config_entry_struct *entry;
800 debug_msg_logger(LOG_INFO, __FUNCTION__);
801 if (ENABLE_DEBUG && info->overrun_count > 0)
802 debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
804 /* Discard lookups on "/dev/log" and "/dev/initctl" */
805 if( info->type == DEVFSD_NOTIFY_LOOKUP &&
806 ((info->devname[0]=='l' && info->devname[1]=='o' &&
807 info->devname[2]=='g' && !info->devname[3]) ||
808 ( info->devname[0]=='i' && info->devname[1]=='n' &&
809 info->devname[2]=='i' && info->devname[3]=='t' &&
810 info->devname[4]=='c' && info->devname[5]=='t' &&
811 info->devname[6]=='l' && !info->devname[7])))
813 for (entry = first_config; entry != NULL; entry = entry->next)
815 /* First check if action matches the type, then check if name matches */
816 if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
818 for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
821 debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
823 switch (entry->action.what)
826 action_permissions (info, entry);
829 if(ENABLE_DEVFSD_MODLOAD)
830 action_modload (info, entry);
833 action_execute (info, entry, mbuf, n);
836 action_copy (info, entry, mbuf, n);
845 action_compat (info, entry->action.what);
848 msg_logger_and_die(LOG_ERR, "Unknown action");
851 } /* End Function service_name */
853 static void action_permissions (const struct devfsd_notify_struct *info,
854 const struct config_entry_struct *entry)
855 /* [SUMMARY] Update permissions for a device entry.
856 <info> The devfs change.
857 <entry> The config file entry.
863 debug_msg_logger(LOG_INFO, __FUNCTION__);
865 if ( stat (info->devname, &statbuf) != 0 ||
866 chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
867 chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
869 msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
871 } /* End Function action_permissions */
873 static void action_modload (const struct devfsd_notify_struct *info,
874 const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
875 /* [SUMMARY] Load a module.
876 <info> The devfs change.
877 <entry> The config file entry.
882 char device[STRING_LENGTH];
885 argv[1] = MODPROBE_SWITCH_1; /* "-k" */
886 argv[2] = MODPROBE_SWITCH_2; /* "-C" */
887 argv[3] = CONFIG_MODULES_DEVFS;
891 snprintf (device, sizeof (device), "/dev/%s", info->devname);
892 debug_msg_logger(LOG_INFO, "%s: %s %s %s %s %s",__FUNCTION__, argv[0],argv[1],argv[2],argv[3],argv[4]);
893 fork_and_execute(DIE, argv[0], argv);
894 } /* End Function action_modload */
896 static void action_execute (const struct devfsd_notify_struct *info,
897 const struct config_entry_struct *entry,
898 const regmatch_t *regexpr, unsigned int numexpr)
899 /* [SUMMARY] Execute a programme.
900 <info> The devfs change.
901 <entry> The config file entry.
902 <regexpr> The number of subexpression (start, end) offsets within the
904 <numexpr> The number of elements within <<regexpr>>.
909 struct get_variable_info gv_info;
910 char *argv[MAX_ARGS + 1];
911 char largv[MAX_ARGS + 1][STRING_LENGTH];
913 debug_msg_logger(LOG_INFO ,__FUNCTION__);
915 gv_info.devname = info->devname;
916 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
917 for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
919 expand_expression (largv[count], STRING_LENGTH,
920 entry->u.execute.argv[count],
921 get_variable, &gv_info,
922 gv_info.devname, regexpr, numexpr );
923 argv[count] = largv[count];
926 fork_and_execute(NO_DIE, argv[0], argv);
927 } /* End Function action_execute */
930 static void action_copy (const struct devfsd_notify_struct *info,
931 const struct config_entry_struct *entry,
932 const regmatch_t *regexpr, unsigned int numexpr)
933 /* [SUMMARY] Copy permissions.
934 <info> The devfs change.
935 <entry> The config file entry.
936 <regexpr> This list of subexpression (start, end) offsets within the
938 <numexpr> The number of elements in <<regexpr>>.
943 struct get_variable_info gv_info;
944 struct stat source_stat, dest_stat;
945 char source[STRING_LENGTH], destination[STRING_LENGTH];
948 debug_msg_logger(LOG_INFO, __FUNCTION__);
950 dest_stat.st_mode = 0;
952 if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
955 gv_info.devname = info->devname;
957 snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
958 expand_expression (source, STRING_LENGTH, entry->u.copy.source,
959 get_variable, &gv_info, gv_info.devname,
962 expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
963 get_variable, &gv_info, gv_info.devname,
966 if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
968 lstat (destination, &dest_stat);
969 new_mode = source_stat.st_mode & ~S_ISVTX;
970 if (info->type == DEVFSD_NOTIFY_CREATE)
972 else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
974 ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
975 if (ENABLE_DEBUG && ret && (errno != EEXIST))
976 debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
978 } /* End Function action_copy */
980 static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
981 /* [SUMMARY] Process a compatibility request.
982 <info> The devfs change.
983 <action> The action to take.
988 const char *compat_name = NULL;
989 const char *dest_name = info->devname;
991 char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
992 int mode, host, bus, target, lun;
995 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
996 static const char *const fmt[] = {
998 "sg/c%db%dt%du%d", /* scsi/generic */
999 "sd/c%db%dt%du%d", /* scsi/disc */
1000 "sr/c%db%dt%du%d", /* scsi/cd */
1001 "sd/c%db%dt%du%dp%d", /* scsi/part */
1002 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
1003 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
1004 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
1005 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
1006 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
1010 /* First construct compatibility name */
1013 case AC_MKOLDCOMPAT:
1014 case AC_RMOLDCOMPAT:
1015 compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
1017 case AC_MKNEWCOMPAT:
1018 case AC_RMNEWCOMPAT:
1019 ptr = strrchr (info->devname, '/') + 1;
1020 i=scan_dev_name(info->devname, info->namelen, ptr);
1022 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
1028 sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
1029 snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
1030 dest_name = dest_buf;
1031 compat_name = compat_buf;
1034 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
1035 if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
1036 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
1038 /* 4 == scsi/part 8 == ide/host/part */
1039 if( i == 4 || i == 8)
1040 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
1045 rewind_ = info->devname[info->namelen - 1];
1049 if(ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/)
1050 mode = ptr[2] - 107; /* 1 or 2 */
1053 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
1056 /* 9 == ide/host/mt */
1058 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
1060 } /* switch(action) */
1062 if(compat_name == NULL )
1065 debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
1067 /* Now decide what to do with it */
1070 case AC_MKOLDCOMPAT:
1071 case AC_MKNEWCOMPAT:
1072 mksymlink (dest_name, compat_name);
1074 case AC_RMOLDCOMPAT:
1075 case AC_RMNEWCOMPAT:
1076 ret = unlink (compat_name);
1077 if (ENABLE_DEBUG && ret)
1078 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
1081 } /* switch(action) */
1082 } /* End Function action_compat */
1084 static void restore(char *spath, struct stat source_stat, int rootlen)
1086 char dpath[STRING_LENGTH];
1087 struct stat dest_stat;
1089 debug_msg_logger(LOG_INFO, __FUNCTION__);
1091 dest_stat.st_mode = 0;
1092 snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
1093 lstat (dpath, &dest_stat);
1095 if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
1096 copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
1098 if ( S_ISDIR (source_stat.st_mode) )
1099 dir_operation(RESTORE, spath, rootlen,NULL);
1103 static int copy_inode (const char *destpath, const struct stat *dest_stat,
1105 const char *sourcepath, const struct stat *source_stat)
1106 /* [SUMMARY] Copy an inode.
1107 <destpath> The destination path. An existing inode may be deleted.
1108 <dest_stat> The destination stat(2) information.
1109 <new_mode> The desired new mode for the destination.
1110 <sourcepath> The source path.
1111 <source_stat> The source stat(2) information.
1112 [RETURNS] TRUE on success, else FALSE.
1115 int source_len, dest_len;
1116 char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
1118 struct sockaddr_un un_addr;
1119 char symlink_val[STRING_LENGTH];
1121 debug_msg_logger(LOG_INFO, __FUNCTION__);
1123 if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
1126 if ( S_ISLNK (source_stat->st_mode) )
1128 if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
1129 ( dest_len = readlink (destpath , dest_link , STRING_LENGTH - 1) ) < 0 )
1131 source_link[source_len] = '\0';
1132 dest_link[dest_len] = '\0';
1133 if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
1136 symlink (source_link, destpath);
1139 } /* Else not a symlink */
1140 chmod (destpath, new_mode & ~S_IFMT);
1141 chown (destpath, source_stat->st_uid, source_stat->st_gid);
1144 /* Different types: unlink and create */
1146 switch (source_stat->st_mode & S_IFMT)
1149 if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
1151 un_addr.sun_family = AF_UNIX;
1152 snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
1153 val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
1155 if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
1159 if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
1161 symlink_val[val] = '\0';
1162 if (symlink (symlink_val, destpath) == 0)
1166 if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
1169 if (chmod (destpath, new_mode & ~S_IFMT) != 0)
1175 if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
1179 if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
1182 if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
1187 } /* End Function copy_inode */
1189 static void free_config ()
1190 /* [SUMMARY] Free the configuration information.
1194 struct config_entry_struct *c_entry;
1197 debug_msg_logger(LOG_INFO, __FUNCTION__);
1199 for (c_entry = first_config; c_entry != NULL; c_entry = next)
1203 next = c_entry->next;
1204 regfree (&c_entry->preg);
1205 if (c_entry->action.what == AC_EXECUTE)
1207 for (count = 0; count < MAX_ARGS; ++count)
1209 if (c_entry->u.execute.argv[count] == NULL)
1211 free (c_entry->u.execute.argv[count]);
1216 first_config = NULL;
1218 } /* End Function free_config */
1220 static int get_uid_gid (int flag, const char *string)
1221 /* [SUMMARY] Convert a string to a UID or GID value.
1222 <flag> "UID" or "GID".
1223 <string> The string.
1224 [RETURNS] The UID or GID value.
1227 struct passwd *pw_ent;
1228 struct group *grp_ent;
1231 if (ENABLE_DEVFSD_VERBOSE)
1234 debug_msg_logger(LOG_INFO, __FUNCTION__);
1236 if(ENABLE_DEBUG && flag != UID && flag != GID)
1237 msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
1239 if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
1240 return atoi (string);
1242 if ( flag == UID && ( pw_ent = getpwnam (string) ) != NULL )
1243 return (pw_ent->pw_uid);
1245 if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
1246 return (grp_ent->gr_gid);
1247 else if(ENABLE_DEVFSD_VERBOSE)
1250 if(ENABLE_DEVFSD_VERBOSE)
1251 msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
1253 }/* End Function get_uid_gid */
1255 static mode_t get_mode (const char *string)
1256 /* [SUMMARY] Convert a string to a mode value.
1257 <string> The string.
1258 [RETURNS] The mode value.
1264 debug_msg_logger(LOG_INFO, __FUNCTION__);
1266 if ( isdigit (string[0]) )
1267 return strtoul (string, NULL, 8);
1268 if (strlen (string) != 9)
1269 msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
1275 if(string[0]=='r'||string[0]=='w'||string[0]=='x')
1281 } /* End Function get_mode */
1283 static void signal_handler (int sig)
1285 debug_msg_logger(LOG_INFO, __FUNCTION__);
1287 caught_signal = TRUE;
1289 caught_sighup = TRUE;
1291 msg_logger(LOG_INFO, "Caught signal %d", sig);
1292 } /* End Function signal_handler */
1294 static const char *get_variable (const char *variable, void *info)
1296 struct get_variable_info *gv_info = info;
1297 static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
1298 const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
1299 "uid", "gid", "mode", hostname, mount_point,
1300 gv_info->devpath, gv_info->devname, 0 };
1303 debug_msg_logger(LOG_INFO, __FUNCTION__);
1305 if (gethostname (hostname, STRING_LENGTH - 1) != 0)
1306 msg_logger_and_die(LOG_ERR, "gethostname: %m");
1308 /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
1309 hostname[STRING_LENGTH - 1] = '\0';
1311 /* compare_string_array returns i>=0 */
1312 i=compare_string_array(field_names, variable);
1314 if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL))
1316 if( i >= 0 && i <= 3)
1318 debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
1319 return(field_names[i+7]);
1323 sprintf (sbuf, "%u", gv_info->info->uid);
1325 sprintf (sbuf, "%u", gv_info->info->gid);
1327 sprintf (sbuf, "%o", gv_info->info->mode);
1329 debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
1332 } /* End Function get_variable */
1334 static void service(struct stat statbuf, char *path)
1336 struct devfsd_notify_struct info;
1338 debug_msg_logger(LOG_INFO, __FUNCTION__);
1340 memset (&info, 0, sizeof info);
1341 info.type = DEVFSD_NOTIFY_REGISTERED;
1342 info.mode = statbuf.st_mode;
1343 info.major = major (statbuf.st_rdev);
1344 info.minor = minor (statbuf.st_rdev);
1345 info.uid = statbuf.st_uid;
1346 info.gid = statbuf.st_gid;
1347 snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
1348 info.namelen = strlen (info.devname);
1349 service_name (&info);
1350 if ( S_ISDIR (statbuf.st_mode) )
1351 dir_operation(SERVICE,path,0,NULL);
1354 static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
1355 /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
1356 <flag> To choose which function to perform
1357 <dp> The directory pointer. This is closed upon completion.
1358 <dir_name> The name of the directory.
1359 <rootlen> string length parameter.
1363 struct stat statbuf;
1366 char path[STRING_LENGTH];
1368 debug_msg_logger(LOG_INFO, __FUNCTION__);
1370 if((dp = opendir( dir_name))==NULL)
1372 debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
1376 while ( (de = readdir (dp) ) != NULL )
1379 if(de->d_name && *de->d_name == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])))
1381 snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
1382 debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
1384 if (lstat (path, &statbuf) != 0)
1386 debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
1392 service(statbuf,path);
1395 restore(path, statbuf, var);
1398 read_config_file (path, var, event_mask);
1403 } /* End Function do_scan_and_service */
1405 static int mksymlink (const char *oldpath, const char *newpath)
1406 /* [SUMMARY] Create a symlink, creating intervening directories as required.
1407 <oldpath> The string contained in the symlink.
1408 <newpath> The name of the new symlink.
1409 [RETURNS] 0 on success, else -1.
1412 debug_msg_logger(LOG_INFO, __FUNCTION__);
1414 if ( !make_dir_tree (newpath) )
1417 if (symlink (oldpath, newpath) != 0)
1419 if (errno != EEXIST)
1421 debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
1426 } /* End Function mksymlink */
1429 static int make_dir_tree (const char *path)
1430 /* [SUMMARY] Creating intervening directories for a path as required.
1431 <path> The full pathname (including the leaf node).
1432 [RETURNS] TRUE on success, else FALSE.
1435 debug_msg_logger(LOG_INFO, __FUNCTION__);
1437 if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
1439 debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
1443 } /* End Function make_dir_tree */
1445 static int expand_expression(char *output, unsigned int outsize,
1447 const char *(*get_variable_func)(const char *variable, void *info),
1449 const char *devname,
1450 const regmatch_t *ex, unsigned int numexp)
1451 /* [SUMMARY] Expand environment variables and regular subexpressions in string.
1452 <output> The output expanded expression is written here.
1453 <length> The size of the output buffer.
1454 <input> The input expression. This may equal <<output>>.
1455 <get_variable> A function which will be used to get variable values. If
1456 this returns NULL, the environment is searched instead. If this is NULL,
1457 only the environment is searched.
1458 <info> An arbitrary pointer passed to <<get_variable>>.
1459 <devname> Device name; specifically, this is the string that contains all
1460 of the regular subexpressions.
1461 <ex> Array of start / end offsets into info->devname for each subexpression
1462 <numexp> Number of regular subexpressions found in <<devname>>.
1463 [RETURNS] TRUE on success, else FALSE.
1466 char temp[STRING_LENGTH];
1468 debug_msg_logger(LOG_INFO, __FUNCTION__);
1470 if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
1472 expand_regexp (output, outsize, temp, devname, ex, numexp);
1474 } /* End Function expand_expression */
1476 static void expand_regexp (char *output, size_t outsize, const char *input,
1477 const char *devname,
1478 const regmatch_t *ex, unsigned int numex )
1479 /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1480 <output> The output expanded expression is written here.
1481 <outsize> The size of the output buffer.
1482 <input> The input expression. This may NOT equal <<output>>, because
1483 supporting that would require yet another string-copy. However, it's not
1484 hard to write a simple wrapper function to add this functionality for those
1485 few cases that need it.
1486 <devname> Device name; specifically, this is the string that contains all
1487 of the regular subexpressions.
1488 <ex> An array of start and end offsets into <<devname>>, one for each
1490 <numex> Number of subexpressions in the offset-array <<ex>>.
1494 const char last_exp = '0' - 1 + numex;
1497 debug_msg_logger(LOG_INFO, __FUNCTION__);
1499 /* Guarantee NULL termination by writing an explicit '\0' character into
1500 the very last byte */
1502 output[--outsize] = '\0';
1503 /* Copy the input string into the output buffer, replacing '\\' with '\'
1504 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1505 codes are deleted */
1506 while ( (c != '\0') && (outsize != 0) )
1516 if ((c >= '0') && (c <= last_exp))
1518 const regmatch_t *subexp = ex + (c - '0');
1519 unsigned int sublen = subexp->rm_eo - subexp->rm_so;
1521 /* Range checking */
1522 if (sublen > outsize)
1524 strncpy (output, devname + subexp->rm_so, sublen);
1535 } /* End Function expand_regexp */
1538 /* from compat_name.c */
1540 struct translate_struct
1542 char *match; /* The string to match to (up to length) */
1543 char *format; /* Format of output, "%s" takes data past match string,
1544 NULL is effectively "%s" (just more efficient) */
1547 static struct translate_struct translate_table[] =
1550 {"printers/", "lp%s"},
1552 {"parports/", "parport%s"},
1555 {"loop/", "loop%s"},
1556 {"floppy/", "fd%s"},
1558 {"md/", "md%s"}, /* Meta-devices */
1562 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1564 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1565 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1566 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1567 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1568 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1569 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1570 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1571 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1572 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1573 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1574 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1575 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1576 {"input/js", "js%s"}, /* Joystick driver */
1580 const char *get_old_name (const char *devname, unsigned int namelen,
1581 char *buffer, unsigned int major, unsigned int minor)
1582 /* [SUMMARY] Translate a kernel-supplied name into an old name.
1583 <devname> The device name provided by the kernel.
1584 <namelen> The length of the name.
1585 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1586 <major> The major number for the device.
1587 <minor> The minor number for the device.
1588 [RETURNS] A pointer to the old name if known, else NULL.
1591 const char *compat_name = NULL;
1593 struct translate_struct *trans;
1600 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
1601 static const char *const fmt[] = {
1603 "sg%u", /* scsi/generic */
1604 NULL, /* scsi/disc */
1605 "sr%u", /* scsi/cd */
1606 NULL, /* scsi/part */
1607 "nst%u%c", /* scsi/mt */
1608 "hd%c" , /* ide/host/disc */
1609 "hd%c" , /* ide/host/cd */
1610 "hd%c%s", /* ide/host/part */
1611 "%sht%d", /* ide/host/mt */
1612 "sbpcd%u", /* sbp/ */
1614 "%cty%c%c", /* pty/ */
1618 debug_msg_logger(LOG_INFO, __FUNCTION__);
1620 for (trans = translate_table; trans->match != NULL; ++trans)
1622 len = strlen (trans->match);
1624 if (strncmp (devname, trans->match, len) == 0)
1626 if (trans->format == NULL)
1627 return (devname + len);
1628 sprintf (buffer, trans->format, devname + len);
1633 ptr = (strrchr (devname, '/') + 1);
1634 i = scan_dev_name(devname, namelen, ptr);
1636 if( i > 0 && i < 13)
1637 compat_name = buffer;
1641 debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
1643 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1644 if( i == 1 || i == 3 || i == 10 )
1645 sprintf (buffer, fmt[i], minor);
1647 /* 2 ==scsi/disc, 4 == scsi/part */
1648 if( i == 2 || i == 4)
1649 compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
1657 sprintf (buffer, fmt[i], minor & 0x1f, mode);
1658 if (devname[namelen - 1] != 'n')
1661 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1662 if( i == 6 || i == 7 || i == 8 )
1663 /* last arg should be ignored for i == 6 or i== 7 */
1664 sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
1666 /* 9 == ide/host/mt */
1668 sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
1673 sprintf (buffer, fmt[i], devname + 4);
1674 if (buffer[3] == '0')
1680 pty1 = "pqrstuvwxyzabcde";
1681 pty2 = "0123456789abcdef";
1682 indexx = atoi (devname + 5);
1683 sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
1686 if(ENABLE_DEBUG && compat_name!=NULL)
1687 msg_logger(LOG_INFO, "%s: compat_name %s", __FUNCTION__, compat_name);
1689 return (compat_name);
1690 } /* End Function get_old_name */
1692 static char get_old_ide_name (unsigned int major, unsigned int minor)
1693 /* [SUMMARY] Get the old IDE name for a device.
1694 <major> The major number for the device.
1695 <minor> The minor number for the device.
1696 [RETURNS] The drive letter.
1699 char letter='y'; /* 121 */
1700 char c='a'; /* 97 */
1703 debug_msg_logger(LOG_INFO, __FUNCTION__);
1705 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1707 if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
1708 i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
1709 i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
1712 if((unsigned int)i==major)
1720 } while(i<=IDE9_MAJOR);
1725 } /* End Function get_old_ide_name */
1727 static char *write_old_sd_name (char *buffer,
1728 unsigned int major, unsigned int minor,
1730 /* [SUMMARY] Write the old SCSI disc name to a buffer.
1731 <buffer> The buffer to write to.
1732 <major> The major number for the device.
1733 <minor> The minor number for the device.
1734 <part> The partition string. Must be "" for a whole-disc entry.
1735 [RETURNS] A pointer to the buffer on success, else NULL.
1738 unsigned int disc_index;
1740 debug_msg_logger(LOG_INFO, __FUNCTION__);
1744 sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
1747 if ( (major > 64) && (major < 72) )
1749 disc_index = ( (major - 64) << 4 ) + (minor >> 4);
1750 if (disc_index < 26)
1751 sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
1753 sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
1757 } /* End Function write_old_sd_name */
1762 /*EXPERIMENTAL_FUNCTION*/
1764 int st_expr_expand (char *output, unsigned int length, const char *input,
1765 const char *(*get_variable_func) (const char *variable,
1768 /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1769 <output> The output expanded expression is written here.
1770 <length> The size of the output buffer.
1771 <input> The input expression. This may equal <<output>>.
1772 <get_variable> A function which will be used to get variable values. If
1773 this returns NULL, the environment is searched instead. If this is NULL,
1774 only the environment is searched.
1775 <info> An arbitrary pointer passed to <<get_variable>>.
1776 [RETURNS] TRUE on success, else FALSE.
1781 unsigned int out_pos = 0;
1784 struct passwd *pwent;
1785 char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
1787 debug_msg_logger(LOG_INFO, __FUNCTION__);
1789 if (length > BUFFER_SIZE)
1790 length = BUFFER_SIZE;
1791 for (; TRUE; ++input)
1793 switch (ch = *input)
1796 /* Variable expansion */
1797 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
1802 /* Home directory expansion */
1804 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
1806 /* User's own home directory: leave separator for next time */
1807 if ( ( env = getenv ("HOME") ) == NULL )
1809 msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
1813 if (len + out_pos >= length)
1814 goto st_expr_expand_out;
1815 memcpy (buffer + out_pos, env, len + 1);
1819 /* Someone else's home directory */
1820 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
1823 if (len >= sizeof tmp)
1824 goto st_expr_expand_out;
1825 safe_memcpy (tmp, input, len);
1827 if ( ( pwent = getpwnam (tmp) ) == NULL )
1829 msg_logger(LOG_INFO, "no pwent for: %s", tmp);
1832 len = strlen (pwent->pw_dir);
1833 if (len + out_pos >= length)
1834 goto st_expr_expand_out;
1835 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
1841 if (out_pos >= length)
1842 goto st_expr_expand_out;
1843 buffer[out_pos++] = ch;
1846 memcpy (output, buffer, out_pos);
1855 msg_logger(LOG_INFO, bb_msg_small_buffer);
1857 } /* End Function st_expr_expand */
1860 /* Private functions follow */
1862 static const char *expand_variable (char *buffer, unsigned int length,
1863 unsigned int *out_pos, const char *input,
1864 const char *(*func) (const char *variable,
1867 /* [SUMMARY] Expand a variable.
1868 <buffer> The buffer to write to.
1869 <length> The length of the output buffer.
1870 <out_pos> The current output position. This is updated.
1871 <input> A pointer to the input character pointer.
1872 <func> A function which will be used to get variable values. If this
1873 returns NULL, the environment is searched instead. If this is NULL, only
1874 the environment is searched.
1875 <info> An arbitrary pointer passed to <<func>>.
1876 <errfp> Diagnostic messages are written here.
1877 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1882 unsigned int open_braces;
1883 const char *env, *ptr;
1884 char tmp[STRING_LENGTH];
1886 debug_msg_logger(LOG_INFO, __FUNCTION__);
1891 /* Special case for "$$": PID */
1892 sprintf ( tmp, "%d", (int) getpid () );
1894 if (len + *out_pos >= length)
1895 goto expand_variable_out;
1897 memcpy (buffer + *out_pos, tmp, len + 1);
1901 /* Ordinary variable expansion, possibly in braces */
1904 /* Simple variable expansion */
1905 for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
1908 if ((size_t)len >= sizeof tmp)
1909 goto expand_variable_out;
1911 safe_memcpy (tmp, input, len);
1913 if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
1915 msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
1919 if (len + *out_pos >= length)
1920 goto expand_variable_out;
1922 memcpy (buffer + *out_pos, env, len + 1);
1926 /* Variable in braces: check for ':' tricks */
1928 for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
1932 /* Must be simple variable expansion with "${var}" */
1934 if ((size_t)len >= sizeof tmp)
1935 goto expand_variable_out;
1937 safe_memcpy (tmp, input, len);
1938 ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
1941 return (input + len);
1943 if (ch != ':' || ptr[1] != '-' )
1945 msg_logger(LOG_INFO, "illegal char in var name");
1948 /* It's that handy "${var:-word}" expression. Check if var is defined */
1950 if ((size_t)len >= sizeof tmp)
1951 goto expand_variable_out;
1953 safe_memcpy (tmp, input, len);
1954 /* Move input pointer to ':' */
1956 /* First skip to closing brace, taking note of nested expressions */
1959 for (open_braces = 1; open_braces > 0; ch = *++ptr)
1970 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
1977 /* At this point ptr should point to closing brace of "${var:-word}" */
1978 if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
1980 /* Found environment variable, so skip the input to the closing brace
1981 and return the variable */
1984 if (len + *out_pos >= length)
1985 goto expand_variable_out;
1987 memcpy (buffer + *out_pos, env, len + 1);
1991 /* Environment variable was not found, so process word. Advance input
1992 pointer to start of word in "${var:-word}" */
1995 if ((size_t)len >= sizeof tmp)
1996 goto expand_variable_out;
1998 safe_memcpy (tmp, input, len);
2000 if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
2003 if (len + *out_pos >= length)
2004 goto expand_variable_out;
2006 memcpy (buffer + *out_pos, tmp, len + 1);
2009 expand_variable_out:
2010 msg_logger(LOG_INFO, bb_msg_small_buffer);
2012 } /* End Function expand_variable */
2015 static const char *get_variable_v2 (const char *variable,
2016 const char *(*func) (const char *variable, void *info),
2018 /* [SUMMARY] Get a variable from the environment or .
2019 <variable> The variable name.
2020 <func> A function which will be used to get the variable. If this returns
2021 NULL, the environment is searched instead. If this is NULL, only the
2022 environment is searched.
2023 [RETURNS] The value of the variable on success, else NULL.
2028 debug_msg_logger(LOG_INFO, __FUNCTION__);
2032 value = (*func) (variable, info);
2036 return getenv (variable);
2037 } /* End Function get_variable */