Browse Source

Template for syslog logging sym

Serj Kalichev 5 months ago
parent
commit
f3f23f6797

+ 5 - 1
bin/klishd/klishd.c

@@ -84,6 +84,7 @@ int main(int argc, char **argv)
 	int client_fd = -1;
 	struct sigaction sig_act = {};
 	sigset_t sig_set = {};
+	char *log_service_name = NULL;
 
 	// Parse command line options
 	opts = opts_init();
@@ -190,7 +191,8 @@ err: // For listen daemon
 	eloop = NULL;
 
 	// Re-Initialize syslog
-	openlog(LOG_SERVICE_NAME, logoptions, opts->log_facility);
+	log_service_name = faux_str_sprintf(LOG_SERVICE_NAME "[%d]", getpid());
+	openlog(log_service_name, logoptions, opts->log_facility);
 	if (!opts->verbose)
 		setlogmask(LOG_UPTO(LOG_INFO));
 
@@ -238,6 +240,8 @@ err_client:
 	opts_free(opts);
 	faux_ini_free(config);
 
+	faux_str_free(log_service_name);
+
 	return retval;
 }
 

+ 2 - 2
bin/klishd/private.h

@@ -4,8 +4,8 @@
 
 #include <faux/ini.h>
 
-#define LOG_NAME "klishd"
-#define LOG_SERVICE_NAME "klishd-service"
+#define LOG_NAME "klishd-listen"
+#define LOG_SERVICE_NAME "klishd"
 #define DEFAULT_PIDFILE "/var/run/klishd.pid"
 #define DEFAULT_CFGFILE "/etc/klish/klishd.conf"
 #define DEFAULT_DBS "libxml2"

+ 4 - 0
klish/ksession.h

@@ -34,6 +34,10 @@ bool_t ksession_set_term_width(ksession_t *session, size_t term_width);
 size_t ksession_term_height(const ksession_t *session);
 bool_t ksession_set_term_height(ksession_t *session, size_t term_height);
 
+// PID of server's service process
+pid_t ksession_spid(const ksession_t *session);
+bool_t ksession_set_spid(ksession_t *session, pid_t spid);
+
 // PID of client (Unix socket peer)
 pid_t ksession_pid(const ksession_t *session);
 bool_t ksession_set_pid(ksession_t *session, pid_t pid);

+ 10 - 3
klish/ksession/ksession.c

@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <unistd.h>
 
 #include <klish/khelper.h>
 #include <klish/kscheme.h>
@@ -18,9 +19,10 @@ struct ksession_s {
 	bool_t done; // Indicates that session is over and must be closed
 	size_t term_width;
 	size_t term_height;
-	pid_t pid;
-	uid_t uid;
-	char *user;
+	pid_t spid; // Server process to serve current session
+	pid_t pid; // Client's PID
+	uid_t uid; // Client's UID
+	char *user; // Client's user name (get by uid)
 	bool_t isatty_stdin;
 	bool_t isatty_stdout;
 	bool_t isatty_stderr;
@@ -45,6 +47,10 @@ KSET(session, size_t, term_width);
 KGET(session, size_t, term_height);
 KSET(session, size_t, term_height);
 
+// PID of server's service process
+KGET(session, pid_t, spid);
+KSET(session, pid_t, spid);
+
 // PID of client (Unix socket peer)
 KGET(session, pid_t, pid);
 KSET(session, pid_t, pid);
@@ -114,6 +120,7 @@ ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
 	session->isatty_stdin = BOOL_FALSE;
 	session->isatty_stdout = BOOL_FALSE;
 	session->isatty_stderr = BOOL_FALSE;
+	session->spid = getpid(); // For forked processes
 
 	return session;
 }

+ 2 - 0
klish/ktp/ktpd_session.c

@@ -298,6 +298,8 @@ static bool_t ktpd_session_process_auth(ktpd_session_t *ktpd, faux_msg_t *msg)
 	ksession_set_uid(ktpd->session, ucred.uid);
 	user = faux_sysdb_name_by_uid(ucred.uid);
 	ksession_set_user(ktpd->session, user);
+	syslog(LOG_INFO, "Authenticated user %d(%s), client PID %u\n",
+		ucred.uid, user ? user : "?", ucred.pid);
 	faux_str_free(user);
 
 	// Get tty information from auth message status

+ 1 - 0
klish/xml-helper/load.c

@@ -399,6 +399,7 @@ static bool_t process_action(const kxml_node_t *element, void *parent,
 		(KTAG_COMPL != parent_tag) &&
 		(KTAG_HELP != parent_tag) &&
 		(KTAG_PROMPT != parent_tag) &&
+		(KTAG_LOG != parent_tag) &&
 		(KTAG_PTYPE != parent_tag)) {
 		faux_error_sprintf(error,
 			TAG": Tag \"%s\" can't contain ACTION tag",

+ 2 - 1
plugins/klish/Makefile.am

@@ -10,4 +10,5 @@ kplugin_klish_la_SOURCES += \
 	plugins/klish/plugin_init.c \
 	plugins/klish/ptypes.c \
 	plugins/klish/misc.c \
-	plugins/klish/nav.c
+	plugins/klish/nav.c \
+	plugins/klish/log.c

+ 3 - 0
plugins/klish/plugin_init.c

@@ -35,6 +35,9 @@ int kplugin_klish_init(kcontext_t *context)
 		KSYM_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new("prompt", klish_prompt));
 
+	// Log
+	kplugin_add_syms(plugin, ksym_new("syslog", klish_syslog));
+
 	// Navigation
 	// Navigation must be permanent (no dry-run) and sync. Because unsync
 	// actions will be fork()-ed so it can't change current path.

+ 3 - 0
plugins/klish/private.h

@@ -19,6 +19,9 @@ int klish_printl(kcontext_t *context);
 int klish_pwd(kcontext_t *context);
 int klish_prompt(kcontext_t *context);
 
+// Log
+int klish_syslog(kcontext_t *context);
+
 // Navigation
 int klish_nav(kcontext_t *context);