Contents of /tags/mkinitrd-6_3_5/busybox/miscutils/devfsd.c
Parent Directory | Revision Log
Revision 1223 -
(show annotations)
(download)
Sat Jan 29 14:06:54 2011 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 54966 byte(s)
Sat Jan 29 14:06:54 2011 UTC (13 years, 8 months ago) by niro
File MIME type: text/plain
File size: 54966 byte(s)
tagged 'mkinitrd-6_3_5'
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
4 | */ |
5 | |
6 | /* |
7 | devfsd implementation for busybox |
8 | |
9 | Copyright (C) 2003 by Tito Ragusa <farmatito@tiscali.it> |
10 | |
11 | Busybox version is based on some previous work and ideas |
12 | Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it> |
13 | |
14 | devfsd.c |
15 | |
16 | Main file for devfsd (devfs daemon for Linux). |
17 | |
18 | Copyright (C) 1998-2002 Richard Gooch |
19 | |
20 | devfsd.h |
21 | |
22 | Header file for devfsd (devfs daemon for Linux). |
23 | |
24 | Copyright (C) 1998-2000 Richard Gooch |
25 | |
26 | compat_name.c |
27 | |
28 | Compatibility name file for devfsd (build compatibility names). |
29 | |
30 | Copyright (C) 1998-2002 Richard Gooch |
31 | |
32 | expression.c |
33 | |
34 | This code provides Borne Shell-like expression expansion. |
35 | |
36 | Copyright (C) 1997-1999 Richard Gooch |
37 | |
38 | This program is free software; you can redistribute it and/or modify |
39 | it under the terms of the GNU General Public License as published by |
40 | the Free Software Foundation; either version 2 of the License, or |
41 | (at your option) any later version. |
42 | |
43 | This program is distributed in the hope that it will be useful, |
44 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
45 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
46 | GNU General Public License for more details. |
47 | |
48 | You should have received a copy of the GNU General Public License |
49 | along with this program; if not, write to the Free Software |
50 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
51 | |
52 | Richard Gooch may be reached by email at rgooch@atnf.csiro.au |
53 | The postal address is: |
54 | Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia. |
55 | */ |
56 | #include "libbb.h" |
57 | #include "xregex.h" |
58 | #include <syslog.h> |
59 | |
60 | #include <sys/un.h> |
61 | #include <sys/sysmacros.h> |
62 | |
63 | /* Various defines taken from linux/major.h */ |
64 | #define IDE0_MAJOR 3 |
65 | #define IDE1_MAJOR 22 |
66 | #define IDE2_MAJOR 33 |
67 | #define IDE3_MAJOR 34 |
68 | #define IDE4_MAJOR 56 |
69 | #define IDE5_MAJOR 57 |
70 | #define IDE6_MAJOR 88 |
71 | #define IDE7_MAJOR 89 |
72 | #define IDE8_MAJOR 90 |
73 | #define IDE9_MAJOR 91 |
74 | |
75 | |
76 | /* Various defines taken from linux/devfs_fs.h */ |
77 | #define DEVFSD_PROTOCOL_REVISION_KERNEL 5 |
78 | #define DEVFSD_IOCTL_BASE 'd' |
79 | /* These are the various ioctls */ |
80 | #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int) |
81 | #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int) |
82 | #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int) |
83 | #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int) |
84 | #define DEVFSD_NOTIFY_REGISTERED 0 |
85 | #define DEVFSD_NOTIFY_UNREGISTERED 1 |
86 | #define DEVFSD_NOTIFY_ASYNC_OPEN 2 |
87 | #define DEVFSD_NOTIFY_CLOSE 3 |
88 | #define DEVFSD_NOTIFY_LOOKUP 4 |
89 | #define DEVFSD_NOTIFY_CHANGE 5 |
90 | #define DEVFSD_NOTIFY_CREATE 6 |
91 | #define DEVFSD_NOTIFY_DELETE 7 |
92 | #define DEVFS_PATHLEN 1024 |
93 | /* Never change this otherwise the binary interface will change */ |
94 | |
95 | struct devfsd_notify_struct { |
96 | /* Use native C types to ensure same types in kernel and user space */ |
97 | unsigned int type; /* DEVFSD_NOTIFY_* value */ |
98 | unsigned int mode; /* Mode of the inode or device entry */ |
99 | unsigned int major; /* Major number of device entry */ |
100 | unsigned int minor; /* Minor number of device entry */ |
101 | unsigned int uid; /* Uid of process, inode or device entry */ |
102 | unsigned int gid; /* Gid of process, inode or device entry */ |
103 | unsigned int overrun_count; /* Number of lost events */ |
104 | unsigned int namelen; /* Number of characters not including '\0' */ |
105 | /* The device name MUST come last */ |
106 | char devname[DEVFS_PATHLEN]; /* This will be '\0' terminated */ |
107 | }; |
108 | |
109 | #define BUFFER_SIZE 16384 |
110 | #define DEVFSD_VERSION "1.3.25" |
111 | #define CONFIG_FILE "/etc/devfsd.conf" |
112 | #define MODPROBE "/sbin/modprobe" |
113 | #define MODPROBE_SWITCH_1 "-k" |
114 | #define MODPROBE_SWITCH_2 "-C" |
115 | #define CONFIG_MODULES_DEVFS "/etc/modules.devfs" |
116 | #define MAX_ARGS (6 + 1) |
117 | #define MAX_SUBEXPR 10 |
118 | #define STRING_LENGTH 255 |
119 | |
120 | /* for get_uid_gid() */ |
121 | #define UID 0 |
122 | #define GID 1 |
123 | |
124 | /* fork_and_execute() */ |
125 | # define DIE 1 |
126 | # define NO_DIE 0 |
127 | |
128 | /* for dir_operation() */ |
129 | #define RESTORE 0 |
130 | #define SERVICE 1 |
131 | #define READ_CONFIG 2 |
132 | |
133 | /* Update only after changing code to reflect new protocol */ |
134 | #define DEVFSD_PROTOCOL_REVISION_DAEMON 5 |
135 | |
136 | /* Compile-time check */ |
137 | #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON |
138 | #error protocol version mismatch. Update your kernel headers |
139 | #endif |
140 | |
141 | #define AC_PERMISSIONS 0 |
142 | #define AC_MODLOAD 1 |
143 | #define AC_EXECUTE 2 |
144 | #define AC_MFUNCTION 3 /* not supported by busybox */ |
145 | #define AC_CFUNCTION 4 /* not supported by busybox */ |
146 | #define AC_COPY 5 |
147 | #define AC_IGNORE 6 |
148 | #define AC_MKOLDCOMPAT 7 |
149 | #define AC_MKNEWCOMPAT 8 |
150 | #define AC_RMOLDCOMPAT 9 |
151 | #define AC_RMNEWCOMPAT 10 |
152 | #define AC_RESTORE 11 |
153 | |
154 | struct permissions_type { |
155 | mode_t mode; |
156 | uid_t uid; |
157 | gid_t gid; |
158 | }; |
159 | |
160 | struct execute_type { |
161 | char *argv[MAX_ARGS + 1]; /* argv[0] must always be the programme */ |
162 | }; |
163 | |
164 | struct copy_type { |
165 | const char *source; |
166 | const char *destination; |
167 | }; |
168 | |
169 | struct action_type { |
170 | unsigned int what; |
171 | unsigned int when; |
172 | }; |
173 | |
174 | struct config_entry_struct { |
175 | struct action_type action; |
176 | regex_t preg; |
177 | union |
178 | { |
179 | struct permissions_type permissions; |
180 | struct execute_type execute; |
181 | struct copy_type copy; |
182 | } |
183 | u; |
184 | struct config_entry_struct *next; |
185 | }; |
186 | |
187 | struct get_variable_info { |
188 | const struct devfsd_notify_struct *info; |
189 | const char *devname; |
190 | char devpath[STRING_LENGTH]; |
191 | }; |
192 | |
193 | static void dir_operation(int , const char * , int, unsigned long*); |
194 | static void service(struct stat statbuf, char *path); |
195 | static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *); |
196 | static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned); |
197 | static int mksymlink(const char *oldpath, const char *newpath); |
198 | static void read_config_file(char *path, int optional, unsigned long *event_mask); |
199 | static void process_config_line(const char *, unsigned long *); |
200 | static int do_servicing(int, unsigned long); |
201 | static void service_name(const struct devfsd_notify_struct *); |
202 | static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *); |
203 | static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *, |
204 | const regmatch_t *, unsigned); |
205 | static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry); |
206 | static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *, |
207 | const regmatch_t *, unsigned); |
208 | static void action_compat(const struct devfsd_notify_struct *, unsigned); |
209 | static void free_config(void); |
210 | static void restore(char *spath, struct stat source_stat, int rootlen); |
211 | static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *); |
212 | static mode_t get_mode(const char *); |
213 | static void signal_handler(int); |
214 | static const char *get_variable(const char *, void *); |
215 | static int make_dir_tree(const char *); |
216 | static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *, |
217 | const char *, const regmatch_t *, unsigned); |
218 | static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned); |
219 | static const char *expand_variable( char *, unsigned, unsigned *, const char *, |
220 | const char *(*)(const char *, void *), void *); |
221 | static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *); |
222 | static char get_old_ide_name(unsigned , unsigned); |
223 | static char *write_old_sd_name(char *, unsigned, unsigned, const char *); |
224 | |
225 | /* busybox functions */ |
226 | static int get_uid_gid(int flag, const char *string); |
227 | static void safe_memcpy(char * dest, const char * src, int len); |
228 | static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr); |
229 | static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr); |
230 | |
231 | /* Structs and vars */ |
232 | static struct config_entry_struct *first_config = NULL; |
233 | static struct config_entry_struct *last_config = NULL; |
234 | static char *mount_point = NULL; |
235 | static volatile int caught_signal = FALSE; |
236 | static volatile int caught_sighup = FALSE; |
237 | static struct initial_symlink_struct { |
238 | const char *dest; |
239 | const char *name; |
240 | } initial_symlinks[] = { |
241 | {"/proc/self/fd", "fd"}, |
242 | {"fd/0", "stdin"}, |
243 | {"fd/1", "stdout"}, |
244 | {"fd/2", "stderr"}, |
245 | {NULL, NULL}, |
246 | }; |
247 | |
248 | static struct event_type { |
249 | unsigned int type; /* The DEVFSD_NOTIFY_* value */ |
250 | const char *config_name; /* The name used in the config file */ |
251 | } event_types[] = { |
252 | {DEVFSD_NOTIFY_REGISTERED, "REGISTER"}, |
253 | {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"}, |
254 | {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"}, |
255 | {DEVFSD_NOTIFY_CLOSE, "CLOSE"}, |
256 | {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"}, |
257 | {DEVFSD_NOTIFY_CHANGE, "CHANGE"}, |
258 | {DEVFSD_NOTIFY_CREATE, "CREATE"}, |
259 | {DEVFSD_NOTIFY_DELETE, "DELETE"}, |
260 | {0xffffffff, NULL} |
261 | }; |
262 | |
263 | /* Busybox messages */ |
264 | |
265 | static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision"; |
266 | static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s"; |
267 | static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small"; |
268 | static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found"; |
269 | |
270 | /* Busybox stuff */ |
271 | #if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG |
272 | #define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args) |
273 | #define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args) |
274 | #define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args) |
275 | #define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args) |
276 | #define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args) |
277 | #else |
278 | #define info_logger(p, fmt, args...) |
279 | #define msg_logger(p, fmt, args...) |
280 | #define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) |
281 | #define error_logger(p, fmt, args...) |
282 | #define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE) |
283 | #endif |
284 | |
285 | static void safe_memcpy(char *dest, const char *src, int len) |
286 | { |
287 | memcpy(dest , src, len); |
288 | dest[len] = '\0'; |
289 | } |
290 | |
291 | static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr) |
292 | { |
293 | if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c') |
294 | return 2 + addendum; |
295 | if (d[n - 2] == 'c' && d[n - 1] == 'd') |
296 | return 3 + addendum; |
297 | if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't') |
298 | return 4 + addendum; |
299 | if (ptr[n - 2] == 'm' && ptr[n - 1] == 't') |
300 | return 5 + addendum; |
301 | return 0; |
302 | } |
303 | |
304 | static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr) |
305 | { |
306 | if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') { |
307 | if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n' |
308 | && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c' |
309 | ) |
310 | return 1; |
311 | return scan_dev_name_common(d, n, 0, ptr); |
312 | } |
313 | if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/' |
314 | && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't' |
315 | ) |
316 | return scan_dev_name_common(d, n, 4, ptr); |
317 | if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/') |
318 | return 10; |
319 | if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/') |
320 | return 11; |
321 | if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/') |
322 | return 12; |
323 | return 0; |
324 | } |
325 | |
326 | /* Public functions follow */ |
327 | |
328 | int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
329 | int devfsd_main(int argc, char **argv) |
330 | { |
331 | int print_version = FALSE; |
332 | int do_daemon = TRUE; |
333 | int no_polling = FALSE; |
334 | int do_scan; |
335 | int fd, proto_rev, count; |
336 | unsigned long event_mask = 0; |
337 | struct sigaction new_action; |
338 | struct initial_symlink_struct *curr; |
339 | |
340 | if (argc < 2) |
341 | bb_show_usage(); |
342 | |
343 | for (count = 2; count < argc; ++count) { |
344 | if (argv[count][0] == '-') { |
345 | if (argv[count][1] == 'v' && !argv[count][2]) /* -v */ |
346 | print_version = TRUE; |
347 | else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f' |
348 | && argv[count][2] == 'g' && !argv[count][3]) /* -fg */ |
349 | do_daemon = FALSE; |
350 | else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n' |
351 | && argv[count][2] == 'p' && !argv[count][3]) /* -np */ |
352 | no_polling = TRUE; |
353 | else |
354 | bb_show_usage(); |
355 | } |
356 | } |
357 | |
358 | mount_point = bb_simplify_path(argv[1]); |
359 | |
360 | xchdir(mount_point); |
361 | |
362 | fd = xopen(".devfsd", O_RDONLY); |
363 | close_on_exec_on(fd); |
364 | xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev); |
365 | |
366 | /*setup initial entries */ |
367 | for (curr = initial_symlinks; curr->dest != NULL; ++curr) |
368 | symlink(curr->dest, curr->name); |
369 | |
370 | /* NB: The check for CONFIG_FILE is done in read_config_file() */ |
371 | |
372 | if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) { |
373 | printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n", |
374 | applet_name, DEVFSD_VERSION, bb_msg_proto_rev, |
375 | DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev); |
376 | if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) |
377 | bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev); |
378 | exit(EXIT_SUCCESS); /* -v */ |
379 | } |
380 | /* Tell kernel we are special(i.e. we get to see hidden entries) */ |
381 | xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0); |
382 | |
383 | /* Set up SIGHUP and SIGUSR1 handlers */ |
384 | sigemptyset(&new_action.sa_mask); |
385 | new_action.sa_flags = 0; |
386 | new_action.sa_handler = signal_handler; |
387 | sigaction_set(SIGHUP, &new_action); |
388 | sigaction_set(SIGUSR1, &new_action); |
389 | |
390 | printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point); |
391 | |
392 | /* Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions */ |
393 | umask(0); |
394 | read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); |
395 | /* Do the scan before forking, so that boot scripts see the finished product */ |
396 | dir_operation(SERVICE, mount_point, 0, NULL); |
397 | |
398 | if (ENABLE_DEVFSD_FG_NP && no_polling) |
399 | exit(EXIT_SUCCESS); |
400 | |
401 | if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG) |
402 | logmode = LOGMODE_BOTH; |
403 | else if (do_daemon == TRUE) |
404 | logmode = LOGMODE_SYSLOG; |
405 | /* This is the default */ |
406 | /*else |
407 | logmode = LOGMODE_STDIO; */ |
408 | |
409 | if (do_daemon) { |
410 | /* Release so that the child can grab it */ |
411 | xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0); |
412 | bb_daemonize_or_rexec(0, argv); |
413 | } else if (ENABLE_DEVFSD_FG_NP) { |
414 | setpgid(0, 0); /* Become process group leader */ |
415 | } |
416 | |
417 | while (TRUE) { |
418 | do_scan = do_servicing(fd, event_mask); |
419 | |
420 | free_config(); |
421 | read_config_file((char*)CONFIG_FILE, FALSE, &event_mask); |
422 | if (do_scan) |
423 | dir_operation(SERVICE, mount_point, 0, NULL); |
424 | } |
425 | if (ENABLE_FEATURE_CLEAN_UP) free(mount_point); |
426 | } /* End Function main */ |
427 | |
428 | |
429 | /* Private functions follow */ |
430 | |
431 | static void read_config_file(char *path, int optional, unsigned long *event_mask) |
432 | /* [SUMMARY] Read a configuration database. |
433 | <path> The path to read the database from. If this is a directory, all |
434 | entries in that directory will be read(except hidden entries). |
435 | <optional> If TRUE, the routine will silently ignore a missing config file. |
436 | <event_mask> The event mask is written here. This is not initialised. |
437 | [RETURNS] Nothing. |
438 | */ |
439 | { |
440 | struct stat statbuf; |
441 | FILE *fp; |
442 | char buf[STRING_LENGTH]; |
443 | char *line = NULL; |
444 | char *p; |
445 | |
446 | if (stat(path, &statbuf) == 0) { |
447 | /* Don't read 0 length files: ignored */ |
448 | /*if (statbuf.st_size == 0) |
449 | return;*/ |
450 | if (S_ISDIR(statbuf.st_mode)) { |
451 | p = bb_simplify_path(path); |
452 | dir_operation(READ_CONFIG, p, 0, event_mask); |
453 | free(p); |
454 | return; |
455 | } |
456 | fp = fopen_for_read(path); |
457 | if (fp != NULL) { |
458 | while (fgets(buf, STRING_LENGTH, fp) != NULL) { |
459 | /* Skip whitespace */ |
460 | line = buf; |
461 | line = skip_whitespace(line); |
462 | if (line[0] == '\0' || line[0] == '#') |
463 | continue; |
464 | process_config_line(line, event_mask); |
465 | } |
466 | fclose(fp); |
467 | } else { |
468 | goto read_config_file_err; |
469 | } |
470 | } else { |
471 | read_config_file_err: |
472 | if (optional == 0 && errno == ENOENT) |
473 | error_logger_and_die(LOG_ERR, "read config file: %s", path); |
474 | } |
475 | } /* End Function read_config_file */ |
476 | |
477 | static void process_config_line(const char *line, unsigned long *event_mask) |
478 | /* [SUMMARY] Process a line from a configuration file. |
479 | <line> The configuration line. |
480 | <event_mask> The event mask is written here. This is not initialised. |
481 | [RETURNS] Nothing. |
482 | */ |
483 | { |
484 | int num_args, count; |
485 | struct config_entry_struct *new; |
486 | char p[MAX_ARGS][STRING_LENGTH]; |
487 | char when[STRING_LENGTH], what[STRING_LENGTH]; |
488 | char name[STRING_LENGTH]; |
489 | const char *msg = ""; |
490 | char *ptr; |
491 | int i; |
492 | |
493 | /* !!!! Only Uppercase Keywords in devsfd.conf */ |
494 | static const char options[] ALIGN1 = |
495 | "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0" |
496 | "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0" |
497 | "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0" |
498 | "RMOLDCOMPAT\0""RMNEWCOMPAT\0"; |
499 | |
500 | for (count = 0; count < MAX_ARGS; ++count) |
501 | p[count][0] = '\0'; |
502 | num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s", |
503 | when, name, what, |
504 | p[0], p[1], p[2], p[3], p[4], p[5], p[6]); |
505 | |
506 | i = index_in_strings(options, when); |
507 | |
508 | /* "CLEAR_CONFIG" */ |
509 | if (i == 0) { |
510 | free_config(); |
511 | *event_mask = 0; |
512 | return; |
513 | } |
514 | |
515 | if (num_args < 2) |
516 | goto process_config_line_err; |
517 | |
518 | /* "INCLUDE" & "OPTIONAL_INCLUDE" */ |
519 | if (i == 1 || i == 2) { |
520 | st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL); |
521 | info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name); |
522 | read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask); |
523 | return; |
524 | } |
525 | /* "RESTORE" */ |
526 | if (i == 3) { |
527 | dir_operation(RESTORE, name, strlen(name),NULL); |
528 | return; |
529 | } |
530 | if (num_args < 3) |
531 | goto process_config_line_err; |
532 | |
533 | new = xzalloc(sizeof *new); |
534 | |
535 | for (count = 0; event_types[count].config_name != NULL; ++count) { |
536 | if (strcasecmp(when, event_types[count].config_name) != 0) |
537 | continue; |
538 | new->action.when = event_types[count].type; |
539 | break; |
540 | } |
541 | if (event_types[count].config_name == NULL) { |
542 | msg = "WHEN in"; |
543 | goto process_config_line_err; |
544 | } |
545 | |
546 | i = index_in_strings(options, what); |
547 | |
548 | switch (i) { |
549 | case 4: /* "PERMISSIONS" */ |
550 | new->action.what = AC_PERMISSIONS; |
551 | /* Get user and group */ |
552 | ptr = strchr(p[0], '.'); |
553 | if (ptr == NULL) { |
554 | msg = "UID.GID"; |
555 | goto process_config_line_err; /*"missing '.' in UID.GID"*/ |
556 | } |
557 | |
558 | *ptr++ = '\0'; |
559 | new->u.permissions.uid = get_uid_gid(UID, p[0]); |
560 | new->u.permissions.gid = get_uid_gid(GID, ptr); |
561 | /* Get mode */ |
562 | new->u.permissions.mode = get_mode(p[1]); |
563 | break; |
564 | case 5: /* MODLOAD */ |
565 | /*This action will pass "/dev/$devname"(i.e. "/dev/" prefixed to |
566 | the device name) to the module loading facility. In addition, |
567 | the /etc/modules.devfs configuration file is used.*/ |
568 | if (ENABLE_DEVFSD_MODLOAD) |
569 | new->action.what = AC_MODLOAD; |
570 | break; |
571 | case 6: /* EXECUTE */ |
572 | new->action.what = AC_EXECUTE; |
573 | num_args -= 3; |
574 | |
575 | for (count = 0; count < num_args; ++count) |
576 | new->u.execute.argv[count] = xstrdup(p[count]); |
577 | |
578 | new->u.execute.argv[num_args] = NULL; |
579 | break; |
580 | case 7: /* COPY */ |
581 | new->action.what = AC_COPY; |
582 | num_args -= 3; |
583 | if (num_args != 2) |
584 | goto process_config_line_err; /* missing path and function in line */ |
585 | |
586 | new->u.copy.source = xstrdup(p[0]); |
587 | new->u.copy.destination = xstrdup(p[1]); |
588 | break; |
589 | case 8: /* IGNORE */ |
590 | /* FALLTROUGH */ |
591 | case 9: /* MKOLDCOMPAT */ |
592 | /* FALLTROUGH */ |
593 | case 10: /* MKNEWCOMPAT */ |
594 | /* FALLTROUGH */ |
595 | case 11:/* RMOLDCOMPAT */ |
596 | /* FALLTROUGH */ |
597 | case 12: /* RMNEWCOMPAT */ |
598 | /* AC_IGNORE 6 |
599 | AC_MKOLDCOMPAT 7 |
600 | AC_MKNEWCOMPAT 8 |
601 | AC_RMOLDCOMPAT 9 |
602 | AC_RMNEWCOMPAT 10*/ |
603 | new->action.what = i - 2; |
604 | break; |
605 | default: |
606 | msg = "WHAT in"; |
607 | goto process_config_line_err; |
608 | /*esac*/ |
609 | } /* switch (i) */ |
610 | |
611 | xregcomp(&new->preg, name, REG_EXTENDED); |
612 | |
613 | *event_mask |= 1 << new->action.when; |
614 | new->next = NULL; |
615 | if (first_config == NULL) |
616 | first_config = new; |
617 | else |
618 | last_config->next = new; |
619 | last_config = new; |
620 | return; |
621 | |
622 | process_config_line_err: |
623 | msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line); |
624 | } /* End Function process_config_line */ |
625 | |
626 | static int do_servicing(int fd, unsigned long event_mask) |
627 | /* [SUMMARY] Service devfs changes until a signal is received. |
628 | <fd> The open control file. |
629 | <event_mask> The event mask. |
630 | [RETURNS] TRUE if SIGHUP was caught, else FALSE. |
631 | */ |
632 | { |
633 | ssize_t bytes; |
634 | struct devfsd_notify_struct info; |
635 | |
636 | /* (void*) cast is only in order to match prototype */ |
637 | xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask); |
638 | while (!caught_signal) { |
639 | errno = 0; |
640 | bytes = read(fd, (char *) &info, sizeof info); |
641 | if (caught_signal) |
642 | break; /* Must test for this first */ |
643 | if (errno == EINTR) |
644 | continue; /* Yes, the order is important */ |
645 | if (bytes < 1) |
646 | break; |
647 | service_name(&info); |
648 | } |
649 | if (caught_signal) { |
650 | int c_sighup = caught_sighup; |
651 | |
652 | caught_signal = FALSE; |
653 | caught_sighup = FALSE; |
654 | return c_sighup; |
655 | } |
656 | msg_logger_and_die(LOG_ERR, "read error on control file"); |
657 | } /* End Function do_servicing */ |
658 | |
659 | static void service_name(const struct devfsd_notify_struct *info) |
660 | /* [SUMMARY] Service a single devfs change. |
661 | <info> The devfs change. |
662 | [RETURNS] Nothing. |
663 | */ |
664 | { |
665 | unsigned int n; |
666 | regmatch_t mbuf[MAX_SUBEXPR]; |
667 | struct config_entry_struct *entry; |
668 | |
669 | if (ENABLE_DEBUG && info->overrun_count > 0) |
670 | msg_logger(LOG_ERR, "lost %u events", info->overrun_count); |
671 | |
672 | /* Discard lookups on "/dev/log" and "/dev/initctl" */ |
673 | if (info->type == DEVFSD_NOTIFY_LOOKUP |
674 | && ((info->devname[0] == 'l' && info->devname[1] == 'o' |
675 | && info->devname[2] == 'g' && !info->devname[3]) |
676 | || (info->devname[0] == 'i' && info->devname[1] == 'n' |
677 | && info->devname[2] == 'i' && info->devname[3] == 't' |
678 | && info->devname[4] == 'c' && info->devname[5] == 't' |
679 | && info->devname[6] == 'l' && !info->devname[7])) |
680 | ) |
681 | return; |
682 | |
683 | for (entry = first_config; entry != NULL; entry = entry->next) { |
684 | /* First check if action matches the type, then check if name matches */ |
685 | if (info->type != entry->action.when |
686 | || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0) |
687 | continue; |
688 | for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n) |
689 | /* VOID */; |
690 | |
691 | switch (entry->action.what) { |
692 | case AC_PERMISSIONS: |
693 | action_permissions(info, entry); |
694 | break; |
695 | case AC_MODLOAD: |
696 | if (ENABLE_DEVFSD_MODLOAD) |
697 | action_modload(info, entry); |
698 | break; |
699 | case AC_EXECUTE: |
700 | action_execute(info, entry, mbuf, n); |
701 | break; |
702 | case AC_COPY: |
703 | action_copy(info, entry, mbuf, n); |
704 | break; |
705 | case AC_IGNORE: |
706 | return; |
707 | /*break;*/ |
708 | case AC_MKOLDCOMPAT: |
709 | case AC_MKNEWCOMPAT: |
710 | case AC_RMOLDCOMPAT: |
711 | case AC_RMNEWCOMPAT: |
712 | action_compat(info, entry->action.what); |
713 | break; |
714 | default: |
715 | msg_logger_and_die(LOG_ERR, "Unknown action"); |
716 | } |
717 | } |
718 | } /* End Function service_name */ |
719 | |
720 | static void action_permissions(const struct devfsd_notify_struct *info, |
721 | const struct config_entry_struct *entry) |
722 | /* [SUMMARY] Update permissions for a device entry. |
723 | <info> The devfs change. |
724 | <entry> The config file entry. |
725 | [RETURNS] Nothing. |
726 | */ |
727 | { |
728 | struct stat statbuf; |
729 | |
730 | if (stat(info->devname, &statbuf) != 0 |
731 | || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 |
732 | || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0 |
733 | ) |
734 | error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname); |
735 | } /* End Function action_permissions */ |
736 | |
737 | static void action_modload(const struct devfsd_notify_struct *info, |
738 | const struct config_entry_struct *entry UNUSED_PARAM) |
739 | /* [SUMMARY] Load a module. |
740 | <info> The devfs change. |
741 | <entry> The config file entry. |
742 | [RETURNS] Nothing. |
743 | */ |
744 | { |
745 | char *argv[6]; |
746 | |
747 | argv[0] = (char*)MODPROBE; |
748 | argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */ |
749 | argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */ |
750 | argv[3] = (char*)CONFIG_MODULES_DEVFS; |
751 | argv[4] = concat_path_file("/dev", info->devname); /* device */ |
752 | argv[5] = NULL; |
753 | |
754 | spawn_and_wait(argv); |
755 | free(argv[4]); |
756 | } /* End Function action_modload */ |
757 | |
758 | static void action_execute(const struct devfsd_notify_struct *info, |
759 | const struct config_entry_struct *entry, |
760 | const regmatch_t *regexpr, unsigned int numexpr) |
761 | /* [SUMMARY] Execute a programme. |
762 | <info> The devfs change. |
763 | <entry> The config file entry. |
764 | <regexpr> The number of subexpression(start, end) offsets within the |
765 | device name. |
766 | <numexpr> The number of elements within <<regexpr>>. |
767 | [RETURNS] Nothing. |
768 | */ |
769 | { |
770 | unsigned int count; |
771 | struct get_variable_info gv_info; |
772 | char *argv[MAX_ARGS + 1]; |
773 | char largv[MAX_ARGS + 1][STRING_LENGTH]; |
774 | |
775 | gv_info.info = info; |
776 | gv_info.devname = info->devname; |
777 | snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); |
778 | for (count = 0; entry->u.execute.argv[count] != NULL; ++count) { |
779 | expand_expression(largv[count], STRING_LENGTH, |
780 | entry->u.execute.argv[count], |
781 | get_variable, &gv_info, |
782 | gv_info.devname, regexpr, numexpr); |
783 | argv[count] = largv[count]; |
784 | } |
785 | argv[count] = NULL; |
786 | spawn_and_wait(argv); |
787 | } /* End Function action_execute */ |
788 | |
789 | |
790 | static void action_copy(const struct devfsd_notify_struct *info, |
791 | const struct config_entry_struct *entry, |
792 | const regmatch_t *regexpr, unsigned int numexpr) |
793 | /* [SUMMARY] Copy permissions. |
794 | <info> The devfs change. |
795 | <entry> The config file entry. |
796 | <regexpr> This list of subexpression(start, end) offsets within the |
797 | device name. |
798 | <numexpr> The number of elements in <<regexpr>>. |
799 | [RETURNS] Nothing. |
800 | */ |
801 | { |
802 | mode_t new_mode; |
803 | struct get_variable_info gv_info; |
804 | struct stat source_stat, dest_stat; |
805 | char source[STRING_LENGTH], destination[STRING_LENGTH]; |
806 | int ret = 0; |
807 | |
808 | dest_stat.st_mode = 0; |
809 | |
810 | if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode)) |
811 | return; |
812 | gv_info.info = info; |
813 | gv_info.devname = info->devname; |
814 | |
815 | snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname); |
816 | expand_expression(source, STRING_LENGTH, entry->u.copy.source, |
817 | get_variable, &gv_info, gv_info.devname, |
818 | regexpr, numexpr); |
819 | |
820 | expand_expression(destination, STRING_LENGTH, entry->u.copy.destination, |
821 | get_variable, &gv_info, gv_info.devname, |
822 | regexpr, numexpr); |
823 | |
824 | if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0) |
825 | return; |
826 | lstat(destination, &dest_stat); |
827 | new_mode = source_stat.st_mode & ~S_ISVTX; |
828 | if (info->type == DEVFSD_NOTIFY_CREATE) |
829 | new_mode |= S_ISVTX; |
830 | else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX)) |
831 | new_mode |= S_ISVTX; |
832 | ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat); |
833 | if (ENABLE_DEBUG && ret && (errno != EEXIST)) |
834 | error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination); |
835 | } /* End Function action_copy */ |
836 | |
837 | static void action_compat(const struct devfsd_notify_struct *info, unsigned int action) |
838 | /* [SUMMARY] Process a compatibility request. |
839 | <info> The devfs change. |
840 | <action> The action to take. |
841 | [RETURNS] Nothing. |
842 | */ |
843 | { |
844 | int ret; |
845 | const char *compat_name = NULL; |
846 | const char *dest_name = info->devname; |
847 | const char *ptr; |
848 | char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH]; |
849 | int mode, host, bus, target, lun; |
850 | unsigned int i; |
851 | char rewind_; |
852 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host" */ |
853 | static const char *const fmt[] = { |
854 | NULL , |
855 | "sg/c%db%dt%du%d", /* scsi/generic */ |
856 | "sd/c%db%dt%du%d", /* scsi/disc */ |
857 | "sr/c%db%dt%du%d", /* scsi/cd */ |
858 | "sd/c%db%dt%du%dp%d", /* scsi/part */ |
859 | "st/c%db%dt%du%dm%d%c", /* scsi/mt */ |
860 | "ide/hd/c%db%dt%du%d", /* ide/host/disc */ |
861 | "ide/cd/c%db%dt%du%d", /* ide/host/cd */ |
862 | "ide/hd/c%db%dt%du%dp%d", /* ide/host/part */ |
863 | "ide/mt/c%db%dt%du%d%s", /* ide/host/mt */ |
864 | NULL |
865 | }; |
866 | |
867 | /* First construct compatibility name */ |
868 | switch (action) { |
869 | case AC_MKOLDCOMPAT: |
870 | case AC_RMOLDCOMPAT: |
871 | compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor); |
872 | break; |
873 | case AC_MKNEWCOMPAT: |
874 | case AC_RMNEWCOMPAT: |
875 | ptr = bb_basename(info->devname); |
876 | i = scan_dev_name(info->devname, info->namelen, ptr); |
877 | |
878 | /* nothing found */ |
879 | if (i == 0 || i > 9) |
880 | return; |
881 | |
882 | sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun); |
883 | snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0)); |
884 | dest_name = dest_buf; |
885 | compat_name = compat_buf; |
886 | |
887 | |
888 | /* 1 == scsi/generic 2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */ |
889 | if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7) |
890 | sprintf(compat_buf, fmt[i], host, bus, target, lun); |
891 | |
892 | /* 4 == scsi/part 8 == ide/host/part */ |
893 | if (i == 4 || i == 8) |
894 | sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4)); |
895 | |
896 | /* 5 == scsi/mt */ |
897 | if (i == 5) { |
898 | rewind_ = info->devname[info->namelen - 1]; |
899 | if (rewind_ != 'n') |
900 | rewind_ = '\0'; |
901 | mode=0; |
902 | if (ptr[2] == 'l' /*108*/ || ptr[2] == 'm'/*109*/) |
903 | mode = ptr[2] - 107; /* 1 or 2 */ |
904 | if (ptr[2] == 'a') |
905 | mode = 3; |
906 | sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_); |
907 | } |
908 | |
909 | /* 9 == ide/host/mt */ |
910 | if (i == 9) |
911 | snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2); |
912 | /* esac */ |
913 | } /* switch (action) */ |
914 | |
915 | if (compat_name == NULL) |
916 | return; |
917 | |
918 | /* Now decide what to do with it */ |
919 | switch (action) { |
920 | case AC_MKOLDCOMPAT: |
921 | case AC_MKNEWCOMPAT: |
922 | mksymlink(dest_name, compat_name); |
923 | break; |
924 | case AC_RMOLDCOMPAT: |
925 | case AC_RMNEWCOMPAT: |
926 | ret = unlink(compat_name); |
927 | if (ENABLE_DEBUG && ret) |
928 | error_logger(LOG_ERR, "unlink: %s", compat_name); |
929 | break; |
930 | /*esac*/ |
931 | } /* switch (action) */ |
932 | } /* End Function action_compat */ |
933 | |
934 | static void restore(char *spath, struct stat source_stat, int rootlen) |
935 | { |
936 | char *dpath; |
937 | struct stat dest_stat; |
938 | |
939 | dest_stat.st_mode = 0; |
940 | dpath = concat_path_file(mount_point, spath + rootlen); |
941 | lstat(dpath, &dest_stat); |
942 | free(dpath); |
943 | if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX)) |
944 | copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat); |
945 | |
946 | if (S_ISDIR(source_stat.st_mode)) |
947 | dir_operation(RESTORE, spath, rootlen, NULL); |
948 | } |
949 | |
950 | |
951 | static int copy_inode(const char *destpath, const struct stat *dest_stat, |
952 | mode_t new_mode, |
953 | const char *sourcepath, const struct stat *source_stat) |
954 | /* [SUMMARY] Copy an inode. |
955 | <destpath> The destination path. An existing inode may be deleted. |
956 | <dest_stat> The destination stat(2) information. |
957 | <new_mode> The desired new mode for the destination. |
958 | <sourcepath> The source path. |
959 | <source_stat> The source stat(2) information. |
960 | [RETURNS] TRUE on success, else FALSE. |
961 | */ |
962 | { |
963 | int source_len, dest_len; |
964 | char source_link[STRING_LENGTH], dest_link[STRING_LENGTH]; |
965 | int fd, val; |
966 | struct sockaddr_un un_addr; |
967 | char symlink_val[STRING_LENGTH]; |
968 | |
969 | if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) { |
970 | /* Same type */ |
971 | if (S_ISLNK(source_stat->st_mode)) { |
972 | source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1); |
973 | if ((source_len < 0) |
974 | || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0 |
975 | ) |
976 | return FALSE; |
977 | source_link[source_len] = '\0'; |
978 | dest_link[dest_len] = '\0'; |
979 | if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) { |
980 | unlink(destpath); |
981 | symlink(source_link, destpath); |
982 | } |
983 | return TRUE; |
984 | } /* Else not a symlink */ |
985 | chmod(destpath, new_mode & ~S_IFMT); |
986 | chown(destpath, source_stat->st_uid, source_stat->st_gid); |
987 | return TRUE; |
988 | } |
989 | /* Different types: unlink and create */ |
990 | unlink(destpath); |
991 | switch (source_stat->st_mode & S_IFMT) { |
992 | case S_IFSOCK: |
993 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
994 | if (fd < 0) |
995 | break; |
996 | un_addr.sun_family = AF_UNIX; |
997 | snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath); |
998 | val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr); |
999 | close(fd); |
1000 | if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0) |
1001 | break; |
1002 | goto do_chown; |
1003 | case S_IFLNK: |
1004 | val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1); |
1005 | if (val < 0) |
1006 | break; |
1007 | symlink_val[val] = '\0'; |
1008 | if (symlink(symlink_val, destpath) == 0) |
1009 | return TRUE; |
1010 | break; |
1011 | case S_IFREG: |
1012 | fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT); |
1013 | if (fd < 0) |
1014 | break; |
1015 | close(fd); |
1016 | if (chmod(destpath, new_mode & ~S_IFMT) != 0) |
1017 | break; |
1018 | goto do_chown; |
1019 | case S_IFBLK: |
1020 | case S_IFCHR: |
1021 | case S_IFIFO: |
1022 | if (mknod(destpath, new_mode, source_stat->st_rdev) != 0) |
1023 | break; |
1024 | goto do_chown; |
1025 | case S_IFDIR: |
1026 | if (mkdir(destpath, new_mode & ~S_IFMT) != 0) |
1027 | break; |
1028 | do_chown: |
1029 | if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0) |
1030 | return TRUE; |
1031 | /*break;*/ |
1032 | } |
1033 | return FALSE; |
1034 | } /* End Function copy_inode */ |
1035 | |
1036 | static void free_config(void) |
1037 | /* [SUMMARY] Free the configuration information. |
1038 | [RETURNS] Nothing. |
1039 | */ |
1040 | { |
1041 | struct config_entry_struct *c_entry; |
1042 | void *next; |
1043 | |
1044 | for (c_entry = first_config; c_entry != NULL; c_entry = next) { |
1045 | unsigned int count; |
1046 | |
1047 | next = c_entry->next; |
1048 | regfree(&c_entry->preg); |
1049 | if (c_entry->action.what == AC_EXECUTE) { |
1050 | for (count = 0; count < MAX_ARGS; ++count) { |
1051 | if (c_entry->u.execute.argv[count] == NULL) |
1052 | break; |
1053 | free(c_entry->u.execute.argv[count]); |
1054 | } |
1055 | } |
1056 | free(c_entry); |
1057 | } |
1058 | first_config = NULL; |
1059 | last_config = NULL; |
1060 | } /* End Function free_config */ |
1061 | |
1062 | static int get_uid_gid(int flag, const char *string) |
1063 | /* [SUMMARY] Convert a string to a UID or GID value. |
1064 | <flag> "UID" or "GID". |
1065 | <string> The string. |
1066 | [RETURNS] The UID or GID value. |
1067 | */ |
1068 | { |
1069 | struct passwd *pw_ent; |
1070 | struct group *grp_ent; |
1071 | static const char *msg; |
1072 | |
1073 | if (ENABLE_DEVFSD_VERBOSE) |
1074 | msg = "user"; |
1075 | |
1076 | if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1]))) |
1077 | return atoi(string); |
1078 | |
1079 | if (flag == UID && (pw_ent = getpwnam(string)) != NULL) |
1080 | return pw_ent->pw_uid; |
1081 | |
1082 | if (flag == GID && (grp_ent = getgrnam(string)) != NULL) |
1083 | return grp_ent->gr_gid; |
1084 | else if (ENABLE_DEVFSD_VERBOSE) |
1085 | msg = "group"; |
1086 | |
1087 | if (ENABLE_DEVFSD_VERBOSE) |
1088 | msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]); |
1089 | return 0; |
1090 | }/* End Function get_uid_gid */ |
1091 | |
1092 | static mode_t get_mode(const char *string) |
1093 | /* [SUMMARY] Convert a string to a mode value. |
1094 | <string> The string. |
1095 | [RETURNS] The mode value. |
1096 | */ |
1097 | { |
1098 | mode_t mode; |
1099 | int i; |
1100 | |
1101 | if (isdigit(string[0])) |
1102 | return strtoul(string, NULL, 8); |
1103 | if (strlen(string) != 9) |
1104 | msg_logger_and_die(LOG_ERR, "bad mode: %s", string); |
1105 | |
1106 | mode = 0; |
1107 | i = S_IRUSR; |
1108 | while (i > 0) { |
1109 | if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x') |
1110 | mode += i; |
1111 | i = i / 2; |
1112 | string++; |
1113 | } |
1114 | return mode; |
1115 | } /* End Function get_mode */ |
1116 | |
1117 | static void signal_handler(int sig) |
1118 | { |
1119 | caught_signal = TRUE; |
1120 | if (sig == SIGHUP) |
1121 | caught_sighup = TRUE; |
1122 | |
1123 | info_logger(LOG_INFO, "Caught signal %d", sig); |
1124 | } /* End Function signal_handler */ |
1125 | |
1126 | static const char *get_variable(const char *variable, void *info) |
1127 | { |
1128 | static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ |
1129 | static char *hostname; |
1130 | |
1131 | struct get_variable_info *gv_info = info; |
1132 | const char *field_names[] = { |
1133 | "hostname", "mntpt", "devpath", "devname", |
1134 | "uid", "gid", "mode", hostname, mount_point, |
1135 | gv_info->devpath, gv_info->devname, NULL |
1136 | }; |
1137 | int i; |
1138 | |
1139 | if (!hostname) |
1140 | hostname = safe_gethostname(); |
1141 | /* index_in_str_array returns i>=0 */ |
1142 | i = index_in_str_array(field_names, variable); |
1143 | |
1144 | if (i > 6 || i < 0 || (i > 1 && gv_info == NULL)) |
1145 | return NULL; |
1146 | if (i >= 0 && i <= 3) |
1147 | return field_names[i + 7]; |
1148 | |
1149 | if (i == 4) |
1150 | sprintf(sbuf, "%u", gv_info->info->uid); |
1151 | else if (i == 5) |
1152 | sprintf(sbuf, "%u", gv_info->info->gid); |
1153 | else if (i == 6) |
1154 | sprintf(sbuf, "%o", gv_info->info->mode); |
1155 | return sbuf; |
1156 | } /* End Function get_variable */ |
1157 | |
1158 | static void service(struct stat statbuf, char *path) |
1159 | { |
1160 | struct devfsd_notify_struct info; |
1161 | |
1162 | memset(&info, 0, sizeof info); |
1163 | info.type = DEVFSD_NOTIFY_REGISTERED; |
1164 | info.mode = statbuf.st_mode; |
1165 | info.major = major(statbuf.st_rdev); |
1166 | info.minor = minor(statbuf.st_rdev); |
1167 | info.uid = statbuf.st_uid; |
1168 | info.gid = statbuf.st_gid; |
1169 | snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1); |
1170 | info.namelen = strlen(info.devname); |
1171 | service_name(&info); |
1172 | if (S_ISDIR(statbuf.st_mode)) |
1173 | dir_operation(SERVICE, path, 0, NULL); |
1174 | } |
1175 | |
1176 | static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask) |
1177 | /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes. |
1178 | <flag> To choose which function to perform |
1179 | <dp> The directory pointer. This is closed upon completion. |
1180 | <dir_name> The name of the directory. |
1181 | <rootlen> string length parameter. |
1182 | [RETURNS] Nothing. |
1183 | */ |
1184 | { |
1185 | struct stat statbuf; |
1186 | DIR *dp; |
1187 | struct dirent *de; |
1188 | char *path; |
1189 | |
1190 | dp = warn_opendir(dir_name); |
1191 | if (dp == NULL) |
1192 | return; |
1193 | |
1194 | while ((de = readdir(dp)) != NULL) { |
1195 | |
1196 | if (de->d_name && DOT_OR_DOTDOT(de->d_name)) |
1197 | continue; |
1198 | path = concat_path_file(dir_name, de->d_name); |
1199 | if (lstat(path, &statbuf) == 0) { |
1200 | switch (type) { |
1201 | case SERVICE: |
1202 | service(statbuf, path); |
1203 | break; |
1204 | case RESTORE: |
1205 | restore(path, statbuf, var); |
1206 | break; |
1207 | case READ_CONFIG: |
1208 | read_config_file(path, var, event_mask); |
1209 | break; |
1210 | } |
1211 | } |
1212 | free(path); |
1213 | } |
1214 | closedir(dp); |
1215 | } /* End Function do_scan_and_service */ |
1216 | |
1217 | static int mksymlink(const char *oldpath, const char *newpath) |
1218 | /* [SUMMARY] Create a symlink, creating intervening directories as required. |
1219 | <oldpath> The string contained in the symlink. |
1220 | <newpath> The name of the new symlink. |
1221 | [RETURNS] 0 on success, else -1. |
1222 | */ |
1223 | { |
1224 | if (!make_dir_tree(newpath)) |
1225 | return -1; |
1226 | |
1227 | if (symlink(oldpath, newpath) != 0) { |
1228 | if (errno != EEXIST) |
1229 | return -1; |
1230 | } |
1231 | return 0; |
1232 | } /* End Function mksymlink */ |
1233 | |
1234 | |
1235 | static int make_dir_tree(const char *path) |
1236 | /* [SUMMARY] Creating intervening directories for a path as required. |
1237 | <path> The full pathname(including the leaf node). |
1238 | [RETURNS] TRUE on success, else FALSE. |
1239 | */ |
1240 | { |
1241 | if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1) |
1242 | return FALSE; |
1243 | return TRUE; |
1244 | } /* End Function make_dir_tree */ |
1245 | |
1246 | static int expand_expression(char *output, unsigned int outsize, |
1247 | const char *input, |
1248 | const char *(*get_variable_func)(const char *variable, void *info), |
1249 | void *info, |
1250 | const char *devname, |
1251 | const regmatch_t *ex, unsigned int numexp) |
1252 | /* [SUMMARY] Expand environment variables and regular subexpressions in string. |
1253 | <output> The output expanded expression is written here. |
1254 | <length> The size of the output buffer. |
1255 | <input> The input expression. This may equal <<output>>. |
1256 | <get_variable> A function which will be used to get variable values. If |
1257 | this returns NULL, the environment is searched instead. If this is NULL, |
1258 | only the environment is searched. |
1259 | <info> An arbitrary pointer passed to <<get_variable>>. |
1260 | <devname> Device name; specifically, this is the string that contains all |
1261 | of the regular subexpressions. |
1262 | <ex> Array of start / end offsets into info->devname for each subexpression |
1263 | <numexp> Number of regular subexpressions found in <<devname>>. |
1264 | [RETURNS] TRUE on success, else FALSE. |
1265 | */ |
1266 | { |
1267 | char temp[STRING_LENGTH]; |
1268 | |
1269 | if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info)) |
1270 | return FALSE; |
1271 | expand_regexp(output, outsize, temp, devname, ex, numexp); |
1272 | return TRUE; |
1273 | } /* End Function expand_expression */ |
1274 | |
1275 | static void expand_regexp(char *output, size_t outsize, const char *input, |
1276 | const char *devname, |
1277 | const regmatch_t *ex, unsigned int numex) |
1278 | /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9. |
1279 | <output> The output expanded expression is written here. |
1280 | <outsize> The size of the output buffer. |
1281 | <input> The input expression. This may NOT equal <<output>>, because |
1282 | supporting that would require yet another string-copy. However, it's not |
1283 | hard to write a simple wrapper function to add this functionality for those |
1284 | few cases that need it. |
1285 | <devname> Device name; specifically, this is the string that contains all |
1286 | of the regular subexpressions. |
1287 | <ex> An array of start and end offsets into <<devname>>, one for each |
1288 | subexpression |
1289 | <numex> Number of subexpressions in the offset-array <<ex>>. |
1290 | [RETURNS] Nothing. |
1291 | */ |
1292 | { |
1293 | const char last_exp = '0' - 1 + numex; |
1294 | int c = -1; |
1295 | |
1296 | /* Guarantee NULL termination by writing an explicit '\0' character into |
1297 | the very last byte */ |
1298 | if (outsize) |
1299 | output[--outsize] = '\0'; |
1300 | /* Copy the input string into the output buffer, replacing '\\' with '\' |
1301 | and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x |
1302 | codes are deleted */ |
1303 | while ((c != '\0') && (outsize != 0)) { |
1304 | c = *input; |
1305 | ++input; |
1306 | if (c == '\\') { |
1307 | c = *input; |
1308 | ++input; |
1309 | if (c != '\\') { |
1310 | if ((c >= '0') && (c <= last_exp)) { |
1311 | const regmatch_t *subexp = ex + (c - '0'); |
1312 | unsigned int sublen = subexp->rm_eo - subexp->rm_so; |
1313 | |
1314 | /* Range checking */ |
1315 | if (sublen > outsize) |
1316 | sublen = outsize; |
1317 | strncpy(output, devname + subexp->rm_so, sublen); |
1318 | output += sublen; |
1319 | outsize -= sublen; |
1320 | } |
1321 | continue; |
1322 | } |
1323 | } |
1324 | *output = c; |
1325 | ++output; |
1326 | --outsize; |
1327 | } /* while */ |
1328 | } /* End Function expand_regexp */ |
1329 | |
1330 | |
1331 | /* from compat_name.c */ |
1332 | |
1333 | struct translate_struct { |
1334 | const char *match; /* The string to match to(up to length) */ |
1335 | const char *format; /* Format of output, "%s" takes data past match string, |
1336 | NULL is effectively "%s"(just more efficient) */ |
1337 | }; |
1338 | |
1339 | static struct translate_struct translate_table[] = |
1340 | { |
1341 | {"sound/", NULL}, |
1342 | {"printers/", "lp%s"}, |
1343 | {"v4l/", NULL}, |
1344 | {"parports/", "parport%s"}, |
1345 | {"fb/", "fb%s"}, |
1346 | {"netlink/", NULL}, |
1347 | {"loop/", "loop%s"}, |
1348 | {"floppy/", "fd%s"}, |
1349 | {"rd/", "ram%s"}, |
1350 | {"md/", "md%s"}, /* Meta-devices */ |
1351 | {"vc/", "tty%s"}, |
1352 | {"misc/", NULL}, |
1353 | {"isdn/", NULL}, |
1354 | {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/ |
1355 | {"i2c/", "i2c-%s"}, |
1356 | {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */ |
1357 | {"tts/E", "ttyE%s"}, /* Stallion serial driver */ |
1358 | {"cua/E", "cue%s"}, /* Stallion serial driver callout */ |
1359 | {"tts/R", "ttyR%s"}, /* Rocketport serial driver */ |
1360 | {"cua/R", "cur%s"}, /* Rocketport serial driver callout */ |
1361 | {"ip2/", "ip2%s"}, /* Computone serial driver control */ |
1362 | {"tts/F", "ttyF%s"}, /* Computone serial driver */ |
1363 | {"cua/F", "cuf%s"}, /* Computone serial driver callout */ |
1364 | {"tts/C", "ttyC%s"}, /* Cyclades serial driver */ |
1365 | {"cua/C", "cub%s"}, /* Cyclades serial driver callout */ |
1366 | {"tts/", "ttyS%s"}, /* Generic serial: must be after others */ |
1367 | {"cua/", "cua%s"}, /* Generic serial: must be after others */ |
1368 | {"input/js", "js%s"}, /* Joystick driver */ |
1369 | {NULL, NULL} |
1370 | }; |
1371 | |
1372 | const char *get_old_name(const char *devname, unsigned int namelen, |
1373 | char *buffer, unsigned int major, unsigned int minor) |
1374 | /* [SUMMARY] Translate a kernel-supplied name into an old name. |
1375 | <devname> The device name provided by the kernel. |
1376 | <namelen> The length of the name. |
1377 | <buffer> A buffer that may be used. This should be at least 128 bytes long. |
1378 | <major> The major number for the device. |
1379 | <minor> The minor number for the device. |
1380 | [RETURNS] A pointer to the old name if known, else NULL. |
1381 | */ |
1382 | { |
1383 | const char *compat_name = NULL; |
1384 | const char *ptr; |
1385 | struct translate_struct *trans; |
1386 | unsigned int i; |
1387 | char mode; |
1388 | int indexx; |
1389 | const char *pty1; |
1390 | const char *pty2; |
1391 | size_t len; |
1392 | /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */ |
1393 | static const char *const fmt[] = { |
1394 | NULL , |
1395 | "sg%u", /* scsi/generic */ |
1396 | NULL, /* scsi/disc */ |
1397 | "sr%u", /* scsi/cd */ |
1398 | NULL, /* scsi/part */ |
1399 | "nst%u%c", /* scsi/mt */ |
1400 | "hd%c" , /* ide/host/disc */ |
1401 | "hd%c" , /* ide/host/cd */ |
1402 | "hd%c%s", /* ide/host/part */ |
1403 | "%sht%d", /* ide/host/mt */ |
1404 | "sbpcd%u", /* sbp/ */ |
1405 | "vcs%s", /* vcc/ */ |
1406 | "%cty%c%c", /* pty/ */ |
1407 | NULL |
1408 | }; |
1409 | |
1410 | for (trans = translate_table; trans->match != NULL; ++trans) { |
1411 | len = strlen(trans->match); |
1412 | |
1413 | if (strncmp(devname, trans->match, len) == 0) { |
1414 | if (trans->format == NULL) |
1415 | return devname + len; |
1416 | sprintf(buffer, trans->format, devname + len); |
1417 | return buffer; |
1418 | } |
1419 | } |
1420 | |
1421 | ptr = bb_basename(devname); |
1422 | i = scan_dev_name(devname, namelen, ptr); |
1423 | |
1424 | if (i > 0 && i < 13) |
1425 | compat_name = buffer; |
1426 | else |
1427 | return NULL; |
1428 | |
1429 | /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */ |
1430 | if (i == 1 || i == 3 || i == 10) |
1431 | sprintf(buffer, fmt[i], minor); |
1432 | |
1433 | /* 2 ==scsi/disc, 4 == scsi/part */ |
1434 | if (i == 2 || i == 4) |
1435 | compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4))); |
1436 | |
1437 | /* 5 == scsi/mt */ |
1438 | if (i == 5) { |
1439 | mode = ptr[2]; |
1440 | if (mode == 'n') |
1441 | mode = '\0'; |
1442 | sprintf(buffer, fmt[i], minor & 0x1f, mode); |
1443 | if (devname[namelen - 1] != 'n') |
1444 | ++compat_name; |
1445 | } |
1446 | /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */ |
1447 | if (i == 6 || i == 7 || i == 8) |
1448 | /* last arg should be ignored for i == 6 or i== 7 */ |
1449 | sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4); |
1450 | |
1451 | /* 9 == ide/host/mt */ |
1452 | if (i == 9) |
1453 | sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f); |
1454 | |
1455 | /* 11 == vcc/ */ |
1456 | if (i == 11) { |
1457 | sprintf(buffer, fmt[i], devname + 4); |
1458 | if (buffer[3] == '0') |
1459 | buffer[3] = '\0'; |
1460 | } |
1461 | /* 12 == pty/ */ |
1462 | if (i == 12) { |
1463 | pty1 = "pqrstuvwxyzabcde"; |
1464 | pty2 = "0123456789abcdef"; |
1465 | indexx = atoi(devname + 5); |
1466 | sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]); |
1467 | } |
1468 | return compat_name; |
1469 | } /* End Function get_old_name */ |
1470 | |
1471 | static char get_old_ide_name(unsigned int major, unsigned int minor) |
1472 | /* [SUMMARY] Get the old IDE name for a device. |
1473 | <major> The major number for the device. |
1474 | <minor> The minor number for the device. |
1475 | [RETURNS] The drive letter. |
1476 | */ |
1477 | { |
1478 | char letter = 'y'; /* 121 */ |
1479 | char c = 'a'; /* 97 */ |
1480 | int i = IDE0_MAJOR; |
1481 | |
1482 | /* I hope it works like the previous code as it saves a few bytes. Tito ;P */ |
1483 | do { |
1484 | if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR |
1485 | || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR |
1486 | || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR |
1487 | || i == IDE9_MAJOR |
1488 | ) { |
1489 | if ((unsigned int)i == major) { |
1490 | letter = c; |
1491 | break; |
1492 | } |
1493 | c += 2; |
1494 | } |
1495 | i++; |
1496 | } while (i <= IDE9_MAJOR); |
1497 | |
1498 | if (minor > 63) |
1499 | ++letter; |
1500 | return letter; |
1501 | } /* End Function get_old_ide_name */ |
1502 | |
1503 | static char *write_old_sd_name(char *buffer, |
1504 | unsigned int major, unsigned int minor, |
1505 | const char *part) |
1506 | /* [SUMMARY] Write the old SCSI disc name to a buffer. |
1507 | <buffer> The buffer to write to. |
1508 | <major> The major number for the device. |
1509 | <minor> The minor number for the device. |
1510 | <part> The partition string. Must be "" for a whole-disc entry. |
1511 | [RETURNS] A pointer to the buffer on success, else NULL. |
1512 | */ |
1513 | { |
1514 | unsigned int disc_index; |
1515 | |
1516 | if (major == 8) { |
1517 | sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part); |
1518 | return buffer; |
1519 | } |
1520 | if ((major > 64) && (major < 72)) { |
1521 | disc_index = ((major - 64) << 4) +(minor >> 4); |
1522 | if (disc_index < 26) |
1523 | sprintf(buffer, "sd%c%s", 'a' + disc_index, part); |
1524 | else |
1525 | sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part); |
1526 | return buffer; |
1527 | } |
1528 | return NULL; |
1529 | } /* End Function write_old_sd_name */ |
1530 | |
1531 | |
1532 | /* expression.c */ |
1533 | |
1534 | /*EXPERIMENTAL_FUNCTION*/ |
1535 | |
1536 | int st_expr_expand(char *output, unsigned int length, const char *input, |
1537 | const char *(*get_variable_func)(const char *variable, |
1538 | void *info), |
1539 | void *info) |
1540 | /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules. |
1541 | <output> The output expanded expression is written here. |
1542 | <length> The size of the output buffer. |
1543 | <input> The input expression. This may equal <<output>>. |
1544 | <get_variable> A function which will be used to get variable values. If |
1545 | this returns NULL, the environment is searched instead. If this is NULL, |
1546 | only the environment is searched. |
1547 | <info> An arbitrary pointer passed to <<get_variable>>. |
1548 | [RETURNS] TRUE on success, else FALSE. |
1549 | */ |
1550 | { |
1551 | char ch; |
1552 | unsigned int len; |
1553 | unsigned int out_pos = 0; |
1554 | const char *env; |
1555 | const char *ptr; |
1556 | struct passwd *pwent; |
1557 | char buffer[BUFFER_SIZE], tmp[STRING_LENGTH]; |
1558 | |
1559 | if (length > BUFFER_SIZE) |
1560 | length = BUFFER_SIZE; |
1561 | for (; TRUE; ++input) { |
1562 | switch (ch = *input) { |
1563 | case '$': |
1564 | /* Variable expansion */ |
1565 | input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info); |
1566 | if (input == NULL) |
1567 | return FALSE; |
1568 | break; |
1569 | case '~': |
1570 | /* Home directory expansion */ |
1571 | ch = input[1]; |
1572 | if (isspace(ch) ||(ch == '/') ||(ch == '\0')) { |
1573 | /* User's own home directory: leave separator for next time */ |
1574 | env = getenv("HOME"); |
1575 | if (env == NULL) { |
1576 | info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME"); |
1577 | return FALSE; |
1578 | } |
1579 | len = strlen(env); |
1580 | if (len + out_pos >= length) |
1581 | goto st_expr_expand_out; |
1582 | memcpy(buffer + out_pos, env, len + 1); |
1583 | out_pos += len; |
1584 | continue; |
1585 | } |
1586 | /* Someone else's home directory */ |
1587 | for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr) |
1588 | /* VOID */; |
1589 | len = ptr - input; |
1590 | if (len >= sizeof tmp) |
1591 | goto st_expr_expand_out; |
1592 | safe_memcpy(tmp, input, len); |
1593 | input = ptr - 1; |
1594 | pwent = getpwnam(tmp); |
1595 | if (pwent == NULL) { |
1596 | info_logger(LOG_INFO, "no pwent for: %s", tmp); |
1597 | return FALSE; |
1598 | } |
1599 | len = strlen(pwent->pw_dir); |
1600 | if (len + out_pos >= length) |
1601 | goto st_expr_expand_out; |
1602 | memcpy(buffer + out_pos, pwent->pw_dir, len + 1); |
1603 | out_pos += len; |
1604 | break; |
1605 | case '\0': |
1606 | /* Falltrough */ |
1607 | default: |
1608 | if (out_pos >= length) |
1609 | goto st_expr_expand_out; |
1610 | buffer[out_pos++] = ch; |
1611 | if (ch == '\0') { |
1612 | memcpy(output, buffer, out_pos); |
1613 | return TRUE; |
1614 | } |
1615 | break; |
1616 | /* esac */ |
1617 | } |
1618 | } |
1619 | return FALSE; |
1620 | st_expr_expand_out: |
1621 | info_logger(LOG_INFO, bb_msg_small_buffer); |
1622 | return FALSE; |
1623 | } /* End Function st_expr_expand */ |
1624 | |
1625 | |
1626 | /* Private functions follow */ |
1627 | |
1628 | static const char *expand_variable(char *buffer, unsigned int length, |
1629 | unsigned int *out_pos, const char *input, |
1630 | const char *(*func)(const char *variable, |
1631 | void *info), |
1632 | void *info) |
1633 | /* [SUMMARY] Expand a variable. |
1634 | <buffer> The buffer to write to. |
1635 | <length> The length of the output buffer. |
1636 | <out_pos> The current output position. This is updated. |
1637 | <input> A pointer to the input character pointer. |
1638 | <func> A function which will be used to get variable values. If this |
1639 | returns NULL, the environment is searched instead. If this is NULL, only |
1640 | the environment is searched. |
1641 | <info> An arbitrary pointer passed to <<func>>. |
1642 | <errfp> Diagnostic messages are written here. |
1643 | [RETURNS] A pointer to the end of this subexpression on success, else NULL. |
1644 | */ |
1645 | { |
1646 | char ch; |
1647 | int len; |
1648 | unsigned int open_braces; |
1649 | const char *env, *ptr; |
1650 | char tmp[STRING_LENGTH]; |
1651 | |
1652 | ch = input[0]; |
1653 | if (ch == '$') { |
1654 | /* Special case for "$$": PID */ |
1655 | sprintf(tmp, "%d", (int) getpid()); |
1656 | len = strlen(tmp); |
1657 | if (len + *out_pos >= length) |
1658 | goto expand_variable_out; |
1659 | |
1660 | memcpy(buffer + *out_pos, tmp, len + 1); |
1661 | out_pos += len; |
1662 | return input; |
1663 | } |
1664 | /* Ordinary variable expansion, possibly in braces */ |
1665 | if (ch != '{') { |
1666 | /* Simple variable expansion */ |
1667 | for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr) |
1668 | /* VOID */; |
1669 | len = ptr - input; |
1670 | if ((size_t)len >= sizeof tmp) |
1671 | goto expand_variable_out; |
1672 | |
1673 | safe_memcpy(tmp, input, len); |
1674 | input = ptr - 1; |
1675 | env = get_variable_v2(tmp, func, info); |
1676 | if (env == NULL) { |
1677 | info_logger(LOG_INFO, bb_msg_variable_not_found, tmp); |
1678 | return NULL; |
1679 | } |
1680 | len = strlen(env); |
1681 | if (len + *out_pos >= length) |
1682 | goto expand_variable_out; |
1683 | |
1684 | memcpy(buffer + *out_pos, env, len + 1); |
1685 | *out_pos += len; |
1686 | return input; |
1687 | } |
1688 | /* Variable in braces: check for ':' tricks */ |
1689 | ch = *++input; |
1690 | for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr) |
1691 | /* VOID */; |
1692 | if (ch == '}') { |
1693 | /* Must be simple variable expansion with "${var}" */ |
1694 | len = ptr - input; |
1695 | if ((size_t)len >= sizeof tmp) |
1696 | goto expand_variable_out; |
1697 | |
1698 | safe_memcpy(tmp, input, len); |
1699 | ptr = expand_variable(buffer, length, out_pos, tmp, func, info); |
1700 | if (ptr == NULL) |
1701 | return NULL; |
1702 | return input + len; |
1703 | } |
1704 | if (ch != ':' || ptr[1] != '-') { |
1705 | info_logger(LOG_INFO, "illegal char in var name"); |
1706 | return NULL; |
1707 | } |
1708 | /* It's that handy "${var:-word}" expression. Check if var is defined */ |
1709 | len = ptr - input; |
1710 | if ((size_t)len >= sizeof tmp) |
1711 | goto expand_variable_out; |
1712 | |
1713 | safe_memcpy(tmp, input, len); |
1714 | /* Move input pointer to ':' */ |
1715 | input = ptr; |
1716 | /* First skip to closing brace, taking note of nested expressions */ |
1717 | ptr += 2; |
1718 | ch = ptr[0]; |
1719 | for (open_braces = 1; open_braces > 0; ch = *++ptr) { |
1720 | switch (ch) { |
1721 | case '{': |
1722 | ++open_braces; |
1723 | break; |
1724 | case '}': |
1725 | --open_braces; |
1726 | break; |
1727 | case '\0': |
1728 | info_logger(LOG_INFO, "\"}\" not found in: %s", input); |
1729 | return NULL; |
1730 | default: |
1731 | break; |
1732 | } |
1733 | } |
1734 | --ptr; |
1735 | /* At this point ptr should point to closing brace of "${var:-word}" */ |
1736 | env = get_variable_v2(tmp, func, info); |
1737 | if (env != NULL) { |
1738 | /* Found environment variable, so skip the input to the closing brace |
1739 | and return the variable */ |
1740 | input = ptr; |
1741 | len = strlen(env); |
1742 | if (len + *out_pos >= length) |
1743 | goto expand_variable_out; |
1744 | |
1745 | memcpy(buffer + *out_pos, env, len + 1); |
1746 | *out_pos += len; |
1747 | return input; |
1748 | } |
1749 | /* Environment variable was not found, so process word. Advance input |
1750 | pointer to start of word in "${var:-word}" */ |
1751 | input += 2; |
1752 | len = ptr - input; |
1753 | if ((size_t)len >= sizeof tmp) |
1754 | goto expand_variable_out; |
1755 | |
1756 | safe_memcpy(tmp, input, len); |
1757 | input = ptr; |
1758 | if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info)) |
1759 | return NULL; |
1760 | len = strlen(tmp); |
1761 | if (len + *out_pos >= length) |
1762 | goto expand_variable_out; |
1763 | |
1764 | memcpy(buffer + *out_pos, tmp, len + 1); |
1765 | *out_pos += len; |
1766 | return input; |
1767 | expand_variable_out: |
1768 | info_logger(LOG_INFO, bb_msg_small_buffer); |
1769 | return NULL; |
1770 | } /* End Function expand_variable */ |
1771 | |
1772 | |
1773 | static const char *get_variable_v2(const char *variable, |
1774 | const char *(*func)(const char *variable, void *info), |
1775 | void *info) |
1776 | /* [SUMMARY] Get a variable from the environment or . |
1777 | <variable> The variable name. |
1778 | <func> A function which will be used to get the variable. If this returns |
1779 | NULL, the environment is searched instead. If this is NULL, only the |
1780 | environment is searched. |
1781 | [RETURNS] The value of the variable on success, else NULL. |
1782 | */ |
1783 | { |
1784 | const char *value; |
1785 | |
1786 | if (func != NULL) { |
1787 | value = (*func)(variable, info); |
1788 | if (value != NULL) |
1789 | return value; |
1790 | } |
1791 | return getenv(variable); |
1792 | } /* End Function get_variable */ |
1793 | |
1794 | /* END OF CODE */ |