blob: d092056bf151cb4b903e6bf1972e676ebdd669bd [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
Bernhard Reutner-Fischerd04e9b82006-06-01 14:36:14 +000012#include "busybox.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 Vlasenko085231f2007-01-10 22:35:54 +000022 int32_t size; // size of data written
23 int32_t head; // start of message list
24 int32_t tail; // end of message list
25 char data[1]; // data/messages
26} *buf; // shared memory pointer
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000027
28// Semaphore operation structures
29static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
30static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
31
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000032
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000033static void error_exit(const char *str) ATTRIBUTE_NORETURN;
34static void error_exit(const char *str)
35{
36 //release all acquired resources
Denis Vlasenko085231f2007-01-10 22:35:54 +000037 shmdt(buf);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000038 bb_perror_msg_and_die(str);
39}
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000040
41/*
42 * sem_up - up()'s a semaphore.
43 */
Rob Landley88621d72006-08-29 19:41:06 +000044static void sem_up(int semid)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000045{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000046 if (semop(semid, SMrup, 1) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000047 error_exit("semop[SMrup]");
48}
49
Denis Vlasenko085231f2007-01-10 22:35:54 +000050static void interrupted(int sig ATTRIBUTE_UNUSED)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000051{
52 signal(SIGINT, SIG_IGN);
53 shmdt(buf);
54 exit(0);
55}
56
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 */
62 smallint follow = getopt32(argc, 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
68 // Attach shared memory to our char*
69 buf = shmat(log_shmid, NULL, SHM_RDONLY);
70 if (buf == 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 // attempt to redefine ^C signal
78 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000079
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000080 // Suppose atomic memory move
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000081 cur = follow ? buf->tail : buf->head;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000082
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000083 do {
Denis Vlasenko085231f2007-01-10 22:35:54 +000084#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000085 char *buf_data;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000086 int log_len, j;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000087#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +000088 if (semop(log_semid, SMrdn, 2) == -1)
89 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000090
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000091 if (DEBUG)
Denis Vlasenko085231f2007-01-10 22:35:54 +000092 printf("head:%i cur:%d tail:%i size:%i\n",
93 buf->head, cur, buf->tail, buf->size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000094
95 if (buf->head == buf->tail || cur == buf->tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000096 if (follow) {
97 sem_up(log_semid);
Denis Vlasenko085231f2007-01-10 22:35:54 +000098 fflush(stdout);
99 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000100 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000101 }
Denis Vlasenko085231f2007-01-10 22:35:54 +0000102 puts("<empty syslog>");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000103 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000104
105 // Read Memory
Denis Vlasenko085231f2007-01-10 22:35:54 +0000106#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000107 log_len = buf->tail - cur;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000108 if (log_len < 0)
109 log_len += buf->size;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000110 buf_data = xmalloc(log_len);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000111
Denis Vlasenko085231f2007-01-10 22:35:54 +0000112 if (buf->tail >= cur)
113 j = log_len;
114 else
115 j = buf->size - cur;
116 memcpy(buf_data, buf->data + cur, j);
117
118 if (buf->tail < cur)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000119 memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000120 cur = buf->tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000121#else
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000122 while (cur != buf->tail) {
123 fputs(buf->data + cur, stdout);
124 cur += strlen(buf->data + cur) + 1;
125 if (cur >= buf->size)
126 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000127 }
128#endif
129 // release the lock on the log chain
130 sem_up(log_semid);
131
Denis Vlasenko085231f2007-01-10 22:35:54 +0000132#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000133 for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
134 fputs(buf_data + j, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000135 }
136 free(buf_data);
137#endif
138 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000139
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000140 shmdt(buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000141
Denis Vlasenko085231f2007-01-10 22:35:54 +0000142 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000143}