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 | */ |
| 11 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | //usage: "\n -f Output data as log grows" |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 19 | #include <sys/ipc.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 20 | #include <sys/sem.h> |
| 21 | #include <sys/shm.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 23 | #define DEBUG 0 |
| 24 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 25 | /* our shared key (syslogd.c and logread.c must be in sync) */ |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 26 | enum { KEY_ID = 0x414e4547 }; /* "GENA" */ |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 28 | struct shbuf_ds { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 29 | int32_t size; // size of data - 1 |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 30 | int32_t tail; // end of message list |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 31 | char data[1]; // messages |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 32 | }; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 34 | static const struct sembuf init_sem[3] = { |
| 35 | {0, -1, IPC_NOWAIT | SEM_UNDO}, |
| 36 | {1, 0}, {0, +1, SEM_UNDO} |
| 37 | }; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 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; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 43 | } FIX_ALIASING; |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 44 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 45 | #define SMrup (G.SMrup) |
| 46 | #define SMrdn (G.SMrdn) |
| 47 | #define shbuf (G.shbuf) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 48 | #define INIT_G() do { \ |
| 49 | memcpy(SMrup, init_sem, sizeof(init_sem)); \ |
| 50 | } while (0) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 51 | |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 52 | #if 0 |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 53 | static void error_exit(const char *str) NORETURN; |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 54 | static void error_exit(const char *str) |
| 55 | { |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 56 | /* Release all acquired resources */ |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 57 | shmdt(shbuf); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 58 | bb_perror_msg_and_die(str); |
| 59 | } |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 60 | #else |
| 61 | /* On Linux, shmdt is not mandatory on exit */ |
| 62 | # define error_exit(str) bb_perror_msg_and_die(str) |
| 63 | #endif |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * sem_up - up()'s a semaphore. |
| 67 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 68 | static void sem_up(int semid) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 69 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 70 | if (semop(semid, SMrup, 1) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 71 | error_exit("semop[SMrup]"); |
| 72 | } |
| 73 | |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 74 | static void interrupted(int sig) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 75 | { |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 76 | /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */ |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 77 | kill_myself_with_sig(sig); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 80 | int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 81 | int logread_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 82 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 83 | unsigned cur; |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 84 | int log_semid; /* ipc semaphore id */ |
| 85 | int log_shmid; /* ipc shared memory id */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 86 | smallint follow = getopt32(argv, "f"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | e85dbae | 2007-11-23 03:41:20 +0000 | [diff] [blame] | 88 | INIT_G(); |
| 89 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 90 | log_shmid = shmget(KEY_ID, 0, 0); |
| 91 | if (log_shmid == -1) |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 92 | bb_perror_msg_and_die("can't %s syslogd buffer", "find"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 94 | /* Attach shared memory to our char* */ |
| 95 | shbuf = shmat(log_shmid, NULL, SHM_RDONLY); |
| 96 | if (shbuf == NULL) |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 97 | bb_perror_msg_and_die("can't %s syslogd buffer", "access"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 98 | |
| 99 | log_semid = semget(KEY_ID, 0, 0); |
| 100 | if (log_semid == -1) |
| 101 | error_exit("can't get access to semaphores for syslogd buffer"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | |
Denys Vlasenko | 2641426 | 2013-07-28 23:17:00 +0200 | [diff] [blame] | 103 | bb_signals(BB_FATAL_SIGS, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 105 | /* Suppose atomic memory read */ |
| 106 | /* Max possible value for tail is shbuf->size - 1 */ |
| 107 | cur = shbuf->tail; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 109 | /* Loop for logread -f, one pass if there was no -f */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 110 | do { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 111 | unsigned shbuf_size; |
| 112 | unsigned shbuf_tail; |
| 113 | const char *shbuf_data; |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 114 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 115 | int i; |
| 116 | int len_first_part; |
| 117 | int len_total = len_total; /* for gcc */ |
| 118 | char *copy = copy; /* for gcc */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 119 | #endif |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 120 | if (semop(log_semid, SMrdn, 2) == -1) |
| 121 | error_exit("semop[SMrdn]"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 123 | /* Copy the info, helps gcc to realize that it doesn't change */ |
| 124 | shbuf_size = shbuf->size; |
| 125 | shbuf_tail = shbuf->tail; |
| 126 | shbuf_data = shbuf->data; /* pointer! */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 128 | if (DEBUG) |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 129 | printf("cur:%u tail:%u size:%u\n", |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 130 | cur, shbuf_tail, shbuf_size); |
| 131 | |
| 132 | if (!follow) { |
| 133 | /* advance to oldest complete message */ |
| 134 | /* find NUL */ |
| 135 | cur += strlen(shbuf_data + cur); |
| 136 | if (cur >= shbuf_size) { /* last byte in buffer? */ |
| 137 | cur = strnlen(shbuf_data, shbuf_tail); |
| 138 | if (cur == shbuf_tail) |
| 139 | goto unlock; /* no complete messages */ |
| 140 | } |
| 141 | /* advance to first byte of the message */ |
| 142 | cur++; |
| 143 | if (cur >= shbuf_size) /* last byte in buffer? */ |
| 144 | cur = 0; |
| 145 | } else { /* logread -f */ |
| 146 | if (cur == shbuf_tail) { |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 147 | sem_up(log_semid); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 148 | fflush_all(); |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 149 | sleep(1); /* TODO: replace me with a sleep_on */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 150 | continue; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 154 | /* Read from cur to tail */ |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 155 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 156 | len_first_part = len_total = shbuf_tail - cur; |
| 157 | if (len_total < 0) { |
| 158 | /* message wraps: */ |
| 159 | /* [SECOND PART.........FIRST PART] */ |
| 160 | /* ^data ^tail ^cur ^size */ |
| 161 | len_total += shbuf_size; |
| 162 | } |
| 163 | copy = xmalloc(len_total + 1); |
| 164 | if (len_first_part < 0) { |
| 165 | /* message wraps (see above) */ |
| 166 | len_first_part = shbuf_size - cur; |
| 167 | memcpy(copy + len_first_part, shbuf_data, shbuf_tail); |
| 168 | } |
| 169 | memcpy(copy, shbuf_data + cur, len_first_part); |
| 170 | copy[len_total] = '\0'; |
| 171 | cur = shbuf_tail; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 172 | #else |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 173 | while (cur != shbuf_tail) { |
| 174 | fputs(shbuf_data + cur, stdout); |
| 175 | cur += strlen(shbuf_data + cur) + 1; |
| 176 | if (cur >= shbuf_size) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 177 | cur = 0; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 180 | unlock: |
| 181 | /* release the lock on the log chain */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 182 | sem_up(log_semid); |
| 183 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 184 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 185 | for (i = 0; i < len_total; i += strlen(copy + i) + 1) { |
| 186 | fputs(copy + i, stdout); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 187 | } |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 188 | free(copy); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 189 | #endif |
Denys Vlasenko | 71f6c1a | 2013-11-26 11:44:27 +0100 | [diff] [blame] | 190 | fflush_all(); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 191 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 192 | |
Denys Vlasenko | 26ad256 | 2013-11-26 12:02:18 +0100 | [diff] [blame] | 193 | /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 194 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 195 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 196 | } |