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:
Denis Vlasenko 2007-01-04 17:59:59 +00:00
parent ceab8700df
commit a9b60e93ee
6 changed files with 103 additions and 93 deletions

View File

@ -10,8 +10,6 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include <limits.h>
#include <stdlib.h>
#include "busybox.h" #include "busybox.h"
int realpath_main(int argc, char **argv) int realpath_main(int argc, char **argv)

View File

@ -238,6 +238,7 @@ extern DIR *warn_opendir(const char *path);
char *xgetcwd(char *cwd); char *xgetcwd(char *cwd);
char *xreadlink(const char *path); char *xreadlink(const char *path);
char *xmalloc_realpath(const char *path);
extern void xstat(char *filename, struct stat *buf); extern void xstat(char *filename, struct stat *buf);
extern pid_t spawn(char **argv); extern pid_t spawn(char **argv);
extern pid_t xspawn(char **argv); extern pid_t xspawn(char **argv);

View File

@ -3021,7 +3021,8 @@
"\n -R HOST[:PORT] Log to IP or hostname on PORT (default PORT=514/UDP)" \ "\n -R HOST[:PORT] Log to IP or hostname on PORT (default PORT=514/UDP)" \
"\n -L Log locally and via network logging (default is network only)") \ "\n -L Log locally and via network logging (default is network only)") \
USE_FEATURE_IPC_SYSLOG( \ USE_FEATURE_IPC_SYSLOG( \
"\n -C [size(KiB)] Log to a circular buffer (read the buffer using logread)") "\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 \ #define syslogd_example_usage \
"$ syslogd -R masterlog:514\n" \ "$ syslogd -R masterlog:514\n" \
"$ syslogd -R 192.168.1.1:601\n" "$ syslogd -R 192.168.1.1:601\n"

View File

@ -4,16 +4,13 @@
* Returns a NULL on failure... * Returns a NULL on failure...
*/ */
#include <stdio.h> #include "libbb.h"
/* /*
* NOTE: This function returns a malloced char* that you will have to free * NOTE: This function returns a malloced char* that you will have to free
* yourself. You have been warned. * yourself. You have been warned.
*/ */
#include <unistd.h>
#include "libbb.h"
char *xreadlink(const char *path) char *xreadlink(const char *path)
{ {
enum { GROWBY = 80 }; /* how large we will grow strings by */ enum { GROWBY = 80 }; /* how large we will grow strings by */
@ -36,3 +33,16 @@ char *xreadlink(const char *path)
return buf; 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
}

View File

@ -9,46 +9,48 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/ */
#include "busybox.h" #include "busybox.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/types.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 <signal.h>
#include <setjmp.h> //#include <setjmp.h>
#include <unistd.h>
static const long KEY_ID = 0x414e4547; /*"GENA"*/ #define DEBUG 0
static const long KEY_ID = 0x414e4547; /* "GENA" */
static struct shbuf_ds { static struct shbuf_ds {
int size; // size of data written int32_t size; // size of data written
int head; // start of message list int32_t head; // start of message list
int tail; // end of message list int32_t tail; // end of message list
char data[1]; // data/messages char data[1]; // data/messages
} *buf = NULL; // 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_shmid = -1; // ipc shared memory id
static int log_semid = -1; // ipc semaphore id static int log_semid = -1; // ipc semaphore id
static jmp_buf jmp_env;
static void error_exit(const char *str); static void error_exit(const char *str) ATTRIBUTE_NORETURN;
static void interrupted(int sig); 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. * sem_up - up()'s a semaphore.
*/ */
static 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]"); error_exit("semop[SMrup]");
} }
@ -57,52 +59,59 @@ static void sem_up(int semid)
*/ */
static void sem_down(int semid) static void sem_down(int semid)
{ {
if ( semop(semid, SMrdn, 2) == -1 ) if (semop(semid, SMrdn, 2) == -1)
error_exit("semop[SMrdn]"); 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 logread_main(int argc, char **argv)
{ {
int i; int cur;
int follow=0; int follow = 1;
if (argc == 2 && argv[1][0]=='-' && argv[1][1]=='f') { if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) {
follow = 1; follow = 0;
} else {
/* no options, no getopt */ /* no options, no getopt */
if (argc > 1) if (argc > 1)
bb_show_usage(); bb_show_usage();
} }
// handle interrupt signal log_shmid = shmget(KEY_ID, 0, 0);
if (setjmp(jmp_env)) goto output_end; 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 // attempt to redefine ^C signal
signal(SIGINT, interrupted); 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 // Suppose atomic memory move
i = follow ? buf->tail : buf->head; cur = follow ? buf->tail : buf->head;
do { do {
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
char *buf_data; char *buf_data;
int log_len,j; int log_len, j;
#endif #endif
sem_down(log_semid); sem_down(log_semid);
//printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size); if (DEBUG)
if (buf->head == buf->tail || i==buf->tail) { 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) { if (follow) {
sem_up(log_semid); sem_up(log_semid);
sleep(1); /* TODO: replace me with a sleep_on */ sleep(1); /* TODO: replace me with a sleep_on */
@ -114,58 +123,39 @@ int logread_main(int argc, char **argv)
// Read Memory // Read Memory
#ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
log_len = buf->tail - i; 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 < i) { if (buf->tail >= cur) {
memcpy(buf_data, buf->data+i, buf->size-i); memcpy(buf_data, buf->data + cur, log_len);
memcpy(buf_data+buf->size-i, buf->data, buf->tail);
} else { } 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 #else
while ( i != buf->tail) { while (cur != buf->tail) {
printf("%s", buf->data+i); fputs(buf->data + cur, stdout);
i+= strlen(buf->data+i) + 1; cur += strlen(buf->data + cur) + 1;
if (i >= buf->size ) if (cur >= buf->size)
i=0; cur = 0;
} }
#endif #endif
// 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 #ifdef CONFIG_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) {
printf("%s", buf_data+j); fputs(buf_data + j, stdout);
if (follow)
fflush(stdout);
} }
free(buf_data); free(buf_data);
#endif #endif
fflush(stdout); fflush(stdout);
} while (follow); } while (follow);
output_end: shmdt(buf);
if (log_shmid != -1)
shmdt(buf);
return EXIT_SUCCESS; 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);
}

View File

@ -22,19 +22,14 @@
#include <sys/syslog.h> #include <sys/syslog.h>
#include <sys/uio.h> #include <sys/uio.h>
#define DEBUG 0
/* Path to the unix socket */ /* 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 */ /* Path for the file where all log messages are written */
static const char *logFilePath = "/var/log/messages"; 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 */ /* interval between marks in seconds */
static int markInterval = 20 * 60; static int markInterval = 20 * 60;
@ -44,6 +39,13 @@ static int logLevel = 8;
/* localhost's name */ /* localhost's name */
static char localHostName[64]; 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 #if ENABLE_FEATURE_REMOTE_LOG
#include <netinet/in.h> #include <netinet/in.h>
/* udp socket for logging to remote host */ /* udp socket for logging to remote host */
@ -142,6 +144,9 @@ static void ipcsyslog_cleanup(void)
static void ipcsyslog_init(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); shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023);
if (shmid == -1) { if (shmid == -1) {
bb_perror_msg_and_die("shmget"); 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) { if (semop(s_semid, SMwup, 1) == -1) {
bb_perror_msg_and_die("SMwup"); bb_perror_msg_and_die("SMwup");
} }
if (DEBUG)
printf("head:%d tail:%d\n", shbuf->head, shbuf->tail);
} }
#else #else
void ipcsyslog_cleanup(void); void ipcsyslog_cleanup(void);
@ -450,10 +457,12 @@ static void do_syslogd(void)
signal(SIGALRM, do_mark); signal(SIGALRM, do_mark);
alarm(markInterval); 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) */ /* Unlink old /dev/log (or object it points to) */
if (realpath(_PATH_LOG, dev_log_name) != NULL) { unlink(dev_log_name);
unlink(dev_log_name);
}
memset(&sunx, 0, sizeof(sunx)); memset(&sunx, 0, sizeof(sunx));
sunx.sun_family = AF_UNIX; sunx.sun_family = AF_UNIX;
@ -520,6 +529,7 @@ int syslogd_main(int argc, char **argv)
char *p; char *p;
/* do normal option parsing */ /* do normal option parsing */
opt_complementary = "=0"; /* no non-option params */
getopt32(argc, argv, OPTION_STR, OPTION_PARAM); getopt32(argc, argv, OPTION_STR, OPTION_PARAM);
if (option_mask32 & OPT_mark) // -m if (option_mask32 & OPT_mark) // -m
markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;