blob: da4a4d4dfc481b8798d06539b79e533605ac55bf [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 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define logread_trivial_usage
Phil Sutter92edab12015-03-22 17:36:20 +010013//usage: "[-fF]"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage:#define logread_full_usage "\n\n"
15//usage: "Show messages in syslogd's circular buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -f Output data as log grows"
Phil Sutter92edab12015-03-22 17:36:20 +010017//usage: "\n -F Same as -f, but dump buffer first"
Pere Orga5bc8c002011-04-11 03:29:49 +020018
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000020#include <sys/ipc.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000021#include <sys/sem.h>
22#include <sys/shm.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000023
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000024#define DEBUG 0
25
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000026/* our shared key (syslogd.c and logread.c must be in sync) */
Denis Vlasenko085231f2007-01-10 22:35:54 +000027enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000028
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000029struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000030 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000031 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000032 char data[1]; // messages
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000033};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000034
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000035static const struct sembuf init_sem[3] = {
36 {0, -1, IPC_NOWAIT | SEM_UNDO},
37 {1, 0}, {0, +1, SEM_UNDO}
38};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000039
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000040struct globals {
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;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010044} FIX_ALIASING;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000045#define G (*(struct globals*)&bb_common_bufsiz1)
46#define SMrup (G.SMrup)
47#define SMrdn (G.SMrdn)
48#define shbuf (G.shbuf)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000049#define INIT_G() do { \
50 memcpy(SMrup, init_sem, sizeof(init_sem)); \
51} while (0)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000052
Denys Vlasenko26ad2562013-11-26 12:02:18 +010053#if 0
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000054static void error_exit(const char *str) NORETURN;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000055static void error_exit(const char *str)
56{
Denys Vlasenko26414262013-07-28 23:17:00 +020057 /* Release all acquired resources */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000058 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000059 bb_perror_msg_and_die(str);
60}
Denys Vlasenko26ad2562013-11-26 12:02:18 +010061#else
62/* On Linux, shmdt is not mandatory on exit */
63# define error_exit(str) bb_perror_msg_and_die(str)
64#endif
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000065
66/*
67 * sem_up - up()'s a semaphore.
68 */
Rob Landley88621d72006-08-29 19:41:06 +000069static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000070{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000071 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000072 error_exit("semop[SMrup]");
73}
74
Denys Vlasenko26414262013-07-28 23:17:00 +020075static void interrupted(int sig)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000076{
Denys Vlasenko26ad2562013-11-26 12:02:18 +010077 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Denys Vlasenko26414262013-07-28 23:17:00 +020078 kill_myself_with_sig(sig);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000079}
80
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000081int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000082int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000083{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000084 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000085 int log_semid; /* ipc semaphore id */
86 int log_shmid; /* ipc shared memory id */
Phil Sutter92edab12015-03-22 17:36:20 +010087 int follow = getopt32(argv, "fF");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +000089 INIT_G();
90
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000091 log_shmid = shmget(KEY_ID, 0, 0);
92 if (log_shmid == -1)
Denys Vlasenko26ad2562013-11-26 12:02:18 +010093 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000094
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000095 /* Attach shared memory to our char* */
96 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
97 if (shbuf == NULL)
Denys Vlasenko26ad2562013-11-26 12:02:18 +010098 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000099
100 log_semid = semget(KEY_ID, 0, 0);
101 if (log_semid == -1)
102 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Denys Vlasenko26414262013-07-28 23:17:00 +0200104 bb_signals(BB_FATAL_SIGS, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000106 /* Suppose atomic memory read */
107 /* Max possible value for tail is shbuf->size - 1 */
108 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000109
Phil Sutter92edab12015-03-22 17:36:20 +0100110 /* Loop for -f or -F, one pass otherwise */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000111 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000112 unsigned shbuf_size;
113 unsigned shbuf_tail;
114 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000115#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000116 int i;
117 int len_first_part;
118 int len_total = len_total; /* for gcc */
119 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000120#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000121 if (semop(log_semid, SMrdn, 2) == -1)
122 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000123
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000124 /* 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! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000128
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000129 if (DEBUG)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100130 printf("cur:%u tail:%u size:%u\n",
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000131 cur, shbuf_tail, shbuf_size);
132
Phil Sutter92edab12015-03-22 17:36:20 +0100133 if (!(follow & 1)) { /* not -f */
134 /* if -F, "convert" it to -f, so that we dont
135 * dump the entire buffer on each iteration
136 */
137 follow >>= 1;
138
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000139 /* advance to oldest complete message */
140 /* find NUL */
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 */
146 }
147 /* advance to first byte of the message */
148 cur++;
149 if (cur >= shbuf_size) /* last byte in buffer? */
150 cur = 0;
Phil Sutter92edab12015-03-22 17:36:20 +0100151 } else { /* -f */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000152 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000153 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100154 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000155 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000156 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000157 }
158 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000159
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000160 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000161#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000162 len_first_part = len_total = shbuf_tail - cur;
163 if (len_total < 0) {
164 /* message wraps: */
165 /* [SECOND PART.........FIRST PART] */
166 /* ^data ^tail ^cur ^size */
167 len_total += shbuf_size;
168 }
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);
174 }
175 memcpy(copy, shbuf_data + cur, len_first_part);
176 copy[len_total] = '\0';
177 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000178#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000179 while (cur != shbuf_tail) {
180 fputs(shbuf_data + cur, stdout);
181 cur += strlen(shbuf_data + cur) + 1;
182 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000183 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000184 }
185#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000186 unlock:
187 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000188 sem_up(log_semid);
189
Denis Vlasenko085231f2007-01-10 22:35:54 +0000190#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000191 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
192 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000193 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000194 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000195#endif
Denys Vlasenko71f6c1a2013-11-26 11:44:27 +0100196 fflush_all();
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000197 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000198
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100199 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000200
Denis Vlasenko085231f2007-01-10 22:35:54 +0000201 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000202}