From 9b421de0f7ba233f5c08dd1a9d1ae80e14f3418c Mon Sep 17 00:00:00 2001 From: Stefan Ritt Date: Tue, 20 Dec 2005 08:03:57 +0000 Subject: [PATCH] Finished shell subsitution under linux SVN revision: 1580 --- Makefile | 3 +++ src/elogd.c | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index b916ed6a..ad0b98ea 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,9 @@ RM = /bin/rm -f OSTYPE = $(shell uname) +# -lutil needed for forkpty() +LIBS += -lutil + ifeq ($(OSTYPE),solaris) CC = gcc LIBS += -lsocket -lnsl diff --git a/src/elogd.c b/src/elogd.c index b15cb2d4..6b411e23 100755 --- a/src/elogd.c +++ b/src/elogd.c @@ -1028,22 +1028,23 @@ int subst_shell(char *cmd, char *result, int size) #ifndef NO_PTY pid_t pid; int i, pipe; - char line[32], buffer[1024], shell[32]; + char line[32], buffer[256], shell[1024]; fd_set readfds; + struct timeval timeout; if ((pid = forkpty(&pipe, line, NULL, NULL)) < 0) return 0; else if (pid > 0) { /* parent process */ - - write(pipe, cmd, strlen(cmd)); result[0] = 0; do { FD_ZERO(&readfds); FD_SET(pipe, &readfds); + timeout.tv_sec = 3; + timeout.tv_usec = 0; - select(FD_SETSIZE, (void *) &readfds, NULL, NULL, NULL); + select(FD_SETSIZE, (void *) &readfds, NULL, NULL, (void *) &timeout); if (FD_ISSET(pipe, &readfds)) { memset(buffer, 0, sizeof(buffer)); @@ -1052,7 +1053,8 @@ int subst_shell(char *cmd, char *result, int size) break; buffer[i] = 0; strlcat(result, buffer, size); - } + } else + break; } while (1); @@ -1067,7 +1069,8 @@ int subst_shell(char *cmd, char *result, int size) strlcpy(shell, getenv("SHELL"), sizeof(shell)); else strcpy(shell, "/bin/sh"); - execl(shell, shell, 0); + + execl(shell, shell, "-c", cmd, NULL); } #else assert(FALSE);