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