move remaining help text from include/usage.src.h
[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 source tree.
10  */
11
12 //usage:#define logread_trivial_usage
13 //usage:       "[-f]"
14 //usage:#define logread_full_usage "\n\n"
15 //usage:       "Show messages in syslogd's circular buffer\n"
16 //usage:     "\nOptions:"
17 //usage:     "\n        -f      Output data as log grows"
18
19 #include "libbb.h"
20 #include <sys/ipc.h>
21 #include <sys/sem.h>
22 #include <sys/shm.h>
23
24 #define DEBUG 0
25
26 /* our shared key (syslogd.c and logread.c must be in sync) */
27 enum { KEY_ID = 0x414e4547 }; /* "GENA" */
28
29 struct shbuf_ds {
30         int32_t size;           // size of data - 1
31         int32_t tail;           // end of message list
32         char data[1];           // messages
33 };
34
35 static const struct sembuf init_sem[3] = {
36         {0, -1, IPC_NOWAIT | SEM_UNDO},
37         {1, 0}, {0, +1, SEM_UNDO}
38 };
39
40 struct globals {
41         struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
42         struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
43         struct shbuf_ds *shbuf;
44 } FIX_ALIASING;
45 #define G (*(struct globals*)&bb_common_bufsiz1)
46 #define SMrup (G.SMrup)
47 #define SMrdn (G.SMrdn)
48 #define shbuf (G.shbuf)
49 #define INIT_G() do { \
50         memcpy(SMrup, init_sem, sizeof(init_sem)); \
51 } while (0)
52
53 static void error_exit(const char *str) NORETURN;
54 static void error_exit(const char *str)
55 {
56         //release all acquired resources
57         shmdt(shbuf);
58         bb_perror_msg_and_die(str);
59 }
60
61 /*
62  * sem_up - up()'s a semaphore.
63  */
64 static void sem_up(int semid)
65 {
66         if (semop(semid, SMrup, 1) == -1)
67                 error_exit("semop[SMrup]");
68 }
69
70 static void interrupted(int sig UNUSED_PARAM)
71 {
72         signal(SIGINT, SIG_IGN);
73         shmdt(shbuf);
74         exit(EXIT_SUCCESS);
75 }
76
77 int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int logread_main(int argc UNUSED_PARAM, char **argv)
79 {
80         unsigned cur;
81         int log_semid; /* ipc semaphore id */
82         int log_shmid; /* ipc shared memory id */
83         smallint follow = getopt32(argv, "f");
84
85         INIT_G();
86
87         log_shmid = shmget(KEY_ID, 0, 0);
88         if (log_shmid == -1)
89                 bb_perror_msg_and_die("can't find syslogd buffer");
90
91         /* Attach shared memory to our char* */
92         shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
93         if (shbuf == NULL)
94                 bb_perror_msg_and_die("can't access syslogd buffer");
95
96         log_semid = semget(KEY_ID, 0, 0);
97         if (log_semid == -1)
98                 error_exit("can't get access to semaphores for syslogd buffer");
99
100         signal(SIGINT, interrupted);
101
102         /* Suppose atomic memory read */
103         /* Max possible value for tail is shbuf->size - 1 */
104         cur = shbuf->tail;
105
106         /* Loop for logread -f, one pass if there was no -f */
107         do {
108                 unsigned shbuf_size;
109                 unsigned shbuf_tail;
110                 const char *shbuf_data;
111 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
112                 int i;
113                 int len_first_part;
114                 int len_total = len_total; /* for gcc */
115                 char *copy = copy; /* for gcc */
116 #endif
117                 if (semop(log_semid, SMrdn, 2) == -1)
118                         error_exit("semop[SMrdn]");
119
120                 /* Copy the info, helps gcc to realize that it doesn't change */
121                 shbuf_size = shbuf->size;
122                 shbuf_tail = shbuf->tail;
123                 shbuf_data = shbuf->data; /* pointer! */
124
125                 if (DEBUG)
126                         printf("cur:%d tail:%i size:%i\n",
127                                         cur, shbuf_tail, shbuf_size);
128
129                 if (!follow) {
130                         /* advance to oldest complete message */
131                         /* find NUL */
132                         cur += strlen(shbuf_data + cur);
133                         if (cur >= shbuf_size) { /* last byte in buffer? */
134                                 cur = strnlen(shbuf_data, shbuf_tail);
135                                 if (cur == shbuf_tail)
136                                         goto unlock; /* no complete messages */
137                         }
138                         /* advance to first byte of the message */
139                         cur++;
140                         if (cur >= shbuf_size) /* last byte in buffer? */
141                                 cur = 0;
142                 } else { /* logread -f */
143                         if (cur == shbuf_tail) {
144                                 sem_up(log_semid);
145                                 fflush_all();
146                                 sleep(1); /* TODO: replace me with a sleep_on */
147                                 continue;
148                         }
149                 }
150
151                 /* Read from cur to tail */
152 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
153                 len_first_part = len_total = shbuf_tail - cur;
154                 if (len_total < 0) {
155                         /* message wraps: */
156                         /* [SECOND PART.........FIRST PART] */
157                         /*  ^data      ^tail    ^cur      ^size */
158                         len_total += shbuf_size;
159                 }
160                 copy = xmalloc(len_total + 1);
161                 if (len_first_part < 0) {
162                         /* message wraps (see above) */
163                         len_first_part = shbuf_size - cur;
164                         memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
165                 }
166                 memcpy(copy, shbuf_data + cur, len_first_part);
167                 copy[len_total] = '\0';
168                 cur = shbuf_tail;
169 #else
170                 while (cur != shbuf_tail) {
171                         fputs(shbuf_data + cur, stdout);
172                         cur += strlen(shbuf_data + cur) + 1;
173                         if (cur >= shbuf_size)
174                                 cur = 0;
175                 }
176 #endif
177  unlock:
178                 /* release the lock on the log chain */
179                 sem_up(log_semid);
180
181 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
182                 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
183                         fputs(copy + i, stdout);
184                 }
185                 free(copy);
186 #endif
187         } while (follow);
188
189         shmdt(shbuf);
190
191         fflush_stdout_and_exit(EXIT_SUCCESS);
192 }