Closing bug 730. libbb run_parts is using scandir (a GNUism),
[oweals/busybox.git] / sysklogd / syslogd.c
index a28dde6b36a8368596e6e3eb547a5533d8bf6111..9e030bd63dd957a66acdbae4ef57e1bf6d1aa51a 100644 (file)
@@ -52,16 +52,9 @@ static char LocalHostName[64];
 static int remotefd = -1;
 static struct sockaddr_in remoteaddr;
 
-/* where do we log? */
-static char *RemoteHost;
-
-/* what port to log to? */
-static int RemotePort = 514;
-
 #endif
 
 /* options */
-static unsigned option_mask;
 /* Correct regardless of combination of CONFIG_xxx */
 enum {
        OPTBIT_mark = 0, // -m
@@ -304,7 +297,7 @@ static void message(char *fmt, ...)
        fl.l_len = 1;
 
 #ifdef CONFIG_FEATURE_IPC_SYSLOG
-       if ((option_mask & OPT_circularlog) && shbuf) {
+       if ((option_mask32 & OPT_circularlog) && shbuf) {
                char b[1024];
 
                va_start(arguments, fmt);
@@ -375,19 +368,6 @@ static void message(char *fmt, ...)
        }
 }
 
-#ifdef CONFIG_FEATURE_REMOTE_LOG
-static void init_RemoteLog(void)
-{
-       memset(&remoteaddr, 0, sizeof(remoteaddr));
-       remotefd = xsocket(AF_INET, SOCK_DGRAM, 0);
-       remoteaddr.sin_family = AF_INET;
-       remoteaddr.sin_addr = *(struct in_addr *) *(xgethostbyname(RemoteHost))->h_addr_list;
-       remoteaddr.sin_port = htons(RemotePort);
-}
-#else
-void init_RemoteLog(void);
-#endif
-
 static void logMessage(int pri, char *msg)
 {
        time_t now;
@@ -423,37 +403,27 @@ static void logMessage(int pri, char *msg)
        /* todo: supress duplicates */
 
 #ifdef CONFIG_FEATURE_REMOTE_LOG
-       if (option_mask & OPT_remotelog) {
+       if (option_mask32 & OPT_remotelog) {
                char line[MAXLINE + 1];
                /* trying connect the socket */
                if (-1 == remotefd) {
-                       init_RemoteLog();
+                       remotefd = socket(AF_INET, SOCK_DGRAM, 0);
                }
-
                /* if we have a valid socket, send the message */
                if (-1 != remotefd) {
-                       now = 1;
                        snprintf(line, sizeof(line), "<%d>%s", pri, msg);
-
-retry:
-                       /* send message to remote logger */
-                       if ((-1 == sendto(remotefd, line, strlen(line), 0,
-                                                       (struct sockaddr *) &remoteaddr,
-                                                       sizeof(remoteaddr))) && (errno == EINTR)) {
-                               /* sleep now seconds and retry (with now * 2) */
-                               sleep(now);
-                               now *= 2;
-                               goto retry;
-                       }
+                       /* send message to remote logger, ignore possible error */
+                       sendto(remotefd, line, strlen(line), 0,
+                                       (struct sockaddr *) &remoteaddr, sizeof(remoteaddr));
                }
        }
 
-       if (option_mask & OPT_locallog)
+       if (option_mask32 & OPT_locallog)
 #endif
        {
                /* now spew out the message to wherever it is supposed to go */
                if (pri == 0 || LOG_PRI(pri) < logLevel) {
-                       if (option_mask & OPT_small)
+                       if (option_mask32 & OPT_small)
                                message("%s %s\n", timestamp, msg);
                        else
                                message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
@@ -468,7 +438,7 @@ static void quit_signal(int sig)
        if (ENABLE_FEATURE_IPC_SYSLOG)
                ipcsyslog_cleanup();
 
-       exit(TRUE);
+       exit(1);
 }
 
 static void domark(int sig)
@@ -560,14 +530,10 @@ static void doSyslogd(void)
        if (chmod(lfile, 0666) < 0) {
                bb_perror_msg_and_die("cannot set permission on %s", lfile);
        }
-       if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask & OPT_circularlog)) {
+       if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
                ipcsyslog_init();
        }
 
-       if (ENABLE_FEATURE_REMOTE_LOG && (option_mask & OPT_remotelog)) {
-               init_RemoteLog();
-       }
-
        logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " "BusyBox v" BB_VER );
 
        for (;;) {
@@ -608,48 +574,52 @@ int syslogd_main(int argc, char **argv)
        char *p;
 
        /* do normal option parsing */
-       option_mask = bb_getopt_ulflags(argc, argv, OPTION_STR, OPTION_PARAM);
-       if (option_mask & OPT_mark) MarkInterval = atoi(opt_m) * 60; // -m
-       //if (option_mask & OPT_nofork) // -n
-       //if (option_mask & OPT_outfile) // -O
-       if (option_mask & OPT_loglevel) { // -l
-               logLevel = atoi(opt_l);
+       getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
+       if (option_mask32 & OPT_mark) MarkInterval = xatoul_range(opt_m, 0, INT_MAX/60) * 60; // -m
+       //if (option_mask32 & OPT_nofork) // -n
+       //if (option_mask32 & OPT_outfile) // -O
+       if (option_mask32 & OPT_loglevel) { // -l
+               logLevel = xatoi_u(opt_l);
                /* Valid levels are between 1 and 8 */
                if (logLevel < 1 || logLevel > 8)
                        bb_show_usage();
        }
-       //if (option_mask & OPT_small) // -S
+       //if (option_mask32 & OPT_small) // -S
 #if ENABLE_FEATURE_ROTATE_LOGFILE
-       if (option_mask & OPT_filesize) logFileSize = atoi(opt_s) * 1024; // -s
-       if (option_mask & OPT_rotatecnt) { // -b
-               logFileRotate = atoi(opt_b);
+       if (option_mask32 & OPT_filesize) logFileSize = xatoul_range(opt_s, 0, INT_MAX/1024) * 1024; // -s
+       if (option_mask32 & OPT_rotatecnt) { // -b
+               logFileRotate = xatoi_u(opt_b);
                if (logFileRotate > 99) logFileRotate = 99; 
        }
 #endif
 #if ENABLE_FEATURE_REMOTE_LOG
-       if (option_mask & OPT_remotelog) { // -R
-               RemoteHost = xstrdup(opt_R);
-               p = strchr(RemoteHost, ':');
+       if (option_mask32 & OPT_remotelog) { // -R
+               int port = 514;
+               char *host = xstrdup(opt_R);
+               p = strchr(host, ':');
                if (p) {
-                       RemotePort = atoi(p + 1);
+                       port = xatou16(p + 1);
                        *p = '\0';
                }
+               remoteaddr.sin_family = AF_INET;
+               /* FIXME: looks ip4-specific. need to do better */
+               remoteaddr.sin_addr = *(struct in_addr *) *(xgethostbyname(host)->h_addr_list);
+               remoteaddr.sin_port = htons(port);
+               free(host);
        }
-       //if (option_mask & OPT_locallog) // -L
+       //if (option_mask32 & OPT_locallog) // -L
 #endif
 #if ENABLE_FEATURE_IPC_SYSLOG
-       if (option_mask & OPT_circularlog) { // -C
+       if (option_mask32 & OPT_circularlog) { // -C
                if (opt_C) {
-                       int buf_size = atoi(opt_C);
-                       if (buf_size >= 4)
-                               shm_size = buf_size * 1024;
+                       shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024;
                }
        }
 #endif
 
        /* If they have not specified remote logging, then log locally */
-       if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask & OPT_remotelog))
-               option_mask |= OPT_locallog;
+       if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog))
+               option_mask32 |= OPT_locallog;
 
        /* Store away localhost's name before the fork */
        gethostname(LocalHostName, sizeof(LocalHostName));
@@ -660,7 +630,7 @@ int syslogd_main(int argc, char **argv)
 
        umask(0);
 
-       if (!(option_mask & OPT_nofork)) {
+       if (!(option_mask32 & OPT_nofork)) {
 #ifdef BB_NOMMU
                vfork_daemon_rexec(0, 1, argc, argv, "-n");
 #else