X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=sysklogd%2Flogread.c;h=b1758a6959454b56b86e5abfca1f7460f9e22b50;hb=24efe4f15fe45151681b41333f3713357ce8694b;hp=ae8dbae3a5bc9ebc261cf35feffdf25e58187f90;hpb=cad5364599eb5062d59e0c397ed638ddd61a8d5d;p=oweals%2Fbusybox.git diff --git a/sysklogd/logread.c b/sysklogd/logread.c index ae8dbae3a..b1758a695 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -6,139 +6,139 @@ * * Maintainer: Gennady Feldman as of Mar 12, 2001 * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - * 02111-1307 USA - * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ - -#include -#include -#include +#include "busybox.h" #include -#include #include #include -#include -#include -#include "busybox.h" -#if __GNU_LIBRARY__ < 5 -#error Sorry. Looks like you are using libc5. -#error libc5 shm support isnt good enough. -#error Please disable CONFIG_FEATURE_IPC_SYSLOG -#endif +#define DEBUG 0 - -static const long KEY_ID = 0x414e4547; /*"GENA"*/ +enum { KEY_ID = 0x414e4547 }; /* "GENA" */ static struct shbuf_ds { - int size; // size of data written - int head; // start of message list - int tail; // end of message list - char data[1]; // data/messages -} *buf = NULL; // shared memory pointer - + int32_t size; // size of data written + int32_t head; // start of message list + int32_t tail; // end of message list + char data[1]; // data/messages +} *buf; // shared memory pointer // Semaphore operation structures static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn -static int log_shmid = -1; // ipc shared memory id -static int log_semid = -1; // ipc semaphore id -static jmp_buf jmp_env; -static void error_exit(const char *str); -static void interrupted(int sig); +static void error_exit(const char *str) ATTRIBUTE_NORETURN; +static void error_exit(const char *str) +{ + //release all acquired resources + shmdt(buf); + bb_perror_msg_and_die(str); +} /* * sem_up - up()'s a semaphore. */ -static inline void sem_up(int semid) +static void sem_up(int semid) { - if ( semop(semid, SMrup, 1) == -1 ) + if (semop(semid, SMrup, 1) == -1) error_exit("semop[SMrup]"); } -/* - * sem_down - down()'s a semaphore - */ -static inline void sem_down(int semid) +static void interrupted(int sig ATTRIBUTE_UNUSED) { - if ( semop(semid, SMrdn, 2) == -1 ) - error_exit("semop[SMrdn]"); + signal(SIGINT, SIG_IGN); + shmdt(buf); + exit(0); } -extern int logread_main(int argc, char **argv) +int logread_main(int argc, char **argv); +int logread_main(int argc, char **argv) { - int i; - - /* no options, no getopt */ - if (argc > 1) - bb_show_usage(); - - // handle intrrupt signal - if (setjmp(jmp_env)) goto output_end; - - // attempt to redefine ^C signal - signal(SIGINT, interrupted); - - if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) - error_exit("Can't find circular buffer"); - + int cur; + int log_semid; /* ipc semaphore id */ + int log_shmid; /* ipc shared memory id */ + smallint follow = getopt32(argc, argv, "f"); + + log_shmid = shmget(KEY_ID, 0, 0); + if (log_shmid == -1) + bb_perror_msg_and_die("can't find syslogd buffer"); + // Attach shared memory to our char* - if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) - error_exit("Can't get access to circular buffer from syslogd"); - - if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) - error_exit("Can't get access to semaphone(s) for circular buffer from syslogd"); - - sem_down(log_semid); - // Read Memory - i=buf->head; - - //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); - if (buf->head == buf->tail) { - printf("\n"); - } - - while ( i != buf->tail) { - printf("%s", buf->data+i); - i+= strlen(buf->data+i) + 1; - if (i >= buf->size ) - i=0; - } - sem_up(log_semid); - -output_end: - if (log_shmid != -1) - shmdt(buf); - - return EXIT_SUCCESS; -} + buf = shmat(log_shmid, NULL, SHM_RDONLY); + if (buf == NULL) + bb_perror_msg_and_die("can't access syslogd buffer"); -static void interrupted(int sig){ - signal(SIGINT, SIG_IGN); - longjmp(jmp_env, 1); -} + log_semid = semget(KEY_ID, 0, 0); + if (log_semid == -1) + error_exit("can't get access to semaphores for syslogd buffer"); -static void error_exit(const char *str){ - perror(str); - //release all acquired resources - if (log_shmid != -1) - shmdt(buf); + // attempt to redefine ^C signal + signal(SIGINT, interrupted); - exit(1); + // Suppose atomic memory move + cur = follow ? buf->tail : buf->head; + + do { +#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING + char *buf_data; + int log_len, j; +#endif + if (semop(log_semid, SMrdn, 2) == -1) + error_exit("semop[SMrdn]"); + + if (DEBUG) + printf("head:%i cur:%d tail:%i size:%i\n", + buf->head, cur, buf->tail, buf->size); + + if (buf->head == buf->tail || cur == buf->tail) { + if (follow) { + sem_up(log_semid); + fflush(stdout); + sleep(1); /* TODO: replace me with a sleep_on */ + continue; + } + puts(""); + } + + // Read Memory +#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING + log_len = buf->tail - cur; + if (log_len < 0) + log_len += buf->size; + buf_data = xmalloc(log_len); + + if (buf->tail >= cur) + j = log_len; + else + j = buf->size - cur; + memcpy(buf_data, buf->data + cur, j); + + if (buf->tail < cur) + memcpy(buf_data + buf->size - cur, buf->data, buf->tail); + cur = buf->tail; +#else + while (cur != buf->tail) { + fputs(buf->data + cur, stdout); + cur += strlen(buf->data + cur) + 1; + if (cur >= buf->size) + cur = 0; + } +#endif + // release the lock on the log chain + sem_up(log_semid); + +#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING + for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { + fputs(buf_data + j, stdout); + } + free(buf_data); +#endif + } while (follow); + + shmdt(buf); + + fflush_stdout_and_exit(EXIT_SUCCESS); }