blob: 69a084ff68156872bbb54cfd9c611b3d4c1bb91c [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 Vlasenko085231f2007-01-10 22:35:54 +000019enum { KEY_ID = 0x414e4547 }; /* "GENA" */
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000020
21static struct shbuf_ds {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000022 int32_t size; // size of data - 1
Denis Vlasenko085231f2007-01-10 22:35:54 +000023 int32_t tail; // end of message list
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000024 char data[1]; // messages
25} *shbuf;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000026
27// Semaphore operation structures
28static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
29static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
30
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000031
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000032static void error_exit(const char *str) ATTRIBUTE_NORETURN;
33static void error_exit(const char *str)
34{
35 //release all acquired resources
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000036 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000037 bb_perror_msg_and_die(str);
38}
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000039
40/*
41 * sem_up - up()'s a semaphore.
42 */
Rob Landley88621d72006-08-29 19:41:06 +000043static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000044{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000045 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000046 error_exit("semop[SMrup]");
47}
48
Denis Vlasenko085231f2007-01-10 22:35:54 +000049static void interrupted(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000050{
51 signal(SIGINT, SIG_IGN);
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000052 shmdt(shbuf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000053 exit(0);
54}
55
Denis Vlasenko06af2162007-02-03 17:28:39 +000056int logread_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000057int logread_main(int argc, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000058{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000059 int cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000060 int log_semid; /* ipc semaphore id */
61 int log_shmid; /* ipc shared memory id */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000062 smallint follow = getopt32(argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000064 log_shmid = shmget(KEY_ID, 0, 0);
65 if (log_shmid == -1)
Denis Vlasenko085231f2007-01-10 22:35:54 +000066 bb_perror_msg_and_die("can't find syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000067
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000068 /* Attach shared memory to our char* */
69 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
70 if (shbuf == NULL)
Denis Vlasenko085231f2007-01-10 22:35:54 +000071 bb_perror_msg_and_die("can't access syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000072
73 log_semid = semget(KEY_ID, 0, 0);
74 if (log_semid == -1)
75 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000077 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000079 /* Suppose atomic memory read */
80 /* Max possible value for tail is shbuf->size - 1 */
81 cur = shbuf->tail;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000082
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000083 /* Loop for logread -f, one pass if there was no -f */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000084 do {
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000085 unsigned shbuf_size;
86 unsigned shbuf_tail;
87 const char *shbuf_data;
Denis Vlasenko085231f2007-01-10 22:35:54 +000088#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000089 int i;
90 int len_first_part;
91 int len_total = len_total; /* for gcc */
92 char *copy = copy; /* for gcc */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000093#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +000094 if (semop(log_semid, SMrdn, 2) == -1)
95 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000096
Denis Vlasenko5f1b1492007-08-12 21:33:06 +000097 /* Copy the info, helps gcc to realize that it doesn't change */
98 shbuf_size = shbuf->size;
99 shbuf_tail = shbuf->tail;
100 shbuf_data = shbuf->data; /* pointer! */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000101
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000102 if (DEBUG)
103 printf("cur:%d tail:%i size:%i\n",
104 cur, shbuf_tail, shbuf_size);
105
106 if (!follow) {
107 /* advance to oldest complete message */
108 /* find NUL */
109 cur += strlen(shbuf_data + cur);
110 if (cur >= shbuf_size) { /* last byte in buffer? */
111 cur = strnlen(shbuf_data, shbuf_tail);
112 if (cur == shbuf_tail)
113 goto unlock; /* no complete messages */
114 }
115 /* advance to first byte of the message */
116 cur++;
117 if (cur >= shbuf_size) /* last byte in buffer? */
118 cur = 0;
119 } else { /* logread -f */
120 if (cur == shbuf_tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000121 sem_up(log_semid);
Denis Vlasenko085231f2007-01-10 22:35:54 +0000122 fflush(stdout);
123 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000124 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000125 }
126 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000127
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000128 /* Read from cur to tail */
Denis Vlasenko085231f2007-01-10 22:35:54 +0000129#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000130 len_first_part = len_total = shbuf_tail - cur;
131 if (len_total < 0) {
132 /* message wraps: */
133 /* [SECOND PART.........FIRST PART] */
134 /* ^data ^tail ^cur ^size */
135 len_total += shbuf_size;
136 }
137 copy = xmalloc(len_total + 1);
138 if (len_first_part < 0) {
139 /* message wraps (see above) */
140 len_first_part = shbuf_size - cur;
141 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
142 }
143 memcpy(copy, shbuf_data + cur, len_first_part);
144 copy[len_total] = '\0';
145 cur = shbuf_tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000146#else
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000147 while (cur != shbuf_tail) {
148 fputs(shbuf_data + cur, stdout);
149 cur += strlen(shbuf_data + cur) + 1;
150 if (cur >= shbuf_size)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000151 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000152 }
153#endif
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000154 unlock:
155 /* release the lock on the log chain */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000156 sem_up(log_semid);
157
Denis Vlasenko085231f2007-01-10 22:35:54 +0000158#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000159 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
160 fputs(copy + i, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000161 }
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000162 free(copy);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000163#endif
164 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000165
Denis Vlasenko5f1b1492007-08-12 21:33:06 +0000166 shmdt(shbuf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000167
Denis Vlasenko085231f2007-01-10 22:35:54 +0000168 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000169}