[PATCH] send cgi stderr to syslog

Luciano Miguel Ferreira Rocha strange at nsk.no-ip.org
Mon Sep 25 20:12:38 UTC 2006


Hello,

The attached patch adds the option of sending the stderr of cgis to
syslog, using busybox's logger.

Options (Config.in) for keeping current behaviour (default), use a
logger or use a logger per cgi process are included.

Regards,
Luciano Rocha

-- 
lfr
0/0
-------------- next part --------------
diff -ur busybox-1.2.1.orig/networking/Config.in busybox-1.2.1/networking/Config.in
--- busybox-1.2.1.orig/networking/Config.in	2006-06-30 23:42:02.000000000 +0100
+++ busybox-1.2.1/networking/Config.in	2006-09-25 21:05:52.000000000 +0100
@@ -126,6 +126,31 @@
 	  This option allows scripts and executables to be invoked
 	  when specific URLs are requested.
 
+config CONFIG_FEATURE_HTTPD_CGI_SYSLOG
+	bool "Log stderr to syslog"
+	default n
+	depends on CONFIG_FEATURE_HTTPD_CGI && CONFIG_LOGGER
+	help
+	  This option starts a logger and causes the stderr of cgis to be
+	  sent to it.
+
+config CONFIG_FEATURE_HTTPD_CGI_SYSLOG_FAC
+	string "Priority"
+	default "daemon.notice"
+	depends on CONFIG_FEATURE_HTTPD_CGI_SYSLOG
+	help
+	  Define the priority of the messages (facility and level, see -p
+	  option for logger).
+
+config CONFIG_FEATURE_HTTPD_CGI_SYSLOG_PERPROC
+	bool "One logger per process"
+	default n
+	depends on CONFIG_FEATURE_HTTPD_CGI_SYSLOG && CONFIG_FEATURE_HTTPD_WITHOUT_INETD
+	help
+	  This option starts one logger per cgi called instead of one global
+	  logger. This will prevent output from different processes to be
+	  mixed, but will use more memory.
+
 config CONFIG_FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR
 	bool "Enable support for running scripts through an interpreter"
 	default n
diff -ur busybox-1.2.1.orig/networking/httpd.c busybox-1.2.1/networking/httpd.c
--- busybox-1.2.1.orig/networking/httpd.c	2006-06-30 23:42:02.000000000 +0100
+++ busybox-1.2.1/networking/httpd.c	2006-09-25 21:05:52.000000000 +0100
@@ -104,11 +104,18 @@
 #include <fcntl.h>         /* for open modes        */
 #include "busybox.h"
 
+#if defined(CONFIG_FEATURE_HTTPD_CGI_SYSLOG) && !defined(CONFIG_FEATURE_HTTPD_WITHOUT_INETD)
+/* if running from inetd, behaviour is the same as if perproc logger */
+# define CONFIG_FEATURE_HTTPD_CGI_SYSLOG_PERPROC
+#endif
 
 static const char httpdVersion[] = "busybox httpd/1.35 6-Oct-2004";
 static const char default_path_httpd_conf[] = "/etc";
 static const char httpd_conf[] = "httpd.conf";
 static const char home[] = "./";
+#if defined(CONFIG_FEATURE_HTTPD_CGI_SYSLOG) && !defined(CONFIG_FEATURE_HTTPD_CGI_SYSLOG_PERPROC)
+static int logFd;
+#endif
 
 #ifdef CONFIG_LFS
 # define cont_l_fmt "%lld"
@@ -993,6 +1000,45 @@
   else return -1;
 }
 
+#ifdef CONFIG_FEATURE_HTTPD_CGI_SYSLOG
+/****************************************************************************
+ *
+ > $Function: openLogger()
+ *
+ * $Description: Open a connection to a logger
+ *
+ * $Parameters:
+
+ *
+ * $Return: int  . . . . File descriptor for writting to the logger.
+ *
+ * $Errors: None, but returns STDERR on error
+ *
+ ****************************************************************************/
+# ifndef CONFIG_FEATURE_HTTPD_CGI_SYSLOG_FAC
+#  define FACILITY "daemon.notice"
+# else
+#  define FACILITY CONFIG_FEATURE_HTTPD_CGI_SYSLOG_FAC
+# endif
+static int openLogger(void)
+{
+	char *cmd[] = { "logger", "-thttpd", "-p", FACILITY, NULL };
+	int p[2];
+
+	if (pipe(p))
+		return 2;
+	dup2(p[0], 0);
+	close(p[0]);
+	fcntl(p[1], F_SETFD, FD_CLOEXEC);
+	if (bb_spawn(cmd) < 0) {
+		close(p[1]);
+		return 2;
+	}
+	fcntl(p[1], F_SETFD, 0);
+	return p[1];
+}
+#endif          /* CONFIG_FEATURE_HTTPD_CGI_SYSLOG */
+
 #ifdef CONFIG_FEATURE_HTTPD_CGI
 /****************************************************************************
  *
@@ -1022,6 +1068,9 @@
 {
   int fromCgi[2];  /* pipe for reading data from CGI */
   int toCgi[2];    /* pipe for sending data to CGI */
+#ifdef CONFIG_FEATURE_HTTPD_CGI_SYSLOG_PERPROC
+  int logFd = openLogger();
+#endif
 
   static char * argp[] = { 0, 0 };
   int pid = 0;
@@ -1057,8 +1106,13 @@
 
       dup2(inFd, 0);  // replace stdin with the pipe
       dup2(outFd, 1);  // replace stdout with the pipe
+#ifdef CONFIG_FEATURE_HTTPD_CGI_SYSLOG
+	  dup2(logFd, 2); // replace stderr w/ pipe to logger
+	  close(logFd);
+#else
       if(!DEBUG)
 	dup2(outFd, 2);  // replace stderr with the pipe
+#endif
 
       close(toCgi[0]);
       close(toCgi[1]);
@@ -2029,6 +2083,9 @@
 # if !DEBUG
   bb_xdaemon(1, 0);     /* don`t change curent directory */
 # endif
+#if defined(CONFIG_FEATURE_HTTPD_CGI_SYSLOG) && !defined(CONFIG_FEATURE_HTTPD_CGI_SYSLOG_PERPROC)
+  logFd = openLogger();
+#endif
   return miniHttpd(server);
 #else
   return miniHttpd();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20060925/6efe1cbd/attachment-0002.pgp 


More information about the busybox mailing list