diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-26 01:03:16 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-26 01:03:16 +0000 |
commit | f7dd10f67ce892530b2a2093b149722cf4903e76 (patch) | |
tree | c8376bd83d6a883e3a8d445087a358463a3f91a3 /sysklogd/logread.c | |
parent | a79220db6e71b7fa509ffa88a02ff1ea785bf811 (diff) | |
download | busybox-w32-f7dd10f67ce892530b2a2093b149722cf4903e76.tar.gz busybox-w32-f7dd10f67ce892530b2a2093b149722cf4903e76.tar.bz2 busybox-w32-f7dd10f67ce892530b2a2093b149722cf4903e76.zip |
Patch from Fillod Stephane
Add follow mode to logread, ala "tail -f /var/log/messages"
Note: output to a slow serial terminal can have side effects
on syslog because of the semaphore. In such case, define
RC_LOGREAD.
Diffstat (limited to 'sysklogd/logread.c')
-rw-r--r-- | sysklogd/logread.c | 86 |
1 files changed, 68 insertions, 18 deletions
diff --git a/sysklogd/logread.c b/sysklogd/logread.c index 2692efbee..524178fe8 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c | |||
@@ -33,6 +33,7 @@ | |||
33 | #include <sys/shm.h> | 33 | #include <sys/shm.h> |
34 | #include <signal.h> | 34 | #include <signal.h> |
35 | #include <setjmp.h> | 35 | #include <setjmp.h> |
36 | #include <unistd.h> | ||
36 | #include "busybox.h" | 37 | #include "busybox.h" |
37 | 38 | ||
38 | static const long KEY_ID = 0x414e4547; /*"GENA"*/ | 39 | static const long KEY_ID = 0x414e4547; /*"GENA"*/ |
@@ -77,10 +78,15 @@ static inline void sem_down(int semid) | |||
77 | extern int logread_main(int argc, char **argv) | 78 | extern int logread_main(int argc, char **argv) |
78 | { | 79 | { |
79 | int i; | 80 | int i; |
81 | int follow=0; | ||
80 | 82 | ||
81 | /* no options, no getopt */ | 83 | if (argc == 2 && strcmp(argv[1],"-f")==0) { |
82 | if (argc > 1) | 84 | follow = 1; |
83 | bb_show_usage(); | 85 | } else { |
86 | /* no options, no getopt */ | ||
87 | if (argc > 1) | ||
88 | bb_show_usage(); | ||
89 | } | ||
84 | 90 | ||
85 | // handle intrrupt signal | 91 | // handle intrrupt signal |
86 | if (setjmp(jmp_env)) goto output_end; | 92 | if (setjmp(jmp_env)) goto output_end; |
@@ -98,22 +104,66 @@ extern int logread_main(int argc, char **argv) | |||
98 | if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) | 104 | if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) |
99 | error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); | 105 | error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); |
100 | 106 | ||
101 | sem_down(log_semid); | 107 | // Suppose atomic memory move |
102 | // Read Memory | 108 | i = follow ? buf->tail : buf->head; |
103 | i=buf->head; | 109 | |
104 | 110 | do { | |
105 | //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); | 111 | #undef RC_LOGREAD |
106 | if (buf->head == buf->tail) { | 112 | #ifdef RC_LOGREAD |
107 | printf("<empty syslog>\n"); | 113 | char *buf_data; |
108 | } | 114 | int log_len,j; |
115 | #endif | ||
116 | |||
117 | sem_down(log_semid); | ||
118 | |||
119 | //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); | ||
120 | if (buf->head == buf->tail || i==buf->tail) { | ||
121 | if (follow) { | ||
122 | sem_up(log_semid); | ||
123 | sleep(1); /* TODO: replace me with a sleep_on */ | ||
124 | continue; | ||
125 | } else { | ||
126 | printf("<empty syslog>\n"); | ||
127 | } | ||
128 | } | ||
109 | 129 | ||
110 | while ( i != buf->tail) { | 130 | // Read Memory |
111 | printf("%s", buf->data+i); | 131 | #ifdef RC_LOGREAD |
112 | i+= strlen(buf->data+i) + 1; | 132 | log_len = buf->tail - i; |
113 | if (i >= buf->size ) | 133 | if (log_len < 0) |
114 | i=0; | 134 | log_len += buf->size; |
115 | } | 135 | buf_data = (char *)malloc(log_len); |
116 | sem_up(log_semid); | 136 | if (!buf_data) |
137 | error_exit("malloc failed"); | ||
138 | |||
139 | if (buf->tail < i) { | ||
140 | memcpy(buf_data, buf->data+i, buf->size-i); | ||
141 | memcpy(buf_data+buf->size-i, buf->data, buf->tail); | ||
142 | } else { | ||
143 | memcpy(buf_data, buf->data+i, buf->tail-i); | ||
144 | } | ||
145 | i = buf->tail; | ||
146 | |||
147 | #else | ||
148 | while ( i != buf->tail) { | ||
149 | printf("%s", buf->data+i); | ||
150 | i+= strlen(buf->data+i) + 1; | ||
151 | if (i >= buf->size ) | ||
152 | i=0; | ||
153 | } | ||
154 | #endif | ||
155 | // release the lock on the log chain | ||
156 | sem_up(log_semid); | ||
157 | |||
158 | #ifdef RC_LOGREAD | ||
159 | for (j=0; j < log_len; j+=strlen(buf_data+j)+1) { | ||
160 | printf("%s", buf_data+j); | ||
161 | if (follow) | ||
162 | fflush(stdout); | ||
163 | } | ||
164 | free(buf_data); | ||
165 | #endif | ||
166 | } while (follow); | ||
117 | 167 | ||
118 | output_end: | 168 | output_end: |
119 | if (log_shmid != -1) | 169 | if (log_shmid != -1) |