# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / busybox / sysklogd / logread.c.org
1 /* vi: set sw=4 ts=4: */
2 /*
3  * circular buffer syslog implementation for busybox
4  *
5  * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
6  *
7  * Maintainer: Gennady Feldman <gfeldman@gena01.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 <unistd.h>
37 #include "busybox.h"
38
39 static const long KEY_ID = 0x414e4547; /*"GENA"*/
40
41 static struct shbuf_ds {
42         int size;               // size of data written
43         int head;               // start of message list
44         int tail;               // end of message list
45     int pre_tail;           //added by echo for pre-end message
46         char data[1];           // data/messages
47 } *buf = NULL;                  // shared memory pointer
48
49
50 // Semaphore operation structures
51 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
52 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
53
54 static int      log_shmid = -1; // ipc shared memory id
55 static int      log_semid = -1; // ipc semaphore id
56 static jmp_buf  jmp_env;
57
58 static void error_exit(const char *str);
59 static void interrupted(int sig);
60
61 /*
62  * sem_up - up()'s a semaphore.
63  */
64 static inline void sem_up(int semid)
65 {
66         if ( semop(semid, SMrup, 1) == -1 )
67                 error_exit("semop[SMrup]");
68 }
69
70 /*
71  * sem_down - down()'s a semaphore
72  */
73 static inline void sem_down(int semid)
74 {
75         if ( semop(semid, SMrdn, 2) == -1 )
76                 error_exit("semop[SMrdn]");
77 }
78
79 extern int logread_main(int argc, char **argv)
80 {
81         int i;
82         int follow=0;
83
84         if (argc == 2 && strcmp(argv[1],"-f")==0) {
85                 follow = 1;
86         } else {
87                 /* no options, no getopt */
88                 if (argc > 1)
89                         bb_show_usage();
90         }
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
98         if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
99                 error_exit("Can't find circular buffer");
100
101         // Attach shared memory to our char*
102         if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
103                 error_exit("Can't get access to circular buffer from syslogd");
104
105         if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
106                 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
107
108         // Suppose atomic memory move
109         i = follow ? buf->tail : buf->head;
110
111         do {
112 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
113                 char *buf_data;
114                 int log_len,j;
115 #endif
116
117                 sem_down(log_semid);
118
119                 //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
120                 if (buf->head == buf->tail || i==buf->tail) {
121                         if (follow) {
122                                 sem_up(log_semid);
123                                 sleep(1);       /* TODO: replace me with a sleep_on */
124                                 continue;
125                         } else {
126                                 printf("<empty syslog>\n");
127                         }
128                 }
129
130                 // Read Memory
131 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
132                 log_len = buf->tail - i;
133                 if (log_len < 0)
134                         log_len += buf->size;
135                 buf_data = (char *)malloc(log_len);
136                 if (!buf_data)
137                         error_exit("malloc failed");
138
139                 if (buf->tail < i) {
140                         memcpy(buf_data, buf->data+i, buf->size-i);
141                         memcpy(buf_data+buf->size-i, buf->data, buf->tail);
142                 } else {
143                         memcpy(buf_data, buf->data+i, buf->tail-i);
144                 }
145                 i = buf->tail;
146
147 #else
148                 while ( i != buf->tail) {
149                         printf("%s", buf->data+i);
150                         i+= strlen(buf->data+i) + 1;
151                         if (i >= buf->size )
152                                 i=0;
153                 }
154 #endif
155                 // release the lock on the log chain
156                 sem_up(log_semid);
157
158 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
159                 for (j=0; j < log_len; j+=strlen(buf_data+j)+1) {
160                         printf("%s", buf_data+j);
161                         if (follow)
162                                 fflush(stdout);
163                 }
164                 free(buf_data);
165 #endif
166         } while (follow);
167
168 output_end:
169         if (log_shmid != -1)
170                 shmdt(buf);
171
172         return EXIT_SUCCESS;
173 }
174
175 static void interrupted(int sig){
176         signal(SIGINT, SIG_IGN);
177         longjmp(jmp_env, 1);
178 }
179
180 static void error_exit(const char *str){
181         perror(str);
182         //release all acquired resources
183         if (log_shmid != -1)
184                 shmdt(buf);
185
186         exit(1);
187 }