summaryrefslogtreecommitdiff
path: root/sysklogd/logread.c (plain)
blob: bea73d9323955992e45ad62ca921b0fe46b692e6
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
12//usage:#define logread_trivial_usage
13//usage: "[-f]"
14//usage:#define logread_full_usage "\n\n"
15//usage: "Show messages in syslogd's circular buffer\n"
16//usage: "\n -f Output data as log grows"
17
18#include "libbb.h"
19#include <sys/ipc.h>
20#include <sys/sem.h>
21#include <sys/shm.h>
22
23#define DEBUG 0
24
25/* our shared key (syslogd.c and logread.c must be in sync) */
26enum { KEY_ID = 0x414e4547 }; /* "GENA" */
27
28struct shbuf_ds {
29 int32_t size; // size of data - 1
30 int32_t tail; // end of message list
31 char data[1]; // messages
32};
33
34static const struct sembuf init_sem[3] = {
35 {0, -1, IPC_NOWAIT | SEM_UNDO},
36 {1, 0}, {0, +1, SEM_UNDO}
37};
38
39struct globals {
40 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
41 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
42 struct shbuf_ds *shbuf;
43} FIX_ALIASING;
44#define G (*(struct globals*)&bb_common_bufsiz1)
45#define SMrup (G.SMrup)
46#define SMrdn (G.SMrdn)
47#define shbuf (G.shbuf)
48#define INIT_G() do { \
49 memcpy(SMrup, init_sem, sizeof(init_sem)); \
50} while (0)
51
52#if 0
53static void error_exit(const char *str) NORETURN;
54static void error_exit(const char *str)
55{
56 /* Release all acquired resources */
57 shmdt(shbuf);
58 bb_perror_msg_and_die(str);
59}
60#else
61/* On Linux, shmdt is not mandatory on exit */
62# define error_exit(str) bb_perror_msg_and_die(str)
63#endif
64
65/*
66 * sem_up - up()'s a semaphore.
67 */
68static void sem_up(int semid)
69{
70 if (semop(semid, SMrup, 1) == -1)
71 error_exit("semop[SMrup]");
72}
73
74static void interrupted(int sig)
75{
76 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
77 kill_myself_with_sig(sig);
78}
79
80int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
81int logread_main(int argc UNUSED_PARAM, char **argv)
82{
83 unsigned cur;
84 int log_semid; /* ipc semaphore id */
85 int log_shmid; /* ipc shared memory id */
86 smallint follow = getopt32(argv, "f");
87
88 INIT_G();
89
90 log_shmid = shmget(KEY_ID, 0, 0);
91 if (log_shmid == -1)
92 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
93
94 /* Attach shared memory to our char* */
95 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
96 if (shbuf == NULL)
97 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
98
99 log_semid = semget(KEY_ID, 0, 0);
100 if (log_semid == -1)
101 error_exit("can't get access to semaphores for syslogd buffer");
102
103 bb_signals(BB_FATAL_SIGS, interrupted);
104
105 /* Suppose atomic memory read */
106 /* Max possible value for tail is shbuf->size - 1 */
107 cur = shbuf->tail;
108
109 /* Loop for logread -f, one pass if there was no -f */
110 do {
111 unsigned shbuf_size;
112 unsigned shbuf_tail;
113 const char *shbuf_data;
114#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
115 int i;
116 int len_first_part;
117 int len_total = len_total; /* for gcc */
118 char *copy = copy; /* for gcc */
119#endif
120 if (semop(log_semid, SMrdn, 2) == -1)
121 error_exit("semop[SMrdn]");
122
123 /* Copy the info, helps gcc to realize that it doesn't change */
124 shbuf_size = shbuf->size;
125 shbuf_tail = shbuf->tail;
126 shbuf_data = shbuf->data; /* pointer! */
127
128 if (DEBUG)
129 printf("cur:%u tail:%u size:%u\n",
130 cur, shbuf_tail, shbuf_size);
131
132 if (!follow) {
133 /* advance to oldest complete message */
134 /* find NUL */
135 cur += strlen(shbuf_data + cur);
136 if (cur >= shbuf_size) { /* last byte in buffer? */
137 cur = strnlen(shbuf_data, shbuf_tail);
138 if (cur == shbuf_tail)
139 goto unlock; /* no complete messages */
140 }
141 /* advance to first byte of the message */
142 cur++;
143 if (cur >= shbuf_size) /* last byte in buffer? */
144 cur = 0;
145 } else { /* logread -f */
146 if (cur == shbuf_tail) {
147 sem_up(log_semid);
148 fflush_all();
149 sleep(1); /* TODO: replace me with a sleep_on */
150 continue;
151 }
152 }
153
154 /* Read from cur to tail */
155#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
156 len_first_part = len_total = shbuf_tail - cur;
157 if (len_total < 0) {
158 /* message wraps: */
159 /* [SECOND PART.........FIRST PART] */
160 /* ^data ^tail ^cur ^size */
161 len_total += shbuf_size;
162 }
163 copy = xmalloc(len_total + 1);
164 if (len_first_part < 0) {
165 /* message wraps (see above) */
166 len_first_part = shbuf_size - cur;
167 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
168 }
169 memcpy(copy, shbuf_data + cur, len_first_part);
170 copy[len_total] = '\0';
171 cur = shbuf_tail;
172#else
173 while (cur != shbuf_tail) {
174 fputs(shbuf_data + cur, stdout);
175 cur += strlen(shbuf_data + cur) + 1;
176 if (cur >= shbuf_size)
177 cur = 0;
178 }
179#endif
180 unlock:
181 /* release the lock on the log chain */
182 sem_up(log_semid);
183
184#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
185 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
186 fputs(copy + i, stdout);
187 }
188 free(copy);
189#endif
190 fflush_all();
191 } while (follow);
192
193 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
194
195 fflush_stdout_and_exit(EXIT_SUCCESS);
196}
197