blob: da552e0876e61d091dea16699502048f96f69eb1 [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
Denys Vlasenkoae178ce2017-07-19 14:32:54 +020012//config: bool "logread (6 kb)"
Denys Vlasenkod34f3002015-10-18 18:42:03 +020013//config: default y
Denys Vlasenko9cc3d3a2016-12-23 02:42:26 +010014//WRONG: it should be compilable without SYSLOG=y:
15//WRONG: depends on FEATURE_IPC_SYSLOG
Denys Vlasenkod34f3002015-10-18 18:42:03 +020016//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//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.
Denys Vlasenkod34f3002015-10-18 18:42:03 +020021//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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: 'logread' output 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.
Denys Vlasenkod34f3002015-10-18 18:42:03 +020032//config:
33
34//applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
35
36//kbuild:lib-$(CONFIG_LOGREAD) += logread.o
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000037
Pere Orga5bc8c002011-04-11 03:29:49 +020038//usage:#define logread_trivial_usage
Phil Sutter92edab12015-03-22 17:36:20 +010039//usage: "[-fF]"
Pere Orga5bc8c002011-04-11 03:29:49 +020040//usage:#define logread_full_usage "\n\n"
41//usage: "Show messages in syslogd's circular buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020042//usage: "\n -f Output data as log grows"
Phil Sutter92edab12015-03-22 17:36:20 +010043//usage: "\n -F Same as -f, but dump buffer first"
Pere Orga5bc8c002011-04-11 03:29:49 +020044
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000045#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020046#include "common_bufsiz.h"
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000047#include <sys/ipc.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000048#include <sys/sem.h>
49#include <sys/shm.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000050
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000051#define DEBUG 0
52
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000053/* our shared key (syslogd.c and logread.c must be in sync) */
Denis Vlasenko085231f2007-01-10 22:35:54 +000054enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000055
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000056struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000057 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000058 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000059 char data[1]; // messages
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000060};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000061
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000062static const struct sembuf init_sem[3] = {
63 {0, -1, IPC_NOWAIT | SEM_UNDO},
64 {1, 0}, {0, +1, SEM_UNDO}
65};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000066
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000067struct 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;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010071} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020072#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000073#define SMrup (G.SMrup)
74#define SMrdn (G.SMrdn)
75#define shbuf (G.shbuf)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000076#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020077 setup_common_bufsiz(); \
Denis Vlasenko7049ff82008-06-25 09:53:17 +000078 memcpy(SMrup, init_sem, sizeof(init_sem)); \
79} while (0)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000080
Denys Vlasenko26ad2562013-11-26 12:02:18 +010081#if 0
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000082static void error_exit(const char *str) NORETURN;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000083static void error_exit(const char *str)
84{
Denys Vlasenko26414262013-07-28 23:17:00 +020085 /* Release all acquired resources */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000086 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000087 bb_perror_msg_and_die(str);
88}
Denys Vlasenko26ad2562013-11-26 12:02:18 +010089#else
90/* On Linux, shmdt is not mandatory on exit */
91# define error_exit(str) bb_perror_msg_and_die(str)
92#endif
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000093
94/*
95 * sem_up - up()'s a semaphore.
96 */
Rob Landley88621d72006-08-29 19:41:06 +000097static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000098{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000099 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000100 error_exit("semop[SMrup]");
101}
102
Denys Vlasenko26414262013-07-28 23:17:00 +0200103static void interrupted(int sig)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000104{
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100105 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Denys Vlasenko26414262013-07-28 23:17:00 +0200106 kill_myself_with_sig(sig);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000107}
108
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000109int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000110int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000111{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000112 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000113 int log_semid; /* ipc semaphore id */
114 int log_shmid; /* ipc shared memory id */
Phil Sutter92edab12015-03-22 17:36:20 +0100115 int follow = getopt32(argv, "fF");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +0000117 INIT_G();
118
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000119 log_shmid = shmget(KEY_ID, 0, 0);
120 if (log_shmid == -1)
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100121 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000122
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000123 /* Attach shared memory to our char* */
124 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
125 if (shbuf == NULL)
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100126 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000127
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");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000131
Denys Vlasenko26414262013-07-28 23:17:00 +0200132 bb_signals(BB_FATAL_SIGS, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000134 /* Suppose atomic memory read */
135 /* Max possible value for tail is shbuf->size - 1 */
136 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000137
Phil Sutter92edab12015-03-22 17:36:20 +0100138 /* Loop for -f or -F, one pass otherwise */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000139 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000140 unsigned shbuf_size;
141 unsigned shbuf_tail;
142 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000143#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000144 int i;
145 int len_first_part;
146 int len_total = len_total; /* for gcc */
147 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000148#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000149 if (semop(log_semid, SMrdn, 2) == -1)
150 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000151
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000152 /* 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! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000156
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000157 if (DEBUG)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100158 printf("cur:%u tail:%u size:%u\n",
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000159 cur, shbuf_tail, shbuf_size);
160
Phil Sutter92edab12015-03-22 17:36:20 +0100161 if (!(follow & 1)) { /* not -f */
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200162 /* if -F, "convert" it to -f, so that we don't
Phil Sutter92edab12015-03-22 17:36:20 +0100163 * dump the entire buffer on each iteration
164 */
165 follow >>= 1;
166
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000167 /* 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;
Phil Sutter92edab12015-03-22 17:36:20 +0100179 } else { /* -f */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000180 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000181 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100182 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000183 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000184 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000185 }
186 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000187
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000188 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000189#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000190 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;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000206#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000207 while (cur != shbuf_tail) {
208 fputs(shbuf_data + cur, stdout);
209 cur += strlen(shbuf_data + cur) + 1;
210 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000211 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000212 }
213#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000214 unlock:
215 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000216 sem_up(log_semid);
217
Denis Vlasenko085231f2007-01-10 22:35:54 +0000218#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000219 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
220 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000221 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000222 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000223#endif
Denys Vlasenko71f6c1a2013-11-26 11:44:27 +0100224 fflush_all();
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000225 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000226
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100227 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000228
Denis Vlasenko085231f2007-01-10 22:35:54 +0000229 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000230}