- struct sockaddr_un sunx;
- int fd, conn;
- size_t addrLength;
- char buf[1024];
- char *q, *p = buf;
- int readSize;
-
- /* Set up sig handlers */
- signal(SIGINT, quit_signal);
- signal(SIGTERM, quit_signal);
- signal(SIGQUIT, quit_signal);
- signal(SIGHUP, restart_signal);
- signal(SIGALRM, domark);
- alarm(MarkInterval);
-
- /* Remove any preexisting socket/file */
- unlink(_PATH_LOG);
-
- memset(&sunx, 0, sizeof(sunx));
- sunx.sun_family = AF_UNIX; /* Unix domain socket */
- strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
- if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
- perror("Couldn't obtain descriptor for socket " _PATH_LOG);
- exit( FALSE);
- }
-
- addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
- if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
- (listen(fd, 5)) )
- {
- perror("Could not connect to socket " _PATH_LOG);
- exit( FALSE);
- }
-
- umask(0);
- if (chmod(_PATH_LOG, 0666) < 0) {
- perror("Could not set permission on " _PATH_LOG);
- exit (FALSE);
- }
-
- logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
- "BusyBox v" BB_VER " (" BB_BT ")");
-
-
- while ((conn = accept(fd, (struct sockaddr *) &sunx,
- &addrLength)) >= 0)
- {
- while ((readSize=read(conn, buf, sizeof(buf))) > 0)
- {
- char line[1025];
- unsigned char c;
- int pri = (LOG_USER|LOG_NOTICE);
-
- memset (line, 0, sizeof(line));
- p = buf;
- q = line;
- while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
- if (c == '<') {
- /* Parse the magic priority number */
- pri = 0;
- while (isdigit(*(++p))) {
- pri = 10 * pri + (*p - '0');
- }
- if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
- pri = (LOG_USER|LOG_NOTICE);
- } else if (c == '\n') {
- *q++ = ' ';
- } else if (iscntrl(c)&&(c<0177)) {
- *q++ = '^';
- *q++ = c ^ 0100;
- } else {
- *q++ = c;
+ struct sockaddr_un sunx;
+ socklen_t addrLength;
+
+
+ int sock_fd;
+ fd_set fds;
+
+ char lfile[BUFSIZ];
+
+ /* Set up signal handlers. */
+ signal (SIGINT, quit_signal);
+ signal (SIGTERM, quit_signal);
+ signal (SIGQUIT, quit_signal);
+ signal (SIGHUP, SIG_IGN);
+ signal (SIGCLD, SIG_IGN);
+ signal (SIGALRM, domark);
+ alarm (MarkInterval);
+
+ /* Create the syslog file so realpath() can work. */
+ close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
+ if (realpath (_PATH_LOG, lfile) == NULL)
+ fatalError ("Could not resolve path to " _PATH_LOG ": %s\n", strerror (errno));
+
+ unlink (lfile);
+
+ memset (&sunx, 0, sizeof (sunx));
+ sunx.sun_family = AF_UNIX;
+ strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
+ if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
+ fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
+
+ addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
+ if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
+ fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
+
+ if (chmod (lfile, 0666) < 0)
+ fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
+
+ FD_ZERO (&fds);
+ FD_SET (sock_fd, &fds);
+
+ #ifdef BB_FEATURE_REMOTE_LOG
+ if (doRemoteLog == TRUE){
+ init_RemoteLog();
+ }
+ #endif
+
+ logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
+
+ for (;;) {
+
+ fd_set readfds;
+ int n_ready;
+ int fd;
+
+ memcpy (&readfds, &fds, sizeof (fds));
+
+ if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
+ if (errno == EINTR) continue; /* alarm may have happened. */
+ fatalError ("select error: %s\n", strerror (errno));