execute "safe applets" exev if not standalone shell
[oweals/busybox.git] / sysklogd / logread.c
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  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "busybox.h"
13 #include <sys/ipc.h>
14 //#include <sys/types.h>
15 #include <sys/sem.h>
16 #include <sys/shm.h>
17 //#include <signal.h>
18 //#include <setjmp.h>
19
20 #define DEBUG 0
21
22 static const long KEY_ID = 0x414e4547; /* "GENA" */
23
24 static struct shbuf_ds {
25         int32_t size;           // size of data written
26         int32_t head;           // start of message list
27         int32_t tail;           // end of message list
28         char data[1];           // data/messages
29 } *buf;                         // shared memory pointer
30
31
32 // Semaphore operation structures
33 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
34 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
35
36 static int log_shmid = -1;      // ipc shared memory id
37 static int log_semid = -1;      // ipc semaphore id
38
39 static void error_exit(const char *str) ATTRIBUTE_NORETURN;
40 static void error_exit(const char *str)
41 {
42         //release all acquired resources
43         if (log_shmid != -1)
44                 shmdt(buf);
45         bb_perror_msg_and_die(str);
46 }
47
48 /*
49  * sem_up - up()'s a semaphore.
50  */
51 static void sem_up(int semid)
52 {
53         if (semop(semid, SMrup, 1) == -1)
54                 error_exit("semop[SMrup]");
55 }
56
57 /*
58  * sem_down - down()'s a semaphore
59  */
60 static void sem_down(int semid)
61 {
62         if (semop(semid, SMrdn, 2) == -1)
63                 error_exit("semop[SMrdn]");
64 }
65
66 static void interrupted(int sig)
67 {
68         signal(SIGINT, SIG_IGN);
69         shmdt(buf);
70         exit(0);
71 }
72
73 int logread_main(int argc, char **argv)
74 {
75         int cur;
76         int follow = 1;
77
78         if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) {
79                 follow = 0;
80                 /* no options, no getopt */
81                 if (argc > 1)
82                         bb_show_usage();
83         }
84
85         log_shmid = shmget(KEY_ID, 0, 0);
86         if (log_shmid == -1)
87                 error_exit("can't find circular buffer");
88
89         // Attach shared memory to our char*
90         buf = shmat(log_shmid, NULL, SHM_RDONLY);
91         if (buf == NULL)
92                 error_exit("can't get access to syslogd buffer");
93
94         log_semid = semget(KEY_ID, 0, 0);
95         if (log_semid == -1)
96                 error_exit("can't get access to semaphores for syslogd buffer");
97
98         // attempt to redefine ^C signal
99         signal(SIGINT, interrupted);
100
101         // Suppose atomic memory move
102         cur = follow ? buf->tail : buf->head;
103
104         do {
105 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
106                 char *buf_data;
107                 int log_len, j;
108 #endif
109                 sem_down(log_semid);
110
111                 if (DEBUG)
112                         printf("head:%i cur:%d tail:%i size:%i\n", buf->head, cur, buf->tail, buf->size);
113
114                 if (buf->head == buf->tail || cur == buf->tail) {
115                         if (follow) {
116                                 sem_up(log_semid);
117                                 sleep(1);       /* TODO: replace me with a sleep_on */
118                                 continue;
119                         } else {
120                                 printf("<empty syslog>\n");
121                         }
122                 }
123
124                 // Read Memory
125 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
126                 log_len = buf->tail - cur;
127                 if (log_len < 0)
128                         log_len += buf->size;
129                 buf_data = xmalloc(log_len);
130
131                 if (buf->tail >= cur) {
132                         memcpy(buf_data, buf->data + cur, log_len);
133                 } else {
134                         memcpy(buf_data, buf->data + cur, buf->size - cur);
135                         memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
136                 }
137                 cur = buf->tail;
138 #else
139                 while (cur != buf->tail) {
140                         fputs(buf->data + cur, stdout);
141                         cur += strlen(buf->data + cur) + 1;
142                         if (cur >= buf->size)
143                                 cur = 0;
144                 }
145 #endif
146                 // release the lock on the log chain
147                 sem_up(log_semid);
148
149 #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
150                 for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
151                         fputs(buf_data + j, stdout);
152                 }
153                 free(buf_data);
154 #endif
155                 fflush(stdout);
156         } while (follow);
157
158         shmdt(buf);
159
160         return EXIT_SUCCESS;
161 }