1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 devfsd implementation for busybox
9 Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it>
11 Busybox version is based on some previous work and ideas
12 Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
16 Main file for devfsd (devfs daemon for Linux).
18 Copyright (C) 1998-2002 Richard Gooch
22 Header file for devfsd (devfs daemon for Linux).
24 Copyright (C) 1998-2000 Richard Gooch
28 Compatibility name file for devfsd (build compatibility names).
30 Copyright (C) 1998-2002 Richard Gooch
34 This code provides Borne Shell-like expression expansion.
36 Copyright (C) 1997-1999 Richard Gooch
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2 of the License, or
41 (at your option) any later version.
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
52 Richard Gooch may be reached by email at rgooch@atnf.csiro.au
53 The postal address is:
54 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
56 //config:config DEVFSD
57 //config: bool "devfsd (obsolete)"
59 //config: select FEATURE_SYSLOG
61 //config: This is deprecated and should NOT be used anymore.
62 //config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
63 //config: See docs/mdev.txt for detailed instructions on how to use mdev
66 //config: Provides compatibility with old device names on a devfs systems.
67 //config: You should set it to true if you have devfs enabled.
68 //config: The following keywords in devsfd.conf are supported:
69 //config: "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE", "RESTORE",
70 //config: "PERMISSIONS", "EXECUTE", "COPY", "IGNORE",
71 //config: "MKOLDCOMPAT", "MKNEWCOMPAT","RMOLDCOMPAT", "RMNEWCOMPAT".
73 //config: But only if they are written UPPERCASE!!!!!!!!
75 //config:config DEVFSD_MODLOAD
76 //config: bool "Adds support for MODLOAD keyword in devsfd.conf"
78 //config: depends on DEVFSD
80 //config: This actually doesn't work with busybox modutils but needs
81 //config: the external modutils.
83 //config:config DEVFSD_FG_NP
84 //config: bool "Enable the -fg and -np options"
86 //config: depends on DEVFSD
88 //config: -fg Run the daemon in the foreground.
89 //config: -np Exit after parsing config. Do not poll for events.
91 //config:config DEVFSD_VERBOSE
92 //config: bool "Increases logging (and size)"
94 //config: depends on DEVFSD
96 //config: Increases logging to stderr or syslog.
98 //config:config FEATURE_DEVFS
99 //config: bool "Use devfs names for all devices (obsolete)"
102 //config: This is obsolete and should NOT be used anymore.
103 //config: Use linux >= 2.6 (optionally with hotplug) and mdev instead!
105 //config: For legacy systems -- if there is no way around devfsd -- this
106 //config: tells busybox to look for names like /dev/loop/0 instead of
107 //config: /dev/loop0. If your /dev directory has normal names instead of
108 //config: devfs names, you don't want this.
110 //applet:IF_DEVFSD(APPLET(devfsd, BB_DIR_SBIN, BB_SUID_DROP))
112 //kbuild:lib-$(CONFIG_DEVFSD) += devfsd.o
114 //usage:#define devfsd_trivial_usage
115 //usage: "mntpnt [-v]" IF_DEVFSD_FG_NP("[-fg][-np]")
116 //usage:#define devfsd_full_usage "\n\n"
117 //usage: "Manage devfs permissions and old device name symlinks\n"
118 //usage: "\n mntpnt The mount point where devfs is mounted"
119 //usage: "\n -v Print the protocol version numbers for devfsd"
120 //usage: "\n and the kernel-side protocol version and exit"
121 //usage: IF_DEVFSD_FG_NP(
122 //usage: "\n -fg Run in foreground"
123 //usage: "\n -np Exit after parsing the configuration file"
124 //usage: "\n and processing synthetic REGISTER events,"
125 //usage: "\n don't poll for events"
133 #include <sys/sysmacros.h>
135 /* Various defines taken from linux/major.h */
137 #define IDE1_MAJOR 22
138 #define IDE2_MAJOR 33
139 #define IDE3_MAJOR 34
140 #define IDE4_MAJOR 56
141 #define IDE5_MAJOR 57
142 #define IDE6_MAJOR 88
143 #define IDE7_MAJOR 89
144 #define IDE8_MAJOR 90
145 #define IDE9_MAJOR 91
148 /* Various defines taken from linux/devfs_fs.h */
149 #define DEVFSD_PROTOCOL_REVISION_KERNEL 5
150 #define DEVFSD_IOCTL_BASE 'd'
151 /* These are the various ioctls */
152 #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
153 #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
154 #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
155 #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
156 #define DEVFSD_NOTIFY_REGISTERED 0
157 #define DEVFSD_NOTIFY_UNREGISTERED 1
158 #define DEVFSD_NOTIFY_ASYNC_OPEN 2
159 #define DEVFSD_NOTIFY_CLOSE 3
160 #define DEVFSD_NOTIFY_LOOKUP 4
161 #define DEVFSD_NOTIFY_CHANGE 5
162 #define DEVFSD_NOTIFY_CREATE 6
163 #define DEVFSD_NOTIFY_DELETE 7
164 #define DEVFS_PATHLEN 1024
165 /* Never change this otherwise the binary interface will change */
167 struct devfsd_notify_struct
{
168 /* Use native C types to ensure same types in kernel and user space */
169 unsigned int type
; /* DEVFSD_NOTIFY_* value */
170 unsigned int mode
; /* Mode of the inode or device entry */
171 unsigned int major
; /* Major number of device entry */
172 unsigned int minor
; /* Minor number of device entry */
173 unsigned int uid
; /* Uid of process, inode or device entry */
174 unsigned int gid
; /* Gid of process, inode or device entry */
175 unsigned int overrun_count
; /* Number of lost events */
176 unsigned int namelen
; /* Number of characters not including '\0' */
177 /* The device name MUST come last */
178 char devname
[DEVFS_PATHLEN
]; /* This will be '\0' terminated */
181 #define BUFFER_SIZE 16384
182 #define DEVFSD_VERSION "1.3.25"
183 #define CONFIG_FILE "/etc/devfsd.conf"
184 #define MODPROBE "/sbin/modprobe"
185 #define MODPROBE_SWITCH_1 "-k"
186 #define MODPROBE_SWITCH_2 "-C"
187 #define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
188 #define MAX_ARGS (6 + 1)
189 #define MAX_SUBEXPR 10
190 #define STRING_LENGTH 255
192 /* for get_uid_gid() */
196 /* fork_and_execute() */
200 /* for dir_operation() */
203 #define READ_CONFIG 2
205 /* Update only after changing code to reflect new protocol */
206 #define DEVFSD_PROTOCOL_REVISION_DAEMON 5
208 /* Compile-time check */
209 #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
210 #error protocol version mismatch. Update your kernel headers
213 #define AC_PERMISSIONS 0
216 #define AC_MFUNCTION 3 /* not supported by busybox */
217 #define AC_CFUNCTION 4 /* not supported by busybox */
220 #define AC_MKOLDCOMPAT 7
221 #define AC_MKNEWCOMPAT 8
222 #define AC_RMOLDCOMPAT 9
223 #define AC_RMNEWCOMPAT 10
224 #define AC_RESTORE 11
226 struct permissions_type
{
232 struct execute_type
{
233 char *argv
[MAX_ARGS
+ 1]; /* argv[0] must always be the programme */
238 const char *destination
;
246 struct config_entry_struct
{
247 struct action_type action
;
251 struct permissions_type permissions
;
252 struct execute_type execute
;
253 struct copy_type copy
;
256 struct config_entry_struct
*next
;
259 struct get_variable_info
{
260 const struct devfsd_notify_struct
*info
;
262 char devpath
[STRING_LENGTH
];
265 static void dir_operation(int , const char * , int, unsigned long*);
266 static void service(struct stat statbuf
, char *path
);
267 static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
268 static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
269 static int mksymlink(const char *oldpath
, const char *newpath
);
270 static void read_config_file(char *path
, int optional
, unsigned long *event_mask
);
271 static void process_config_line(const char *, unsigned long *);
272 static int do_servicing(int, unsigned long);
273 static void service_name(const struct devfsd_notify_struct
*);
274 static void action_permissions(const struct devfsd_notify_struct
*, const struct config_entry_struct
*);
275 static void action_execute(const struct devfsd_notify_struct
*, const struct config_entry_struct
*,
276 const regmatch_t
*, unsigned);
277 static void action_modload(const struct devfsd_notify_struct
*info
, const struct config_entry_struct
*entry
);
278 static void action_copy(const struct devfsd_notify_struct
*, const struct config_entry_struct
*,
279 const regmatch_t
*, unsigned);
280 static void action_compat(const struct devfsd_notify_struct
*, unsigned);
281 static void free_config(void);
282 static void restore(char *spath
, struct stat source_stat
, int rootlen
);
283 static int copy_inode(const char *, const struct stat
*, mode_t
, const char *, const struct stat
*);
284 static mode_t
get_mode(const char *);
285 static void signal_handler(int);
286 static const char *get_variable(const char *, void *);
287 static int make_dir_tree(const char *);
288 static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
289 const char *, const regmatch_t
*, unsigned);
290 static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t
*, unsigned);
291 static const char *expand_variable( char *, unsigned, unsigned *, const char *,
292 const char *(*)(const char *, void *), void *);
293 static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
294 static char get_old_ide_name(unsigned, unsigned);
295 static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
297 /* busybox functions */
298 static int get_uid_gid(int flag
, const char *string
);
299 static void safe_memcpy(char * dest
, const char * src
, int len
);
300 static unsigned int scan_dev_name_common(const char *d
, unsigned int n
, int addendum
, const char *ptr
);
301 static unsigned int scan_dev_name(const char *d
, unsigned int n
, const char *ptr
);
303 /* Structs and vars */
304 static struct config_entry_struct
*first_config
= NULL
;
305 static struct config_entry_struct
*last_config
= NULL
;
306 static char *mount_point
= NULL
;
307 static volatile int caught_signal
= FALSE
;
308 static volatile int caught_sighup
= FALSE
;
309 static struct initial_symlink_struct
{
312 } initial_symlinks
[] = {
313 {"/proc/self/fd", "fd"},
320 static struct event_type
{
321 unsigned int type
; /* The DEVFSD_NOTIFY_* value */
322 const char *config_name
; /* The name used in the config file */
324 {DEVFSD_NOTIFY_REGISTERED
, "REGISTER"},
325 {DEVFSD_NOTIFY_UNREGISTERED
, "UNREGISTER"},
326 {DEVFSD_NOTIFY_ASYNC_OPEN
, "ASYNC_OPEN"},
327 {DEVFSD_NOTIFY_CLOSE
, "CLOSE"},
328 {DEVFSD_NOTIFY_LOOKUP
, "LOOKUP"},
329 {DEVFSD_NOTIFY_CHANGE
, "CHANGE"},
330 {DEVFSD_NOTIFY_CREATE
, "CREATE"},
331 {DEVFSD_NOTIFY_DELETE
, "DELETE"},
335 /* Busybox messages */
337 static const char bb_msg_proto_rev
[] ALIGN1
= "protocol revision";
338 static const char bb_msg_bad_config
[] ALIGN1
= "bad %s config file: %s";
339 static const char bb_msg_small_buffer
[] ALIGN1
= "buffer too small";
340 static const char bb_msg_variable_not_found
[] ALIGN1
= "variable: %s not found";
343 #if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
344 #define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
345 #define simple_info_logger(p, msg) bb_simple_info_msg(msg)
346 #define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
347 #define simple_msg_logger(p, msg) bb_simple_error_msg(msg)
348 #define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
349 #define simple_msg_logger_and_die(p, msg) bb_simple_error_msg_and_die(msg)
350 #define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
351 #define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
353 #define info_logger(p, fmt, args...)
354 #define simple_info_logger(p, msg)
355 #define msg_logger(p, fmt, args...)
356 #define simple_msg_logger(p, msg)
357 #define msg_logger_and_die(p, fmt, args...) exit_FAILURE()
358 #define simple_msg_logger_and_die(p, msg) exit_FAILURE()
359 #define error_logger(p, fmt, args...)
360 #define error_logger_and_die(p, fmt, args...) exit_FAILURE()
363 static void safe_memcpy(char *dest
, const char *src
, int len
)
365 memcpy(dest
, src
, len
);
369 static unsigned int scan_dev_name_common(const char *d
, unsigned int n
, int addendum
, const char *ptr
)
371 if (d
[n
- 4] == 'd' && d
[n
- 3] == 'i' && d
[n
- 2] == 's' && d
[n
- 1] == 'c')
373 if (d
[n
- 2] == 'c' && d
[n
- 1] == 'd')
375 if (ptr
[0] == 'p' && ptr
[1] == 'a' && ptr
[2] == 'r' && ptr
[3] == 't')
377 if (ptr
[n
- 2] == 'm' && ptr
[n
- 1] == 't')
382 static unsigned int scan_dev_name(const char *d
, unsigned int n
, const char *ptr
)
384 if (d
[0] == 's' && d
[1] == 'c' && d
[2] == 's' && d
[3] == 'i' && d
[4] == '/') {
385 if (d
[n
- 7] == 'g' && d
[n
- 6] == 'e' && d
[n
- 5] == 'n'
386 && d
[n
- 4] == 'e' && d
[n
- 3] == 'r' && d
[n
- 2] == 'i' && d
[n
- 1] == 'c'
389 return scan_dev_name_common(d
, n
, 0, ptr
);
391 if (d
[0] == 'i' && d
[1] == 'd' && d
[2] == 'e' && d
[3] == '/'
392 && d
[4] == 'h' && d
[5] == 'o' && d
[6] == 's' && d
[7] == 't'
394 return scan_dev_name_common(d
, n
, 4, ptr
);
395 if (d
[0] == 's' && d
[1] == 'b' && d
[2] == 'p' && d
[3] == '/')
397 if (d
[0] == 'v' && d
[1] == 'c' && d
[2] == 'c' && d
[3] == '/')
399 if (d
[0] == 'p' && d
[1] == 't' && d
[2] == 'y' && d
[3] == '/')
404 /* Public functions follow */
406 int devfsd_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
407 int devfsd_main(int argc
, char **argv
)
409 int print_version
= FALSE
;
410 int do_daemon
= TRUE
;
411 int no_polling
= FALSE
;
413 int fd
, proto_rev
, count
;
414 unsigned long event_mask
= 0;
415 struct sigaction new_action
;
416 struct initial_symlink_struct
*curr
;
421 for (count
= 2; count
< argc
; ++count
) {
422 if (argv
[count
][0] == '-') {
423 if (argv
[count
][1] == 'v' && !argv
[count
][2]) /* -v */
424 print_version
= TRUE
;
425 else if (ENABLE_DEVFSD_FG_NP
&& argv
[count
][1] == 'f'
426 && argv
[count
][2] == 'g' && !argv
[count
][3]) /* -fg */
428 else if (ENABLE_DEVFSD_FG_NP
&& argv
[count
][1] == 'n'
429 && argv
[count
][2] == 'p' && !argv
[count
][3]) /* -np */
436 mount_point
= bb_simplify_path(argv
[1]);
440 fd
= xopen(".devfsd", O_RDONLY
);
441 close_on_exec_on(fd
);
442 xioctl(fd
, DEVFSDIOC_GET_PROTO_REV
, &proto_rev
);
444 /*setup initial entries */
445 for (curr
= initial_symlinks
; curr
->dest
!= NULL
; ++curr
)
446 symlink(curr
->dest
, curr
->name
);
448 /* NB: The check for CONFIG_FILE is done in read_config_file() */
450 if (print_version
|| (DEVFSD_PROTOCOL_REVISION_DAEMON
!= proto_rev
)) {
451 printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
452 applet_name
, DEVFSD_VERSION
, bb_msg_proto_rev
,
453 DEVFSD_PROTOCOL_REVISION_DAEMON
, bb_msg_proto_rev
, proto_rev
);
454 if (DEVFSD_PROTOCOL_REVISION_DAEMON
!= proto_rev
)
455 bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev
);
456 exit_SUCCESS(); /* -v */
458 /* Tell kernel we are special(i.e. we get to see hidden entries) */
459 xioctl(fd
, DEVFSDIOC_SET_EVENT_MASK
, 0);
461 /* Set up SIGHUP and SIGUSR1 handlers */
462 sigemptyset(&new_action
.sa_mask
);
463 new_action
.sa_flags
= 0;
464 new_action
.sa_handler
= signal_handler
;
465 sigaction_set(SIGHUP
, &new_action
);
466 sigaction_set(SIGUSR1
, &new_action
);
468 printf("%s v%s started for %s\n", applet_name
, DEVFSD_VERSION
, mount_point
);
470 /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */
472 read_config_file((char*)CONFIG_FILE
, FALSE
, &event_mask
);
473 /* Do the scan before forking, so that boot scripts see the finished product */
474 dir_operation(SERVICE
, mount_point
, 0, NULL
);
476 if (ENABLE_DEVFSD_FG_NP
&& no_polling
)
479 if (ENABLE_DEVFSD_VERBOSE
|| ENABLE_DEBUG
)
480 logmode
= LOGMODE_BOTH
;
481 else if (do_daemon
== TRUE
)
482 logmode
= LOGMODE_SYSLOG
;
483 /* This is the default */
485 logmode = LOGMODE_STDIO; */
488 /* Release so that the child can grab it */
489 xioctl(fd
, DEVFSDIOC_RELEASE_EVENT_QUEUE
, 0);
490 bb_daemonize_or_rexec(0, argv
);
491 } else if (ENABLE_DEVFSD_FG_NP
) {
492 setpgid(0, 0); /* Become process group leader */
496 do_scan
= do_servicing(fd
, event_mask
);
499 read_config_file((char*)CONFIG_FILE
, FALSE
, &event_mask
);
501 dir_operation(SERVICE
, mount_point
, 0, NULL
);
503 if (ENABLE_FEATURE_CLEAN_UP
) free(mount_point
);
504 } /* End Function main */
507 /* Private functions follow */
509 static void read_config_file(char *path
, int optional
, unsigned long *event_mask
)
510 /* [SUMMARY] Read a configuration database.
511 <path> The path to read the database from. If this is a directory, all
512 entries in that directory will be read(except hidden entries).
513 <optional> If TRUE, the routine will silently ignore a missing config file.
514 <event_mask> The event mask is written here. This is not initialised.
520 char buf
[STRING_LENGTH
];
524 if (stat(path
, &statbuf
) == 0) {
525 /* Don't read 0 length files: ignored */
526 /*if (statbuf.st_size == 0)
528 if (S_ISDIR(statbuf
.st_mode
)) {
529 p
= bb_simplify_path(path
);
530 dir_operation(READ_CONFIG
, p
, 0, event_mask
);
534 fp
= fopen_for_read(path
);
536 while (fgets(buf
, STRING_LENGTH
, fp
) != NULL
) {
537 /* Skip whitespace */
539 line
= skip_whitespace(line
);
540 if (line
[0] == '\0' || line
[0] == '#')
542 process_config_line(line
, event_mask
);
546 goto read_config_file_err
;
549 read_config_file_err
:
550 if (optional
== 0 && errno
== ENOENT
)
551 error_logger_and_die(LOG_ERR
, "read config file: %s", path
);
553 } /* End Function read_config_file */
555 static void process_config_line(const char *line
, unsigned long *event_mask
)
556 /* [SUMMARY] Process a line from a configuration file.
557 <line> The configuration line.
558 <event_mask> The event mask is written here. This is not initialised.
563 struct config_entry_struct
*new;
564 char p
[MAX_ARGS
][STRING_LENGTH
];
565 char when
[STRING_LENGTH
], what
[STRING_LENGTH
];
566 char name
[STRING_LENGTH
];
567 const char *msg
= "";
571 /* !!!! Only Uppercase Keywords in devsfd.conf */
572 static const char options
[] ALIGN1
=
573 "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
574 "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
575 "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
576 "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
578 for (count
= 0; count
< MAX_ARGS
; ++count
)
580 num_args
= sscanf(line
, "%s %s %s %s %s %s %s %s %s %s",
582 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6]);
584 i
= index_in_strings(options
, when
);
594 goto process_config_line_err
;
596 /* "INCLUDE" & "OPTIONAL_INCLUDE" */
597 if (i
== 1 || i
== 2) {
598 st_expr_expand(name
, STRING_LENGTH
, name
, get_variable
, NULL
);
599 info_logger(LOG_INFO
, "%sinclude: %s", (toupper(when
[0]) == 'I') ? "": "optional_", name
);
600 read_config_file(name
, (toupper(when
[0]) == 'I') ? FALSE
: TRUE
, event_mask
);
605 dir_operation(RESTORE
, name
, strlen(name
),NULL
);
609 goto process_config_line_err
;
611 new = xzalloc(sizeof *new);
613 for (count
= 0; event_types
[count
].config_name
!= NULL
; ++count
) {
614 if (strcasecmp(when
, event_types
[count
].config_name
) != 0)
616 new->action
.when
= event_types
[count
].type
;
619 if (event_types
[count
].config_name
== NULL
) {
621 goto process_config_line_err
;
624 i
= index_in_strings(options
, what
);
627 case 4: /* "PERMISSIONS" */
628 new->action
.what
= AC_PERMISSIONS
;
629 /* Get user and group */
630 ptr
= strchr(p
[0], '.');
633 goto process_config_line_err
; /*"missing '.' in UID.GID"*/
637 new->u
.permissions
.uid
= get_uid_gid(UID
, p
[0]);
638 new->u
.permissions
.gid
= get_uid_gid(GID
, ptr
);
640 new->u
.permissions
.mode
= get_mode(p
[1]);
642 case 5: /* MODLOAD */
643 /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
644 the device name) to the module loading facility. In addition,
645 the /etc/modules.devfs configuration file is used.*/
646 if (ENABLE_DEVFSD_MODLOAD
)
647 new->action
.what
= AC_MODLOAD
;
649 case 6: /* EXECUTE */
650 new->action
.what
= AC_EXECUTE
;
653 for (count
= 0; count
< num_args
; ++count
)
654 new->u
.execute
.argv
[count
] = xstrdup(p
[count
]);
656 new->u
.execute
.argv
[num_args
] = NULL
;
659 new->action
.what
= AC_COPY
;
662 goto process_config_line_err
; /* missing path and function in line */
664 new->u
.copy
.source
= xstrdup(p
[0]);
665 new->u
.copy
.destination
= xstrdup(p
[1]);
669 case 9: /* MKOLDCOMPAT */
671 case 10: /* MKNEWCOMPAT */
673 case 11:/* RMOLDCOMPAT */
675 case 12: /* RMNEWCOMPAT */
681 new->action
.what
= i
- 2;
685 goto process_config_line_err
;
689 xregcomp(&new->preg
, name
, REG_EXTENDED
);
691 *event_mask
|= 1 << new->action
.when
;
693 if (first_config
== NULL
)
696 last_config
->next
= new;
700 process_config_line_err
:
701 msg_logger_and_die(LOG_ERR
, bb_msg_bad_config
, msg
, line
);
702 } /* End Function process_config_line */
704 static int do_servicing(int fd
, unsigned long event_mask
)
705 /* [SUMMARY] Service devfs changes until a signal is received.
706 <fd> The open control file.
707 <event_mask> The event mask.
708 [RETURNS] TRUE if SIGHUP was caught, else FALSE.
712 struct devfsd_notify_struct info
;
714 /* (void*) cast is only in order to match prototype */
715 xioctl(fd
, DEVFSDIOC_SET_EVENT_MASK
, (void*)event_mask
);
716 while (!caught_signal
) {
718 bytes
= read(fd
, (char *) &info
, sizeof info
);
720 break; /* Must test for this first */
722 continue; /* Yes, the order is important */
728 int c_sighup
= caught_sighup
;
730 caught_signal
= FALSE
;
731 caught_sighup
= FALSE
;
734 simple_msg_logger_and_die(LOG_ERR
, "read error on control file");
735 } /* End Function do_servicing */
737 static void service_name(const struct devfsd_notify_struct
*info
)
738 /* [SUMMARY] Service a single devfs change.
739 <info> The devfs change.
744 regmatch_t mbuf
[MAX_SUBEXPR
];
745 struct config_entry_struct
*entry
;
747 if (ENABLE_DEBUG
&& info
->overrun_count
> 0)
748 msg_logger(LOG_ERR
, "lost %u events", info
->overrun_count
);
750 /* Discard lookups on "/dev/log" and "/dev/initctl" */
751 if (info
->type
== DEVFSD_NOTIFY_LOOKUP
752 && ((info
->devname
[0] == 'l' && info
->devname
[1] == 'o'
753 && info
->devname
[2] == 'g' && !info
->devname
[3])
754 || (info
->devname
[0] == 'i' && info
->devname
[1] == 'n'
755 && info
->devname
[2] == 'i' && info
->devname
[3] == 't'
756 && info
->devname
[4] == 'c' && info
->devname
[5] == 't'
757 && info
->devname
[6] == 'l' && !info
->devname
[7]))
761 for (entry
= first_config
; entry
!= NULL
; entry
= entry
->next
) {
762 /* First check if action matches the type, then check if name matches */
763 if (info
->type
!= entry
->action
.when
764 || regexec(&entry
->preg
, info
->devname
, MAX_SUBEXPR
, mbuf
, 0) != 0)
766 for (n
= 0;(n
< MAX_SUBEXPR
) && (mbuf
[n
].rm_so
!= -1); ++n
)
769 switch (entry
->action
.what
) {
771 action_permissions(info
, entry
);
774 if (ENABLE_DEVFSD_MODLOAD
)
775 action_modload(info
, entry
);
778 action_execute(info
, entry
, mbuf
, n
);
781 action_copy(info
, entry
, mbuf
, n
);
790 action_compat(info
, entry
->action
.what
);
793 simple_msg_logger_and_die(LOG_ERR
, "Unknown action");
796 } /* End Function service_name */
798 static void action_permissions(const struct devfsd_notify_struct
*info
,
799 const struct config_entry_struct
*entry
)
800 /* [SUMMARY] Update permissions for a device entry.
801 <info> The devfs change.
802 <entry> The config file entry.
808 if (stat(info
->devname
, &statbuf
) != 0
809 || chmod(info
->devname
, (statbuf
.st_mode
& S_IFMT
) | (entry
->u
.permissions
.mode
& ~S_IFMT
)) != 0
810 || chown(info
->devname
, entry
->u
.permissions
.uid
, entry
->u
.permissions
.gid
) != 0
812 error_logger(LOG_ERR
, "Can't chmod or chown: %s", info
->devname
);
813 } /* End Function action_permissions */
815 static void action_modload(const struct devfsd_notify_struct
*info
,
816 const struct config_entry_struct
*entry UNUSED_PARAM
)
817 /* [SUMMARY] Load a module.
818 <info> The devfs change.
819 <entry> The config file entry.
825 argv
[0] = (char*)MODPROBE
;
826 argv
[1] = (char*)MODPROBE_SWITCH_1
; /* "-k" */
827 argv
[2] = (char*)MODPROBE_SWITCH_2
; /* "-C" */
828 argv
[3] = (char*)CONFIG_MODULES_DEVFS
;
829 argv
[4] = concat_path_file("/dev", info
->devname
); /* device */
832 spawn_and_wait(argv
);
834 } /* End Function action_modload */
836 static void action_execute(const struct devfsd_notify_struct
*info
,
837 const struct config_entry_struct
*entry
,
838 const regmatch_t
*regexpr
, unsigned int numexpr
)
839 /* [SUMMARY] Execute a programme.
840 <info> The devfs change.
841 <entry> The config file entry.
842 <regexpr> The number of subexpression(start, end) offsets within the
844 <numexpr> The number of elements within <<regexpr>>.
849 struct get_variable_info gv_info
;
850 char *argv
[MAX_ARGS
+ 1];
851 char largv
[MAX_ARGS
+ 1][STRING_LENGTH
];
854 gv_info
.devname
= info
->devname
;
855 snprintf(gv_info
.devpath
, sizeof(gv_info
.devpath
), "%s/%s", mount_point
, info
->devname
);
856 for (count
= 0; entry
->u
.execute
.argv
[count
] != NULL
; ++count
) {
857 expand_expression(largv
[count
], STRING_LENGTH
,
858 entry
->u
.execute
.argv
[count
],
859 get_variable
, &gv_info
,
860 gv_info
.devname
, regexpr
, numexpr
);
861 argv
[count
] = largv
[count
];
864 spawn_and_wait(argv
);
865 } /* End Function action_execute */
868 static void action_copy(const struct devfsd_notify_struct
*info
,
869 const struct config_entry_struct
*entry
,
870 const regmatch_t
*regexpr
, unsigned int numexpr
)
871 /* [SUMMARY] Copy permissions.
872 <info> The devfs change.
873 <entry> The config file entry.
874 <regexpr> This list of subexpression(start, end) offsets within the
876 <numexpr> The number of elements in <<regexpr>>.
881 struct get_variable_info gv_info
;
882 struct stat source_stat
, dest_stat
;
883 char source
[STRING_LENGTH
], destination
[STRING_LENGTH
];
886 dest_stat
.st_mode
= 0;
888 if ((info
->type
== DEVFSD_NOTIFY_CHANGE
) && S_ISLNK(info
->mode
))
891 gv_info
.devname
= info
->devname
;
893 snprintf(gv_info
.devpath
, sizeof(gv_info
.devpath
), "%s/%s", mount_point
, info
->devname
);
894 expand_expression(source
, STRING_LENGTH
, entry
->u
.copy
.source
,
895 get_variable
, &gv_info
, gv_info
.devname
,
898 expand_expression(destination
, STRING_LENGTH
, entry
->u
.copy
.destination
,
899 get_variable
, &gv_info
, gv_info
.devname
,
902 if (!make_dir_tree(destination
) || lstat(source
, &source_stat
) != 0)
904 lstat(destination
, &dest_stat
);
905 new_mode
= source_stat
.st_mode
& ~S_ISVTX
;
906 if (info
->type
== DEVFSD_NOTIFY_CREATE
)
908 else if ((info
->type
== DEVFSD_NOTIFY_CHANGE
) &&(dest_stat
.st_mode
& S_ISVTX
))
910 ret
= copy_inode(destination
, &dest_stat
, new_mode
, source
, &source_stat
);
911 if (ENABLE_DEBUG
&& ret
&& (errno
!= EEXIST
))
912 error_logger(LOG_ERR
, "copy_inode: %s to %s", source
, destination
);
913 } /* End Function action_copy */
915 static void action_compat(const struct devfsd_notify_struct
*info
, unsigned int action
)
916 /* [SUMMARY] Process a compatibility request.
917 <info> The devfs change.
918 <action> The action to take.
923 const char *compat_name
= NULL
;
924 const char *dest_name
= info
->devname
;
926 char compat_buf
[STRING_LENGTH
], dest_buf
[STRING_LENGTH
];
927 int mode
, host
, bus
, target
, lun
;
930 /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */
931 static const char *const fmt
[] ALIGN_PTR
= {
933 "sg/c%db%dt%du%d", /* scsi/generic */
934 "sd/c%db%dt%du%d", /* scsi/disc */
935 "sr/c%db%dt%du%d", /* scsi/cd */
936 "sd/c%db%dt%du%dp%d", /* scsi/part */
937 "st/c%db%dt%du%dm%d%c", /* scsi/mt */
938 "ide/hd/c%db%dt%du%d", /* ide/host/disc */
939 "ide/cd/c%db%dt%du%d", /* ide/host/cd */
940 "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */
941 "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */
945 /* First construct compatibility name */
949 compat_name
= get_old_name(info
->devname
, info
->namelen
, compat_buf
, info
->major
, info
->minor
);
953 ptr
= bb_basename(info
->devname
);
954 i
= scan_dev_name(info
->devname
, info
->namelen
, ptr
);
960 sscanf(info
->devname
+ ((i
< 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host
, &bus
, &target
, &lun
);
961 snprintf(dest_buf
, sizeof(dest_buf
), "../%s", info
->devname
+ (( i
> 5) ? 4 : 0));
962 dest_name
= dest_buf
;
963 compat_name
= compat_buf
;
966 /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
967 if (i
== 1 || i
== 2 || i
== 3 || i
== 6 || i
==7)
968 sprintf(compat_buf
, fmt
[i
], host
, bus
, target
, lun
);
970 /* 4 == scsi/part 8 == ide/host/part */
971 if (i
== 4 || i
== 8)
972 sprintf(compat_buf
, fmt
[i
], host
, bus
, target
, lun
, atoi(ptr
+ 4));
976 rewind_
= info
->devname
[info
->namelen
- 1];
980 if (ptr
[2] == 'l' /*108*/ || ptr
[2] == 'm'/*109*/)
981 mode
= ptr
[2] - 107; /* 1 or 2 */
984 sprintf(compat_buf
, fmt
[i
], host
, bus
, target
, lun
, mode
, rewind_
);
987 /* 9 == ide/host/mt */
989 snprintf(compat_buf
, sizeof(compat_buf
), fmt
[i
], host
, bus
, target
, lun
, ptr
+ 2);
991 } /* switch (action) */
993 if (compat_name
== NULL
)
996 /* Now decide what to do with it */
1000 mksymlink(dest_name
, compat_name
);
1002 case AC_RMOLDCOMPAT
:
1003 case AC_RMNEWCOMPAT
:
1004 ret
= unlink(compat_name
);
1005 if (ENABLE_DEBUG
&& ret
)
1006 error_logger(LOG_ERR
, "unlink: %s", compat_name
);
1009 } /* switch (action) */
1010 } /* End Function action_compat */
1012 static void restore(char *spath
, struct stat source_stat
, int rootlen
)
1015 struct stat dest_stat
;
1017 dest_stat
.st_mode
= 0;
1018 dpath
= concat_path_file(mount_point
, spath
+ rootlen
);
1019 lstat(dpath
, &dest_stat
);
1021 if (S_ISLNK(source_stat
.st_mode
) || (source_stat
.st_mode
& S_ISVTX
))
1022 copy_inode(dpath
, &dest_stat
, (source_stat
.st_mode
& ~S_ISVTX
), spath
, &source_stat
);
1024 if (S_ISDIR(source_stat
.st_mode
))
1025 dir_operation(RESTORE
, spath
, rootlen
, NULL
);
1029 static int copy_inode(const char *destpath
, const struct stat
*dest_stat
,
1031 const char *sourcepath
, const struct stat
*source_stat
)
1032 /* [SUMMARY] Copy an inode.
1033 <destpath> The destination path. An existing inode may be deleted.
1034 <dest_stat> The destination stat(2) information.
1035 <new_mode> The desired new mode for the destination.
1036 <sourcepath> The source path.
1037 <source_stat> The source stat(2) information.
1038 [RETURNS] TRUE on success, else FALSE.
1041 int source_len
, dest_len
;
1042 char source_link
[STRING_LENGTH
], dest_link
[STRING_LENGTH
];
1044 struct sockaddr_un un_addr
;
1045 char symlink_val
[STRING_LENGTH
];
1047 if ((source_stat
->st_mode
& S_IFMT
) ==(dest_stat
->st_mode
& S_IFMT
)) {
1049 if (S_ISLNK(source_stat
->st_mode
)) {
1050 source_len
= readlink(sourcepath
, source_link
, STRING_LENGTH
- 1);
1051 if ((source_len
< 0)
1052 || (dest_len
= readlink(destpath
, dest_link
, STRING_LENGTH
- 1)) < 0
1055 source_link
[source_len
] = '\0';
1056 dest_link
[dest_len
] = '\0';
1057 if ((source_len
!= dest_len
) || (strcmp(source_link
, dest_link
) != 0)) {
1059 symlink(source_link
, destpath
);
1062 } /* Else not a symlink */
1063 chmod(destpath
, new_mode
& ~S_IFMT
);
1064 chown(destpath
, source_stat
->st_uid
, source_stat
->st_gid
);
1067 /* Different types: unlink and create */
1069 switch (source_stat
->st_mode
& S_IFMT
) {
1071 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1074 un_addr
.sun_family
= AF_UNIX
;
1075 snprintf(un_addr
.sun_path
, sizeof(un_addr
.sun_path
), "%s", destpath
);
1076 val
= bind(fd
, (struct sockaddr
*) &un_addr
, (int) sizeof un_addr
);
1078 if (val
!= 0 || chmod(destpath
, new_mode
& ~S_IFMT
) != 0)
1082 val
= readlink(sourcepath
, symlink_val
, STRING_LENGTH
- 1);
1085 symlink_val
[val
] = '\0';
1086 if (symlink(symlink_val
, destpath
) == 0)
1090 fd
= open(destpath
, O_RDONLY
| O_CREAT
, new_mode
& ~S_IFMT
);
1094 if (chmod(destpath
, new_mode
& ~S_IFMT
) != 0)
1100 if (mknod(destpath
, new_mode
, source_stat
->st_rdev
) != 0)
1104 if (mkdir(destpath
, new_mode
& ~S_IFMT
) != 0)
1107 if (chown(destpath
, source_stat
->st_uid
, source_stat
->st_gid
) == 0)
1112 } /* End Function copy_inode */
1114 static void free_config(void)
1115 /* [SUMMARY] Free the configuration information.
1119 struct config_entry_struct
*c_entry
;
1122 for (c_entry
= first_config
; c_entry
!= NULL
; c_entry
= next
) {
1125 next
= c_entry
->next
;
1126 regfree(&c_entry
->preg
);
1127 if (c_entry
->action
.what
== AC_EXECUTE
) {
1128 for (count
= 0; count
< MAX_ARGS
; ++count
) {
1129 if (c_entry
->u
.execute
.argv
[count
] == NULL
)
1131 free(c_entry
->u
.execute
.argv
[count
]);
1136 first_config
= NULL
;
1138 } /* End Function free_config */
1140 static int get_uid_gid(int flag
, const char *string
)
1141 /* [SUMMARY] Convert a string to a UID or GID value.
1142 <flag> "UID" or "GID".
1143 <string> The string.
1144 [RETURNS] The UID or GID value.
1147 struct passwd
*pw_ent
;
1148 struct group
*grp_ent
;
1151 if (isdigit(string
[0]) || ((string
[0] == '-') && isdigit(string
[1])))
1152 return atoi(string
);
1154 if (flag
== UID
&& (pw_ent
= getpwnam(string
)) != NULL
)
1155 return pw_ent
->pw_uid
;
1157 if (ENABLE_DEVFSD_VERBOSE
)
1161 if ((grp_ent
= getgrnam(string
)) != NULL
)
1162 return grp_ent
->gr_gid
;
1163 if (ENABLE_DEVFSD_VERBOSE
)
1167 if (ENABLE_DEVFSD_VERBOSE
)
1168 msg_logger(LOG_ERR
, "unknown %s: %s, defaulting to %cid=0", msg
, string
, msg
[0]);
1170 }/* End Function get_uid_gid */
1172 static mode_t
get_mode(const char *string
)
1173 /* [SUMMARY] Convert a string to a mode value.
1174 <string> The string.
1175 [RETURNS] The mode value.
1181 if (isdigit(string
[0]))
1182 return strtoul(string
, NULL
, 8);
1183 if (strlen(string
) != 9)
1184 msg_logger_and_die(LOG_ERR
, "bad mode: %s", string
);
1189 if (string
[0] == 'r' || string
[0] == 'w' || string
[0] == 'x')
1195 } /* End Function get_mode */
1197 static void signal_handler(int sig
)
1199 caught_signal
= TRUE
;
1201 caught_sighup
= TRUE
;
1203 info_logger(LOG_INFO
, "Caught signal %d", sig
);
1204 } /* End Function signal_handler */
1206 static const char *get_variable(const char *variable
, void *info
)
1208 static char *hostname
;
1210 struct get_variable_info
*gv_info
= info
;
1211 const char *field_names
[] = {
1212 "hostname", "mntpt", "devpath", "devname", "uid", "gid", "mode",
1213 NULL
, mount_point
, gv_info
->devpath
, gv_info
->devname
, NULL
1218 hostname
= safe_gethostname();
1219 field_names
[7] = hostname
;
1221 /* index_in_str_array returns i>=0 */
1222 i
= index_in_str_array(field_names
, variable
);
1224 if (i
> 6 || i
< 0 || (i
> 1 && gv_info
== NULL
))
1226 if (i
>= 0 && i
<= 3)
1227 return field_names
[i
+ 7];
1230 return auto_string(xasprintf("%u", gv_info
->info
->uid
));
1232 return auto_string(xasprintf("%u", gv_info
->info
->gid
));
1234 return auto_string(xasprintf("%o", gv_info
->info
->mode
));
1235 } /* End Function get_variable */
1237 static void service(struct stat statbuf
, char *path
)
1239 struct devfsd_notify_struct info
;
1241 memset(&info
, 0, sizeof info
);
1242 info
.type
= DEVFSD_NOTIFY_REGISTERED
;
1243 info
.mode
= statbuf
.st_mode
;
1244 info
.major
= major(statbuf
.st_rdev
);
1245 info
.minor
= minor(statbuf
.st_rdev
);
1246 info
.uid
= statbuf
.st_uid
;
1247 info
.gid
= statbuf
.st_gid
;
1248 snprintf(info
.devname
, sizeof(info
.devname
), "%s", path
+ strlen(mount_point
) + 1);
1249 info
.namelen
= strlen(info
.devname
);
1250 service_name(&info
);
1251 if (S_ISDIR(statbuf
.st_mode
))
1252 dir_operation(SERVICE
, path
, 0, NULL
);
1255 static void dir_operation(int type
, const char * dir_name
, int var
, unsigned long *event_mask
)
1256 /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
1257 <flag> To choose which function to perform
1258 <dp> The directory pointer. This is closed upon completion.
1259 <dir_name> The name of the directory.
1260 <rootlen> string length parameter.
1264 struct stat statbuf
;
1269 dp
= warn_opendir(dir_name
);
1273 while ((de
= readdir(dp
)) != NULL
) {
1275 if (de
->d_name
&& DOT_OR_DOTDOT(de
->d_name
))
1277 path
= concat_path_file(dir_name
, de
->d_name
);
1278 if (lstat(path
, &statbuf
) == 0) {
1281 service(statbuf
, path
);
1284 restore(path
, statbuf
, var
);
1287 read_config_file(path
, var
, event_mask
);
1294 } /* End Function do_scan_and_service */
1296 static int mksymlink(const char *oldpath
, const char *newpath
)
1297 /* [SUMMARY] Create a symlink, creating intervening directories as required.
1298 <oldpath> The string contained in the symlink.
1299 <newpath> The name of the new symlink.
1300 [RETURNS] 0 on success, else -1.
1303 if (!make_dir_tree(newpath
))
1306 if (symlink(oldpath
, newpath
) != 0) {
1307 if (errno
!= EEXIST
)
1311 } /* End Function mksymlink */
1314 static int make_dir_tree(const char *path
)
1315 /* [SUMMARY] Creating intervening directories for a path as required.
1316 <path> The full pathname(including the leaf node).
1317 [RETURNS] TRUE on success, else FALSE.
1320 if (bb_make_directory(dirname((char *)path
), -1, FILEUTILS_RECUR
) == -1)
1323 } /* End Function make_dir_tree */
1325 static int expand_expression(char *output
, unsigned int outsize
,
1327 const char *(*get_variable_func
)(const char *variable
, void *info
),
1329 const char *devname
,
1330 const regmatch_t
*ex
, unsigned int numexp
)
1331 /* [SUMMARY] Expand environment variables and regular subexpressions in string.
1332 <output> The output expanded expression is written here.
1333 <length> The size of the output buffer.
1334 <input> The input expression. This may equal <<output>>.
1335 <get_variable> A function which will be used to get variable values. If
1336 this returns NULL, the environment is searched instead. If this is NULL,
1337 only the environment is searched.
1338 <info> An arbitrary pointer passed to <<get_variable>>.
1339 <devname> Device name; specifically, this is the string that contains all
1340 of the regular subexpressions.
1341 <ex> Array of start / end offsets into info->devname for each subexpression
1342 <numexp> Number of regular subexpressions found in <<devname>>.
1343 [RETURNS] TRUE on success, else FALSE.
1346 char temp
[STRING_LENGTH
];
1348 if (!st_expr_expand(temp
, STRING_LENGTH
, input
, get_variable_func
, info
))
1350 expand_regexp(output
, outsize
, temp
, devname
, ex
, numexp
);
1352 } /* End Function expand_expression */
1354 static void expand_regexp(char *output
, size_t outsize
, const char *input
,
1355 const char *devname
,
1356 const regmatch_t
*ex
, unsigned int numex
)
1357 /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
1358 <output> The output expanded expression is written here.
1359 <outsize> The size of the output buffer.
1360 <input> The input expression. This may NOT equal <<output>>, because
1361 supporting that would require yet another string-copy. However, it's not
1362 hard to write a simple wrapper function to add this functionality for those
1363 few cases that need it.
1364 <devname> Device name; specifically, this is the string that contains all
1365 of the regular subexpressions.
1366 <ex> An array of start and end offsets into <<devname>>, one for each
1368 <numex> Number of subexpressions in the offset-array <<ex>>.
1372 const char last_exp
= '0' - 1 + numex
;
1375 /* Guarantee NULL termination by writing an explicit '\0' character into
1376 the very last byte */
1378 output
[--outsize
] = '\0';
1379 /* Copy the input string into the output buffer, replacing '\\' with '\'
1380 and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
1381 codes are deleted */
1382 while ((c
!= '\0') && (outsize
!= 0)) {
1389 if ((c
>= '0') && (c
<= last_exp
)) {
1390 const regmatch_t
*subexp
= ex
+ (c
- '0');
1391 unsigned int sublen
= subexp
->rm_eo
- subexp
->rm_so
;
1393 /* Range checking */
1394 if (sublen
> outsize
)
1396 strncpy(output
, devname
+ subexp
->rm_so
, sublen
);
1407 } /* End Function expand_regexp */
1410 /* from compat_name.c */
1412 struct translate_struct
{
1413 const char *match
; /* The string to match to(up to length) */
1414 const char *format
; /* Format of output, "%s" takes data past match string,
1415 NULL is effectively "%s"(just more efficient) */
1418 static struct translate_struct translate_table
[] =
1421 {"printers/", "lp%s"},
1423 {"parports/", "parport%s"},
1426 {"loop/", "loop%s"},
1427 {"floppy/", "fd%s"},
1429 {"md/", "md%s"}, /* Meta-devices */
1433 {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
1435 {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
1436 {"tts/E", "ttyE%s"}, /* Stallion serial driver */
1437 {"cua/E", "cue%s"}, /* Stallion serial driver callout */
1438 {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
1439 {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
1440 {"ip2/", "ip2%s"}, /* Computone serial driver control */
1441 {"tts/F", "ttyF%s"}, /* Computone serial driver */
1442 {"cua/F", "cuf%s"}, /* Computone serial driver callout */
1443 {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
1444 {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
1445 {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
1446 {"cua/", "cua%s"}, /* Generic serial: must be after others */
1447 {"input/js", "js%s"}, /* Joystick driver */
1451 const char *get_old_name(const char *devname
, unsigned int namelen
,
1452 char *buffer
, unsigned int major
, unsigned int minor
)
1453 /* [SUMMARY] Translate a kernel-supplied name into an old name.
1454 <devname> The device name provided by the kernel.
1455 <namelen> The length of the name.
1456 <buffer> A buffer that may be used. This should be at least 128 bytes long.
1457 <major> The major number for the device.
1458 <minor> The minor number for the device.
1459 [RETURNS] A pointer to the old name if known, else NULL.
1462 const char *compat_name
= NULL
;
1464 struct translate_struct
*trans
;
1470 /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
1471 static const char *const fmt
[] ALIGN_PTR
= {
1473 "sg%u", /* scsi/generic */
1474 NULL
, /* scsi/disc */
1475 "sr%u", /* scsi/cd */
1476 NULL
, /* scsi/part */
1477 "nst%u%c", /* scsi/mt */
1478 "hd%c" , /* ide/host/disc */
1479 "hd%c" , /* ide/host/cd */
1480 "hd%c%s", /* ide/host/part */
1481 "%sht%d", /* ide/host/mt */
1482 "sbpcd%u", /* sbp/ */
1484 "%cty%c%c", /* pty/ */
1488 for (trans
= translate_table
; trans
->match
!= NULL
; ++trans
) {
1489 char *after_match
= is_prefixed_with(devname
, trans
->match
);
1491 if (trans
->format
== NULL
)
1493 sprintf(buffer
, trans
->format
, after_match
);
1498 ptr
= bb_basename(devname
);
1499 i
= scan_dev_name(devname
, namelen
, ptr
);
1501 if (i
> 0 && i
< 13)
1502 compat_name
= buffer
;
1506 /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
1507 if (i
== 1 || i
== 3 || i
== 10)
1508 sprintf(buffer
, fmt
[i
], minor
);
1510 /* 2 ==scsi/disc, 4 == scsi/part */
1511 if (i
== 2 || i
== 4)
1512 compat_name
= write_old_sd_name(buffer
, major
, minor
, ((i
== 2) ? "" : (ptr
+ 4)));
1519 sprintf(buffer
, fmt
[i
], minor
& 0x1f, mode
);
1520 if (devname
[namelen
- 1] != 'n')
1523 /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
1524 if (i
== 6 || i
== 7 || i
== 8)
1525 /* last arg should be ignored for i == 6 or i== 7 */
1526 sprintf(buffer
, fmt
[i
] , get_old_ide_name(major
, minor
), ptr
+ 4);
1528 /* 9 == ide/host/mt */
1530 sprintf(buffer
, fmt
[i
], ptr
+ 2, minor
& 0x7f);
1534 sprintf(buffer
, fmt
[i
], devname
+ 4);
1535 if (buffer
[3] == '0')
1540 pty1
= "pqrstuvwxyzabcde";
1541 pty2
= "0123456789abcdef";
1542 indexx
= atoi(devname
+ 5);
1543 sprintf(buffer
, fmt
[i
], (devname
[4] == 'm') ? 'p' : 't', pty1
[indexx
>> 4], pty2
[indexx
& 0x0f]);
1546 } /* End Function get_old_name */
1548 static char get_old_ide_name(unsigned int major
, unsigned int minor
)
1549 /* [SUMMARY] Get the old IDE name for a device.
1550 <major> The major number for the device.
1551 <minor> The minor number for the device.
1552 [RETURNS] The drive letter.
1555 char letter
= 'y'; /* 121 */
1556 char c
= 'a'; /* 97 */
1559 /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
1561 if (i
== IDE0_MAJOR
|| i
== IDE1_MAJOR
|| i
== IDE2_MAJOR
1562 || i
== IDE3_MAJOR
|| i
== IDE4_MAJOR
|| i
== IDE5_MAJOR
1563 || i
== IDE6_MAJOR
|| i
== IDE7_MAJOR
|| i
== IDE8_MAJOR
1566 if ((unsigned int)i
== major
) {
1573 } while (i
<= IDE9_MAJOR
);
1578 } /* End Function get_old_ide_name */
1580 static char *write_old_sd_name(char *buffer
,
1581 unsigned int major
, unsigned int minor
,
1583 /* [SUMMARY] Write the old SCSI disc name to a buffer.
1584 <buffer> The buffer to write to.
1585 <major> The major number for the device.
1586 <minor> The minor number for the device.
1587 <part> The partition string. Must be "" for a whole-disc entry.
1588 [RETURNS] A pointer to the buffer on success, else NULL.
1591 unsigned int disc_index
;
1594 sprintf(buffer
, "sd%c%s", 'a' + (minor
>> 4), part
);
1597 if ((major
> 64) && (major
< 72)) {
1598 disc_index
= ((major
- 64) << 4) + (minor
>> 4);
1599 if (disc_index
< 26)
1600 sprintf(buffer
, "sd%c%s", 'a' + disc_index
, part
);
1602 sprintf(buffer
, "sd%c%c%s", 'a' + (disc_index
/ 26) - 1, 'a' + disc_index
% 26, part
);
1606 } /* End Function write_old_sd_name */
1611 /*EXPERIMENTAL_FUNCTION*/
1613 int st_expr_expand(char *output
, unsigned int length
, const char *input
,
1614 const char *(*get_variable_func
)(const char *variable
,
1617 /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
1618 <output> The output expanded expression is written here.
1619 <length> The size of the output buffer.
1620 <input> The input expression. This may equal <<output>>.
1621 <get_variable> A function which will be used to get variable values. If
1622 this returns NULL, the environment is searched instead. If this is NULL,
1623 only the environment is searched.
1624 <info> An arbitrary pointer passed to <<get_variable>>.
1625 [RETURNS] TRUE on success, else FALSE.
1630 unsigned int out_pos
= 0;
1633 struct passwd
*pwent
;
1634 char buffer
[BUFFER_SIZE
], tmp
[STRING_LENGTH
];
1636 if (length
> BUFFER_SIZE
)
1637 length
= BUFFER_SIZE
;
1638 for (; TRUE
; ++input
) {
1639 switch (ch
= *input
) {
1641 /* Variable expansion */
1642 input
= expand_variable(buffer
, length
, &out_pos
, ++input
, get_variable_func
, info
);
1647 /* Home directory expansion */
1649 if (isspace(ch
) ||(ch
== '/') ||(ch
== '\0')) {
1650 /* User's own home directory: leave separator for next time */
1651 env
= getenv("HOME");
1653 info_logger(LOG_INFO
, bb_msg_variable_not_found
, "HOME");
1657 if (len
+ out_pos
>= length
)
1658 goto st_expr_expand_out
;
1659 memcpy(buffer
+ out_pos
, env
, len
+ 1);
1663 /* Someone else's home directory */
1664 for (ptr
= ++input
; !isspace(ch
) && (ch
!= '/') && (ch
!= '\0'); ch
= *++ptr
)
1667 if (len
>= sizeof tmp
)
1668 goto st_expr_expand_out
;
1669 safe_memcpy(tmp
, input
, len
);
1671 pwent
= getpwnam(tmp
);
1672 if (pwent
== NULL
) {
1673 info_logger(LOG_INFO
, "no pwent for: %s", tmp
);
1676 len
= strlen(pwent
->pw_dir
);
1677 if (len
+ out_pos
>= length
)
1678 goto st_expr_expand_out
;
1679 memcpy(buffer
+ out_pos
, pwent
->pw_dir
, len
+ 1);
1685 if (out_pos
>= length
)
1686 goto st_expr_expand_out
;
1687 buffer
[out_pos
++] = ch
;
1689 memcpy(output
, buffer
, out_pos
);
1698 simple_info_logger(LOG_INFO
, bb_msg_small_buffer
);
1700 } /* End Function st_expr_expand */
1703 /* Private functions follow */
1705 static const char *expand_variable(char *buffer
, unsigned int length
,
1706 unsigned int *out_pos
, const char *input
,
1707 const char *(*func
)(const char *variable
,
1710 /* [SUMMARY] Expand a variable.
1711 <buffer> The buffer to write to.
1712 <length> The length of the output buffer.
1713 <out_pos> The current output position. This is updated.
1714 <input> A pointer to the input character pointer.
1715 <func> A function which will be used to get variable values. If this
1716 returns NULL, the environment is searched instead. If this is NULL, only
1717 the environment is searched.
1718 <info> An arbitrary pointer passed to <<func>>.
1719 <errfp> Diagnostic messages are written here.
1720 [RETURNS] A pointer to the end of this subexpression on success, else NULL.
1725 unsigned int open_braces
;
1726 const char *env
, *ptr
;
1727 char tmp
[STRING_LENGTH
];
1731 /* Special case for "$$": PID */
1732 sprintf(tmp
, "%d", (int) getpid());
1734 if (len
+ *out_pos
>= length
)
1735 goto expand_variable_out
;
1737 memcpy(buffer
+ *out_pos
, tmp
, len
+ 1);
1741 /* Ordinary variable expansion, possibly in braces */
1743 /* Simple variable expansion */
1744 for (ptr
= input
; isalnum(ch
) || (ch
== '_') || (ch
== ':'); ch
= *++ptr
)
1747 if ((size_t)len
>= sizeof tmp
)
1748 goto expand_variable_out
;
1750 safe_memcpy(tmp
, input
, len
);
1752 env
= get_variable_v2(tmp
, func
, info
);
1754 info_logger(LOG_INFO
, bb_msg_variable_not_found
, tmp
);
1758 if (len
+ *out_pos
>= length
)
1759 goto expand_variable_out
;
1761 memcpy(buffer
+ *out_pos
, env
, len
+ 1);
1765 /* Variable in braces: check for ':' tricks */
1767 for (ptr
= input
; isalnum(ch
) || (ch
== '_'); ch
= *++ptr
)
1770 /* Must be simple variable expansion with "${var}" */
1772 if ((size_t)len
>= sizeof tmp
)
1773 goto expand_variable_out
;
1775 safe_memcpy(tmp
, input
, len
);
1776 ptr
= expand_variable(buffer
, length
, out_pos
, tmp
, func
, info
);
1781 if (ch
!= ':' || ptr
[1] != '-') {
1782 simple_info_logger(LOG_INFO
, "illegal char in var name");
1785 /* It's that handy "${var:-word}" expression. Check if var is defined */
1787 if ((size_t)len
>= sizeof tmp
)
1788 goto expand_variable_out
;
1790 safe_memcpy(tmp
, input
, len
);
1791 /* Move input pointer to ':' */
1793 /* First skip to closing brace, taking note of nested expressions */
1796 for (open_braces
= 1; open_braces
> 0; ch
= *++ptr
) {
1805 info_logger(LOG_INFO
, "\"}\" not found in: %s", input
);
1812 /* At this point ptr should point to closing brace of "${var:-word}" */
1813 env
= get_variable_v2(tmp
, func
, info
);
1815 /* Found environment variable, so skip the input to the closing brace
1816 and return the variable */
1819 if (len
+ *out_pos
>= length
)
1820 goto expand_variable_out
;
1822 memcpy(buffer
+ *out_pos
, env
, len
+ 1);
1826 /* Environment variable was not found, so process word. Advance input
1827 pointer to start of word in "${var:-word}" */
1830 if ((size_t)len
>= sizeof tmp
)
1831 goto expand_variable_out
;
1833 safe_memcpy(tmp
, input
, len
);
1835 if (!st_expr_expand(tmp
, STRING_LENGTH
, tmp
, func
, info
))
1838 if (len
+ *out_pos
>= length
)
1839 goto expand_variable_out
;
1841 memcpy(buffer
+ *out_pos
, tmp
, len
+ 1);
1844 expand_variable_out
:
1845 simple_info_logger(LOG_INFO
, bb_msg_small_buffer
);
1847 } /* End Function expand_variable */
1850 static const char *get_variable_v2(const char *variable
,
1851 const char *(*func
)(const char *variable
, void *info
),
1853 /* [SUMMARY] Get a variable from the environment or .
1854 <variable> The variable name.
1855 <func> A function which will be used to get the variable. If this returns
1856 NULL, the environment is searched instead. If this is NULL, only the
1857 environment is searched.
1858 [RETURNS] The value of the variable on success, else NULL.
1864 value
= (*func
)(variable
, info
);
1868 return getenv(variable
);
1869 } /* End Function get_variable */