blob: 1753ce3247a5967f89cd75c43e59deda308900e7 [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 *
Bernhard Reutner-Fischerd04e9b82006-06-01 14:36:14 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000010 */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000013#include <sys/ipc.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000014#include <sys/sem.h>
15#include <sys/shm.h>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000016
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000017#define DEBUG 0
18
Denis Vlasenkobd1aeeb2008-06-11 15:43:19 +000019/* our shared key (syslogd.c and logread.c must be in sync) */
Denis Vlasenko085231f2007-01-10 22:35:54 +000020enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000021
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000022struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000023 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000024 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000025 char data[1]; // messages
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000026};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000027
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000028static const struct sembuf init_sem[3] = {
29 {0, -1, IPC_NOWAIT | SEM_UNDO},
30 {1, 0}, {0, +1, SEM_UNDO}
31};
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000032
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +000033struct globals {
34 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
35 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
36 struct shbuf_ds *shbuf;
37};
38#define G (*(struct globals*)&bb_common_bufsiz1)
39#define SMrup (G.SMrup)
40#define SMrdn (G.SMrdn)
41#define shbuf (G.shbuf)
Denis Vlasenko7049ff82008-06-25 09:53:17 +000042#define INIT_G() do { \
43 memcpy(SMrup, init_sem, sizeof(init_sem)); \
44} while (0)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000045
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000046static void error_exit(const char *str) ATTRIBUTE_NORETURN;
47static void error_exit(const char *str)
48{
49 //release all acquired resources
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000050 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000051 bb_perror_msg_and_die(str);
52}
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000053
54/*
55 * sem_up - up()'s a semaphore.
56 */
Rob Landley88621d72006-08-29 19:41:06 +000057static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000058{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000059 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000060 error_exit("semop[SMrup]");
61}
62
Denis Vlasenko085231f2007-01-10 22:35:54 +000063static void interrupted(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000064{
65 signal(SIGINT, SIG_IGN);
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000066 shmdt(shbuf);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000067 exit(EXIT_SUCCESS);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000068}
69
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000070int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000071int logread_main(int argc ATTRIBUTE_UNUSED, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000072{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000073 unsigned cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000074 int log_semid; /* ipc semaphore id */
75 int log_shmid; /* ipc shared memory id */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000076 smallint follow = getopt32(argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Denis Vlasenkoe85dbae2007-11-23 03:41:20 +000078 INIT_G();
79
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000080 log_shmid = shmget(KEY_ID, 0, 0);
81 if (log_shmid == -1)
Denis Vlasenko085231f2007-01-10 22:35:54 +000082 bb_perror_msg_and_die("can't find syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000083
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000084 /* Attach shared memory to our char* */
85 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
86 if (shbuf == NULL)
Denis Vlasenko085231f2007-01-10 22:35:54 +000087 bb_perror_msg_and_die("can't access syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000088
89 log_semid = semget(KEY_ID, 0, 0);
90 if (log_semid == -1)
91 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000093 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000095 /* Suppose atomic memory read */
96 /* Max possible value for tail is shbuf->size - 1 */
97 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000098
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000099 /* Loop for logread -f, one pass if there was no -f */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000100 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000101 unsigned shbuf_size;
102 unsigned shbuf_tail;
103 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +0000104#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000105 int i;
106 int len_first_part;
107 int len_total = len_total; /* for gcc */
108 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000109#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +0000110 if (semop(log_semid, SMrdn, 2) == -1)
111 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000112
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000113 /* Copy the info, helps gcc to realize that it doesn't change */
114 shbuf_size = shbuf->size;
115 shbuf_tail = shbuf->tail;
116 shbuf_data = shbuf->data; /* pointer! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000117
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000118 if (DEBUG)
119 printf("cur:%d tail:%i size:%i\n",
120 cur, shbuf_tail, shbuf_size);
121
122 if (!follow) {
123 /* advance to oldest complete message */
124 /* find NUL */
125 cur += strlen(shbuf_data + cur);
126 if (cur >= shbuf_size) { /* last byte in buffer? */
127 cur = strnlen(shbuf_data, shbuf_tail);
128 if (cur == shbuf_tail)
129 goto unlock; /* no complete messages */
130 }
131 /* advance to first byte of the message */
132 cur++;
133 if (cur >= shbuf_size) /* last byte in buffer? */
134 cur = 0;
135 } else { /* logread -f */
136 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000137 sem_up(log_semid);
Denis Vlasenko085231f2007-01-10 22:35:54 +0000138 fflush(stdout);
139 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000140 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000141 }
142 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000143
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000144 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000145#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000146 len_first_part = len_total = shbuf_tail - cur;
147 if (len_total < 0) {
148 /* message wraps: */
149 /* [SECOND PART.........FIRST PART] */
150 /* ^data ^tail ^cur ^size */
151 len_total += shbuf_size;
152 }
153 copy = xmalloc(len_total + 1);
154 if (len_first_part < 0) {
155 /* message wraps (see above) */
156 len_first_part = shbuf_size - cur;
157 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
158 }
159 memcpy(copy, shbuf_data + cur, len_first_part);
160 copy[len_total] = '\0';
161 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000162#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000163 while (cur != shbuf_tail) {
164 fputs(shbuf_data + cur, stdout);
165 cur += strlen(shbuf_data + cur) + 1;
166 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000167 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000168 }
169#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000170 unlock:
171 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000172 sem_up(log_semid);
173
Denis Vlasenko085231f2007-01-10 22:35:54 +0000174#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000175 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
176 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000177 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000178 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000179#endif
180 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000181
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000182 shmdt(shbuf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000183
Denis Vlasenko085231f2007-01-10 22:35:54 +0000184 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000185}