2019-06-22 23:46:07 +03:00
|
|
|
/* $NetBSD: run.c,v 1.12 2019/06/22 20:46:07 christos Exp $ */
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1997 Piermont Information Systems Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Philip A. Nelson for Piermont Information Systems Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2011-04-04 12:30:12 +04:00
|
|
|
* 3. The name of Piermont Information Systems Inc. may not be used to endorse
|
1997-09-27 03:02:53 +04:00
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2011-04-04 12:30:12 +04:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
1997-09-27 03:02:53 +04:00
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
2011-04-04 12:30:12 +04:00
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
1997-09-27 03:02:53 +04:00
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* run.c -- routines to interact with other programs. */
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
/* XXX write return codes ignored. XXX */
|
|
|
|
|
|
|
|
#include <errno.h>
|
1997-09-27 03:02:53 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
1999-01-21 11:02:17 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <termios.h>
|
2003-07-18 13:34:42 +04:00
|
|
|
#include <dirent.h>
|
1999-01-21 11:02:17 +03:00
|
|
|
#include <util.h>
|
2003-09-27 14:38:05 +04:00
|
|
|
#include <signal.h>
|
1999-01-21 11:02:17 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <sys/ioctl.h>
|
1997-09-27 03:02:53 +04:00
|
|
|
#include <sys/types.h>
|
1999-01-21 11:02:17 +03:00
|
|
|
#include <sys/wait.h>
|
1997-09-27 03:02:53 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "defs.h"
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
#include "menu_defs.h"
|
1997-11-02 06:45:27 +03:00
|
|
|
#include "msg_defs.h"
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
#define MAXBUF 256
|
|
|
|
|
2018-11-02 21:07:33 +03:00
|
|
|
#if defined(DEBUG) && defined(DEBUG_SYSTEM)
|
|
|
|
static inline int
|
|
|
|
Xsystem(const char *y)
|
|
|
|
{
|
|
|
|
printf ("%s\n", y);
|
|
|
|
return 0;
|
|
|
|
}
|
1997-09-27 03:02:53 +04:00
|
|
|
#else
|
|
|
|
#define Xsystem(y) system(y)
|
|
|
|
#endif
|
|
|
|
|
1997-11-02 06:45:27 +03:00
|
|
|
/*
|
2011-04-04 12:30:12 +04:00
|
|
|
* local prototypes
|
1997-11-02 06:45:27 +03:00
|
|
|
*/
|
2006-09-29 13:29:12 +04:00
|
|
|
int log_flip (menudesc *, void *);
|
2003-10-20 00:17:31 +04:00
|
|
|
static int script_flip (menudesc *, void *);
|
1997-11-02 06:45:27 +03:00
|
|
|
|
1997-09-27 03:02:53 +04:00
|
|
|
#define BUFSIZE 4096
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
menu_ent logmenu [2] = {
|
2019-06-22 23:46:07 +03:00
|
|
|
{ .opt_action=log_flip},
|
|
|
|
{ .opt_action=script_flip}
|
2018-11-20 22:02:07 +03:00
|
|
|
};
|
2003-10-20 00:17:31 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
log_menu_label(menudesc *m, int opt, void *arg)
|
|
|
|
{
|
|
|
|
wprintw(m->mw, "%s: %s",
|
|
|
|
msg_string(opt ? MSG_Scripting : MSG_Logging),
|
2012-01-06 01:29:24 +04:00
|
|
|
msg_string((opt ? script != NULL : logfp != NULL) ?
|
|
|
|
MSG_On : MSG_Off));
|
2003-10-20 00:17:31 +04:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
do_logging(void)
|
|
|
|
{
|
|
|
|
int menu_no;
|
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
menu_no = new_menu(MSG_Logging_functions, logmenu, 2, -1, 12,
|
|
|
|
0, 20, MC_SCROLL, NULL, log_menu_label, NULL,
|
|
|
|
MSG_Pick_an_option, NULL);
|
1999-01-21 11:02:17 +03:00
|
|
|
|
|
|
|
if (menu_no < 0) {
|
|
|
|
(void)fprintf(stderr, "Dynamic menu creation failed.\n");
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp)
|
2002-12-05 04:17:15 +03:00
|
|
|
(void)fprintf(logfp, "Dynamic menu creation failed.\n");
|
1999-01-21 11:02:17 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2003-06-03 15:54:48 +04:00
|
|
|
process_menu(menu_no, NULL);
|
1999-01-21 11:02:17 +03:00
|
|
|
free_menu(menu_no);
|
|
|
|
}
|
|
|
|
|
2006-09-29 13:29:12 +04:00
|
|
|
int
|
2003-01-10 23:00:27 +03:00
|
|
|
/*ARGSUSED*/
|
2003-07-27 11:45:08 +04:00
|
|
|
log_flip(menudesc *m, void *arg)
|
1999-01-21 11:02:17 +03:00
|
|
|
{
|
|
|
|
time_t tloc;
|
|
|
|
|
|
|
|
(void)time(&tloc);
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp) {
|
2014-10-14 20:35:20 +04:00
|
|
|
fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
|
2002-12-05 04:17:15 +03:00
|
|
|
fflush(logfp);
|
|
|
|
fclose(logfp);
|
2012-01-06 01:29:24 +04:00
|
|
|
logfp = NULL;
|
1999-01-21 11:02:17 +03:00
|
|
|
} else {
|
2008-12-01 13:22:36 +03:00
|
|
|
logfp = fopen("/tmp/sysinst.log", "a");
|
2002-12-05 04:17:15 +03:00
|
|
|
if (logfp != NULL) {
|
|
|
|
fprintf(logfp,
|
2014-10-14 20:35:20 +04:00
|
|
|
"Log started at: %s\n", safectime(&tloc));
|
2011-04-04 12:30:12 +04:00
|
|
|
fflush(logfp);
|
2000-01-07 05:02:05 +03:00
|
|
|
} else {
|
2014-08-03 20:09:38 +04:00
|
|
|
if (mainwin) {
|
2019-06-20 03:43:55 +03:00
|
|
|
msg_fmt_display(MSG_openfail, "%s%s",
|
|
|
|
"log file", strerror(errno));
|
2014-08-03 20:09:38 +04:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "could not open /tmp/sysinst.log: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2000-01-07 05:02:05 +03:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
static int
|
2003-01-10 23:00:27 +03:00
|
|
|
/*ARGSUSED*/
|
2003-07-27 11:45:08 +04:00
|
|
|
script_flip(menudesc *m, void *arg)
|
1999-01-21 11:02:17 +03:00
|
|
|
{
|
|
|
|
time_t tloc;
|
|
|
|
|
|
|
|
(void)time(&tloc);
|
2012-01-06 01:29:24 +04:00
|
|
|
if (script) {
|
2012-02-26 14:17:44 +04:00
|
|
|
scripting_fprintf(NULL, "# Script ended at: %s\n",
|
2014-10-14 20:35:20 +04:00
|
|
|
safectime(&tloc));
|
1999-01-21 11:02:17 +03:00
|
|
|
fflush(script);
|
|
|
|
fclose(script);
|
2012-01-06 01:29:24 +04:00
|
|
|
script = NULL;
|
1999-01-21 11:02:17 +03:00
|
|
|
} else {
|
2008-12-01 13:22:36 +03:00
|
|
|
script = fopen("/tmp/sysinst.sh", "w");
|
2000-01-07 05:02:05 +03:00
|
|
|
if (script != NULL) {
|
extend sysinst disk handling, some general clean ups, better /etc support
and some sparc64 fixes. details:
- new scripting_{,v}fprintf() that also write to the script log (if open).
- support of creation of LFS filesystems
- standard installs now allow one to choose which file systems to create,
out of the list of /usr, /var, /home, MFS /tmp, and swap. a / is always
created, but the rest all optional. [*]
- new disklabel method: use existing. this requires one to fill in the
mount points (and having at least a "/" is enforced).
- preservation of existing filesystems (at install) is supported.
- rewrote most of make_fstab(): the entire thing is generated from the
bsdlabel now, rather than hard coding / to 'a' and swap to 'b'. create
MFS /tmp if requested.
- if IPv6 support isn't present, don't display any ipv6 messages.
- better rc.conf support
- sparc & sparc64 default to "cylinders" now.
- choosing "standard" instead of "standard with X" de-selects the X pkgs
by default.
- sparc64: don't install bootblocks until we have a complete system, avoids
booting with no /dev/console.
- sparc64: bump STDNEEDMB, it is way low.
[*] i have added default values for DEFVARSIZE & DEFUSRSIZE (this is used
only if /usr and /home are selected) to each port, based on STDNEEDMB. as
such, my guesses might be wrong so port maintainers should look at these.
2001-01-14 05:38:13 +03:00
|
|
|
scripting_fprintf(NULL, "#!/bin/sh\n");
|
|
|
|
scripting_fprintf(NULL, "# Script started at: %s\n",
|
2014-10-14 20:35:20 +04:00
|
|
|
safectime(&tloc));
|
2011-04-04 12:30:12 +04:00
|
|
|
fflush(script);
|
2000-01-07 05:02:05 +03:00
|
|
|
} else {
|
2019-06-20 03:43:55 +03:00
|
|
|
msg_fmt_display(MSG_openfail, "%s%s", "script file",
|
2012-02-26 14:17:44 +04:00
|
|
|
strerror(errno));
|
2000-01-07 05:02:05 +03:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1997-09-27 03:02:53 +04:00
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
collect(int kind, char **buffer, const char *name, ...)
|
1997-09-27 03:02:53 +04:00
|
|
|
{
|
|
|
|
size_t nbytes; /* Number of bytes in buffer. */
|
|
|
|
size_t fbytes; /* Number of bytes in file. */
|
|
|
|
struct stat st; /* stat information. */
|
|
|
|
int ch;
|
|
|
|
FILE *f;
|
2004-05-16 01:51:30 +04:00
|
|
|
char fileorcmd[STRSIZE];
|
1997-09-27 03:02:53 +04:00
|
|
|
va_list ap;
|
2004-05-16 01:51:30 +04:00
|
|
|
char *cp;
|
1997-09-27 03:02:53 +04:00
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
va_start(ap, name);
|
2006-01-13 01:02:44 +03:00
|
|
|
vsnprintf(fileorcmd, sizeof fileorcmd, name, ap);
|
1998-06-20 17:05:48 +04:00
|
|
|
va_end(ap);
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
if (kind == T_FILE) {
|
|
|
|
/* Get the file information. */
|
1998-06-20 17:05:48 +04:00
|
|
|
if (stat(fileorcmd, &st)) {
|
1997-09-27 03:02:53 +04:00
|
|
|
*buffer = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fbytes = (size_t)st.st_size;
|
|
|
|
|
|
|
|
/* Open the file. */
|
1998-06-20 17:05:48 +04:00
|
|
|
f = fopen(fileorcmd, "r");
|
1997-09-27 03:02:53 +04:00
|
|
|
if (f == NULL) {
|
|
|
|
*buffer = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Open the program. */
|
1998-06-20 17:05:48 +04:00
|
|
|
f = popen(fileorcmd, "r");
|
1997-09-27 03:02:53 +04:00
|
|
|
if (f == NULL) {
|
|
|
|
*buffer = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fbytes = BUFSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fbytes == 0)
|
|
|
|
fbytes = BUFSIZE;
|
2011-04-04 12:30:12 +04:00
|
|
|
|
1997-09-27 03:02:53 +04:00
|
|
|
/* Allocate the buffer size. */
|
2004-05-16 01:51:30 +04:00
|
|
|
*buffer = cp = malloc(fbytes + 1);
|
|
|
|
if (!cp)
|
|
|
|
nbytes = -1;
|
|
|
|
else {
|
|
|
|
/* Read the buffer. */
|
|
|
|
nbytes = 0;
|
|
|
|
while (nbytes < fbytes && (ch = fgetc(f)) != EOF)
|
|
|
|
cp[nbytes++] = ch;
|
|
|
|
cp[nbytes] = 0;
|
|
|
|
}
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
if (kind == T_FILE)
|
|
|
|
fclose(f);
|
|
|
|
else
|
|
|
|
pclose(f);
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-02 06:45:27 +03:00
|
|
|
/*
|
|
|
|
* system(3), but with a debug wrapper.
|
1999-01-21 11:02:17 +03:00
|
|
|
* use only for curses sub-applications.
|
1997-11-02 06:45:27 +03:00
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
2003-06-16 23:42:13 +04:00
|
|
|
do_system(const char *execstr)
|
1997-11-02 06:45:27 +03:00
|
|
|
{
|
|
|
|
register int ret;
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
/*
|
|
|
|
* The following may be more than one function call. Can't just
|
|
|
|
* "return Xsystem (command);"
|
|
|
|
*/
|
1997-11-02 06:45:27 +03:00
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
ret = Xsystem(execstr);
|
|
|
|
return (ret);
|
1997-11-02 06:45:27 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-07-18 13:34:42 +04:00
|
|
|
static char **
|
|
|
|
make_argv(const char *cmd)
|
|
|
|
{
|
|
|
|
char **argv = 0;
|
|
|
|
int argc = 0;
|
|
|
|
const char *cp;
|
|
|
|
char *dp, *fn;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *dirent;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
for (; *cmd != 0; cmd = cp + strspn(cp, " "), argc++) {
|
2003-10-20 00:17:31 +04:00
|
|
|
if (*cmd == '\'')
|
|
|
|
cp = strchr(++cmd, '\'');
|
|
|
|
else
|
|
|
|
cp = strchr(cmd, ' ');
|
2003-07-18 13:34:42 +04:00
|
|
|
if (cp == NULL)
|
|
|
|
cp = strchr(cmd, 0);
|
|
|
|
argv = realloc(argv, (argc + 2) * sizeof *argv);
|
|
|
|
if (argv == NULL)
|
|
|
|
err(1, "realloc(argv) for %s", cmd);
|
|
|
|
asprintf(argv + argc, "%.*s", (int)(cp - cmd), cmd);
|
2004-06-06 01:18:59 +04:00
|
|
|
/* Hack to remove %xx encoded ftp password */
|
|
|
|
dp = strstr(cmd, ":%");
|
|
|
|
if (dp != NULL && dp < cp) {
|
|
|
|
for (fn = dp + 4; *fn == '%'; fn += 3)
|
|
|
|
continue;
|
|
|
|
if (*fn == '@')
|
|
|
|
memset(dp + 1, '*', fn - dp - 1);
|
|
|
|
}
|
2003-10-20 00:17:31 +04:00
|
|
|
if (*cp == '\'')
|
|
|
|
cp++;
|
2003-07-18 13:34:42 +04:00
|
|
|
if (cp[-1] != '*')
|
|
|
|
continue;
|
|
|
|
/* do limited filename globbing */
|
|
|
|
dp = argv[argc];
|
|
|
|
fn = strrchr(dp, '/');
|
|
|
|
if (fn != NULL)
|
|
|
|
*fn = 0;
|
|
|
|
dir = opendir(dp);
|
|
|
|
if (fn != NULL)
|
|
|
|
*fn++ = '/';
|
|
|
|
else
|
|
|
|
fn = dp;
|
|
|
|
if (dir == NULL)
|
|
|
|
continue;
|
|
|
|
l = strlen(fn) - 1;
|
|
|
|
while ((dirent = readdir(dir))) {
|
|
|
|
if (dirent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
if (strncmp(dirent->d_name, fn, l) != 0)
|
|
|
|
continue;
|
|
|
|
if (dp != argv[argc])
|
|
|
|
argc++;
|
|
|
|
argv = realloc(argv, (argc + 2) * sizeof *argv);
|
|
|
|
if (argv == NULL)
|
|
|
|
err(1, "realloc(argv) for %s", cmd);
|
|
|
|
asprintf(argv + argc, "%.*s%s", (int)(fn - dp), dp,
|
|
|
|
dirent->d_name);
|
|
|
|
}
|
|
|
|
if (dp != argv[argc])
|
|
|
|
free(dp);
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_argv(char **argv)
|
|
|
|
{
|
|
|
|
char **n, *a;
|
|
|
|
|
|
|
|
for (n = argv; (a = *n++);)
|
|
|
|
free(a);
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
static WINDOW *
|
|
|
|
show_cmd(const char *scmd, struct winsize *win)
|
|
|
|
{
|
|
|
|
WINDOW *actionwin;
|
2004-06-06 01:18:59 +04:00
|
|
|
int nrow;
|
2003-10-20 00:17:31 +04:00
|
|
|
|
|
|
|
wclear(stdscr);
|
|
|
|
clearok(stdscr, 1);
|
|
|
|
touchwin(stdscr);
|
|
|
|
refresh();
|
|
|
|
|
2003-11-11 20:27:13 +03:00
|
|
|
mvaddstr(0, 4, msg_string(MSG_Status));
|
2003-10-20 00:17:31 +04:00
|
|
|
standout();
|
|
|
|
addstr(msg_string(MSG_Running));
|
|
|
|
standend();
|
|
|
|
mvaddstr(1, 4, msg_string(MSG_Command));
|
|
|
|
standout();
|
2004-06-06 01:18:59 +04:00
|
|
|
printw("%s", scmd);
|
2003-10-20 00:17:31 +04:00
|
|
|
standend();
|
2004-06-06 01:18:59 +04:00
|
|
|
addstr("\n\n");
|
2019-02-27 15:59:35 +03:00
|
|
|
hline(0, win->ws_col);
|
2003-10-20 00:17:31 +04:00
|
|
|
refresh();
|
|
|
|
|
2004-06-06 01:18:59 +04:00
|
|
|
nrow = getcury(stdscr) + 1;
|
|
|
|
|
|
|
|
actionwin = subwin(stdscr, win->ws_row - nrow, win->ws_col, nrow, 0);
|
|
|
|
if (actionwin == NULL) {
|
|
|
|
fprintf(stderr, "sysinst: failed to allocate output window.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
scrollok(actionwin, TRUE);
|
|
|
|
if (has_colors()) {
|
|
|
|
wbkgd(actionwin, getbkgd(stdscr));
|
|
|
|
wattrset(actionwin, getattrs(stdscr));
|
|
|
|
}
|
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
wmove(actionwin, 0, 0);
|
|
|
|
wrefresh(actionwin);
|
|
|
|
|
|
|
|
return actionwin;
|
|
|
|
}
|
|
|
|
|
1997-11-02 06:45:27 +03:00
|
|
|
/*
|
2006-04-24 23:00:29 +04:00
|
|
|
* launch a program inside a subwindow, and report its return status when done
|
1997-11-02 06:45:27 +03:00
|
|
|
*/
|
2001-09-13 22:07:26 +04:00
|
|
|
static int
|
2003-10-20 00:17:31 +04:00
|
|
|
launch_subwin(WINDOW **actionwin, char **args, struct winsize *win, int flags,
|
2012-02-26 14:17:44 +04:00
|
|
|
const char *scmd, const char **errstr)
|
1997-11-02 06:45:27 +03:00
|
|
|
{
|
2003-08-05 17:35:26 +04:00
|
|
|
int n, i;
|
1999-03-22 12:02:47 +03:00
|
|
|
int selectfailed;
|
1999-01-21 11:02:17 +03:00
|
|
|
int status, master, slave;
|
|
|
|
fd_set active_fd_set, read_fd_set;
|
2003-07-28 15:32:21 +04:00
|
|
|
pid_t child, pid;
|
|
|
|
char ibuf[MAXBUF];
|
2000-10-12 03:47:55 +04:00
|
|
|
char pktdata;
|
2003-08-09 23:26:38 +04:00
|
|
|
char *cp, *ncp;
|
2012-02-26 14:17:44 +04:00
|
|
|
struct termios rtt, tt;
|
2003-07-07 16:30:19 +04:00
|
|
|
struct timeval tmo;
|
2003-10-20 00:17:31 +04:00
|
|
|
static int do_tioccons = 2;
|
1999-01-21 11:02:17 +03:00
|
|
|
|
|
|
|
(void)tcgetattr(STDIN_FILENO, &tt);
|
2001-09-13 22:07:26 +04:00
|
|
|
if (openpty(&master, &slave, NULL, &tt, win) == -1) {
|
|
|
|
*errstr = "openpty() failed";
|
2003-10-20 00:17:31 +04:00
|
|
|
return -1;
|
2001-09-13 22:07:26 +04:00
|
|
|
}
|
|
|
|
|
2000-10-12 03:47:55 +04:00
|
|
|
rtt = tt;
|
1999-01-21 11:02:17 +03:00
|
|
|
|
1999-06-22 04:43:57 +04:00
|
|
|
/* ignore tty signals until we're done with subprocess setup */
|
|
|
|
ttysig_ignore = 1;
|
2000-10-12 03:47:55 +04:00
|
|
|
ioctl(master, TIOCPKT, &ttysig_ignore);
|
1999-06-22 04:43:57 +04:00
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
/* Try to get console output into our pipe */
|
|
|
|
if (do_tioccons) {
|
|
|
|
if (ioctl(slave, TIOCCONS, &do_tioccons) == 0
|
|
|
|
&& do_tioccons == 2) {
|
|
|
|
/* test our output - we don't want it grabbed */
|
|
|
|
write(1, " \b", 2);
|
|
|
|
ioctl(master, FIONREAD, &do_tioccons);
|
|
|
|
if (do_tioccons != 0) {
|
|
|
|
do_tioccons = 0;
|
|
|
|
ioctl(slave, TIOCCONS, &do_tioccons);
|
|
|
|
} else
|
|
|
|
do_tioccons = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp)
|
2005-02-26 20:40:49 +03:00
|
|
|
fflush(logfp);
|
2012-01-06 01:29:24 +04:00
|
|
|
if (script)
|
2005-02-26 20:40:49 +03:00
|
|
|
fflush(script);
|
|
|
|
|
2003-07-17 23:44:53 +04:00
|
|
|
child = fork();
|
|
|
|
switch (child) {
|
1999-01-21 11:02:17 +03:00
|
|
|
case -1:
|
1999-06-22 04:43:57 +04:00
|
|
|
ttysig_ignore = 0;
|
2000-01-19 12:44:01 +03:00
|
|
|
refresh();
|
2001-09-13 22:07:26 +04:00
|
|
|
*errstr = "fork() failed";
|
1999-01-21 11:02:17 +03:00
|
|
|
return -1;
|
2003-07-17 23:44:53 +04:00
|
|
|
case 0: /* child */
|
1999-01-21 11:02:17 +03:00
|
|
|
(void)close(STDIN_FILENO);
|
2003-07-28 15:32:21 +04:00
|
|
|
/* silently stop curses */
|
|
|
|
(void)close(STDOUT_FILENO);
|
|
|
|
(void)open("/dev/null", O_RDWR, 0);
|
|
|
|
dup2(STDIN_FILENO, STDOUT_FILENO);
|
|
|
|
endwin();
|
1999-01-21 11:02:17 +03:00
|
|
|
(void)close(master);
|
2000-10-12 03:47:55 +04:00
|
|
|
rtt = tt;
|
2011-04-04 12:30:12 +04:00
|
|
|
rtt.c_lflag |= (ICANON|ECHO);
|
2000-10-12 03:47:55 +04:00
|
|
|
(void)tcsetattr(slave, TCSANOW, &rtt);
|
1999-01-21 11:02:17 +03:00
|
|
|
login_tty(slave);
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp) {
|
2005-02-26 20:40:49 +03:00
|
|
|
fprintf(logfp, "executing: %s\n", scmd);
|
2002-12-05 04:17:15 +03:00
|
|
|
fclose(logfp);
|
2012-01-06 01:29:24 +04:00
|
|
|
logfp = NULL;
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
2012-01-06 01:29:24 +04:00
|
|
|
if (script) {
|
2005-02-26 20:40:49 +03:00
|
|
|
fprintf(script, "%s\n", scmd);
|
1999-01-21 11:02:17 +03:00
|
|
|
fclose(script);
|
2012-01-06 01:29:24 +04:00
|
|
|
script = NULL;
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
2006-02-25 23:21:00 +03:00
|
|
|
if (strcmp(args[0], "cd") == 0 && strcmp(args[2], "&&") == 0) {
|
|
|
|
target_chdir_or_die(args[1]);
|
|
|
|
args += 3;
|
|
|
|
}
|
2006-01-13 01:02:44 +03:00
|
|
|
if (flags & RUN_XFER_DIR)
|
|
|
|
target_chdir_or_die(xfer_dir);
|
2000-10-12 03:47:55 +04:00
|
|
|
/*
|
|
|
|
* If target_prefix == "", the chroot will fail, but
|
|
|
|
* that's ok, since we don't need it then.
|
|
|
|
*/
|
2006-01-13 01:02:44 +03:00
|
|
|
if (flags & RUN_CHROOT && *target_prefix()
|
|
|
|
&& chroot(target_prefix()) != 0)
|
2005-02-26 20:40:49 +03:00
|
|
|
warn("chroot(%s) for %s", target_prefix(), *args);
|
|
|
|
else {
|
|
|
|
execvp(*args, args);
|
|
|
|
warn("execvp %s", *args);
|
|
|
|
}
|
1999-03-11 19:48:27 +03:00
|
|
|
_exit(EXIT_FAILURE);
|
2006-01-13 01:02:44 +03:00
|
|
|
// break; /* end of child */
|
1999-06-22 04:43:57 +04:00
|
|
|
default:
|
|
|
|
/*
|
2003-07-17 23:44:53 +04:00
|
|
|
* parent: we've set up the subprocess.
|
|
|
|
* forward tty signals to its process group.
|
1999-06-22 04:43:57 +04:00
|
|
|
*/
|
|
|
|
ttysig_forward = child;
|
|
|
|
ttysig_ignore = 0;
|
|
|
|
break;
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
2006-02-25 23:21:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now loop transferring program output to screen, and keyboard
|
|
|
|
* input to the program.
|
|
|
|
*/
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
FD_ZERO(&active_fd_set);
|
2003-07-28 15:32:21 +04:00
|
|
|
FD_SET(master, &active_fd_set);
|
1999-01-21 11:02:17 +03:00
|
|
|
FD_SET(STDIN_FILENO, &active_fd_set);
|
|
|
|
|
1999-03-22 12:02:47 +03:00
|
|
|
for (selectfailed = 0;;) {
|
|
|
|
if (selectfailed) {
|
2012-02-26 14:17:44 +04:00
|
|
|
const char mmsg[] =
|
|
|
|
"select(2) failed but no child died?";
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp)
|
2003-01-10 23:00:27 +03:00
|
|
|
(void)fprintf(logfp, mmsg);
|
|
|
|
errx(1, mmsg);
|
1999-03-22 12:02:47 +03:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
read_fd_set = active_fd_set;
|
2014-12-29 17:43:06 +03:00
|
|
|
tmo.tv_sec = flags & RUN_SILENT ? 20 : 2;
|
2003-07-07 16:30:19 +04:00
|
|
|
tmo.tv_usec = 0;
|
2006-02-25 23:21:00 +03:00
|
|
|
i = select(FD_SETSIZE, &read_fd_set, NULL, NULL, &tmo);
|
2014-12-30 13:10:22 +03:00
|
|
|
if (i == 0 && *actionwin == NULL && (flags & RUN_SILENT) == 0)
|
2006-02-25 23:21:00 +03:00
|
|
|
*actionwin = show_cmd(scmd, win);
|
|
|
|
if (i < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
warn("select");
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp)
|
2006-02-25 23:21:00 +03:00
|
|
|
(void)fprintf(logfp,
|
|
|
|
"select failure: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
selectfailed = 1;
|
|
|
|
}
|
1999-03-22 12:02:47 +03:00
|
|
|
} else for (i = 0; i < FD_SETSIZE; ++i) {
|
2003-08-09 23:26:38 +04:00
|
|
|
if (!FD_ISSET(i, &read_fd_set))
|
|
|
|
continue;
|
|
|
|
n = read(i, ibuf, sizeof ibuf - 1);
|
|
|
|
if (n <= 0) {
|
|
|
|
if (n < 0)
|
|
|
|
warn("read");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ibuf[n] = 0;
|
|
|
|
cp = ibuf;
|
|
|
|
if (i == STDIN_FILENO) {
|
|
|
|
(void)write(master, ibuf, (size_t)n);
|
|
|
|
if (!(rtt.c_lflag & ECHO))
|
2003-10-20 00:17:31 +04:00
|
|
|
continue;
|
2003-08-09 23:26:38 +04:00
|
|
|
} else {
|
|
|
|
pktdata = ibuf[0];
|
|
|
|
if (pktdata != 0) {
|
|
|
|
if (pktdata & TIOCPKT_IOCTL)
|
2003-10-20 00:17:31 +04:00
|
|
|
memcpy(&rtt, ibuf, sizeof(rtt));
|
|
|
|
continue;
|
2000-10-12 03:47:55 +04:00
|
|
|
}
|
2003-10-20 00:17:31 +04:00
|
|
|
cp += 1;
|
|
|
|
}
|
|
|
|
if (*cp == 0 || flags & RUN_SILENT)
|
|
|
|
continue;
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp) {
|
2003-10-20 00:17:31 +04:00
|
|
|
fprintf(logfp, "%s", cp);
|
|
|
|
fflush(logfp);
|
1999-01-21 11:02:17 +03:00
|
|
|
}
|
2003-10-20 00:17:31 +04:00
|
|
|
if (*actionwin == NULL)
|
|
|
|
*actionwin = show_cmd(scmd, win);
|
|
|
|
/* posix curses is braindead wrt \r\n so... */
|
|
|
|
for (ncp = cp; (ncp = strstr(ncp, "\r\n")); ncp += 2) {
|
|
|
|
ncp[0] = '\n';
|
|
|
|
ncp[1] = '\r';
|
|
|
|
}
|
|
|
|
waddstr(*actionwin, cp);
|
|
|
|
wrefresh(*actionwin);
|
1999-03-22 12:02:47 +03:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
pid = wait4(child, &status, WNOHANG, 0);
|
|
|
|
if (pid == child && (WIFEXITED(status) || WIFSIGNALED(status)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(master);
|
|
|
|
close(slave);
|
2012-01-06 01:29:24 +04:00
|
|
|
if (logfp)
|
2002-12-05 04:17:15 +03:00
|
|
|
fflush(logfp);
|
1999-01-21 11:02:17 +03:00
|
|
|
|
1999-06-22 04:43:57 +04:00
|
|
|
/* from here on out, we take tty signals ourselves */
|
|
|
|
ttysig_forward = 0;
|
|
|
|
|
2003-07-10 17:36:48 +04:00
|
|
|
reset_prog_mode();
|
1999-06-22 04:43:57 +04:00
|
|
|
|
2001-09-13 22:07:26 +04:00
|
|
|
if (WIFEXITED(status)) {
|
2003-10-20 00:17:31 +04:00
|
|
|
*errstr = msg_string(MSG_Command_failed);
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
*errstr = msg_string(MSG_Command_ended_on_signal);
|
|
|
|
return WTERMSIG(status);
|
|
|
|
}
|
|
|
|
return 0;
|
1997-09-27 03:02:53 +04:00
|
|
|
}
|
|
|
|
|
1997-11-02 06:45:27 +03:00
|
|
|
/*
|
2003-10-20 00:17:31 +04:00
|
|
|
* generic program runner.
|
|
|
|
* flags:
|
|
|
|
* RUN_DISPLAY display command name and output
|
|
|
|
* RUN_FATAL program errors are fatal
|
|
|
|
* RUN_CHROOT chroot to target before the exec
|
|
|
|
* RUN_FULLSCREEN display output only
|
|
|
|
* RUN_SILENT do not display program output
|
|
|
|
* RUN_ERROR_OK don't wait for key if program fails
|
|
|
|
* RUN_PROGRESS don't wait for key if program has output
|
|
|
|
* If both RUN_DISPLAY and RUN_SILENT are clear then the program name will
|
|
|
|
* be displayed as soon as it generates output.
|
|
|
|
* Steps are taken to collect console messages, they will be interleaved
|
|
|
|
* into the program output - but not upset curses.
|
1997-11-02 06:45:27 +03:00
|
|
|
*/
|
1999-01-21 11:02:17 +03:00
|
|
|
|
|
|
|
int
|
2003-11-30 17:36:43 +03:00
|
|
|
run_program(int flags, const char *cmd, ...)
|
1997-11-02 06:45:27 +03:00
|
|
|
{
|
|
|
|
va_list ap;
|
1999-01-21 11:02:17 +03:00
|
|
|
struct winsize win;
|
1997-11-02 06:45:27 +03:00
|
|
|
int ret;
|
2003-10-20 00:17:31 +04:00
|
|
|
WINDOW *actionwin = NULL;
|
2003-07-18 13:34:42 +04:00
|
|
|
char *scmd;
|
|
|
|
char **args;
|
2003-09-27 14:38:05 +04:00
|
|
|
const char *errstr = NULL;
|
1999-01-21 11:02:17 +03:00
|
|
|
|
2003-07-18 13:34:42 +04:00
|
|
|
va_start(ap, cmd);
|
|
|
|
vasprintf(&scmd, cmd, ap);
|
2006-02-25 23:21:00 +03:00
|
|
|
va_end(ap);
|
2003-07-18 13:34:42 +04:00
|
|
|
if (scmd == NULL)
|
|
|
|
err(1, "vasprintf(&scmd, \"%s\", ...)", cmd);
|
1999-01-21 11:02:17 +03:00
|
|
|
|
2003-07-18 13:34:42 +04:00
|
|
|
args = make_argv(scmd);
|
1999-01-21 11:02:17 +03:00
|
|
|
|
2003-07-10 17:36:48 +04:00
|
|
|
/* Make curses save tty settings */
|
|
|
|
def_prog_mode();
|
|
|
|
|
1999-01-21 11:02:17 +03:00
|
|
|
(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
|
1999-03-09 22:30:00 +03:00
|
|
|
/* Apparently, we sometimes get 0x0 back, and that's not useful */
|
|
|
|
if (win.ws_row == 0)
|
|
|
|
win.ws_row = 24;
|
|
|
|
if (win.ws_col == 0)
|
|
|
|
win.ws_col = 80;
|
|
|
|
|
2003-09-27 14:38:05 +04:00
|
|
|
if ((flags & RUN_DISPLAY) != 0) {
|
2003-10-20 00:17:31 +04:00
|
|
|
if (flags & RUN_FULLSCREEN) {
|
|
|
|
wclear(stdscr);
|
|
|
|
clearok(stdscr, 1);
|
|
|
|
touchwin(stdscr);
|
|
|
|
refresh();
|
|
|
|
actionwin = stdscr;
|
|
|
|
} else
|
|
|
|
actionwin = show_cmd(scmd, &win);
|
|
|
|
} else
|
2000-01-13 21:52:21 +03:00
|
|
|
win.ws_row -= 4;
|
1999-01-21 11:02:17 +03:00
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
ret = launch_subwin(&actionwin, args, &win, flags, scmd, &errstr);
|
2006-02-25 23:21:00 +03:00
|
|
|
fpurge(stdin);
|
2003-10-20 00:17:31 +04:00
|
|
|
|
2003-11-30 17:36:43 +03:00
|
|
|
/* If the command failed, show command name */
|
2005-02-26 20:40:49 +03:00
|
|
|
if (actionwin == NULL && ret != 0 && !(flags & RUN_ERROR_OK))
|
2003-10-20 00:17:31 +04:00
|
|
|
actionwin = show_cmd(scmd, &win);
|
|
|
|
|
|
|
|
if (actionwin != NULL) {
|
|
|
|
int y, x;
|
|
|
|
getyx(actionwin, y, x);
|
2004-06-06 01:18:59 +04:00
|
|
|
if (actionwin != stdscr)
|
|
|
|
mvaddstr(0, 4, msg_string(MSG_Status));
|
2003-10-20 00:17:31 +04:00
|
|
|
if (ret != 0) {
|
2004-06-06 01:18:59 +04:00
|
|
|
if (actionwin == stdscr && x != 0)
|
2003-10-20 00:17:31 +04:00
|
|
|
addstr("\n");
|
|
|
|
x = 1; /* force newline below */
|
2004-06-06 01:18:59 +04:00
|
|
|
standout();
|
|
|
|
addstr(errstr);
|
|
|
|
standend();
|
|
|
|
} else {
|
|
|
|
if (actionwin != stdscr) {
|
|
|
|
standout();
|
|
|
|
addstr(msg_string(MSG_Finished));
|
|
|
|
standend();
|
|
|
|
}
|
|
|
|
}
|
2007-10-09 22:43:26 +04:00
|
|
|
clrtoeol();
|
2003-10-20 00:17:31 +04:00
|
|
|
refresh();
|
2003-11-30 17:36:43 +03:00
|
|
|
if ((ret != 0 && !(flags & RUN_ERROR_OK)) ||
|
|
|
|
(y + x != 0 && !(flags & RUN_PROGRESS))) {
|
2003-10-20 00:17:31 +04:00
|
|
|
if (actionwin != stdscr)
|
2004-06-06 01:18:59 +04:00
|
|
|
move(getbegy(actionwin) - 2, 5);
|
2003-10-20 00:17:31 +04:00
|
|
|
else if (x != 0)
|
|
|
|
addstr("\n");
|
|
|
|
addstr(msg_string(MSG_Hit_enter_to_continue));
|
|
|
|
refresh();
|
|
|
|
getchar();
|
2004-06-06 01:18:59 +04:00
|
|
|
} else {
|
2005-02-26 20:40:49 +03:00
|
|
|
if (y + x != 0) {
|
2004-06-06 01:18:59 +04:00
|
|
|
/* give user 1 second to see messages */
|
|
|
|
refresh();
|
|
|
|
sleep(1);
|
2005-02-26 20:40:49 +03:00
|
|
|
}
|
2000-01-13 21:52:21 +03:00
|
|
|
}
|
2003-10-20 00:17:31 +04:00
|
|
|
}
|
1999-01-21 11:02:17 +03:00
|
|
|
|
2003-11-30 17:36:43 +03:00
|
|
|
/* restore tty setting we saved earlier */
|
|
|
|
reset_prog_mode();
|
|
|
|
|
2003-10-20 00:17:31 +04:00
|
|
|
/* clean things up */
|
|
|
|
if (actionwin != NULL) {
|
|
|
|
if (actionwin != stdscr)
|
|
|
|
delwin(actionwin);
|
2011-07-06 05:18:08 +04:00
|
|
|
if (errstr == 0 || !(flags & RUN_NO_CLEAR)) {
|
2003-11-30 17:36:43 +03:00
|
|
|
wclear(stdscr);
|
|
|
|
touchwin(stdscr);
|
|
|
|
clearok(stdscr, 1);
|
|
|
|
refresh();
|
2003-10-20 00:17:31 +04:00
|
|
|
}
|
1999-04-09 14:24:38 +04:00
|
|
|
}
|
2003-11-30 17:36:43 +03:00
|
|
|
|
2003-07-18 13:34:42 +04:00
|
|
|
free(scmd);
|
|
|
|
free_argv(args);
|
2003-11-30 17:36:43 +03:00
|
|
|
|
|
|
|
if (ret != 0 && flags & RUN_FATAL)
|
|
|
|
exit(ret);
|
2003-10-20 00:17:31 +04:00
|
|
|
return ret;
|
1997-11-02 06:45:27 +03:00
|
|
|
}
|