blob: ae0b2194b80b0b6cf33175a63608344eaa7dddeb [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"
16//usage: "\nOptions:"
17//usage: "\n -f Output data as log grows"
18
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
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{
56 //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}
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000060
61/*
62 * sem_up - up()'s a semaphore.
63 */
Rob Landley88621d72006-08-29 19:41:06 +000064static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000065{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000066 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000067 error_exit("semop[SMrup]");
68}
69
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000070static void interrupted(int sig UNUSED_PARAM)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000071{
72 signal(SIGINT, SIG_IGN);
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000073 shmdt(shbuf);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000074 exit(EXIT_SUCCESS);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000075}
76
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000077int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000078int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000079{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000080 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000081 int log_semid; /* ipc semaphore id */
82 int log_shmid; /* ipc shared memory id */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000083 smallint follow = getopt32(argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +000085 INIT_G();
86
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000087 log_shmid = shmget(KEY_ID, 0, 0);
88 if (log_shmid == -1)
Denis Vlasenko085231f2007-01-10 22:35:54 +000089 bb_perror_msg_and_die("can't find syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000090
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000091 /* Attach shared memory to our char* */
92 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
93 if (shbuf == NULL)
Denis Vlasenko085231f2007-01-10 22:35:54 +000094 bb_perror_msg_and_die("can't access syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000095
96 log_semid = semget(KEY_ID, 0, 0);
97 if (log_semid == -1)
98 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000099
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000100 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000101
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000102 /* Suppose atomic memory read */
103 /* Max possible value for tail is shbuf->size - 1 */
104 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000105
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000106 /* Loop for logread -f, one pass if there was no -f */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000107 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000108 unsigned shbuf_size;
109 unsigned shbuf_tail;
110 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000111#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000112 int i;
113 int len_first_part;
114 int len_total = len_total; /* for gcc */
115 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000116#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000117 if (semop(log_semid, SMrdn, 2) == -1)
118 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000119
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000120 /* Copy the info, helps gcc to realize that it doesn't change */
121 shbuf_size = shbuf->size;
122 shbuf_tail = shbuf->tail;
123 shbuf_data = shbuf->data; /* pointer! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000124
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000125 if (DEBUG)
126 printf("cur:%d tail:%i size:%i\n",
127 cur, shbuf_tail, shbuf_size);
128
129 if (!follow) {
130 /* advance to oldest complete message */
131 /* find NUL */
132 cur += strlen(shbuf_data + cur);
133 if (cur >= shbuf_size) { /* last byte in buffer? */
134 cur = strnlen(shbuf_data, shbuf_tail);
135 if (cur == shbuf_tail)
136 goto unlock; /* no complete messages */
137 }
138 /* advance to first byte of the message */
139 cur++;
140 if (cur >= shbuf_size) /* last byte in buffer? */
141 cur = 0;
142 } else { /* logread -f */
143 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000144 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100145 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000146 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000147 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000148 }
149 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000150
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000151 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000152#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000153 len_first_part = len_total = shbuf_tail - cur;
154 if (len_total < 0) {
155 /* message wraps: */
156 /* [SECOND PART.........FIRST PART] */
157 /* ^data ^tail ^cur ^size */
158 len_total += shbuf_size;
159 }
160 copy = xmalloc(len_total + 1);
161 if (len_first_part < 0) {
162 /* message wraps (see above) */
163 len_first_part = shbuf_size - cur;
164 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
165 }
166 memcpy(copy, shbuf_data + cur, len_first_part);
167 copy[len_total] = '\0';
168 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000169#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000170 while (cur != shbuf_tail) {
171 fputs(shbuf_data + cur, stdout);
172 cur += strlen(shbuf_data + cur) + 1;
173 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000174 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000175 }
176#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000177 unlock:
178 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000179 sem_up(log_semid);
180
Denis Vlasenko085231f2007-01-10 22:35:54 +0000181#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000182 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
183 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000184 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000185 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000186#endif
187 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000188
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000189 shmdt(shbuf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000190
Denis Vlasenko085231f2007-01-10 22:35:54 +0000191 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000192}