Browse Source

Syslog facility for clish utility

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

+ 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"
 #include "clish/internal.h"
 
@@ -65,6 +67,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;
 	const char *xml_path = getenv("CLISH_PATH");
 	const char *view = getenv("CLISH_VIEW");
 	const char *viewid = getenv("CLISH_VIEWID");
@@ -82,7 +85,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'},
@@ -99,6 +102,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'},
@@ -159,6 +163,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':
 			my_hooks.script_fn = clish_dryrun_callback;
 			break;
@@ -264,8 +275,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 idle timeout */
 	if (istimeout)
 		clish_shell__set_timeout(shell, timeout);
@@ -370,7 +383,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)

+ 2 - 2
clish/callback_log.c

@@ -12,7 +12,6 @@
 #include "internal.h"
 
 #define SYSLOG_IDENT "klish"
-#define SYSLOG_FACILITY LOG_LOCAL0
 
 /*--------------------------------------------------------- */
 int clish_log_callback(clish_context_t *context, const char *line,
@@ -24,7 +23,8 @@ int clish_log_callback(clish_context_t *context, const char *line,
 
 	/* Initialization */
 	if (!line) {
-		openlog(SYSLOG_IDENT, LOG_PID, SYSLOG_FACILITY);
+		openlog(SYSLOG_IDENT, LOG_PID,
+			clish_shell__get_facility(this));
 		return 0;
 	}
 

+ 2 - 0
clish/shell.h

@@ -351,6 +351,8 @@ int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
 int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
 void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
 struct passwd *clish_shell__get_user(clish_shell_t *instance);
+void clish_shell__set_facility(clish_shell_t *instance, int facility);
+int clish_shell__get_facility(clish_shell_t *instance);
 
 _END_C_DECL
 

+ 1 - 0
clish/shell/private.h

@@ -63,6 +63,7 @@ struct clish_shell_s {
 	char *fifo_name; /* The name of temporary fifo file. */
 	bool_t interactive; /* Is shell interactive. */
 	bool_t log; /* If command logging is enabled */
+	int log_facility; /* Syslog facility */
 	struct passwd *user; /* Current user information */
 
 	/* Static params for var expanding. The refactoring is needed. */

+ 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"
@@ -59,6 +60,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->user = lub_db_getpwuid(getuid()); /* Get user information */
 
 	/* Create internal ptypes and params */

+ 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

@@ -13,7 +13,8 @@ nobase_include_HEADERS += \
     lub/string.h \
     lub/types.h \
     lub/system.h \
-    lub/db.h
+    lub/db.h \
+    lub/log.h
 
 EXTRA_DIST +=   \
     lub/argv/module.am \
@@ -24,6 +25,7 @@ EXTRA_DIST +=   \
     lub/string/module.am \
     lub/system/module.am \
     lub/db/module.am \
+    lub/log/module.am \
     lub/README
 
 include $(top_srcdir)/lub/argv/module.am
@@ -34,3 +36,4 @@ include $(top_srcdir)/lub/dump/module.am
 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/log/module.am