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