mirror of
https://github.com/sheumann/hush.git
synced 2024-09-12 00:55:11 +00:00
code shrinkage by Bernhard Fischer <rep.nop@aon.at>
This commit is contained in:
parent
1a8bf7f5d3
commit
085231fbba
@ -11,37 +11,30 @@
|
|||||||
|
|
||||||
#include "busybox.h"
|
#include "busybox.h"
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
//#include <sys/types.h>
|
|
||||||
#include <sys/sem.h>
|
#include <sys/sem.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
//#include <signal.h>
|
|
||||||
//#include <setjmp.h>
|
|
||||||
|
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
static const long KEY_ID = 0x414e4547; /* "GENA" */
|
enum { KEY_ID = 0x414e4547 }; /* "GENA" */
|
||||||
|
|
||||||
static struct shbuf_ds {
|
static struct shbuf_ds {
|
||||||
int32_t size; // size of data written
|
int32_t size; // size of data written
|
||||||
int32_t head; // start of message list
|
int32_t head; // start of message list
|
||||||
int32_t tail; // end of message list
|
int32_t tail; // end of message list
|
||||||
char data[1]; // data/messages
|
char data[1]; // data/messages
|
||||||
} *buf; // shared memory pointer
|
} *buf; // shared memory pointer
|
||||||
|
|
||||||
|
|
||||||
// Semaphore operation structures
|
// Semaphore operation structures
|
||||||
static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
|
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 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 void error_exit(const char *str) ATTRIBUTE_NORETURN;
|
static void error_exit(const char *str) ATTRIBUTE_NORETURN;
|
||||||
static void error_exit(const char *str)
|
static void error_exit(const char *str)
|
||||||
{
|
{
|
||||||
//release all acquired resources
|
//release all acquired resources
|
||||||
if (log_shmid != -1)
|
shmdt(buf);
|
||||||
shmdt(buf);
|
|
||||||
bb_perror_msg_and_die(str);
|
bb_perror_msg_and_die(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,16 +47,7 @@ static void sem_up(int semid)
|
|||||||
error_exit("semop[SMrup]");
|
error_exit("semop[SMrup]");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
static void interrupted(int sig ATTRIBUTE_UNUSED)
|
||||||
* sem_down - down()'s a semaphore
|
|
||||||
*/
|
|
||||||
static void sem_down(int semid)
|
|
||||||
{
|
|
||||||
if (semop(semid, SMrdn, 2) == -1)
|
|
||||||
error_exit("semop[SMrdn]");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void interrupted(int sig)
|
|
||||||
{
|
{
|
||||||
signal(SIGINT, SIG_IGN);
|
signal(SIGINT, SIG_IGN);
|
||||||
shmdt(buf);
|
shmdt(buf);
|
||||||
@ -73,23 +57,18 @@ static void interrupted(int sig)
|
|||||||
int logread_main(int argc, char **argv)
|
int logread_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int cur;
|
int cur;
|
||||||
int follow = 1;
|
int log_semid; /* ipc semaphore id */
|
||||||
|
int log_shmid; /* ipc shared memory id */
|
||||||
if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) {
|
smallint follow = getopt32(argc, argv, "f");
|
||||||
follow = 0;
|
|
||||||
/* no options, no getopt */
|
|
||||||
if (argc > 1)
|
|
||||||
bb_show_usage();
|
|
||||||
}
|
|
||||||
|
|
||||||
log_shmid = shmget(KEY_ID, 0, 0);
|
log_shmid = shmget(KEY_ID, 0, 0);
|
||||||
if (log_shmid == -1)
|
if (log_shmid == -1)
|
||||||
error_exit("can't find circular buffer");
|
bb_perror_msg_and_die("can't find syslogd buffer");
|
||||||
|
|
||||||
// Attach shared memory to our char*
|
// Attach shared memory to our char*
|
||||||
buf = shmat(log_shmid, NULL, SHM_RDONLY);
|
buf = shmat(log_shmid, NULL, SHM_RDONLY);
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
error_exit("can't get access to syslogd buffer");
|
bb_perror_msg_and_die("can't access syslogd buffer");
|
||||||
|
|
||||||
log_semid = semget(KEY_ID, 0, 0);
|
log_semid = semget(KEY_ID, 0, 0);
|
||||||
if (log_semid == -1)
|
if (log_semid == -1)
|
||||||
@ -102,38 +81,42 @@ int logread_main(int argc, char **argv)
|
|||||||
cur = follow ? buf->tail : buf->head;
|
cur = follow ? buf->tail : buf->head;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||||
char *buf_data;
|
char *buf_data;
|
||||||
int log_len, j;
|
int log_len, j;
|
||||||
#endif
|
#endif
|
||||||
sem_down(log_semid);
|
if (semop(log_semid, SMrdn, 2) == -1)
|
||||||
|
error_exit("semop[SMrdn]");
|
||||||
|
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
printf("head:%i cur:%d tail:%i size:%i\n", buf->head, cur, buf->tail, buf->size);
|
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 (buf->head == buf->tail || cur == buf->tail) {
|
||||||
if (follow) {
|
if (follow) {
|
||||||
sem_up(log_semid);
|
sem_up(log_semid);
|
||||||
sleep(1); /* TODO: replace me with a sleep_on */
|
fflush(stdout);
|
||||||
|
sleep(1); /* TODO: replace me with a sleep_on */
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
printf("<empty syslog>\n");
|
|
||||||
}
|
}
|
||||||
|
puts("<empty syslog>");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read Memory
|
// Read Memory
|
||||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||||
log_len = buf->tail - cur;
|
log_len = buf->tail - cur;
|
||||||
if (log_len < 0)
|
if (log_len < 0)
|
||||||
log_len += buf->size;
|
log_len += buf->size;
|
||||||
buf_data = xmalloc(log_len);
|
buf_data = xmalloc(log_len);
|
||||||
|
|
||||||
if (buf->tail >= cur) {
|
if (buf->tail >= cur)
|
||||||
memcpy(buf_data, buf->data + cur, log_len);
|
j = log_len;
|
||||||
} else {
|
else
|
||||||
memcpy(buf_data, buf->data + cur, buf->size - cur);
|
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);
|
memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
|
||||||
}
|
|
||||||
cur = buf->tail;
|
cur = buf->tail;
|
||||||
#else
|
#else
|
||||||
while (cur != buf->tail) {
|
while (cur != buf->tail) {
|
||||||
@ -146,16 +129,15 @@ int logread_main(int argc, char **argv)
|
|||||||
// release the lock on the log chain
|
// release the lock on the log chain
|
||||||
sem_up(log_semid);
|
sem_up(log_semid);
|
||||||
|
|
||||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||||
for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
|
for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
|
||||||
fputs(buf_data + j, stdout);
|
fputs(buf_data + j, stdout);
|
||||||
}
|
}
|
||||||
free(buf_data);
|
free(buf_data);
|
||||||
#endif
|
#endif
|
||||||
fflush(stdout);
|
|
||||||
} while (follow);
|
} while (follow);
|
||||||
|
|
||||||
shmdt(buf);
|
shmdt(buf);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user