aboutsummaryrefslogtreecommitdiff
path: root/sysklogd
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-04 17:59:59 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-04 17:59:59 +0000
commita9b60e93eeb4d2706ebc95bafb18bd4267a03d6f (patch)
tree541b0130abde25c96918f4c0176514c3b29baa9f /sysklogd
parentceab8700dfa0a4f987c9872e12e57cfba6ddb95c (diff)
downloadbusybox-w32-a9b60e93eeb4d2706ebc95bafb18bd4267a03d6f.tar.gz
busybox-w32-a9b60e93eeb4d2706ebc95bafb18bd4267a03d6f.tar.bz2
busybox-w32-a9b60e93eeb4d2706ebc95bafb18bd4267a03d6f.zip
new libbb func: xmalloc_realpath (+ use it where makes sense)
syslogd, logread: add debugging code (disabled) syslogs: drastically smaller bss; fix "-C n" behaviour
Diffstat (limited to 'sysklogd')
-rw-r--r--sysklogd/logread.c140
-rw-r--r--sysklogd/syslogd.c32
2 files changed, 86 insertions, 86 deletions
diff --git a/sysklogd/logread.c b/sysklogd/logread.c
index 745f97629..4427cf37e 100644
--- a/sysklogd/logread.c
+++ b/sysklogd/logread.c
@@ -9,46 +9,48 @@
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */ 10 */
11 11
12
13#include "busybox.h" 12#include "busybox.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/ipc.h> 13#include <sys/ipc.h>
18#include <sys/types.h> 14//#include <sys/types.h>
19#include <sys/sem.h> 15#include <sys/sem.h>
20#include <sys/shm.h> 16#include <sys/shm.h>
21#include <signal.h> 17//#include <signal.h>
22#include <setjmp.h> 18//#include <setjmp.h>
23#include <unistd.h> 19
20#define DEBUG 0
24 21
25static const long KEY_ID = 0x414e4547; /*"GENA"*/ 22static const long KEY_ID = 0x414e4547; /* "GENA" */
26 23
27static struct shbuf_ds { 24static struct shbuf_ds {
28 int size; // size of data written 25 int32_t size; // size of data written
29 int head; // start of message list 26 int32_t head; // start of message list
30 int tail; // end of message list 27 int32_t tail; // end of message list
31 char data[1]; // data/messages 28 char data[1]; // data/messages
32} *buf = NULL; // shared memory pointer 29} *buf; // shared memory pointer
33 30
34 31
35// Semaphore operation structures 32// Semaphore operation structures
36static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup 33static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
37static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn 34static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
38 35
39static int log_shmid = -1; // ipc shared memory id 36static int log_shmid = -1; // ipc shared memory id
40static int log_semid = -1; // ipc semaphore id 37static int log_semid = -1; // ipc semaphore id
41static jmp_buf jmp_env;
42 38
43static void error_exit(const char *str); 39static void error_exit(const char *str) ATTRIBUTE_NORETURN;
44static void interrupted(int sig); 40static void error_exit(const char *str)
41{
42 //release all acquired resources
43 if (log_shmid != -1)
44 shmdt(buf);
45 bb_perror_msg_and_die(str);
46}
45 47
46/* 48/*
47 * sem_up - up()'s a semaphore. 49 * sem_up - up()'s a semaphore.
48 */ 50 */
49static void sem_up(int semid) 51static void sem_up(int semid)
50{ 52{
51 if ( semop(semid, SMrup, 1) == -1 ) 53 if (semop(semid, SMrup, 1) == -1)
52 error_exit("semop[SMrup]"); 54 error_exit("semop[SMrup]");
53} 55}
54 56
@@ -57,52 +59,59 @@ static void sem_up(int semid)
57 */ 59 */
58static void sem_down(int semid) 60static void sem_down(int semid)
59{ 61{
60 if ( semop(semid, SMrdn, 2) == -1 ) 62 if (semop(semid, SMrdn, 2) == -1)
61 error_exit("semop[SMrdn]"); 63 error_exit("semop[SMrdn]");
62} 64}
63 65
66static void interrupted(int sig)
67{
68 signal(SIGINT, SIG_IGN);
69 shmdt(buf);
70 exit(0);
71}
72
64int logread_main(int argc, char **argv) 73int logread_main(int argc, char **argv)
65{ 74{
66 int i; 75 int cur;
67 int follow=0; 76 int follow = 1;
68 77
69 if (argc == 2 && argv[1][0]=='-' && argv[1][1]=='f') { 78 if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) {
70 follow = 1; 79 follow = 0;
71 } else {
72 /* no options, no getopt */ 80 /* no options, no getopt */
73 if (argc > 1) 81 if (argc > 1)
74 bb_show_usage(); 82 bb_show_usage();
75 } 83 }
76 84
77 // handle interrupt signal 85 log_shmid = shmget(KEY_ID, 0, 0);
78 if (setjmp(jmp_env)) goto output_end; 86 if (log_shmid == -1)
79 87 error_exit("can't find circular buffer");
80 // attempt to redefine ^C signal
81 signal(SIGINT, interrupted);
82
83 if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
84 error_exit("Can't find circular buffer");
85 88
86 // Attach shared memory to our char* 89 // Attach shared memory to our char*
87 if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) 90 buf = shmat(log_shmid, NULL, SHM_RDONLY);
88 error_exit("Can't get access to circular buffer from syslogd"); 91 if (buf == NULL)
92 error_exit("can't get access to syslogd buffer");
93
94 log_semid = semget(KEY_ID, 0, 0);
95 if (log_semid == -1)
96 error_exit("can't get access to semaphores for syslogd buffer");
89 97
90 if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) 98 // attempt to redefine ^C signal
91 error_exit("Can't get access to semaphore(s) for circular buffer from syslogd"); 99 signal(SIGINT, interrupted);
92 100
93 // Suppose atomic memory move 101 // Suppose atomic memory move
94 i = follow ? buf->tail : buf->head; 102 cur = follow ? buf->tail : buf->head;
95 103
96 do { 104 do {
97#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING 105#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
98 char *buf_data; 106 char *buf_data;
99 int log_len,j; 107 int log_len, j;
100#endif 108#endif
101
102 sem_down(log_semid); 109 sem_down(log_semid);
103 110
104 //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); 111 if (DEBUG)
105 if (buf->head == buf->tail || i==buf->tail) { 112 printf("head:%i cur:%d tail:%i size:%i\n", buf->head, cur, buf->tail, buf->size);
113
114 if (buf->head == buf->tail || cur == buf->tail) {
106 if (follow) { 115 if (follow) {
107 sem_up(log_semid); 116 sem_up(log_semid);
108 sleep(1); /* TODO: replace me with a sleep_on */ 117 sleep(1); /* TODO: replace me with a sleep_on */
@@ -114,58 +123,39 @@ int logread_main(int argc, char **argv)
114 123
115 // Read Memory 124 // Read Memory
116#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING 125#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
117 log_len = buf->tail - i; 126 log_len = buf->tail - cur;
118 if (log_len < 0) 127 if (log_len < 0)
119 log_len += buf->size; 128 log_len += buf->size;
120 buf_data = xmalloc(log_len); 129 buf_data = xmalloc(log_len);
121 130
122 if (buf->tail < i) { 131 if (buf->tail >= cur) {
123 memcpy(buf_data, buf->data+i, buf->size-i); 132 memcpy(buf_data, buf->data + cur, log_len);
124 memcpy(buf_data+buf->size-i, buf->data, buf->tail);
125 } else { 133 } else {
126 memcpy(buf_data, buf->data+i, buf->tail-i); 134 memcpy(buf_data, buf->data + cur, buf->size - cur);
135 memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
127 } 136 }
128 i = buf->tail; 137 cur = buf->tail;
129
130#else 138#else
131 while ( i != buf->tail) { 139 while (cur != buf->tail) {
132 printf("%s", buf->data+i); 140 fputs(buf->data + cur, stdout);
133 i+= strlen(buf->data+i) + 1; 141 cur += strlen(buf->data + cur) + 1;
134 if (i >= buf->size ) 142 if (cur >= buf->size)
135 i=0; 143 cur = 0;
136 } 144 }
137#endif 145#endif
138 // release the lock on the log chain 146 // release the lock on the log chain
139 sem_up(log_semid); 147 sem_up(log_semid);
140 148
141#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING 149#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
142 for (j=0; j < log_len; j+=strlen(buf_data+j)+1) { 150 for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
143 printf("%s", buf_data+j); 151 fputs(buf_data + j, stdout);
144 if (follow)
145 fflush(stdout);
146 } 152 }
147 free(buf_data); 153 free(buf_data);
148#endif 154#endif
149 fflush(stdout); 155 fflush(stdout);
150 } while (follow); 156 } while (follow);
151 157
152output_end: 158 shmdt(buf);
153 if (log_shmid != -1)
154 shmdt(buf);
155 159
156 return EXIT_SUCCESS; 160 return EXIT_SUCCESS;
157} 161}
158
159static void interrupted(int sig){
160 signal(SIGINT, SIG_IGN);
161 longjmp(jmp_env, 1);
162}
163
164static void error_exit(const char *str){
165 bb_perror_msg(str);
166 //release all acquired resources
167 if (log_shmid != -1)
168 shmdt(buf);
169
170 exit(1);
171}
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 9e4bc63c2..f4644f218 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -22,19 +22,14 @@
22#include <sys/syslog.h> 22#include <sys/syslog.h>
23#include <sys/uio.h> 23#include <sys/uio.h>
24 24
25#define DEBUG 0
26
25/* Path to the unix socket */ 27/* Path to the unix socket */
26static char dev_log_name[MAXPATHLEN]; 28static char *dev_log_name;
27 29
28/* Path for the file where all log messages are written */ 30/* Path for the file where all log messages are written */
29static const char *logFilePath = "/var/log/messages"; 31static const char *logFilePath = "/var/log/messages";
30 32
31#if ENABLE_FEATURE_ROTATE_LOGFILE
32/* max size of message file before being rotated */
33static int logFileSize = 200 * 1024;
34/* number of rotated message files */
35static int logFileRotate = 1;
36#endif
37
38/* interval between marks in seconds */ 33/* interval between marks in seconds */
39static int markInterval = 20 * 60; 34static int markInterval = 20 * 60;
40 35
@@ -44,6 +39,13 @@ static int logLevel = 8;
44/* localhost's name */ 39/* localhost's name */
45static char localHostName[64]; 40static char localHostName[64];
46 41
42#if ENABLE_FEATURE_ROTATE_LOGFILE
43/* max size of message file before being rotated */
44static int logFileSize = 200 * 1024;
45/* number of rotated message files */
46static int logFileRotate = 1;
47#endif
48
47#if ENABLE_FEATURE_REMOTE_LOG 49#if ENABLE_FEATURE_REMOTE_LOG
48#include <netinet/in.h> 50#include <netinet/in.h>
49/* udp socket for logging to remote host */ 51/* udp socket for logging to remote host */
@@ -142,6 +144,9 @@ static void ipcsyslog_cleanup(void)
142 144
143static void ipcsyslog_init(void) 145static void ipcsyslog_init(void)
144{ 146{
147 if (DEBUG)
148 printf("shmget(%lx, %d,...)\n", KEY_ID, shm_size);
149
145 shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023); 150 shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023);
146 if (shmid == -1) { 151 if (shmid == -1) {
147 bb_perror_msg_and_die("shmget"); 152 bb_perror_msg_and_die("shmget");
@@ -236,6 +241,8 @@ static void log_to_shmem(const char *msg, int len)
236 if (semop(s_semid, SMwup, 1) == -1) { 241 if (semop(s_semid, SMwup, 1) == -1) {
237 bb_perror_msg_and_die("SMwup"); 242 bb_perror_msg_and_die("SMwup");
238 } 243 }
244 if (DEBUG)
245 printf("head:%d tail:%d\n", shbuf->head, shbuf->tail);
239} 246}
240#else 247#else
241void ipcsyslog_cleanup(void); 248void ipcsyslog_cleanup(void);
@@ -450,10 +457,12 @@ static void do_syslogd(void)
450 signal(SIGALRM, do_mark); 457 signal(SIGALRM, do_mark);
451 alarm(markInterval); 458 alarm(markInterval);
452 459
460 dev_log_name = xmalloc_realpath(_PATH_LOG);
461 if (!dev_log_name)
462 dev_log_name = _PATH_LOG;
463
453 /* Unlink old /dev/log (or object it points to) */ 464 /* Unlink old /dev/log (or object it points to) */
454 if (realpath(_PATH_LOG, dev_log_name) != NULL) { 465 unlink(dev_log_name);
455 unlink(dev_log_name);
456 }
457 466
458 memset(&sunx, 0, sizeof(sunx)); 467 memset(&sunx, 0, sizeof(sunx));
459 sunx.sun_family = AF_UNIX; 468 sunx.sun_family = AF_UNIX;
@@ -520,6 +529,7 @@ int syslogd_main(int argc, char **argv)
520 char *p; 529 char *p;
521 530
522 /* do normal option parsing */ 531 /* do normal option parsing */
532 opt_complementary = "=0"; /* no non-option params */
523 getopt32(argc, argv, OPTION_STR, OPTION_PARAM); 533 getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
524 if (option_mask32 & OPT_mark) // -m 534 if (option_mask32 & OPT_mark) // -m
525 markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; 535 markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;