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