devfsd.c 59 KB

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