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 | |
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 */ |
| 62 | smallint follow = getopt32(argc, 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 | |
| 68 | // Attach shared memory to our char* |
| 69 | buf = shmat(log_shmid, NULL, SHM_RDONLY); |
| 70 | if (buf == 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 | // attempt to redefine ^C signal |
| 78 | signal(SIGINT, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 80 | // Suppose atomic memory move |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 81 | cur = follow ? buf->tail : buf->head; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 83 | do { |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 84 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 85 | char *buf_data; |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 86 | int log_len, j; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 87 | #endif |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 88 | if (semop(log_semid, SMrdn, 2) == -1) |
| 89 | error_exit("semop[SMrdn]"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 91 | if (DEBUG) |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 92 | printf("head:%i cur:%d tail:%i size:%i\n", |
| 93 | buf->head, cur, buf->tail, buf->size); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 94 | |
| 95 | if (buf->head == buf->tail || cur == buf->tail) { |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 96 | if (follow) { |
| 97 | sem_up(log_semid); |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 98 | fflush(stdout); |
| 99 | sleep(1); /* TODO: replace me with a sleep_on */ |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 100 | continue; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 101 | } |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 102 | puts("<empty syslog>"); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | |
| 105 | // Read Memory |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 106 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 107 | log_len = buf->tail - cur; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 108 | if (log_len < 0) |
| 109 | log_len += buf->size; |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 110 | buf_data = xmalloc(log_len); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 112 | if (buf->tail >= cur) |
| 113 | j = log_len; |
| 114 | else |
| 115 | j = buf->size - cur; |
| 116 | memcpy(buf_data, buf->data + cur, j); |
| 117 | |
| 118 | if (buf->tail < cur) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 119 | memcpy(buf_data + buf->size - cur, buf->data, buf->tail); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 120 | cur = buf->tail; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 121 | #else |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 122 | while (cur != buf->tail) { |
| 123 | fputs(buf->data + cur, stdout); |
| 124 | cur += strlen(buf->data + cur) + 1; |
| 125 | if (cur >= buf->size) |
| 126 | cur = 0; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 127 | } |
| 128 | #endif |
| 129 | // release the lock on the log chain |
| 130 | sem_up(log_semid); |
| 131 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 132 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 133 | for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { |
| 134 | fputs(buf_data + j, stdout); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 135 | } |
| 136 | free(buf_data); |
| 137 | #endif |
| 138 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 140 | shmdt(buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 141 | |
Denis Vlasenko | 085231f | 2007-01-10 22:35:54 +0000 | [diff] [blame] | 142 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 143 | } |