mirror of
https://github.com/sheumann/hush.git
synced 2024-12-22 14:30:31 +00:00
new libbb func: xmalloc_realpath (+ use it where makes sense)
syslogd, logread: add debugging code (disabled) syslogs: drastically smaller bss; fix "-C n" behaviour
This commit is contained in:
parent
ceab8700df
commit
a9b60e93ee
@ -10,8 +10,6 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include "busybox.h"
|
||||
|
||||
int realpath_main(int argc, char **argv)
|
||||
|
@ -238,6 +238,7 @@ extern DIR *warn_opendir(const char *path);
|
||||
|
||||
char *xgetcwd(char *cwd);
|
||||
char *xreadlink(const char *path);
|
||||
char *xmalloc_realpath(const char *path);
|
||||
extern void xstat(char *filename, struct stat *buf);
|
||||
extern pid_t spawn(char **argv);
|
||||
extern pid_t xspawn(char **argv);
|
||||
|
@ -3022,6 +3022,7 @@
|
||||
"\n -L Log locally and via network logging (default is network only)") \
|
||||
USE_FEATURE_IPC_SYSLOG( \
|
||||
"\n -C[size(KiB)] Log to a circular buffer (read the buffer using logread)")
|
||||
/* NB: -Csize shouldn't have space (because size is optional) */
|
||||
#define syslogd_example_usage \
|
||||
"$ syslogd -R masterlog:514\n" \
|
||||
"$ syslogd -R 192.168.1.1:601\n"
|
||||
|
@ -4,16 +4,13 @@
|
||||
* Returns a NULL on failure...
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "libbb.h"
|
||||
|
||||
/*
|
||||
* NOTE: This function returns a malloced char* that you will have to free
|
||||
* yourself. You have been warned.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include "libbb.h"
|
||||
|
||||
char *xreadlink(const char *path)
|
||||
{
|
||||
enum { GROWBY = 80 }; /* how large we will grow strings by */
|
||||
@ -36,3 +33,16 @@ char *xreadlink(const char *path)
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *xmalloc_realpath(const char *path)
|
||||
{
|
||||
#ifdef __GLIBC__
|
||||
/* glibc provides a non-standard extension */
|
||||
return realpath(path, NULL);
|
||||
#else
|
||||
char buf[PATH_MAX+1];
|
||||
|
||||
/* on error returns NULL (xstrdup(NULL) ==NULL) */
|
||||
return xstrdup(realpath(path, buf));
|
||||
#endif
|
||||
}
|
||||
|
@ -9,27 +9,24 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
||||
*/
|
||||
|
||||
|
||||
#include "busybox.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/types.h>
|
||||
//#include <sys/types.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
#include <signal.h>
|
||||
#include <setjmp.h>
|
||||
#include <unistd.h>
|
||||
//#include <signal.h>
|
||||
//#include <setjmp.h>
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
static const long 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
|
||||
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 = NULL; // shared memory pointer
|
||||
} *buf; // shared memory pointer
|
||||
|
||||
|
||||
// Semaphore operation structures
|
||||
@ -38,10 +35,15 @@ 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
|
||||
if (log_shmid != -1)
|
||||
shmdt(buf);
|
||||
bb_perror_msg_and_die(str);
|
||||
}
|
||||
|
||||
/*
|
||||
* sem_up - up()'s a semaphore.
|
||||
@ -61,48 +63,55 @@ static void sem_down(int semid)
|
||||
error_exit("semop[SMrdn]");
|
||||
}
|
||||
|
||||
static void interrupted(int sig)
|
||||
{
|
||||
signal(SIGINT, SIG_IGN);
|
||||
shmdt(buf);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int logread_main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int follow=0;
|
||||
int cur;
|
||||
int follow = 1;
|
||||
|
||||
if (argc == 2 && argv[1][0]=='-' && argv[1][1]=='f') {
|
||||
follow = 1;
|
||||
} else {
|
||||
if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) {
|
||||
follow = 0;
|
||||
/* no options, no getopt */
|
||||
if (argc > 1)
|
||||
bb_show_usage();
|
||||
}
|
||||
|
||||
// handle interrupt signal
|
||||
if (setjmp(jmp_env)) goto output_end;
|
||||
log_shmid = shmget(KEY_ID, 0, 0);
|
||||
if (log_shmid == -1)
|
||||
error_exit("can't find circular buffer");
|
||||
|
||||
// Attach shared memory to our char*
|
||||
buf = shmat(log_shmid, NULL, SHM_RDONLY);
|
||||
if (buf == NULL)
|
||||
error_exit("can't get access to syslogd buffer");
|
||||
|
||||
log_semid = semget(KEY_ID, 0, 0);
|
||||
if (log_semid == -1)
|
||||
error_exit("can't get access to semaphores for syslogd buffer");
|
||||
|
||||
// attempt to redefine ^C signal
|
||||
signal(SIGINT, interrupted);
|
||||
|
||||
if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
|
||||
error_exit("Can't find circular 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 semaphore(s) for circular buffer from syslogd");
|
||||
|
||||
// Suppose atomic memory move
|
||||
i = follow ? buf->tail : buf->head;
|
||||
cur = follow ? buf->tail : buf->head;
|
||||
|
||||
do {
|
||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||
char *buf_data;
|
||||
int log_len, j;
|
||||
#endif
|
||||
|
||||
sem_down(log_semid);
|
||||
|
||||
//printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
|
||||
if (buf->head == buf->tail || i==buf->tail) {
|
||||
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);
|
||||
sleep(1); /* TODO: replace me with a sleep_on */
|
||||
@ -114,25 +123,24 @@ int logread_main(int argc, char **argv)
|
||||
|
||||
// Read Memory
|
||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||
log_len = buf->tail - i;
|
||||
log_len = buf->tail - cur;
|
||||
if (log_len < 0)
|
||||
log_len += buf->size;
|
||||
buf_data = xmalloc(log_len);
|
||||
|
||||
if (buf->tail < i) {
|
||||
memcpy(buf_data, buf->data+i, buf->size-i);
|
||||
memcpy(buf_data+buf->size-i, buf->data, buf->tail);
|
||||
if (buf->tail >= cur) {
|
||||
memcpy(buf_data, buf->data + cur, log_len);
|
||||
} else {
|
||||
memcpy(buf_data, buf->data+i, buf->tail-i);
|
||||
memcpy(buf_data, buf->data + cur, buf->size - cur);
|
||||
memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
|
||||
}
|
||||
i = buf->tail;
|
||||
|
||||
cur = buf->tail;
|
||||
#else
|
||||
while ( i != buf->tail) {
|
||||
printf("%s", buf->data+i);
|
||||
i+= strlen(buf->data+i) + 1;
|
||||
if (i >= buf->size )
|
||||
i=0;
|
||||
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
|
||||
@ -140,32 +148,14 @@ int logread_main(int argc, char **argv)
|
||||
|
||||
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
|
||||
for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
|
||||
printf("%s", buf_data+j);
|
||||
if (follow)
|
||||
fflush(stdout);
|
||||
fputs(buf_data + j, stdout);
|
||||
}
|
||||
free(buf_data);
|
||||
#endif
|
||||
fflush(stdout);
|
||||
} while (follow);
|
||||
|
||||
output_end:
|
||||
if (log_shmid != -1)
|
||||
shmdt(buf);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static void interrupted(int sig){
|
||||
signal(SIGINT, SIG_IGN);
|
||||
longjmp(jmp_env, 1);
|
||||
}
|
||||
|
||||
static void error_exit(const char *str){
|
||||
bb_perror_msg(str);
|
||||
//release all acquired resources
|
||||
if (log_shmid != -1)
|
||||
shmdt(buf);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
@ -22,19 +22,14 @@
|
||||
#include <sys/syslog.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
/* Path to the unix socket */
|
||||
static char dev_log_name[MAXPATHLEN];
|
||||
static char *dev_log_name;
|
||||
|
||||
/* Path for the file where all log messages are written */
|
||||
static const char *logFilePath = "/var/log/messages";
|
||||
|
||||
#if ENABLE_FEATURE_ROTATE_LOGFILE
|
||||
/* max size of message file before being rotated */
|
||||
static int logFileSize = 200 * 1024;
|
||||
/* number of rotated message files */
|
||||
static int logFileRotate = 1;
|
||||
#endif
|
||||
|
||||
/* interval between marks in seconds */
|
||||
static int markInterval = 20 * 60;
|
||||
|
||||
@ -44,6 +39,13 @@ static int logLevel = 8;
|
||||
/* localhost's name */
|
||||
static char localHostName[64];
|
||||
|
||||
#if ENABLE_FEATURE_ROTATE_LOGFILE
|
||||
/* max size of message file before being rotated */
|
||||
static int logFileSize = 200 * 1024;
|
||||
/* number of rotated message files */
|
||||
static int logFileRotate = 1;
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_REMOTE_LOG
|
||||
#include <netinet/in.h>
|
||||
/* udp socket for logging to remote host */
|
||||
@ -142,6 +144,9 @@ static void ipcsyslog_cleanup(void)
|
||||
|
||||
static void ipcsyslog_init(void)
|
||||
{
|
||||
if (DEBUG)
|
||||
printf("shmget(%lx, %d,...)\n", KEY_ID, shm_size);
|
||||
|
||||
shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023);
|
||||
if (shmid == -1) {
|
||||
bb_perror_msg_and_die("shmget");
|
||||
@ -236,6 +241,8 @@ static void log_to_shmem(const char *msg, int len)
|
||||
if (semop(s_semid, SMwup, 1) == -1) {
|
||||
bb_perror_msg_and_die("SMwup");
|
||||
}
|
||||
if (DEBUG)
|
||||
printf("head:%d tail:%d\n", shbuf->head, shbuf->tail);
|
||||
}
|
||||
#else
|
||||
void ipcsyslog_cleanup(void);
|
||||
@ -450,10 +457,12 @@ static void do_syslogd(void)
|
||||
signal(SIGALRM, do_mark);
|
||||
alarm(markInterval);
|
||||
|
||||
dev_log_name = xmalloc_realpath(_PATH_LOG);
|
||||
if (!dev_log_name)
|
||||
dev_log_name = _PATH_LOG;
|
||||
|
||||
/* Unlink old /dev/log (or object it points to) */
|
||||
if (realpath(_PATH_LOG, dev_log_name) != NULL) {
|
||||
unlink(dev_log_name);
|
||||
}
|
||||
|
||||
memset(&sunx, 0, sizeof(sunx));
|
||||
sunx.sun_family = AF_UNIX;
|
||||
@ -520,6 +529,7 @@ int syslogd_main(int argc, char **argv)
|
||||
char *p;
|
||||
|
||||
/* do normal option parsing */
|
||||
opt_complementary = "=0"; /* no non-option params */
|
||||
getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
|
||||
if (option_mask32 & OPT_mark) // -m
|
||||
markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
|
||||
|
Loading…
Reference in New Issue
Block a user