devfsd.c 59 KB

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