1 /* vi: set sw=4 ts=4: */
3 * circular buffer syslog implementation for busybox
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 #include <sys/types.h>
25 static const long KEY_ID = 0x414e4547; /*"GENA"*/
27 static struct shbuf_ds {
28 int size; // size of data written
29 int head; // start of message list
30 int tail; // end of message list
31 char data[1]; // data/messages
32 } *buf = NULL; // shared memory pointer
35 // Semaphore operation structures
36 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
37 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
39 static int log_shmid = -1; // ipc shared memory id
40 static int log_semid = -1; // ipc semaphore id
41 static jmp_buf jmp_env;
43 static void error_exit(const char *str);
44 static void interrupted(int sig);
47 * sem_up - up()'s a semaphore.
49 static inline void sem_up(int semid)
51 if ( semop(semid, SMrup, 1) == -1 )
52 error_exit("semop[SMrup]");
56 * sem_down - down()'s a semaphore
58 static inline void sem_down(int semid)
60 if ( semop(semid, SMrdn, 2) == -1 )
61 error_exit("semop[SMrdn]");
64 int logread_main(int argc, char **argv)
69 if (argc == 2 && argv[1][0]=='-' && argv[1][1]=='f') {
72 /* no options, no getopt */
77 // handle interrupt signal
78 if (setjmp(jmp_env)) goto output_end;
80 // attempt to redefine ^C signal
81 signal(SIGINT, interrupted);
83 if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
84 error_exit("Can't find circular buffer");
86 // Attach shared memory to our char*
87 if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
88 error_exit("Can't get access to circular buffer from syslogd");
90 if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
91 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
93 // Suppose atomic memory move
94 i = follow ? buf->tail : buf->head;
97 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
104 //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
105 if (buf->head == buf->tail || i==buf->tail) {
108 sleep(1); /* TODO: replace me with a sleep_on */
111 printf("<empty syslog>\n");
116 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
117 log_len = buf->tail - i;
119 log_len += buf->size;
120 buf_data = (char *)xmalloc(log_len);
123 memcpy(buf_data, buf->data+i, buf->size-i);
124 memcpy(buf_data+buf->size-i, buf->data, buf->tail);
126 memcpy(buf_data, buf->data+i, buf->tail-i);
131 while ( i != buf->tail) {
132 printf("%s", buf->data+i);
133 i+= strlen(buf->data+i) + 1;
138 // release the lock on the log chain
141 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
142 for (j=0; j < log_len; j+=strlen(buf_data+j)+1) {
143 printf("%s", buf_data+j);
159 static void interrupted(int sig){
160 signal(SIGINT, SIG_IGN);
164 static void error_exit(const char *str){
166 //release all acquired resources