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"
25 /* our shared key (syslogd.c and logread.c must be in sync) */
26 enum { KEY_ID = 0x414e4547 }; /* "GENA" */
29 int32_t size; // size of data - 1
30 int32_t tail; // end of message list
31 char data[1]; // messages
34 static const struct sembuf init_sem[3] = {
35 {0, -1, IPC_NOWAIT | SEM_UNDO},
36 {1, 0}, {0, +1, SEM_UNDO}
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;
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)); \
53 static void error_exit(const char *str) NORETURN;
54 static void error_exit(const char *str)
56 /* Release all acquired resources */
58 bb_perror_msg_and_die(str);
61 /* On Linux, shmdt is not mandatory on exit */
62 # define error_exit(str) bb_perror_msg_and_die(str)
66 * sem_up - up()'s a semaphore.
68 static void sem_up(int semid)
70 if (semop(semid, SMrup, 1) == -1)
71 error_exit("semop[SMrup]");
74 static void interrupted(int sig)
76 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
77 kill_myself_with_sig(sig);
80 int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
81 int logread_main(int argc UNUSED_PARAM, char **argv)
84 int log_semid; /* ipc semaphore id */
85 int log_shmid; /* ipc shared memory id */
86 smallint follow = getopt32(argv, "f");
90 log_shmid = shmget(KEY_ID, 0, 0);
92 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
94 /* Attach shared memory to our char* */
95 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
97 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
99 log_semid = semget(KEY_ID, 0, 0);
101 error_exit("can't get access to semaphores for syslogd buffer");
103 bb_signals(BB_FATAL_SIGS, interrupted);
105 /* Suppose atomic memory read */
106 /* Max possible value for tail is shbuf->size - 1 */
109 /* Loop for logread -f, one pass if there was no -f */
113 const char *shbuf_data;
114 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
117 int len_total = len_total; /* for gcc */
118 char *copy = copy; /* for gcc */
120 if (semop(log_semid, SMrdn, 2) == -1)
121 error_exit("semop[SMrdn]");
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! */
129 printf("cur:%u tail:%u size:%u\n",
130 cur, shbuf_tail, shbuf_size);
133 /* advance to oldest complete message */
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 */
141 /* advance to first byte of the message */
143 if (cur >= shbuf_size) /* last byte in buffer? */
145 } else { /* logread -f */
146 if (cur == shbuf_tail) {
149 sleep(1); /* TODO: replace me with a sleep_on */
154 /* Read from cur to tail */
155 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
156 len_first_part = len_total = shbuf_tail - cur;
159 /* [SECOND PART.........FIRST PART] */
160 /* ^data ^tail ^cur ^size */
161 len_total += shbuf_size;
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);
169 memcpy(copy, shbuf_data + cur, len_first_part);
170 copy[len_total] = '\0';
173 while (cur != shbuf_tail) {
174 fputs(shbuf_data + cur, stdout);
175 cur += strlen(shbuf_data + cur) + 1;
176 if (cur >= shbuf_size)
181 /* release the lock on the log chain */
184 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
185 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
186 fputs(copy + i, stdout);
193 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
195 fflush_stdout_and_exit(EXIT_SUCCESS);