Applied patch from Gennady Feldman to split up syslogd.c into syslogd and
authorMark Whitley <markw@lineo.com>
Mon, 12 Mar 2001 22:51:50 +0000 (22:51 -0000)
committerMark Whitley <markw@lineo.com>
Mon, 12 Mar 2001 22:51:50 +0000 (22:51 -0000)
klogd (as it should be).

17 files changed:
Config.h
applets.h
applets/usage.h
busybox.h
include/applets.h
include/busybox.h
include/usage.h
klogd.c [new file with mode: 0644]
logger.c
logread.c [new file with mode: 0644]
sysklogd/klogd.c [new file with mode: 0644]
sysklogd/logger.c
sysklogd/logread.c [new file with mode: 0644]
sysklogd/syslogd.c
syslogd.c
usage.h
utility.c

index 8c6bfe3ec948f16f46ef37e89aad9c1e02273e38..b6f2f8a05cd0a576f862fa433aa0e63eae965fee 100644 (file)
--- a/Config.h
+++ b/Config.h
@@ -52,6 +52,7 @@
 //#define BB_INSMOD
 #define BB_KILL
 #define BB_KILLALL
+#define BB_KLOGD
 //#define BB_LENGTH
 #define BB_LN
 //#define BB_LOADACM
@@ -59,6 +60,7 @@
 //#define BB_LOADKMAP
 #define BB_LOGGER
 //#define BB_LOGNAME
+#define BB_LOGREAD
 #define BB_LS
 #define BB_LSMOD
 //#define BB_MAKEDEVS
index 36817fef73c9dd4b8d438f9e4381b1a1d094cb87..b3fb291d7671857190b1e1ae82d70ca8af148b94 100644 (file)
--- a/applets.h
+++ b/applets.h
 #ifdef BB_KILLALL
        APPLET(killall, kill_main, _BB_DIR_USR_BIN)
 #endif
+#ifdef BB_KLOGD
+       APPLET(klogd, klogd_main, _BB_DIR_SBIN)
+#endif
 #ifdef BB_LENGTH
        APPLET(length, length_main, _BB_DIR_USR_BIN)
 #endif
 #ifdef BB_LOGNAME
        APPLET(logname, logname_main, _BB_DIR_USR_BIN)
 #endif
+#ifdef BB_LOGREAD
+       APPLET(logread, logread_main, _BB_DIR_SBIN)
+#endif
 #ifdef BB_LS
        APPLET(ls, ls_main, _BB_DIR_BIN)
 #endif
index 8ba86d5696cc2bf183cd1c62ecfef809da1867c5..f241d3a043a662524bdc9c85c64143a401c8ee92 100644 (file)
        "Options:\n" \
        "\t-l\tList all signal names and numbers."
 
+#define klogd_trivial_usage \
+       "-n"
+#define klogd_full_usage \
+       "Kernel logger.\n"\
+       "Options:\n"\
+       "\t-n\tRun as a foreground process."
+
 #define length_trivial_usage \
        "STRING"
 #define length_full_usage \
 #define logname_full_usage \
        "Print the name of the current user."
 
+#define logread_trivial_usage \
+        ""
+
+#define logread_full_usage \
+        "Shows the messages from syslogd (using circular buffer)."
+
 #ifdef BB_FEATURE_LS_TIMESTAMPS
   #define USAGE_LS_TIMESTAMPS(a) a
 #else
        "Write all buffered filesystem blocks to disk."
 
 
-#ifdef BB_FEATURE_KLOGD
-  #define USAGE_KLOGD(a) a
-#else
-  #define USAGE_KLOGD(a)
-#endif
 #ifdef BB_FEATURE_REMOTE_LOG
   #define USAGE_REMOTE_LOG(a) a
 #else
 #define syslogd_trivial_usage \
        "[OPTION]..."
 #define syslogd_full_usage \
-       "Linux system and kernel (provides klogd) logging utility.\n" \
-       "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n" \
+       "Linux system and kernel logging utility.\n" \
+       "Note that this version of syslogd ignores /etc/syslog.conf.\n\n" \
        "Options:\n" \
        "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n" \
        "\t-n\t\tRun as a foreground process\n" \
-       USAGE_KLOGD("\t-K\t\tDo not start up the klogd process\n") \
        "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)" \
        USAGE_REMOTE_LOG( \
        "\n\t-R HOST[:PORT]\tLog to IP or hostname on PORT (default PORT=514/UDP)\n" \
index d9362b58b6103df8212277a356577b93542223bc..abf62410f9046f722294a8793c50f0455bbb850e 100644 (file)
--- a/busybox.h
+++ b/busybox.h
@@ -205,6 +205,11 @@ int nfsmount(const char *spec, const char *node, int *flags,
 #define RB_POWER_OFF   0x4321fedc
 #endif
 
+#if defined(BB_KLOGD) || defined(BB_LOGGER)
+void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
+void syslog_msg(int facility, int pri, const char *msg);
+#endif
+
 /* Include our own copy of struct sysinfo to avoid binary compatability
  * problems with Linux 2.4, which changed things.  Grumble, grumble. */
 struct sysinfo {
index 36817fef73c9dd4b8d438f9e4381b1a1d094cb87..b3fb291d7671857190b1e1ae82d70ca8af148b94 100644 (file)
 #ifdef BB_KILLALL
        APPLET(killall, kill_main, _BB_DIR_USR_BIN)
 #endif
+#ifdef BB_KLOGD
+       APPLET(klogd, klogd_main, _BB_DIR_SBIN)
+#endif
 #ifdef BB_LENGTH
        APPLET(length, length_main, _BB_DIR_USR_BIN)
 #endif
 #ifdef BB_LOGNAME
        APPLET(logname, logname_main, _BB_DIR_USR_BIN)
 #endif
+#ifdef BB_LOGREAD
+       APPLET(logread, logread_main, _BB_DIR_SBIN)
+#endif
 #ifdef BB_LS
        APPLET(ls, ls_main, _BB_DIR_BIN)
 #endif
index d9362b58b6103df8212277a356577b93542223bc..abf62410f9046f722294a8793c50f0455bbb850e 100644 (file)
@@ -205,6 +205,11 @@ int nfsmount(const char *spec, const char *node, int *flags,
 #define RB_POWER_OFF   0x4321fedc
 #endif
 
+#if defined(BB_KLOGD) || defined(BB_LOGGER)
+void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
+void syslog_msg(int facility, int pri, const char *msg);
+#endif
+
 /* Include our own copy of struct sysinfo to avoid binary compatability
  * problems with Linux 2.4, which changed things.  Grumble, grumble. */
 struct sysinfo {
index 8ba86d5696cc2bf183cd1c62ecfef809da1867c5..f241d3a043a662524bdc9c85c64143a401c8ee92 100644 (file)
        "Options:\n" \
        "\t-l\tList all signal names and numbers."
 
+#define klogd_trivial_usage \
+       "-n"
+#define klogd_full_usage \
+       "Kernel logger.\n"\
+       "Options:\n"\
+       "\t-n\tRun as a foreground process."
+
 #define length_trivial_usage \
        "STRING"
 #define length_full_usage \
 #define logname_full_usage \
        "Print the name of the current user."
 
+#define logread_trivial_usage \
+        ""
+
+#define logread_full_usage \
+        "Shows the messages from syslogd (using circular buffer)."
+
 #ifdef BB_FEATURE_LS_TIMESTAMPS
   #define USAGE_LS_TIMESTAMPS(a) a
 #else
        "Write all buffered filesystem blocks to disk."
 
 
-#ifdef BB_FEATURE_KLOGD
-  #define USAGE_KLOGD(a) a
-#else
-  #define USAGE_KLOGD(a)
-#endif
 #ifdef BB_FEATURE_REMOTE_LOG
   #define USAGE_REMOTE_LOG(a) a
 #else
 #define syslogd_trivial_usage \
        "[OPTION]..."
 #define syslogd_full_usage \
-       "Linux system and kernel (provides klogd) logging utility.\n" \
-       "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n" \
+       "Linux system and kernel logging utility.\n" \
+       "Note that this version of syslogd ignores /etc/syslog.conf.\n\n" \
        "Options:\n" \
        "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n" \
        "\t-n\t\tRun as a foreground process\n" \
-       USAGE_KLOGD("\t-K\t\tDo not start up the klogd process\n") \
        "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)" \
        USAGE_REMOTE_LOG( \
        "\n\t-R HOST[:PORT]\tLog to IP or hostname on PORT (default PORT=514/UDP)\n" \
diff --git a/klogd.c b/klogd.c
new file mode 100644 (file)
index 0000000..f443836
--- /dev/null
+++ b/klogd.c
@@ -0,0 +1,171 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini klogd implementation for busybox
+ *
+ * Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>.
+ * Changes: Made this a standalone busybox module which uses standalone
+ *                                     syslog() client interface.
+ *
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
+ * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
+ *
+ * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com>
+ *
+ * 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
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h> /* for our signal() handlers */
+#include <string.h> /* strncpy() */
+#include <errno.h>  /* errno and friends */
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/syslog.h>
+
+#if ! defined __GLIBC__ && ! defined __UCLIBC__
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+typedef unsigned int socklen_t;
+
+#ifndef __alpha__
+# define __NR_klogctl __NR_syslog
+static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
+#else                                                  /* __alpha__ */
+#define klogctl syslog
+#endif
+
+#else
+# include <sys/klog.h>
+#endif
+#include "busybox.h"
+
+static void klogd_signal(int sig)
+{
+       klogctl(7, NULL, 0);
+       klogctl(0, 0, 0);
+       //logMessage(0, "Kernel log daemon exiting.");
+       syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
+       exit(TRUE);
+}
+
+static void doKlogd (void) __attribute__ ((noreturn));
+static void doKlogd (void)
+{
+       int priority = LOG_INFO;
+       char log_buffer[4096];
+       int i, n, lastc;
+       char *start;
+
+       /* Set up sig handlers */
+       signal(SIGINT, klogd_signal);
+       signal(SIGKILL, klogd_signal);
+       signal(SIGTERM, klogd_signal);
+       signal(SIGHUP, SIG_IGN);
+
+       /* "Open the log. Currently a NOP." */
+       klogctl(1, NULL, 0);
+
+       syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")");
+
+       while (1) {
+               /* Use kernel syscalls */
+               memset(log_buffer, '\0', sizeof(log_buffer));
+               n = klogctl(2, log_buffer, sizeof(log_buffer));
+               if (n < 0) {
+                       char message[80];
+
+                       if (errno == EINTR)
+                               continue;
+                       snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n", 
+                                                                                               errno, strerror(errno));
+                       syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message);
+                       exit(1);
+               }
+
+               /* klogctl buffer parsing modelled after code in dmesg.c */
+               start=&log_buffer[0];
+               lastc='\0';
+               for (i=0; i<n; i++) {
+                       if (lastc == '\0' && log_buffer[i] == '<') {
+                               priority = 0;
+                               i++;
+                               while (isdigit(log_buffer[i])) {
+                                       priority = priority*10+(log_buffer[i]-'0');
+                                       i++;
+                               }
+                               if (log_buffer[i] == '>') i++;
+                               start = &log_buffer[i];
+                       }
+                       if (log_buffer[i] == '\n') {
+                               log_buffer[i] = '\0';  /* zero terminate this message */
+                               syslog_msg(LOG_DAEMON, LOG_KERN | priority, start);
+                               start = &log_buffer[i+1];
+                               priority = LOG_INFO;
+                       }
+                       lastc = log_buffer[i];
+               }
+       }
+}
+
+static void daemon_init (char **argv, char *dz, void fn (void))
+{
+       setsid(); /* start a new session? */
+       strncpy(argv[0], dz, strlen(argv[0]));
+       fn();
+       exit(0);
+}
+
+extern int klogd_main(int argc, char **argv)
+{
+       /* no options, no getopt */
+       int opt, pid;
+       int doFork = TRUE;
+
+       /* do normal option parsing */
+       while ((opt = getopt(argc, argv, "n")) > 0) {
+               switch (opt) {
+                       case 'n':
+                               doFork = FALSE;
+                               break;
+                       default:
+                               show_usage();
+               }
+       }
+
+       if (doFork == TRUE) {
+               pid = fork();
+               if (pid < 0)
+                       exit(pid);
+               else if (pid == 0) {
+                       daemon_init (argv, "klogd", doKlogd);
+               }
+       } else {
+               doKlogd();
+       }
+       
+       return EXIT_SUCCESS;
+}
+
+/*
+Local Variables
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/
index 1218d8d2e5dd491b8b7028ed47460325d698fcbb..b8aae3d28d9b0bf518c3dc492db3e98954f8dca0 100644 (file)
--- a/logger.c
+++ b/logger.c
@@ -148,9 +148,10 @@ extern int logger_main(int argc, char **argv)
                message[strlen(message)-1] = '\0';
        }
 
-       openlog(name, option, (pri | LOG_FACMASK));
+       /*openlog(name, option, (pri | LOG_FACMASK));
        syslog(pri, "%s", message);
-       closelog();
+       closelog();*/
+       syslog_msg_with_name(name,(pri | LOG_FACMASK),pri,message);
        return EXIT_SUCCESS;
 }
 
diff --git a/logread.c b/logread.c
new file mode 100644 (file)
index 0000000..3bf4c54
--- /dev/null
+++ b/logread.c
@@ -0,0 +1,135 @@
+/* vi: set sw=4 ts=4: */\r
+/*\r
+ * circular buffer syslog implementation for busybox\r
+ *\r
+ * Copyright (C) 2000 by Gennady Feldman <gfeldman@cachier.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA\r
+ * 02111-1307 USA\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <sys/ipc.h>\r
+#include <sys/types.h>\r
+#include <sys/sem.h>\r
+#include <sys/shm.h>\r
+#include <signal.h>\r
+#include <setjmp.h>\r
+#include "busybox.h"\r
+\r
+static const long KEY_ID = 0x414e4547; /*"GENA"*/\r
+\r
+static struct shbuf_ds {\r
+       int size;               // size of data written\r
+       int head;               // start of message list\r
+       int tail;               // end of message list\r
+       char data[1];           // data/messages\r
+} *buf = NULL;                 // shared memory pointer\r
+\r
+\r
+// Semaphore operation structures\r
+static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup\r
+static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn\r
+\r
+static int     shmid = -1;     // ipc shared memory id\r
+static int     semid = -1;     // ipc semaphore id\r
+static jmp_buf jmp_env;\r
+\r
+static void error_exit(const char *str);\r
+static void interrupted(int sig);\r
+\r
+/*\r
+ * sem_up - up()'s a semaphore.\r
+ */\r
+static inline void sem_up(int semid)\r
+{\r
+       if ( semop(semid, SMrup, 1) == -1 ) \r
+               error_exit("semop[SMrup]");\r
+}\r
+\r
+/*\r
+ * sem_down - down()'s a semaphore\r
+ */                            \r
+static inline void sem_down(int semid)\r
+{\r
+       if ( semop(semid, SMrdn, 2) == -1 )\r
+               error_exit("semop[SMrdn]");\r
+}\r
+\r
+extern int logread_main(int argc, char **argv)\r
+{\r
+       int i;\r
+       \r
+       /* no options, no getopt */\r
+       if (argc > 1)\r
+               show_usage();\r
+       \r
+       // handle intrrupt signal\r
+       if (setjmp(jmp_env)) goto output_end;\r
+       \r
+       // attempt to redefine ^C signal\r
+       signal(SIGINT, interrupted);\r
+       \r
+       if ( (shmid = shmget(KEY_ID, 0, 0)) == -1)\r
+               error_exit("Can't find circular buffer");\r
+       \r
+       // Attach shared memory to our char*\r
+       if ( (buf = shmat(shmid, NULL, SHM_RDONLY)) == NULL)\r
+               error_exit("Can't get access to circular buffer from syslogd");\r
+\r
+       if ( (semid = semget(KEY_ID, 0, 0)) == -1)\r
+               error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");\r
+\r
+       sem_down(semid);        \r
+       // Read Memory \r
+       i=buf->head;\r
+\r
+       //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);\r
+       if (buf->head == buf->tail) {\r
+               printf("<empty syslog>\n");\r
+       }\r
+       \r
+       while ( i != buf->tail) {\r
+               printf("%s", buf->data+i);\r
+               i+= strlen(buf->data+i) + 1;\r
+               if (i >= buf->size )\r
+                       i=0;\r
+       }\r
+       sem_up(semid);\r
+\r
+output_end:\r
+       if (shmid != -1) \r
+               shmdt(buf);\r
+               \r
+       return EXIT_SUCCESS;            \r
+}\r
+\r
+static void interrupted(int sig){\r
+       signal(SIGINT, SIG_IGN);\r
+       longjmp(jmp_env, 1);\r
+}\r
+\r
+static void error_exit(const char *str){\r
+       perror(str);\r
+       //release all acquired resources\r
+       if (shmid != -1) \r
+               shmdt(buf);\r
+\r
+       exit(1);\r
+}\r
+\r
diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c
new file mode 100644 (file)
index 0000000..f443836
--- /dev/null
@@ -0,0 +1,171 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini klogd implementation for busybox
+ *
+ * Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>.
+ * Changes: Made this a standalone busybox module which uses standalone
+ *                                     syslog() client interface.
+ *
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
+ * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ *
+ * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
+ *
+ * "circular buffer" Copyright (C) 2000 by Gennady Feldman <gfeldman@mail.com>
+ *
+ * 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
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h> /* for our signal() handlers */
+#include <string.h> /* strncpy() */
+#include <errno.h>  /* errno and friends */
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/syslog.h>
+
+#if ! defined __GLIBC__ && ! defined __UCLIBC__
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+typedef unsigned int socklen_t;
+
+#ifndef __alpha__
+# define __NR_klogctl __NR_syslog
+static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
+#else                                                  /* __alpha__ */
+#define klogctl syslog
+#endif
+
+#else
+# include <sys/klog.h>
+#endif
+#include "busybox.h"
+
+static void klogd_signal(int sig)
+{
+       klogctl(7, NULL, 0);
+       klogctl(0, 0, 0);
+       //logMessage(0, "Kernel log daemon exiting.");
+       syslog_msg(LOG_DAEMON, 0, "Kernel log daemon exiting.");
+       exit(TRUE);
+}
+
+static void doKlogd (void) __attribute__ ((noreturn));
+static void doKlogd (void)
+{
+       int priority = LOG_INFO;
+       char log_buffer[4096];
+       int i, n, lastc;
+       char *start;
+
+       /* Set up sig handlers */
+       signal(SIGINT, klogd_signal);
+       signal(SIGKILL, klogd_signal);
+       signal(SIGTERM, klogd_signal);
+       signal(SIGHUP, SIG_IGN);
+
+       /* "Open the log. Currently a NOP." */
+       klogctl(1, NULL, 0);
+
+       syslog_msg(LOG_DAEMON, 0, "klogd started: BusyBox v" BB_VER " (" BB_BT ")");
+
+       while (1) {
+               /* Use kernel syscalls */
+               memset(log_buffer, '\0', sizeof(log_buffer));
+               n = klogctl(2, log_buffer, sizeof(log_buffer));
+               if (n < 0) {
+                       char message[80];
+
+                       if (errno == EINTR)
+                               continue;
+                       snprintf(message, 79, "klogd: Error return from sys_sycall: %d - %s.\n", 
+                                                                                               errno, strerror(errno));
+                       syslog_msg(LOG_DAEMON, LOG_SYSLOG | LOG_ERR, message);
+                       exit(1);
+               }
+
+               /* klogctl buffer parsing modelled after code in dmesg.c */
+               start=&log_buffer[0];
+               lastc='\0';
+               for (i=0; i<n; i++) {
+                       if (lastc == '\0' && log_buffer[i] == '<') {
+                               priority = 0;
+                               i++;
+                               while (isdigit(log_buffer[i])) {
+                                       priority = priority*10+(log_buffer[i]-'0');
+                                       i++;
+                               }
+                               if (log_buffer[i] == '>') i++;
+                               start = &log_buffer[i];
+                       }
+                       if (log_buffer[i] == '\n') {
+                               log_buffer[i] = '\0';  /* zero terminate this message */
+                               syslog_msg(LOG_DAEMON, LOG_KERN | priority, start);
+                               start = &log_buffer[i+1];
+                               priority = LOG_INFO;
+                       }
+                       lastc = log_buffer[i];
+               }
+       }
+}
+
+static void daemon_init (char **argv, char *dz, void fn (void))
+{
+       setsid(); /* start a new session? */
+       strncpy(argv[0], dz, strlen(argv[0]));
+       fn();
+       exit(0);
+}
+
+extern int klogd_main(int argc, char **argv)
+{
+       /* no options, no getopt */
+       int opt, pid;
+       int doFork = TRUE;
+
+       /* do normal option parsing */
+       while ((opt = getopt(argc, argv, "n")) > 0) {
+               switch (opt) {
+                       case 'n':
+                               doFork = FALSE;
+                               break;
+                       default:
+                               show_usage();
+               }
+       }
+
+       if (doFork == TRUE) {
+               pid = fork();
+               if (pid < 0)
+                       exit(pid);
+               else if (pid == 0) {
+                       daemon_init (argv, "klogd", doKlogd);
+               }
+       } else {
+               doKlogd();
+       }
+       
+       return EXIT_SUCCESS;
+}
+
+/*
+Local Variables
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/
index 1218d8d2e5dd491b8b7028ed47460325d698fcbb..b8aae3d28d9b0bf518c3dc492db3e98954f8dca0 100644 (file)
@@ -148,9 +148,10 @@ extern int logger_main(int argc, char **argv)
                message[strlen(message)-1] = '\0';
        }
 
-       openlog(name, option, (pri | LOG_FACMASK));
+       /*openlog(name, option, (pri | LOG_FACMASK));
        syslog(pri, "%s", message);
-       closelog();
+       closelog();*/
+       syslog_msg_with_name(name,(pri | LOG_FACMASK),pri,message);
        return EXIT_SUCCESS;
 }
 
diff --git a/sysklogd/logread.c b/sysklogd/logread.c
new file mode 100644 (file)
index 0000000..3bf4c54
--- /dev/null
@@ -0,0 +1,135 @@
+/* vi: set sw=4 ts=4: */\r
+/*\r
+ * circular buffer syslog implementation for busybox\r
+ *\r
+ * Copyright (C) 2000 by Gennady Feldman <gfeldman@cachier.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ * General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA\r
+ * 02111-1307 USA\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <sys/ipc.h>\r
+#include <sys/types.h>\r
+#include <sys/sem.h>\r
+#include <sys/shm.h>\r
+#include <signal.h>\r
+#include <setjmp.h>\r
+#include "busybox.h"\r
+\r
+static const long KEY_ID = 0x414e4547; /*"GENA"*/\r
+\r
+static struct shbuf_ds {\r
+       int size;               // size of data written\r
+       int head;               // start of message list\r
+       int tail;               // end of message list\r
+       char data[1];           // data/messages\r
+} *buf = NULL;                 // shared memory pointer\r
+\r
+\r
+// Semaphore operation structures\r
+static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup\r
+static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn\r
+\r
+static int     shmid = -1;     // ipc shared memory id\r
+static int     semid = -1;     // ipc semaphore id\r
+static jmp_buf jmp_env;\r
+\r
+static void error_exit(const char *str);\r
+static void interrupted(int sig);\r
+\r
+/*\r
+ * sem_up - up()'s a semaphore.\r
+ */\r
+static inline void sem_up(int semid)\r
+{\r
+       if ( semop(semid, SMrup, 1) == -1 ) \r
+               error_exit("semop[SMrup]");\r
+}\r
+\r
+/*\r
+ * sem_down - down()'s a semaphore\r
+ */                            \r
+static inline void sem_down(int semid)\r
+{\r
+       if ( semop(semid, SMrdn, 2) == -1 )\r
+               error_exit("semop[SMrdn]");\r
+}\r
+\r
+extern int logread_main(int argc, char **argv)\r
+{\r
+       int i;\r
+       \r
+       /* no options, no getopt */\r
+       if (argc > 1)\r
+               show_usage();\r
+       \r
+       // handle intrrupt signal\r
+       if (setjmp(jmp_env)) goto output_end;\r
+       \r
+       // attempt to redefine ^C signal\r
+       signal(SIGINT, interrupted);\r
+       \r
+       if ( (shmid = shmget(KEY_ID, 0, 0)) == -1)\r
+               error_exit("Can't find circular buffer");\r
+       \r
+       // Attach shared memory to our char*\r
+       if ( (buf = shmat(shmid, NULL, SHM_RDONLY)) == NULL)\r
+               error_exit("Can't get access to circular buffer from syslogd");\r
+\r
+       if ( (semid = semget(KEY_ID, 0, 0)) == -1)\r
+               error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");\r
+\r
+       sem_down(semid);        \r
+       // Read Memory \r
+       i=buf->head;\r
+\r
+       //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);\r
+       if (buf->head == buf->tail) {\r
+               printf("<empty syslog>\n");\r
+       }\r
+       \r
+       while ( i != buf->tail) {\r
+               printf("%s", buf->data+i);\r
+               i+= strlen(buf->data+i) + 1;\r
+               if (i >= buf->size )\r
+                       i=0;\r
+       }\r
+       sem_up(semid);\r
+\r
+output_end:\r
+       if (shmid != -1) \r
+               shmdt(buf);\r
+               \r
+       return EXIT_SUCCESS;            \r
+}\r
+\r
+static void interrupted(int sig){\r
+       signal(SIGINT, SIG_IGN);\r
+       longjmp(jmp_env, 1);\r
+}\r
+\r
+static void error_exit(const char *str){\r
+       perror(str);\r
+       //release all acquired resources\r
+       if (shmid != -1) \r
+               shmdt(buf);\r
+\r
+       exit(1);\r
+}\r
+\r
index 1276201c337f97cbaa7a1f10d806e246eba558c0..a7f982a3512afc8843ca0429f569afda69247445 100644 (file)
@@ -7,6 +7,8 @@
  *
  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
  *
+ * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>
+ *
  * 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
 #include <sys/un.h>
 #include <sys/param.h>
 
-#if ! defined __GLIBC__ && ! defined __UCLIBC__
-#include <sys/syscall.h>
-#include <linux/unistd.h>
-typedef unsigned int socklen_t;
-
-#ifndef __alpha__
-# define __NR_klogctl __NR_syslog
-static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
-#else                                                  /* __alpha__ */
-#define klogctl syslog
-#endif
-
-#else
-# include <sys/klog.h>
-#endif
 #include "busybox.h"
 
-
-
 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
 #define SYSLOG_NAMES
 #include <sys/syslog.h>
@@ -91,6 +76,184 @@ static int doRemoteLog = FALSE;
 static int local_logging = FALSE;
 #endif
 
+/* circular buffer variables/structures */
+#ifdef BB_FEATURE_IPC_SYSLOG
+
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+
+/* our shared key */
+static const long KEY_ID = 0x414e4547; /*"GENA"*/
+
+// Semaphore operation structures
+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
+
+static struct sembuf SMwup[1] = {{1, -1, IPC_NOWAIT}}; // set SMwup
+static struct sembuf SMwdn[3] = {{0, 0}, {1, 0}, {1, +1}}; // set SMwdn
+
+static int     shmid = -1;     // ipc shared memory id
+static int     s_semid = -1;   // ipc semaphore id
+int    data_size = 16000; // data size
+int    shm_size = 16000 + sizeof(*buf); // our buffer size
+static int circular_logging = FALSE;
+
+/*
+ * sem_up - up()'s a semaphore.
+ */
+static inline void sem_up(int semid)
+{
+       if ( semop(semid, SMwup, 1) == -1 )
+               perror_msg_and_die("semop[SMwup]");
+}
+
+/*
+ * sem_down - down()'s a semaphore
+ */
+static inline void sem_down(int semid)
+{
+       if ( semop(semid, SMwdn, 2) == -1 )
+               perror_msg_and_die("semop[SMwdn]");
+}
+
+
+void ipcsyslog_cleanup(void){
+       printf("Exiting Syslogd!\n");
+       if (shmid != -1)
+               shmdt(buf);
+
+       if (shmid != -1)
+               shmctl(shmid, IPC_RMID, NULL);
+       if (s_semid != -1)
+               semctl(s_semid, 0, IPC_RMID, 0);
+}
+
+void ipcsyslog_init(void){
+       if (buf == NULL){
+           if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1)
+                       perror_msg_and_die("shmget");
+
+
+           if ((buf = shmat(shmid, NULL, 0)) == NULL)
+                       perror_msg_and_die("shmat");
+
+
+           buf->size=data_size;
+           buf->head=buf->tail=0;
+
+           // we'll trust the OS to set initial semval to 0 (let's hope)
+           if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1){
+               if (errno == EEXIST){
+                  if ((s_semid = semget(KEY_ID, 2, 0)) == -1)
+                   perror_msg_and_die("semget");
+               }else
+                       perror_msg_and_die("semget");
+           }
+       }else{
+               printf("Buffer already allocated just grab the semaphore?");
+       }
+}
+
+/* write message to buffer */
+void circ_message(const char *msg){
+       int l=strlen(msg)+1; /* count the whole message w/ '\0' included */
+
+       sem_down(s_semid);
+
+       /*
+        * Circular Buffer Algorithm:
+        * --------------------------
+        *
+        * Start-off w/ empty buffer of specific size SHM_SIZ
+        * Start filling it up w/ messages. I use '\0' as separator to break up messages.
+        * This is also very handy since we can do printf on message.
+        *
+        * Once the buffer is full we need to get rid of the first message in buffer and
+        * insert the new message. (Note: if the message being added is >1 message then
+        * we will need to "remove" >1 old message from the buffer). The way this is done
+        * is the following:
+        *      When we reach the end of the buffer we set a mark and start from the beginning.
+        *      Now what about the beginning and end of the buffer? Well we have the "head"
+        *      index/pointer which is the starting point for the messages and we have "tail"
+        *      index/pointer which is the ending point for the messages. When we "display" the
+        *      messages we start from the beginning and continue until we reach "tail". If we
+        *      reach end of buffer, then we just start from the beginning (offset 0). "head" and
+        *      "tail" are actually offsets from the beginning of the buffer.
+        *
+        * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide
+        *       a threasafe way of handling shared memory operations.
+        */
+       if ( (buf->tail + l) < buf->size ){
+               /* before we append the message we need to check the HEAD so that we won't
+                  overwrite any of the message that we still need and adjust HEAD to point
+                  to the next message! */
+               if ( buf->tail < buf->head){
+                       if ( (buf->tail + l) >= buf->head ){
+                         /* we need to move the HEAD to point to the next message
+                          * Theoretically we have enough room to add the whole message to the
+                          * buffer, because of the first outer IF statement, so we don't have
+                          * to worry about overflows here!
+                          */
+                          int k= buf->tail + l - buf->head; /* we need to know how many bytes
+                                                               we are overwriting to make
+                                                               enough room */
+                          char *c=memchr(buf->data+buf->head + k,'\0',buf->size - (buf->head + k));
+                          if (c != NULL) {/* do a sanity check just in case! */
+                               buf->head = c - buf->data + 1; /* we need to convert pointer to
+                                                                 offset + skip the '\0' since
+                                                                 we need to point to the beginning
+                                                                 of the next message */
+                               /* Note: HEAD is only used to "retrieve" messages, it's not used
+                                       when writing messages into our buffer */
+                          }else{ /* show an error message to know we messed up? */
+                               printf("Weird! Can't find the terminator token??? \n");
+                               buf->head=0;
+                          }
+                       }
+               } /* in other cases no overflows have been done yet, so we don't care! */
+
+               /* we should be ok to append the message now */
+               strncpy(buf->data + buf->tail,msg,l); /* append our message */
+               buf->tail+=l; /* count full message w/ '\0' terminating char */
+       }else{
+               /* we need to break up the message and "circle" it around */
+               char *c;
+               int k=buf->tail + l - buf->size; /* count # of bytes we don't fit */
+               
+               /* We need to move HEAD! This is always the case since we are going
+                * to "circle" the message.
+                */
+               c=memchr(buf->data + k ,'\0', buf->size - k);
+               
+               if (c != NULL) /* if we don't have '\0'??? weird!!! */{
+                       /* move head pointer*/
+                       buf->head=c-buf->data+1; 
+                       
+                       /* now write the first part of the message */                   
+                       strncpy(buf->data + buf->tail, msg, l - k - 1);
+                       
+                       /* ALWAYS terminate end of buffer w/ '\0' */
+                       buf->data[buf->size-1]='\0'; 
+                       
+                       /* now write out the rest of the string to the beginning of the buffer */
+                       strcpy(buf->data, &msg[l-k-1]);
+
+                       /* we need to place the TAIL at the end of the message */
+                       buf->tail = k + 1;
+               }else{
+                       printf("Weird! Can't find the terminator token from the beginning??? \n");
+                       buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */
+               }
+               
+       }
+       sem_up(s_semid);
+}
+#endif
 /* Note: There is also a function called "message()" in init.c */
 /* Print a message to the log file. */
 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
@@ -104,6 +267,16 @@ static void message (char *fmt, ...)
        fl.l_start  = 0;
        fl.l_len    = 1;
 
+#ifdef BB_FEATURE_IPC_SYSLOG
+       if ((circular_logging == TRUE) && (buf != NULL)){
+                       char b[1024];
+                       va_start (arguments, fmt);
+                       vsprintf (b, fmt, arguments);
+                       va_end (arguments);
+                       circ_message(b);
+
+       }else
+#endif
        if ((fd = device_open (logFilePath,
                                                   O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
                                                   O_NONBLOCK)) >= 0) {
@@ -197,6 +370,10 @@ static void quit_signal(int sig)
 {
        logMessage(0, "System log daemon exiting.");
        unlink(lfile);
+#ifdef BB_FEATURE_IPC_SYSLOG
+       ipcsyslog_cleanup();
+#endif
+
        exit(TRUE);
 }
 
@@ -382,85 +559,6 @@ static void doSyslogd (void)
        } /* for main loop */
 }
 
-#ifdef BB_FEATURE_KLOGD
-
-static void klogd_signal(int sig)
-{
-       klogctl(7, NULL, 0);
-       klogctl(0, 0, 0);
-       logMessage(0, "Kernel log daemon exiting.");
-       exit(TRUE);
-}
-
-static void doKlogd (void) __attribute__ ((noreturn));
-static void doKlogd (void)
-{
-       int priority = LOG_INFO;
-       char log_buffer[4096];
-       int i, n, lastc;
-       char *start;
-
-       /* Set up sig handlers */
-       signal(SIGINT, klogd_signal);
-       signal(SIGKILL, klogd_signal);
-       signal(SIGTERM, klogd_signal);
-       signal(SIGHUP, SIG_IGN);
-
-#ifdef BB_FEATURE_REMOTE_LOG
-        if (doRemoteLog == TRUE){
-          init_RemoteLog();
-        }
-#endif
-
-       logMessage(0, "klogd started: "
-                          "BusyBox v" BB_VER " (" BB_BT ")");
-
-       /* "Open the log. Currently a NOP." */
-       klogctl(1, NULL, 0);
-
-       while (1) {
-               /* Use kernel syscalls */
-               memset(log_buffer, '\0', sizeof(log_buffer));
-               n = klogctl(2, log_buffer, sizeof(log_buffer));
-               if (n < 0) {
-                       char message[80];
-
-                       if (errno == EINTR)
-                               continue;
-                       snprintf(message, 79, "klogd: Error return from sys_sycall: " \
-                                        "%d - %s.\n", errno, strerror(errno));
-                       logMessage(LOG_SYSLOG | LOG_ERR, message);
-                       exit(1);
-               }
-
-               /* klogctl buffer parsing modelled after code in dmesg.c */
-               start=&log_buffer[0];
-               lastc='\0';
-               for (i=0; i<n; i++) {
-                       if (lastc == '\0' && log_buffer[i] == '<') {
-                               priority = 0;
-                               i++;
-                               while (isdigit(log_buffer[i])) {
-                                       priority = priority*10+(log_buffer[i]-'0');
-                                       i++;
-                               }
-                               if (log_buffer[i] == '>') i++;
-                               start = &log_buffer[i];
-                       }
-                       if (log_buffer[i] == '\n') {
-                               log_buffer[i] = '\0';  /* zero terminate this message */
-                               logMessage(LOG_KERN | priority, start);
-                               start = &log_buffer[i+1];
-                               priority = LOG_INFO;
-                       }
-                       lastc = log_buffer[i];
-               }
-       }
-
-}
-
-#endif
-
 static void daemon_init (char **argv, char *dz, void fn (void))
 {
        setsid();
@@ -472,16 +570,13 @@ static void daemon_init (char **argv, char *dz, void fn (void))
 
 extern int syslogd_main(int argc, char **argv)
 {
-       int opt, pid, klogd_pid;
+       int opt, pid;
        int doFork = TRUE;
 
-#ifdef BB_FEATURE_KLOGD
-       int startKlogd = TRUE;
-#endif
        char *p;
 
        /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
+       while ((opt = getopt(argc, argv, "m:nO:R:LC")) > 0) {
                switch (opt) {
                        case 'm':
                                MarkInterval = atoi(optarg) * 60;
@@ -489,11 +584,6 @@ extern int syslogd_main(int argc, char **argv)
                        case 'n':
                                doFork = FALSE;
                                break;
-#ifdef BB_FEATURE_KLOGD
-                       case 'K':
-                               startKlogd = FALSE;
-                               break;
-#endif
                        case 'O':
                                logFilePath = strdup(optarg);
                                break;
@@ -509,6 +599,11 @@ extern int syslogd_main(int argc, char **argv)
                        case 'L':
                                local_logging = TRUE;
                                break;
+#endif
+#ifdef BB_FEATURE_IPC_SYSLOG
+                       case 'C':
+                               circular_logging = TRUE;
+                               break;
 #endif
                        default:
                                show_usage();
@@ -521,6 +616,7 @@ extern int syslogd_main(int argc, char **argv)
                local_logging = TRUE;
 #endif
 
+
        /* Store away localhost's name before the fork */
        gethostname(LocalHostName, sizeof(LocalHostName));
        if ((p = strchr(LocalHostName, '.'))) {
@@ -529,13 +625,9 @@ extern int syslogd_main(int argc, char **argv)
 
        umask(0);
 
-#ifdef BB_FEATURE_KLOGD
-       /* Start up the klogd process */
-       if (startKlogd == TRUE) {
-               klogd_pid = fork();
-               if (klogd_pid == 0) {
-                       daemon_init (argv, "klogd", doKlogd);
-               }
+#ifdef BB_FEATURE_IPC_SYSLOG
+       if (circular_logging == TRUE ){
+          ipcsyslog_init();
        }
 #endif
 
index 1276201c337f97cbaa7a1f10d806e246eba558c0..a7f982a3512afc8843ca0429f569afda69247445 100644 (file)
--- a/syslogd.c
+++ b/syslogd.c
@@ -7,6 +7,8 @@
  *
  * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
  *
+ * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@cachier.com>
+ *
  * 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
 #include <sys/un.h>
 #include <sys/param.h>
 
-#if ! defined __GLIBC__ && ! defined __UCLIBC__
-#include <sys/syscall.h>
-#include <linux/unistd.h>
-typedef unsigned int socklen_t;
-
-#ifndef __alpha__
-# define __NR_klogctl __NR_syslog
-static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
-#else                                                  /* __alpha__ */
-#define klogctl syslog
-#endif
-
-#else
-# include <sys/klog.h>
-#endif
 #include "busybox.h"
 
-
-
 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
 #define SYSLOG_NAMES
 #include <sys/syslog.h>
@@ -91,6 +76,184 @@ static int doRemoteLog = FALSE;
 static int local_logging = FALSE;
 #endif
 
+/* circular buffer variables/structures */
+#ifdef BB_FEATURE_IPC_SYSLOG
+
+#include <sys/ipc.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+
+/* our shared key */
+static const long KEY_ID = 0x414e4547; /*"GENA"*/
+
+// Semaphore operation structures
+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
+
+static struct sembuf SMwup[1] = {{1, -1, IPC_NOWAIT}}; // set SMwup
+static struct sembuf SMwdn[3] = {{0, 0}, {1, 0}, {1, +1}}; // set SMwdn
+
+static int     shmid = -1;     // ipc shared memory id
+static int     s_semid = -1;   // ipc semaphore id
+int    data_size = 16000; // data size
+int    shm_size = 16000 + sizeof(*buf); // our buffer size
+static int circular_logging = FALSE;
+
+/*
+ * sem_up - up()'s a semaphore.
+ */
+static inline void sem_up(int semid)
+{
+       if ( semop(semid, SMwup, 1) == -1 )
+               perror_msg_and_die("semop[SMwup]");
+}
+
+/*
+ * sem_down - down()'s a semaphore
+ */
+static inline void sem_down(int semid)
+{
+       if ( semop(semid, SMwdn, 2) == -1 )
+               perror_msg_and_die("semop[SMwdn]");
+}
+
+
+void ipcsyslog_cleanup(void){
+       printf("Exiting Syslogd!\n");
+       if (shmid != -1)
+               shmdt(buf);
+
+       if (shmid != -1)
+               shmctl(shmid, IPC_RMID, NULL);
+       if (s_semid != -1)
+               semctl(s_semid, 0, IPC_RMID, 0);
+}
+
+void ipcsyslog_init(void){
+       if (buf == NULL){
+           if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1)
+                       perror_msg_and_die("shmget");
+
+
+           if ((buf = shmat(shmid, NULL, 0)) == NULL)
+                       perror_msg_and_die("shmat");
+
+
+           buf->size=data_size;
+           buf->head=buf->tail=0;
+
+           // we'll trust the OS to set initial semval to 0 (let's hope)
+           if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1){
+               if (errno == EEXIST){
+                  if ((s_semid = semget(KEY_ID, 2, 0)) == -1)
+                   perror_msg_and_die("semget");
+               }else
+                       perror_msg_and_die("semget");
+           }
+       }else{
+               printf("Buffer already allocated just grab the semaphore?");
+       }
+}
+
+/* write message to buffer */
+void circ_message(const char *msg){
+       int l=strlen(msg)+1; /* count the whole message w/ '\0' included */
+
+       sem_down(s_semid);
+
+       /*
+        * Circular Buffer Algorithm:
+        * --------------------------
+        *
+        * Start-off w/ empty buffer of specific size SHM_SIZ
+        * Start filling it up w/ messages. I use '\0' as separator to break up messages.
+        * This is also very handy since we can do printf on message.
+        *
+        * Once the buffer is full we need to get rid of the first message in buffer and
+        * insert the new message. (Note: if the message being added is >1 message then
+        * we will need to "remove" >1 old message from the buffer). The way this is done
+        * is the following:
+        *      When we reach the end of the buffer we set a mark and start from the beginning.
+        *      Now what about the beginning and end of the buffer? Well we have the "head"
+        *      index/pointer which is the starting point for the messages and we have "tail"
+        *      index/pointer which is the ending point for the messages. When we "display" the
+        *      messages we start from the beginning and continue until we reach "tail". If we
+        *      reach end of buffer, then we just start from the beginning (offset 0). "head" and
+        *      "tail" are actually offsets from the beginning of the buffer.
+        *
+        * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide
+        *       a threasafe way of handling shared memory operations.
+        */
+       if ( (buf->tail + l) < buf->size ){
+               /* before we append the message we need to check the HEAD so that we won't
+                  overwrite any of the message that we still need and adjust HEAD to point
+                  to the next message! */
+               if ( buf->tail < buf->head){
+                       if ( (buf->tail + l) >= buf->head ){
+                         /* we need to move the HEAD to point to the next message
+                          * Theoretically we have enough room to add the whole message to the
+                          * buffer, because of the first outer IF statement, so we don't have
+                          * to worry about overflows here!
+                          */
+                          int k= buf->tail + l - buf->head; /* we need to know how many bytes
+                                                               we are overwriting to make
+                                                               enough room */
+                          char *c=memchr(buf->data+buf->head + k,'\0',buf->size - (buf->head + k));
+                          if (c != NULL) {/* do a sanity check just in case! */
+                               buf->head = c - buf->data + 1; /* we need to convert pointer to
+                                                                 offset + skip the '\0' since
+                                                                 we need to point to the beginning
+                                                                 of the next message */
+                               /* Note: HEAD is only used to "retrieve" messages, it's not used
+                                       when writing messages into our buffer */
+                          }else{ /* show an error message to know we messed up? */
+                               printf("Weird! Can't find the terminator token??? \n");
+                               buf->head=0;
+                          }
+                       }
+               } /* in other cases no overflows have been done yet, so we don't care! */
+
+               /* we should be ok to append the message now */
+               strncpy(buf->data + buf->tail,msg,l); /* append our message */
+               buf->tail+=l; /* count full message w/ '\0' terminating char */
+       }else{
+               /* we need to break up the message and "circle" it around */
+               char *c;
+               int k=buf->tail + l - buf->size; /* count # of bytes we don't fit */
+               
+               /* We need to move HEAD! This is always the case since we are going
+                * to "circle" the message.
+                */
+               c=memchr(buf->data + k ,'\0', buf->size - k);
+               
+               if (c != NULL) /* if we don't have '\0'??? weird!!! */{
+                       /* move head pointer*/
+                       buf->head=c-buf->data+1; 
+                       
+                       /* now write the first part of the message */                   
+                       strncpy(buf->data + buf->tail, msg, l - k - 1);
+                       
+                       /* ALWAYS terminate end of buffer w/ '\0' */
+                       buf->data[buf->size-1]='\0'; 
+                       
+                       /* now write out the rest of the string to the beginning of the buffer */
+                       strcpy(buf->data, &msg[l-k-1]);
+
+                       /* we need to place the TAIL at the end of the message */
+                       buf->tail = k + 1;
+               }else{
+                       printf("Weird! Can't find the terminator token from the beginning??? \n");
+                       buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */
+               }
+               
+       }
+       sem_up(s_semid);
+}
+#endif
 /* Note: There is also a function called "message()" in init.c */
 /* Print a message to the log file. */
 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
@@ -104,6 +267,16 @@ static void message (char *fmt, ...)
        fl.l_start  = 0;
        fl.l_len    = 1;
 
+#ifdef BB_FEATURE_IPC_SYSLOG
+       if ((circular_logging == TRUE) && (buf != NULL)){
+                       char b[1024];
+                       va_start (arguments, fmt);
+                       vsprintf (b, fmt, arguments);
+                       va_end (arguments);
+                       circ_message(b);
+
+       }else
+#endif
        if ((fd = device_open (logFilePath,
                                                   O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
                                                   O_NONBLOCK)) >= 0) {
@@ -197,6 +370,10 @@ static void quit_signal(int sig)
 {
        logMessage(0, "System log daemon exiting.");
        unlink(lfile);
+#ifdef BB_FEATURE_IPC_SYSLOG
+       ipcsyslog_cleanup();
+#endif
+
        exit(TRUE);
 }
 
@@ -382,85 +559,6 @@ static void doSyslogd (void)
        } /* for main loop */
 }
 
-#ifdef BB_FEATURE_KLOGD
-
-static void klogd_signal(int sig)
-{
-       klogctl(7, NULL, 0);
-       klogctl(0, 0, 0);
-       logMessage(0, "Kernel log daemon exiting.");
-       exit(TRUE);
-}
-
-static void doKlogd (void) __attribute__ ((noreturn));
-static void doKlogd (void)
-{
-       int priority = LOG_INFO;
-       char log_buffer[4096];
-       int i, n, lastc;
-       char *start;
-
-       /* Set up sig handlers */
-       signal(SIGINT, klogd_signal);
-       signal(SIGKILL, klogd_signal);
-       signal(SIGTERM, klogd_signal);
-       signal(SIGHUP, SIG_IGN);
-
-#ifdef BB_FEATURE_REMOTE_LOG
-        if (doRemoteLog == TRUE){
-          init_RemoteLog();
-        }
-#endif
-
-       logMessage(0, "klogd started: "
-                          "BusyBox v" BB_VER " (" BB_BT ")");
-
-       /* "Open the log. Currently a NOP." */
-       klogctl(1, NULL, 0);
-
-       while (1) {
-               /* Use kernel syscalls */
-               memset(log_buffer, '\0', sizeof(log_buffer));
-               n = klogctl(2, log_buffer, sizeof(log_buffer));
-               if (n < 0) {
-                       char message[80];
-
-                       if (errno == EINTR)
-                               continue;
-                       snprintf(message, 79, "klogd: Error return from sys_sycall: " \
-                                        "%d - %s.\n", errno, strerror(errno));
-                       logMessage(LOG_SYSLOG | LOG_ERR, message);
-                       exit(1);
-               }
-
-               /* klogctl buffer parsing modelled after code in dmesg.c */
-               start=&log_buffer[0];
-               lastc='\0';
-               for (i=0; i<n; i++) {
-                       if (lastc == '\0' && log_buffer[i] == '<') {
-                               priority = 0;
-                               i++;
-                               while (isdigit(log_buffer[i])) {
-                                       priority = priority*10+(log_buffer[i]-'0');
-                                       i++;
-                               }
-                               if (log_buffer[i] == '>') i++;
-                               start = &log_buffer[i];
-                       }
-                       if (log_buffer[i] == '\n') {
-                               log_buffer[i] = '\0';  /* zero terminate this message */
-                               logMessage(LOG_KERN | priority, start);
-                               start = &log_buffer[i+1];
-                               priority = LOG_INFO;
-                       }
-                       lastc = log_buffer[i];
-               }
-       }
-
-}
-
-#endif
-
 static void daemon_init (char **argv, char *dz, void fn (void))
 {
        setsid();
@@ -472,16 +570,13 @@ static void daemon_init (char **argv, char *dz, void fn (void))
 
 extern int syslogd_main(int argc, char **argv)
 {
-       int opt, pid, klogd_pid;
+       int opt, pid;
        int doFork = TRUE;
 
-#ifdef BB_FEATURE_KLOGD
-       int startKlogd = TRUE;
-#endif
        char *p;
 
        /* do normal option parsing */
-       while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
+       while ((opt = getopt(argc, argv, "m:nO:R:LC")) > 0) {
                switch (opt) {
                        case 'm':
                                MarkInterval = atoi(optarg) * 60;
@@ -489,11 +584,6 @@ extern int syslogd_main(int argc, char **argv)
                        case 'n':
                                doFork = FALSE;
                                break;
-#ifdef BB_FEATURE_KLOGD
-                       case 'K':
-                               startKlogd = FALSE;
-                               break;
-#endif
                        case 'O':
                                logFilePath = strdup(optarg);
                                break;
@@ -509,6 +599,11 @@ extern int syslogd_main(int argc, char **argv)
                        case 'L':
                                local_logging = TRUE;
                                break;
+#endif
+#ifdef BB_FEATURE_IPC_SYSLOG
+                       case 'C':
+                               circular_logging = TRUE;
+                               break;
 #endif
                        default:
                                show_usage();
@@ -521,6 +616,7 @@ extern int syslogd_main(int argc, char **argv)
                local_logging = TRUE;
 #endif
 
+
        /* Store away localhost's name before the fork */
        gethostname(LocalHostName, sizeof(LocalHostName));
        if ((p = strchr(LocalHostName, '.'))) {
@@ -529,13 +625,9 @@ extern int syslogd_main(int argc, char **argv)
 
        umask(0);
 
-#ifdef BB_FEATURE_KLOGD
-       /* Start up the klogd process */
-       if (startKlogd == TRUE) {
-               klogd_pid = fork();
-               if (klogd_pid == 0) {
-                       daemon_init (argv, "klogd", doKlogd);
-               }
+#ifdef BB_FEATURE_IPC_SYSLOG
+       if (circular_logging == TRUE ){
+          ipcsyslog_init();
        }
 #endif
 
diff --git a/usage.h b/usage.h
index 8ba86d5696cc2bf183cd1c62ecfef809da1867c5..f241d3a043a662524bdc9c85c64143a401c8ee92 100644 (file)
--- a/usage.h
+++ b/usage.h
        "Options:\n" \
        "\t-l\tList all signal names and numbers."
 
+#define klogd_trivial_usage \
+       "-n"
+#define klogd_full_usage \
+       "Kernel logger.\n"\
+       "Options:\n"\
+       "\t-n\tRun as a foreground process."
+
 #define length_trivial_usage \
        "STRING"
 #define length_full_usage \
 #define logname_full_usage \
        "Print the name of the current user."
 
+#define logread_trivial_usage \
+        ""
+
+#define logread_full_usage \
+        "Shows the messages from syslogd (using circular buffer)."
+
 #ifdef BB_FEATURE_LS_TIMESTAMPS
   #define USAGE_LS_TIMESTAMPS(a) a
 #else
        "Write all buffered filesystem blocks to disk."
 
 
-#ifdef BB_FEATURE_KLOGD
-  #define USAGE_KLOGD(a) a
-#else
-  #define USAGE_KLOGD(a)
-#endif
 #ifdef BB_FEATURE_REMOTE_LOG
   #define USAGE_REMOTE_LOG(a) a
 #else
 #define syslogd_trivial_usage \
        "[OPTION]..."
 #define syslogd_full_usage \
-       "Linux system and kernel (provides klogd) logging utility.\n" \
-       "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n" \
+       "Linux system and kernel logging utility.\n" \
+       "Note that this version of syslogd ignores /etc/syslog.conf.\n\n" \
        "Options:\n" \
        "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n" \
        "\t-n\t\tRun as a foreground process\n" \
-       USAGE_KLOGD("\t-K\t\tDo not start up the klogd process\n") \
        "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)" \
        USAGE_REMOTE_LOG( \
        "\n\t-R HOST[:PORT]\tLog to IP or hostname on PORT (default PORT=514/UDP)\n" \
index e54a3ea65d3f49b240fc3112df24e91b07d9498f..c557130b9fd9e300bf3895257afd6061a6f5b3d6 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -77,6 +77,10 @@ const char mtab_file[] = "/proc/mounts";
 #  endif
 #endif
 
+#if defined(BB_KLOGD) || defined(BB_LOGGER)
+#include <syslog.h>
+#endif
+
 static struct BB_applet *applet_using;
 
 extern void show_usage(void)
@@ -1828,6 +1832,20 @@ void chomp(char *s)
 }
 #endif
 
+#if defined(BB_KLOGD) || defined(BB_LOGGER)
+void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg)
+{
+       openlog(name, 0, facility);
+       syslog(pri, "%s", msg);
+       closelog();
+}
+
+void syslog_msg(int facility, int pri, const char *msg)
+{
+       syslog_msg_with_name(applet_using->name, facility, pri, msg);
+}
+#endif
+
 #if defined(BB_SH)
 void trim(char *s)
 {