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> |
| 36 | #include "busybox.h" |
| 37 | |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 38 | static const long KEY_ID = 0x414e4547; /*"GENA"*/ |
| 39 | |
| 40 | static struct shbuf_ds { |
| 41 | int size; // size of data written |
| 42 | int head; // start of message list |
| 43 | int tail; // end of message list |
| 44 | char data[1]; // data/messages |
| 45 | } *buf = NULL; // shared memory pointer |
| 46 | |
| 47 | |
| 48 | // Semaphore operation structures |
| 49 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup |
| 50 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn |
| 51 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 52 | static int log_shmid = -1; // ipc shared memory id |
| 53 | static int log_semid = -1; // ipc semaphore id |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 54 | static jmp_buf jmp_env; |
| 55 | |
| 56 | static void error_exit(const char *str); |
| 57 | static void interrupted(int sig); |
| 58 | |
| 59 | /* |
| 60 | * sem_up - up()'s a semaphore. |
| 61 | */ |
| 62 | static inline void sem_up(int semid) |
| 63 | { |
| 64 | if ( semop(semid, SMrup, 1) == -1 ) |
| 65 | error_exit("semop[SMrup]"); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * sem_down - down()'s a semaphore |
| 70 | */ |
| 71 | static inline void sem_down(int semid) |
| 72 | { |
| 73 | if ( semop(semid, SMrdn, 2) == -1 ) |
| 74 | error_exit("semop[SMrdn]"); |
| 75 | } |
| 76 | |
| 77 | extern int logread_main(int argc, char **argv) |
| 78 | { |
| 79 | int i; |
| 80 | |
| 81 | /* no options, no getopt */ |
| 82 | if (argc > 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | bb_show_usage(); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 84 | |
| 85 | // handle intrrupt signal |
| 86 | if (setjmp(jmp_env)) goto output_end; |
| 87 | |
| 88 | // attempt to redefine ^C signal |
| 89 | signal(SIGINT, interrupted); |
| 90 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 91 | if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 92 | error_exit("Can't find circular buffer"); |
| 93 | |
| 94 | // Attach shared memory to our char* |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 95 | if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 96 | error_exit("Can't get access to circular buffer from syslogd"); |
| 97 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 98 | if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 99 | error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); |
| 100 | |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 101 | sem_down(log_semid); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 102 | // Read Memory |
| 103 | i=buf->head; |
| 104 | |
| 105 | //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); |
| 106 | if (buf->head == buf->tail) { |
| 107 | printf("<empty syslog>\n"); |
| 108 | } |
| 109 | |
| 110 | while ( i != buf->tail) { |
| 111 | printf("%s", buf->data+i); |
| 112 | i+= strlen(buf->data+i) + 1; |
| 113 | if (i >= buf->size ) |
| 114 | i=0; |
| 115 | } |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 116 | sem_up(log_semid); |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 117 | |
| 118 | output_end: |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 119 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 120 | shmdt(buf); |
| 121 | |
| 122 | return EXIT_SUCCESS; |
| 123 | } |
| 124 | |
| 125 | static void interrupted(int sig){ |
| 126 | signal(SIGINT, SIG_IGN); |
| 127 | longjmp(jmp_env, 1); |
| 128 | } |
| 129 | |
| 130 | static void error_exit(const char *str){ |
| 131 | perror(str); |
| 132 | //release all acquired resources |
Eric Andersen | 8ffaf8d | 2001-03-23 17:04:47 +0000 | [diff] [blame] | 133 | if (log_shmid != -1) |
Mark Whitley | b0c2b7d | 2001-03-15 19:05:59 +0000 | [diff] [blame] | 134 | shmdt(buf); |
| 135 | |
| 136 | exit(1); |
| 137 | } |