3
0

devfsd.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  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 sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
  1003. static char *hostname;
  1004. struct get_variable_info *gv_info = info;
  1005. const char *field_names[] = {
  1006. "hostname", "mntpt", "devpath", "devname",
  1007. "uid", "gid", "mode", hostname, mount_point,
  1008. gv_info->devpath, gv_info->devname, NULL
  1009. };
  1010. int i;
  1011. if (!hostname)
  1012. hostname = safe_gethostname();
  1013. /* index_in_str_array returns i>=0 */
  1014. i = index_in_str_array(field_names, variable);
  1015. if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
  1016. return NULL;
  1017. if (i >= 0 && i <= 3)
  1018. return field_names[i + 7];
  1019. if (i == 4)
  1020. sprintf(sbuf, "%u", gv_info->info->uid);
  1021. else if (i == 5)
  1022. sprintf(sbuf, "%u", gv_info->info->gid);
  1023. else if (i == 6)
  1024. sprintf(sbuf, "%o", gv_info->info->mode);
  1025. return sbuf;
  1026. } /* End Function get_variable */
  1027. static void service(struct stat statbuf, char *path)
  1028. {
  1029. struct devfsd_notify_struct info;
  1030. memset(&info, 0, sizeof info);
  1031. info.type = DEVFSD_NOTIFY_REGISTERED;
  1032. info.mode = statbuf.st_mode;
  1033. info.major = major(statbuf.st_rdev);
  1034. info.minor = minor(statbuf.st_rdev);
  1035. info.uid = statbuf.st_uid;
  1036. info.gid = statbuf.st_gid;
  1037. snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
  1038. info.namelen = strlen(info.devname);
  1039. service_name(&info);
  1040. if (S_ISDIR(statbuf.st_mode))
  1041. dir_operation(SERVICE, path, 0, NULL);
  1042. }
  1043. static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
  1044. /* [SUMMARY] Scan a directory tree and generate register events on leaf nodes.
  1045. <flag> To choose which function to perform
  1046. <dp> The directory pointer. This is closed upon completion.
  1047. <dir_name> The name of the directory.
  1048. <rootlen> string length parameter.
  1049. [RETURNS] Nothing.
  1050. */
  1051. {
  1052. struct stat statbuf;
  1053. DIR *dp;
  1054. struct dirent *de;
  1055. char *path;
  1056. dp = warn_opendir(dir_name);
  1057. if (dp == NULL)
  1058. return;
  1059. while ((de = readdir(dp)) != NULL) {
  1060. if (de->d_name && DOT_OR_DOTDOT(de->d_name))
  1061. continue;
  1062. path = concat_path_file(dir_name, de->d_name);
  1063. if (lstat(path, &statbuf) == 0) {
  1064. switch (type) {
  1065. case SERVICE:
  1066. service(statbuf, path);
  1067. break;
  1068. case RESTORE:
  1069. restore(path, statbuf, var);
  1070. break;
  1071. case READ_CONFIG:
  1072. read_config_file(path, var, event_mask);
  1073. break;
  1074. }
  1075. }
  1076. free(path);
  1077. }
  1078. closedir(dp);
  1079. } /* End Function do_scan_and_service */
  1080. static int mksymlink(const char *oldpath, const char *newpath)
  1081. /* [SUMMARY] Create a symlink, creating intervening directories as required.
  1082. <oldpath> The string contained in the symlink.
  1083. <newpath> The name of the new symlink.
  1084. [RETURNS] 0 on success, else -1.
  1085. */
  1086. {
  1087. if (!make_dir_tree(newpath))
  1088. return -1;
  1089. if (symlink(oldpath, newpath) != 0) {
  1090. if (errno != EEXIST)
  1091. return -1;
  1092. }
  1093. return 0;
  1094. } /* End Function mksymlink */
  1095. static int make_dir_tree(const char *path)
  1096. /* [SUMMARY] Creating intervening directories for a path as required.
  1097. <path> The full pathname(including the leaf node).
  1098. [RETURNS] TRUE on success, else FALSE.
  1099. */
  1100. {
  1101. if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
  1102. return FALSE;
  1103. return TRUE;
  1104. } /* End Function make_dir_tree */
  1105. static int expand_expression(char *output, unsigned int outsize,
  1106. const char *input,
  1107. const char *(*get_variable_func)(const char *variable, void *info),
  1108. void *info,
  1109. const char *devname,
  1110. const regmatch_t *ex, unsigned int numexp)
  1111. /* [SUMMARY] Expand environment variables and regular subexpressions in string.
  1112. <output> The output expanded expression is written here.
  1113. <length> The size of the output buffer.
  1114. <input> The input expression. This may equal <<output>>.
  1115. <get_variable> A function which will be used to get variable values. If
  1116. this returns NULL, the environment is searched instead. If this is NULL,
  1117. only the environment is searched.
  1118. <info> An arbitrary pointer passed to <<get_variable>>.
  1119. <devname> Device name; specifically, this is the string that contains all
  1120. of the regular subexpressions.
  1121. <ex> Array of start / end offsets into info->devname for each subexpression
  1122. <numexp> Number of regular subexpressions found in <<devname>>.
  1123. [RETURNS] TRUE on success, else FALSE.
  1124. */
  1125. {
  1126. char temp[STRING_LENGTH];
  1127. if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
  1128. return FALSE;
  1129. expand_regexp(output, outsize, temp, devname, ex, numexp);
  1130. return TRUE;
  1131. } /* End Function expand_expression */
  1132. static void expand_regexp(char *output, size_t outsize, const char *input,
  1133. const char *devname,
  1134. const regmatch_t *ex, unsigned int numex)
  1135. /* [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
  1136. <output> The output expanded expression is written here.
  1137. <outsize> The size of the output buffer.
  1138. <input> The input expression. This may NOT equal <<output>>, because
  1139. supporting that would require yet another string-copy. However, it's not
  1140. hard to write a simple wrapper function to add this functionality for those
  1141. few cases that need it.
  1142. <devname> Device name; specifically, this is the string that contains all
  1143. of the regular subexpressions.
  1144. <ex> An array of start and end offsets into <<devname>>, one for each
  1145. subexpression
  1146. <numex> Number of subexpressions in the offset-array <<ex>>.
  1147. [RETURNS] Nothing.
  1148. */
  1149. {
  1150. const char last_exp = '0' - 1 + numex;
  1151. int c = -1;
  1152. /* Guarantee NULL termination by writing an explicit '\0' character into
  1153. the very last byte */
  1154. if (outsize)
  1155. output[--outsize] = '\0';
  1156. /* Copy the input string into the output buffer, replacing '\\' with '\'
  1157. and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
  1158. codes are deleted */
  1159. while ((c != '\0') && (outsize != 0)) {
  1160. c = *input;
  1161. ++input;
  1162. if (c == '\\') {
  1163. c = *input;
  1164. ++input;
  1165. if (c != '\\') {
  1166. if ((c >= '0') && (c <= last_exp)) {
  1167. const regmatch_t *subexp = ex + (c - '0');
  1168. unsigned int sublen = subexp->rm_eo - subexp->rm_so;
  1169. /* Range checking */
  1170. if (sublen > outsize)
  1171. sublen = outsize;
  1172. strncpy(output, devname + subexp->rm_so, sublen);
  1173. output += sublen;
  1174. outsize -= sublen;
  1175. }
  1176. continue;
  1177. }
  1178. }
  1179. *output = c;
  1180. ++output;
  1181. --outsize;
  1182. } /* while */
  1183. } /* End Function expand_regexp */
  1184. /* from compat_name.c */
  1185. struct translate_struct {
  1186. const char *match; /* The string to match to(up to length) */
  1187. const char *format; /* Format of output, "%s" takes data past match string,
  1188. NULL is effectively "%s"(just more efficient) */
  1189. };
  1190. static struct translate_struct translate_table[] =
  1191. {
  1192. {"sound/", NULL},
  1193. {"printers/", "lp%s"},
  1194. {"v4l/", NULL},
  1195. {"parports/", "parport%s"},
  1196. {"fb/", "fb%s"},
  1197. {"netlink/", NULL},
  1198. {"loop/", "loop%s"},
  1199. {"floppy/", "fd%s"},
  1200. {"rd/", "ram%s"},
  1201. {"md/", "md%s"}, /* Meta-devices */
  1202. {"vc/", "tty%s"},
  1203. {"misc/", NULL},
  1204. {"isdn/", NULL},
  1205. {"pg/", "pg%s"}, /* Parallel port generic ATAPI interface*/
  1206. {"i2c/", "i2c-%s"},
  1207. {"staliomem/", "staliomem%s"}, /* Stallion serial driver control */
  1208. {"tts/E", "ttyE%s"}, /* Stallion serial driver */
  1209. {"cua/E", "cue%s"}, /* Stallion serial driver callout */
  1210. {"tts/R", "ttyR%s"}, /* Rocketport serial driver */
  1211. {"cua/R", "cur%s"}, /* Rocketport serial driver callout */
  1212. {"ip2/", "ip2%s"}, /* Computone serial driver control */
  1213. {"tts/F", "ttyF%s"}, /* Computone serial driver */
  1214. {"cua/F", "cuf%s"}, /* Computone serial driver callout */
  1215. {"tts/C", "ttyC%s"}, /* Cyclades serial driver */
  1216. {"cua/C", "cub%s"}, /* Cyclades serial driver callout */
  1217. {"tts/", "ttyS%s"}, /* Generic serial: must be after others */
  1218. {"cua/", "cua%s"}, /* Generic serial: must be after others */
  1219. {"input/js", "js%s"}, /* Joystick driver */
  1220. {NULL, NULL}
  1221. };
  1222. const char *get_old_name(const char *devname, unsigned int namelen,
  1223. char *buffer, unsigned int major, unsigned int minor)
  1224. /* [SUMMARY] Translate a kernel-supplied name into an old name.
  1225. <devname> The device name provided by the kernel.
  1226. <namelen> The length of the name.
  1227. <buffer> A buffer that may be used. This should be at least 128 bytes long.
  1228. <major> The major number for the device.
  1229. <minor> The minor number for the device.
  1230. [RETURNS] A pointer to the old name if known, else NULL.
  1231. */
  1232. {
  1233. const char *compat_name = NULL;
  1234. const char *ptr;
  1235. struct translate_struct *trans;
  1236. unsigned int i;
  1237. char mode;
  1238. int indexx;
  1239. const char *pty1;
  1240. const char *pty2;
  1241. size_t len;
  1242. /* 1 to 5 "scsi/" , 6 to 9 "ide/host", 10 sbp/, 11 vcc/, 12 pty/ */
  1243. static const char *const fmt[] = {
  1244. NULL ,
  1245. "sg%u", /* scsi/generic */
  1246. NULL, /* scsi/disc */
  1247. "sr%u", /* scsi/cd */
  1248. NULL, /* scsi/part */
  1249. "nst%u%c", /* scsi/mt */
  1250. "hd%c" , /* ide/host/disc */
  1251. "hd%c" , /* ide/host/cd */
  1252. "hd%c%s", /* ide/host/part */
  1253. "%sht%d", /* ide/host/mt */
  1254. "sbpcd%u", /* sbp/ */
  1255. "vcs%s", /* vcc/ */
  1256. "%cty%c%c", /* pty/ */
  1257. NULL
  1258. };
  1259. for (trans = translate_table; trans->match != NULL; ++trans) {
  1260. len = strlen(trans->match);
  1261. if (strncmp(devname, trans->match, len) == 0) {
  1262. if (trans->format == NULL)
  1263. return devname + len;
  1264. sprintf(buffer, trans->format, devname + len);
  1265. return buffer;
  1266. }
  1267. }
  1268. ptr = bb_basename(devname);
  1269. i = scan_dev_name(devname, namelen, ptr);
  1270. if (i > 0 && i < 13)
  1271. compat_name = buffer;
  1272. else
  1273. return NULL;
  1274. /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
  1275. if (i == 1 || i == 3 || i == 10)
  1276. sprintf(buffer, fmt[i], minor);
  1277. /* 2 ==scsi/disc, 4 == scsi/part */
  1278. if (i == 2 || i == 4)
  1279. compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
  1280. /* 5 == scsi/mt */
  1281. if (i == 5) {
  1282. mode = ptr[2];
  1283. if (mode == 'n')
  1284. mode = '\0';
  1285. sprintf(buffer, fmt[i], minor & 0x1f, mode);
  1286. if (devname[namelen - 1] != 'n')
  1287. ++compat_name;
  1288. }
  1289. /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
  1290. if (i == 6 || i == 7 || i == 8)
  1291. /* last arg should be ignored for i == 6 or i== 7 */
  1292. sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
  1293. /* 9 == ide/host/mt */
  1294. if (i == 9)
  1295. sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
  1296. /* 11 == vcc/ */
  1297. if (i == 11) {
  1298. sprintf(buffer, fmt[i], devname + 4);
  1299. if (buffer[3] == '0')
  1300. buffer[3] = '\0';
  1301. }
  1302. /* 12 == pty/ */
  1303. if (i == 12) {
  1304. pty1 = "pqrstuvwxyzabcde";
  1305. pty2 = "0123456789abcdef";
  1306. indexx = atoi(devname + 5);
  1307. sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
  1308. }
  1309. return compat_name;
  1310. } /* End Function get_old_name */
  1311. static char get_old_ide_name(unsigned int major, unsigned int minor)
  1312. /* [SUMMARY] Get the old IDE name for a device.
  1313. <major> The major number for the device.
  1314. <minor> The minor number for the device.
  1315. [RETURNS] The drive letter.
  1316. */
  1317. {
  1318. char letter = 'y'; /* 121 */
  1319. char c = 'a'; /* 97 */
  1320. int i = IDE0_MAJOR;
  1321. /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
  1322. do {
  1323. if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
  1324. || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
  1325. || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
  1326. || i == IDE9_MAJOR
  1327. ) {
  1328. if ((unsigned int)i == major) {
  1329. letter = c;
  1330. break;
  1331. }
  1332. c += 2;
  1333. }
  1334. i++;
  1335. } while (i <= IDE9_MAJOR);
  1336. if (minor > 63)
  1337. ++letter;
  1338. return letter;
  1339. } /* End Function get_old_ide_name */
  1340. static char *write_old_sd_name(char *buffer,
  1341. unsigned int major, unsigned int minor,
  1342. const char *part)
  1343. /* [SUMMARY] Write the old SCSI disc name to a buffer.
  1344. <buffer> The buffer to write to.
  1345. <major> The major number for the device.
  1346. <minor> The minor number for the device.
  1347. <part> The partition string. Must be "" for a whole-disc entry.
  1348. [RETURNS] A pointer to the buffer on success, else NULL.
  1349. */
  1350. {
  1351. unsigned int disc_index;
  1352. if (major == 8) {
  1353. sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
  1354. return buffer;
  1355. }
  1356. if ((major > 64) && (major < 72)) {
  1357. disc_index = ((major - 64) << 4) +(minor >> 4);
  1358. if (disc_index < 26)
  1359. sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
  1360. else
  1361. sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
  1362. return buffer;
  1363. }
  1364. return NULL;
  1365. } /* End Function write_old_sd_name */
  1366. /* expression.c */
  1367. /*EXPERIMENTAL_FUNCTION*/
  1368. int st_expr_expand(char *output, unsigned int length, const char *input,
  1369. const char *(*get_variable_func)(const char *variable,
  1370. void *info),
  1371. void *info)
  1372. /* [SUMMARY] Expand an expression using Borne Shell-like unquoted rules.
  1373. <output> The output expanded expression is written here.
  1374. <length> The size of the output buffer.
  1375. <input> The input expression. This may equal <<output>>.
  1376. <get_variable> A function which will be used to get variable values. If
  1377. this returns NULL, the environment is searched instead. If this is NULL,
  1378. only the environment is searched.
  1379. <info> An arbitrary pointer passed to <<get_variable>>.
  1380. [RETURNS] TRUE on success, else FALSE.
  1381. */
  1382. {
  1383. char ch;
  1384. unsigned int len;
  1385. unsigned int out_pos = 0;
  1386. const char *env;
  1387. const char *ptr;
  1388. struct passwd *pwent;
  1389. char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
  1390. if (length > BUFFER_SIZE)
  1391. length = BUFFER_SIZE;
  1392. for (; TRUE; ++input) {
  1393. switch (ch = *input) {
  1394. case '$':
  1395. /* Variable expansion */
  1396. input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
  1397. if (input == NULL)
  1398. return FALSE;
  1399. break;
  1400. case '~':
  1401. /* Home directory expansion */
  1402. ch = input[1];
  1403. if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
  1404. /* User's own home directory: leave separator for next time */
  1405. env = getenv("HOME");
  1406. if (env == NULL) {
  1407. info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
  1408. return FALSE;
  1409. }
  1410. len = strlen(env);
  1411. if (len + out_pos >= length)
  1412. goto st_expr_expand_out;
  1413. memcpy(buffer + out_pos, env, len + 1);
  1414. out_pos += len;
  1415. continue;
  1416. }
  1417. /* Someone else's home directory */
  1418. for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
  1419. /* VOID */;
  1420. len = ptr - input;
  1421. if (len >= sizeof tmp)
  1422. goto st_expr_expand_out;
  1423. safe_memcpy(tmp, input, len);
  1424. input = ptr - 1;
  1425. pwent = getpwnam(tmp);
  1426. if (pwent == NULL) {
  1427. info_logger(LOG_INFO, "no pwent for: %s", tmp);
  1428. return FALSE;
  1429. }
  1430. len = strlen(pwent->pw_dir);
  1431. if (len + out_pos >= length)
  1432. goto st_expr_expand_out;
  1433. memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
  1434. out_pos += len;
  1435. break;
  1436. case '\0':
  1437. /* Falltrough */
  1438. default:
  1439. if (out_pos >= length)
  1440. goto st_expr_expand_out;
  1441. buffer[out_pos++] = ch;
  1442. if (ch == '\0') {
  1443. memcpy(output, buffer, out_pos);
  1444. return TRUE;
  1445. }
  1446. break;
  1447. /* esac */
  1448. }
  1449. }
  1450. return FALSE;
  1451. st_expr_expand_out:
  1452. info_logger(LOG_INFO, bb_msg_small_buffer);
  1453. return FALSE;
  1454. } /* End Function st_expr_expand */
  1455. /* Private functions follow */
  1456. static const char *expand_variable(char *buffer, unsigned int length,
  1457. unsigned int *out_pos, const char *input,
  1458. const char *(*func)(const char *variable,
  1459. void *info),
  1460. void *info)
  1461. /* [SUMMARY] Expand a variable.
  1462. <buffer> The buffer to write to.
  1463. <length> The length of the output buffer.
  1464. <out_pos> The current output position. This is updated.
  1465. <input> A pointer to the input character pointer.
  1466. <func> A function which will be used to get variable values. If this
  1467. returns NULL, the environment is searched instead. If this is NULL, only
  1468. the environment is searched.
  1469. <info> An arbitrary pointer passed to <<func>>.
  1470. <errfp> Diagnostic messages are written here.
  1471. [RETURNS] A pointer to the end of this subexpression on success, else NULL.
  1472. */
  1473. {
  1474. char ch;
  1475. int len;
  1476. unsigned int open_braces;
  1477. const char *env, *ptr;
  1478. char tmp[STRING_LENGTH];
  1479. ch = input[0];
  1480. if (ch == '$') {
  1481. /* Special case for "$$": PID */
  1482. sprintf(tmp, "%d", (int) getpid());
  1483. len = strlen(tmp);
  1484. if (len + *out_pos >= length)
  1485. goto expand_variable_out;
  1486. memcpy(buffer + *out_pos, tmp, len + 1);
  1487. out_pos += len;
  1488. return input;
  1489. }
  1490. /* Ordinary variable expansion, possibly in braces */
  1491. if (ch != '{') {
  1492. /* Simple variable expansion */
  1493. for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
  1494. /* VOID */;
  1495. len = ptr - input;
  1496. if ((size_t)len >= sizeof tmp)
  1497. goto expand_variable_out;
  1498. safe_memcpy(tmp, input, len);
  1499. input = ptr - 1;
  1500. env = get_variable_v2(tmp, func, info);
  1501. if (env == NULL) {
  1502. info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
  1503. return NULL;
  1504. }
  1505. len = strlen(env);
  1506. if (len + *out_pos >= length)
  1507. goto expand_variable_out;
  1508. memcpy(buffer + *out_pos, env, len + 1);
  1509. *out_pos += len;
  1510. return input;
  1511. }
  1512. /* Variable in braces: check for ':' tricks */
  1513. ch = *++input;
  1514. for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
  1515. /* VOID */;
  1516. if (ch == '}') {
  1517. /* Must be simple variable expansion with "${var}" */
  1518. len = ptr - input;
  1519. if ((size_t)len >= sizeof tmp)
  1520. goto expand_variable_out;
  1521. safe_memcpy(tmp, input, len);
  1522. ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
  1523. if (ptr == NULL)
  1524. return NULL;
  1525. return input + len;
  1526. }
  1527. if (ch != ':' || ptr[1] != '-') {
  1528. info_logger(LOG_INFO, "illegal char in var name");
  1529. return NULL;
  1530. }
  1531. /* It's that handy "${var:-word}" expression. Check if var is defined */
  1532. len = ptr - input;
  1533. if ((size_t)len >= sizeof tmp)
  1534. goto expand_variable_out;
  1535. safe_memcpy(tmp, input, len);
  1536. /* Move input pointer to ':' */
  1537. input = ptr;
  1538. /* First skip to closing brace, taking note of nested expressions */
  1539. ptr += 2;
  1540. ch = ptr[0];
  1541. for (open_braces = 1; open_braces > 0; ch = *++ptr) {
  1542. switch (ch) {
  1543. case '{':
  1544. ++open_braces;
  1545. break;
  1546. case '}':
  1547. --open_braces;
  1548. break;
  1549. case '\0':
  1550. info_logger(LOG_INFO, "\"}\" not found in: %s", input);
  1551. return NULL;
  1552. default:
  1553. break;
  1554. }
  1555. }
  1556. --ptr;
  1557. /* At this point ptr should point to closing brace of "${var:-word}" */
  1558. env = get_variable_v2(tmp, func, info);
  1559. if (env != NULL) {
  1560. /* Found environment variable, so skip the input to the closing brace
  1561. and return the variable */
  1562. input = ptr;
  1563. len = strlen(env);
  1564. if (len + *out_pos >= length)
  1565. goto expand_variable_out;
  1566. memcpy(buffer + *out_pos, env, len + 1);
  1567. *out_pos += len;
  1568. return input;
  1569. }
  1570. /* Environment variable was not found, so process word. Advance input
  1571. pointer to start of word in "${var:-word}" */
  1572. input += 2;
  1573. len = ptr - input;
  1574. if ((size_t)len >= sizeof tmp)
  1575. goto expand_variable_out;
  1576. safe_memcpy(tmp, input, len);
  1577. input = ptr;
  1578. if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
  1579. return NULL;
  1580. len = strlen(tmp);
  1581. if (len + *out_pos >= length)
  1582. goto expand_variable_out;
  1583. memcpy(buffer + *out_pos, tmp, len + 1);
  1584. *out_pos += len;
  1585. return input;
  1586. expand_variable_out:
  1587. info_logger(LOG_INFO, bb_msg_small_buffer);
  1588. return NULL;
  1589. } /* End Function expand_variable */
  1590. static const char *get_variable_v2(const char *variable,
  1591. const char *(*func)(const char *variable, void *info),
  1592. void *info)
  1593. /* [SUMMARY] Get a variable from the environment or .
  1594. <variable> The variable name.
  1595. <func> A function which will be used to get the variable. If this returns
  1596. NULL, the environment is searched instead. If this is NULL, only the
  1597. environment is searched.
  1598. [RETURNS] The value of the variable on success, else NULL.
  1599. */
  1600. {
  1601. const char *value;
  1602. if (func != NULL) {
  1603. value = (*func)(variable, info);
  1604. if (value != NULL)
  1605. return value;
  1606. }
  1607. return getenv(variable);
  1608. } /* End Function get_variable */
  1609. /* END OF CODE */