mirror of
https://gitea.psi.ch/ELOG/elog.git
synced 2026-09-12 19:33:03 +00:00
Replaced forkpty() with shell redirection to temporary file
SVN revision: 1658
This commit is contained in:
parent
ca60501c94
commit
01b53a2b55
132
src/elogd.c
132
src/elogd.c
@ -93,6 +93,7 @@ typedef int BOOL;
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
@ -899,85 +900,6 @@ void redirect_to_stderr(void)
|
|||||||
|
|
||||||
/*------------------------------------------------------------------*/
|
/*------------------------------------------------------------------*/
|
||||||
|
|
||||||
#ifdef OS_SOLARIS /* Solaris does not have forkpty(), so emaulate it */
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <sys/stream.h>
|
|
||||||
#include <sys/stropts.h>
|
|
||||||
|
|
||||||
/* fork_pty() remplacement for Solaris.
|
|
||||||
* This ignore the last two arguments
|
|
||||||
* for the moment
|
|
||||||
*/
|
|
||||||
int forkpty(int *amaster, char *name, void *unused1, void *unused2)
|
|
||||||
{
|
|
||||||
int master, slave;
|
|
||||||
char *slave_name;
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
master = open("/dev/ptmx", O_RDWR);
|
|
||||||
if (master & lt; 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (grantpt(master) & lt; 0) {
|
|
||||||
close(master);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unlockpt(master) & lt; 0) {
|
|
||||||
close(master);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
slave_name = ptsname(master);
|
|
||||||
if (slave_name == NULL) {
|
|
||||||
close(master);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
slave = open(slave_name, O_RDWR);
|
|
||||||
if (slave & lt; 0) {
|
|
||||||
close(master);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(slave, I_PUSH, "ptem") & lt; 0 || ioctl(slave, I_PUSH, "ldterm") & lt; 0) {
|
|
||||||
close(slave);
|
|
||||||
close(master);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amaster)
|
|
||||||
*amaster = master;
|
|
||||||
|
|
||||||
if (name)
|
|
||||||
strcpy(name, slave_name);
|
|
||||||
|
|
||||||
pid = fork();
|
|
||||||
switch (pid) {
|
|
||||||
case -1: /* Error */
|
|
||||||
return -1;
|
|
||||||
case 0: /* Child */
|
|
||||||
close(master);
|
|
||||||
dup2(slave, STDIN_FILENO);
|
|
||||||
dup2(slave, STDOUT_FILENO);
|
|
||||||
dup2(slave, STDERR_FILENO);
|
|
||||||
return 0;
|
|
||||||
default: /* Parent */
|
|
||||||
close(slave);
|
|
||||||
return pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* OS_SOLARIS */
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
int subst_shell(char *cmd, char *result, int size)
|
int subst_shell(char *cmd, char *result, int size)
|
||||||
{
|
{
|
||||||
#ifdef OS_WINNT
|
#ifdef OS_WINNT
|
||||||
@ -1110,38 +1032,25 @@ int subst_shell(char *cmd, char *result, int size)
|
|||||||
#endif /* OS_WINNT */
|
#endif /* OS_WINNT */
|
||||||
|
|
||||||
#ifdef OS_UNIX
|
#ifdef OS_UNIX
|
||||||
#ifndef NO_PTY
|
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int i, pipe;
|
int fh;
|
||||||
char line[32], buffer[256], shell[1024], str[256];
|
char str[256];
|
||||||
fd_set readfds;
|
|
||||||
struct timeval timeout;
|
|
||||||
|
|
||||||
if ((pid = forkpty(&pipe, line, NULL, NULL)) < 0)
|
if ((pid = fork()) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
else if (pid > 0) {
|
else if (pid > 0) {
|
||||||
/* parent process */
|
/* parent process waits for child */
|
||||||
result[0] = 0;
|
wait(NULL);
|
||||||
|
|
||||||
do {
|
/* read back result */
|
||||||
FD_ZERO(&readfds);
|
fh = open("/tmp/elog-shell", O_RDONLY);
|
||||||
FD_SET(pipe, &readfds);
|
if (fh > 0) {
|
||||||
timeout.tv_sec = 3;
|
read(fh, result, size);
|
||||||
timeout.tv_usec = 0;
|
close(fh);
|
||||||
|
}
|
||||||
|
|
||||||
select(FD_SETSIZE, (void *) &readfds, NULL, NULL, (void *) &timeout);
|
/* remove temporary file */
|
||||||
|
remove("/tmp/elog-shell");
|
||||||
if (FD_ISSET(pipe, &readfds)) {
|
|
||||||
memset(buffer, 0, sizeof(buffer));
|
|
||||||
i = read(pipe, buffer, sizeof(buffer));
|
|
||||||
if (i <= 0)
|
|
||||||
break;
|
|
||||||
buffer[i] = 0;
|
|
||||||
strlcat(result, buffer, size);
|
|
||||||
} else
|
|
||||||
break;
|
|
||||||
|
|
||||||
} while (1);
|
|
||||||
|
|
||||||
/* strip trailing CR/LF */
|
/* strip trailing CR/LF */
|
||||||
while (strlen(result) > 0 && (result[strlen(result) - 1] == '\r' || result[strlen(result) - 1] == '\n'))
|
while (strlen(result) > 0 && (result[strlen(result) - 1] == '\r' || result[strlen(result) - 1] == '\n'))
|
||||||
@ -1183,16 +1092,11 @@ int subst_shell(char *cmd, char *result, int size)
|
|||||||
eprintf("Falling back to user \"%s\"\n", str);
|
eprintf("Falling back to user \"%s\"\n", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getenv("SHELL"))
|
/* execute shell with redirection to /tmp/elog-shell */
|
||||||
strlcpy(shell, getenv("SHELL"), sizeof(shell));
|
sprintf(str, "/bin/sh -c \"%s\" > /tmp/elog-shell 2>&1", cmd);
|
||||||
else
|
system(str);
|
||||||
strcpy(shell, "/bin/sh");
|
exit(0);
|
||||||
|
|
||||||
execl(shell, shell, "-c", cmd, NULL);
|
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
assert(FALSE);
|
|
||||||
#endif /* NO_PTY */
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user