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