blob: d3349625caadcbc01ba386a3e280f2fd74dcb87e [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 *
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@cachier.com>
6 *
7 * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 *
24 */
25
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ipc.h>
31#include <sys/types.h>
32#include <sys/sem.h>
33#include <sys/shm.h>
34#include <signal.h>
35#include <setjmp.h>
36#include "busybox.h"
37
Eric Andersenb6b519b2001-04-09 23:52:18 +000038#if __GNU_LIBRARY__ < 5
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000039#error Sorry. Looks like you are using libc5.
40#error libc5 shm support isnt good enough.
41#error Please disable BB_FEATURE_IPC_SYSLOG
42#endif
43
44
45static const long KEY_ID = 0x414e4547; /*"GENA"*/
46
47static struct shbuf_ds {
48 int size; // size of data written
49 int head; // start of message list
50 int tail; // end of message list
51 char data[1]; // data/messages
52} *buf = NULL; // shared memory pointer
53
54
55// Semaphore operation structures
56static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
57static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
58
Eric Andersen8ffaf8d2001-03-23 17:04:47 +000059static int log_shmid = -1; // ipc shared memory id
60static int log_semid = -1; // ipc semaphore id
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000061static jmp_buf jmp_env;
62
63static void error_exit(const char *str);
64static void interrupted(int sig);
65
66/*
67 * sem_up - up()'s a semaphore.
68 */
69static inline void sem_up(int semid)
70{
71 if ( semop(semid, SMrup, 1) == -1 )
72 error_exit("semop[SMrup]");
73}
74
75/*
76 * sem_down - down()'s a semaphore
77 */
78static inline void sem_down(int semid)
79{
80 if ( semop(semid, SMrdn, 2) == -1 )
81 error_exit("semop[SMrdn]");
82}
83
84extern int logread_main(int argc, char **argv)
85{
86 int i;
87
88 /* no options, no getopt */
89 if (argc > 1)
90 show_usage();
91
92 // handle intrrupt signal
93 if (setjmp(jmp_env)) goto output_end;
94
95 // attempt to redefine ^C signal
96 signal(SIGINT, interrupted);
97
Eric Andersen8ffaf8d2001-03-23 17:04:47 +000098 if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000099 error_exit("Can't find circular buffer");
100
101 // Attach shared memory to our char*
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000102 if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000103 error_exit("Can't get access to circular buffer from syslogd");
104
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000105 if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000106 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
107
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000108 sem_down(log_semid);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000109 // Read Memory
110 i=buf->head;
111
112 //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
113 if (buf->head == buf->tail) {
114 printf("<empty syslog>\n");
115 }
116
117 while ( i != buf->tail) {
118 printf("%s", buf->data+i);
119 i+= strlen(buf->data+i) + 1;
120 if (i >= buf->size )
121 i=0;
122 }
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000123 sem_up(log_semid);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000124
125output_end:
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000126 if (log_shmid != -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000127 shmdt(buf);
128
129 return EXIT_SUCCESS;
130}
131
132static void interrupted(int sig){
133 signal(SIGINT, SIG_IGN);
134 longjmp(jmp_env, 1);
135}
136
137static void error_exit(const char *str){
138 perror(str);
139 //release all acquired resources
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000140 if (log_shmid != -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000141 shmdt(buf);
142
143 exit(1);
144}