summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2010-06-18 11:25:16 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2010-06-18 11:32:11 -0700
commit5414692efe92a9fb87cbcb374493102bca7424bc (patch)
treeeb62b179aed6658a74479b208de27dbfe95e3b58
parentf726fb8698b59328fae9842a88e8a054ccddb45c (diff)
downloadvyatta-bash-5414692efe92a9fb87cbcb374493102bca7424bc.tar.gz
vyatta-bash-5414692efe92a9fb87cbcb374493102bca7424bc.zip
Shell command logging support
Log command exit to message queue for use by command logging daemons.
-rw-r--r--Makefile.in8
-rw-r--r--externs.h3
-rw-r--r--jobs.c1
-rw-r--r--logging.c98
-rw-r--r--logmessage.h16
-rw-r--r--shell.c2
6 files changed, 125 insertions, 3 deletions
diff --git a/Makefile.in b/Makefile.in
index 421458f..4028778 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -385,7 +385,7 @@ BASHINCFILES = $(BASHINCDIR)/posixstat.h $(BASHINCDIR)/ansi_stdlib.h \
$(BASHINCDIR)/ocache.h
LIBRARIES = $(SHLIB_LIB) $(READLINE_LIB) $(HISTORY_LIB) $(TERMCAP_LIB) $(GLOB_LIB) \
- $(TILDE_LIB) $(MALLOC_LIB) $(INTL_LIB) $(LOCAL_LIBS) $(AUDIT_LIB)
+ $(TILDE_LIB) $(MALLOC_LIB) $(INTL_LIB) $(LOCAL_LIBS) -lrt
LIBDEP = $(SHLIB_DEP) $(INTL_DEP) $(READLINE_DEP) $(HISTORY_DEP) $(TERMCAP_DEP) $(GLOB_DEP) \
$(TILDE_DEP) $(MALLOC_DEP)
@@ -405,7 +405,8 @@ CSOURCES = shell.c eval.c parse.y general.c make_cmd.c print_cmd.c y.tab.c \
input.c bashhist.c array.c arrayfunc.c sig.c pathexp.c \
unwind_prot.c siglist.c bashline.c bracecomp.c error.c \
list.c stringlib.c locale.c findcmd.c redir.c \
- pcomplete.c pcomplib.c syntax.c xmalloc.c vyatta-restricted.c
+ pcomplete.c pcomplib.c syntax.c xmalloc.c \
+ logging.c vyatta-restricted.c
HSOURCES = shell.h flags.h trap.h hashcmd.h hashlib.h jobs.h builtins.h \
general.h variables.h config.h $(ALLOC_HEADERS) alias.h \
@@ -435,7 +436,7 @@ OBJECTS = shell.o eval.o y.tab.o general.o make_cmd.o print_cmd.o $(GLOBO) \
alias.o array.o arrayfunc.o braces.o bracecomp.o bashhist.o \
bashline.o $(SIGLIST_O) list.o stringlib.o locale.o findcmd.o redir.o \
pcomplete.o pcomplib.o syntax.o xmalloc.o $(SIGNAMES_O) \
- vyatta-restricted.o
+ logging.o vyatta-restricted.o
# Where the source code of the shell builtins resides.
BUILTIN_SRCDIR=$(srcdir)/builtins
@@ -1008,6 +1009,7 @@ variables.o: ${BASHINCDIR}/posixtime.h
version.o: conftypes.h patchlevel.h version.h
xmalloc.o: config.h bashtypes.h ${BASHINCDIR}/ansi_stdlib.h error.h
vyatta-restricted.o: shell.h command.h vyatta-restricted.h
+logging.o: shell.h ${BASHINCDIR}/filecntl.h jobs.h
# job control
diff --git a/externs.h b/externs.h
index bc28eda..ca4260a 100644
--- a/externs.h
+++ b/externs.h
@@ -85,6 +85,9 @@ extern int reader_loop __P((void));
extern int parse_command __P((void));
extern int read_command __P((void));
+/* Functions from logging.c */
+extern void initialize_logging __P((void));
+
/* Functions from braces.c. */
#if defined (BRACE_EXPANSION)
extern char **brace_expand __P((char *));
diff --git a/jobs.c b/jobs.c
index 5e4b1f6..74ab627 100644
--- a/jobs.c
+++ b/jobs.c
@@ -3023,6 +3023,7 @@ waitchld (wpid, block)
if (PEXITED (child))
{
+ log_process_exit (child);
js.c_totreaped++;
if (job != NO_JOB)
js.c_reaped++;
diff --git a/logging.c b/logging.c
new file mode 100644
index 0000000..93838b5
--- /dev/null
+++ b/logging.c
@@ -0,0 +1,98 @@
+/* logging.c -- Shell command logging functionality */
+
+/* This file is part of GNU Bash, the Bourne Again SHell.
+
+ Bash is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ Bash is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with Bash; see the file COPYING. If not, write to the Free Software
+ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+
+ This code was originally developed by Vyatta, Inc.
+ Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc. */
+
+#include "shell.h"
+#include "filecntl.h"
+#include "jobs.h"
+
+#include <utmp.h>
+#include <unistd.h>
+#include <mqueue.h>
+
+#include "logmessage.h"
+
+/*
+ * Find terminal port associated with stdin.
+ * Since shell is interactive
+ */
+static const char *get_tty()
+{
+ const char *tty = ttyname(0);
+
+ if (tty) {
+ if (!strncmp(tty, "/dev/", 5))
+ tty += 5;
+ }
+
+ return tty;
+}
+
+static mqd_t message_queue = -1;
+static const char *current_tty;
+
+void initialize_logging ()
+{
+ const char *tty;
+
+ tty = get_tty();
+ if (tty == NULL)
+ return; /* Not really an interactive shell */
+
+ message_queue = mq_open("/vbash", O_WRONLY);
+ if (message_queue == (mqd_t)-1)
+ return; /* No logging server running */
+
+ SET_CLOSE_ON_EXEC(message_queue);
+
+ current_tty = strdup(tty);
+}
+
+/*
+ * Logs the result of the child for more detailed accounting
+ */
+void log_process_exit (child)
+ PROCESS *child;
+{
+ size_t cc;
+ struct command_log *lrec;
+
+ if (message_queue == (mqd_t) -1)
+ return;
+
+ cc = sizeof(*lrec) + strlen(child->command) + 1;
+ lrec = alloca(cc);
+ memset(lrec, 0, cc);
+
+ lrec->pid = child->pid;
+ lrec->status = WEXITSTATUS(child->status);
+ lrec->endtime = time(0);
+ lrec->uid = current_user.uid;
+ lrec->euid = current_user.euid;
+ lrec->gid = current_user.gid;
+ lrec->egid = current_user.egid;
+ strncpy(lrec->name, current_user.user_name, UT_NAMESIZE);
+ strncpy(lrec->tty, current_tty, UT_LINESIZE);
+ strcpy(lrec->command, child->command);
+
+ /* Ignore errors?? */
+ mq_send(message_queue, (char *)lrec, cc, 0);
+}
+
diff --git a/logmessage.h b/logmessage.h
new file mode 100644
index 0000000..5bccacc
--- /dev/null
+++ b/logmessage.h
@@ -0,0 +1,16 @@
+/* Format of messages sent on /vbash message queue.
+
+ This code was originally developed by Vyatta, Inc.
+ Copyright (C) 2010 Vyatta, Inc.
+ */
+
+struct command_log {
+ pid_t pid;
+ int status;
+ time_t endtime;
+ uid_t uid, euid;
+ gid_t gid, egid;
+ char name[UT_NAMESIZE];
+ char tty[UT_LINESIZE];
+ char command[0];
+};
diff --git a/shell.c b/shell.c
index 1aac510..09b3cfd 100644
--- a/shell.c
+++ b/shell.c
@@ -703,6 +703,8 @@ main (argc, argv, env)
/* Initialize terminal state for interactive shells after the
.bash_profile and .bashrc are interpreted. */
get_tty_state ();
+
+ initialize_logging();
}
#if !defined (ONESHOT)