From: Denis Vlasenko Date: Thu, 4 Jan 2007 17:59:59 +0000 (-0000) Subject: new libbb func: xmalloc_realpath (+ use it where makes sense) X-Git-Tag: 1_4_0~111 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=a9b60e93eeb4d2706ebc95bafb18bd4267a03d6f;p=oweals%2Fbusybox.git new libbb func: xmalloc_realpath (+ use it where makes sense) syslogd, logread: add debugging code (disabled) syslogs: drastically smaller bss; fix "-C n" behaviour --- diff --git a/coreutils/realpath.c b/coreutils/realpath.c index 5f3242fb5..7fac471eb 100644 --- a/coreutils/realpath.c +++ b/coreutils/realpath.c @@ -10,8 +10,6 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include #include "busybox.h" int realpath_main(int argc, char **argv) diff --git a/include/libbb.h b/include/libbb.h index 7ee66dce6..e92e4db1c 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -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); diff --git a/include/usage.h b/include/usage.h index af518de0b..6202c4afd 100644 --- a/include/usage.h +++ b/include/usage.h @@ -3021,7 +3021,8 @@ "\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)") \ 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 \ "$ syslogd -R masterlog:514\n" \ "$ syslogd -R 192.168.1.1:601\n" diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c index 14863d9d5..76f52ca06 100644 --- a/libbb/xreadlink.c +++ b/libbb/xreadlink.c @@ -4,16 +4,13 @@ * Returns a NULL on failure... */ -#include +#include "libbb.h" /* * NOTE: This function returns a malloced char* that you will have to free * yourself. You have been warned. */ -#include -#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 +} diff --git a/sysklogd/logread.c b/sysklogd/logread.c index 745f97629..4427cf37e 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -9,46 +9,48 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ - #include "busybox.h" -#include -#include -#include #include -#include +//#include #include #include -#include -#include -#include +//#include +//#include + +#define DEBUG 0 -static const long KEY_ID = 0x414e4547; /*"GENA"*/ +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 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 int log_shmid = -1; // ipc shared memory id +static int log_semid = -1; // ipc semaphore id -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. */ static void sem_up(int semid) { - if ( semop(semid, SMrup, 1) == -1 ) + if (semop(semid, SMrup, 1) == -1) error_exit("semop[SMrup]"); } @@ -57,52 +59,59 @@ static void sem_up(int semid) */ static void sem_down(int semid) { - if ( semop(semid, SMrdn, 2) == -1 ) + if (semop(semid, SMrdn, 2) == -1) 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; - - // attempt to redefine ^C signal - signal(SIGINT, interrupted); - - if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) - error_exit("Can't find circular buffer"); + log_shmid = shmget(KEY_ID, 0, 0); + if (log_shmid == -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"); + 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"); - if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) - error_exit("Can't get access to semaphore(s) for circular buffer from syslogd"); + // attempt to redefine ^C signal + signal(SIGINT, interrupted); // 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; + 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,58 +123,39 @@ 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 sem_up(log_semid); #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); + for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { + fputs(buf_data + j, stdout); } free(buf_data); #endif fflush(stdout); } while (follow); -output_end: - if (log_shmid != -1) - shmdt(buf); + 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); -} diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 9e4bc63c2..f4644f218 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c @@ -22,19 +22,14 @@ #include #include +#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 /* 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); - } + 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;