devfsd.c 54 KB

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