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