3
0

devfsd.c 54 KB

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