blob: 5b999730aeffbb829ed839f64cc23143b226f88a [file] [log] [blame]
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * circular buffer syslog implementation for busybox
4 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00005 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00006 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00007 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000010 */
Denys Vlasenkod34f3002015-10-18 18:42:03 +020011//config:config LOGREAD
12//config: bool "logread"
13//config: default y
14//config: depends on FEATURE_IPC_SYSLOG
15//config: help
16//config: If you enabled Circular Buffer support, you almost
17//config: certainly want to enable this feature as well. This
18//config: utility will allow you to read the messages that are
19//config: stored in the syslogd circular buffer.
20//config:
21//config:config FEATURE_LOGREAD_REDUCED_LOCKING
22//config: bool "Double buffering"
23//config: default y
24//config: depends on LOGREAD
25//config: help
26//config: 'logread' ouput to slow serial terminals can have
27//config: side effects on syslog because of the semaphore.
28//config: This option make logread to double buffer copy
29//config: from circular buffer, minimizing semaphore
30//config: contention at some minor memory expense.
31//config:
32
33//applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
34
35//kbuild:lib-$(CONFIG_LOGREAD) += logread.o
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000036
Pere Orga5bc8c002011-04-11 03:29:49 +020037//usage:#define logread_trivial_usage
Phil Sutter92edab12015-03-22 17:36:20 +010038//usage: "[-fF]"
Pere Orga5bc8c002011-04-11 03:29:49 +020039//usage:#define logread_full_usage "\n\n"
40//usage: "Show messages in syslogd's circular buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020041//usage: "\n -f Output data as log grows"
Phil Sutter92edab12015-03-22 17:36:20 +010042//usage: "\n -F Same as -f, but dump buffer first"
Pere Orga5bc8c002011-04-11 03:29:49 +020043
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000044#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020045#include "common_bufsiz.h"
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000046#include <sys/ipc.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000047#include <sys/sem.h>
48#include <sys/shm.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000049
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000050#define DEBUG 0
51
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000052/* our shared key (syslogd.c and logread.c must be in sync) */
Denis Vlasenko085231f2007-01-10 22:35:54 +000053enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000054
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000055struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000056 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000057 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000058 char data[1]; // messages
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000059};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000060
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000061static const struct sembuf init_sem[3] = {
62 {0, -1, IPC_NOWAIT | SEM_UNDO},
63 {1, 0}, {0, +1, SEM_UNDO}
64};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000065
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000066struct globals {
67 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
68 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
69 struct shbuf_ds *shbuf;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010070} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020071#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000072#define SMrup (G.SMrup)
73#define SMrdn (G.SMrdn)
74#define shbuf (G.shbuf)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000075#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020076 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +000077 memcpy(SMrup, init_sem, sizeof(init_sem)); \
78} while (0)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000079
Denys Vlasenko26ad2562013-11-26 12:02:18 +010080#if 0
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000081static void error_exit(const char *str) NORETURN;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000082static void error_exit(const char *str)
83{
Denys Vlasenko26414262013-07-28 23:17:00 +020084 /* Release all acquired resources */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000085 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000086 bb_perror_msg_and_die(str);
87}
Denys Vlasenko26ad2562013-11-26 12:02:18 +010088#else
89/* On Linux, shmdt is not mandatory on exit */
90# define error_exit(str) bb_perror_msg_and_die(str)
91#endif
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000092
93/*
94 * sem_up - up()'s a semaphore.
95 */
Rob Landley88621d72006-08-29 19:41:06 +000096static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000097{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000098 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000099 error_exit("semop[SMrup]");
100}
101
Denys Vlasenko26414262013-07-28 23:17:00 +0200102static void interrupted(int sig)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000103{
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100104 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Denys Vlasenko26414262013-07-28 23:17:00 +0200105 kill_myself_with_sig(sig);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000106}
107
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000108int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000109int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000110{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000111 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000112 int log_semid; /* ipc semaphore id */
113 int log_shmid; /* ipc shared memory id */
Phil Sutter92edab12015-03-22 17:36:20 +0100114 int follow = getopt32(argv, "fF");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000115
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +0000116 INIT_G();
117
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000118 log_shmid = shmget(KEY_ID, 0, 0);
119 if (log_shmid == -1)
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100120 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000121
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000122 /* Attach shared memory to our char* */
123 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
124 if (shbuf == NULL)
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100125 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000126
127 log_semid = semget(KEY_ID, 0, 0);
128 if (log_semid == -1)
129 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000130
Denys Vlasenko26414262013-07-28 23:17:00 +0200131 bb_signals(BB_FATAL_SIGS, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000132
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000133 /* Suppose atomic memory read */
134 /* Max possible value for tail is shbuf->size - 1 */
135 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000136
Phil Sutter92edab12015-03-22 17:36:20 +0100137 /* Loop for -f or -F, one pass otherwise */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000138 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000139 unsigned shbuf_size;
140 unsigned shbuf_tail;
141 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000142#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000143 int i;
144 int len_first_part;
145 int len_total = len_total; /* for gcc */
146 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000147#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000148 if (semop(log_semid, SMrdn, 2) == -1)
149 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000150
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000151 /* Copy the info, helps gcc to realize that it doesn't change */
152 shbuf_size = shbuf->size;
153 shbuf_tail = shbuf->tail;
154 shbuf_data = shbuf->data; /* pointer! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000155
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000156 if (DEBUG)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100157 printf("cur:%u tail:%u size:%u\n",
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000158 cur, shbuf_tail, shbuf_size);
159
Phil Sutter92edab12015-03-22 17:36:20 +0100160 if (!(follow & 1)) { /* not -f */
161 /* if -F, "convert" it to -f, so that we dont
162 * dump the entire buffer on each iteration
163 */
164 follow >>= 1;
165
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000166 /* advance to oldest complete message */
167 /* find NUL */
168 cur += strlen(shbuf_data + cur);
169 if (cur >= shbuf_size) { /* last byte in buffer? */
170 cur = strnlen(shbuf_data, shbuf_tail);
171 if (cur == shbuf_tail)
172 goto unlock; /* no complete messages */
173 }
174 /* advance to first byte of the message */
175 cur++;
176 if (cur >= shbuf_size) /* last byte in buffer? */
177 cur = 0;
Phil Sutter92edab12015-03-22 17:36:20 +0100178 } else { /* -f */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000179 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000180 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100181 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000182 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000183 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000184 }
185 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000186
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000187 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000188#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000189 len_first_part = len_total = shbuf_tail - cur;
190 if (len_total < 0) {
191 /* message wraps: */
192 /* [SECOND PART.........FIRST PART] */
193 /* ^data ^tail ^cur ^size */
194 len_total += shbuf_size;
195 }
196 copy = xmalloc(len_total + 1);
197 if (len_first_part < 0) {
198 /* message wraps (see above) */
199 len_first_part = shbuf_size - cur;
200 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
201 }
202 memcpy(copy, shbuf_data + cur, len_first_part);
203 copy[len_total] = '\0';
204 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000205#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000206 while (cur != shbuf_tail) {
207 fputs(shbuf_data + cur, stdout);
208 cur += strlen(shbuf_data + cur) + 1;
209 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000210 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000211 }
212#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000213 unlock:
214 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000215 sem_up(log_semid);
216
Denis Vlasenko085231f2007-01-10 22:35:54 +0000217#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000218 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
219 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000220 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000221 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000222#endif
Denys Vlasenko71f6c1a2013-11-26 11:44:27 +0100223 fflush_all();
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000224 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000225
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100226 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000227
Denis Vlasenko085231f2007-01-10 22:35:54 +0000228 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000229}