Browse Source

Syslog facility for clish utility

Serj Kalichev 11 years ago
parent
commit
60376f0577
11 changed files with 119 additions and 8 deletions
  1. 17 3
      bin/clish.c
  2. 4 2
      bin/konfd.c
  3. 5 0
      clish/shell.h
  4. 1 0
      clish/shell/private.h
  5. 2 0
      clish/shell/shell_new.c
  6. 18 0
      clish/shell/shell_pwd.c
  7. 13 0
      lub/log.h
  8. 51 0
      lub/log/log.c
  9. 2 0
      lub/log/module.am
  10. 4 1
      lub/module.am
  11. 2 2
      plugins/default/hook_log.c

+ 17 - 3
bin/clish.c

@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <syslog.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
@@ -27,6 +28,7 @@
 
 #include "lub/list.h"
 #include "lub/system.h"
+#include "lub/log.h"
 #include "clish/shell.h"
 
 #define QUOTE(t) #t
@@ -52,6 +54,7 @@ int main(int argc, char **argv)
 	bool_t utf8 = BOOL_FALSE;
 	bool_t bit8 = BOOL_FALSE;
 	bool_t log = BOOL_FALSE;
+	int log_facility = LOG_LOCAL0;
 	bool_t dryrun = BOOL_FALSE;
 	bool_t dryrun_config = BOOL_FALSE;
 	const char *xml_path = getenv("CLISH_PATH");
@@ -73,7 +76,7 @@ int main(int argc, char **argv)
 	struct sigaction sigpipe_act;
 	sigset_t sigpipe_set;
 
-	static const char *shortopts = "hvs:ledx:w:i:bqu8okt:c:f:z:";
+	static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kt:c:f:z:";
 #ifdef HAVE_GETOPT_H
 	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
@@ -90,6 +93,7 @@ int main(int argc, char **argv)
 		{"utf8",	0, NULL, 'u'},
 		{"8bit",	0, NULL, '8'},
 		{"log",		0, NULL, 'o'},
+		{"facility",	1, NULL, 'O'},
 		{"check",	0, NULL, 'k'},
 		{"timeout",	1, NULL, 't'},
 		{"command",	1, NULL, 'c'},
@@ -150,6 +154,13 @@ int main(int argc, char **argv)
 		case 'o':
 			log = BOOL_TRUE;
 			break;
+		case 'O':
+			if (lub_log_facility(optarg, &log_facility)) {
+				fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
+				help(-1, argv[0]);
+				goto end;
+			}
+			break;
 		case 'd':
 			dryrun = BOOL_TRUE;
 			break;
@@ -256,8 +267,10 @@ int main(int argc, char **argv)
 #endif
 	}
 	/* Set logging */
-	if (log)
+	if (log) {
 		clish_shell__set_log(shell, log);
+		clish_shell__set_facility(shell, log_facility);
+	}
 	/* Set dry-run */
 	if (dryrun)
 		clish_shell__set_dryrun(shell, dryrun);
@@ -377,7 +390,8 @@ static void help(int status, const char *argv0)
 		printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
 		printf("\t-u, --utf8\tForce UTF-8 encoding.\n");
 		printf("\t-8, --8bit\tForce 8-bit encoding.\n");
-		printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
+		printf("\t-o, --log\tEnable command logging to syslog's.\n");
+		printf("\t-O, --facility\tSyslog facility.\n");
 		printf("\t-k, --check\tCheck input files for syntax errors only.\n");
 		printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
 		printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");

+ 4 - 2
bin/konfd.c

@@ -504,8 +504,10 @@ int daemonize(int nochdir, int noclose)
 		_exit(0); /* Exit parent */
 	if (setsid() == -1)
 		return -1;
-	if (!nochdir)
-		chdir("/");
+	if (!nochdir) {
+		if (chdir("/"))
+			return -1;
+	}
 	if (!noclose) {
 		fd = open("/dev/null", O_RDWR, 0);
 		if (fd < 0)

+ 5 - 0
clish/shell.h

@@ -160,8 +160,13 @@ void clish_shell__set_timeout(clish_shell_t *instance, int timeout);
 char *clish_shell__get_line(clish_context_t *context);
 char *clish_shell__get_full_line(clish_context_t *context);
 char *clish_shell__get_params(clish_context_t *context);
+
+/* Log functions */
 void clish_shell__set_log(clish_shell_t *instance, bool_t log);
 bool_t clish_shell__get_log(const clish_shell_t *instance);
+void clish_shell__set_facility(clish_shell_t *instance, int facility);
+int clish_shell__get_facility(clish_shell_t *instance);
+
 int clish_shell_wdog(clish_shell_t *instance);
 void clish_shell__set_wdog_timeout(clish_shell_t *instance,
 	unsigned int timeout);

+ 1 - 0
clish/shell/private.h

@@ -84,6 +84,7 @@ struct clish_shell_s {
 	/* Boolean flags */
 	bool_t interactive; /* Is shell interactive. */
 	bool_t log; /* If command logging is enabled */
+	int log_facility; /* Syslog facility */
 	bool_t dryrun; /* Is this a dry-running */
 	bool_t default_plugin; /* Use or not default plugin */
 

+ 2 - 0
clish/shell/shell_new.c

@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <syslog.h>
 
 #include "lub/string.h"
 #include "lub/db.h"
@@ -71,6 +72,7 @@ static void clish_shell_init(clish_shell_t * this,
 	this->fifo_name = NULL;
 	this->interactive = BOOL_TRUE; /* The interactive shell by default. */
 	this->log = BOOL_FALSE; /* Disable logging by default */
+	this->log_facility = LOG_LOCAL0; /* LOCAL0 for compatibility */
 	this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
 	this->user = lub_db_getpwuid(getuid()); /* Get user information */
 	this->default_plugin = BOOL_TRUE; /* Load default plugin by default */

+ 18 - 0
clish/shell/shell_pwd.c

@@ -3,6 +3,7 @@
  */
 #include <stdlib.h>
 #include <assert.h>
+#include <syslog.h>
 
 #include "lub/string.h"
 #include "private.h"
@@ -153,3 +154,20 @@ int clish_shell__set_socket(clish_shell_t * this, const char * path)
 
 	return 0;
 }
+
+/*--------------------------------------------------------- */
+void clish_shell__set_facility(clish_shell_t *this, int facility)
+{
+	if (!this)
+		return;
+	this->log_facility = facility;
+}
+
+/*--------------------------------------------------------- */
+int clish_shell__get_facility(clish_shell_t *this)
+{
+	if (!this)
+		return LOG_LOCAL0;
+
+	return this->log_facility;
+}

+ 13 - 0
lub/log.h

@@ -0,0 +1,13 @@
+#ifndef _lub_log_h
+#define _lub_log_h
+
+#include <syslog.h>
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+int lub_log_facility(const char *str, int *facility);
+
+#endif
+

+ 51 - 0
lub/log/log.c

@@ -0,0 +1,51 @@
+#include "lub/log.h"
+#include <syslog.h>
+#include "lub/string.h"
+
+int lub_log_facility(const char *str, int *facility)
+{
+	if (!lub_string_nocasecmp(str, "local0"))
+		*facility = LOG_LOCAL0;
+	else if (!lub_string_nocasecmp(str, "local1"))
+		*facility = LOG_LOCAL1;
+	else if (!lub_string_nocasecmp(str, "local2"))
+		*facility = LOG_LOCAL2;
+	else if (!lub_string_nocasecmp(str, "local3"))
+		*facility = LOG_LOCAL3;
+	else if (!lub_string_nocasecmp(str, "local4"))
+		*facility = LOG_LOCAL4;
+	else if (!lub_string_nocasecmp(str, "local5"))
+		*facility = LOG_LOCAL5;
+	else if (!lub_string_nocasecmp(str, "local6"))
+		*facility = LOG_LOCAL6;
+	else if (!lub_string_nocasecmp(str, "local7"))
+		*facility = LOG_LOCAL7;
+	else if (!lub_string_nocasecmp(str, "auth"))
+		*facility = LOG_AUTH;
+	else if (!lub_string_nocasecmp(str, "authpriv"))
+		*facility = LOG_AUTHPRIV;
+	else if (!lub_string_nocasecmp(str, "cron"))
+		*facility = LOG_CRON;
+	else if (!lub_string_nocasecmp(str, "daemon"))
+		*facility = LOG_DAEMON;
+	else if (!lub_string_nocasecmp(str, "ftp"))
+		*facility = LOG_FTP;
+	else if (!lub_string_nocasecmp(str, "kern"))
+		*facility = LOG_KERN;
+	else if (!lub_string_nocasecmp(str, "lpr"))
+		*facility = LOG_LPR;
+	else if (!lub_string_nocasecmp(str, "mail"))
+		*facility = LOG_MAIL;
+	else if (!lub_string_nocasecmp(str, "news"))
+		*facility = LOG_NEWS;
+	else if (!lub_string_nocasecmp(str, "syslog"))
+		*facility = LOG_SYSLOG;
+	else if (!lub_string_nocasecmp(str, "user"))
+		*facility = LOG_USER;
+	else if (!lub_string_nocasecmp(str, "uucp"))
+		*facility = LOG_UUCP;
+	else
+		return -1;
+
+	return 0;
+}

+ 2 - 0
lub/log/module.am

@@ -0,0 +1,2 @@
+liblub_la_SOURCES += lub/log/log.c
+

+ 4 - 1
lub/module.am

@@ -14,7 +14,8 @@ nobase_include_HEADERS += \
     lub/types.h \
     lub/system.h \
     lub/db.h \
-    lub/ini.h
+    lub/ini.h \
+    lub/log.h
 
 EXTRA_DIST +=   \
     lub/argv/module.am \
@@ -26,6 +27,7 @@ EXTRA_DIST +=   \
     lub/system/module.am \
     lub/db/module.am \
     lub/ini/module.am \
+    lub/log/module.am \
     lub/README
 
 include $(top_srcdir)/lub/argv/module.am
@@ -37,3 +39,4 @@ include $(top_srcdir)/lub/string/module.am
 include $(top_srcdir)/lub/system/module.am
 include $(top_srcdir)/lub/db/module.am
 include $(top_srcdir)/lub/ini/module.am
+include $(top_srcdir)/lub/log/module.am

+ 2 - 2
plugins/default/hook_log.c

@@ -12,7 +12,6 @@
 #include "clish/shell.h"
 
 #define SYSLOG_IDENT "klish"
-#define SYSLOG_FACILITY LOG_LOCAL0
 
 /*--------------------------------------------------------- */
 CLISH_HOOK_LOG(clish_hook_log)
@@ -23,7 +22,8 @@ CLISH_HOOK_LOG(clish_hook_log)
 
 	/* Initialization */
 	if (!line) {
-		openlog(SYSLOG_IDENT, LOG_PID, SYSLOG_FACILITY);
+		openlog(SYSLOG_IDENT, LOG_PID,
+			clish_shell__get_facility(this));
 		return 0;
 	}