1 /* vi: set sw=4 ts=4: */
3 * circular buffer syslog implementation for busybox
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //config:config LOGREAD
12 //config: bool "logread (5 kb)"
14 //WRONG: it should be compilable without SYSLOG=y:
15 //WRONG: depends on FEATURE_IPC_SYSLOG
17 //config: If you enabled Circular Buffer support, you almost
18 //config: certainly want to enable this feature as well. This
19 //config: utility will allow you to read the messages that are
20 //config: stored in the syslogd circular buffer.
22 //config:config FEATURE_LOGREAD_REDUCED_LOCKING
23 //config: bool "Double buffering"
25 //config: depends on LOGREAD
27 //config: 'logread' output to slow serial terminals can have
28 //config: side effects on syslog because of the semaphore.
29 //config: This option make logread to double buffer copy
30 //config: from circular buffer, minimizing semaphore
31 //config: contention at some minor memory expense.
34 //applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
36 //kbuild:lib-$(CONFIG_LOGREAD) += logread.o
38 //usage:#define logread_trivial_usage
40 //usage:#define logread_full_usage "\n\n"
41 //usage: "Show messages in syslogd's circular buffer\n"
42 //usage: "\n -f Output data as log grows"
43 //usage: "\n -F Same as -f, but dump buffer first"
46 #include "common_bufsiz.h"
53 /* our shared key (syslogd.c and logread.c must be in sync) */
54 enum { KEY_ID
= 0x414e4547 }; /* "GENA" */
57 int32_t size
; // size of data - 1
58 int32_t tail
; // end of message list
59 char data
[1]; // messages
62 static const struct sembuf init_sem
[3] = {
63 {0, -1, IPC_NOWAIT
| SEM_UNDO
},
64 {1, 0}, {0, +1, SEM_UNDO
}
68 struct sembuf SMrup
[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
69 struct sembuf SMrdn
[2]; // {1, 0}, {0, +1, SEM_UNDO}
70 struct shbuf_ds
*shbuf
;
72 #define G (*(struct globals*)bb_common_bufsiz1)
73 #define SMrup (G.SMrup)
74 #define SMrdn (G.SMrdn)
75 #define shbuf (G.shbuf)
76 #define INIT_G() do { \
77 setup_common_bufsiz(); \
78 memcpy(SMrup, init_sem, sizeof(init_sem)); \
82 static void error_exit(const char *str
) NORETURN
;
83 static void error_exit(const char *str
)
85 /* Release all acquired resources */
87 bb_perror_msg_and_die(str
);
90 /* On Linux, shmdt is not mandatory on exit */
91 # define error_exit(str) bb_simple_perror_msg_and_die(str)
95 * sem_up - up()'s a semaphore.
97 static void sem_up(int semid
)
99 if (semop(semid
, SMrup
, 1) == -1)
100 error_exit("semop[SMrup]");
103 static void interrupted(int sig
)
105 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
106 kill_myself_with_sig(sig
);
109 int logread_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
110 int logread_main(int argc UNUSED_PARAM
, char **argv
)
113 int log_semid
; /* ipc semaphore id */
114 int log_shmid
; /* ipc shared memory id */
115 int follow
= getopt32(argv
, "fF");
119 log_shmid
= shmget(KEY_ID
, 0, 0);
121 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
123 /* Attach shared memory to our char* */
124 shbuf
= shmat(log_shmid
, NULL
, SHM_RDONLY
);
126 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
128 log_semid
= semget(KEY_ID
, 0, 0);
130 error_exit("can't get access to semaphores for syslogd buffer");
132 bb_signals(BB_FATAL_SIGS
, interrupted
);
134 /* Suppose atomic memory read */
135 /* Max possible value for tail is shbuf->size - 1 */
138 /* Loop for -f or -F, one pass otherwise */
142 const char *shbuf_data
;
143 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
146 int len_total
= len_total
; /* for gcc */
147 char *copy
= copy
; /* for gcc */
149 if (semop(log_semid
, SMrdn
, 2) == -1)
150 error_exit("semop[SMrdn]");
152 /* Copy the info, helps gcc to realize that it doesn't change */
153 shbuf_size
= shbuf
->size
;
154 shbuf_tail
= shbuf
->tail
;
155 shbuf_data
= shbuf
->data
; /* pointer! */
158 printf("cur:%u tail:%u size:%u\n",
159 cur
, shbuf_tail
, shbuf_size
);
161 if (!(follow
& 1)) { /* not -f */
162 /* if -F, "convert" it to -f, so that we don't
163 * dump the entire buffer on each iteration
167 /* advance to oldest complete message */
169 cur
+= strlen(shbuf_data
+ cur
);
170 if (cur
>= shbuf_size
) { /* last byte in buffer? */
171 cur
= strnlen(shbuf_data
, shbuf_tail
);
172 if (cur
== shbuf_tail
)
173 goto unlock
; /* no complete messages */
175 /* advance to first byte of the message */
177 if (cur
>= shbuf_size
) /* last byte in buffer? */
180 if (cur
== shbuf_tail
) {
183 sleep1(); /* TODO: replace me with a sleep_on */
188 /* Read from cur to tail */
189 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
190 len_first_part
= len_total
= shbuf_tail
- cur
;
193 /* [SECOND PART.........FIRST PART] */
194 /* ^data ^tail ^cur ^size */
195 len_total
+= shbuf_size
;
197 copy
= xmalloc(len_total
+ 1);
198 if (len_first_part
< 0) {
199 /* message wraps (see above) */
200 len_first_part
= shbuf_size
- cur
;
201 memcpy(copy
+ len_first_part
, shbuf_data
, shbuf_tail
);
203 memcpy(copy
, shbuf_data
+ cur
, len_first_part
);
204 copy
[len_total
] = '\0';
207 while (cur
!= shbuf_tail
) {
208 fputs_stdout(shbuf_data
+ cur
);
209 cur
+= strlen(shbuf_data
+ cur
) + 1;
210 if (cur
>= shbuf_size
)
215 /* release the lock on the log chain */
218 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
219 for (i
= 0; i
< len_total
; i
+= strlen(copy
+ i
) + 1) {
220 fputs_stdout(copy
+ i
);
227 /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
229 fflush_stdout_and_exit_SUCCESS();