Ignore:
Timestamp:
Nov 6, 2007, 11:01:53 AM (16 years ago)
Author:
Bruno Cornec
Message:
  • Better output for mindi-busybox revision
  • Remove dummy file created on NFS - report from Arnaud Tiger <arnaud.tiger_at_hp.com>
  • strace useful for debug
  • fix new versions for pb (2.0.0 for mindi and 1.7.2 for mindi-busybox)
  • fix build process for mindi-busybox + options used in that version (dd for label-partitions-as-necessary)
  • fix typo in label-partitions-as-necessary which doesn't seem to work
  • Update to busybox 1.7.2
  • perl is now required at restore time to support uuid swap partitions (and will be used for many other thigs

in the future for sure)

  • next mindi version will be 2.0.0 due to all the changes made in it (udev may break working distros)
  • small optimization in mindi on keyboard handling (one single find instead of multiple)
  • better interaction for USB device when launching mindi manually
  • attempt to automatically guess block disk size for ramdisk
  • fix typos in bkphw
  • Fix the remaining problem with UUID support for swap partitions
  • Updates mondoarchive man page for USB support
  • Adds preliminary Hardware support to mindi (Proliant SSSTK)
  • Tries to add udev support also for rhel4
  • Fix UUID support which was still broken.
  • Be conservative in test for the start-nfs script
  • Update config file for mindi-busybox for 1.7.2 migration
  • Try to run around a busybox bug (1.2.2 pb on inexistant links)
  • Add build content for mindi-busybox in pb
  • Remove distributions content for mindi-busybox
  • Fix a warning on inexistant raidtab
  • Solve problem on tmpfs in restore init (Problem of inexistant symlink and busybox)
  • Create MONDO_CACHE and use it everywhere + creation at start
  • Really never try to eject a USB device
  • Fix a issue with &> usage (replaced with 1> and 2>)
  • Adds magic file to depllist in order to have file working + ldd which helps for debugging issues
  • tty modes correct to avoid sh error messages
  • Use ext3 normally and not ext2 instead
  • USB device should be corrected after reading (take 1st part)
  • Adds a mount_USB_here function derived from mount_CDROM_here
  • usb detection place before /dev detection in device name at restore time
  • Fix when restoring from USB: media is asked in interactive mode
  • Adds USB support for mondorestore
  • mount_cdrom => mount_media
  • elilo.efi is now searched throughout /boot/efi and not in a fixed place as there is no standard
  • untar-and-softlink => untar (+ interface change)
  • suppress useless softlinks creation/removal in boot process
  • avoids udevd messages on groups
  • Increase # of disks to 99 as in mindi at restore time (should be a conf file parameter)
  • skip existing big file creation
  • seems to work correctly for USB mindi boot
  • Adds group and tty link to udev conf
  • Always load usb-torage (even 2.6) to initiate USB bus discovery
  • Better printing of messages
  • Attempt to fix a bug in supporting OpenSusE 10.3 kernel for initramfs (mindi may now use multiple regex for kernel initrd detection)
  • Links were not correctly done as non relative for modules in mindi
  • exclusion of modules denied now works
  • Also create modules in their ordinary place, so that classical modprobe works + copy modules.dep
  • Fix bugs for DENY_MODS handling
  • Add device /dev/console for udev
  • ide-generic should now really be excluded
  • Fix a bug in major number for tty
  • If udev then adds modprobe/insmod to rootfs
  • tty0 is also cretaed with udev
  • ide-generic put rather in DENY_MODS
  • udevd remove from deplist s handled in mindi directly
  • better default for mindi when using --usb
  • Handles dynamically linked busybox (in case we want to use it soon ;-)
  • Adds fixed devices to create for udev
  • ide-generic should not be part of the initrd when using libata v2
  • support a dynamically linked udev (case on Ubuntu 7.10 and Mandriva 2008.0 so should be quite generic) This will give incitation to move to dyn. linked binaries in the initrd which will help for other tasks (ia6 4)
  • Improvement in udev support (do not use cl options not available in busybox)
  • Udev in mindi
    • auto creation of the right links at boot time with udev-links.conf(from Mandriva 2008.0)
    • rework startup of udev as current makes kernel crash (from Mandriva 2008.0)
    • add support for 64 bits udev
  • Try to render MyInsmod silent at boot time
  • Adds udev support (mandatory for newest distributions to avoid remapping of devices in a different way as on the original system)
  • We also need vaft format support for USB boot
  • Adds libusual support (Ubuntu 7.10 needs it for USB)
  • Improve Ubuntu/Debian keyboard detection and support
  • pbinit adapted to new pb (0.8.10). Filtering of docs done in it
  • Suppress some mondo warnings and errors on USB again
  • Tries to fix lack of files in deb mindi package
  • Verify should now work for USB devices
  • More log/mesages improvement for USB support
  • - Supress g_erase_tmpdir_and_scratchdir
  • Improve some log messages for USB support
  • Try to improve install in mindi to avoid issues with isolinux.cfg not installed vene if in the pkg :-(
  • Improve mindi-busybox build
  • In conformity with pb 0.8.9
  • Add support for Ubuntu 7.10 in build process
  • Add USB Key button to Menu UI (CD streamer removed)
  • Attempt to fix error messages on tmp/scratch files at the end by removing those dir at the latest possible.
  • Fix a bug linked to the size of the -E param which could be used (Arnaud Tiger/René Ribaud).
  • Integrate ~/.pbrc content into mondorescue.pb (required project-builder >= 0.8.7)
  • Put mondorescue in conformity with new pb filtering rules
  • Add USB support at restore time (no test done yet). New start-usb script PB varibale added where useful
  • Unmounting USB device before removal of temporary scratchdir
  • Stil refining USB copy back to mondo (one command was not executed)
  • No need to have the image subdor in the csratchdir when USB.
  • umount the USB partition before attempting to use it
  • Remove useless copy from mindi to mondo at end of USB handling

(risky merge, we are raising the limits of 2 diverging branches. The status of stable is not completely sure as such. Will need lots of tests, but it's not yet done :-()
(merge -r1692:1769 $SVN_M/branches/2.2.5)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/mindi-busybox/miscutils/devfsd.c

    r821 r1770  
     1/* vi: set sw=4 ts=4: */
     2/*
     3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     4 */
     5
    16/*
    27    devfsd implementation for busybox
     
    5055*/
    5156
    52 #include "busybox.h"
    53 #include "xregex.h"
    54 #include <unistd.h>
    55 #include <stdio.h>
    56 #include <stdlib.h>
    57 #include <stdarg.h>
    58 #include <string.h>
    59 #include <ctype.h>
    60 #include <sys/stat.h>
    61 #include <sys/types.h>
    62 #include <sys/wait.h>
    63 #include <sys/ioctl.h>
    64 #include <sys/socket.h>
     57//#include <sys/wait.h>
     58//#include <sys/ioctl.h>
     59//#include <sys/socket.h>
    6560#include <sys/un.h>
    6661#include <dirent.h>
    67 #include <fcntl.h>
    6862#include <syslog.h>
    69 #include <signal.h>
    70 #include <errno.h>
    7163#include <sys/sysmacros.h>
     64#include "libbb.h"
     65#include "xregex.h"
    7266
    7367
     
    165159struct permissions_type
    166160{
    167     mode_t mode;
    168     uid_t uid;
    169     gid_t gid;
     161    mode_t mode;
     162    uid_t uid;
     163    gid_t gid;
    170164};
    171165
    172166struct execute_type
    173167{
    174     char *argv[MAX_ARGS + 1];  /*  argv[0] must always be the programme  */
     168    char *argv[MAX_ARGS + 1];  /*  argv[0] must always be the programme  */
    175169};
    176170
    177171struct copy_type
    178172{
    179     const char *source;
    180     const char *destination;
     173    const char *source;
     174    const char *destination;
    181175};
    182176
    183177struct action_type
    184178{
    185     unsigned int what;
    186     unsigned int when;
     179    unsigned int what;
     180    unsigned int when;
    187181};
    188182
    189183struct config_entry_struct
    190184{
    191     struct action_type action;
    192     regex_t preg;
    193     union
    194     {
     185    struct action_type action;
     186    regex_t preg;
     187    union
     188    {
    195189    struct permissions_type permissions;
    196190    struct execute_type execute;
    197191    struct copy_type copy;
    198     }
    199     u;
    200     struct config_entry_struct *next;
     192    }
     193    u;
     194    struct config_entry_struct *next;
    201195};
    202196
    203197struct get_variable_info
    204198{
    205     const struct devfsd_notify_struct *info;
    206     const char *devname;
    207     char devpath[STRING_LENGTH];
     199    const struct devfsd_notify_struct *info;
     200    const char *devname;
     201    char devpath[STRING_LENGTH];
    208202};
    209203
    210 static void dir_operation(int , const char * , int,  unsigned long* );
     204static void dir_operation(int , const char * , int,  unsigned long*);
    211205static void service(struct stat statbuf, char *path);
    212 static int st_expr_expand(char *, unsigned, const char *, const char *(*) (const char *, void *), void *);
     206static int st_expr_expand(char *, unsigned, const char *, const char *(*)(const char *, void *), void *);
    213207static const char *get_old_name(const char *, unsigned, char *, unsigned, unsigned);
    214 static int mksymlink (const char *oldpath, const char *newpath);
    215 static void read_config_file (char *path, int optional, unsigned long *event_mask);
    216 static void process_config_line (const char *, unsigned long *);
    217 static int  do_servicing (int, unsigned long);
    218 static void service_name (const struct devfsd_notify_struct *);
    219 static void action_permissions (const struct devfsd_notify_struct *, const struct config_entry_struct *);
    220 static void action_execute (const struct devfsd_notify_struct *, const struct config_entry_struct *,
     208static int mksymlink(const char *oldpath, const char *newpath);
     209static void read_config_file(char *path, int optional, unsigned long *event_mask);
     210static void process_config_line(const char *, unsigned long *);
     211static int  do_servicing(int, unsigned long);
     212static void service_name(const struct devfsd_notify_struct *);
     213static void action_permissions(const struct devfsd_notify_struct *, const struct config_entry_struct *);
     214static void action_execute(const struct devfsd_notify_struct *, const struct config_entry_struct *,
    221215                            const regmatch_t *, unsigned);
    222 static void action_modload (const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
    223 static void action_copy (const struct devfsd_notify_struct *, const struct config_entry_struct *,
     216static void action_modload(const struct devfsd_notify_struct *info, const struct config_entry_struct *entry);
     217static void action_copy(const struct devfsd_notify_struct *, const struct config_entry_struct *,
    224218                         const regmatch_t *, unsigned);
    225 static void action_compat (const struct devfsd_notify_struct *, unsigned);
    226 static void free_config (void);
     219static void action_compat(const struct devfsd_notify_struct *, unsigned);
     220static void free_config(void);
    227221static void restore(char *spath, struct stat source_stat, int rootlen);
    228 static int copy_inode (const char *, const struct stat *, mode_t, const char *, const struct stat *);
    229 static mode_t get_mode (const char *);
    230 static void signal_handler (int);
    231 static const char *get_variable (const char *, void *);
    232 static int make_dir_tree (const char *);
     222static int copy_inode(const char *, const struct stat *, mode_t, const char *, const struct stat *);
     223static mode_t get_mode(const char *);
     224static void signal_handler(int);
     225static const char *get_variable(const char *, void *);
     226static int make_dir_tree(const char *);
    233227static int expand_expression(char *, unsigned, const char *, const char *(*)(const char *, void *), void *,
    234                              const char *, const regmatch_t *, unsigned );
    235 static void expand_regexp (char *, size_t, const char *, const char *, const regmatch_t *, unsigned );
     228                             const char *, const regmatch_t *, unsigned);
     229static void expand_regexp(char *, size_t, const char *, const char *, const regmatch_t *, unsigned);
    236230static const char *expand_variable( char *, unsigned, unsigned *, const char *,
    237                                     const char *(*) (const char *, void *), void * );
    238 static const char *get_variable_v2(const char *, const char *(*) (const char *, void *), void *);
    239 static char get_old_ide_name (unsigned , unsigned);
    240 static char *write_old_sd_name (char *, unsigned, unsigned, char *);
     231                                    const char *(*)(const char *, void *), void *);
     232static const char *get_variable_v2(const char *, const char *(*)(const char *, void *), void *);
     233static char get_old_ide_name(unsigned , unsigned);
     234static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
    241235
    242236/* busybox functions */
    243 static void msg_logger(int pri, const char * fmt, ... )__attribute__ ((format (printf, 2, 3)));
    244 static void msg_logger_and_die(int pri, const char * fmt, ... )__attribute__ ((noreturn, format (printf, 2, 3)));
    245 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
    246 static void fork_and_execute(int die, char *arg0, char **arg );
    247 static int get_uid_gid ( int, const char *);
    248 static void safe_memcpy( char * dest, const char * src, int len);
    249 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr);
    250 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr);
     237static int get_uid_gid(int flag, const char *string);
     238static void safe_memcpy(char * dest, const char * src, int len);
     239static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr);
     240static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr);
    251241
    252242/* Structs and vars */
    253243static struct config_entry_struct *first_config = NULL;
    254244static struct config_entry_struct *last_config = NULL;
    255 static const char *mount_point = NULL;
     245static char *mount_point = NULL;
    256246static volatile int caught_signal = FALSE;
    257247static volatile int caught_sighup = FALSE;
    258 static struct initial_symlink_struct
    259 {
    260     char *dest;
    261     char *name;
    262 } initial_symlinks[] =
    263 {
    264     {"/proc/self/fd", "fd"},
    265     {"fd/0", "stdin"},
    266     {"fd/1", "stdout"},
    267     {"fd/2", "stderr"},
    268     {NULL, NULL},
     248static struct initial_symlink_struct {
     249    const char *dest;
     250    const char *name;
     251} initial_symlinks[] = {
     252    {"/proc/self/fd", "fd"},
     253    {"fd/0", "stdin"},
     254    {"fd/1", "stdout"},
     255    {"fd/2", "stderr"},
     256    {NULL, NULL},
    269257};
    270258
    271 static struct event_type
    272 {
    273     unsigned int type;        /*  The DEVFSD_NOTIFY_* value                  */
    274     const char *config_name;  /*  The name used in the config file           */
    275 } event_types[] =
    276 {
    277     {DEVFSD_NOTIFY_REGISTERED,   "REGISTER"},
    278     {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
    279     {DEVFSD_NOTIFY_ASYNC_OPEN,   "ASYNC_OPEN"},
    280     {DEVFSD_NOTIFY_CLOSE,        "CLOSE"},
    281     {DEVFSD_NOTIFY_LOOKUP,       "LOOKUP"},
    282     {DEVFSD_NOTIFY_CHANGE,       "CHANGE"},
    283     {DEVFSD_NOTIFY_CREATE,       "CREATE"},
    284     {DEVFSD_NOTIFY_DELETE,       "DELETE"},
    285     {0xffffffff,                 NULL}
     259static struct event_type {
     260    unsigned int type;        /*  The DEVFSD_NOTIFY_* value                  */
     261    const char *config_name;  /*  The name used in the config file           */
     262} event_types[] = {
     263    {DEVFSD_NOTIFY_REGISTERED,   "REGISTER"},
     264    {DEVFSD_NOTIFY_UNREGISTERED, "UNREGISTER"},
     265    {DEVFSD_NOTIFY_ASYNC_OPEN,   "ASYNC_OPEN"},
     266    {DEVFSD_NOTIFY_CLOSE,        "CLOSE"},
     267    {DEVFSD_NOTIFY_LOOKUP,       "LOOKUP"},
     268    {DEVFSD_NOTIFY_CHANGE,       "CHANGE"},
     269    {DEVFSD_NOTIFY_CREATE,       "CREATE"},
     270    {DEVFSD_NOTIFY_DELETE,       "DELETE"},
     271    {0xffffffff,                 NULL}
    286272};
    287273
    288274/* Busybox messages */
    289275
    290 static const char * const bb_msg_proto_rev          = "protocol revision";
    291 static const char * const bb_msg_bad_config     = "bad %s config file: %s";
    292 static const char * const bb_msg_small_buffer       = "buffer too small";
    293 static const char * const bb_msg_variable_not_found = "variable: %s not found";
    294 
    295 /* Busybox functions  */
    296 static void msg_logger(int pri, const char * fmt, ... )
    297 {
    298     va_list ap;
    299     int ret;
    300 
    301     va_start(ap, fmt);
    302     ret = access ("/dev/log", F_OK);
    303     if (ret == 0) {
    304         openlog(bb_applet_name, 0, LOG_DAEMON);
    305         vsyslog( pri , fmt, ap);
    306         /* Man: A trailing newline is added when needed. */
    307         closelog();
    308     }
    309     /* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
    310     if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
    311         bb_error_msg(fmt, ap);
    312     }
    313     va_end(ap);
    314 }
    315 
    316 static void msg_logger_and_die(int pri, const char* fmt, ...)
    317 {
    318     va_list ap;
    319 
    320     va_start(ap, fmt);
    321     msg_logger(pri, fmt, ap);
    322     va_end(ap);
    323     exit(EXIT_FAILURE);
    324 }
     276static const char bb_msg_proto_rev[] ALIGN1          = "protocol revision";
     277static const char bb_msg_bad_config[] ALIGN1         = "bad %s config file: %s";
     278static const char bb_msg_small_buffer[] ALIGN1       = "buffer too small";
     279static const char bb_msg_variable_not_found[] ALIGN1 = "variable: %s not found";
    325280
    326281/* Busybox stuff */
    327 #if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
    328 #define devfsd_error_msg(fmt, args...)                bb_error_msg(fmt, ## args)
    329 #define devfsd_perror_msg_and_die(fmt, args...)       bb_perror_msg_and_die(fmt, ## args)
    330 #define devfsd_error_msg_and_die(fmt, args...)        bb_error_msg_and_die(fmt, ## args)
    331 #if defined(CONFIG_DEBUG)
    332 #define debug_msg_logger(x, fmt, args...)             msg_logger(x, fmt, ## args)
     282#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
     283#define info_logger(p, fmt, args...)                 bb_info_msg(fmt, ## args)
     284#define msg_logger(p, fmt, args...)                  bb_error_msg(fmt, ## args)
     285#define msg_logger_and_die(p, fmt, args...)          bb_error_msg_and_die(fmt, ## args)
     286#define error_logger(p, fmt, args...)                bb_perror_msg(fmt, ## args)
     287#define error_logger_and_die(p, fmt, args...)        bb_perror_msg_and_die(fmt, ## args)
    333288#else
    334 #define debug_msg_logger(x, fmt, args...)
    335 #endif
    336 #else
    337 #define debug_msg_logger(x, fmt, args...)
     289#define info_logger(p, fmt, args...)
    338290#define msg_logger(p, fmt, args...)
    339291#define msg_logger_and_die(p, fmt, args...)           exit(1)
    340 #define devfsd_perror_msg_and_die(fmt, args...)       exit(1)
    341 #define devfsd_error_msg_and_die(fmt, args...)        exit(1)
    342 #define devfsd_error_msg(fmt, args...)
     292#define error_logger(p, fmt, args...)
     293#define error_logger_and_die(p, fmt, args...)         exit(1)
    343294#endif
    344295
    345 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
    346 {
    347     if (ioctl (fd, request, event_mask_flag) == -1)
    348         msg_logger_and_die(LOG_ERR, "ioctl");
    349 }
    350 
    351 static void fork_and_execute(int die, char *arg0, char **arg )
    352 {
    353     switch ( fork () )
    354     {
    355     case 0:
    356         /*  Child  */
    357         break;
    358     case -1:
    359         /*  Parent: Error  : die or return */
    360         msg_logger(LOG_ERR,(char *) bb_msg_memory_exhausted);
    361         if(die)
    362             exit(EXIT_FAILURE);
    363         return;
    364     default:
    365         /*  Parent : ok : return or exit  */
    366         if(arg0 != NULL)
    367         {
    368             wait (NULL);
    369             return;
    370         }
    371         exit (EXIT_SUCCESS);
    372     }
    373      /* Child : if arg0 != NULL do execvp */
    374     if(arg0 != NULL )
    375     {
    376         execvp (arg0, arg);
    377         msg_logger_and_die(LOG_ERR, "execvp");
    378     }
    379 }
    380 
    381 static void safe_memcpy( char *dest, const char *src, int len)
    382 {
    383     memcpy (dest , src , len );
     296static void safe_memcpy(char *dest, const char *src, int len)
     297{
     298    memcpy(dest , src, len);
    384299    dest[len] = '\0';
    385300}
    386301
    387 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, char *ptr)
    388 {
    389     if( d[n - 4]=='d' && d[n - 3]=='i' && d[n - 2]=='s' && d[n - 1]=='c')
    390         return (2 + addendum);
    391     else if( d[n - 2]=='c' && d[n - 1]=='d')
    392         return (3 + addendum);
    393     else if(ptr[0]=='p' && ptr[1]=='a' && ptr[2]=='r' && ptr[3]=='t')
    394         return (4 + addendum);
    395     else if( ptr[n - 2]=='m' && ptr[n - 1]=='t')
    396         return (5 + addendum);
    397     else
    398         return 0;
     302static unsigned int scan_dev_name_common(const char *d, unsigned int n, int addendum, const char *ptr)
     303{
     304    if (d[n - 4] == 'd' && d[n - 3] == 'i' && d[n - 2] == 's' && d[n - 1] == 'c')
     305        return 2 + addendum;
     306    if (d[n - 2] == 'c' && d[n - 1] == 'd')
     307        return 3 + addendum;
     308    if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 'r' && ptr[3] == 't')
     309        return 4 + addendum;
     310    if (ptr[n - 2] == 'm' && ptr[n - 1] == 't')
     311        return 5 + addendum;
     312    return 0;
    399313}
    400314
    401 static unsigned int scan_dev_name(const char *d, unsigned int n, char *ptr)
    402 {
    403     if(d[0]=='s' && d[1]=='c' && d[2]=='s' && d[3]=='i' && d[4]=='/')
    404     {
    405         if( d[n - 7]=='g' && d[n - 6]=='e' && d[n - 5]=='n' &&
    406             d[n - 4]=='e' && d[n - 3]=='r' && d[n - 2]=='i' &&
    407             d[n - 1]=='c' )
     315static unsigned int scan_dev_name(const char *d, unsigned int n, const char *ptr)
     316{
     317    if (d[0] == 's' && d[1] == 'c' && d[2] == 's' && d[3] == 'i' && d[4] == '/') {
     318        if (d[n - 7] == 'g' && d[n - 6] == 'e' && d[n - 5] == 'n'
     319            && d[n - 4] == 'e' && d[n - 3] == 'r' && d[n - 2] == 'i' && d[n - 1] == 'c'
     320        )
    408321            return 1;
    409322        return scan_dev_name_common(d, n, 0, ptr);
    410323    }
    411     else if(d[0]=='i' && d[1]=='d' && d[2]=='e' && d[3]=='/' &&
    412             d[4]=='h' && d[5]=='o' && d[6]=='s' && d[7]=='t')
    413     {
     324    if (d[0] == 'i' && d[1] == 'd' && d[2] == 'e' && d[3] == '/'
     325        && d[4] == 'h' && d[5] == 'o' && d[6] == 's' && d[7] == 't'
     326    )
    414327        return scan_dev_name_common(d, n, 4, ptr);
    415     }
    416     else if(d[0]=='s' && d[1]=='b' && d[2]=='p' && d[3]=='/')
    417     {
     328    if (d[0] == 's' && d[1] == 'b' && d[2] == 'p' && d[3] == '/')
    418329        return 10;
    419     }
    420     else if(d[0]=='v' && d[1]=='c' && d[2]=='c' && d[3]=='/')
    421     {
     330    if (d[0] == 'v' && d[1] == 'c' && d[2] == 'c' && d[3] == '/')
    422331        return 11;
    423     }
    424     else if(d[0]=='p' && d[1]=='t' && d[2]=='y' && d[3]=='/')
    425     {
     332    if (d[0] == 'p' && d[1] == 't' && d[2] == 'y' && d[3] == '/')
    426333        return 12;
    427     }
    428334    return 0;
    429335}
     
    431337/*  Public functions follow  */
    432338
    433 int devfsd_main (int argc, char **argv)
     339int devfsd_main(int argc, char **argv);
     340int devfsd_main(int argc, char **argv)
    434341{
    435342    int print_version = FALSE;
     
    445352        bb_show_usage();
    446353
    447     for (count = 2; count < argc; ++count)
    448     {
    449         if(argv[count][0] == '-')
    450         {
    451             if(argv[count][1]=='v' && !argv[count][2]) /* -v */
    452                     print_version = TRUE;
    453             else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='f'
    454                     && argv[count][2]=='g' && !argv[count][3]) /* -fg */
    455                     do_daemon = FALSE;
    456             else if(ENABLE_DEVFSD_FG_NP && argv[count][1]=='n'
    457                     && argv[count][2]=='p' && !argv[count][3]) /* -np */
    458                     no_polling = TRUE;
     354    for (count = 2; count < argc; ++count) {
     355        if (argv[count][0] == '-') {
     356            if (argv[count][1] == 'v' && !argv[count][2]) /* -v */
     357                print_version = TRUE;
     358            else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'f'
     359             && argv[count][2] == 'g' && !argv[count][3]) /* -fg */
     360                do_daemon = FALSE;
     361            else if (ENABLE_DEVFSD_FG_NP && argv[count][1] == 'n'
     362             && argv[count][2] == 'p' && !argv[count][3]) /* -np */
     363                no_polling = TRUE;
    459364            else
    460365                bb_show_usage();
     
    462367    }
    463368
    464     /* strip last / from mount point, so we don't need to check for it later */
    465     while( argv[1][1]!='\0' && argv[1][strlen(argv[1])-1] == '/' )
    466         argv[1][strlen(argv[1]) -1] = '\0';
    467 
    468     mount_point = argv[1];
    469 
    470     if (chdir (mount_point) != 0)
    471         devfsd_perror_msg_and_die(mount_point);
    472 
    473     fd = bb_xopen (".devfsd", O_RDONLY);
    474 
    475     if (fcntl (fd, F_SETFD, FD_CLOEXEC) != 0)
    476         devfsd_perror_msg_and_die("FD_CLOEXEC");
    477 
    478     if (ioctl (fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
    479         msg_logger_and_die(LOG_ERR, "ioctl");
     369    mount_point = bb_simplify_path(argv[1]);
     370
     371    xchdir(mount_point);
     372
     373    fd = xopen(".devfsd", O_RDONLY);
     374
     375    if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
     376        bb_perror_msg_and_die("FD_CLOEXEC");
     377
     378    xioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev);
    480379
    481380    /*setup initial entries */
    482     for (curr = initial_symlinks; curr->dest != NULL; ++curr)
    483         symlink (curr->dest, curr->name);
     381    for (curr = initial_symlinks; curr->dest != NULL; ++curr)
     382        symlink(curr->dest, curr->name);
    484383
    485384    /* NB: The check for CONFIG_FILE is done in read_config_file() */
    486385
    487     if ( print_version  || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev) )
    488     {
    489         bb_printf( "%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
    490                      bb_applet_name,DEVFSD_VERSION,bb_msg_proto_rev,
    491                      DEVFSD_PROTOCOL_REVISION_DAEMON,bb_msg_proto_rev, proto_rev);
     386    if (print_version || (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)) {
     387        printf("%s v%s\nDaemon %s:\t%d\nKernel-side %s:\t%d\n",
     388                applet_name, DEVFSD_VERSION, bb_msg_proto_rev,
     389                DEVFSD_PROTOCOL_REVISION_DAEMON, bb_msg_proto_rev, proto_rev);
    492390        if (DEVFSD_PROTOCOL_REVISION_DAEMON != proto_rev)
    493             bb_error_msg_and_die( "%s mismatch!",bb_msg_proto_rev);
     391            bb_error_msg_and_die("%s mismatch!", bb_msg_proto_rev);
    494392        exit(EXIT_SUCCESS); /* -v */
    495393    }
    496     /*  Tell kernel we are special (i.e. we get to see hidden entries)  */
    497     do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
    498 
    499     sigemptyset (&new_action.sa_mask);
     394    /*  Tell kernel we are special(i.e. we get to see hidden entries)  */
     395    xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, 0);
     396
     397    sigemptyset(&new_action.sa_mask);
    500398    new_action.sa_flags = 0;
    501399
    502400    /*  Set up SIGHUP and SIGUSR1 handlers  */
    503401    new_action.sa_handler = signal_handler;
    504     if (sigaction (SIGHUP, &new_action, NULL) != 0 || sigaction (SIGUSR1, &new_action, NULL) != 0 )
    505         devfsd_error_msg_and_die( "sigaction");
    506 
    507     bb_printf("%s v%s  started for %s\n",bb_applet_name, DEVFSD_VERSION, mount_point);
     402    if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, NULL) != 0)
     403        bb_error_msg_and_die("sigaction");
     404
     405    printf("%s v%s  started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
    508406
    509407    /*  Set umask so that mknod(2), open(2) and mkdir(2) have complete control over permissions  */
    510     umask (0);
    511     read_config_file (CONFIG_FILE, FALSE, &event_mask);
     408    umask(0);
     409    read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
    512410    /*  Do the scan before forking, so that boot scripts see the finished product  */
    513     dir_operation(SERVICE,mount_point,0,NULL);
     411    dir_operation(SERVICE, mount_point, 0, NULL);
    514412
    515413    if (ENABLE_DEVFSD_FG_NP && no_polling)
    516         exit (0);
    517     if (do_daemon)
    518     {
     414        exit(0);
     415
     416    if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
     417        logmode = LOGMODE_BOTH;
     418    else if (do_daemon == TRUE)
     419        logmode = LOGMODE_SYSLOG;
     420    /* This is the default */
     421    /*else
     422        logmode = LOGMODE_STDIO; */
     423
     424    if (do_daemon) {
    519425        /*  Release so that the child can grab it  */
    520         do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
    521         fork_and_execute(DIE, NULL, NULL);
    522         setsid ();        /*  Prevent hangups and become pgrp leader         */
    523     } else if(ENABLE_DEVFSD_FG_NP) {
    524         setpgid (0, 0);  /*  Become process group leader                    */
    525     }
    526 
    527     while (TRUE)
    528     {
    529         do_scan = do_servicing (fd, event_mask);
    530 
    531         free_config ();
    532         read_config_file (CONFIG_FILE, FALSE, &event_mask);
     426        xioctl(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
     427        bb_daemonize_or_rexec(0, argv);
     428    } else if (ENABLE_DEVFSD_FG_NP) {
     429        setpgid(0, 0);  /*  Become process group leader                    */
     430    }
     431
     432    while (TRUE) {
     433        do_scan = do_servicing(fd, event_mask);
     434
     435        free_config();
     436        read_config_file((char*)CONFIG_FILE, FALSE, &event_mask);
    533437        if (do_scan)
    534             dir_operation(SERVICE,mount_point,0,NULL);
    535     }
     438            dir_operation(SERVICE, mount_point, 0, NULL);
     439    }
     440    if (ENABLE_FEATURE_CLEAN_UP) free(mount_point);
    536441}   /*  End Function main  */
    537442
     
    539444/*  Private functions follow  */
    540445
    541 static void read_config_file (char *path, int optional, unsigned long *event_mask)
     446static void read_config_file(char *path, int optional, unsigned long *event_mask)
    542447/*  [SUMMARY] Read a configuration database.
    543448    <path> The path to read the database from. If this is a directory, all
    544     entries in that directory will be read (except hidden entries).
     449    entries in that directory will be read(except hidden entries).
    545450    <optional> If TRUE, the routine will silently ignore a missing config file.
    546451    <event_mask> The event mask is written here. This is not initialised.
     
    551456    FILE *fp;
    552457    char buf[STRING_LENGTH];
    553     char *line=NULL;
    554 
    555     debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, path);
    556 
    557     if (stat (path, &statbuf) == 0 )
    558     {
     458    char *line = NULL;
     459    char *p;
     460
     461    if (stat(path, &statbuf) == 0) {
    559462        /* Don't read 0 length files: ignored */
    560         /*if( statbuf.st_size == 0 )
     463        /*if (statbuf.st_size == 0)
    561464                return;*/
    562         if ( S_ISDIR (statbuf.st_mode) )
    563         {
    564             /* strip last / from dirname so we don't need to check for it later */
    565             while( path  && path[1]!='\0' && path[strlen(path)-1] == '/')
    566                 path[strlen(path) -1] = '\0';
    567 
    568             dir_operation(READ_CONFIG, path, 0, event_mask);
     465        if (S_ISDIR(statbuf.st_mode)) {
     466            p = bb_simplify_path(path);
     467            dir_operation(READ_CONFIG, p, 0, event_mask);
     468            free(p);
    569469            return;
    570470        }
    571         if ( ( fp = fopen (path, "r") ) != NULL )
    572         {
    573             while (fgets (buf, STRING_LENGTH, fp) != NULL)
    574             {
     471        if ((fp = fopen(path, "r")) != NULL) {
     472            while (fgets(buf, STRING_LENGTH, fp) != NULL) {
    575473                /*  Skip whitespace  */
    576                 for (line = buf; isspace (*line); ++line)
    577                     /*VOID*/;
    578                 if (line[0] == '\0' || line[0] == '#' )
     474                line = buf;
     475                line = skip_whitespace(line);
     476                if (line[0] == '\0' || line[0] == '#')
    579477                    continue;
    580                 process_config_line (line, event_mask);
     478                process_config_line(line, event_mask);
    581479            }
    582             fclose (fp);
     480            fclose(fp);
    583481        } else {
    584482            goto read_config_file_err;
     
    586484    } else {
    587485read_config_file_err:
    588     if(optional ==  0  && errno == ENOENT)
    589         msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
    590     }
    591     return;
     486        if (optional == 0 && errno == ENOENT)
     487            error_logger_and_die(LOG_ERR, "read config file: %s", path);
     488    }
    592489}   /*  End Function read_config_file   */
    593490
    594 static void process_config_line (const char *line, unsigned long *event_mask)
     491static void process_config_line(const char *line, unsigned long *event_mask)
    595492/*  [SUMMARY] Process a line from a configuration file.
    596493    <line> The configuration line.
     
    604501    char when[STRING_LENGTH], what[STRING_LENGTH];
    605502    char name[STRING_LENGTH];
    606     char * msg="";
     503    const char *msg = "";
    607504    char *ptr;
    608505    int i;
    609506
    610507    /* !!!! Only Uppercase Keywords in devsfd.conf */
    611     static const char *const options[] = {
    612         "CLEAR_CONFIG", "INCLUDE", "OPTIONAL_INCLUDE",
    613         "RESTORE", "PERMISSIONS", "MODLOAD", "EXECUTE",
    614         "COPY", "IGNORE", "MKOLDCOMPAT", "MKNEWCOMPAT",
    615         "RMOLDCOMPAT", "RMNEWCOMPAT", 0
    616     };
    617 
    618     debug_msg_logger(LOG_INFO, __FUNCTION__);
    619 
    620     for (count = 0; count < MAX_ARGS; ++count) p[count][0] = '\0';
    621     num_args = sscanf (line, "%s %s %s %s %s %s %s %s %s %s",
     508    static const char options[] ALIGN1 =
     509        "CLEAR_CONFIG\0""INCLUDE\0""OPTIONAL_INCLUDE\0"
     510        "RESTORE\0""PERMISSIONS\0""MODLOAD\0""EXECUTE\0"
     511        "COPY\0""IGNORE\0""MKOLDCOMPAT\0""MKNEWCOMPAT\0"
     512        "RMOLDCOMPAT\0""RMNEWCOMPAT\0";
     513
     514    for (count = 0; count < MAX_ARGS; ++count)
     515        p[count][0] = '\0';
     516    num_args = sscanf(line, "%s %s %s %s %s %s %s %s %s %s",
    622517            when, name, what,
    623518            p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
    624519
    625     i = compare_string_array(options, when );
    626 
    627     /*"CLEAR_CONFIG"*/
    628     if( i == 0)
    629     {
    630         free_config ();
     520    i = index_in_strings(options, when);
     521
     522    /* "CLEAR_CONFIG" */
     523    if (i == 0) {
     524        free_config();
    631525        *event_mask = 0;
    632526        return;
    633527    }
    634528
    635     if ( num_args < 2)
     529    if (num_args < 2)
    636530        goto process_config_line_err;
    637531
    638532    /* "INCLUDE" & "OPTIONAL_INCLUDE" */
    639     if( i == 1 || i == 2 )
    640     {
    641         st_expr_expand (name, STRING_LENGTH, name, get_variable, NULL );
    642         msg_logger(LOG_INFO, "%sinclude: %s",(toupper (when[0]) == 'I') ? "": "optional_", name);
    643         read_config_file (name, (toupper (when[0]) == 'I') ? FALSE : TRUE, event_mask);
     533    if (i == 1 || i == 2) {
     534        st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
     535        info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", name);
     536        read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, event_mask);
    644537        return;
    645538    }
    646539    /* "RESTORE" */
    647     if( i == 3)
    648     {
    649         dir_operation(RESTORE,name, strlen (name),NULL);
     540    if (i == 3) {
     541        dir_operation(RESTORE, name, strlen(name),NULL);
    650542        return;
    651543    }
     
    653545        goto process_config_line_err;
    654546
    655     new = xmalloc (sizeof *new);
    656     memset (new, 0, sizeof *new);
    657 
    658     for (count = 0; event_types[count].config_name != NULL; ++count)
    659     {
    660         if (strcasecmp (when, event_types[count].config_name) != 0)
     547    new = xzalloc(sizeof *new);
     548
     549    for (count = 0; event_types[count].config_name != NULL; ++count) {
     550        if (strcasecmp(when, event_types[count].config_name) != 0)
    661551            continue;
    662552        new->action.when = event_types[count].type;
    663553        break;
    664554    }
    665     if (event_types[count].config_name == NULL)
    666     {
    667         msg="WHEN in";
     555    if (event_types[count].config_name == NULL) {
     556        msg = "WHEN in";
    668557        goto process_config_line_err;
    669558    }
    670559
    671     i = compare_string_array(options, what );
    672 
    673     switch(i)
    674     {
     560    i = index_in_strings(options, what);
     561
     562    switch (i) {
    675563        case 4: /* "PERMISSIONS" */
    676564            new->action.what = AC_PERMISSIONS;
    677565            /*  Get user and group  */
    678             if ( ( ptr = strchr (p[0], '.') ) == NULL )
    679             {
    680                 msg="UID.GID";
     566            if ((ptr = strchr(p[0], '.')) == NULL) {
     567                msg = "UID.GID";
    681568                goto process_config_line_err; /*"missing '.' in UID.GID"*/
    682569            }
    683570
    684571            *ptr++ = '\0';
    685             new->u.permissions.uid = get_uid_gid (UID, p[0]);
    686             new->u.permissions.gid = get_uid_gid (GID, ptr);
     572            new->u.permissions.uid = get_uid_gid(UID, p[0]);
     573            new->u.permissions.gid = get_uid_gid(GID, ptr);
    687574            /*  Get mode  */
    688             new->u.permissions.mode = get_mode (p[1]);
     575            new->u.permissions.mode = get_mode(p[1]);
    689576            break;
    690577        case 5: /*  MODLOAD */
    691             /*This  action will pass "/dev/$devname" (i.e. "/dev/" prefixed to
     578            /*This  action will pass "/dev/$devname"(i.e. "/dev/" prefixed to
    692579            the device name) to the module loading  facility.  In  addition,
    693580            the /etc/modules.devfs configuration file is used.*/
     
    700587
    701588            for (count = 0; count < num_args; ++count)
    702                 new->u.execute.argv[count] = bb_xstrdup (p[count]);
     589                new->u.execute.argv[count] = xstrdup(p[count]);
    703590
    704591            new->u.execute.argv[num_args] = NULL;
     
    710597                goto process_config_line_err; /* missing path and function in line */
    711598
    712             new->u.copy.source = bb_xstrdup (p[0]);
    713             new->u.copy.destination = bb_xstrdup (p[1]);
     599            new->u.copy.source = xstrdup(p[0]);
     600            new->u.copy.destination = xstrdup(p[1]);
    714601            break;
    715602        case 8: /* IGNORE */
     
    730617            break;
    731618        default:
    732             msg ="WHAT in";
     619            msg = "WHAT in";
    733620            goto process_config_line_err;
    734621        /*esac*/
    735622    } /* switch (i) */
    736623
    737     xregcomp( &new->preg, name, REG_EXTENDED);
     624    xregcomp(&new->preg, name, REG_EXTENDED);
    738625
    739626    *event_mask |= 1 << new->action.when;
     
    745632    last_config = new;
    746633    return;
    747 process_config_line_err:
     634
     635 process_config_line_err:
    748636    msg_logger_and_die(LOG_ERR, bb_msg_bad_config, msg , line);
    749637}  /*  End Function process_config_line   */
    750638
    751 static int do_servicing (int fd, unsigned long event_mask)
     639static int do_servicing(int fd, unsigned long event_mask)
    752640/*  [SUMMARY] Service devfs changes until a signal is received.
    753641    <fd> The open control file.
     
    758646    ssize_t bytes;
    759647    struct devfsd_notify_struct info;
    760     unsigned long tmp_event_mask;
    761 
    762     debug_msg_logger(LOG_INFO, __FUNCTION__);
    763 
    764     /*  Tell devfs what events we care about  */
    765     tmp_event_mask = event_mask;
    766     do_ioctl_and_die(fd, DEVFSDIOC_SET_EVENT_MASK, tmp_event_mask);
    767     while (!caught_signal)
    768     {
     648
     649    /* (void*) cast is only in order to match prototype */
     650    xioctl(fd, DEVFSDIOC_SET_EVENT_MASK, (void*)event_mask);
     651    while (!caught_signal) {
    769652        errno = 0;
    770         bytes = read (fd, (char *) &info, sizeof info);
     653        bytes = read(fd,(char *) &info, sizeof info);
    771654        if (caught_signal)
    772655            break;      /*  Must test for this first     */
     
    775658        if (bytes < 1)
    776659            break;
    777         service_name (&info);
    778     }
    779     if (caught_signal)
    780     {
     660        service_name(&info);
     661    }
     662    if (caught_signal) {
    781663        int c_sighup = caught_sighup;
    782664
    783665        caught_signal = FALSE;
    784666        caught_sighup = FALSE;
    785         return (c_sighup);
     667        return c_sighup;
    786668    }
    787669    msg_logger_and_die(LOG_ERR, "read error on control file");
    788670}   /*  End Function do_servicing  */
    789671
    790 static void service_name (const struct devfsd_notify_struct *info)
     672static void service_name(const struct devfsd_notify_struct *info)
    791673/*  [SUMMARY] Service a single devfs change.
    792674    <info> The devfs change.
     
    798680    struct config_entry_struct *entry;
    799681
    800     debug_msg_logger(LOG_INFO, __FUNCTION__);
    801682    if (ENABLE_DEBUG && info->overrun_count > 0)
    802         debug_msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
     683        msg_logger(LOG_ERR, "lost %u events", info->overrun_count);
    803684
    804685    /*  Discard lookups on "/dev/log" and "/dev/initctl"  */
    805     if(   info->type == DEVFSD_NOTIFY_LOOKUP &&
    806         ((info->devname[0]=='l' && info->devname[1]=='o' &&
    807           info->devname[2]=='g' && !info->devname[3]) ||
    808         ( info->devname[0]=='i' && info->devname[1]=='n' &&
    809           info->devname[2]=='i' && info->devname[3]=='t' &&
    810           info->devname[4]=='c' && info->devname[5]=='t' &&
    811           info->devname[6]=='l' && !info->devname[7])))
    812             return;
    813     for (entry = first_config; entry != NULL; entry = entry->next)
    814     {
     686    if (info->type == DEVFSD_NOTIFY_LOOKUP
     687        && ((info->devname[0] == 'l' && info->devname[1] == 'o'
     688        && info->devname[2] == 'g' && !info->devname[3])
     689        || (info->devname[0] == 'i' && info->devname[1] == 'n'
     690        && info->devname[2] == 'i' && info->devname[3] == 't'
     691        && info->devname[4] == 'c' && info->devname[5] == 't'
     692        && info->devname[6] == 'l' && !info->devname[7]))
     693    )
     694        return;
     695
     696    for (entry = first_config; entry != NULL; entry = entry->next) {
    815697        /*  First check if action matches the type, then check if name matches */
    816         if (info->type != entry->action.when || regexec (&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0 )
     698        if (info->type != entry->action.when
     699        || regexec(&entry->preg, info->devname, MAX_SUBEXPR, mbuf, 0) != 0)
    817700            continue;
    818         for (n = 0; (n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
     701        for (n = 0;(n < MAX_SUBEXPR) && (mbuf[n].rm_so != -1); ++n)
    819702            /* VOID */;
    820703
    821         debug_msg_logger(LOG_INFO, "%s: action.what %d", __FUNCTION__, entry->action.what);
    822 
    823         switch (entry->action.what)
    824         {
     704        switch (entry->action.what) {
    825705            case AC_PERMISSIONS:
    826                 action_permissions (info, entry);
     706                action_permissions(info, entry);
    827707                break;
    828708            case AC_MODLOAD:
    829                 if(ENABLE_DEVFSD_MODLOAD)
    830                     action_modload (info, entry);
     709                if (ENABLE_DEVFSD_MODLOAD)
     710                    action_modload(info, entry);
    831711                break;
    832712            case AC_EXECUTE:
    833                 action_execute (info, entry, mbuf, n);
     713                action_execute(info, entry, mbuf, n);
    834714                break;
    835715            case AC_COPY:
    836                 action_copy (info, entry, mbuf, n);
     716                action_copy(info, entry, mbuf, n);
    837717                break;
    838718            case AC_IGNORE:
     
    843723            case AC_RMOLDCOMPAT:
    844724            case AC_RMNEWCOMPAT:
    845                 action_compat (info, entry->action.what);
     725                action_compat(info, entry->action.what);
    846726                break;
    847727            default:
     
    851731}   /*  End Function service_name  */
    852732
    853 static void action_permissions (const struct devfsd_notify_struct *info,
     733static void action_permissions(const struct devfsd_notify_struct *info,
    854734                const struct config_entry_struct *entry)
    855735/*  [SUMMARY] Update permissions for a device entry.
     
    861741    struct stat statbuf;
    862742
    863     debug_msg_logger(LOG_INFO, __FUNCTION__);
    864 
    865     if ( stat (info->devname, &statbuf) != 0    ||
    866          chmod (info->devname,(statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0 ||
    867          chown (info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0)
    868     {
    869         msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m",info->devname);
    870     }
     743    if (stat(info->devname, &statbuf) != 0
     744     || chmod(info->devname, (statbuf.st_mode & S_IFMT) | (entry->u.permissions.mode & ~S_IFMT)) != 0
     745     || chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
     746    )
     747        error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
    871748}   /*  End Function action_permissions  */
    872749
    873 static void action_modload (const struct devfsd_notify_struct *info,
     750static void action_modload(const struct devfsd_notify_struct *info,
    874751                const struct config_entry_struct *entry ATTRIBUTE_UNUSED)
    875752/*  [SUMMARY] Load a module.
     
    880757{
    881758    char *argv[6];
    882     char device[STRING_LENGTH];
    883 
    884     argv[0] = MODPROBE;
    885     argv[1] = MODPROBE_SWITCH_1; /* "-k" */
    886     argv[2] = MODPROBE_SWITCH_2; /* "-C" */
    887     argv[3] = CONFIG_MODULES_DEVFS;
    888     argv[4] = device;
     759
     760    argv[0] = (char*)MODPROBE;
     761    argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
     762    argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
     763    argv[3] = (char*)CONFIG_MODULES_DEVFS;
     764    argv[4] = concat_path_file("/dev", info->devname); /* device */
    889765    argv[5] = NULL;
    890766
    891     snprintf (device, sizeof (device), "/dev/%s", info->devname);
    892     debug_msg_logger(LOG_INFO, "%s: %s %s %s %s %s",__FUNCTION__, argv[0],argv[1],argv[2],argv[3],argv[4]);
    893     fork_and_execute(DIE, argv[0], argv);
     767    wait4pid(xspawn(argv));
     768    free(argv[4]);
    894769}  /*  End Function action_modload  */
    895770
    896 static void action_execute (const struct devfsd_notify_struct *info,
     771static void action_execute(const struct devfsd_notify_struct *info,
    897772                const struct config_entry_struct *entry,
    898773                const regmatch_t *regexpr, unsigned int numexpr)
     
    900775    <info> The devfs change.
    901776    <entry> The config file entry.
    902     <regexpr> The number of subexpression (start, end) offsets within the
     777    <regexpr> The number of subexpression(start, end) offsets within the
    903778    device name.
    904779    <numexpr> The number of elements within <<regexpr>>.
     
    911786    char largv[MAX_ARGS + 1][STRING_LENGTH];
    912787
    913     debug_msg_logger(LOG_INFO ,__FUNCTION__);
    914788    gv_info.info = info;
    915789    gv_info.devname = info->devname;
    916     snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
    917     for (count = 0; entry->u.execute.argv[count] != NULL; ++count)
    918     {
    919         expand_expression (largv[count], STRING_LENGTH,
     790    snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
     791    for (count = 0; entry->u.execute.argv[count] != NULL; ++count) {
     792        expand_expression(largv[count], STRING_LENGTH,
    920793                entry->u.execute.argv[count],
    921794                get_variable, &gv_info,
    922                 gv_info.devname, regexpr, numexpr );
     795                gv_info.devname, regexpr, numexpr);
    923796        argv[count] = largv[count];
    924797    }
    925798    argv[count] = NULL;
    926     fork_and_execute(NO_DIE, argv[0], argv);
     799    wait4pid(spawn(argv));
    927800}   /*  End Function action_execute  */
    928801
    929802
    930 static void action_copy (const struct devfsd_notify_struct *info,
     803static void action_copy(const struct devfsd_notify_struct *info,
    931804             const struct config_entry_struct *entry,
    932805             const regmatch_t *regexpr, unsigned int numexpr)
     
    934807    <info> The devfs change.
    935808    <entry> The config file entry.
    936     <regexpr> This list of subexpression (start, end) offsets within the
     809    <regexpr> This list of subexpression(start, end) offsets within the
    937810    device name.
    938811    <numexpr> The number of elements in <<regexpr>>.
     
    946819    int ret = 0;
    947820
    948     debug_msg_logger(LOG_INFO, __FUNCTION__);
    949 
    950821    dest_stat.st_mode = 0;
    951822
    952     if ( (info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK (info->mode) )
     823    if ((info->type == DEVFSD_NOTIFY_CHANGE) && S_ISLNK(info->mode))
    953824        return;
    954825    gv_info.info = info;
    955826    gv_info.devname = info->devname;
    956827
    957     snprintf (gv_info.devpath, sizeof (gv_info.devpath), "%s/%s", mount_point, info->devname);
    958     expand_expression (source, STRING_LENGTH, entry->u.copy.source,
     828    snprintf(gv_info.devpath, sizeof(gv_info.devpath), "%s/%s", mount_point, info->devname);
     829    expand_expression(source, STRING_LENGTH, entry->u.copy.source,
    959830                get_variable, &gv_info, gv_info.devname,
    960831                regexpr, numexpr);
    961832
    962     expand_expression (destination, STRING_LENGTH, entry->u.copy.destination,
     833    expand_expression(destination, STRING_LENGTH, entry->u.copy.destination,
    963834                get_variable, &gv_info, gv_info.devname,
    964835                regexpr, numexpr);
    965836
    966     if ( !make_dir_tree (destination) || lstat (source, &source_stat) != 0)
     837    if (!make_dir_tree(destination) || lstat(source, &source_stat) != 0)
    967838            return;
    968     lstat (destination, &dest_stat);
     839    lstat(destination, &dest_stat);
    969840    new_mode = source_stat.st_mode & ~S_ISVTX;
    970841    if (info->type == DEVFSD_NOTIFY_CREATE)
    971842        new_mode |= S_ISVTX;
    972     else if ( (info->type == DEVFSD_NOTIFY_CHANGE) && (dest_stat.st_mode & S_ISVTX) )
     843    else if ((info->type == DEVFSD_NOTIFY_CHANGE) &&(dest_stat.st_mode & S_ISVTX))
    973844        new_mode |= S_ISVTX;
    974     ret = copy_inode (destination, &dest_stat, new_mode, source, &source_stat);
     845    ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
    975846    if (ENABLE_DEBUG && ret && (errno != EEXIST))
    976         debug_msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
    977     return;
     847        error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
    978848}   /*  End Function action_copy  */
    979849
    980 static void action_compat (const struct devfsd_notify_struct *info, unsigned int action)
     850static void action_compat(const struct devfsd_notify_struct *info, unsigned int action)
    981851/*  [SUMMARY] Process a compatibility request.
    982852    <info> The devfs change.
     
    988858    const char *compat_name = NULL;
    989859    const char *dest_name = info->devname;
    990     char *ptr=NULL;
     860    const char *ptr;
    991861    char compat_buf[STRING_LENGTH], dest_buf[STRING_LENGTH];
    992862    int mode, host, bus, target, lun;
     
    1009879
    1010880    /*  First construct compatibility name  */
    1011     switch (action)
    1012     {
     881    switch (action) {
    1013882        case AC_MKOLDCOMPAT:
    1014883        case AC_RMOLDCOMPAT:
    1015             compat_name = get_old_name (info->devname, info->namelen, compat_buf, info->major, info->minor);
     884            compat_name = get_old_name(info->devname, info->namelen, compat_buf, info->major, info->minor);
    1016885            break;
    1017886        case AC_MKNEWCOMPAT:
    1018887        case AC_RMNEWCOMPAT:
    1019             ptr = strrchr (info->devname, '/') + 1;
    1020             i=scan_dev_name(info->devname, info->namelen, ptr);
    1021 
    1022             debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
     888            ptr = bb_basename(info->devname);
     889            i = scan_dev_name(info->devname, info->namelen, ptr);
    1023890
    1024891            /* nothing found */
    1025             if(i==0 || i > 9)
     892            if (i == 0 || i > 9)
    1026893                return;
    1027894
    1028             sscanf (info->devname +((i<6)?5:4), "host%d/bus%d/target%d/lun%d/", &host, &bus, &target, &lun);
    1029             snprintf (dest_buf, sizeof (dest_buf), "../%s", info->devname + ((i>5)?4:0));
     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));
    1030897            dest_name = dest_buf;
    1031898            compat_name = compat_buf;
     
    1033900
    1034901            /* 1 == scsi/generic  2 == scsi/disc 3 == scsi/cd 6 == ide/host/disc 7 == ide/host/cd */
    1035             if( i == 1 || i == 2 || i == 3 || i == 6 || i ==7 )
    1036                 sprintf ( compat_buf, fmt[i], host, bus, target, lun);
     902            if (i == 1 || i == 2 || i == 3 || i == 6 || i ==7)
     903                sprintf(compat_buf, fmt[i], host, bus, target, lun);
    1037904
    1038905            /* 4 == scsi/part 8 == ide/host/part */
    1039             if( i == 4 || i == 8)
    1040                 sprintf ( compat_buf, fmt[i], host, bus, target, lun, atoi (ptr + 4) );
     906            if (i == 4 || i == 8)
     907                sprintf(compat_buf, fmt[i], host, bus, target, lun, atoi(ptr + 4));
    1041908
    1042909            /* 5 == scsi/mt */
    1043             if( i == 5)
    1044             {
     910            if (i == 5) {
    1045911                rewind_ = info->devname[info->namelen - 1];
    1046912                if (rewind_ != 'n')
    1047913                    rewind_ = '\0';
    1048914                mode=0;
    1049                 if(ptr[2] ==  'l' /*108*/ || ptr[2] == 'm'/*109*/)
     915                if (ptr[2] ==  'l' /*108*/ || ptr[2] == 'm'/*109*/)
    1050916                    mode = ptr[2] - 107; /* 1 or 2 */
    1051                 if(ptr[2] ==  'a')
     917                if (ptr[2] ==  'a')
    1052918                    mode = 3;
    1053                 sprintf (compat_buf, fmt [i], host, bus, target, lun, mode, rewind_);
     919                sprintf(compat_buf, fmt[i], host, bus, target, lun, mode, rewind_);
    1054920            }
    1055921
    1056922            /* 9 == ide/host/mt */
    1057             if( i ==  9 )
    1058                 snprintf (compat_buf, sizeof (compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
     923            if (i ==  9)
     924                snprintf(compat_buf, sizeof(compat_buf), fmt[i], host, bus, target, lun, ptr + 2);
    1059925        /* esac */
    1060     } /* switch(action) */
    1061 
    1062     if(compat_name == NULL )
     926    } /* switch (action) */
     927
     928    if (compat_name == NULL)
    1063929        return;
    1064930
    1065     debug_msg_logger( LOG_INFO, "%s: %s", __FUNCTION__, compat_name);
    1066 
    1067931    /*  Now decide what to do with it  */
    1068     switch (action)
    1069     {
     932    switch (action) {
    1070933        case AC_MKOLDCOMPAT:
    1071934        case AC_MKNEWCOMPAT:
    1072             mksymlink (dest_name, compat_name);
     935            mksymlink(dest_name, compat_name);
    1073936            break;
    1074937        case AC_RMOLDCOMPAT:
    1075938        case AC_RMNEWCOMPAT:
    1076             ret = unlink (compat_name);
     939            ret = unlink(compat_name);
    1077940            if (ENABLE_DEBUG && ret)
    1078                 debug_msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
     941                error_logger(LOG_ERR, "unlink: %s", compat_name);
    1079942            break;
    1080943        /*esac*/
    1081     } /* switch(action) */
     944    } /* switch (action) */
    1082945}   /*  End Function action_compat  */
    1083946
    1084947static void restore(char *spath, struct stat source_stat, int rootlen)
    1085948{
    1086     char dpath[STRING_LENGTH];
     949    char *dpath;
    1087950    struct stat dest_stat;
    1088951
    1089     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1090 
    1091952    dest_stat.st_mode = 0;
    1092     snprintf (dpath, sizeof dpath, "%s%s", mount_point, spath + rootlen);
    1093     lstat (dpath, &dest_stat);
    1094 
    1095     if ( S_ISLNK (source_stat.st_mode) || (source_stat.st_mode & S_ISVTX) )
    1096         copy_inode (dpath, &dest_stat, (source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
    1097 
    1098     if ( S_ISDIR (source_stat.st_mode) )
     953    dpath = concat_path_file(mount_point, spath + rootlen);
     954    lstat(dpath, &dest_stat);
     955    free(dpath);
     956    if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
     957        copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, &source_stat);
     958
     959    if (S_ISDIR(source_stat.st_mode))
    1099960        dir_operation(RESTORE, spath, rootlen,NULL);
    1100961}
    1101962
    1102963
    1103 static int copy_inode (const char *destpath, const struct stat *dest_stat,
     964static int copy_inode(const char *destpath, const struct stat *dest_stat,
    1104965            mode_t new_mode,
    1105966            const char *sourcepath, const struct stat *source_stat)
     
    1119980    char symlink_val[STRING_LENGTH];
    1120981
    1121     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1122 
    1123     if ( (source_stat->st_mode & S_IFMT) == (dest_stat->st_mode & S_IFMT) )
    1124     {
     982    if ((source_stat->st_mode & S_IFMT) ==(dest_stat->st_mode & S_IFMT)) {
    1125983        /*  Same type  */
    1126         if ( S_ISLNK (source_stat->st_mode) )
    1127         {
    1128             if (( source_len = readlink (sourcepath, source_link, STRING_LENGTH - 1) ) < 0 ||
    1129                 ( dest_len   = readlink (destpath  , dest_link  , STRING_LENGTH - 1) ) < 0 )
    1130                 return (FALSE);
     984        if (S_ISLNK(source_stat->st_mode)) {
     985            if ((source_len = readlink(sourcepath, source_link, STRING_LENGTH - 1)) < 0
     986                || (dest_len   = readlink(destpath  , dest_link  , STRING_LENGTH - 1)) < 0
     987            )
     988                return FALSE;
    1131989            source_link[source_len] = '\0';
    1132990            dest_link[dest_len] = '\0';
    1133             if ( (source_len != dest_len) || (strcmp (source_link, dest_link) != 0) )
    1134             {
    1135                 unlink (destpath);
    1136                 symlink (source_link, destpath);
     991            if ((source_len != dest_len) || (strcmp(source_link, dest_link) != 0)) {
     992                unlink(destpath);
     993                symlink(source_link, destpath);
    1137994            }
    1138             return (TRUE);
     995            return TRUE;
    1139996        }   /*  Else not a symlink  */
    1140         chmod (destpath, new_mode & ~S_IFMT);
    1141         chown (destpath, source_stat->st_uid, source_stat->st_gid);
    1142         return (TRUE);
     997        chmod(destpath, new_mode & ~S_IFMT);
     998        chown(destpath, source_stat->st_uid, source_stat->st_gid);
     999        return TRUE;
    11431000    }
    11441001    /*  Different types: unlink and create  */
    1145     unlink (destpath);
    1146     switch (source_stat->st_mode & S_IFMT)
    1147     {
     1002    unlink(destpath);
     1003    switch (source_stat->st_mode & S_IFMT) {
    11481004        case S_IFSOCK:
    1149             if ( ( fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0 )
     1005            if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
    11501006                break;
    11511007            un_addr.sun_family = AF_UNIX;
    1152             snprintf (un_addr.sun_path, sizeof (un_addr.sun_path), "%s", destpath);
    1153             val = bind (fd, (struct sockaddr *) &un_addr, (int) sizeof un_addr);
    1154             close (fd);
    1155             if (val != 0 || chmod (destpath, new_mode & ~S_IFMT) != 0)
     1008            snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s", destpath);
     1009            val = bind(fd,(struct sockaddr *) &un_addr,(int) sizeof un_addr);
     1010            close(fd);
     1011            if (val != 0 || chmod(destpath, new_mode & ~S_IFMT) != 0)
    11561012                break;
    11571013            goto do_chown;
    11581014        case S_IFLNK:
    1159             if ( ( val = readlink (sourcepath, symlink_val, STRING_LENGTH - 1) ) < 0 )
     1015            if ((val = readlink(sourcepath, symlink_val, STRING_LENGTH - 1)) < 0)
    11601016                break;
    11611017            symlink_val[val] = '\0';
    1162             if (symlink (symlink_val, destpath) == 0)
    1163                 return (TRUE);
     1018            if (symlink(symlink_val, destpath) == 0)
     1019                return TRUE;
    11641020            break;
    11651021        case S_IFREG:
    1166             if ( ( fd = open (destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT) ) < 0 )
     1022            if ((fd = open(destpath, O_RDONLY | O_CREAT, new_mode & ~S_IFMT)) < 0)
    11671023                break;
    1168             close (fd);
    1169             if (chmod (destpath, new_mode & ~S_IFMT) != 0)
     1024            close(fd);
     1025            if (chmod(destpath, new_mode & ~S_IFMT) != 0)
    11701026                break;
    11711027            goto do_chown;
     
    11731029        case S_IFCHR:
    11741030        case S_IFIFO:
    1175             if (mknod (destpath, new_mode, source_stat->st_rdev) != 0)
     1031            if (mknod(destpath, new_mode, source_stat->st_rdev) != 0)
    11761032                break;
    11771033            goto do_chown;
    11781034        case S_IFDIR:
    1179             if (mkdir (destpath, new_mode & ~S_IFMT) != 0)
     1035            if (mkdir(destpath, new_mode & ~S_IFMT) != 0)
    11801036                break;
    11811037do_chown:
    1182             if (chown (destpath, source_stat->st_uid, source_stat->st_gid) == 0)
    1183                 return (TRUE);
     1038            if (chown(destpath, source_stat->st_uid, source_stat->st_gid) == 0)
     1039                return TRUE;
    11841040        /*break;*/
    11851041    }
    1186     return (FALSE);
     1042    return FALSE;
    11871043}   /*  End Function copy_inode  */
    11881044
    1189 static void free_config (void)
     1045static void free_config(void)
    11901046/*  [SUMMARY] Free the configuration information.
    11911047    [RETURNS] Nothing.
     
    11951051    void *next;
    11961052
    1197     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1198 
    1199     for (c_entry = first_config; c_entry != NULL; c_entry = next)
    1200     {
     1053    for (c_entry = first_config; c_entry != NULL; c_entry = next) {
    12011054        unsigned int count;
    12021055
    12031056        next = c_entry->next;
    1204         regfree (&c_entry->preg);
    1205         if (c_entry->action.what == AC_EXECUTE)
    1206         {
    1207             for (count = 0; count < MAX_ARGS; ++count)
    1208             {
     1057        regfree(&c_entry->preg);
     1058        if (c_entry->action.what == AC_EXECUTE) {
     1059            for (count = 0; count < MAX_ARGS; ++count) {
    12091060                if (c_entry->u.execute.argv[count] == NULL)
    12101061                    break;
    1211                 free (c_entry->u.execute.argv[count]);
     1062                free(c_entry->u.execute.argv[count]);
    12121063            }
    12131064        }
    1214         free (c_entry);
     1065        free(c_entry);
    12151066    }
    12161067    first_config = NULL;
     
    12181069}   /*  End Function free_config  */
    12191070
    1220 static int get_uid_gid (int flag, const char *string)
     1071static int get_uid_gid(int flag, const char *string)
    12211072/*  [SUMMARY] Convert a string to a UID or GID value.
    12221073    <flag> "UID" or "GID".
     
    12271078    struct passwd *pw_ent;
    12281079    struct group *grp_ent;
    1229     static char *msg;
     1080    static const char *msg;
    12301081
    12311082    if (ENABLE_DEVFSD_VERBOSE)
    1232         msg="user";
    1233 
    1234     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1235 
    1236     if(ENABLE_DEBUG && flag != UID && flag != GID)
    1237         msg_logger_and_die(LOG_ERR,"%s: flag != UID && flag != GID", __FUNCTION__);
    1238 
    1239     if ( isdigit (string[0]) || ( (string[0] == '-') && isdigit (string[1]) ) )
    1240         return atoi (string);
    1241 
    1242     if ( flag == UID && ( pw_ent  = getpwnam (string) ) != NULL )
    1243         return (pw_ent->pw_uid);
    1244 
    1245     if ( flag == GID && ( grp_ent = getgrnam (string) ) != NULL )
    1246         return (grp_ent->gr_gid);
    1247     else if(ENABLE_DEVFSD_VERBOSE)
    1248         msg="group";
    1249 
    1250     if(ENABLE_DEVFSD_VERBOSE)
     1083        msg = "user";
     1084
     1085    if (isdigit(string[0]) ||((string[0] == '-') && isdigit(string[1])))
     1086        return atoi(string);
     1087
     1088    if (flag == UID && (pw_ent  = getpwnam(string)) != NULL)
     1089        return pw_ent->pw_uid;
     1090
     1091    if (flag == GID && (grp_ent = getgrnam(string)) != NULL)
     1092        return grp_ent->gr_gid;
     1093    else if (ENABLE_DEVFSD_VERBOSE)
     1094        msg = "group";
     1095
     1096    if (ENABLE_DEVFSD_VERBOSE)
    12511097        msg_logger(LOG_ERR,"unknown %s: %s, defaulting to %cid=0",  msg, string, msg[0]);
    1252     return (0);
     1098    return 0;
    12531099}/*  End Function get_uid_gid  */
    12541100
    1255 static mode_t get_mode (const char *string)
     1101static mode_t get_mode(const char *string)
    12561102/*  [SUMMARY] Convert a string to a mode value.
    12571103    <string> The string.
     
    12621108    int i;
    12631109
    1264     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1265 
    1266     if ( isdigit (string[0]) )
    1267         return strtoul (string, NULL, 8);
    1268     if (strlen (string) != 9)
     1110    if (isdigit(string[0]))
     1111        return strtoul(string, NULL, 8);
     1112    if (strlen(string) != 9)
    12691113        msg_logger_and_die(LOG_ERR, "bad mode: %s", string);
    12701114
    12711115    mode = 0;
    1272     i= S_IRUSR;
    1273     while(i>0)
    1274     {
    1275         if(string[0]=='r'||string[0]=='w'||string[0]=='x')
    1276             mode+=i;
    1277         i=i/2;
     1116    i = S_IRUSR;
     1117    while (i > 0) {
     1118        if (string[0] == 'r' || string[0] == 'w' || string[0] == 'x')
     1119            mode += i;
     1120        i = i / 2;
    12781121        string++;
    12791122    }
    1280     return (mode);
     1123    return mode;
    12811124}   /*  End Function get_mode  */
    12821125
    1283 static void signal_handler (int sig)
    1284 {
    1285     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1286 
     1126static void signal_handler(int sig)
     1127{
    12871128    caught_signal = TRUE;
    12881129    if (sig == SIGHUP)
    12891130        caught_sighup = TRUE;
    12901131
    1291     msg_logger(LOG_INFO, "Caught signal %d", sig);
     1132    info_logger(LOG_INFO, "Caught signal %d", sig);
    12921133}   /*  End Function signal_handler  */
    12931134
    1294 static const char *get_variable (const char *variable, void *info)
    1295 {
     1135static const char *get_variable(const char *variable, void *info)
     1136{
     1137    static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
     1138
     1139    char hostname[STRING_LENGTH];
    12961140    struct get_variable_info *gv_info = info;
    1297     static char hostname[STRING_LENGTH], sbuf[STRING_LENGTH];
    1298     const char *field_names[] = { "hostname", "mntpt", "devpath", "devname",
    1299                                    "uid", "gid", "mode", hostname, mount_point,
    1300                                    gv_info->devpath, gv_info->devname, 0 };
     1141    const char *field_names[] = {
     1142            "hostname", "mntpt", "devpath", "devname",
     1143            "uid", "gid", "mode", hostname, mount_point,
     1144            gv_info->devpath, gv_info->devname, NULL
     1145    };
    13011146    int i;
    13021147
    1303     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1304 
    1305     if (gethostname (hostname, STRING_LENGTH - 1) != 0)
    1306         msg_logger_and_die(LOG_ERR, "gethostname: %m");
    1307 
     1148    if (gethostname(hostname, STRING_LENGTH - 1) != 0)
    13081149        /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */
    1309         hostname[STRING_LENGTH - 1] = '\0';
    1310 
    1311     /* compare_string_array returns i>=0  */
    1312     i=compare_string_array(field_names, variable);
    1313 
    1314     if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL))
    1315             return (NULL);
    1316     if( i >= 0 && i <= 3)
    1317     {
    1318         debug_msg_logger(LOG_INFO, "%s: i=%d %s", __FUNCTION__, i ,field_names[i+7]);
    1319         return(field_names[i+7]);
    1320     }
    1321 
    1322     if(i == 4 )
    1323         sprintf (sbuf, "%u", gv_info->info->uid);
    1324     else if(i == 5)
    1325         sprintf (sbuf, "%u", gv_info->info->gid);
    1326     else if(i == 6)
    1327         sprintf (sbuf, "%o", gv_info->info->mode);
    1328 
    1329     debug_msg_logger(LOG_INFO, "%s: %s", __FUNCTION__, sbuf);
    1330 
    1331     return (sbuf);
     1150        error_logger_and_die(LOG_ERR, "gethostname");
     1151
     1152    hostname[STRING_LENGTH - 1] = '\0';
     1153
     1154    /* index_in_str_array returns i>=0  */
     1155    i = index_in_str_array(field_names, variable);
     1156
     1157    if (i > 6 || i < 0 || (i > 1 && gv_info == NULL))
     1158        return NULL;
     1159    if (i >= 0 && i <= 3)
     1160        return field_names[i + 7];
     1161
     1162    if (i == 4)
     1163        sprintf(sbuf, "%u", gv_info->info->uid);
     1164    else if (i == 5)
     1165        sprintf(sbuf, "%u", gv_info->info->gid);
     1166    else if (i == 6)
     1167        sprintf(sbuf, "%o", gv_info->info->mode);
     1168    return sbuf;
    13321169}   /*  End Function get_variable  */
    13331170
     
    13361173    struct devfsd_notify_struct info;
    13371174
    1338     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1339 
    1340     memset (&info, 0, sizeof info);
     1175    memset(&info, 0, sizeof info);
    13411176    info.type = DEVFSD_NOTIFY_REGISTERED;
    13421177    info.mode = statbuf.st_mode;
    1343     info.major = major (statbuf.st_rdev);
    1344     info.minor = minor (statbuf.st_rdev);
     1178    info.major = major(statbuf.st_rdev);
     1179    info.minor = minor(statbuf.st_rdev);
    13451180    info.uid = statbuf.st_uid;
    13461181    info.gid = statbuf.st_gid;
    1347     snprintf (info.devname, sizeof (info.devname), "%s", path + strlen (mount_point) + 1);
    1348     info.namelen = strlen (info.devname);
    1349     service_name (&info);
    1350     if ( S_ISDIR (statbuf.st_mode) )
    1351         dir_operation(SERVICE,path,0,NULL);
     1182    snprintf(info.devname, sizeof(info.devname), "%s", path + strlen(mount_point) + 1);
     1183    info.namelen = strlen(info.devname);
     1184    service_name(&info);
     1185    if (S_ISDIR(statbuf.st_mode))
     1186        dir_operation(SERVICE, path, 0, NULL);
    13521187}
    13531188
     
    13641199    DIR *dp;
    13651200    struct dirent *de;
    1366     char path[STRING_LENGTH];
    1367 
    1368     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1369 
    1370     if((dp = opendir( dir_name))==NULL)
    1371     {
    1372         debug_msg_logger(LOG_ERR, "opendir: %s: %m", dir_name);
     1201    char *path;
     1202
     1203    if ((dp = warn_opendir(dir_name)) == NULL)
    13731204        return;
    1374     }
    1375 
    1376     while ( (de = readdir (dp) ) != NULL )
    1377     {
    1378 
    1379         if(de->d_name && *de->d_name == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])))
     1205
     1206    while ((de = readdir(dp)) != NULL) {
     1207
     1208        if (de->d_name && DOT_OR_DOTDOT(de->d_name))
    13801209            continue;
    1381         snprintf (path, sizeof (path), "%s/%s", dir_name, de->d_name);
    1382         debug_msg_logger(LOG_ERR, "%s: %s", __FUNCTION__, path);
    1383 
    1384         if (lstat (path, &statbuf) != 0)
    1385         {
    1386             debug_msg_logger(LOG_ERR, "%s: %s: %m", __FUNCTION__, path);
    1387             continue;
     1210        path = concat_path_file(dir_name, de->d_name);
     1211        if (lstat(path, &statbuf) == 0) {
     1212            switch (type) {
     1213                case SERVICE:
     1214                    service(statbuf, path);
     1215                    break;
     1216                case RESTORE:
     1217                    restore(path, statbuf, var);
     1218                    break;
     1219                case READ_CONFIG:
     1220                    read_config_file(path, var, event_mask);
     1221                    break;
     1222            }
    13881223        }
    1389         switch(type)
    1390         {
    1391             case SERVICE:
    1392                 service(statbuf,path);
    1393                 break;
    1394             case RESTORE:
    1395                 restore(path, statbuf, var);
    1396                 break;
    1397             case READ_CONFIG:
    1398                 read_config_file (path, var, event_mask);
    1399                 break;
    1400         }
    1401     }
    1402     closedir (dp);
     1224        free(path);
     1225    }
     1226    closedir(dp);
    14031227}   /*  End Function do_scan_and_service  */
    14041228
    1405 static int mksymlink (const char *oldpath, const char *newpath)
     1229static int mksymlink(const char *oldpath, const char *newpath)
    14061230/*  [SUMMARY] Create a symlink, creating intervening directories as required.
    14071231    <oldpath> The string contained in the symlink.
     
    14101234*/
    14111235{
    1412     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1413 
    1414     if ( !make_dir_tree (newpath) )
    1415         return (-1);
    1416 
    1417     if (symlink (oldpath, newpath) != 0)
    1418     {
     1236    if (!make_dir_tree(newpath))
     1237        return -1;
     1238
     1239    if (symlink(oldpath, newpath) != 0) {
    14191240        if (errno != EEXIST)
    1420         {
    1421             debug_msg_logger(LOG_ERR, "%s: %s to %s: %m", __FUNCTION__, oldpath, newpath);
    1422             return (-1);
    1423         }
    1424     }
    1425     return (0);
     1241            return -1;
     1242    }
     1243    return 0;
    14261244}   /*  End Function mksymlink  */
    14271245
    14281246
    1429 static int make_dir_tree (const char *path)
     1247static int make_dir_tree(const char *path)
    14301248/*  [SUMMARY] Creating intervening directories for a path as required.
    1431     <path> The full pathname (including the leaf node).
     1249    <path> The full pathname(including the leaf node).
    14321250    [RETURNS] TRUE on success, else FALSE.
    14331251*/
    14341252{
    1435     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1436 
    1437     if (bb_make_directory( dirname((char *)path), -1, FILEUTILS_RECUR )==-1)
    1438     {
    1439         debug_msg_logger(LOG_ERR, "%s: %s: %m",__FUNCTION__, path);
    1440         return (FALSE);
    1441     }
    1442     return(TRUE);
     1253    if (bb_make_directory(dirname((char *)path), -1, FILEUTILS_RECUR) == -1)
     1254        return FALSE;
     1255    return TRUE;
    14431256} /*  End Function make_dir_tree  */
    14441257
     
    14661279    char temp[STRING_LENGTH];
    14671280
    1468     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1469 
    1470     if ( !st_expr_expand (temp, STRING_LENGTH, input, get_variable_func, info) )
    1471         return (FALSE);
    1472     expand_regexp (output, outsize, temp, devname, ex, numexp);
    1473     return (TRUE);
     1281    if (!st_expr_expand(temp, STRING_LENGTH, input, get_variable_func, info))
     1282        return FALSE;
     1283    expand_regexp(output, outsize, temp, devname, ex, numexp);
     1284    return TRUE;
    14741285}   /*  End Function expand_expression  */
    14751286
    1476 static void expand_regexp (char *output, size_t outsize, const char *input,
     1287static void expand_regexp(char *output, size_t outsize, const char *input,
    14771288               const char *devname,
    1478                const regmatch_t *ex, unsigned int numex )
     1289               const regmatch_t *ex, unsigned int numex)
    14791290/*  [SUMMARY] Expand all occurrences of the regular subexpressions \0 to \9.
    14801291    <output> The output expanded expression is written here.
     
    14951306    int c = -1;
    14961307
    1497     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1498 
    14991308    /*  Guarantee NULL termination by writing an explicit '\0' character into
    15001309    the very last byte  */
     
    15041313    and '\0' .. '\9' with subexpressions 0 .. 9, if they exist. Other \x
    15051314    codes are deleted  */
    1506     while ( (c != '\0') && (outsize != 0) )
    1507     {
     1315    while ((c != '\0') && (outsize != 0)) {
    15081316        c = *input;
    15091317        ++input;
    1510         if (c == '\\')
    1511         {
     1318        if (c == '\\') {
    15121319            c = *input;
    15131320            ++input;
    1514             if (c != '\\')
    1515             {
    1516                 if ((c >= '0') && (c <= last_exp))
    1517                 {
     1321            if (c != '\\') {
     1322                if ((c >= '0') && (c <= last_exp)) {
    15181323                    const regmatch_t *subexp = ex + (c - '0');
    15191324                    unsigned int sublen = subexp->rm_eo - subexp->rm_so;
     
    15221327                    if (sublen > outsize)
    15231328                        sublen = outsize;
    1524                     strncpy (output, devname + subexp->rm_so, sublen);
     1329                    strncpy(output, devname + subexp->rm_so, sublen);
    15251330                    output += sublen;
    15261331                    outsize -= sublen;
     
    15401345struct translate_struct
    15411346{
    1542     char *match;    /*  The string to match to (up to length)                */
    1543     char *format;   /*  Format of output, "%s" takes data past match string,
    1544             NULL is effectively "%s" (just more efficient)       */
     1347    const char *match;    /*  The string to match to(up to length)                */
     1348    const char *format;   /*  Format of output, "%s" takes data past match string,
     1349            NULL is effectively "%s"(just more efficient)       */
    15451350};
    15461351
     
    15781383};
    15791384
    1580 const char *get_old_name (const char *devname, unsigned int namelen,
     1385const char *get_old_name(const char *devname, unsigned int namelen,
    15811386              char *buffer, unsigned int major, unsigned int minor)
    15821387/*  [SUMMARY] Translate a kernel-supplied name into an old name.
     
    15901395{
    15911396    const char *compat_name = NULL;
    1592     char *ptr;
     1397    const char *ptr;
    15931398    struct translate_struct *trans;
    15941399    unsigned int i;
     
    16161421    };
    16171422
    1618     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1619 
    1620     for (trans = translate_table; trans->match != NULL; ++trans)
    1621     {
    1622          len = strlen (trans->match);
    1623 
    1624         if (strncmp (devname, trans->match, len) == 0)
    1625         {
     1423    for (trans = translate_table; trans->match != NULL; ++trans) {
     1424         len = strlen(trans->match);
     1425
     1426        if (strncmp(devname, trans->match, len) == 0) {
    16261427            if (trans->format == NULL)
    1627                 return (devname + len);
    1628             sprintf (buffer, trans->format, devname + len);
    1629             return (buffer);
     1428                return devname + len;
     1429            sprintf(buffer, trans->format, devname + len);
     1430            return buffer;
    16301431        }
    16311432    }
    16321433
    1633     ptr = (strrchr (devname, '/') + 1);
     1434    ptr = bb_basename(devname);
    16341435    i = scan_dev_name(devname, namelen, ptr);
    16351436
    1636     if( i > 0 && i < 13)
     1437    if (i > 0 && i < 13)
    16371438        compat_name = buffer;
    16381439    else
    16391440        return NULL;
    16401441
    1641     debug_msg_logger(LOG_INFO, "%s: scan_dev_name = %d", __FUNCTION__, i);
    1642 
    16431442    /* 1 == scsi/generic, 3 == scsi/cd, 10 == sbp/ */
    1644     if( i == 1 || i == 3 || i == 10 )
    1645         sprintf (buffer, fmt[i], minor);
     1443    if (i == 1 || i == 3 || i == 10)
     1444        sprintf(buffer, fmt[i], minor);
    16461445
    16471446    /* 2 ==scsi/disc, 4 == scsi/part */
    1648     if( i == 2 || i == 4)
    1649         compat_name = write_old_sd_name (buffer, major, minor,((i == 2)?"":(ptr + 4)));
     1447    if (i == 2 || i == 4)
     1448        compat_name = write_old_sd_name(buffer, major, minor,((i == 2) ? "" : (ptr + 4)));
    16501449
    16511450    /* 5 == scsi/mt */
    1652     if( i == 5)
    1653     {
     1451    if (i == 5) {
    16541452        mode = ptr[2];
    16551453        if (mode == 'n')
    16561454            mode = '\0';
    1657         sprintf (buffer, fmt[i], minor & 0x1f, mode);
     1455        sprintf(buffer, fmt[i], minor & 0x1f, mode);
    16581456        if (devname[namelen - 1] != 'n')
    16591457            ++compat_name;
    16601458    }
    16611459    /* 6 == ide/host/disc, 7 == ide/host/cd, 8 == ide/host/part */
    1662     if( i == 6 || i == 7 || i == 8 )
     1460    if (i == 6 || i == 7 || i == 8)
    16631461        /* last arg should be ignored for i == 6 or i== 7 */
    1664         sprintf (buffer, fmt[i] , get_old_ide_name (major, minor), ptr + 4);
     1462        sprintf(buffer, fmt[i] , get_old_ide_name(major, minor), ptr + 4);
    16651463
    16661464    /* 9 ==  ide/host/mt */
    1667     if( i == 9 )
    1668         sprintf (buffer, fmt[i], ptr + 2, minor & 0x7f);
     1465    if (i == 9)
     1466        sprintf(buffer, fmt[i], ptr + 2, minor & 0x7f);
    16691467
    16701468    /*  11 == vcc/ */
    1671     if( i == 11 )
    1672     {
    1673         sprintf (buffer, fmt[i], devname + 4);
     1469    if (i == 11) {
     1470        sprintf(buffer, fmt[i], devname + 4);
    16741471        if (buffer[3] == '0')
    16751472            buffer[3] = '\0';
    16761473    }
    16771474    /* 12 ==  pty/ */
    1678     if( i == 12 )
    1679     {
     1475    if (i == 12) {
    16801476        pty1 = "pqrstuvwxyzabcde";
    16811477        pty2 = "0123456789abcdef";
    1682         indexx = atoi (devname + 5);
    1683         sprintf (buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
    1684     }
    1685 
    1686     if(ENABLE_DEBUG && compat_name!=NULL)
    1687         msg_logger(LOG_INFO, "%s: compat_name  %s", __FUNCTION__, compat_name);
    1688 
    1689     return (compat_name);
     1478        indexx = atoi(devname + 5);
     1479        sprintf(buffer, fmt[i], (devname[4] == 'm') ? 'p' : 't', pty1[indexx >> 4], pty2[indexx & 0x0f]);
     1480    }
     1481    return compat_name;
    16901482}   /*  End Function get_old_name  */
    16911483
    1692 static char get_old_ide_name (unsigned int major, unsigned int minor)
     1484static char get_old_ide_name(unsigned int major, unsigned int minor)
    16931485/*  [SUMMARY] Get the old IDE name for a device.
    16941486    <major> The major number for the device.
     
    16971489*/
    16981490{
    1699     char letter='y';    /* 121 */
    1700     char c='a';     /*  97 */
    1701     int i=IDE0_MAJOR;
    1702 
    1703     debug_msg_logger(LOG_INFO, __FUNCTION__);
     1491    char letter = 'y';  /* 121 */
     1492    char c = 'a';       /*  97 */
     1493    int i = IDE0_MAJOR;
    17041494
    17051495    /* I hope it works like the previous code as it saves a few bytes. Tito ;P */
    17061496    do {
    1707         if( i==IDE0_MAJOR || i==IDE1_MAJOR || i==IDE2_MAJOR ||
    1708             i==IDE3_MAJOR || i==IDE4_MAJOR || i==IDE5_MAJOR ||
    1709             i==IDE6_MAJOR || i==IDE7_MAJOR || i==IDE8_MAJOR ||
    1710             i==IDE9_MAJOR )
    1711         {
    1712             if((unsigned int)i==major)
    1713             {
    1714                 letter=c;
     1497        if (i == IDE0_MAJOR || i == IDE1_MAJOR || i == IDE2_MAJOR
     1498         || i == IDE3_MAJOR || i == IDE4_MAJOR || i == IDE5_MAJOR
     1499         || i == IDE6_MAJOR || i == IDE7_MAJOR || i == IDE8_MAJOR
     1500         || i == IDE9_MAJOR
     1501        ) {
     1502            if ((unsigned int)i == major) {
     1503                letter = c;
    17151504                break;
    17161505            }
    1717             c+=2;
     1506            c += 2;
    17181507        }
    17191508        i++;
    1720     } while(i<=IDE9_MAJOR);
     1509    } while (i <= IDE9_MAJOR);
    17211510
    17221511    if (minor > 63)
    17231512        ++letter;
    1724     return (letter);
     1513    return letter;
    17251514}   /*  End Function get_old_ide_name  */
    17261515
    1727 static char *write_old_sd_name (char *buffer,
     1516static char *write_old_sd_name(char *buffer,
    17281517                unsigned int major, unsigned int minor,
    1729                 char *part)
     1518                const char *part)
    17301519/*  [SUMMARY] Write the old SCSI disc name to a buffer.
    17311520    <buffer> The buffer to write to.
     
    17381527    unsigned int disc_index;
    17391528
    1740     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1741 
    1742     if (major == 8)
    1743     {
    1744         sprintf (buffer, "sd%c%s", 'a' + (minor >> 4), part);
    1745         return (buffer);
    1746     }
    1747     if ( (major > 64) && (major < 72) )
    1748     {
    1749         disc_index = ( (major - 64) << 4 ) + (minor >> 4);
     1529    if (major == 8) {
     1530        sprintf(buffer, "sd%c%s", 'a' + (minor >> 4), part);
     1531        return buffer;
     1532    }
     1533    if ((major > 64) && (major < 72)) {
     1534        disc_index = ((major - 64) << 4) +(minor >> 4);
    17501535        if (disc_index < 26)
    1751             sprintf (buffer, "sd%c%s", 'a' + disc_index, part);
     1536            sprintf(buffer, "sd%c%s", 'a' + disc_index, part);
    17521537        else
    1753             sprintf (buffer, "sd%c%c%s", 'a' + (disc_index / 26) - 1, 'a' + disc_index % 26,part);
    1754         return (buffer);
    1755     }
    1756     return (NULL);
     1538            sprintf(buffer, "sd%c%c%s", 'a' +(disc_index / 26) - 1, 'a' + disc_index % 26, part);
     1539        return buffer;
     1540    }
     1541    return NULL;
    17571542}   /*  End Function write_old_sd_name  */
    17581543
     
    17621547/*EXPERIMENTAL_FUNCTION*/
    17631548
    1764 int st_expr_expand (char *output, unsigned int length, const char *input,
    1765              const char *(*get_variable_func) (const char *variable,
     1549int st_expr_expand(char *output, unsigned int length, const char *input,
     1550             const char *(*get_variable_func)(const char *variable,
    17661551                          void *info),
    17671552             void *info)
     
    17851570    char buffer[BUFFER_SIZE], tmp[STRING_LENGTH];
    17861571
    1787     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1788 
    17891572    if (length > BUFFER_SIZE)
    17901573        length = BUFFER_SIZE;
    1791     for (; TRUE; ++input)
    1792     {
    1793         switch (ch = *input)
    1794         {
     1574    for (; TRUE; ++input) {
     1575        switch (ch = *input) {
    17951576            case '$':
    17961577                /*  Variable expansion  */
    1797                 input = expand_variable (buffer, length, &out_pos, ++input, get_variable_func, info);
     1578                input = expand_variable(buffer, length, &out_pos, ++input, get_variable_func, info);
    17981579                if (input == NULL)
    1799                     return (FALSE);
     1580                    return FALSE;
    18001581                break;
    18011582            case '~':
    18021583                /*  Home directory expansion  */
    18031584                ch = input[1];
    1804                 if ( isspace (ch) || (ch == '/') || (ch == '\0') )
    1805                 {
     1585                if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
    18061586                    /* User's own home directory: leave separator for next time */
    1807                     if ( ( env = getenv ("HOME") ) == NULL )
    1808                     {
    1809                         msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
    1810                         return (FALSE);
     1587                    if ((env = getenv("HOME")) == NULL) {
     1588                        info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
     1589                        return FALSE;
    18111590                    }
    1812                     len = strlen (env);
     1591                    len = strlen(env);
    18131592                    if (len + out_pos >= length)
    18141593                        goto st_expr_expand_out;
    1815                     memcpy (buffer + out_pos, env, len + 1);
     1594                    memcpy(buffer + out_pos, env, len + 1);
    18161595                    out_pos += len;
    18171596                    continue;
    18181597                }
    18191598                /*  Someone else's home directory  */
    1820                 for (ptr = ++input; !isspace (ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
    1821                     /* VOID */ ;
     1599                for (ptr = ++input; !isspace(ch) && (ch != '/') && (ch != '\0'); ch = *++ptr)
     1600                    /* VOID */;
    18221601                len = ptr - input;
    18231602                if (len >= sizeof tmp)
    18241603                    goto st_expr_expand_out;
    1825                 safe_memcpy (tmp, input, len);
     1604                safe_memcpy(tmp, input, len);
    18261605                input = ptr - 1;
    1827                 if ( ( pwent = getpwnam (tmp) ) == NULL )
    1828                 {
    1829                     msg_logger(LOG_INFO, "no pwent for: %s", tmp);
    1830                     return (FALSE);
     1606                if ((pwent = getpwnam(tmp)) == NULL) {
     1607                    info_logger(LOG_INFO, "no pwent for: %s", tmp);
     1608                    return FALSE;
    18311609                }
    1832                 len = strlen (pwent->pw_dir);
     1610                len = strlen(pwent->pw_dir);
    18331611                if (len + out_pos >= length)
    18341612                    goto st_expr_expand_out;
    1835                 memcpy (buffer + out_pos, pwent->pw_dir, len + 1);
     1613                memcpy(buffer + out_pos, pwent->pw_dir, len + 1);
    18361614                out_pos += len;
    18371615                break;
     
    18421620                    goto st_expr_expand_out;
    18431621                buffer[out_pos++] = ch;
    1844                 if (ch == '\0')
    1845                 {
    1846                     memcpy (output, buffer, out_pos);
    1847                     return (TRUE);
     1622                if (ch == '\0') {
     1623                    memcpy(output, buffer, out_pos);
     1624                    return TRUE;
    18481625                }
    18491626                break;
     
    18511628        }
    18521629    }
    1853     return (FALSE);
     1630    return FALSE;
    18541631st_expr_expand_out:
    1855     msg_logger(LOG_INFO, bb_msg_small_buffer);
    1856     return (FALSE);
     1632    info_logger(LOG_INFO, bb_msg_small_buffer);
     1633    return FALSE;
    18571634}   /*  End Function st_expr_expand  */
    18581635
     
    18601637/*  Private functions follow  */
    18611638
    1862 static const char *expand_variable (char *buffer, unsigned int length,
     1639static const char *expand_variable(char *buffer, unsigned int length,
    18631640                    unsigned int *out_pos, const char *input,
    1864                     const char *(*func) (const char *variable,
     1641                    const char *(*func)(const char *variable,
    18651642                             void *info),
    18661643                    void *info)
     
    18841661    char tmp[STRING_LENGTH];
    18851662
    1886     debug_msg_logger(LOG_INFO, __FUNCTION__);
    1887 
    18881663    ch = input[0];
    1889     if (ch == '$')
    1890     {
     1664    if (ch == '$') {
    18911665        /*  Special case for "$$": PID  */
    1892         sprintf ( tmp, "%d", (int) getpid () );
    1893         len = strlen (tmp);
     1666        sprintf(tmp, "%d",(int) getpid());
     1667        len = strlen(tmp);
    18941668        if (len + *out_pos >= length)
    18951669            goto expand_variable_out;
    18961670
    1897         memcpy (buffer + *out_pos, tmp, len + 1);
     1671        memcpy(buffer + *out_pos, tmp, len + 1);
    18981672        out_pos += len;
    1899         return (input);
     1673        return input;
    19001674    }
    19011675    /*  Ordinary variable expansion, possibly in braces  */
    1902     if (ch != '{')
    1903     {
     1676    if (ch != '{') {
    19041677        /*  Simple variable expansion  */
    1905         for (ptr = input; isalnum (ch) || (ch == '_') || (ch == ':');ch = *++ptr)
    1906             /* VOID */ ;
     1678        for (ptr = input; isalnum(ch) || (ch == '_') || (ch == ':'); ch = *++ptr)
     1679            /* VOID */;
    19071680        len = ptr - input;
    19081681        if ((size_t)len >= sizeof tmp)
    19091682            goto expand_variable_out;
    19101683
    1911         safe_memcpy (tmp, input, len);
     1684        safe_memcpy(tmp, input, len);
    19121685        input = ptr - 1;
    1913         if ( ( env = get_variable_v2 (tmp, func, info) ) == NULL )
    1914         {
    1915             msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
    1916             return (NULL);
     1686        if ((env = get_variable_v2(tmp, func, info)) == NULL) {
     1687            info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
     1688            return NULL;
    19171689        }
    1918         len = strlen (env);
     1690        len = strlen(env);
    19191691        if (len + *out_pos >= length)
    19201692            goto expand_variable_out;
    19211693
    1922         memcpy (buffer + *out_pos, env, len + 1);
     1694        memcpy(buffer + *out_pos, env, len + 1);
    19231695        *out_pos += len;
    1924         return (input);
     1696        return input;
    19251697    }
    19261698    /*  Variable in braces: check for ':' tricks  */
    19271699    ch = *++input;
    1928     for (ptr = input; isalnum (ch) || (ch == '_'); ch = *++ptr)
     1700    for (ptr = input; isalnum(ch) || (ch == '_'); ch = *++ptr)
    19291701        /* VOID */;
    1930     if (ch == '}')
    1931     {
     1702    if (ch == '}') {
    19321703        /*  Must be simple variable expansion with "${var}"  */
    19331704        len = ptr - input;
     
    19351706            goto expand_variable_out;
    19361707
    1937         safe_memcpy (tmp, input, len);
    1938         ptr = expand_variable (buffer, length, out_pos, tmp, func, info );
     1708        safe_memcpy(tmp, input, len);
     1709        ptr = expand_variable(buffer, length, out_pos, tmp, func, info);
    19391710        if (ptr == NULL)
    1940             return (NULL);
    1941         return (input + len);
    1942     }
    1943     if (ch != ':' || ptr[1] != '-' )
    1944     {
    1945         msg_logger(LOG_INFO, "illegal char in var name");
    1946         return (NULL);
     1711            return NULL;
     1712        return input + len;
     1713    }
     1714    if (ch != ':' || ptr[1] != '-') {
     1715        info_logger(LOG_INFO, "illegal char in var name");
     1716        return NULL;
    19471717    }
    19481718    /*  It's that handy "${var:-word}" expression. Check if var is defined  */
     
    19511721        goto expand_variable_out;
    19521722
    1953     safe_memcpy (tmp, input, len);
     1723    safe_memcpy(tmp, input, len);
    19541724    /*  Move input pointer to ':'  */
    19551725    input = ptr;
     
    19571727    ptr += 2;
    19581728    ch = ptr[0];
    1959     for (open_braces = 1; open_braces > 0; ch = *++ptr)
    1960     {
    1961         switch (ch)
    1962         {
     1729    for (open_braces = 1; open_braces > 0; ch = *++ptr) {
     1730        switch (ch) {
    19631731            case '{':
    19641732                ++open_braces;
     
    19681736                break;
    19691737            case '\0':
    1970                 msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
    1971                 return (NULL);
     1738                info_logger(LOG_INFO,"\"}\" not found in: %s", input);
     1739                return NULL;
    19721740            default:
    19731741                break;
     
    19761744    --ptr;
    19771745    /*  At this point ptr should point to closing brace of "${var:-word}"  */
    1978     if ( ( env = get_variable_v2 (tmp, func, info) ) != NULL )
    1979     {
     1746    if ((env = get_variable_v2(tmp, func, info)) != NULL) {
    19801747        /*  Found environment variable, so skip the input to the closing brace
    19811748            and return the variable  */
    19821749        input = ptr;
    1983         len = strlen (env);
     1750        len = strlen(env);
    19841751        if (len + *out_pos >= length)
    19851752            goto expand_variable_out;
    19861753
    1987         memcpy (buffer + *out_pos, env, len + 1);
     1754        memcpy(buffer + *out_pos, env, len + 1);
    19881755        *out_pos += len;
    1989         return (input);
     1756        return input;
    19901757    }
    19911758    /*  Environment variable was not found, so process word. Advance input
     
    19961763        goto expand_variable_out;
    19971764
    1998     safe_memcpy (tmp, input, len);
     1765    safe_memcpy(tmp, input, len);
    19991766    input = ptr;
    2000     if ( !st_expr_expand (tmp, STRING_LENGTH, tmp, func, info ) )
    2001         return (NULL);
    2002     len = strlen (tmp);
     1767    if (!st_expr_expand(tmp, STRING_LENGTH, tmp, func, info))
     1768        return NULL;
     1769    len = strlen(tmp);
    20031770    if (len + *out_pos >= length)
    20041771        goto expand_variable_out;
    20051772
    2006     memcpy (buffer + *out_pos, tmp, len + 1);
     1773    memcpy(buffer + *out_pos, tmp, len + 1);
    20071774    *out_pos += len;
    2008     return (input);
     1775    return input;
    20091776expand_variable_out:
    2010     msg_logger(LOG_INFO, bb_msg_small_buffer);
    2011     return (NULL);
     1777    info_logger(LOG_INFO, bb_msg_small_buffer);
     1778    return NULL;
    20121779}   /*  End Function expand_variable  */
    20131780
    20141781
    2015 static const char *get_variable_v2 (const char *variable,
    2016                   const char *(*func) (const char *variable, void *info),
     1782static const char *get_variable_v2(const char *variable,
     1783                  const char *(*func)(const char *variable, void *info),
    20171784                 void *info)
    20181785/*  [SUMMARY] Get a variable from the environment or .
     
    20261793    const char *value;
    20271794
    2028     debug_msg_logger(LOG_INFO, __FUNCTION__);
    2029 
    2030     if (func != NULL)
    2031     {
    2032         value = (*func) (variable, info);
     1795    if (func != NULL) {
     1796        value = (*func)(variable, info);
    20331797        if (value != NULL)
    2034             return (value);
    2035     }
    2036     return getenv (variable);
     1798            return value;
     1799    }
     1800    return getenv(variable);
    20371801}   /*  End Function get_variable  */
    20381802
Note: See TracChangeset for help on using the changeset viewer.