devfsd.c 61 KB

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