blob: bea73d9323955992e45ad62ca921b0fe46b692e6 [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
13//usage: "[-f]"
14//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"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000019#include <sys/ipc.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000020#include <sys/sem.h>
21#include <sys/shm.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000022
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000023#define DEBUG 0
24
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000025/* our shared key (syslogd.c and logread.c must be in sync) */
Denis Vlasenko085231f2007-01-10 22:35:54 +000026enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000027
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000028struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000029 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000030 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000031 char data[1]; // messages
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000032};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000033
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000034static const struct sembuf init_sem[3] = {
35 {0, -1, IPC_NOWAIT | SEM_UNDO},
36 {1, 0}, {0, +1, SEM_UNDO}
37};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000038
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000039struct globals {
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;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010043} FIX_ALIASING;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000044#define G (*(struct globals*)&bb_common_bufsiz1)
45#define SMrup (G.SMrup)
46#define SMrdn (G.SMrdn)
47#define shbuf (G.shbuf)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000048#define INIT_G() do { \
49 memcpy(SMrup, init_sem, sizeof(init_sem)); \
50} while (0)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000051
Denys Vlasenko26ad2562013-11-26 12:02:18 +010052#if 0
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000053static void error_exit(const char *str) NORETURN;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000054static void error_exit(const char *str)
55{
Denys Vlasenko26414262013-07-28 23:17:00 +020056 /* Release all acquired resources */
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000057 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000058 bb_perror_msg_and_die(str);
59}
Denys Vlasenko26ad2562013-11-26 12:02:18 +010060#else
61/* On Linux, shmdt is not mandatory on exit */
62# define error_exit(str) bb_perror_msg_and_die(str)
63#endif
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000064
65/*
66 * sem_up - up()'s a semaphore.
67 */
Rob Landley88621d72006-08-29 19:41:06 +000068static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000069{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000070 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000071 error_exit("semop[SMrup]");
72}
73
Denys Vlasenko26414262013-07-28 23:17:00 +020074static void interrupted(int sig)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000075{
Denys Vlasenko26ad2562013-11-26 12:02:18 +010076 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Denys Vlasenko26414262013-07-28 23:17:00 +020077 kill_myself_with_sig(sig);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000078}
79
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000080int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000081int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000082{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000083 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000084 int log_semid; /* ipc semaphore id */
85 int log_shmid; /* ipc shared memory id */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000086 smallint follow = getopt32(argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +000088 INIT_G();
89
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000090 log_shmid = shmget(KEY_ID, 0, 0);
91 if (log_shmid == -1)
Denys Vlasenko26ad2562013-11-26 12:02:18 +010092 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000093
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000094 /* Attach shared memory to our char* */
95 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
96 if (shbuf == NULL)
Denys Vlasenko26ad2562013-11-26 12:02:18 +010097 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000098
99 log_semid = semget(KEY_ID, 0, 0);
100 if (log_semid == -1)
101 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Denys Vlasenko26414262013-07-28 23:17:00 +0200103 bb_signals(BB_FATAL_SIGS, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000104
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000105 /* Suppose atomic memory read */
106 /* Max possible value for tail is shbuf->size - 1 */
107 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000108
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000109 /* Loop for logread -f, one pass if there was no -f */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000110 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000111 unsigned shbuf_size;
112 unsigned shbuf_tail;
113 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000114#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000115 int i;
116 int len_first_part;
117 int len_total = len_total; /* for gcc */
118 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000119#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000120 if (semop(log_semid, SMrdn, 2) == -1)
121 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000122
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000123 /* 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! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000127
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000128 if (DEBUG)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100129 printf("cur:%u tail:%u size:%u\n",
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000130 cur, shbuf_tail, shbuf_size);
131
132 if (!follow) {
133 /* advance to oldest complete message */
134 /* find NUL */
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 */
140 }
141 /* advance to first byte of the message */
142 cur++;
143 if (cur >= shbuf_size) /* last byte in buffer? */
144 cur = 0;
145 } else { /* logread -f */
146 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000147 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100148 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000149 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000150 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000151 }
152 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000153
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000154 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000155#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000156 len_first_part = len_total = shbuf_tail - cur;
157 if (len_total < 0) {
158 /* message wraps: */
159 /* [SECOND PART.........FIRST PART] */
160 /* ^data ^tail ^cur ^size */
161 len_total += shbuf_size;
162 }
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);
168 }
169 memcpy(copy, shbuf_data + cur, len_first_part);
170 copy[len_total] = '\0';
171 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000172#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000173 while (cur != shbuf_tail) {
174 fputs(shbuf_data + cur, stdout);
175 cur += strlen(shbuf_data + cur) + 1;
176 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000177 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000178 }
179#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000180 unlock:
181 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000182 sem_up(log_semid);
183
Denis Vlasenko085231f2007-01-10 22:35:54 +0000184#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000185 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
186 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000187 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000188 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000189#endif
Denys Vlasenko71f6c1a2013-11-26 11:44:27 +0100190 fflush_all();
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000191 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000192
Denys Vlasenko26ad2562013-11-26 12:02:18 +0100193 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000194
Denis Vlasenko085231f2007-01-10 22:35:54 +0000195 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000196}