aboutsummaryrefslogtreecommitdiff
path: root/sysklogd/logread.c
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/logread.c
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/logread.c')
-rw-r--r--sysklogd/logread.c140
1 files changed, 65 insertions, 75 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}