devfsd.c 54 KB

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