summaryrefslogtreecommitdiff
path: root/sysklogd/logread.c (plain)
blob: 1f0c6252d9eab2509804d08b8eed0547e231cb68
1/* vi: set sw=4 ts=4: */
2/*
3 * circular buffer syslog implementation for busybox
4 *
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
6 *
7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 */
11//config:config LOGREAD
12//config: bool "logread"
13//config: default y
14//WRONG: it should be compilable without SYSLOG=y:
15//WRONG: depends on FEATURE_IPC_SYSLOG
16//config: help
17//config: If you enabled Circular Buffer support, you almost
18//config: certainly want to enable this feature as well. This
19//config: utility will allow you to read the messages that are
20//config: stored in the syslogd circular buffer.
21//config:
22//config:config FEATURE_LOGREAD_REDUCED_LOCKING
23//config: bool "Double buffering"
24//config: default y
25//config: depends on LOGREAD
26//config: help
27//config: 'logread' ouput to slow serial terminals can have
28//config: side effects on syslog because of the semaphore.
29//config: This option make logread to double buffer copy
30//config: from circular buffer, minimizing semaphore
31//config: contention at some minor memory expense.
32//config:
33
34//applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
35
36//kbuild:lib-$(CONFIG_LOGREAD) += logread.o
37
38//usage:#define logread_trivial_usage
39//usage: "[-fF]"
40//usage:#define logread_full_usage "\n\n"
41//usage: "Show messages in syslogd's circular buffer\n"
42//usage: "\n -f Output data as log grows"
43//usage: "\n -F Same as -f, but dump buffer first"
44
45#include "libbb.h"
46#include "common_bufsiz.h"
47#include <sys/ipc.h>
48#include <sys/sem.h>
49#include <sys/shm.h>
50
51#define DEBUG 0
52
53/* our shared key (syslogd.c and logread.c must be in sync) */
54enum { KEY_ID = 0x414e4547 }; /* "GENA" */
55
56struct shbuf_ds {
57 int32_t size; // size of data - 1
58 int32_t tail; // end of message list
59 char data[1]; // messages
60};
61
62static const struct sembuf init_sem[3] = {
63 {0, -1, IPC_NOWAIT | SEM_UNDO},
64 {1, 0}, {0, +1, SEM_UNDO}
65};
66
67struct globals {
68 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
69 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
70 struct shbuf_ds *shbuf;
71} FIX_ALIASING;
72#define G (*(struct globals*)bb_common_bufsiz1)
73#define SMrup (G.SMrup)
74#define SMrdn (G.SMrdn)
75#define shbuf (G.shbuf)
76#define INIT_G() do { \
77 setup_common_bufsiz(); \
78 memcpy(SMrup, init_sem, sizeof(init_sem)); \
79} while (0)
80
81#if 0
82static void error_exit(const char *str) NORETURN;
83static void error_exit(const char *str)
84{
85 /* Release all acquired resources */
86 shmdt(shbuf);
87 bb_perror_msg_and_die(str);
88}
89#else
90/* On Linux, shmdt is not mandatory on exit */
91# define error_exit(str) bb_perror_msg_and_die(str)
92#endif
93
94/*
95 * sem_up - up()'s a semaphore.
96 */
97static void sem_up(int semid)
98{
99 if (semop(semid, SMrup, 1) == -1)
100 error_exit("semop[SMrup]");
101}
102
103static void interrupted(int sig)
104{
105 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
106 kill_myself_with_sig(sig);
107}
108
109int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110int logread_main(int argc UNUSED_PARAM, char **argv)
111{
112 unsigned cur;
113 int log_semid; /* ipc semaphore id */
114 int log_shmid; /* ipc shared memory id */
115 int follow = getopt32(argv, "fF");
116
117 INIT_G();
118
119 log_shmid = shmget(KEY_ID, 0, 0);
120 if (log_shmid == -1)
121 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
122
123 /* Attach shared memory to our char* */
124 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
125 if (shbuf == NULL)
126 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
127
128 log_semid = semget(KEY_ID, 0, 0);
129 if (log_semid == -1)
130 error_exit("can't get access to semaphores for syslogd buffer");
131
132 bb_signals(BB_FATAL_SIGS, interrupted);
133
134 /* Suppose atomic memory read */
135 /* Max possible value for tail is shbuf->size - 1 */
136 cur = shbuf->tail;
137
138 /* Loop for -f or -F, one pass otherwise */
139 do {
140 unsigned shbuf_size;
141 unsigned shbuf_tail;
142 const char *shbuf_data;
143#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
144 int i;
145 int len_first_part;
146 int len_total = len_total; /* for gcc */
147 char *copy = copy; /* for gcc */
148#endif
149 if (semop(log_semid, SMrdn, 2) == -1)
150 error_exit("semop[SMrdn]");
151
152 /* Copy the info, helps gcc to realize that it doesn't change */
153 shbuf_size = shbuf->size;
154 shbuf_tail = shbuf->tail;
155 shbuf_data = shbuf->data; /* pointer! */
156
157 if (DEBUG)
158 printf("cur:%u tail:%u size:%u\n",
159 cur, shbuf_tail, shbuf_size);
160
161 if (!(follow & 1)) { /* not -f */
162 /* if -F, "convert" it to -f, so that we dont
163 * dump the entire buffer on each iteration
164 */
165 follow >>= 1;
166
167 /* advance to oldest complete message */
168 /* find NUL */
169 cur += strlen(shbuf_data + cur);
170 if (cur >= shbuf_size) { /* last byte in buffer? */
171 cur = strnlen(shbuf_data, shbuf_tail);
172 if (cur == shbuf_tail)
173 goto unlock; /* no complete messages */
174 }
175 /* advance to first byte of the message */
176 cur++;
177 if (cur >= shbuf_size) /* last byte in buffer? */
178 cur = 0;
179 } else { /* -f */
180 if (cur == shbuf_tail) {
181 sem_up(log_semid);
182 fflush_all();
183 sleep(1); /* TODO: replace me with a sleep_on */
184 continue;
185 }
186 }
187
188 /* Read from cur to tail */
189#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
190 len_first_part = len_total = shbuf_tail - cur;
191 if (len_total < 0) {
192 /* message wraps: */
193 /* [SECOND PART.........FIRST PART] */
194 /* ^data ^tail ^cur ^size */
195 len_total += shbuf_size;
196 }
197 copy = xmalloc(len_total + 1);
198 if (len_first_part < 0) {
199 /* message wraps (see above) */
200 len_first_part = shbuf_size - cur;
201 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
202 }
203 memcpy(copy, shbuf_data + cur, len_first_part);
204 copy[len_total] = '\0';
205 cur = shbuf_tail;
206#else
207 while (cur != shbuf_tail) {
208 fputs(shbuf_data + cur, stdout);
209 cur += strlen(shbuf_data + cur) + 1;
210 if (cur >= shbuf_size)
211 cur = 0;
212 }
213#endif
214 unlock:
215 /* release the lock on the log chain */
216 sem_up(log_semid);
217
218#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
219 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
220 fputs(copy + i, stdout);
221 }
222 free(copy);
223#endif
224 fflush_all();
225 } while (follow);
226
227 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
228
229 fflush_stdout_and_exit(EXIT_SUCCESS);
230}
231