1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873 |
- #include "libbb.h"
- #include "xregex.h"
- #include <syslog.h>
- #include <sys/un.h>
- #include <sys/sysmacros.h>
- #define IDE0_MAJOR 3
- #define IDE1_MAJOR 22
- #define IDE2_MAJOR 33
- #define IDE3_MAJOR 34
- #define IDE4_MAJOR 56
- #define IDE5_MAJOR 57
- #define IDE6_MAJOR 88
- #define IDE7_MAJOR 89
- #define IDE8_MAJOR 90
- #define IDE9_MAJOR 91
- #define DEVFSD_PROTOCOL_REVISION_KERNEL 5
- #define DEVFSD_IOCTL_BASE 'd'
- #define DEVFSDIOC_GET_PROTO_REV _IOR(DEVFSD_IOCTL_BASE, 0, int)
- #define DEVFSDIOC_SET_EVENT_MASK _IOW(DEVFSD_IOCTL_BASE, 2, int)
- #define DEVFSDIOC_RELEASE_EVENT_QUEUE _IOW(DEVFSD_IOCTL_BASE, 3, int)
- #define DEVFSDIOC_SET_CONFIG_DEBUG_MASK _IOW(DEVFSD_IOCTL_BASE, 4, int)
- #define DEVFSD_NOTIFY_REGISTERED 0
- #define DEVFSD_NOTIFY_UNREGISTERED 1
- #define DEVFSD_NOTIFY_ASYNC_OPEN 2
- #define DEVFSD_NOTIFY_CLOSE 3
- #define DEVFSD_NOTIFY_LOOKUP 4
- #define DEVFSD_NOTIFY_CHANGE 5
- #define DEVFSD_NOTIFY_CREATE 6
- #define DEVFSD_NOTIFY_DELETE 7
- #define DEVFS_PATHLEN 1024
- struct devfsd_notify_struct {
-
- unsigned int type;
- unsigned int mode;
- unsigned int major;
- unsigned int minor;
- unsigned int uid;
- unsigned int gid;
- unsigned int overrun_count;
- unsigned int namelen;
-
- char devname[DEVFS_PATHLEN];
- };
- #define BUFFER_SIZE 16384
- #define DEVFSD_VERSION "1.3.25"
- #define CONFIG_FILE "/etc/devfsd.conf"
- #define MODPROBE "/sbin/modprobe"
- #define MODPROBE_SWITCH_1 "-k"
- #define MODPROBE_SWITCH_2 "-C"
- #define CONFIG_MODULES_DEVFS "/etc/modules.devfs"
- #define MAX_ARGS (6 + 1)
- #define MAX_SUBEXPR 10
- #define STRING_LENGTH 255
- #define UID 0
- #define GID 1
- # define DIE 1
- # define NO_DIE 0
- #define RESTORE 0
- #define SERVICE 1
- #define READ_CONFIG 2
- #define DEVFSD_PROTOCOL_REVISION_DAEMON 5
- #if DEVFSD_PROTOCOL_REVISION_KERNEL != DEVFSD_PROTOCOL_REVISION_DAEMON
- #error protocol version mismatch. Update your kernel headers
- #endif
- #define AC_PERMISSIONS 0
- #define AC_MODLOAD 1
- #define AC_EXECUTE 2
- #define AC_MFUNCTION 3
- #define AC_CFUNCTION 4
- #define AC_COPY 5
- #define AC_IGNORE 6
- #define AC_MKOLDCOMPAT 7
- #define AC_MKNEWCOMPAT 8
- #define AC_RMOLDCOMPAT 9
- #define AC_RMNEWCOMPAT 10
- #define AC_RESTORE 11
- struct permissions_type {
- mode_t mode;
- uid_t uid;
- gid_t gid;
- };
- struct execute_type {
- char *argv[MAX_ARGS + 1];
- };
- struct copy_type {
- const char *source;
- const char *destination;
- };
- struct action_type {
- unsigned int what;
- unsigned int when;
- };
- struct config_entry_struct {
- struct action_type action;
- regex_t preg;
- union
- {
- struct permissions_type permissions;
- struct execute_type execute;
- struct copy_type copy;
- }
- u;
- struct config_entry_struct *next;
- };
- struct get_variable_info {
- const struct devfsd_notify_struct *info;
- const char *devname;
- char devpath[STRING_LENGTH];
- };
- static void dir_operation(int , const char * , int, unsigned long*);
- static void service(struct stat statbuf, char *path);
- static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
- static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
- static int mksymlink(const char *oldpath, const char *newpath);
- static void read_config_file(char *path, int optional, unsigned long *event_mask);
- static void process_config_line(const char *, unsigned long *);
- static int do_servicing(int, unsigned long);
- static void service_name(const struct devfsd_notify_struct *);
- static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
- static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
- const regmatch_t *, unsigned);
- static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
- static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
- const regmatch_t *, unsigned);
- static void action_compat(const struct devfsd_notify_struct *, unsigned);
- static void free_config(void);
- static void restore(char *spath, struct stat source_stat, int rootlen);
- static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
- static mode_t get_mode(const char *);
- static void signal_handler(int);
- static const char *get_variable(const char *, void *);
- static int make_dir_tree(const char *);
- static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
- const char *, const regmatch_t *, unsigned);
- static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
- static const char *expand_variable( char *, unsigned, unsigned *, const char *,
- const char *(*)(const char *, void *), void *);
- static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
- static char get_old_ide_name(unsigned, unsigned);
- static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
- static int get_uid_gid(int flag, const char *string);
- static void safe_memcpy(char * dest, const char * src, int len);
- static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
- static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
- static struct config_entry_struct *first_config = NULL;
- static struct config_entry_struct *last_config = NULL;
- static char *mount_point = NULL;
- static volatile int caught_signal = FALSE;
- static volatile int caught_sighup = FALSE;
- static struct initial_symlink_struct {
- const char *dest;
- const char *name;
- } initial_symlinks[] = {
- {"/proc/self/fd", "fd"},
- {"fd/0", "stdin"},
- {"fd/1", "stdout"},
- {"fd/2", "stderr"},
- {NULL, NULL},
- };
- static struct event_type {
- unsigned int type;
- const char *config_name;
- } event_types[] = {
- {DEVFSD_NOTIFY_REGISTERED, "REGISTER"},
- {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
- {DEVFSD_NOTIFY_ASYNC_OPEN, "ASYNC_OPEN"},
- {DEVFSD_NOTIFY_CLOSE, "CLOSE"},
- {DEVFSD_NOTIFY_LOOKUP, "LOOKUP"},
- {DEVFSD_NOTIFY_CHANGE, "CHANGE"},
- {DEVFSD_NOTIFY_CREATE, "CREATE"},
- {DEVFSD_NOTIFY_DELETE, "DELETE"},
- {0xffffffff, NULL}
- };
- static const char bb_msg_proto_rev[] ALIGN1 = "protocol revision";
- static const char bb_msg_bad_config[] ALIGN1 = "bad %s config file: %s";
- static const char bb_msg_small_buffer[] ALIGN1 = "buffer too small";
- static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
- #if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
- #define info_logger(p, fmt, args...) bb_info_msg(fmt, ## args)
- #define simple_info_logger(p, msg) bb_simple_info_msg(msg)
- #define msg_logger(p, fmt, args...) bb_error_msg(fmt, ## args)
- #define simple_msg_logger(p, msg) bb_simple_error_msg(msg)
- #define msg_logger_and_die(p, fmt, args...) bb_error_msg_and_die(fmt, ## args)
- #define simple_msg_logger_and_die(p, msg) bb_simple_error_msg_and_die(msg)
- #define error_logger(p, fmt, args...) bb_perror_msg(fmt, ## args)
- #define error_logger_and_die(p, fmt, args...) bb_perror_msg_and_die(fmt, ## args)
- #else
- #define info_logger(p, fmt, args...)
- #define simple_info_logger(p, msg)
- #define msg_logger(p, fmt, args...)
- #define simple_msg_logger(p, msg)
- #define msg_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
- #define simple_msg_logger_and_die(p, msg) exit(EXIT_FAILURE)
- #define error_logger(p, fmt, args...)
- #define error_logger_and_die(p, fmt, args...) exit(EXIT_FAILURE)
- #endif
- static void safe_memcpy(char *dest, const char *src, int len)
- {
- memcpy(dest , src, len);
- dest[len] = '\0';
- }
- static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
- {
- if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
- return 2 + addendum;
- if (d[n - 2] == 'c' && d[n - 1] == 'd')
- return 3 + addendum;
- if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
- return 4 + addendum;
- if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
- return 5 + addendum;
- return 0;
- }
- static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
- {
- if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
- if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
- && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
- )
- return 1;
- return scan_dev_name_common(d, n, 0, ptr);
- }
- if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
- && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
- )
- return scan_dev_name_common(d, n, 4, ptr);
- if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
- return 10;
- if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
- return 11;
- if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
- return 12;
- return 0;
- }
- int devfsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int devfsd_main(int argc, char **argv)
- {
- int print_version = FALSE;
- int do_daemon = TRUE;
- int no_polling = FALSE;
- int do_scan;
- int fd, proto_rev, count;
- unsigned long event_mask = 0;
- struct sigaction new_action;
- struct initial_symlink_struct *curr;
- if (argc < 2)
- bb_show_usage();
- for (count = 2; count < argc; ++count) {
- if (argv[count][0] == '-') {
- if (argv[count][1] == 'v' && !argv[count][2])
- print_version = TRUE;
- else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
- && argv[count][2] == 'g' && !argv[count][3])
- do_daemon = FALSE;
- else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
- && argv[count][2] == 'p' && !argv[count][3])
- no_polling = TRUE;
- else
- bb_show_usage();
- }
- }
- mount_point = bb_simplify_path(argv[1]);
- xchdir(mount_point);
- fd = xopen(".devfsd", O_RDONLY);
- close_on_exec_on(fd);
- xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
-
- for (curr = initial_symlinks; curr->dest != NULL; ++curr)
- symlink(curr->dest, curr->name);
-
- if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
- printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
- applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
- DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
- if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
- bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
- exit(EXIT_SUCCESS);
- }
-
- xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
-
- sigemptyset(&new_action.sa_mask);
- new_action.sa_flags = 0;
- new_action.sa_handler = signal_handler;
- sigaction_set(SIGHUP, &new_action);
- sigaction_set(SIGUSR1, &new_action);
- printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);
-
- umask(0);
- read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
-
- dir_operation(SERVICE, mount_point, 0, NULL);
- if (ENABLE_DEVFSD_FG_NP && no_polling)
- exit(EXIT_SUCCESS);
- if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
- logmode = LOGMODE_BOTH;
- else if (do_daemon == TRUE)
- logmode = LOGMODE_SYSLOG;
-
-
- if (do_daemon) {
-
- xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
- bb_daemonize_or_rexec(0, argv);
- } else if (ENABLE_DEVFSD_FG_NP) {
- setpgid(0, 0);
- }
- while (TRUE) {
- do_scan = do_servicing(fd, event_mask);
- free_config();
- read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
- if (do_scan)
- dir_operation(SERVICE, mount_point, 0, NULL);
- }
- if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
- }
- static void read_config_file(char *path, int optional, unsigned long *event_mask)
- {
- struct stat statbuf;
- FILE *fp;
- char buf[STRING_LENGTH];
- char *line = NULL;
- char *p;
- if (stat(path, &statbuf) == 0) {
-
-
- if (S_ISDIR(statbuf.st_mode)) {
- p = bb_simplify_path(path);
- dir_operation(READ_CONFIG, p, 0, event_mask);
- free(p);
- return;
- }
- fp = fopen_for_read(path);
- if (fp != NULL) {
- while (fgets(buf, STRING_LENGTH, fp) != NULL) {
-
- line = buf;
- line = skip_whitespace(line);
- if (line[0] == '\0' || line[0] == '#')
- continue;
- process_config_line(line, event_mask);
- }
- fclose(fp);
- } else {
- goto read_config_file_err;
- }
- } else {
- read_config_file_err:
- if (optional == 0 && errno == ENOENT)
- error_logger_and_die(LOG_ERR, "read config file: %s", path);
- }
- }
- static void process_config_line(const char *line, unsigned long *event_mask)
- {
- int num_args, count;
- struct config_entry_struct *new;
- char p[MAX_ARGS][STRING_LENGTH];
- char when[STRING_LENGTH], what[STRING_LENGTH];
- char name[STRING_LENGTH];
- const char *msg = "";
- char *ptr;
- int i;
-
- static const char options[] ALIGN1 =
- "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
- "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
- "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
- "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
- for (count = 0; count < MAX_ARGS; ++count)
- p[count][0] = '\0';
- num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
- when, name, what,
- p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
- i = index_in_strings(options, when);
-
- if (i == 0) {
- free_config();
- *event_mask = 0;
- return;
- }
- if (num_args < 2)
- goto process_config_line_err;
-
- if (i == 1 || i == 2) {
- st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
- info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
- read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
- return;
- }
-
- if (i == 3) {
- dir_operation(RESTORE, name, strlen(name),NULL);
- return;
- }
- if (num_args < 3)
- goto process_config_line_err;
- new = xzalloc(sizeof *new);
- for (count = 0; event_types[count].config_name != NULL; ++count) {
- if (strcasecmp(when, event_types[count].config_name) != 0)
- continue;
- new->action.when = event_types[count].type;
- break;
- }
- if (event_types[count].config_name == NULL) {
- msg = "WHEN in";
- goto process_config_line_err;
- }
- i = index_in_strings(options, what);
- switch (i) {
- case 4:
- new->action.what = AC_PERMISSIONS;
-
- ptr = strchr(p[0], '.');
- if (ptr == NULL) {
- msg = "UID.GID";
- goto process_config_line_err;
- }
- *ptr++ = '\0';
- new->u.permissions.uid = get_uid_gid(UID, p[0]);
- new->u.permissions.gid = get_uid_gid(GID, ptr);
-
- new->u.permissions.mode = get_mode(p[1]);
- break;
- case 5:
-
- if (ENABLE_DEVFSD_MODLOAD)
- new->action.what = AC_MODLOAD;
- break;
- case 6:
- new->action.what = AC_EXECUTE;
- num_args -= 3;
- for (count = 0; count < num_args; ++count)
- new->u.execute.argv[count] = xstrdup(p[count]);
- new->u.execute.argv[num_args] = NULL;
- break;
- case 7:
- new->action.what = AC_COPY;
- num_args -= 3;
- if (num_args != 2)
- goto process_config_line_err;
- new->u.copy.source = xstrdup(p[0]);
- new->u.copy.destination = xstrdup(p[1]);
- break;
- case 8:
-
- case 9:
-
- case 10:
-
- case 11:
-
- case 12:
-
- new->action.what = i - 2;
- break;
- default:
- msg = "WHAT in";
- goto process_config_line_err;
-
- }
- xregcomp(&new->preg, name, REG_EXTENDED);
- *event_mask |= 1 << new->action.when;
- new->next = NULL;
- if (first_config == NULL)
- first_config = new;
- else
- last_config->next = new;
- last_config = new;
- return;
- process_config_line_err:
- msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg, line);
- }
- static int do_servicing(int fd, unsigned long event_mask)
- {
- ssize_t bytes;
- struct devfsd_notify_struct info;
-
- xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
- while (!caught_signal) {
- errno = 0;
- bytes = read(fd, (char *) &info, sizeof info);
- if (caught_signal)
- break;
- if (errno == EINTR)
- continue;
- if (bytes < 1)
- break;
- service_name(&info);
- }
- if (caught_signal) {
- int c_sighup = caught_sighup;
- caught_signal = FALSE;
- caught_sighup = FALSE;
- return c_sighup;
- }
- simple_msg_logger_and_die(LOG_ERR, "read error on control file");
- }
- static void service_name(const struct devfsd_notify_struct *info)
- {
- unsigned int n;
- regmatch_t mbuf[MAX_SUBEXPR];
- struct config_entry_struct *entry;
- if (ENABLE_DEBUG && info->overrun_count > 0)
- msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
-
- if (info->type == DEVFSD_NOTIFY_LOOKUP
- && ((info->devname[0] == 'l' && info->devname[1] == 'o'
- && info->devname[2] == 'g' && !info->devname[3])
- || (info->devname[0] == 'i' && info->devname[1] == 'n'
- && info->devname[2] == 'i' && info->devname[3] == 't'
- && info->devname[4] == 'c' && info->devname[5] == 't'
- && info->devname[6] == 'l' && !info->devname[7]))
- )
- return;
- for (entry = first_config; entry != NULL; entry = entry->next) {
-
- if (info->type != entry->action.when
- || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
- continue;
- for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
- ;
- switch (entry->action.what) {
- case AC_PERMISSIONS:
- action_permissions(info, entry);
- break;
- case AC_MODLOAD:
- if (ENABLE_DEVFSD_MODLOAD)
- action_modload(info, entry);
- break;
- case AC_EXECUTE:
- action_execute(info, entry, mbuf, n);
- break;
- case AC_COPY:
- action_copy(info, entry, mbuf, n);
- break;
- case AC_IGNORE:
- return;
-
- case AC_MKOLDCOMPAT:
- case AC_MKNEWCOMPAT:
- case AC_RMOLDCOMPAT:
- case AC_RMNEWCOMPAT:
- action_compat(info, entry->action.what);
- break;
- default:
- simple_msg_logger_and_die(LOG_ERR, "Unknown action");
- }
- }
- }
- static void action_permissions(const struct devfsd_notify_struct *info,
- const struct config_entry_struct *entry)
- {
- struct stat statbuf;
- if (stat(info->devname, &statbuf) != 0
- || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
- || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
- )
- error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
- }
- static void action_modload(const struct devfsd_notify_struct *info,
- const struct config_entry_struct *entry UNUSED_PARAM)
- {
- char *argv[6];
- argv[0] = (char*)MODPROBE;
- argv[1] = (char*)MODPROBE_SWITCH_1;
- argv[2] = (char*)MODPROBE_SWITCH_2;
- argv[3] = (char*)CONFIG_MODULES_DEVFS;
- argv[4] = concat_path_file("/dev", info->devname);
- argv[5] = NULL;
- spawn_and_wait(argv);
- free(argv[4]);
- }
- static void action_execute(const struct devfsd_notify_struct *info,
- const struct config_entry_struct *entry,
- const regmatch_t *regexpr, unsigned int numexpr)
- {
- unsigned int count;
- struct get_variable_info gv_info;
- char *argv[MAX_ARGS + 1];
- char largv[MAX_ARGS + 1][STRING_LENGTH];
- gv_info.info = info;
- gv_info.devname = info->devname;
- snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
- for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
- expand_expression(largv[count], STRING_LENGTH,
- entry->u.execute.argv[count],
- get_variable, &gv_info,
- gv_info.devname, regexpr, numexpr);
- argv[count] = largv[count];
- }
- argv[count] = NULL;
- spawn_and_wait(argv);
- }
- static void action_copy(const struct devfsd_notify_struct *info,
- const struct config_entry_struct *entry,
- const regmatch_t *regexpr, unsigned int numexpr)
- {
- mode_t new_mode;
- struct get_variable_info gv_info;
- struct stat source_stat, dest_stat;
- char source[STRING_LENGTH], destination[STRING_LENGTH];
- int ret = 0;
- dest_stat.st_mode = 0;
- if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
- return;
- gv_info.info = info;
- gv_info.devname = info->devname;
- snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
- expand_expression(source, STRING_LENGTH, entry->u.copy.source,
- get_variable, &gv_info, gv_info.devname,
- regexpr, numexpr);
- expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
- get_variable, &gv_info, gv_info.devname,
- regexpr, numexpr);
- if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
- return;
- lstat(destination, &dest_stat);
- new_mode = source_stat.st_mode & ~S_ISVTX;
- if (info->type == DEVFSD_NOTIFY_CREATE)
- new_mode |= S_ISVTX;
- else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
- new_mode |= S_ISVTX;
- ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
- if (ENABLE_DEBUG && ret && (errno != EEXIST))
- error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
- }
- static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
- {
- int ret;
- const char *compat_name = NULL;
- const char *dest_name = info->devname;
- const char *ptr;
- char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
- int mode, host, bus, target, lun;
- unsigned int i;
- char rewind_;
-
- static const char *const fmt[] = {
- NULL ,
- "sg/c%db%dt%du%d",
- "sd/c%db%dt%du%d",
- "sr/c%db%dt%du%d",
- "sd/c%db%dt%du%dp%d",
- "st/c%db%dt%du%dm%d%c",
- "ide/hd/c%db%dt%du%d",
- "ide/cd/c%db%dt%du%d",
- "ide/hd/c%db%dt%du%dp%d",
- "ide/mt/c%db%dt%du%d%s",
- NULL
- };
-
- switch (action) {
- case AC_MKOLDCOMPAT:
- case AC_RMOLDCOMPAT:
- compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
- break;
- case AC_MKNEWCOMPAT:
- case AC_RMNEWCOMPAT:
- ptr = bb_basename(info->devname);
- i = scan_dev_name(info->devname, info->namelen, ptr);
-
- if (i == 0 || i > 9)
- return;
- sscanf(info->devname + ((i < 6) ? 5 : 4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
- snprintf(dest_buf, sizeof(dest_buf), "../%s", info->devname + (( i > 5) ? 4 : 0));
- dest_name = dest_buf;
- compat_name = compat_buf;
-
- if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
- sprintf(compat_buf, fmt[i], host, bus, target, lun);
-
- if (i == 4 || i == 8)
- sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
-
- if (i == 5) {
- rewind_ = info->devname[info->namelen - 1];
- if (rewind_ != 'n')
- rewind_ = '\0';
- mode=0;
- if (ptr[2] == 'l' || ptr[2] == 'm')
- mode = ptr[2] - 107;
- if (ptr[2] == 'a')
- mode = 3;
- sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
- }
-
- if (i == 9)
- snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
-
- }
- if (compat_name == NULL)
- return;
-
- switch (action) {
- case AC_MKOLDCOMPAT:
- case AC_MKNEWCOMPAT:
- mksymlink(dest_name, compat_name);
- break;
- case AC_RMOLDCOMPAT:
- case AC_RMNEWCOMPAT:
- ret = unlink(compat_name);
- if (ENABLE_DEBUG && ret)
- error_logger(LOG_ERR, "unlink: %s", compat_name);
- break;
-
- }
- }
- static void restore(char *spath, struct stat source_stat, int rootlen)
- {
- char *dpath;
- struct stat dest_stat;
- dest_stat.st_mode = 0;
- dpath = concat_path_file(mount_point, spath + rootlen);
- lstat(dpath, &dest_stat);
- free(dpath);
- if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
- copy_inode(dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX), spath, &source_stat);
- if (S_ISDIR(source_stat.st_mode))
- dir_operation(RESTORE, spath, rootlen, NULL);
- }
- static int copy_inode(const char *destpath, const struct stat *dest_stat,
- mode_t new_mode,
- const char *sourcepath, const struct stat *source_stat)
- {
- int source_len, dest_len;
- char source_link[STRING_LENGTH], dest_link[STRING_LENGTH];
- int fd, val;
- struct sockaddr_un un_addr;
- char symlink_val[STRING_LENGTH];
- if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
-
- if (S_ISLNK(source_stat->st_mode)) {
- source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1);
- if ((source_len < 0)
- || (dest_len = readlink(destpath, dest_link, STRING_LENGTH - 1)) < 0
- )
- return FALSE;
- source_link[source_len] = '\0';
- dest_link[dest_len] = '\0';
- if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
- unlink(destpath);
- symlink(source_link, destpath);
- }
- return TRUE;
- }
- chmod(destpath, new_mode & ~S_IFMT);
- chown(destpath, source_stat->st_uid, source_stat->st_gid);
- return TRUE;
- }
-
- unlink(destpath);
- switch (source_stat->st_mode & S_IFMT) {
- case S_IFSOCK:
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
- if (fd < 0)
- break;
- un_addr.sun_family = AF_UNIX;
- snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
- val = bind(fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
- close(fd);
- if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
- break;
- goto do_chown;
- case S_IFLNK:
- val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1);
- if (val < 0)
- break;
- symlink_val[val] = '\0';
- if (symlink(symlink_val, destpath) == 0)
- return TRUE;
- break;
- case S_IFREG:
- fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT);
- if (fd < 0)
- break;
- close(fd);
- if (chmod(destpath, new_mode & ~S_IFMT) != 0)
- break;
- goto do_chown;
- case S_IFBLK:
- case S_IFCHR:
- case S_IFIFO:
- if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
- break;
- goto do_chown;
- case S_IFDIR:
- if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
- break;
- do_chown:
- if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
- return TRUE;
-
- }
- return FALSE;
- }
- static void free_config(void)
- {
- struct config_entry_struct *c_entry;
- void *next;
- for (c_entry = first_config; c_entry != NULL; c_entry = next) {
- unsigned int count;
- next = c_entry->next;
- regfree(&c_entry->preg);
- if (c_entry->action.what == AC_EXECUTE) {
- for (count = 0; count < MAX_ARGS; ++count) {
- if (c_entry->u.execute.argv[count] == NULL)
- break;
- free(c_entry->u.execute.argv[count]);
- }
- }
- free(c_entry);
- }
- first_config = NULL;
- last_config = NULL;
- }
- static int get_uid_gid(int flag, const char *string)
- {
- struct passwd *pw_ent;
- struct group *grp_ent;
- const char *msg;
- if (isdigit(string[0]) || ((string[0] == '-') && isdigit(string[1])))
- return atoi(string);
- if (flag == UID && (pw_ent = getpwnam(string)) != NULL)
- return pw_ent->pw_uid;
- if (ENABLE_DEVFSD_VERBOSE)
- msg = "user";
- if (flag == GID) {
- if ((grp_ent = getgrnam(string)) != NULL)
- return grp_ent->gr_gid;
- if (ENABLE_DEVFSD_VERBOSE)
- msg = "group";
- }
- if (ENABLE_DEVFSD_VERBOSE)
- msg_logger(LOG_ERR, "unknown %s: %s, defaulting to %cid=0", msg, string, msg[0]);
- return 0;
- }
- static mode_t get_mode(const char *string)
- {
- mode_t mode;
- int i;
- if (isdigit(string[0]))
- return strtoul(string, NULL, 8);
- if (strlen(string) != 9)
- msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
- mode = 0;
- i = S_IRUSR;
- while (i > 0) {
- if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
- mode += i;
- i = i / 2;
- string++;
- }
- return mode;
- }
- static void signal_handler(int sig)
- {
- caught_signal = TRUE;
- if (sig == SIGHUP)
- caught_sighup = TRUE;
- info_logger(LOG_INFO, "Caught signal %d", sig);
- }
- static const char *get_variable(const char *variable, void *info)
- {
- static char *hostname;
- struct get_variable_info *gv_info = info;
- const char *field_names[] = {
- "hostname", "mntpt", "devpath", "devname", "uid", "gid", "mode",
- NULL, mount_point, gv_info->devpath, gv_info->devname, NULL
- };
- int i;
- if (!hostname)
- hostname = safe_gethostname();
- field_names[7] = hostname;
-
- i = index_in_str_array(field_names, variable);
- if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
- return NULL;
- if (i >= 0 && i <= 3)
- return field_names[i + 7];
- if (i == 4)
- return auto_string(xasprintf("%u", gv_info->info->uid));
- if (i == 5)
- return auto_string(xasprintf("%u", gv_info->info->gid));
-
- return auto_string(xasprintf("%o", gv_info->info->mode));
- }
- static void service(struct stat statbuf, char *path)
- {
- struct devfsd_notify_struct info;
- memset(&info, 0, sizeof info);
- info.type = DEVFSD_NOTIFY_REGISTERED;
- info.mode = statbuf.st_mode;
- info.major = major(statbuf.st_rdev);
- info.minor = minor(statbuf.st_rdev);
- info.uid = statbuf.st_uid;
- info.gid = statbuf.st_gid;
- snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
- info.namelen = strlen(info.devname);
- service_name(&info);
- if (S_ISDIR(statbuf.st_mode))
- dir_operation(SERVICE, path, 0, NULL);
- }
- static void dir_operation(int type, const char * dir_name, int var, unsigned long *event_mask)
- {
- struct stat statbuf;
- DIR *dp;
- struct dirent *de;
- char *path;
- dp = warn_opendir(dir_name);
- if (dp == NULL)
- return;
- while ((de = readdir(dp)) != NULL) {
- if (de->d_name && DOT_OR_DOTDOT(de->d_name))
- continue;
- path = concat_path_file(dir_name, de->d_name);
- if (lstat(path, &statbuf) == 0) {
- switch (type) {
- case SERVICE:
- service(statbuf, path);
- break;
- case RESTORE:
- restore(path, statbuf, var);
- break;
- case READ_CONFIG:
- read_config_file(path, var, event_mask);
- break;
- }
- }
- free(path);
- }
- closedir(dp);
- }
- static int mksymlink(const char *oldpath, const char *newpath)
- {
- if (!make_dir_tree(newpath))
- return -1;
- if (symlink(oldpath, newpath) != 0) {
- if (errno != EEXIST)
- return -1;
- }
- return 0;
- }
- static int make_dir_tree(const char *path)
- {
- if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
- return FALSE;
- return TRUE;
- }
- static int expand_expression(char *output, unsigned int outsize,
- const char *input,
- const char *(*get_variable_func)(const char *variable, void *info),
- void *info,
- const char *devname,
- const regmatch_t *ex, unsigned int numexp)
- {
- char temp[STRING_LENGTH];
- if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
- return FALSE;
- expand_regexp(output, outsize, temp, devname, ex, numexp);
- return TRUE;
- }
- static void expand_regexp(char *output, size_t outsize, const char *input,
- const char *devname,
- const regmatch_t *ex, unsigned int numex)
- {
- const char last_exp = '0' - 1 + numex;
- int c = -1;
-
- if (outsize)
- output[--outsize] = '\0';
-
- while ((c != '\0') && (outsize != 0)) {
- c = *input;
- ++input;
- if (c == '\\') {
- c = *input;
- ++input;
- if (c != '\\') {
- if ((c >= '0') && (c <= last_exp)) {
- const regmatch_t *subexp = ex + (c - '0');
- unsigned int sublen = subexp->rm_eo - subexp->rm_so;
-
- if (sublen > outsize)
- sublen = outsize;
- strncpy(output, devname + subexp->rm_so, sublen);
- output += sublen;
- outsize -= sublen;
- }
- continue;
- }
- }
- *output = c;
- ++output;
- --outsize;
- }
- }
- struct translate_struct {
- const char *match;
- const char *format;
- };
- static struct translate_struct translate_table[] =
- {
- {"sound/", NULL},
- {"printers/", "lp%s"},
- {"v4l/", NULL},
- {"parports/", "parport%s"},
- {"fb/", "fb%s"},
- {"netlink/", NULL},
- {"loop/", "loop%s"},
- {"floppy/", "fd%s"},
- {"rd/", "ram%s"},
- {"md/", "md%s"},
- {"vc/", "tty%s"},
- {"misc/", NULL},
- {"isdn/", NULL},
- {"pg/", "pg%s"},
- {"i2c/", "i2c-%s"},
- {"staliomem/", "staliomem%s"},
- {"tts/E", "ttyE%s"},
- {"cua/E", "cue%s"},
- {"tts/R", "ttyR%s"},
- {"cua/R", "cur%s"},
- {"ip2/", "ip2%s"},
- {"tts/F", "ttyF%s"},
- {"cua/F", "cuf%s"},
- {"tts/C", "ttyC%s"},
- {"cua/C", "cub%s"},
- {"tts/", "ttyS%s"},
- {"cua/", "cua%s"},
- {"input/js", "js%s"},
- {NULL, NULL}
- };
- const char *get_old_name(const char *devname, unsigned int namelen,
- char *buffer, unsigned int major, unsigned int minor)
- {
- const char *compat_name = NULL;
- const char *ptr;
- struct translate_struct *trans;
- unsigned int i;
- char mode;
- int indexx;
- const char *pty1;
- const char *pty2;
-
- static const char *const fmt[] = {
- NULL ,
- "sg%u",
- NULL,
- "sr%u",
- NULL,
- "nst%u%c",
- "hd%c" ,
- "hd%c" ,
- "hd%c%s",
- "%sht%d",
- "sbpcd%u",
- "vcs%s",
- "%cty%c%c",
- NULL
- };
- for (trans = translate_table; trans->match != NULL; ++trans) {
- char *after_match = is_prefixed_with(devname, trans->match);
- if (after_match) {
- if (trans->format == NULL)
- return after_match;
- sprintf(buffer, trans->format, after_match);
- return buffer;
- }
- }
- ptr = bb_basename(devname);
- i = scan_dev_name(devname, namelen, ptr);
- if (i > 0 && i < 13)
- compat_name = buffer;
- else
- return NULL;
-
- if (i == 1 || i == 3 || i == 10)
- sprintf(buffer, fmt[i], minor);
-
- if (i == 2 || i == 4)
- compat_name = write_old_sd_name(buffer, major, minor, ((i == 2) ? "" : (ptr + 4)));
-
- if (i == 5) {
- mode = ptr[2];
- if (mode == 'n')
- mode = '\0';
- sprintf(buffer, fmt[i], minor & 0x1f, mode);
- if (devname[namelen - 1] != 'n')
- ++compat_name;
- }
-
- if (i == 6 || i == 7 || i == 8)
-
- sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
-
- if (i == 9)
- sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
-
- if (i == 11) {
- sprintf(buffer, fmt[i], devname + 4);
- if (buffer[3] == '0')
- buffer[3] = '\0';
- }
-
- if (i == 12) {
- pty1 = "pqrstuvwxyzabcde";
- pty2 = "0123456789abcdef";
- indexx = atoi(devname + 5);
- sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
- }
- return compat_name;
- }
- static char get_old_ide_name(unsigned int major, unsigned int minor)
- {
- char letter = 'y';
- char c = 'a';
- int i = IDE0_MAJOR;
-
- do {
- if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
- || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
- || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
- || i == IDE9_MAJOR
- ) {
- if ((unsigned int)i == major) {
- letter = c;
- break;
- }
- c += 2;
- }
- i++;
- } while (i <= IDE9_MAJOR);
- if (minor > 63)
- ++letter;
- return letter;
- }
- static char *write_old_sd_name(char *buffer,
- unsigned int major, unsigned int minor,
- const char *part)
- {
- unsigned int disc_index;
- if (major == 8) {
- sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
- return buffer;
- }
- if ((major > 64) && (major < 72)) {
- disc_index = ((major - 64) << 4) +(minor >> 4);
- if (disc_index < 26)
- sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
- else
- sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
- return buffer;
- }
- return NULL;
- }
- int st_expr_expand(char *output, unsigned int length, const char *input,
- const char *(*get_variable_func)(const char *variable,
- void *info),
- void *info)
- {
- char ch;
- unsigned int len;
- unsigned int out_pos = 0;
- const char *env;
- const char *ptr;
- struct passwd *pwent;
- char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
- if (length > BUFFER_SIZE)
- length = BUFFER_SIZE;
- for (; TRUE; ++input) {
- switch (ch = *input) {
- case '$':
-
- input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
- if (input == NULL)
- return FALSE;
- break;
- case '~':
-
- ch = input[1];
- if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
-
- env = getenv("HOME");
- if (env == NULL) {
- info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
- return FALSE;
- }
- len = strlen(env);
- if (len + out_pos >= length)
- goto st_expr_expand_out;
- memcpy(buffer + out_pos, env, len + 1);
- out_pos += len;
- continue;
- }
-
- for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
- ;
- len = ptr - input;
- if (len >= sizeof tmp)
- goto st_expr_expand_out;
- safe_memcpy(tmp, input, len);
- input = ptr - 1;
- pwent = getpwnam(tmp);
- if (pwent == NULL) {
- info_logger(LOG_INFO, "no pwent for: %s", tmp);
- return FALSE;
- }
- len = strlen(pwent->pw_dir);
- if (len + out_pos >= length)
- goto st_expr_expand_out;
- memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
- out_pos += len;
- break;
- case '\0':
-
- default:
- if (out_pos >= length)
- goto st_expr_expand_out;
- buffer[out_pos++] = ch;
- if (ch == '\0') {
- memcpy(output, buffer, out_pos);
- return TRUE;
- }
- break;
-
- }
- }
- return FALSE;
- st_expr_expand_out:
- simple_info_logger(LOG_INFO, bb_msg_small_buffer);
- return FALSE;
- }
- static const char *expand_variable(char *buffer, unsigned int length,
- unsigned int *out_pos, const char *input,
- const char *(*func)(const char *variable,
- void *info),
- void *info)
- {
- char ch;
- int len;
- unsigned int open_braces;
- const char *env, *ptr;
- char tmp[STRING_LENGTH];
- ch = input[0];
- if (ch == '$') {
-
- sprintf(tmp, "%d", (int) getpid());
- len = strlen(tmp);
- if (len + *out_pos >= length)
- goto expand_variable_out;
- memcpy(buffer + *out_pos, tmp, len + 1);
- out_pos += len;
- return input;
- }
-
- if (ch != '{') {
-
- for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
- ;
- len = ptr - input;
- if ((size_t)len >= sizeof tmp)
- goto expand_variable_out;
- safe_memcpy(tmp, input, len);
- input = ptr - 1;
- env = get_variable_v2(tmp, func, info);
- if (env == NULL) {
- info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
- return NULL;
- }
- len = strlen(env);
- if (len + *out_pos >= length)
- goto expand_variable_out;
- memcpy(buffer + *out_pos, env, len + 1);
- *out_pos += len;
- return input;
- }
-
- ch = *++input;
- for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
- ;
- if (ch == '}') {
-
- len = ptr - input;
- if ((size_t)len >= sizeof tmp)
- goto expand_variable_out;
- safe_memcpy(tmp, input, len);
- ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
- if (ptr == NULL)
- return NULL;
- return input + len;
- }
- if (ch != ':' || ptr[1] != '-') {
- simple_info_logger(LOG_INFO, "illegal char in var name");
- return NULL;
- }
-
- len = ptr - input;
- if ((size_t)len >= sizeof tmp)
- goto expand_variable_out;
- safe_memcpy(tmp, input, len);
-
- input = ptr;
-
- ptr += 2;
- ch = ptr[0];
- for (open_braces = 1; open_braces > 0; ch = *++ptr) {
- switch (ch) {
- case '{':
- ++open_braces;
- break;
- case '}':
- --open_braces;
- break;
- case '\0':
- info_logger(LOG_INFO, "\"}\" not found in: %s", input);
- return NULL;
- default:
- break;
- }
- }
- --ptr;
-
- env = get_variable_v2(tmp, func, info);
- if (env != NULL) {
-
- input = ptr;
- len = strlen(env);
- if (len + *out_pos >= length)
- goto expand_variable_out;
- memcpy(buffer + *out_pos, env, len + 1);
- *out_pos += len;
- return input;
- }
-
- input += 2;
- len = ptr - input;
- if ((size_t)len >= sizeof tmp)
- goto expand_variable_out;
- safe_memcpy(tmp, input, len);
- input = ptr;
- if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
- return NULL;
- len = strlen(tmp);
- if (len + *out_pos >= length)
- goto expand_variable_out;
- memcpy(buffer + *out_pos, tmp, len + 1);
- *out_pos += len;
- return input;
- expand_variable_out:
- simple_info_logger(LOG_INFO, bb_msg_small_buffer);
- return NULL;
- }
- static const char *get_variable_v2(const char *variable,
- const char *(*func)(const char *variable, void *info),
- void *info)
- {
- const char *value;
- if (func != NULL) {
- value = (*func)(variable, info);
- if (value != NULL)
- return value;
- }
- return getenv(variable);
- }
|