Browse Source

Add user name to log file

Serj Kalichev 12 years ago
parent
commit
ff6e4988de
4 changed files with 23 additions and 1 deletions
  1. 9 1
      clish/callback_log.c
  2. 3 0
      clish/shell.h
  3. 1 0
      clish/shell/private.h
  4. 10 0
      clish/shell/shell_new.c

+ 9 - 1
clish/callback_log.c

@@ -6,6 +6,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <syslog.h>
+#include <sys/types.h>
+#include <pwd.h>
 
 #include "internal.h"
 
@@ -16,6 +18,10 @@
 int clish_log_callback(clish_context_t *context, const char *line,
 	int retcode)
 {
+	clish_shell_t *this = context->shell;
+	struct passwd *user = NULL;
+	char *uname = "unknown";
+
 	/* Initialization */
 	if (!line) {
 		openlog(SYSLOG_IDENT, LOG_PID, SYSLOG_FACILITY);
@@ -23,7 +29,9 @@ int clish_log_callback(clish_context_t *context, const char *line,
 	}
 
 	/* Log the given line */
-	syslog(LOG_INFO, "%s : %d", line, retcode);
+	if ((user = clish_shell__get_user(this)))
+		uname = user->pw_name;
+	syslog(LOG_INFO, "(%s) %s : %d", uname, line, retcode);
 
 	return 0;
 }

+ 3 - 0
clish/shell.h

@@ -13,6 +13,8 @@
 #define _clish_shell_h
 
 #include <stdio.h>
+#include <sys/types.h>
+#include <pwd.h>
 
 #include "lub/c_decl.h"
 #include "lub/types.h"
@@ -352,6 +354,7 @@ unsigned int clish_shell__get_wdog_timeout(const clish_shell_t *instance);
 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);
 
 _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 */
+	struct passwd *user; /* Current user information */
 
 	/* Static params for var expanding. The refactoring is needed. */
 	clish_param_t *param_depth;

+ 10 - 0
clish/shell/shell_new.c

@@ -5,9 +5,11 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include "lub/string.h"
+#include "lub/db.h"
 
 /*-------------------------------------------------------- */
 static void clish_shell_init(clish_shell_t * this,
@@ -57,6 +59,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->user = lub_db_getpwuid(getuid()); /* Get user information */
 
 	/* Create internal ptypes and params */
 	/* Current depth */
@@ -143,6 +146,7 @@ static void clish_shell_fini(clish_shell_t * this)
 
 	lub_string_free(this->lockfile);
 	lub_string_free(this->default_shebang);
+	free(this->user);
 	if (this->fifo_name) {
 		unlink(this->fifo_name);
 		lub_string_free(this->fifo_name);
@@ -182,4 +186,10 @@ void clish_shell_delete(clish_shell_t * this)
 	free(this);
 }
 
+/*--------------------------------------------------------- */
+struct passwd *clish_shell__get_user(clish_shell_t * this)
+{
+	return this->user;
+}
+
 /*-------------------------------------------------------- */