Demo Discussion
Forum Config Examples Contributions Vulnerabilities
  Discussion forum about ELOG  Not logged in ELOG logo
Message ID: 67069     Entry time: Fri May 20 22:45:00 2011
Icon: Idea  Author: John M O'Donnell  Author Email: odonnell@lanl.gov 
Category: Bug fix  OS: Linux  ELOG Version: svn 2414 
Subject: my_shell (OS_UNIX) uses /tmp/elog_shell - conflict when more than one elogd runs at the same time 

all instances of elogd use the same file name in /tmp when calling my_shell.  This can cause some inconsistent behavior when two or more copies of elogd are runnnig at the same time.  (eg. one might detect ImageMagik is installed, and the other not,)

 

The propsed solution is to have the parent read from a pipe to the child rather from a file.  A patch is attached.

Attachment 1: elogd.c.patch_shellPipe  1 kB  | Hide | Hide all
--- elogd.c.orig	2011-05-20 13:28:48.000000000 -0600
+++ elogd.c	2011-05-20 14:16:12.000000000 -0600
@@ -866,25 +866,27 @@
 
 #ifdef OS_UNIX
    pid_t child_pid;
-   int fh, status, i;
+   int fd[2], status, i;
    char str[256];
 
+   /* create pipe for parent<->child communication */
+   if (pipe(fd) < 0)
+      return 0;
+
    if ((child_pid = fork()) < 0)
       return 0;
    else if (child_pid > 0) {
-      /* parent process waits for child */
-      waitpid(child_pid, &status, 0);
+
+      /* parent does not write to child */
+      close(fd[1]);
 
       /* read back result */
       memset(result, 0, size);
-      fh = open("/tmp/elog-shell", O_RDONLY);
-      if (fh > 0) {
-         i = read(fh, result, size);
-         close(fh);
-      }
+      i = read(fd[0], result, size);
+      close(fd[0]);
 
-      /* remove temporary file */
-      remove("/tmp/elog-shell");
+      /* parent process waits for child */
+      waitpid(child_pid, &status, 0);
 
       /* strip trailing CR/LF */
       while (strlen(result) > 0 && (result[strlen(result) - 1] == '\r' || result[strlen(result) - 1] == '\n'))
@@ -926,8 +928,7 @@
             eprintf("Falling back to user \"%s\"\n", str);
       }
 
-      /* execute shell with redirection to /tmp/elog-shell */
-      sprintf(str, "/bin/sh -c \"%s\" > /tmp/elog-shell 2>&1", cmd);
+      /* execute command with redirection to pipe to parent */
 
       if (is_verbose()) {
          efputs("Going to execute: ");
@@ -935,7 +936,17 @@
          efputs("\n");
       }
 
-      system(str);
+      /* redirect stdout/stderr to pipe for parent to read */
+      close(STDOUT_FILENO); dup2(fd[1], STDOUT_FILENO);
+      close(STDERR_FILENO); dup2(fd[1], STDERR_FILENO);
+      /* child does not read the pipe */
+      close(fd[0]);
+      /* child nolonger uses fd[1] - use stderr or stdout instead */
+      close(fd[1]);
+      
+      if (system(cmd) == -1) {
+          fprintf(stderr, "unable to execute command: %s\n", cmd);
+      }
       _exit(0);
    }
 
ELOG V3.1.5-fe60aaf