blob: b1758a6959454b56b86e5abfca1f7460f9e22b50 [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
Denis Vlasenko06af2162007-02-03 17:28:39 +000057int logread_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000058int logread_main(int argc, char **argv)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000059{
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000060 int cur;
Denis Vlasenko085231f2007-01-10 22:35:54 +000061 int log_semid; /* ipc semaphore id */
62 int log_shmid; /* ipc shared memory id */
63 smallint follow = getopt32(argc, argv, "f");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000065 log_shmid = shmget(KEY_ID, 0, 0);
66 if (log_shmid == -1)
Denis Vlasenko085231f2007-01-10 22:35:54 +000067 bb_perror_msg_and_die("can't find syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000068
69 // Attach shared memory to our char*
70 buf = shmat(log_shmid, NULL, SHM_RDONLY);
71 if (buf == NULL)
Denis Vlasenko085231f2007-01-10 22:35:54 +000072 bb_perror_msg_and_die("can't access syslogd buffer");
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000073
74 log_semid = semget(KEY_ID, 0, 0);
75 if (log_semid == -1)
76 error_exit("can't get access to semaphores for syslogd buffer");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000078 // attempt to redefine ^C signal
79 signal(SIGINT, interrupted);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000080
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000081 // Suppose atomic memory move
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000082 cur = follow ? buf->tail : buf->head;
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000083
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000084 do {
Denis Vlasenko085231f2007-01-10 22:35:54 +000085#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000086 char *buf_data;
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000087 int log_len, j;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000088#endif
Denis Vlasenko085231f2007-01-10 22:35:54 +000089 if (semop(log_semid, SMrdn, 2) == -1)
90 error_exit("semop[SMrdn]");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000091
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000092 if (DEBUG)
Denis Vlasenko085231f2007-01-10 22:35:54 +000093 printf("head:%i cur:%d tail:%i size:%i\n",
94 buf->head, cur, buf->tail, buf->size);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000095
96 if (buf->head == buf->tail || cur == buf->tail) {
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +000097 if (follow) {
98 sem_up(log_semid);
Denis Vlasenko085231f2007-01-10 22:35:54 +000099 fflush(stdout);
100 sleep(1); /* TODO: replace me with a sleep_on */
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000101 continue;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000102 }
Denis Vlasenko085231f2007-01-10 22:35:54 +0000103 puts("<empty syslog>");
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000104 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105
106 // Read Memory
Denis Vlasenko085231f2007-01-10 22:35:54 +0000107#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000108 log_len = buf->tail - cur;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000109 if (log_len < 0)
110 log_len += buf->size;
Denis Vlasenkob95636c2006-12-19 23:36:04 +0000111 buf_data = xmalloc(log_len);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000112
Denis Vlasenko085231f2007-01-10 22:35:54 +0000113 if (buf->tail >= cur)
114 j = log_len;
115 else
116 j = buf->size - cur;
117 memcpy(buf_data, buf->data + cur, j);
118
119 if (buf->tail < cur)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000120 memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000121 cur = buf->tail;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000122#else
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000123 while (cur != buf->tail) {
124 fputs(buf->data + cur, stdout);
125 cur += strlen(buf->data + cur) + 1;
126 if (cur >= buf->size)
127 cur = 0;
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000128 }
129#endif
130 // release the lock on the log chain
131 sem_up(log_semid);
132
Denis Vlasenko085231f2007-01-10 22:35:54 +0000133#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000134 for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
135 fputs(buf_data + j, stdout);
Glenn L McGrathf7dd10f2003-09-26 01:03:16 +0000136 }
137 free(buf_data);
138#endif
139 } while (follow);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000140
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000141 shmdt(buf);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000142
Denis Vlasenko085231f2007-01-10 22:35:54 +0000143 fflush_stdout_and_exit(EXIT_SUCCESS);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000144}