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 | |
| 12 | |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/ipc.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/sem.h> |
| 20 | #include <sys/shm.h> |
| 21 | #include <signal.h> |
| 22 | #include <setjmp.h> |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 24 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 25 | static const long KEY_ID = 0x414e4547; /*"GENA"*/ |
| 26 | |
| 27 | static struct shbuf_ds { |
| 28 | int size; // size of data written |
| 29 | int head; // start of message list |
| 30 | int tail; // end of message list |
| 31 | char data[1]; // data/messages |
| 32 | } *buf = NULL; // shared memory pointer |
| 33 | |
| 34 | |
| 35 | // Semaphore operation structures |
| 36 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup |
| 37 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn |
| 38 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 39 | static int log_shmid = -1; // ipc shared memory id |
| 40 | static int log_semid = -1; // ipc semaphore id |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 41 | static jmp_buf jmp_env; |
| 42 | |
| 43 | static void error_exit(const char *str); |
| 44 | static void interrupted(int sig); |
| 45 | |
| 46 | /* |
| 47 | * sem_up - up()'s a semaphore. |
| 48 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 49 | static void sem_up(int semid) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 50 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 51 | if ( semop(semid, SMrup, 1) == -1 ) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 52 | error_exit("semop[SMrup]"); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * sem_down - down()'s a semaphore |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 57 | */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 58 | static void sem_down(int semid) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 59 | { |
| 60 | if ( semop(semid, SMrdn, 2) == -1 ) |
| 61 | error_exit("semop[SMrdn]"); |
| 62 | } |
| 63 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 64 | int logread_main(int argc, char **argv) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 65 | { |
| 66 | int i; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 67 | int follow=0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 68 | |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 69 | if (argc == 2 && argv[1][0]=='-' && argv[1][1]=='f') { |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 70 | follow = 1; |
| 71 | } else { |
| 72 | /* no options, no getopt */ |
| 73 | if (argc > 1) |
| 74 | bb_show_usage(); |
| 75 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 76 | |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 77 | // handle interrupt signal |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 78 | if (setjmp(jmp_env)) goto output_end; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 79 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 80 | // attempt to redefine ^C signal |
| 81 | signal(SIGINT, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 82 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 83 | if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 84 | error_exit("Can't find circular buffer"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 86 | // Attach shared memory to our char* |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 87 | if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 88 | error_exit("Can't get access to circular buffer from syslogd"); |
| 89 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 90 | if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 91 | error_exit("Can't get access to semaphore(s) for circular buffer from syslogd"); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 92 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 93 | // Suppose atomic memory move |
| 94 | i = follow ? buf->tail : buf->head; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 95 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 96 | do { |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 97 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 98 | char *buf_data; |
| 99 | int log_len,j; |
| 100 | #endif |
| 101 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | sem_down(log_semid); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 103 | |
| 104 | //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); |
| 105 | if (buf->head == buf->tail || i==buf->tail) { |
| 106 | if (follow) { |
| 107 | sem_up(log_semid); |
| 108 | sleep(1); /* TODO: replace me with a sleep_on */ |
| 109 | continue; |
| 110 | } else { |
| 111 | printf("<empty syslog>\n"); |
| 112 | } |
| 113 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 114 | |
| 115 | // Read Memory |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 116 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 117 | log_len = buf->tail - i; |
| 118 | if (log_len < 0) |
| 119 | log_len += buf->size; |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 120 | buf_data = (char *)xmalloc(log_len); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 121 | |
| 122 | if (buf->tail < i) { |
| 123 | memcpy(buf_data, buf->data+i, buf->size-i); |
| 124 | memcpy(buf_data+buf->size-i, buf->data, buf->tail); |
| 125 | } else { |
| 126 | memcpy(buf_data, buf->data+i, buf->tail-i); |
| 127 | } |
| 128 | i = buf->tail; |
| 129 | |
| 130 | #else |
| 131 | while ( i != buf->tail) { |
| 132 | printf("%s", buf->data+i); |
| 133 | i+= strlen(buf->data+i) + 1; |
| 134 | if (i >= buf->size ) |
| 135 | i=0; |
| 136 | } |
| 137 | #endif |
| 138 | // release the lock on the log chain |
| 139 | sem_up(log_semid); |
| 140 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 141 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 142 | for (j=0; j < log_len; j+=strlen(buf_data+j)+1) { |
| 143 | printf("%s", buf_data+j); |
| 144 | if (follow) |
| 145 | fflush(stdout); |
| 146 | } |
| 147 | free(buf_data); |
| 148 | #endif |
Rob Landley | 10fb2b7 | 2005-12-12 07:04:59 +0000 | [diff] [blame] | 149 | fflush(stdout); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 150 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 151 | |
| 152 | output_end: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 154 | shmdt(buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 155 | |
| 156 | return EXIT_SUCCESS; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void interrupted(int sig){ |
| 160 | signal(SIGINT, SIG_IGN); |
| 161 | longjmp(jmp_env, 1); |
| 162 | } |
| 163 | |
| 164 | static void error_exit(const char *str){ |
Bernhard Reutner-Fischer | d04e9b8 | 2006-06-01 14:36:14 +0000 | [diff] [blame] | 165 | bb_perror_msg(str); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 166 | //release all acquired resources |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 167 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 168 | shmdt(buf); |
| 169 | |
| 170 | exit(1); |
| 171 | } |