1 /* vi: set sw=4 ts=4: */
3 * circular buffer syslog implementation for busybox
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@cachier.com>
7 * Maintainer: Gennady Feldman <gena01@cachier.com> as of Mar 12, 2001
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.
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.
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
31 #include <sys/types.h>
38 /* Stupid libc doesn't have a reliable way for use to know
39 * that libc5 is being used. Assume this is good enough */
40 #if ! defined __GLIBC__ && ! defined __UCLIBC__
41 #error Sorry. Looks like you are using libc5.
42 #error libc5 shm support isnt good enough.
43 #error Please disable BB_FEATURE_IPC_SYSLOG
47 static const long KEY_ID = 0x414e4547; /*"GENA"*/
49 static struct shbuf_ds {
50 int size; // size of data written
51 int head; // start of message list
52 int tail; // end of message list
53 char data[1]; // data/messages
54 } *buf = NULL; // shared memory pointer
57 // Semaphore operation structures
58 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
59 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
61 static int log_shmid = -1; // ipc shared memory id
62 static int log_semid = -1; // ipc semaphore id
63 static jmp_buf jmp_env;
65 static void error_exit(const char *str);
66 static void interrupted(int sig);
69 * sem_up - up()'s a semaphore.
71 static inline void sem_up(int semid)
73 if ( semop(semid, SMrup, 1) == -1 )
74 error_exit("semop[SMrup]");
78 * sem_down - down()'s a semaphore
80 static inline void sem_down(int semid)
82 if ( semop(semid, SMrdn, 2) == -1 )
83 error_exit("semop[SMrdn]");
86 extern int logread_main(int argc, char **argv)
90 /* no options, no getopt */
94 // handle intrrupt signal
95 if (setjmp(jmp_env)) goto output_end;
97 // attempt to redefine ^C signal
98 signal(SIGINT, interrupted);
100 if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
101 error_exit("Can't find circular buffer");
103 // Attach shared memory to our char*
104 if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
105 error_exit("Can't get access to circular buffer from syslogd");
107 if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
108 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
114 //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
115 if (buf->head == buf->tail) {
116 printf("<empty syslog>\n");
119 while ( i != buf->tail) {
120 printf("%s", buf->data+i);
121 i+= strlen(buf->data+i) + 1;
134 static void interrupted(int sig){
135 signal(SIGINT, SIG_IGN);
139 static void error_exit(const char *str){
141 //release all acquired resources