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 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 | * 02111-1307 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/ipc.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/sem.h> |
| 33 | #include <sys/shm.h> |
| 34 | #include <signal.h> |
| 35 | #include <setjmp.h> |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 36 | #include <unistd.h> |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
| 38 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 39 | static const long KEY_ID = 0x414e4547; /*"GENA"*/ |
| 40 | |
| 41 | static struct shbuf_ds { |
| 42 | int size; // size of data written |
| 43 | int head; // start of message list |
| 44 | int tail; // end of message list |
| 45 | char data[1]; // data/messages |
| 46 | } *buf = NULL; // shared memory pointer |
| 47 | |
| 48 | |
| 49 | // Semaphore operation structures |
| 50 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup |
| 51 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn |
| 52 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 53 | static int log_shmid = -1; // ipc shared memory id |
| 54 | static int log_semid = -1; // ipc semaphore id |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 55 | static jmp_buf jmp_env; |
| 56 | |
| 57 | static void error_exit(const char *str); |
| 58 | static void interrupted(int sig); |
| 59 | |
| 60 | /* |
| 61 | * sem_up - up()'s a semaphore. |
| 62 | */ |
| 63 | static inline void sem_up(int semid) |
| 64 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 65 | if ( semop(semid, SMrup, 1) == -1 ) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 66 | error_exit("semop[SMrup]"); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * sem_down - down()'s a semaphore |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 71 | */ |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 72 | static inline void sem_down(int semid) |
| 73 | { |
| 74 | if ( semop(semid, SMrdn, 2) == -1 ) |
| 75 | error_exit("semop[SMrdn]"); |
| 76 | } |
| 77 | |
| 78 | extern int logread_main(int argc, char **argv) |
| 79 | { |
| 80 | int i; |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 81 | int follow=0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 82 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 83 | if (argc == 2 && strcmp(argv[1],"-f")==0) { |
| 84 | follow = 1; |
| 85 | } else { |
| 86 | /* no options, no getopt */ |
| 87 | if (argc > 1) |
| 88 | bb_show_usage(); |
| 89 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 90 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 91 | // handle intrrupt signal |
| 92 | if (setjmp(jmp_env)) goto output_end; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 93 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 94 | // attempt to redefine ^C signal |
| 95 | signal(SIGINT, interrupted); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 96 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 97 | if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 98 | error_exit("Can't find circular buffer"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 99 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 100 | // Attach shared memory to our char* |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 101 | if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 102 | error_exit("Can't get access to circular buffer from syslogd"); |
| 103 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 104 | if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 105 | error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); |
| 106 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 107 | // Suppose atomic memory move |
| 108 | i = follow ? buf->tail : buf->head; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 109 | |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 110 | do { |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 111 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 112 | char *buf_data; |
| 113 | int log_len,j; |
| 114 | #endif |
| 115 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | sem_down(log_semid); |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 117 | |
| 118 | //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); |
| 119 | if (buf->head == buf->tail || i==buf->tail) { |
| 120 | if (follow) { |
| 121 | sem_up(log_semid); |
| 122 | sleep(1); /* TODO: replace me with a sleep_on */ |
| 123 | continue; |
| 124 | } else { |
| 125 | printf("<empty syslog>\n"); |
| 126 | } |
| 127 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 128 | |
| 129 | // Read Memory |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 131 | log_len = buf->tail - i; |
| 132 | if (log_len < 0) |
| 133 | log_len += buf->size; |
| 134 | buf_data = (char *)malloc(log_len); |
| 135 | if (!buf_data) |
| 136 | error_exit("malloc failed"); |
| 137 | |
| 138 | if (buf->tail < i) { |
| 139 | memcpy(buf_data, buf->data+i, buf->size-i); |
| 140 | memcpy(buf_data+buf->size-i, buf->data, buf->tail); |
| 141 | } else { |
| 142 | memcpy(buf_data, buf->data+i, buf->tail-i); |
| 143 | } |
| 144 | i = buf->tail; |
| 145 | |
| 146 | #else |
| 147 | while ( i != buf->tail) { |
| 148 | printf("%s", buf->data+i); |
| 149 | i+= strlen(buf->data+i) + 1; |
| 150 | if (i >= buf->size ) |
| 151 | i=0; |
| 152 | } |
| 153 | #endif |
| 154 | // release the lock on the log chain |
| 155 | sem_up(log_semid); |
| 156 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 157 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING |
Glenn L McGrath | f7dd10f | 2003-09-26 01:03:16 +0000 | [diff] [blame] | 158 | for (j=0; j < log_len; j+=strlen(buf_data+j)+1) { |
| 159 | printf("%s", buf_data+j); |
| 160 | if (follow) |
| 161 | fflush(stdout); |
| 162 | } |
| 163 | free(buf_data); |
| 164 | #endif |
| 165 | } while (follow); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 166 | |
| 167 | output_end: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 168 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 169 | shmdt(buf); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 170 | |
| 171 | return EXIT_SUCCESS; |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void interrupted(int sig){ |
| 175 | signal(SIGINT, SIG_IGN); |
| 176 | longjmp(jmp_env, 1); |
| 177 | } |
| 178 | |
| 179 | static void error_exit(const char *str){ |
| 180 | perror(str); |
| 181 | //release all acquired resources |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 182 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 183 | shmdt(buf); |
| 184 | |
| 185 | exit(1); |
| 186 | } |