blob: 2692efbee98eb7a2d5f031fee07f4641d704e94d [file] [log] [blame]
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * circular buffer syslog implementation for busybox
4 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00005 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00006 *
Glenn L McGrath6ed77592002-12-12 10:54:48 +00007 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +00008 *
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 Whitleyb0c2b7d2001-03-15 19:05:59 +000038static const long KEY_ID = 0x414e4547; /*"GENA"*/
39
40static 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
49static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
50static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
51
Eric Andersen8ffaf8d2001-03-23 17:04:47 +000052static int log_shmid = -1; // ipc shared memory id
53static int log_semid = -1; // ipc semaphore id
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000054static jmp_buf jmp_env;
55
56static void error_exit(const char *str);
57static void interrupted(int sig);
58
59/*
60 * sem_up - up()'s a semaphore.
61 */
62static 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 */
71static inline void sem_down(int semid)
72{
73 if ( semop(semid, SMrdn, 2) == -1 )
74 error_exit("semop[SMrdn]");
75}
76
77extern int logread_main(int argc, char **argv)
78{
79 int i;
80
81 /* no options, no getopt */
82 if (argc > 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_show_usage();
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000084
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 Andersen8ffaf8d2001-03-23 17:04:47 +000091 if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000092 error_exit("Can't find circular buffer");
93
94 // Attach shared memory to our char*
Eric Andersen8ffaf8d2001-03-23 17:04:47 +000095 if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000096 error_exit("Can't get access to circular buffer from syslogd");
97
Eric Andersen8ffaf8d2001-03-23 17:04:47 +000098 if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +000099 error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
100
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000101 sem_down(log_semid);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000102 // 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 Andersen8ffaf8d2001-03-23 17:04:47 +0000116 sem_up(log_semid);
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000117
118output_end:
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000119 if (log_shmid != -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000120 shmdt(buf);
121
122 return EXIT_SUCCESS;
123}
124
125static void interrupted(int sig){
126 signal(SIGINT, SIG_IGN);
127 longjmp(jmp_env, 1);
128}
129
130static void error_exit(const char *str){
131 perror(str);
132 //release all acquired resources
Eric Andersen8ffaf8d2001-03-23 17:04:47 +0000133 if (log_shmid != -1)
Mark Whitleyb0c2b7d2001-03-15 19:05:59 +0000134 shmdt(buf);
135
136 exit(1);
137}