# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / busybox / sysklogd / logread.c
1 /* 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
38 #if __GNU_LIBRARY__ < 5
39 #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
45 static const long KEY_ID = 0x414e4547; /*"GENA"*/
46
47 static 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         int pre_tail;   //added by echo 
52         char data[1];           // data/messages
53 } *buf = NULL;                  // shared memory pointer
54
55
56 // Semaphore operation structures
57 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
58 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
59
60 static int      log_shmid = -1; // ipc shared memory id
61 static int      log_semid = -1; // ipc semaphore id
62 static jmp_buf  jmp_env;
63
64 static void error_exit(const char *str);
65 static void interrupted(int sig);
66
67 /*
68  * sem_up - up()'s a semaphore.
69  */
70 static inline void sem_up(int semid)
71 {
72         if ( semop(semid, SMrup, 1) == -1 ) 
73                 error_exit("semop[SMrup]");
74 }
75
76 /*
77  * sem_down - down()'s a semaphore
78  */                             
79 static inline void sem_down(int semid)
80 {
81         if ( semop(semid, SMrdn, 2) == -1 )
82                 error_exit("semop[SMrdn]");
83 }
84
85 extern int logread_main(int argc, char **argv)
86 {
87         int i;
88         int msglen;
89         
90         // handle intrrupt signal
91         if (setjmp(jmp_env)) goto output_end;
92         
93         // attempt to redefine ^C signal
94         signal(SIGINT, interrupted);
95         
96         if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
97                 error_exit("Can't find circular buffer");
98         
99         // Attach shared memory to our char*
100         if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
101                 error_exit("Can't get access to circular buffer from syslogd");
102
103         if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
104                 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
105
106         sem_down(log_semid);    
107         // Read Memory 
108         i=buf->head;
109
110         //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
111         if (buf->head == buf->tail) {
112                 printf("<empty syslog>\n");
113         }
114         
115         while ( i < buf->tail) {
116                 msglen = strlen(buf->data+i);
117                 if(i+msglen >= buf->size)
118                         break;
119                 printf("%s", buf->data+i);
120                 i+= strlen(buf->data+i) + 1;
121                 /*if (i >= buf->size )
122                         i=0;*/
123         }
124         sem_up(log_semid);
125
126 output_end:
127         if (log_shmid != -1) 
128                 shmdt(buf);
129                 
130         return EXIT_SUCCESS;            
131 }
132
133 static void interrupted(int sig){
134         signal(SIGINT, SIG_IGN);
135         longjmp(jmp_env, 1);
136 }
137
138 static void error_exit(const char *str){
139         perror(str);
140         //release all acquired resources
141         if (log_shmid != -1) 
142                 shmdt(buf);
143
144         exit(1);
145 }