blob: 9939569040d3640e5d46181fa9c05fb3e2d27f1d [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
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052static void error_exit(const char *str) NORETURN;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000053static void error_exit(const char *str)
54{
55 //release all acquired resources
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000056 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000057 bb_perror_msg_and_die(str);
58}
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000059
60/*
61 * sem_up - up()'s a semaphore.
62 */
Rob Landley88621d72006-08-29 19:41:06 +000063static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000064{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000065 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000066 error_exit("semop[SMrup]");
67}
68
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069static void interrupted(int sig UNUSED_PARAM)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000070{
71 signal(SIGINT, SIG_IGN);
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000072 shmdt(shbuf);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000073 exit(EXIT_SUCCESS);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000074}
75
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000076int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000077int logread_main(int argc UNUSED_PARAM, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000078{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000079 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000080 int log_semid; /* ipc semaphore id */
81 int log_shmid; /* ipc shared memory id */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000082 smallint follow = getopt32(argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +000084 INIT_G();
85
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000086 log_shmid = shmget(KEY_ID, 0, 0);
87 if (log_shmid == -1)
Denis Vlasenko085231f2007-01-10 22:35:54 +000088 bb_perror_msg_and_die("can't find syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000089
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000090 /* Attach shared memory to our char* */
91 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
92 if (shbuf == NULL)
Denis Vlasenko085231f2007-01-10 22:35:54 +000093 bb_perror_msg_and_die("can't access syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000094
95 log_semid = semget(KEY_ID, 0, 0);
96 if (log_semid == -1)
97 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000098
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000099 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000101 /* Suppose atomic memory read */
102 /* Max possible value for tail is shbuf->size - 1 */
103 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000104
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000105 /* Loop for logread -f, one pass if there was no -f */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000106 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000107 unsigned shbuf_size;
108 unsigned shbuf_tail;
109 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000110#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000111 int i;
112 int len_first_part;
113 int len_total = len_total; /* for gcc */
114 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000115#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000116 if (semop(log_semid, SMrdn, 2) == -1)
117 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000118
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000119 /* Copy the info, helps gcc to realize that it doesn't change */
120 shbuf_size = shbuf->size;
121 shbuf_tail = shbuf->tail;
122 shbuf_data = shbuf->data; /* pointer! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000123
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000124 if (DEBUG)
125 printf("cur:%d tail:%i size:%i\n",
126 cur, shbuf_tail, shbuf_size);
127
128 if (!follow) {
129 /* advance to oldest complete message */
130 /* find NUL */
131 cur += strlen(shbuf_data + cur);
132 if (cur >= shbuf_size) { /* last byte in buffer? */
133 cur = strnlen(shbuf_data, shbuf_tail);
134 if (cur == shbuf_tail)
135 goto unlock; /* no complete messages */
136 }
137 /* advance to first byte of the message */
138 cur++;
139 if (cur >= shbuf_size) /* last byte in buffer? */
140 cur = 0;
141 } else { /* logread -f */
142 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000143 sem_up(log_semid);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100144 fflush_all();
Denis Vlasenko085231f2007-01-10 22:35:54 +0000145 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000146 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000147 }
148 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000149
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000150 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000151#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000152 len_first_part = len_total = shbuf_tail - cur;
153 if (len_total < 0) {
154 /* message wraps: */
155 /* [SECOND PART.........FIRST PART] */
156 /* ^data ^tail ^cur ^size */
157 len_total += shbuf_size;
158 }
159 copy = xmalloc(len_total + 1);
160 if (len_first_part < 0) {
161 /* message wraps (see above) */
162 len_first_part = shbuf_size - cur;
163 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
164 }
165 memcpy(copy, shbuf_data + cur, len_first_part);
166 copy[len_total] = '\0';
167 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000168#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000169 while (cur != shbuf_tail) {
170 fputs(shbuf_data + cur, stdout);
171 cur += strlen(shbuf_data + cur) + 1;
172 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000173 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000174 }
175#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000176 unlock:
177 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000178 sem_up(log_semid);
179
Denis Vlasenko085231f2007-01-10 22:35:54 +0000180#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000181 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
182 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000183 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000184 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000185#endif
186 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000187
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000188 shmdt(shbuf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000189
Denis Vlasenko085231f2007-01-10 22:35:54 +0000190 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000191}