1 /* vi: set sw=4 ts=4: */
3 * circular buffer syslog implementation for busybox
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 //usage:#define logread_trivial_usage
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 //usage: "\n -F Same as -f, but dump buffer first"
26 /* our shared key (syslogd.c and logread.c must be in sync) */
27 enum { KEY_ID = 0x414e4547 }; /* "GENA" */
30 int32_t size; // size of data - 1
31 int32_t tail; // end of message list
32 char data[1]; // messages
35 static const struct sembuf init_sem[3] = {
36 {0, -1, IPC_NOWAIT | SEM_UNDO},
37 {1, 0}, {0, +1, SEM_UNDO}
41 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
42 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
43 struct shbuf_ds *shbuf;
45 #define G (*(struct globals*)&bb_common_bufsiz1)
46 #define SMrup (G.SMrup)
47 #define SMrdn (G.SMrdn)
48 #define shbuf (G.shbuf)
49 #define INIT_G() do { \
50 memcpy(SMrup, init_sem, sizeof(init_sem)); \
54 static void error_exit(const char *str) NORETURN;
55 static void error_exit(const char *str)
57 /* Release all acquired resources */
59 bb_perror_msg_and_die(str);
62 /* On Linux, shmdt is not mandatory on exit */
63 # define error_exit(str) bb_perror_msg_and_die(str)
67 * sem_up - up()'s a semaphore.
69 static void sem_up(int semid)
71 if (semop(semid, SMrup, 1) == -1)
72 error_exit("semop[SMrup]");
75 static void interrupted(int sig)
77 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
78 kill_myself_with_sig(sig);
81 int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
82 int logread_main(int argc UNUSED_PARAM, char **argv)
85 int log_semid; /* ipc semaphore id */
86 int log_shmid; /* ipc shared memory id */
87 int follow = getopt32(argv, "fF");
91 log_shmid = shmget(KEY_ID, 0, 0);
93 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
95 /* Attach shared memory to our char* */
96 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
98 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
100 log_semid = semget(KEY_ID, 0, 0);
102 error_exit("can't get access to semaphores for syslogd buffer");
104 bb_signals(BB_FATAL_SIGS, interrupted);
106 /* Suppose atomic memory read */
107 /* Max possible value for tail is shbuf->size - 1 */
110 /* Loop for -f or -F, one pass otherwise */
114 const char *shbuf_data;
115 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
118 int len_total = len_total; /* for gcc */
119 char *copy = copy; /* for gcc */
121 if (semop(log_semid, SMrdn, 2) == -1)
122 error_exit("semop[SMrdn]");
124 /* Copy the info, helps gcc to realize that it doesn't change */
125 shbuf_size = shbuf->size;
126 shbuf_tail = shbuf->tail;
127 shbuf_data = shbuf->data; /* pointer! */
130 printf("cur:%u tail:%u size:%u\n",
131 cur, shbuf_tail, shbuf_size);
133 if (!(follow & 1)) { /* not -f */
134 /* if -F, "convert" it to -f, so that we dont
135 * dump the entire buffer on each iteration
139 /* advance to oldest complete message */
141 cur += strlen(shbuf_data + cur);
142 if (cur >= shbuf_size) { /* last byte in buffer? */
143 cur = strnlen(shbuf_data, shbuf_tail);
144 if (cur == shbuf_tail)
145 goto unlock; /* no complete messages */
147 /* advance to first byte of the message */
149 if (cur >= shbuf_size) /* last byte in buffer? */
152 if (cur == shbuf_tail) {
155 sleep(1); /* TODO: replace me with a sleep_on */
160 /* Read from cur to tail */
161 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
162 len_first_part = len_total = shbuf_tail - cur;
165 /* [SECOND PART.........FIRST PART] */
166 /* ^data ^tail ^cur ^size */
167 len_total += shbuf_size;
169 copy = xmalloc(len_total + 1);
170 if (len_first_part < 0) {
171 /* message wraps (see above) */
172 len_first_part = shbuf_size - cur;
173 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
175 memcpy(copy, shbuf_data + cur, len_first_part);
176 copy[len_total] = '\0';
179 while (cur != shbuf_tail) {
180 fputs(shbuf_data + cur, stdout);
181 cur += strlen(shbuf_data + cur) + 1;
182 if (cur >= shbuf_size)
187 /* release the lock on the log chain */
190 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
191 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
192 fputs(copy + i, stdout);
199 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
201 fflush_stdout_and_exit(EXIT_SUCCESS);