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 | |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 12 | #include "busybox.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 | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 22 | int32_t size; // size of data written |
| 23 | int32_t head; // start of message list |
| 24 | int32_t tail; // end of message list |
| 25 | char data[1]; // data/messages |
| 26 | } *buf; // shared memory pointer |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 27 | |
| 28 | // Semaphore operation structures |
| 29 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup |
| 30 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn |
| 31 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 33 | static void error_exit(const char *str) ATTRIBUTE_NORETURN; |
| 34 | static void error_exit(const char *str) |
| 35 | { |
| 36 | //release all acquired resources |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 37 | shmdt(buf); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 38 | bb_perror_msg_and_die(str); |
| 39 | } |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * sem_up - up()'s a semaphore. |
| 43 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 44 | static void sem_up(int semid) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 46 | if (semop(semid, SMrup, 1) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 47 | error_exit("semop[SMrup]"); |
| 48 | } |
| 49 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 50 | static void interrupted(int sig ATTRIBUTE_UNUSED) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 51 | { |
| 52 | signal(SIGINT, SIG_IGN); |
| 53 | shmdt(buf); |
| 54 | exit(0); |
| 55 | } |
| 56 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 57 | int logread_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 58 | int logread_main(int argc, char **argv) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 59 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 60 | int cur; |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 61 | int log_semid; /* ipc semaphore id */ |
| 62 | int log_shmid; /* ipc shared memory id */ |
| 63 | smallint follow = getopt32(argc, argv, "f"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 65 | log_shmid = shmget(KEY_ID, 0, 0); |
| 66 | if (log_shmid == -1) |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 67 | bb_perror_msg_and_die("can't find syslogd buffer"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 68 | |
| 69 | // Attach shared memory to our char* |
| 70 | buf = shmat(log_shmid, NULL, SHM_RDONLY); |
| 71 | if (buf == NULL) |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 72 | bb_perror_msg_and_die("can't access syslogd buffer"); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 73 | |
| 74 | log_semid = semget(KEY_ID, 0, 0); |
| 75 | if (log_semid == -1) |
| 76 | error_exit("can't get access to semaphores for syslogd buffer"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 78 | // attempt to redefine ^C signal |
| 79 | signal(SIGINT, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 80 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 81 | // Suppose atomic memory move |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 82 | cur = follow ? buf->tail : buf->head; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 83 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 84 | do { |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 85 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 86 | char *buf_data; |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 87 | int log_len, j; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 88 | #endif |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 89 | if (semop(log_semid, SMrdn, 2) == -1) |
| 90 | error_exit("semop[SMrdn]"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 92 | if (DEBUG) |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 93 | printf("head:%i cur:%d tail:%i size:%i\n", |
| 94 | buf->head, cur, buf->tail, buf->size); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 95 | |
| 96 | if (buf->head == buf->tail || cur == buf->tail) { |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 97 | if (follow) { |
| 98 | sem_up(log_semid); |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 99 | fflush(stdout); |
| 100 | sleep(1); /* TODO: replace me with a sleep_on */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 101 | continue; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 102 | } |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 103 | puts("<empty syslog>"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 104 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 105 | |
| 106 | // Read Memory |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 108 | log_len = buf->tail - cur; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 109 | if (log_len < 0) |
| 110 | log_len += buf->size; |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 111 | buf_data = xmalloc(log_len); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 113 | if (buf->tail >= cur) |
| 114 | j = log_len; |
| 115 | else |
| 116 | j = buf->size - cur; |
| 117 | memcpy(buf_data, buf->data + cur, j); |
| 118 | |
| 119 | if (buf->tail < cur) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 120 | memcpy(buf_data + buf->size - cur, buf->data, buf->tail); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 121 | cur = buf->tail; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 122 | #else |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 123 | while (cur != buf->tail) { |
| 124 | fputs(buf->data + cur, stdout); |
| 125 | cur += strlen(buf->data + cur) + 1; |
| 126 | if (cur >= buf->size) |
| 127 | cur = 0; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 128 | } |
| 129 | #endif |
| 130 | // release the lock on the log chain |
| 131 | sem_up(log_semid); |
| 132 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 133 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 134 | for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { |
| 135 | fputs(buf_data + j, stdout); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 136 | } |
| 137 | free(buf_data); |
| 138 | #endif |
| 139 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 141 | shmdt(buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 143 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 144 | } |