aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-04-04 18:14:25 +0000
committerErik Andersen <andersen@codepoet.org>2000-04-04 18:14:25 +0000
commit983b51b17bb36e5b77cb160abdfbcf9d25675dd9 (patch)
tree13642fcca428d88a6a25f768ee45c99e9c13cdc6
parent3364d78b18386623e7af5da18ba1bb0cc6286279 (diff)
downloadbusybox-w32-983b51b17bb36e5b77cb160abdfbcf9d25675dd9.tar.gz
busybox-w32-983b51b17bb36e5b77cb160abdfbcf9d25675dd9.tar.bz2
busybox-w32-983b51b17bb36e5b77cb160abdfbcf9d25675dd9.zip
minor changes to mount/umount to support-by-ignoring the "-v" flag.
Added optional core dumping as a feature for init, and include a rewrite of syslogd so that it now supports multiple concurrent connections. -Erik
-rw-r--r--init.c24
-rw-r--r--init/init.c24
-rw-r--r--mount.c1
-rw-r--r--sysklogd/syslogd.c238
-rw-r--r--syslogd.c238
-rw-r--r--umount.c2
-rw-r--r--util-linux/mount.c1
-rw-r--r--util-linux/umount.c2
8 files changed, 354 insertions, 176 deletions
diff --git a/init.c b/init.c
index 907916537..f327a52af 100644
--- a/init.c
+++ b/init.c
@@ -45,7 +45,7 @@
45#include <sys/reboot.h> 45#include <sys/reboot.h>
46#include <sys/sysinfo.h> /* For check_free_memory() */ 46#include <sys/sysinfo.h> /* For check_free_memory() */
47#ifdef BB_SYSLOGD 47#ifdef BB_SYSLOGD
48#include <sys/syslog.h> 48# include <sys/syslog.h>
49#endif 49#endif
50#include <sys/sysmacros.h> 50#include <sys/sysmacros.h>
51#include <sys/types.h> 51#include <sys/types.h>
@@ -54,6 +54,15 @@
54#include <termios.h> 54#include <termios.h>
55#include <unistd.h> 55#include <unistd.h>
56 56
57
58/*
59 * When CORE_ENABLE_FLAG_FILE exists, setrlimit is called before
60 * process is spawned to set corelimit to unlimited.
61 */
62#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
63#include <sys/resource.h>
64#include <sys/time.h>
65
57#ifndef KERNEL_VERSION 66#ifndef KERNEL_VERSION
58#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 67#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
59#endif 68#endif
@@ -406,6 +415,16 @@ static pid_t run(char *command, char *terminal, int get_enter)
406 cmd[i] = NULL; 415 cmd[i] = NULL;
407 } 416 }
408 417
418 {
419 struct stat sb;
420 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
421 struct rlimit limit;
422 limit.rlim_cur = RLIM_INFINITY;
423 limit.rlim_max = RLIM_INFINITY;
424 setrlimit(RLIMIT_CORE, &limit);
425 }
426 }
427
409 /* Now run it. The new program will take over this PID, 428 /* Now run it. The new program will take over this PID,
410 * so nothing further in init.c should be run. */ 429 * so nothing further in init.c should be run. */
411 execve(cmd[0], cmd, environment); 430 execve(cmd[0], cmd, environment);
@@ -836,6 +855,7 @@ extern int init_main(int argc, char **argv)
836 close(1); 855 close(1);
837 close(2); 856 close(2);
838 set_term(0); 857 set_term(0);
858 chdir("/");
839 setsid(); 859 setsid();
840 860
841 /* Make sure PATH is set to something sane */ 861 /* Make sure PATH is set to something sane */
@@ -881,7 +901,7 @@ extern int init_main(int argc, char **argv)
881 * of "askfirst" shells */ 901 * of "askfirst" shells */
882 parse_inittab(); 902 parse_inittab();
883 } 903 }
884 904
885 /* Fix up argv[0] to be certain we claim to be init */ 905 /* Fix up argv[0] to be certain we claim to be init */
886 strncpy(argv[0], "init", strlen(argv[0])+1); 906 strncpy(argv[0], "init", strlen(argv[0])+1);
887 if (argc > 1) 907 if (argc > 1)
diff --git a/init/init.c b/init/init.c
index 907916537..f327a52af 100644
--- a/init/init.c
+++ b/init/init.c
@@ -45,7 +45,7 @@
45#include <sys/reboot.h> 45#include <sys/reboot.h>
46#include <sys/sysinfo.h> /* For check_free_memory() */ 46#include <sys/sysinfo.h> /* For check_free_memory() */
47#ifdef BB_SYSLOGD 47#ifdef BB_SYSLOGD
48#include <sys/syslog.h> 48# include <sys/syslog.h>
49#endif 49#endif
50#include <sys/sysmacros.h> 50#include <sys/sysmacros.h>
51#include <sys/types.h> 51#include <sys/types.h>
@@ -54,6 +54,15 @@
54#include <termios.h> 54#include <termios.h>
55#include <unistd.h> 55#include <unistd.h>
56 56
57
58/*
59 * When CORE_ENABLE_FLAG_FILE exists, setrlimit is called before
60 * process is spawned to set corelimit to unlimited.
61 */
62#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
63#include <sys/resource.h>
64#include <sys/time.h>
65
57#ifndef KERNEL_VERSION 66#ifndef KERNEL_VERSION
58#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 67#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
59#endif 68#endif
@@ -406,6 +415,16 @@ static pid_t run(char *command, char *terminal, int get_enter)
406 cmd[i] = NULL; 415 cmd[i] = NULL;
407 } 416 }
408 417
418 {
419 struct stat sb;
420 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
421 struct rlimit limit;
422 limit.rlim_cur = RLIM_INFINITY;
423 limit.rlim_max = RLIM_INFINITY;
424 setrlimit(RLIMIT_CORE, &limit);
425 }
426 }
427
409 /* Now run it. The new program will take over this PID, 428 /* Now run it. The new program will take over this PID,
410 * so nothing further in init.c should be run. */ 429 * so nothing further in init.c should be run. */
411 execve(cmd[0], cmd, environment); 430 execve(cmd[0], cmd, environment);
@@ -836,6 +855,7 @@ extern int init_main(int argc, char **argv)
836 close(1); 855 close(1);
837 close(2); 856 close(2);
838 set_term(0); 857 set_term(0);
858 chdir("/");
839 setsid(); 859 setsid();
840 860
841 /* Make sure PATH is set to something sane */ 861 /* Make sure PATH is set to something sane */
@@ -881,7 +901,7 @@ extern int init_main(int argc, char **argv)
881 * of "askfirst" shells */ 901 * of "askfirst" shells */
882 parse_inittab(); 902 parse_inittab();
883 } 903 }
884 904
885 /* Fix up argv[0] to be certain we claim to be init */ 905 /* Fix up argv[0] to be certain we claim to be init */
886 strncpy(argv[0], "init", strlen(argv[0])+1); 906 strncpy(argv[0], "init", strlen(argv[0])+1);
887 if (argc > 1) 907 if (argc > 1)
diff --git a/mount.c b/mount.c
index 30a060fc1..329c07780 100644
--- a/mount.c
+++ b/mount.c
@@ -418,6 +418,7 @@ extern int mount_main(int argc, char **argv)
418 break; 418 break;
419#endif 419#endif
420 case 'v': 420 case 'v':
421 break; /* ignore -v */
421 case 'h': 422 case 'h':
422 case '-': 423 case '-':
423 goto goodbye; 424 goto goodbye;
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 4bc1d3d72..464d7846e 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -22,21 +22,21 @@
22 */ 22 */
23 23
24#include "internal.h" 24#include "internal.h"
25#include <stdio.h> 25#include <ctype.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <netdb.h>
29#include <paths.h>
30#include <signal.h>
26#include <stdarg.h> 31#include <stdarg.h>
32#include <stdio.h>
33#include <sys/klog.h>
27#include <sys/socket.h> 34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
28#include <sys/un.h> 37#include <sys/un.h>
29#include <unistd.h>
30#include <time.h> 38#include <time.h>
31#include <sys/types.h> 39#include <unistd.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <ctype.h>
36#include <netdb.h>
37#include <sys/klog.h>
38#include <errno.h>
39#include <paths.h>
40 40
41#define ksyslog klogctl 41#define ksyslog klogctl
42extern int ksyslog(int type, char *buf, int len); 42extern int ksyslog(int type, char *buf, int len);
@@ -47,8 +47,10 @@ extern int ksyslog(int type, char *buf, int len);
47#include <sys/syslog.h> 47#include <sys/syslog.h>
48 48
49/* Path for the file where all log messages are written */ 49/* Path for the file where all log messages are written */
50#define __LOG_FILE "/var/log/messages" 50#define __LOG_FILE "/var/log/messages"
51 51
52/* Path to the unix socket */
53char lfile[PATH_MAX] = "";
52 54
53static char *logFilePath = __LOG_FILE; 55static char *logFilePath = __LOG_FILE;
54 56
@@ -70,9 +72,8 @@ static const char syslogd_usage[] =
70#endif 72#endif
71 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n"; 73 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
72 74
73
74/* Note: There is also a function called "message()" in init.c */ 75/* Note: There is also a function called "message()" in init.c */
75/* print a message to the log file */ 76/* Print a message to the log file. */
76static void message(char *fmt, ...) 77static void message(char *fmt, ...)
77{ 78{
78 int fd; 79 int fd;
@@ -145,7 +146,7 @@ static void logMessage(int pri, char *msg)
145static void quit_signal(int sig) 146static void quit_signal(int sig)
146{ 147{
147 logMessage(0, "System log daemon exiting."); 148 logMessage(0, "System log daemon exiting.");
148 unlink(_PATH_LOG); 149 unlink(lfile);
149 exit(TRUE); 150 exit(TRUE);
150} 151}
151 152
@@ -157,88 +158,134 @@ static void domark(int sig)
157 } 158 }
158} 159}
159 160
160static void doSyslogd(void) 161static void doSyslogd (void) __attribute__ ((noreturn));
162static void doSyslogd (void)
161{ 163{
162 struct sockaddr_un sunx; 164 struct sockaddr_un sunx;
163 int fd, conn;
164 size_t addrLength; 165 size_t addrLength;
165 char buf[1024]; 166 int sock_fd;
166 char *q, *p = buf; 167 fd_set readfds;
167 int readSize; 168 char lfile[PATH_MAX];
169 int t = readlink(_PATH_LOG, lfile, sizeof(lfile) - 1); /* Resolve symlinks */
168 170
169 /* Set up sig handlers */ 171 /* Set up sig handlers */
170 signal(SIGINT, quit_signal); 172 signal (SIGINT, quit_signal);
171 signal(SIGTERM, quit_signal); 173 signal (SIGTERM, quit_signal);
172 signal(SIGQUIT, quit_signal); 174 signal (SIGQUIT, quit_signal);
173 signal(SIGALRM, domark); 175 signal (SIGHUP, SIG_IGN);
174 signal(SIGHUP, SIG_IGN); 176 signal (SIGALRM, domark);
175 alarm(MarkInterval); 177 alarm (MarkInterval);
176 178
177 /* Remove any preexisting socket/file */ 179 if (t == -1)
178 unlink(_PATH_LOG); 180 strncpy(lfile, _PATH_LOG, sizeof(lfile));
179 181 else
180 memset(&sunx, 0, sizeof(sunx)); 182 lfile[t] = '\0';
181 sunx.sun_family = AF_UNIX; /* Unix domain socket */ 183
182 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path)); 184 unlink (lfile);
183 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 185
184 perror("Couldn't obtain descriptor for socket " _PATH_LOG); 186 memset (&sunx, 0, sizeof(sunx));
185 exit(FALSE); 187
188 sunx.sun_family = AF_UNIX;
189 strncpy (sunx.sun_path, lfile, sizeof(sunx.sun_path));
190 if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
191 perror ("Couldn't obtain descriptor for socket " _PATH_LOG);
192 exit (FALSE);
186 } 193 }
187 194
188 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); 195 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
189 if ((bind(fd, (struct sockaddr *) &sunx, addrLength)) || 196 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) ||
190 (listen(fd, 5))) { 197 (listen (sock_fd, 5))) {
191 perror("Could not connect to socket " _PATH_LOG); 198 perror ("Could not connect to socket " _PATH_LOG);
192 exit(FALSE); 199 exit (FALSE);
193 } 200 }
194 201
195 umask(0); 202 if (chmod (lfile, 0666) < 0) {
196 if (chmod(_PATH_LOG, 0666) < 0) { 203 perror ("Could not set permission on " _PATH_LOG);
197 perror("Could not set permission on " _PATH_LOG); 204 exit (FALSE);
198 exit(FALSE);
199 } 205 }
200 206
201 logMessage(0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); 207 FD_ZERO (&readfds);
202 208 FD_SET (sock_fd, &readfds);
203 209
204 while ((conn = accept(fd, (struct sockaddr *) &sunx, 210 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
205 &addrLength)) >= 0) { 211
206 while ((readSize = read(conn, buf, sizeof(buf))) > 0) { 212 for (;;) {
207 char line[1025]; 213 int n_ready;
208 unsigned char c; 214 int fd;
209 int pri = (LOG_USER | LOG_NOTICE); 215
210 216 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
211 memset(line, 0, sizeof(line)); 217 if (errno == EINTR) continue; /* alarm may have happened. */
212 p = buf; 218 perror ("select");
213 q = line; 219 exit (FALSE);
214 while (p && (c = *p) && q < &line[sizeof(line) - 1]) { 220 }
215 if (c == '<') { 221
216 /* Parse the magic priority number */ 222 /* Skip stdin, stdout, stderr */
217 pri = 0; 223 for (fd = 3; fd <= FD_SETSIZE; fd++) {
218 while (isdigit(*(++p))) { 224 if (FD_ISSET (fd, &readfds)) {
219 pri = 10 * pri + (*p - '0'); 225 if (fd == sock_fd) {
226 int conn;
227 if ((conn = accept(sock_fd, (struct sockaddr *) &sunx,
228 &addrLength)) < 0) {
229 perror ("accept");
230 exit (FALSE); /* #### ??? */
220 } 231 }
221 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) 232 FD_SET (conn, &readfds);
222 pri = (LOG_USER | LOG_NOTICE);
223 } else if (c == '\n') {
224 *q++ = ' ';
225 } else if (iscntrl(c) && (c < 0177)) {
226 *q++ = '^';
227 *q++ = c ^ 0100;
228 } else {
229 *q++ = c;
230 } 233 }
231 p++; 234 else {
232 } 235#define BUFSIZE 1024 + 1
233 *q = '\0'; 236 char buf[BUFSIZE];
237 char *q, *p;
238 int n_read;
234 239
235 /* Now log it */ 240 n_read = read (fd, buf, BUFSIZE);
236 logMessage(pri, line); 241
242 if (n_read < 0) {
243 perror ("read error");
244 goto close_fd;
245 }
246 else if (n_read > 0) {
247 char line[BUFSIZE];
248 unsigned char c;
249 int pri = (LOG_USER | LOG_NOTICE);
250
251 memset (line, 0, sizeof(line));
252 p = buf;
253 q = line;
254 while (p && (c = *p) && q < &line[sizeof(line) - 1]) {
255 if (c == '<') {
256 /* Parse the magic priority number */
257 pri = 0;
258 while (isdigit(*(++p))) {
259 pri = 10 * pri + (*p - '0');
260 }
261 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
262 pri = (LOG_USER | LOG_NOTICE);
263 } else if (c == '\n') {
264 *q++ = ' ';
265 } else if (iscntrl(c) && (c < 0177)) {
266 *q++ = '^';
267 *q++ = c ^ 0100;
268 } else {
269 *q++ = c;
270 }
271 p++;
272 }
273 *q = '\0';
274
275 /* Now log it */
276 logMessage(pri, line);
277
278 close_fd:
279 close (fd);
280 FD_CLR (fd, &readfds);
281 }
282 else { /* EOF */
283 goto close_fd;
284 }
285 }
286 }
237 } 287 }
238 close(conn);
239 } 288 }
240
241 close(fd);
242} 289}
243 290
244#ifdef BB_KLOGD 291#ifdef BB_KLOGD
@@ -251,7 +298,8 @@ static void klogd_signal(int sig)
251 exit(TRUE); 298 exit(TRUE);
252} 299}
253 300
254static void doKlogd(void) 301static void doKlogd (void) __attribute__ ((noreturn));
302static void doKlogd (void)
255{ 303{
256 int priority = LOG_INFO; 304 int priority = LOG_INFO;
257 char log_buffer[4096]; 305 char log_buffer[4096];
@@ -317,6 +365,16 @@ static void doKlogd(void)
317 365
318#endif 366#endif
319 367
368static void daemon_init (char **argv, char *dz, void fn (void)) __attribute__ ((noreturn));
369static void daemon_init (char **argv, char *dz, void fn (void))
370{
371 setsid();
372 chdir ("/");
373 strncpy(argv[0], dz, strlen(argv[0]));
374 fn();
375 exit(0);
376}
377
320extern int syslogd_main(int argc, char **argv) 378extern int syslogd_main(int argc, char **argv)
321{ 379{
322 int pid, klogd_pid; 380 int pid, klogd_pid;
@@ -366,13 +424,14 @@ extern int syslogd_main(int argc, char **argv)
366 *p++ = '\0'; 424 *p++ = '\0';
367 } 425 }
368 426
427 umask(0);
428
369#ifdef BB_KLOGD 429#ifdef BB_KLOGD
370 /* Start up the klogd process */ 430 /* Start up the klogd process */
371 if (startKlogd == TRUE) { 431 if (startKlogd == TRUE) {
372 klogd_pid = fork(); 432 klogd_pid = fork();
373 if (klogd_pid == 0) { 433 if (klogd_pid == 0) {
374 strncpy(argv[0], "klogd", strlen(argv[0])); 434 daemon_init (argv, "klogd", doKlogd);
375 doKlogd();
376 } 435 }
377 } 436 }
378#endif 437#endif
@@ -382,8 +441,7 @@ extern int syslogd_main(int argc, char **argv)
382 if (pid < 0) 441 if (pid < 0)
383 exit(pid); 442 exit(pid);
384 else if (pid == 0) { 443 else if (pid == 0) {
385 strncpy(argv[0], "syslogd", strlen(argv[0])); 444 daemon_init (argv, "syslogd", doSyslogd);
386 doSyslogd();
387 } 445 }
388 } else { 446 } else {
389 doSyslogd(); 447 doSyslogd();
@@ -391,3 +449,11 @@ extern int syslogd_main(int argc, char **argv)
391 449
392 exit(TRUE); 450 exit(TRUE);
393} 451}
452
453/*
454 * Local Variables
455 * c-file-style: "linux"
456 * c-basic-offset: 4
457 * tab-width: 4
458 * End:
459 */
diff --git a/syslogd.c b/syslogd.c
index 4bc1d3d72..464d7846e 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -22,21 +22,21 @@
22 */ 22 */
23 23
24#include "internal.h" 24#include "internal.h"
25#include <stdio.h> 25#include <ctype.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <netdb.h>
29#include <paths.h>
30#include <signal.h>
26#include <stdarg.h> 31#include <stdarg.h>
32#include <stdio.h>
33#include <sys/klog.h>
27#include <sys/socket.h> 34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
28#include <sys/un.h> 37#include <sys/un.h>
29#include <unistd.h>
30#include <time.h> 38#include <time.h>
31#include <sys/types.h> 39#include <unistd.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <ctype.h>
36#include <netdb.h>
37#include <sys/klog.h>
38#include <errno.h>
39#include <paths.h>
40 40
41#define ksyslog klogctl 41#define ksyslog klogctl
42extern int ksyslog(int type, char *buf, int len); 42extern int ksyslog(int type, char *buf, int len);
@@ -47,8 +47,10 @@ extern int ksyslog(int type, char *buf, int len);
47#include <sys/syslog.h> 47#include <sys/syslog.h>
48 48
49/* Path for the file where all log messages are written */ 49/* Path for the file where all log messages are written */
50#define __LOG_FILE "/var/log/messages" 50#define __LOG_FILE "/var/log/messages"
51 51
52/* Path to the unix socket */
53char lfile[PATH_MAX] = "";
52 54
53static char *logFilePath = __LOG_FILE; 55static char *logFilePath = __LOG_FILE;
54 56
@@ -70,9 +72,8 @@ static const char syslogd_usage[] =
70#endif 72#endif
71 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n"; 73 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
72 74
73
74/* Note: There is also a function called "message()" in init.c */ 75/* Note: There is also a function called "message()" in init.c */
75/* print a message to the log file */ 76/* Print a message to the log file. */
76static void message(char *fmt, ...) 77static void message(char *fmt, ...)
77{ 78{
78 int fd; 79 int fd;
@@ -145,7 +146,7 @@ static void logMessage(int pri, char *msg)
145static void quit_signal(int sig) 146static void quit_signal(int sig)
146{ 147{
147 logMessage(0, "System log daemon exiting."); 148 logMessage(0, "System log daemon exiting.");
148 unlink(_PATH_LOG); 149 unlink(lfile);
149 exit(TRUE); 150 exit(TRUE);
150} 151}
151 152
@@ -157,88 +158,134 @@ static void domark(int sig)
157 } 158 }
158} 159}
159 160
160static void doSyslogd(void) 161static void doSyslogd (void) __attribute__ ((noreturn));
162static void doSyslogd (void)
161{ 163{
162 struct sockaddr_un sunx; 164 struct sockaddr_un sunx;
163 int fd, conn;
164 size_t addrLength; 165 size_t addrLength;
165 char buf[1024]; 166 int sock_fd;
166 char *q, *p = buf; 167 fd_set readfds;
167 int readSize; 168 char lfile[PATH_MAX];
169 int t = readlink(_PATH_LOG, lfile, sizeof(lfile) - 1); /* Resolve symlinks */
168 170
169 /* Set up sig handlers */ 171 /* Set up sig handlers */
170 signal(SIGINT, quit_signal); 172 signal (SIGINT, quit_signal);
171 signal(SIGTERM, quit_signal); 173 signal (SIGTERM, quit_signal);
172 signal(SIGQUIT, quit_signal); 174 signal (SIGQUIT, quit_signal);
173 signal(SIGALRM, domark); 175 signal (SIGHUP, SIG_IGN);
174 signal(SIGHUP, SIG_IGN); 176 signal (SIGALRM, domark);
175 alarm(MarkInterval); 177 alarm (MarkInterval);
176 178
177 /* Remove any preexisting socket/file */ 179 if (t == -1)
178 unlink(_PATH_LOG); 180 strncpy(lfile, _PATH_LOG, sizeof(lfile));
179 181 else
180 memset(&sunx, 0, sizeof(sunx)); 182 lfile[t] = '\0';
181 sunx.sun_family = AF_UNIX; /* Unix domain socket */ 183
182 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path)); 184 unlink (lfile);
183 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 185
184 perror("Couldn't obtain descriptor for socket " _PATH_LOG); 186 memset (&sunx, 0, sizeof(sunx));
185 exit(FALSE); 187
188 sunx.sun_family = AF_UNIX;
189 strncpy (sunx.sun_path, lfile, sizeof(sunx.sun_path));
190 if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
191 perror ("Couldn't obtain descriptor for socket " _PATH_LOG);
192 exit (FALSE);
186 } 193 }
187 194
188 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); 195 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
189 if ((bind(fd, (struct sockaddr *) &sunx, addrLength)) || 196 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) ||
190 (listen(fd, 5))) { 197 (listen (sock_fd, 5))) {
191 perror("Could not connect to socket " _PATH_LOG); 198 perror ("Could not connect to socket " _PATH_LOG);
192 exit(FALSE); 199 exit (FALSE);
193 } 200 }
194 201
195 umask(0); 202 if (chmod (lfile, 0666) < 0) {
196 if (chmod(_PATH_LOG, 0666) < 0) { 203 perror ("Could not set permission on " _PATH_LOG);
197 perror("Could not set permission on " _PATH_LOG); 204 exit (FALSE);
198 exit(FALSE);
199 } 205 }
200 206
201 logMessage(0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); 207 FD_ZERO (&readfds);
202 208 FD_SET (sock_fd, &readfds);
203 209
204 while ((conn = accept(fd, (struct sockaddr *) &sunx, 210 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
205 &addrLength)) >= 0) { 211
206 while ((readSize = read(conn, buf, sizeof(buf))) > 0) { 212 for (;;) {
207 char line[1025]; 213 int n_ready;
208 unsigned char c; 214 int fd;
209 int pri = (LOG_USER | LOG_NOTICE); 215
210 216 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
211 memset(line, 0, sizeof(line)); 217 if (errno == EINTR) continue; /* alarm may have happened. */
212 p = buf; 218 perror ("select");
213 q = line; 219 exit (FALSE);
214 while (p && (c = *p) && q < &line[sizeof(line) - 1]) { 220 }
215 if (c == '<') { 221
216 /* Parse the magic priority number */ 222 /* Skip stdin, stdout, stderr */
217 pri = 0; 223 for (fd = 3; fd <= FD_SETSIZE; fd++) {
218 while (isdigit(*(++p))) { 224 if (FD_ISSET (fd, &readfds)) {
219 pri = 10 * pri + (*p - '0'); 225 if (fd == sock_fd) {
226 int conn;
227 if ((conn = accept(sock_fd, (struct sockaddr *) &sunx,
228 &addrLength)) < 0) {
229 perror ("accept");
230 exit (FALSE); /* #### ??? */
220 } 231 }
221 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) 232 FD_SET (conn, &readfds);
222 pri = (LOG_USER | LOG_NOTICE);
223 } else if (c == '\n') {
224 *q++ = ' ';
225 } else if (iscntrl(c) && (c < 0177)) {
226 *q++ = '^';
227 *q++ = c ^ 0100;
228 } else {
229 *q++ = c;
230 } 233 }
231 p++; 234 else {
232 } 235#define BUFSIZE 1024 + 1
233 *q = '\0'; 236 char buf[BUFSIZE];
237 char *q, *p;
238 int n_read;
234 239
235 /* Now log it */ 240 n_read = read (fd, buf, BUFSIZE);
236 logMessage(pri, line); 241
242 if (n_read < 0) {
243 perror ("read error");
244 goto close_fd;
245 }
246 else if (n_read > 0) {
247 char line[BUFSIZE];
248 unsigned char c;
249 int pri = (LOG_USER | LOG_NOTICE);
250
251 memset (line, 0, sizeof(line));
252 p = buf;
253 q = line;
254 while (p && (c = *p) && q < &line[sizeof(line) - 1]) {
255 if (c == '<') {
256 /* Parse the magic priority number */
257 pri = 0;
258 while (isdigit(*(++p))) {
259 pri = 10 * pri + (*p - '0');
260 }
261 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
262 pri = (LOG_USER | LOG_NOTICE);
263 } else if (c == '\n') {
264 *q++ = ' ';
265 } else if (iscntrl(c) && (c < 0177)) {
266 *q++ = '^';
267 *q++ = c ^ 0100;
268 } else {
269 *q++ = c;
270 }
271 p++;
272 }
273 *q = '\0';
274
275 /* Now log it */
276 logMessage(pri, line);
277
278 close_fd:
279 close (fd);
280 FD_CLR (fd, &readfds);
281 }
282 else { /* EOF */
283 goto close_fd;
284 }
285 }
286 }
237 } 287 }
238 close(conn);
239 } 288 }
240
241 close(fd);
242} 289}
243 290
244#ifdef BB_KLOGD 291#ifdef BB_KLOGD
@@ -251,7 +298,8 @@ static void klogd_signal(int sig)
251 exit(TRUE); 298 exit(TRUE);
252} 299}
253 300
254static void doKlogd(void) 301static void doKlogd (void) __attribute__ ((noreturn));
302static void doKlogd (void)
255{ 303{
256 int priority = LOG_INFO; 304 int priority = LOG_INFO;
257 char log_buffer[4096]; 305 char log_buffer[4096];
@@ -317,6 +365,16 @@ static void doKlogd(void)
317 365
318#endif 366#endif
319 367
368static void daemon_init (char **argv, char *dz, void fn (void)) __attribute__ ((noreturn));
369static void daemon_init (char **argv, char *dz, void fn (void))
370{
371 setsid();
372 chdir ("/");
373 strncpy(argv[0], dz, strlen(argv[0]));
374 fn();
375 exit(0);
376}
377
320extern int syslogd_main(int argc, char **argv) 378extern int syslogd_main(int argc, char **argv)
321{ 379{
322 int pid, klogd_pid; 380 int pid, klogd_pid;
@@ -366,13 +424,14 @@ extern int syslogd_main(int argc, char **argv)
366 *p++ = '\0'; 424 *p++ = '\0';
367 } 425 }
368 426
427 umask(0);
428
369#ifdef BB_KLOGD 429#ifdef BB_KLOGD
370 /* Start up the klogd process */ 430 /* Start up the klogd process */
371 if (startKlogd == TRUE) { 431 if (startKlogd == TRUE) {
372 klogd_pid = fork(); 432 klogd_pid = fork();
373 if (klogd_pid == 0) { 433 if (klogd_pid == 0) {
374 strncpy(argv[0], "klogd", strlen(argv[0])); 434 daemon_init (argv, "klogd", doKlogd);
375 doKlogd();
376 } 435 }
377 } 436 }
378#endif 437#endif
@@ -382,8 +441,7 @@ extern int syslogd_main(int argc, char **argv)
382 if (pid < 0) 441 if (pid < 0)
383 exit(pid); 442 exit(pid);
384 else if (pid == 0) { 443 else if (pid == 0) {
385 strncpy(argv[0], "syslogd", strlen(argv[0])); 444 daemon_init (argv, "syslogd", doSyslogd);
386 doSyslogd();
387 } 445 }
388 } else { 446 } else {
389 doSyslogd(); 447 doSyslogd();
@@ -391,3 +449,11 @@ extern int syslogd_main(int argc, char **argv)
391 449
392 exit(TRUE); 450 exit(TRUE);
393} 451}
452
453/*
454 * Local Variables
455 * c-file-style: "linux"
456 * c-basic-offset: 4
457 * tab-width: 4
458 * End:
459 */
diff --git a/umount.c b/umount.c
index 23973fc85..3f84aa296 100644
--- a/umount.c
+++ b/umount.c
@@ -260,6 +260,8 @@ extern int umount_main(int argc, char **argv)
260 doRemount = TRUE; 260 doRemount = TRUE;
261 break; 261 break;
262#endif 262#endif
263 case 'v':
264 break; /* ignore -v */
263 default: 265 default:
264 usage(umount_usage); 266 usage(umount_usage);
265 } 267 }
diff --git a/util-linux/mount.c b/util-linux/mount.c
index 30a060fc1..329c07780 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -418,6 +418,7 @@ extern int mount_main(int argc, char **argv)
418 break; 418 break;
419#endif 419#endif
420 case 'v': 420 case 'v':
421 break; /* ignore -v */
421 case 'h': 422 case 'h':
422 case '-': 423 case '-':
423 goto goodbye; 424 goto goodbye;
diff --git a/util-linux/umount.c b/util-linux/umount.c
index 23973fc85..3f84aa296 100644
--- a/util-linux/umount.c
+++ b/util-linux/umount.c
@@ -260,6 +260,8 @@ extern int umount_main(int argc, char **argv)
260 doRemount = TRUE; 260 doRemount = TRUE;
261 break; 261 break;
262#endif 262#endif
263 case 'v':
264 break; /* ignore -v */
263 default: 265 default:
264 usage(umount_usage); 266 usage(umount_usage);
265 } 267 }