busybox/sysklogd/syslogd.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini syslogd implementation for busybox
   4 *
   5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
   6 *
   7 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
   8 *
   9 * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com>
  10 *
  11 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
  12 *
  13 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14 */
  15
  16/*
  17 * Done in syslogd_and_logger.c:
  18#include "libbb.h"
  19#define SYSLOG_NAMES
  20#define SYSLOG_NAMES_CONST
  21#include <syslog.h>
  22*/
  23
  24#include <paths.h>
  25#include <sys/un.h>
  26#include <sys/uio.h>
  27
  28#if ENABLE_FEATURE_REMOTE_LOG
  29#include <netinet/in.h>
  30#endif
  31
  32#if ENABLE_FEATURE_IPC_SYSLOG
  33#include <sys/ipc.h>
  34#include <sys/sem.h>
  35#include <sys/shm.h>
  36#endif
  37
  38
  39#define DEBUG 0
  40
  41/* MARK code is not very useful, is bloat, and broken:
  42 * can deadlock if alarmed to make MARK while writing to IPC buffer
  43 * (semaphores are down but do_mark routine tries to down them again) */
  44#undef SYSLOGD_MARK
  45
  46enum {
  47        MAX_READ = 256,
  48        DNS_WAIT_SEC = 2 * 60,
  49};
  50
  51/* Semaphore operation structures */
  52struct shbuf_ds {
  53        int32_t size;   /* size of data - 1 */
  54        int32_t tail;   /* end of message list */
  55        char data[1];   /* data/messages */
  56};
  57
  58/* Allows us to have smaller initializer. Ugly. */
  59#define GLOBALS \
  60        const char *logFilePath;                \
  61        int logFD;                              \
  62        /* interval between marks in seconds */ \
  63        /*int markInterval;*/                   \
  64        /* level of messages to be logged */    \
  65        int logLevel;                           \
  66USE_FEATURE_ROTATE_LOGFILE( \
  67        /* max size of file before rotation */  \
  68        unsigned logFileSize;                   \
  69        /* number of rotated message files */   \
  70        unsigned logFileRotate;                 \
  71        unsigned curFileSize;                   \
  72        smallint isRegular;                     \
  73) \
  74USE_FEATURE_REMOTE_LOG( \
  75        /* udp socket for remote logging */     \
  76        int remoteFD;                           \
  77        len_and_sockaddr* remoteAddr;           \
  78) \
  79USE_FEATURE_IPC_SYSLOG( \
  80        int shmid; /* ipc shared memory id */   \
  81        int s_semid; /* ipc semaphore id */     \
  82        int shm_size;                           \
  83        struct sembuf SMwup[1];                 \
  84        struct sembuf SMwdn[3];                 \
  85)
  86
  87struct init_globals {
  88        GLOBALS
  89};
  90
  91struct globals {
  92        GLOBALS
  93
  94#if ENABLE_FEATURE_REMOTE_LOG
  95        unsigned last_dns_resolve;
  96        char *remoteAddrStr;
  97#endif
  98
  99#if ENABLE_FEATURE_IPC_SYSLOG
 100        struct shbuf_ds *shbuf;
 101#endif
 102        time_t last_log_time;
 103        /* localhost's name. We print only first 64 chars */
 104        char *hostname;
 105
 106        /* We recv into recvbuf... */
 107        char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)];
 108        /* ...then copy to parsebuf, escaping control chars */
 109        /* (can grow x2 max) */
 110        char parsebuf[MAX_READ*2];
 111        /* ...then sprintf into printbuf, adding timestamp (15 chars),
 112         * host (64), fac.prio (20) to the message */
 113        /* (growth by: 15 + 64 + 20 + delims = ~110) */
 114        char printbuf[MAX_READ*2 + 128];
 115};
 116
 117static const struct init_globals init_data = {
 118        .logFilePath = "/var/log/messages",
 119        .logFD = -1,
 120#ifdef SYSLOGD_MARK
 121        .markInterval = 20 * 60,
 122#endif
 123        .logLevel = 8,
 124#if ENABLE_FEATURE_ROTATE_LOGFILE
 125        .logFileSize = 200 * 1024,
 126        .logFileRotate = 1,
 127#endif
 128#if ENABLE_FEATURE_REMOTE_LOG
 129        .remoteFD = -1,
 130#endif
 131#if ENABLE_FEATURE_IPC_SYSLOG
 132        .shmid = -1,
 133        .s_semid = -1,
 134        .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size
 135        .SMwup = { {1, -1, IPC_NOWAIT} },
 136        .SMwdn = { {0, 0}, {1, 0}, {1, +1} },
 137#endif
 138};
 139
 140#define G (*ptr_to_globals)
 141#define INIT_G() do { \
 142        SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
 143} while (0)
 144
 145
 146/* Options */
 147enum {
 148        OPTBIT_mark = 0, // -m
 149        OPTBIT_nofork, // -n
 150        OPTBIT_outfile, // -O
 151        OPTBIT_loglevel, // -l
 152        OPTBIT_small, // -S
 153        USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize   ,) // -s
 154        USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt  ,) // -b
 155        USE_FEATURE_REMOTE_LOG(    OPTBIT_remote     ,) // -R
 156        USE_FEATURE_REMOTE_LOG(    OPTBIT_locallog   ,) // -L
 157        USE_FEATURE_IPC_SYSLOG(    OPTBIT_circularlog,) // -C
 158        USE_FEATURE_SYSLOGD_DUP(   OPTBIT_dup        ,) // -D
 159
 160        OPT_mark        = 1 << OPTBIT_mark    ,
 161        OPT_nofork      = 1 << OPTBIT_nofork  ,
 162        OPT_outfile     = 1 << OPTBIT_outfile ,
 163        OPT_loglevel    = 1 << OPTBIT_loglevel,
 164        OPT_small       = 1 << OPTBIT_small   ,
 165        OPT_filesize    = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize   )) + 0,
 166        OPT_rotatecnt   = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt  )) + 0,
 167        OPT_remotelog   = USE_FEATURE_REMOTE_LOG(    (1 << OPTBIT_remote     )) + 0,
 168        OPT_locallog    = USE_FEATURE_REMOTE_LOG(    (1 << OPTBIT_locallog   )) + 0,
 169        OPT_circularlog = USE_FEATURE_IPC_SYSLOG(    (1 << OPTBIT_circularlog)) + 0,
 170        OPT_dup         = USE_FEATURE_SYSLOGD_DUP(   (1 << OPTBIT_dup        )) + 0,
 171};
 172#define OPTION_STR "m:nO:l:S" \
 173        USE_FEATURE_ROTATE_LOGFILE("s:" ) \
 174        USE_FEATURE_ROTATE_LOGFILE("b:" ) \
 175        USE_FEATURE_REMOTE_LOG(    "R:" ) \
 176        USE_FEATURE_REMOTE_LOG(    "L"  ) \
 177        USE_FEATURE_IPC_SYSLOG(    "C::") \
 178        USE_FEATURE_SYSLOGD_DUP(   "D"  )
 179#define OPTION_DECL *opt_m, *opt_l \
 180        USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \
 181        USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \
 182        USE_FEATURE_IPC_SYSLOG(    ,*opt_C = NULL)
 183#define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
 184        USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \
 185        USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \
 186        USE_FEATURE_REMOTE_LOG(    ,&G.remoteAddrStr) \
 187        USE_FEATURE_IPC_SYSLOG(    ,&opt_C)
 188
 189
 190/* circular buffer variables/structures */
 191#if ENABLE_FEATURE_IPC_SYSLOG
 192
 193#if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4
 194#error Sorry, you must set the syslogd buffer size to at least 4KB.
 195#error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE
 196#endif
 197
 198/* our shared key (syslogd.c and logread.c must be in sync) */
 199enum { KEY_ID = 0x414e4547 }; /* "GENA" */
 200
 201static void ipcsyslog_cleanup(void)
 202{
 203        if (G.shmid != -1) {
 204                shmdt(G.shbuf);
 205        }
 206        if (G.shmid != -1) {
 207                shmctl(G.shmid, IPC_RMID, NULL);
 208        }
 209        if (G.s_semid != -1) {
 210                semctl(G.s_semid, 0, IPC_RMID, 0);
 211        }
 212}
 213
 214static void ipcsyslog_init(void)
 215{
 216        if (DEBUG)
 217                printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size);
 218
 219        G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644);
 220        if (G.shmid == -1) {
 221                bb_perror_msg_and_die("shmget");
 222        }
 223
 224        G.shbuf = shmat(G.shmid, NULL, 0);
 225        if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */
 226                bb_perror_msg_and_die("shmat");
 227        }
 228
 229        memset(G.shbuf, 0, G.shm_size);
 230        G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1;
 231        /*G.shbuf->tail = 0;*/
 232
 233        // we'll trust the OS to set initial semval to 0 (let's hope)
 234        G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023);
 235        if (G.s_semid == -1) {
 236                if (errno == EEXIST) {
 237                        G.s_semid = semget(KEY_ID, 2, 0);
 238                        if (G.s_semid != -1)
 239                                return;
 240                }
 241                bb_perror_msg_and_die("semget");
 242        }
 243}
 244
 245/* Write message to shared mem buffer */
 246static void log_to_shmem(const char *msg, int len)
 247{
 248        int old_tail, new_tail;
 249
 250        if (semop(G.s_semid, G.SMwdn, 3) == -1) {
 251                bb_perror_msg_and_die("SMwdn");
 252        }
 253
 254        /* Circular Buffer Algorithm:
 255         * --------------------------
 256         * tail == position where to store next syslog message.
 257         * tail's max value is (shbuf->size - 1)
 258         * Last byte of buffer is never used and remains NUL.
 259         */
 260        len++; /* length with NUL included */
 261 again:
 262        old_tail = G.shbuf->tail;
 263        new_tail = old_tail + len;
 264        if (new_tail < G.shbuf->size) {
 265                /* store message, set new tail */
 266                memcpy(G.shbuf->data + old_tail, msg, len);
 267                G.shbuf->tail = new_tail;
 268        } else {
 269                /* k == available buffer space ahead of old tail */
 270                int k = G.shbuf->size - old_tail;
 271                /* copy what fits to the end of buffer, and repeat */
 272                memcpy(G.shbuf->data + old_tail, msg, k);
 273                msg += k;
 274                len -= k;
 275                G.shbuf->tail = 0;
 276                goto again;
 277        }
 278        if (semop(G.s_semid, G.SMwup, 1) == -1) {
 279                bb_perror_msg_and_die("SMwup");
 280        }
 281        if (DEBUG)
 282                printf("tail:%d\n", G.shbuf->tail);
 283}
 284#else
 285void ipcsyslog_cleanup(void);
 286void ipcsyslog_init(void);
 287void log_to_shmem(const char *msg);
 288#endif /* FEATURE_IPC_SYSLOG */
 289
 290
 291/* Print a message to the log file. */
 292static void log_locally(time_t now, char *msg)
 293{
 294        struct flock fl;
 295        int len = strlen(msg);
 296
 297#if ENABLE_FEATURE_IPC_SYSLOG
 298        if ((option_mask32 & OPT_circularlog) && G.shbuf) {
 299                log_to_shmem(msg, len);
 300                return;
 301        }
 302#endif
 303        if (G.logFD >= 0) {
 304                /* Reopen log file every second. This allows admin
 305                 * to delete the file and not worry about restarting us.
 306                 * This costs almost nothing since it happens
 307                 * _at most_ once a second.
 308                 */
 309                if (!now)
 310                        now = time(NULL);
 311                if (G.last_log_time != now) {
 312                        G.last_log_time = now;
 313                        close(G.logFD);
 314                        goto reopen;
 315                }
 316        } else {
 317 reopen:
 318                G.logFD = open(G.logFilePath, O_WRONLY | O_CREAT
 319                                        | O_NOCTTY | O_APPEND | O_NONBLOCK,
 320                                        0666);
 321                if (G.logFD < 0) {
 322                        /* cannot open logfile? - print to /dev/console then */
 323                        int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
 324                        if (fd < 0)
 325                                fd = 2; /* then stderr, dammit */
 326                        full_write(fd, msg, len);
 327                        if (fd != 2)
 328                                close(fd);
 329                        return;
 330                }
 331#if ENABLE_FEATURE_ROTATE_LOGFILE
 332                {
 333                        struct stat statf;
 334                        G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode));
 335                        /* bug (mostly harmless): can wrap around if file > 4gb */
 336                        G.curFileSize = statf.st_size;
 337                }
 338#endif
 339        }
 340
 341        fl.l_whence = SEEK_SET;
 342        fl.l_start = 0;
 343        fl.l_len = 1;
 344        fl.l_type = F_WRLCK;
 345        fcntl(G.logFD, F_SETLKW, &fl);
 346
 347#if ENABLE_FEATURE_ROTATE_LOGFILE
 348        if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) {
 349                if (G.logFileRotate) { /* always 0..99 */
 350                        int i = strlen(G.logFilePath) + 3 + 1;
 351                        char oldFile[i];
 352                        char newFile[i];
 353                        i = G.logFileRotate - 1;
 354                        /* rename: f.8 -> f.9; f.7 -> f.8; ... */
 355                        while (1) {
 356                                sprintf(newFile, "%s.%d", G.logFilePath, i);
 357                                if (i == 0) break;
 358                                sprintf(oldFile, "%s.%d", G.logFilePath, --i);
 359                                /* ignore errors - file might be missing */
 360                                rename(oldFile, newFile);
 361                        }
 362                        /* newFile == "f.0" now */
 363                        rename(G.logFilePath, newFile);
 364                        fl.l_type = F_UNLCK;
 365                        fcntl(G.logFD, F_SETLKW, &fl);
 366                        close(G.logFD);
 367                        goto reopen;
 368                }
 369                ftruncate(G.logFD, 0);
 370        }
 371        G.curFileSize +=
 372#endif
 373                        full_write(G.logFD, msg, len);
 374        fl.l_type = F_UNLCK;
 375        fcntl(G.logFD, F_SETLKW, &fl);
 376}
 377
 378static void parse_fac_prio_20(int pri, char *res20)
 379{
 380        const CODE *c_pri, *c_fac;
 381
 382        if (pri != 0) {
 383                c_fac = facilitynames;
 384                while (c_fac->c_name) {
 385                        if (c_fac->c_val != (LOG_FAC(pri) << 3)) {
 386                                c_fac++;
 387                                continue;
 388                        }
 389                        /* facility is found, look for prio */
 390                        c_pri = prioritynames;
 391                        while (c_pri->c_name) {
 392                                if (c_pri->c_val != LOG_PRI(pri)) {
 393                                        c_pri++;
 394                                        continue;
 395                                }
 396                                snprintf(res20, 20, "%s.%s",
 397                                                c_fac->c_name, c_pri->c_name);
 398                                return;
 399                        }
 400                        /* prio not found, bail out */
 401                        break;
 402                }
 403                snprintf(res20, 20, "<%d>", pri);
 404        }
 405}
 406
 407/* len parameter is used only for "is there a timestamp?" check.
 408 * NB: some callers cheat and supply len==0 when they know
 409 * that there is no timestamp, short-circuiting the test. */
 410static void timestamp_and_log(int pri, char *msg, int len)
 411{
 412        char *timestamp;
 413        time_t now;
 414
 415        if (len < 16 || msg[3] != ' ' || msg[6] != ' '
 416         || msg[9] != ':' || msg[12] != ':' || msg[15] != ' '
 417        ) {
 418                time(&now);
 419                timestamp = ctime(&now) + 4; /* skip day of week */
 420        } else {
 421                now = 0;
 422                timestamp = msg;
 423                msg += 16;
 424        }
 425        timestamp[15] = '\0';
 426
 427        if (option_mask32 & OPT_small)
 428                sprintf(G.printbuf, "%s %s\n", timestamp, msg);
 429        else {
 430                char res[20];
 431                parse_fac_prio_20(pri, res);
 432                sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
 433        }
 434
 435        /* Log message locally (to file or shared mem) */
 436        log_locally(now, G.printbuf);
 437}
 438
 439static void timestamp_and_log_internal(const char *msg)
 440{
 441        if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog))
 442                return;
 443        timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0);
 444}
 445
 446/* tmpbuf[len] is a NUL byte (set by caller), but there can be other,
 447 * embedded NULs. Split messages on each of these NULs, parse prio,
 448 * escape control chars and log each locally. */
 449static void split_escape_and_log(char *tmpbuf, int len)
 450{
 451        char *p = tmpbuf;
 452
 453        tmpbuf += len;
 454        while (p < tmpbuf) {
 455                char c;
 456                char *q = G.parsebuf;
 457                int pri = (LOG_USER | LOG_NOTICE);
 458
 459                if (*p == '<') {
 460                        /* Parse the magic priority number */
 461                        pri = bb_strtou(p + 1, &p, 10);
 462                        if (*p == '>')
 463                                p++;
 464                        if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
 465                                pri = (LOG_USER | LOG_NOTICE);
 466                }
 467
 468                while ((c = *p++)) {
 469                        if (c == '\n')
 470                                c = ' ';
 471                        if (!(c & ~0x1f) && c != '\t') {
 472                                *q++ = '^';
 473                                c += '@'; /* ^@, ^A, ^B... */
 474                        }
 475                        *q++ = c;
 476                }
 477                *q = '\0';
 478
 479                /* Now log it */
 480                if (LOG_PRI(pri) < G.logLevel)
 481                        timestamp_and_log(pri, G.parsebuf, q - G.parsebuf);
 482        }
 483}
 484
 485static void quit_signal(int sig)
 486{
 487        timestamp_and_log_internal("syslogd exiting");
 488        puts("syslogd exiting");
 489        if (ENABLE_FEATURE_IPC_SYSLOG)
 490                ipcsyslog_cleanup();
 491        kill_myself_with_sig(sig);
 492}
 493
 494#ifdef SYSLOGD_MARK
 495static void do_mark(int sig)
 496{
 497        if (G.markInterval) {
 498                timestamp_and_log_internal("-- MARK --");
 499                alarm(G.markInterval);
 500        }
 501}
 502#endif
 503
 504/* Don't inline: prevent struct sockaddr_un to take up space on stack
 505 * permanently */
 506static NOINLINE int create_socket(void)
 507{
 508        struct sockaddr_un sunx;
 509        int sock_fd;
 510        char *dev_log_name;
 511
 512        memset(&sunx, 0, sizeof(sunx));
 513        sunx.sun_family = AF_UNIX;
 514
 515        /* Unlink old /dev/log or object it points to. */
 516        /* (if it exists, bind will fail) */
 517        strcpy(sunx.sun_path, "/dev/log");
 518        dev_log_name = xmalloc_follow_symlinks("/dev/log");
 519        if (dev_log_name) {
 520                safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
 521                free(dev_log_name);
 522        }
 523        unlink(sunx.sun_path);
 524
 525        sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
 526        xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
 527        chmod("/dev/log", 0666);
 528
 529        return sock_fd;
 530}
 531
 532#if ENABLE_FEATURE_REMOTE_LOG
 533static int try_to_resolve_remote(void)
 534{
 535        if (!G.remoteAddr) {
 536                unsigned now = monotonic_sec();
 537
 538                /* Don't resolve name too often - DNS timeouts can be big */
 539                if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
 540                        return -1;
 541                G.last_dns_resolve = now;
 542                G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
 543                if (!G.remoteAddr)
 544                        return -1;
 545        }
 546        return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
 547}
 548#endif
 549
 550static void do_syslogd(void) NORETURN;
 551static void do_syslogd(void)
 552{
 553        int sock_fd;
 554#if ENABLE_FEATURE_SYSLOGD_DUP
 555        int last_sz = -1;
 556        char *last_buf;
 557        char *recvbuf = G.recvbuf;
 558#else
 559#define recvbuf (G.recvbuf)
 560#endif
 561
 562        /* Set up signal handlers */
 563        bb_signals(0
 564                + (1 << SIGINT)
 565                + (1 << SIGTERM)
 566                + (1 << SIGQUIT)
 567                , quit_signal);
 568        signal(SIGHUP, SIG_IGN);
 569        /* signal(SIGCHLD, SIG_IGN); - why? */
 570#ifdef SYSLOGD_MARK
 571        signal(SIGALRM, do_mark);
 572        alarm(G.markInterval);
 573#endif
 574        sock_fd = create_socket();
 575
 576        if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
 577                ipcsyslog_init();
 578        }
 579
 580        timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
 581
 582        for (;;) {
 583                ssize_t sz;
 584
 585#if ENABLE_FEATURE_SYSLOGD_DUP
 586                last_buf = recvbuf;
 587                if (recvbuf == G.recvbuf)
 588                        recvbuf = G.recvbuf + MAX_READ;
 589                else
 590                        recvbuf = G.recvbuf;
 591#endif
 592 read_again:
 593                sz = safe_read(sock_fd, recvbuf, MAX_READ - 1);
 594                if (sz < 0)
 595                        bb_perror_msg_and_die("read from /dev/log");
 596
 597                /* Drop trailing '\n' and NULs (typically there is one NUL) */
 598                while (1) {
 599                        if (sz == 0)
 600                                goto read_again;
 601                        /* man 3 syslog says: "A trailing newline is added when needed".
 602                         * However, neither glibc nor uclibc do this:
 603                         * syslog(prio, "test")   sends "test\0" to /dev/log,
 604                         * syslog(prio, "test\n") sends "test\n\0".
 605                         * IOW: newline is passed verbatim!
 606                         * I take it to mean that it's syslogd's job
 607                         * to make those look identical in the log files. */
 608                        if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n')
 609                                break;
 610                        sz--;
 611                }
 612#if ENABLE_FEATURE_SYSLOGD_DUP
 613                if ((option_mask32 & OPT_dup) && (sz == last_sz))
 614                        if (memcmp(last_buf, recvbuf, sz) == 0)
 615                                continue;
 616                last_sz = sz;
 617#endif
 618#if ENABLE_FEATURE_REMOTE_LOG
 619                /* We are not modifying log messages in any way before send */
 620                /* Remote site cannot trust _us_ anyway and need to do validation again */
 621                if (G.remoteAddrStr) {
 622                        if (-1 == G.remoteFD) {
 623                                G.remoteFD = try_to_resolve_remote();
 624                                if (-1 == G.remoteFD)
 625                                        goto no_luck;
 626                        }
 627                        /* Stock syslogd sends it '\n'-terminated
 628                         * over network, mimic that */
 629                        recvbuf[sz] = '\n';
 630                        /* send message to remote logger, ignore possible error */
 631                        /* TODO: on some errors, close and set G.remoteFD to -1
 632                         * so that DNS resolution and connect is retried? */
 633                        sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
 634                                    &G.remoteAddr->u.sa, G.remoteAddr->len);
 635 no_luck: ;
 636                }
 637#endif
 638                if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
 639                        recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
 640                        split_escape_and_log(recvbuf, sz);
 641                }
 642        } /* for (;;) */
 643#undef recvbuf
 644}
 645
 646int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 647int syslogd_main(int argc UNUSED_PARAM, char **argv)
 648{
 649        char OPTION_DECL;
 650
 651        INIT_G();
 652#if ENABLE_FEATURE_REMOTE_LOG
 653        G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
 654#endif
 655
 656        /* do normal option parsing */
 657        opt_complementary = "=0"; /* no non-option params */
 658        getopt32(argv, OPTION_STR, OPTION_PARAM);
 659#ifdef SYSLOGD_MARK
 660        if (option_mask32 & OPT_mark) // -m
 661                G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
 662#endif
 663        //if (option_mask32 & OPT_nofork) // -n
 664        //if (option_mask32 & OPT_outfile) // -O
 665        if (option_mask32 & OPT_loglevel) // -l
 666                G.logLevel = xatou_range(opt_l, 1, 8);
 667        //if (option_mask32 & OPT_small) // -S
 668#if ENABLE_FEATURE_ROTATE_LOGFILE
 669        if (option_mask32 & OPT_filesize) // -s
 670                G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024;
 671        if (option_mask32 & OPT_rotatecnt) // -b
 672                G.logFileRotate = xatou_range(opt_b, 0, 99);
 673#endif
 674#if ENABLE_FEATURE_IPC_SYSLOG
 675        if (opt_C) // -Cn
 676                G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
 677#endif
 678
 679        /* If they have not specified remote logging, then log locally */
 680        if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
 681                option_mask32 |= OPT_locallog;
 682
 683        /* Store away localhost's name before the fork */
 684        G.hostname = safe_gethostname();
 685        *strchrnul(G.hostname, '.') = '\0';
 686
 687        if (!(option_mask32 & OPT_nofork)) {
 688                bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
 689        }
 690        umask(0);
 691        write_pidfile("/var/run/syslogd.pid");
 692        do_syslogd();
 693        /* return EXIT_SUCCESS; */
 694}
 695
 696/* Clean up. Needed because we are included from syslogd_and_logger.c */
 697#undef G
 698#undef GLOBALS
 699#undef INIT_G
 700#undef OPTION_STR
 701#undef OPTION_DECL
 702#undef OPTION_PARAM
 703