devfsd.c 54 KB

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