Added some comment lines in the header indicating that Gennady Feldman is the
[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  * 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 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <string.h>\r
29 #include <sys/ipc.h>\r
30 #include <sys/types.h>\r
31 #include <sys/sem.h>\r
32 #include <sys/shm.h>\r
33 #include <signal.h>\r
34 #include <setjmp.h>\r
35 #include "busybox.h"\r
36 \r
37 static const long KEY_ID = 0x414e4547; /*"GENA"*/\r
38 \r
39 static struct shbuf_ds {\r
40         int size;               // size of data written\r
41         int head;               // start of message list\r
42         int tail;               // end of message list\r
43         char data[1];           // data/messages\r
44 } *buf = NULL;                  // shared memory pointer\r
45 \r
46 \r
47 // Semaphore operation structures\r
48 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup\r
49 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn\r
50 \r
51 static int      shmid = -1;     // ipc shared memory id\r
52 static int      semid = -1;     // ipc semaphore id\r
53 static jmp_buf  jmp_env;\r
54 \r
55 static void error_exit(const char *str);\r
56 static void interrupted(int sig);\r
57 \r
58 /*\r
59  * sem_up - up()'s a semaphore.\r
60  */\r
61 static inline void sem_up(int semid)\r
62 {\r
63         if ( semop(semid, SMrup, 1) == -1 ) \r
64                 error_exit("semop[SMrup]");\r
65 }\r
66 \r
67 /*\r
68  * sem_down - down()'s a semaphore\r
69  */                             \r
70 static inline void sem_down(int semid)\r
71 {\r
72         if ( semop(semid, SMrdn, 2) == -1 )\r
73                 error_exit("semop[SMrdn]");\r
74 }\r
75 \r
76 extern int logread_main(int argc, char **argv)\r
77 {\r
78         int i;\r
79         \r
80         /* no options, no getopt */\r
81         if (argc > 1)\r
82                 show_usage();\r
83         \r
84         // handle intrrupt signal\r
85         if (setjmp(jmp_env)) goto output_end;\r
86         \r
87         // attempt to redefine ^C signal\r
88         signal(SIGINT, interrupted);\r
89         \r
90         if ( (shmid = shmget(KEY_ID, 0, 0)) == -1)\r
91                 error_exit("Can't find circular buffer");\r
92         \r
93         // Attach shared memory to our char*\r
94         if ( (buf = shmat(shmid, NULL, SHM_RDONLY)) == NULL)\r
95                 error_exit("Can't get access to circular buffer from syslogd");\r
96 \r
97         if ( (semid = semget(KEY_ID, 0, 0)) == -1)\r
98                 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");\r
99 \r
100         sem_down(semid);        \r
101         // Read Memory \r
102         i=buf->head;\r
103 \r
104         //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);\r
105         if (buf->head == buf->tail) {\r
106                 printf("<empty syslog>\n");\r
107         }\r
108         \r
109         while ( i != buf->tail) {\r
110                 printf("%s", buf->data+i);\r
111                 i+= strlen(buf->data+i) + 1;\r
112                 if (i >= buf->size )\r
113                         i=0;\r
114         }\r
115         sem_up(semid);\r
116 \r
117 output_end:\r
118         if (shmid != -1) \r
119                 shmdt(buf);\r
120                 \r
121         return EXIT_SUCCESS;            \r
122 }\r
123 \r
124 static void interrupted(int sig){\r
125         signal(SIGINT, SIG_IGN);\r
126         longjmp(jmp_env, 1);\r
127 }\r
128 \r
129 static void error_exit(const char *str){\r
130         perror(str);\r
131         //release all acquired resources\r
132         if (shmid != -1) \r
133                 shmdt(buf);\r
134 \r
135         exit(1);\r
136 }\r
137 \r