Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * circular buffer syslog implementation for busybox |
| 4 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 6 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 7 | * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001 |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 11 | //config:config LOGREAD |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 12 | //config: bool "logread (6 kb)" |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 13 | //config: default y |
Denys Vlasenko | 9cc3d3a | 2016-12-23 02:42:26 +0100 | [diff] [blame] | 14 | //WRONG: it should be compilable without SYSLOG=y: |
| 15 | //WRONG: depends on FEATURE_IPC_SYSLOG |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 21 | //config: |
| 22 | //config:config FEATURE_LOGREAD_REDUCED_LOCKING |
| 23 | //config: bool "Double buffering" |
| 24 | //config: default y |
| 25 | //config: depends on LOGREAD |
| 26 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | d34f300 | 2015-10-18 18:42:03 +0200 | [diff] [blame] | 32 | //config: |
| 33 | |
| 34 | //applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP)) |
| 35 | |
| 36 | //kbuild:lib-$(CONFIG_LOGREAD) += logread.o |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 37 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 38 | //usage:#define logread_trivial_usage |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 39 | //usage: "[-fF]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 40 | //usage:#define logread_full_usage "\n\n" |
| 41 | //usage: "Show messages in syslogd's circular buffer\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 42 | //usage: "\n -f Output data as log grows" |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 43 | //usage: "\n -F Same as -f, but dump buffer first" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 44 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 46 | #include "common_bufsiz.h" |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 47 | #include <sys/ipc.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 48 | #include <sys/sem.h> |
| 49 | #include <sys/shm.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 51 | #define DEBUG 0 |
| 52 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 53 | /* our shared key (syslogd.c and logread.c must be in sync) */ |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 54 | enum { KEY_ID = 0x414e4547 }; /* "GENA" */ |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 56 | struct shbuf_ds { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 57 | int32_t size; // size of data - 1 |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 58 | int32_t tail; // end of message list |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 59 | char data[1]; // messages |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 60 | }; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 62 | static const struct sembuf init_sem[3] = { |
| 63 | {0, -1, IPC_NOWAIT | SEM_UNDO}, |
| 64 | {1, 0}, {0, +1, SEM_UNDO} |
| 65 | }; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 67 | struct globals { |
| 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; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 71 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 72 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 73 | #define SMrup (G.SMrup) |
| 74 | #define SMrdn (G.SMrdn) |
| 75 | #define shbuf (G.shbuf) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 76 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 77 | setup_common_bufsiz(); \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 78 | memcpy(SMrup, init_sem, sizeof(init_sem)); \ |
| 79 | } while (0) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 80 | |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 81 | #if 0 |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 82 | static void error_exit(const char *str) NORETURN; |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 83 | static void error_exit(const char *str) |
| 84 | { |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 85 | /* Release all acquired resources */ |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 86 | shmdt(shbuf); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 87 | bb_perror_msg_and_die(str); |
| 88 | } |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 89 | #else |
| 90 | /* On Linux, shmdt is not mandatory on exit */ |
| 91 | # define error_exit(str) bb_perror_msg_and_die(str) |
| 92 | #endif |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * sem_up - up()'s a semaphore. |
| 96 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 97 | static void sem_up(int semid) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 98 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 99 | if (semop(semid, SMrup, 1) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 100 | error_exit("semop[SMrup]"); |
| 101 | } |
| 102 | |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 103 | static void interrupted(int sig) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 104 | { |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 105 | /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */ |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 106 | kill_myself_with_sig(sig); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 109 | int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 110 | int logread_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 111 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 112 | unsigned cur; |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 113 | int log_semid; /* ipc semaphore id */ |
| 114 | int log_shmid; /* ipc shared memory id */ |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 115 | int follow = getopt32(argv, "fF"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | e85dbae | 2007-11-23 03:41:20 +0000 | [diff] [blame] | 117 | INIT_G(); |
| 118 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 119 | log_shmid = shmget(KEY_ID, 0, 0); |
| 120 | if (log_shmid == -1) |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 121 | bb_perror_msg_and_die("can't %s syslogd buffer", "find"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 123 | /* Attach shared memory to our char* */ |
| 124 | shbuf = shmat(log_shmid, NULL, SHM_RDONLY); |
| 125 | if (shbuf == NULL) |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 126 | bb_perror_msg_and_die("can't %s syslogd buffer", "access"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 127 | |
| 128 | log_semid = semget(KEY_ID, 0, 0); |
| 129 | if (log_semid == -1) |
| 130 | error_exit("can't get access to semaphores for syslogd buffer"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 131 | |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 132 | bb_signals(BB_FATAL_SIGS, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 134 | /* Suppose atomic memory read */ |
| 135 | /* Max possible value for tail is shbuf->size - 1 */ |
| 136 | cur = shbuf->tail; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 137 | |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 138 | /* Loop for -f or -F, one pass otherwise */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 139 | do { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 140 | unsigned shbuf_size; |
| 141 | unsigned shbuf_tail; |
| 142 | const char *shbuf_data; |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 143 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 144 | int i; |
| 145 | int len_first_part; |
| 146 | int len_total = len_total; /* for gcc */ |
| 147 | char *copy = copy; /* for gcc */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 148 | #endif |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 149 | if (semop(log_semid, SMrdn, 2) == -1) |
| 150 | error_exit("semop[SMrdn]"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 151 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 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! */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 157 | if (DEBUG) |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 158 | printf("cur:%u tail:%u size:%u\n", |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 159 | cur, shbuf_tail, shbuf_size); |
| 160 | |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 161 | if (!(follow & 1)) { /* not -f */ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 162 | /* if -F, "convert" it to -f, so that we don't |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 163 | * dump the entire buffer on each iteration |
| 164 | */ |
| 165 | follow >>= 1; |
| 166 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 167 | /* advance to oldest complete message */ |
| 168 | /* find NUL */ |
| 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 */ |
| 174 | } |
| 175 | /* advance to first byte of the message */ |
| 176 | cur++; |
| 177 | if (cur >= shbuf_size) /* last byte in buffer? */ |
| 178 | cur = 0; |
Phil Sutter | 92edab1 | 2015-03-22 17:36:20 +0100 | [diff] [blame] | 179 | } else { /* -f */ |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 180 | if (cur == shbuf_tail) { |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 181 | sem_up(log_semid); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 182 | fflush_all(); |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 183 | sleep(1); /* TODO: replace me with a sleep_on */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 184 | continue; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 188 | /* Read from cur to tail */ |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 189 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 190 | len_first_part = len_total = shbuf_tail - cur; |
| 191 | if (len_total < 0) { |
| 192 | /* message wraps: */ |
| 193 | /* [SECOND PART.........FIRST PART] */ |
| 194 | /* ^data ^tail ^cur ^size */ |
| 195 | len_total += shbuf_size; |
| 196 | } |
| 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); |
| 202 | } |
| 203 | memcpy(copy, shbuf_data + cur, len_first_part); |
| 204 | copy[len_total] = '\0'; |
| 205 | cur = shbuf_tail; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 206 | #else |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 207 | while (cur != shbuf_tail) { |
| 208 | fputs(shbuf_data + cur, stdout); |
| 209 | cur += strlen(shbuf_data + cur) + 1; |
| 210 | if (cur >= shbuf_size) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 211 | cur = 0; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 212 | } |
| 213 | #endif |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 214 | unlock: |
| 215 | /* release the lock on the log chain */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 216 | sem_up(log_semid); |
| 217 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 218 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 219 | for (i = 0; i < len_total; i += strlen(copy + i) + 1) { |
| 220 | fputs(copy + i, stdout); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 221 | } |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 222 | free(copy); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 223 | #endif |
Denys Vlasenko | 71f6c1a | 2013-11-26 11:44:27 +0100 | [diff] [blame] | 224 | fflush_all(); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 225 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 226 | |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 227 | /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 228 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 229 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 230 | } |