2001-08-17 05:46:24 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2002-10-20 20:58:55 +00:00
|
|
|
* $Id$
|
2001-08-17 05:46:24 +00:00
|
|
|
* Purpose ...............: Execute subprogram
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2005-08-28 09:42:08 +00:00
|
|
|
* Copyright (C) 1997-2005
|
2001-08-17 05:46:24 +00:00
|
|
|
*
|
|
|
|
* Michiel Broek FIDO: 2:280/2802
|
|
|
|
* Beekmansbos 10
|
|
|
|
* 1971 BV IJmuiden
|
|
|
|
* the Netherlands
|
|
|
|
*
|
|
|
|
* This file is part of MBSE BBS.
|
|
|
|
*
|
|
|
|
* This BBS 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.
|
|
|
|
*
|
|
|
|
* MBSE BBS 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 MBSE BBS; see the file COPYING. If not, write to the Free
|
2003-08-15 20:05:34 +00:00
|
|
|
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
2001-08-17 05:46:24 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2002-06-30 12:48:44 +00:00
|
|
|
#include "../config.h"
|
2004-02-21 14:24:03 +00:00
|
|
|
#include "mbselib.h"
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
int e_pid = 0; /* Execute child pid */
|
|
|
|
|
|
|
|
|
2004-03-17 20:24:59 +00:00
|
|
|
int _execute(char **, char *, char *, char *);
|
|
|
|
int _execute(char **args, char *in, char *out, char *err)
|
2001-08-17 05:46:24 +00:00
|
|
|
{
|
2004-01-18 13:05:56 +00:00
|
|
|
char buf[PATH_MAX];
|
2004-03-18 19:33:43 +00:00
|
|
|
int i, pid, status = 0, rc = 0;
|
2002-10-20 20:58:55 +00:00
|
|
|
|
2004-03-02 20:47:23 +00:00
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (args[i])
|
2005-08-30 18:38:39 +00:00
|
|
|
snprintf(buf + strlen(buf), PATH_MAX - strlen(buf), " %s", args[i]);
|
2004-03-02 20:47:23 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Syslog('+', "Execute:%s",buf);
|
2002-10-20 20:58:55 +00:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
if ((pid = fork()) == 0) {
|
2004-03-15 18:48:23 +00:00
|
|
|
/*
|
|
|
|
* A delay in the child to prevent it returns before the main
|
2004-08-13 14:30:17 +00:00
|
|
|
* process sees it ever started.
|
2004-03-15 18:48:23 +00:00
|
|
|
*/
|
|
|
|
msleep(150);
|
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
if (in) {
|
|
|
|
close(0);
|
|
|
|
if (open(in,O_RDONLY) != 0) {
|
|
|
|
WriteError("$Reopen of stdin to %s failed", MBSE_SS(in));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out) {
|
|
|
|
close(1);
|
|
|
|
if (open(out,O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
|
|
|
|
WriteError("$Reopen of stdout to %s failed", MBSE_SS(out));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
2002-10-20 20:58:55 +00:00
|
|
|
if (err) {
|
|
|
|
close(2);
|
|
|
|
if (open(err,O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
|
|
|
|
WriteError("$Reopen of stderr to %s failed", MBSE_SS(err));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
|
|
|
}
|
2004-03-02 22:05:58 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
errno = 0;
|
2004-03-17 20:24:59 +00:00
|
|
|
if (CFG.priority) {
|
2004-03-15 18:48:23 +00:00
|
|
|
rc = getpriority(PRIO_PROCESS, 0);
|
|
|
|
if (errno == 0) {
|
2004-03-17 20:24:59 +00:00
|
|
|
rc = setpriority(PRIO_PROCESS, 0, CFG.priority);
|
2004-03-15 18:48:23 +00:00
|
|
|
if (rc)
|
2004-03-17 20:24:59 +00:00
|
|
|
WriteError("$execv can't set priority to %d", CFG.priority);
|
2004-03-15 18:48:23 +00:00
|
|
|
}
|
2002-10-20 20:58:55 +00:00
|
|
|
}
|
2004-03-02 20:47:23 +00:00
|
|
|
rc = execv(args[0],args);
|
|
|
|
WriteError("$execv \"%s\" returned %d", MBSE_SS(args[0]), rc);
|
2002-10-20 20:58:55 +00:00
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
e_pid = pid;
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
do {
|
|
|
|
rc = wait(&status);
|
|
|
|
e_pid = 0;
|
|
|
|
} while (((rc > 0) && (rc != pid)) || ((rc == -1) && (errno == EINTR)));
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
switch (rc) {
|
2001-08-17 05:46:24 +00:00
|
|
|
case -1:
|
2004-03-02 22:05:58 +00:00
|
|
|
if (errno == ECHILD) {
|
2004-08-13 14:30:17 +00:00
|
|
|
Syslog('+', "Execute: no child process");
|
2004-03-02 22:05:58 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
WriteError("$Wait returned %d, status %d,%d", rc,status>>8,status&0xff);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
if (WIFEXITED(status)) {
|
2002-10-20 20:58:55 +00:00
|
|
|
rc = WEXITSTATUS(status);
|
|
|
|
if (rc) {
|
2008-02-23 21:50:40 +00:00
|
|
|
if ((strstr(args[0], (char *)"unzip") == NULL) || (rc != 11)) {
|
|
|
|
WriteError("Execute: returned error %d", rc);
|
|
|
|
}
|
2002-10-20 20:58:55 +00:00
|
|
|
return (rc + MBERR_EXTERNAL);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
if (WIFSIGNALED(status)) {
|
2002-10-20 20:58:55 +00:00
|
|
|
rc = WTERMSIG(status);
|
|
|
|
WriteError("Wait stopped on signal %d", rc);
|
|
|
|
return rc;
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
if (rc)
|
2002-10-20 20:58:55 +00:00
|
|
|
WriteError("Wait stopped unknown, rc=%d", rc);
|
2001-08-17 05:46:24 +00:00
|
|
|
return rc;
|
2002-10-20 20:58:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-21 21:09:45 +00:00
|
|
|
|
2004-03-02 20:47:23 +00:00
|
|
|
int execute(char **args, char *in, char *out, char *err)
|
2004-01-21 21:09:45 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2004-03-17 20:24:59 +00:00
|
|
|
if (CFG.do_sync)
|
|
|
|
sync();
|
|
|
|
rc = _execute(args, in, out, err);
|
|
|
|
if (CFG.do_sync)
|
|
|
|
sync();
|
2004-01-21 21:09:45 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-02 20:47:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The old behaviour, parse command strings to arguments.
|
|
|
|
*/
|
|
|
|
int execute_str(char *cmd, char *fil, char *pkt, char *in, char *out, char *err)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *args[16], buf[PATH_MAX];
|
|
|
|
|
|
|
|
memset(args, 0, sizeof(args));
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
if ((pkt != NULL) && strlen(pkt))
|
2005-08-28 09:42:08 +00:00
|
|
|
snprintf(buf, PATH_MAX -1, "%s %s %s", cmd, fil, pkt);
|
2004-03-02 20:47:23 +00:00
|
|
|
else
|
2005-08-28 09:42:08 +00:00
|
|
|
snprintf(buf, PATH_MAX -1, "%s %s", cmd, fil);
|
2004-03-02 20:47:23 +00:00
|
|
|
|
|
|
|
args[i++] = strtok(buf, " \t\0");
|
|
|
|
while ((args[i++] = strtok(NULL," \t\n")) && (i < 15));
|
|
|
|
args[i++] = NULL;
|
|
|
|
|
|
|
|
return execute(args, in, out, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute command in the PATH.
|
|
|
|
*/
|
|
|
|
int execute_pth(char *prog, char *opts, char *in, char *out, char *err)
|
|
|
|
{
|
|
|
|
char *pth;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (strchr(prog, ' ') || strchr(prog, '/')) {
|
|
|
|
WriteError("First parameter of execute_pth() must be a program name");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pth = xstrcpy((char *)"/usr/bin/");
|
|
|
|
pth = xstrcat(pth, prog);
|
|
|
|
if (access(pth, X_OK) == -1) {
|
|
|
|
free(pth);
|
|
|
|
pth = xstrcpy((char *)"/usr/local/bin/");
|
|
|
|
pth = xstrcat(pth, prog);
|
|
|
|
if (access(pth, X_OK) == -1) {
|
|
|
|
free(pth);
|
|
|
|
pth = xstrcpy((char *)"/bin/");
|
|
|
|
pth = xstrcat(pth, prog);
|
|
|
|
if (access(pth, X_OK) == -1) {
|
|
|
|
free(pth);
|
|
|
|
pth = xstrcpy((char *)"/usr/pkg/bin/");
|
|
|
|
pth = xstrcat(pth, prog);
|
|
|
|
if (access(pth, X_OK) == -1) {
|
|
|
|
WriteError("Can't find %s", prog);
|
|
|
|
free(pth);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = execute_str(pth, opts, NULL, in, out, err);
|
|
|
|
free(pth);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-17 05:46:24 +00:00
|
|
|
#define SHELL "/bin/sh"
|
|
|
|
|
|
|
|
|
2004-01-21 21:09:45 +00:00
|
|
|
int _execsh(char *, char *, char *, char *);
|
|
|
|
int _execsh(char *cmd, char *in, char *out, char *err)
|
2001-08-17 05:46:24 +00:00
|
|
|
{
|
2002-10-20 20:58:55 +00:00
|
|
|
int pid, status, rc, sverr;
|
|
|
|
|
|
|
|
Syslog('+', "Execute shell: %s", MBSE_SS(cmd));
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
if ((pid = fork()) == 0) {
|
2004-03-15 18:48:23 +00:00
|
|
|
/*
|
|
|
|
* A delay in the child to prevent it returns before the main
|
|
|
|
* process sess it ever started.
|
|
|
|
*/
|
2004-03-17 20:24:59 +00:00
|
|
|
msleep(150);
|
2004-03-15 18:48:23 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
if (in) {
|
|
|
|
close(0);
|
|
|
|
if (open(in, O_RDONLY) != 0) {
|
|
|
|
WriteError("$Reopen of stdin to %s failed",MBSE_SS(in));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out) {
|
|
|
|
close(1);
|
|
|
|
if (open(out, O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
|
|
|
|
WriteError("$Reopen of stdout to %s failed",MBSE_SS(out));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
close(2);
|
|
|
|
if (open(err, O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
|
|
|
|
WriteError("$Reopen of stderr to %s failed",MBSE_SS(err));
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
rc = execl(SHELL, "sh", "-c", cmd, NULL);
|
|
|
|
WriteError("$execl \"%s\" returned %d", MBSE_SS(cmd), rc);
|
|
|
|
exit(MBERR_EXEC_FAILED);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
e_pid = pid;
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
do {
|
|
|
|
rc = wait(&status);
|
|
|
|
e_pid = 0;
|
|
|
|
sverr = errno;
|
|
|
|
if (status)
|
|
|
|
WriteError("$Wait returned %d, status %d,%d", rc, status >> 8, status & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (((rc > 0) && (rc != pid)) || ((rc == -1) && (sverr == EINTR)));
|
|
|
|
if (rc == -1) {
|
|
|
|
WriteError("$Wait returned %d, status %d,%d", rc, status >> 8, status & 0xff);
|
2004-03-02 20:47:23 +00:00
|
|
|
return 0;
|
2002-10-20 20:58:55 +00:00
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2004-01-21 21:09:45 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int execsh(char *cmd, char *in, char *out, char *err)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2004-03-17 20:24:59 +00:00
|
|
|
if (CFG.do_sync)
|
|
|
|
sync();
|
2004-01-21 21:09:45 +00:00
|
|
|
rc = _execsh(cmd, in, out, err);
|
2004-03-17 20:24:59 +00:00
|
|
|
if (CFG.do_sync)
|
|
|
|
sync();
|
2004-01-21 21:09:45 +00:00
|
|
|
return rc;
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|