devfsd.c 54 KB

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