aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-04-19 18:52:56 +0000
committerErik Andersen <andersen@codepoet.org>2000-04-19 18:52:56 +0000
commite3ed156eeb241234f0ad7d12363172e655209654 (patch)
tree60af21090f97b033785093ed00a123006808cf88
parent1101d23604dae236063938c23470c85c17f76988 (diff)
downloadbusybox-w32-e3ed156eeb241234f0ad7d12363172e655209654.tar.gz
busybox-w32-e3ed156eeb241234f0ad7d12363172e655209654.tar.bz2
busybox-w32-e3ed156eeb241234f0ad7d12363172e655209654.zip
Make the sys logger for so that concurrent logging will work
properly (see tests/syslog_test.c for example). -Erik
-rw-r--r--sysklogd/syslogd.c104
-rw-r--r--syslogd.c104
-rw-r--r--tests/.cvsignore1
-rw-r--r--tests/Makefile2
-rw-r--r--tests/syslog_test.c19
5 files changed, 150 insertions, 80 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 4cfb458ed..004e6ab1d 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -80,14 +80,23 @@ static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
80static void message (char *fmt, ...) 80static void message (char *fmt, ...)
81{ 81{
82 int fd; 82 int fd;
83 struct flock fl;
83 va_list arguments; 84 va_list arguments;
84 85
86 fl.l_whence = SEEK_SET;
87 fl.l_start = 0;
88 fl.l_len = 1;
89
85 if ((fd = device_open (logFilePath, 90 if ((fd = device_open (logFilePath,
86 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | 91 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
87 O_NONBLOCK)) >= 0) { 92 O_NONBLOCK)) >= 0) {
93 fl.l_type = F_WRLCK;
94 fcntl (fd, F_SETLKW, &fl);
88 va_start (arguments, fmt); 95 va_start (arguments, fmt);
89 vdprintf (fd, fmt, arguments); 96 vdprintf (fd, fmt, arguments);
90 va_end (arguments); 97 va_end (arguments);
98 fl.l_type = F_UNLCK;
99 fcntl (fd, F_SETLKW, &fl);
91 close (fd); 100 close (fd);
92 } else { 101 } else {
93 /* Always send console messages to /dev/console so people will see them. */ 102 /* Always send console messages to /dev/console so people will see them. */
@@ -173,6 +182,7 @@ static void doSyslogd (void)
173 signal (SIGTERM, quit_signal); 182 signal (SIGTERM, quit_signal);
174 signal (SIGQUIT, quit_signal); 183 signal (SIGQUIT, quit_signal);
175 signal (SIGHUP, SIG_IGN); 184 signal (SIGHUP, SIG_IGN);
185 signal (SIGCLD, SIG_IGN);
176 signal (SIGALRM, domark); 186 signal (SIGALRM, domark);
177 alarm (MarkInterval); 187 alarm (MarkInterval);
178 188
@@ -216,54 +226,68 @@ static void doSyslogd (void)
216 226
217 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) { 227 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
218 if (FD_ISSET (fd, &readfds)) { 228 if (FD_ISSET (fd, &readfds)) {
229
219 --n_ready; 230 --n_ready;
231
220 if (fd == sock_fd) { 232 if (fd == sock_fd) {
221 int conn; 233
234 int conn;
235 pid_t pid;
236
222 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) { 237 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
223 fatalError ("accept error: %s\n", strerror (errno)); 238 fatalError ("accept error: %s\n", strerror (errno));
224 } 239 }
225 FD_SET (conn, &fds);
226 continue;
227 }
228 else {
229# define BUFSIZE 1023
230 char buf[ BUFSIZE + 1 ];
231 int n_read;
232 240
233 while ((n_read = read (fd, buf, BUFSIZE )) > 0) { 241 pid = fork();
234 242
235 int pri = (LOG_USER | LOG_NOTICE); 243 if (pid < 0) {
236 char line[ BUFSIZE + 1 ]; 244 perror ("syslogd: fork");
237 unsigned char c; 245 close (conn);
238 char *p = buf, *q = line; 246 continue;
247 }
239 248
240 buf[ n_read - 1 ] = '\0'; 249 if (pid > 0) {
241 250
242 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) { 251# define BUFSIZE 1023
243 if (c == '<') { 252 char buf[ BUFSIZE + 1 ];
253 int n_read;
254
255 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
256
257 int pri = (LOG_USER | LOG_NOTICE);
258 char line[ BUFSIZE + 1 ];
259 unsigned char c;
260
261 char *p = buf, *q = line;
262
263 buf[ n_read - 1 ] = '\0';
264
265 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
266 if (c == '<') {
244 /* Parse the magic priority number. */ 267 /* Parse the magic priority number. */
245 pri = 0; 268 pri = 0;
246 while (isdigit (*(++p))) { 269 while (isdigit (*(++p))) {
247 pri = 10 * pri + (*p - '0'); 270 pri = 10 * pri + (*p - '0');
271 }
272 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
273 pri = (LOG_USER | LOG_NOTICE);
274 } else if (c == '\n') {
275 *q++ = ' ';
276 } else if (iscntrl (c) && (c < 0177)) {
277 *q++ = '^';
278 *q++ = c ^ 0100;
279 } else {
280 *q++ = c;
248 } 281 }
249 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) 282 p++;
250 pri = (LOG_USER | LOG_NOTICE);
251 } else if (c == '\n') {
252 *q++ = ' ';
253 } else if (iscntrl (c) && (c < 0177)) {
254 *q++ = '^';
255 *q++ = c ^ 0100;
256 } else {
257 *q++ = c;
258 } 283 }
259 p++; 284 *q = '\0';
285 /* Now log it */
286 logMessage (pri, line);
260 } 287 }
261 *q = '\0'; 288 exit (0);
262 /* Now log it */
263 logMessage (pri, line);
264 } 289 }
265 close (fd); 290 close (conn);
266 FD_CLR (fd, &fds);
267 } 291 }
268 } 292 }
269 } 293 }
@@ -432,9 +456,9 @@ extern int syslogd_main(int argc, char **argv)
432} 456}
433 457
434/* 458/*
435 * Local Variables 459Local Variables
436 * c-file-style: "linux" 460c-file-style: "linux"
437 * c-basic-offset: 4 461c-basic-offset: 4
438 * tab-width: 4 462tab-width: 4
439 * End: 463End:
440 */ 464*/
diff --git a/syslogd.c b/syslogd.c
index 4cfb458ed..004e6ab1d 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -80,14 +80,23 @@ static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
80static void message (char *fmt, ...) 80static void message (char *fmt, ...)
81{ 81{
82 int fd; 82 int fd;
83 struct flock fl;
83 va_list arguments; 84 va_list arguments;
84 85
86 fl.l_whence = SEEK_SET;
87 fl.l_start = 0;
88 fl.l_len = 1;
89
85 if ((fd = device_open (logFilePath, 90 if ((fd = device_open (logFilePath,
86 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | 91 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
87 O_NONBLOCK)) >= 0) { 92 O_NONBLOCK)) >= 0) {
93 fl.l_type = F_WRLCK;
94 fcntl (fd, F_SETLKW, &fl);
88 va_start (arguments, fmt); 95 va_start (arguments, fmt);
89 vdprintf (fd, fmt, arguments); 96 vdprintf (fd, fmt, arguments);
90 va_end (arguments); 97 va_end (arguments);
98 fl.l_type = F_UNLCK;
99 fcntl (fd, F_SETLKW, &fl);
91 close (fd); 100 close (fd);
92 } else { 101 } else {
93 /* Always send console messages to /dev/console so people will see them. */ 102 /* Always send console messages to /dev/console so people will see them. */
@@ -173,6 +182,7 @@ static void doSyslogd (void)
173 signal (SIGTERM, quit_signal); 182 signal (SIGTERM, quit_signal);
174 signal (SIGQUIT, quit_signal); 183 signal (SIGQUIT, quit_signal);
175 signal (SIGHUP, SIG_IGN); 184 signal (SIGHUP, SIG_IGN);
185 signal (SIGCLD, SIG_IGN);
176 signal (SIGALRM, domark); 186 signal (SIGALRM, domark);
177 alarm (MarkInterval); 187 alarm (MarkInterval);
178 188
@@ -216,54 +226,68 @@ static void doSyslogd (void)
216 226
217 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) { 227 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
218 if (FD_ISSET (fd, &readfds)) { 228 if (FD_ISSET (fd, &readfds)) {
229
219 --n_ready; 230 --n_ready;
231
220 if (fd == sock_fd) { 232 if (fd == sock_fd) {
221 int conn; 233
234 int conn;
235 pid_t pid;
236
222 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) { 237 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
223 fatalError ("accept error: %s\n", strerror (errno)); 238 fatalError ("accept error: %s\n", strerror (errno));
224 } 239 }
225 FD_SET (conn, &fds);
226 continue;
227 }
228 else {
229# define BUFSIZE 1023
230 char buf[ BUFSIZE + 1 ];
231 int n_read;
232 240
233 while ((n_read = read (fd, buf, BUFSIZE )) > 0) { 241 pid = fork();
234 242
235 int pri = (LOG_USER | LOG_NOTICE); 243 if (pid < 0) {
236 char line[ BUFSIZE + 1 ]; 244 perror ("syslogd: fork");
237 unsigned char c; 245 close (conn);
238 char *p = buf, *q = line; 246 continue;
247 }
239 248
240 buf[ n_read - 1 ] = '\0'; 249 if (pid > 0) {
241 250
242 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) { 251# define BUFSIZE 1023
243 if (c == '<') { 252 char buf[ BUFSIZE + 1 ];
253 int n_read;
254
255 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
256
257 int pri = (LOG_USER | LOG_NOTICE);
258 char line[ BUFSIZE + 1 ];
259 unsigned char c;
260
261 char *p = buf, *q = line;
262
263 buf[ n_read - 1 ] = '\0';
264
265 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
266 if (c == '<') {
244 /* Parse the magic priority number. */ 267 /* Parse the magic priority number. */
245 pri = 0; 268 pri = 0;
246 while (isdigit (*(++p))) { 269 while (isdigit (*(++p))) {
247 pri = 10 * pri + (*p - '0'); 270 pri = 10 * pri + (*p - '0');
271 }
272 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
273 pri = (LOG_USER | LOG_NOTICE);
274 } else if (c == '\n') {
275 *q++ = ' ';
276 } else if (iscntrl (c) && (c < 0177)) {
277 *q++ = '^';
278 *q++ = c ^ 0100;
279 } else {
280 *q++ = c;
248 } 281 }
249 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) 282 p++;
250 pri = (LOG_USER | LOG_NOTICE);
251 } else if (c == '\n') {
252 *q++ = ' ';
253 } else if (iscntrl (c) && (c < 0177)) {
254 *q++ = '^';
255 *q++ = c ^ 0100;
256 } else {
257 *q++ = c;
258 } 283 }
259 p++; 284 *q = '\0';
285 /* Now log it */
286 logMessage (pri, line);
260 } 287 }
261 *q = '\0'; 288 exit (0);
262 /* Now log it */
263 logMessage (pri, line);
264 } 289 }
265 close (fd); 290 close (conn);
266 FD_CLR (fd, &fds);
267 } 291 }
268 } 292 }
269 } 293 }
@@ -432,9 +456,9 @@ extern int syslogd_main(int argc, char **argv)
432} 456}
433 457
434/* 458/*
435 * Local Variables 459Local Variables
436 * c-file-style: "linux" 460c-file-style: "linux"
437 * c-basic-offset: 4 461c-basic-offset: 4
438 * tab-width: 4 462tab-width: 4
439 * End: 463End:
440 */ 464*/
diff --git a/tests/.cvsignore b/tests/.cvsignore
index 5f8452313..3645cf92f 100644
--- a/tests/.cvsignore
+++ b/tests/.cvsignore
@@ -13,3 +13,4 @@ mv
13mv_*.bb 13mv_*.bb
14mv_*.gnu 14mv_*.gnu
15mv_tests 15mv_tests
16syslog_test
diff --git a/tests/Makefile b/tests/Makefile
index c4fb0e911..508bc64f2 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -29,3 +29,5 @@ BBL := $(shell pushd .. >/dev/null && \
29${BBL}: ../busybox 29${BBL}: ../busybox
30 rm -f $@ 30 rm -f $@
31 ln ../busybox $@ 31 ln ../busybox $@
32
33syslog_test: syslog_test.c
diff --git a/tests/syslog_test.c b/tests/syslog_test.c
new file mode 100644
index 000000000..fb4c691b1
--- /dev/null
+++ b/tests/syslog_test.c
@@ -0,0 +1,19 @@
1#include <syslog.h>
2
3int do_log(char* msg, int delay)
4{
5 openlog("testlog", LOG_PID, LOG_DAEMON);
6 while(1) {
7 syslog(LOG_ERR, "%s: testing one, two, three\n", msg);
8 sleep(delay);
9 }
10 closelog();
11 return(0);
12};
13
14int main(void)
15{
16 if (fork()==0)
17 do_log("A", 2);
18 do_log("B", 3);
19}