Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/sysklogd/syslogd.c

    r3232 r3621  
    1313 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1414 */
     15//config:config SYSLOGD
     16//config:   bool "syslogd"
     17//config:   default y
     18//config:   help
     19//config:     The syslogd utility is used to record logs of all the
     20//config:     significant events that occur on a system. Every
     21//config:     message that is logged records the date and time of the
     22//config:     event, and will generally also record the name of the
     23//config:     application that generated the message. When used in
     24//config:     conjunction with klogd, messages from the Linux kernel
     25//config:     can also be recorded. This is terribly useful,
     26//config:     especially for finding what happened when something goes
     27//config:     wrong. And something almost always will go wrong if
     28//config:     you wait long enough....
     29//config:
     30//config:config FEATURE_ROTATE_LOGFILE
     31//config:   bool "Rotate message files"
     32//config:   default y
     33//config:   depends on SYSLOGD
     34//config:   help
     35//config:     This enables syslogd to rotate the message files
     36//config:     on his own. No need to use an external rotate script.
     37//config:
     38//config:config FEATURE_REMOTE_LOG
     39//config:   bool "Remote Log support"
     40//config:   default y
     41//config:   depends on SYSLOGD
     42//config:   help
     43//config:     When you enable this feature, the syslogd utility can
     44//config:     be used to send system log messages to another system
     45//config:     connected via a network. This allows the remote
     46//config:     machine to log all the system messages, which can be
     47//config:     terribly useful for reducing the number of serial
     48//config:     cables you use. It can also be a very good security
     49//config:     measure to prevent system logs from being tampered with
     50//config:     by an intruder.
     51//config:
     52//config:config FEATURE_SYSLOGD_DUP
     53//config:   bool "Support -D (drop dups) option"
     54//config:   default y
     55//config:   depends on SYSLOGD
     56//config:   help
     57//config:     Option -D instructs syslogd to drop consecutive messages
     58//config:     which are totally the same.
     59//config:
     60//config:config FEATURE_SYSLOGD_CFG
     61//config:   bool "Support syslog.conf"
     62//config:   default y
     63//config:   depends on SYSLOGD
     64//config:   help
     65//config:     Supports restricted syslogd config. See docs/syslog.conf.txt
     66//config:
     67//config:config FEATURE_SYSLOGD_READ_BUFFER_SIZE
     68//config:   int "Read buffer size in bytes"
     69//config:   default 256
     70//config:   range 256 20000
     71//config:   depends on SYSLOGD
     72//config:   help
     73//config:     This option sets the size of the syslog read buffer.
     74//config:     Actual memory usage increases around five times the
     75//config:     change done here.
     76//config:
     77//config:config FEATURE_IPC_SYSLOG
     78//config:   bool "Circular Buffer support"
     79//config:   default y
     80//config:   depends on SYSLOGD
     81//config:   help
     82//config:     When you enable this feature, the syslogd utility will
     83//config:     use a circular buffer to record system log messages.
     84//config:     When the buffer is filled it will continue to overwrite
     85//config:     the oldest messages. This can be very useful for
     86//config:     systems with little or no permanent storage, since
     87//config:     otherwise system logs can eventually fill up your
     88//config:     entire filesystem, which may cause your system to
     89//config:     break badly.
     90//config:
     91//config:config FEATURE_IPC_SYSLOG_BUFFER_SIZE
     92//config:   int "Circular buffer size in Kbytes (minimum 4KB)"
     93//config:   default 16
     94//config:   range 4 2147483647
     95//config:   depends on FEATURE_IPC_SYSLOG
     96//config:   help
     97//config:     This option sets the size of the circular buffer
     98//config:     used to record system log messages.
     99//config:
     100//config:config FEATURE_KMSG_SYSLOG
     101//config:   bool "Linux kernel printk buffer support"
     102//config:   default y
     103//config:   depends on SYSLOGD
     104//config:   select PLATFORM_LINUX
     105//config:   help
     106//config:     When you enable this feature, the syslogd utility will
     107//config:     write system log message to the Linux kernel's printk buffer.
     108//config:     This can be used as a smaller alternative to the syslogd IPC
     109//config:     support, as klogd and logread aren't needed.
     110//config:
     111//config:     NOTICE: Syslog facilities in log entries needs kernel 3.5+.
     112
     113//applet:IF_SYSLOGD(APPLET(syslogd, BB_DIR_SBIN, BB_SUID_DROP))
     114
     115//kbuild:lib-$(CONFIG_SYSLOGD) += syslogd_and_logger.o
    15116
    16117//usage:#define syslogd_trivial_usage
     
    22123//usage:    )
    23124//usage:     "\n    -n      Run in foreground"
    24 //usage:     "\n    -O FILE     Log to FILE (default:/var/log/messages)"
    25 //usage:     "\n    -l N        Log only messages more urgent than prio N (1-8)"
    26 //usage:     "\n    -S      Smaller output"
    27 //usage:    IF_FEATURE_ROTATE_LOGFILE(
    28 //usage:     "\n    -s SIZE     Max size (KB) before rotation (default:200KB, 0=off)"
    29 //usage:     "\n    -b N        N rotated logs to keep (default:1, max=99, 0=purge)"
    30 //usage:    )
    31125//usage:    IF_FEATURE_REMOTE_LOG(
    32 //usage:     "\n    -R HOST[:PORT]  Log to IP or hostname on PORT (default PORT=514/UDP)"
     126//usage:     "\n    -R HOST[:PORT]  Log to HOST:PORT (default PORT:514)"
    33127//usage:     "\n    -L      Log locally and via network (default is network only if -R)"
    34 //usage:    )
    35 //usage:    IF_FEATURE_SYSLOGD_DUP(
    36 //usage:     "\n    -D      Drop duplicates"
    37128//usage:    )
    38129//usage:    IF_FEATURE_IPC_SYSLOG(
     
    40131//usage:     "\n    -C[size_kb] Log to shared mem buffer (use logread to read it)"
    41132//usage:    )
     133//usage:    IF_FEATURE_KMSG_SYSLOG(
     134//usage:     "\n    -K      Log to kernel printk buffer (use dmesg to read it)"
     135//usage:    )
     136//usage:     "\n    -O FILE     Log to FILE (default: /var/log/messages, stdout if -)"
     137//usage:    IF_FEATURE_ROTATE_LOGFILE(
     138//usage:     "\n    -s SIZE     Max size (KB) before rotation (default:200KB, 0=off)"
     139//usage:     "\n    -b N        N rotated logs to keep (default:1, max=99, 0=purge)"
     140//usage:    )
     141//usage:     "\n    -l N        Log only messages more urgent than prio N (1-8)"
     142//usage:     "\n    -S      Smaller output"
     143//usage:    IF_FEATURE_SYSLOGD_DUP(
     144//usage:     "\n    -D      Drop duplicates"
     145//usage:    )
    42146//usage:    IF_FEATURE_SYSLOGD_CFG(
    43147//usage:     "\n    -f FILE     Use FILE as config (default:/etc/syslog.conf)"
    44148//usage:    )
    45149/* //usage:  "\n    -m MIN      Minutes between MARK lines (default:20, 0=off)" */
    46 //usage:    IF_FEATURE_KMSG_SYSLOG(
    47 //usage:     "\n    -K      Log to kernel printk buffer (use dmesg to read it)"
    48 //usage:    )
    49150//usage:
    50151//usage:#define syslogd_example_usage
     
    59160#include <syslog.h>
    60161*/
     162#ifndef _PATH_LOG
     163#define _PATH_LOG   "/dev/log"
     164#endif
    61165
    62166#include <sys/un.h>
     
    108212    const char *path;
    109213    int fd;
     214    time_t last_log_time;
    110215#if ENABLE_FEATURE_ROTATE_LOGFILE
    111216    unsigned size;
     
    163268    struct shbuf_ds *shbuf;
    164269#endif
    165     time_t last_log_time;
    166270    /* localhost's name. We print only first 64 chars */
    167271    char *hostname;
     
    235339    OPT_cfg         = IF_FEATURE_SYSLOGD_CFG(   (1 << OPTBIT_cfg        )) + 0,
    236340    OPT_kmsg        = IF_FEATURE_KMSG_SYSLOG(   (1 << OPTBIT_kmsg       )) + 0,
    237 
    238341};
    239342#define OPTION_STR "m:nO:l:S" \
     
    567670    pri &= G.primask;
    568671
    569     write(G.kmsgfd, G.printbuf, sprintf(G.printbuf, "<%d>%s\n", pri, msg));
     672    full_write(G.kmsgfd, G.printbuf, sprintf(G.printbuf, "<%d>%s\n", pri, msg));
    570673}
    571674#else
     
    583686    int len = strlen(msg);
    584687
    585     if (log_file->fd >= 0) {
    586         /* Reopen log file every second. This allows admin
    587          * to delete the file and not worry about restarting us.
     688    /* fd can't be 0 (we connect fd 0 to /dev/log socket) */
     689    /* fd is 1 if "-O -" is in use */
     690    if (log_file->fd > 1) {
     691        /* Reopen log files every second. This allows admin
     692         * to delete the files and not worry about restarting us.
    588693         * This costs almost nothing since it happens
    589          * _at most_ once a second.
     694         * _at most_ once a second for each file, and happens
     695         * only when each file is actually written.
    590696         */
    591697        if (!now)
    592698            now = time(NULL);
    593         if (G.last_log_time != now) {
    594             G.last_log_time = now;
     699        if (log_file->last_log_time != now) {
     700            log_file->last_log_time = now;
    595701            close(log_file->fd);
    596702            goto reopen;
    597703        }
    598     } else {
     704    }
     705    else if (log_file->fd == 1) {
     706        /* We are logging to stdout: do nothing */
     707    }
     708    else {
     709        if (LONE_DASH(log_file->path)) {
     710            log_file->fd = 1;
     711            /* log_file->isRegular = 0; - already is */
     712        } else {
    599713 reopen:
    600         log_file->fd = open(log_file->path, O_WRONLY | O_CREAT
     714            log_file->fd = open(log_file->path, O_WRONLY | O_CREAT
    601715                    | O_NOCTTY | O_APPEND | O_NONBLOCK,
    602716                    0666);
    603         if (log_file->fd < 0) {
    604             /* cannot open logfile? - print to /dev/console then */
    605             int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
    606             if (fd < 0)
    607                 fd = 2; /* then stderr, dammit */
    608             full_write(fd, msg, len);
    609             if (fd != 2)
    610                 close(fd);
    611             return;
    612         }
     717            if (log_file->fd < 0) {
     718                /* cannot open logfile? - print to /dev/console then */
     719                int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
     720                if (fd < 0)
     721                    fd = 2; /* then stderr, dammit */
     722                full_write(fd, msg, len);
     723                if (fd != 2)
     724                    close(fd);
     725                return;
     726            }
    613727#if ENABLE_FEATURE_ROTATE_LOGFILE
    614         {
    615             struct stat statf;
    616             log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode));
    617             /* bug (mostly harmless): can wrap around if file > 4gb */
    618             log_file->size = statf.st_size;
    619         }
    620 #endif
     728            {
     729                struct stat statf;
     730                log_file->isRegular = (fstat(log_file->fd, &statf) == 0 && S_ISREG(statf.st_mode));
     731                /* bug (mostly harmless): can wrap around if file > 4gb */
     732                log_file->size = statf.st_size;
     733            }
     734#endif
     735        }
    621736    }
    622737
     
    646761            /* newFile == "f.0" now */
    647762            rename(log_file->path, newFile);
    648             /* Incredibly, if F and F.0 are hardlinks, POSIX
    649              * _demands_ that rename returns 0 but does not
    650              * remove F!!!
    651              * (hardlinked F/F.0 pair was observed after
    652              * power failure during rename()).
    653              * Ensure old file is gone:
    654              */
    655             unlink(log_file->path);
     763        }
     764
     765        /* We may or may not have just renamed the file away;
     766         * if we didn't rename because we aren't keeping any backlog,
     767         * then it's time to clobber the file. If we did rename it...,
     768         * incredibly, if F and F.0 are hardlinks, POSIX _demands_
     769         * that rename returns 0 but does not remove F!!!
     770         * (hardlinked F/F.0 pair was observed after
     771         * power failure during rename()).
     772         * So ensure old file is gone in any case:
     773         */
     774        unlink(log_file->path);
    656775#ifdef SYSLOGD_WRLOCK
    657             fl.l_type = F_UNLCK;
    658             fcntl(log_file->fd, F_SETLKW, &fl);
    659 #endif
    660             close(log_file->fd);
    661             goto reopen;
    662         }
    663         ftruncate(log_file->fd, 0);
    664     }
    665     log_file->size +=
    666 #endif
    667             full_write(log_file->fd, msg, len);
     776        fl.l_type = F_UNLCK;
     777        fcntl(log_file->fd, F_SETLKW, &fl);
     778#endif
     779        close(log_file->fd);
     780        goto reopen;
     781    }
     782/* TODO: what to do on write errors ("disk full")? */
     783    len = full_write(log_file->fd, msg, len);
     784    if (len > 0)
     785        log_file->size += len;
     786#else
     787    full_write(log_file->fd, msg, len);
     788#endif
     789
    668790#ifdef SYSLOGD_WRLOCK
    669791    fl.l_type = F_UNLCK;
     
    815937    char *dev_log_name;
    816938
    817 #if ENABLE_FEATURE_SYSTEMD
    818     if (sd_listen_fds() == 1)
    819         return SD_LISTEN_FDS_START;
    820 #endif
    821 
    822939    memset(&sunx, 0, sizeof(sunx));
    823940    sunx.sun_family = AF_UNIX;
     
    825942    /* Unlink old /dev/log or object it points to. */
    826943    /* (if it exists, bind will fail) */
    827     strcpy(sunx.sun_path, "/dev/log");
    828     dev_log_name = xmalloc_follow_symlinks("/dev/log");
     944    strcpy(sunx.sun_path, _PATH_LOG);
     945    dev_log_name = xmalloc_follow_symlinks(_PATH_LOG);
    829946    if (dev_log_name) {
    830947        safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
     
    835952    sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
    836953    xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
    837     chmod("/dev/log", 0666);
     954    chmod(_PATH_LOG, 0666);
    838955
    839956    return sock_fd;
     
    861978static void do_syslogd(void)
    862979{
    863     int sock_fd;
    864980#if ENABLE_FEATURE_REMOTE_LOG
    865981    llist_t *item;
     
    882998    alarm(G.markInterval);
    883999#endif
    884     sock_fd = create_socket();
     1000    xmove_fd(create_socket(), STDIN_FILENO);
    8851001
    8861002    if (option_mask32 & OPT_circularlog)
     
    9031019#endif
    9041020 read_again:
    905         sz = read(sock_fd, recvbuf, MAX_READ - 1);
     1021        sz = read(STDIN_FILENO, recvbuf, MAX_READ - 1);
    9061022        if (sz < 0) {
    9071023            if (!bb_got_signal)
    908                 bb_perror_msg("read from /dev/log");
     1024                bb_perror_msg("read from %s", _PATH_LOG);
    9091025            break;
    9101026        }
     
    9741090
    9751091    timestamp_and_log_internal("syslogd exiting");
    976     puts("syslogd exiting");
    9771092    remove_pidfile(CONFIG_PID_FILE_PATH "/syslogd.pid");
    9781093    ipcsyslog_cleanup();
Note: See TracChangeset for help on using the changeset viewer.