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