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