xref: /netbsd-src/usr.sbin/sysinst/run.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: run.c,v 1.13 2019/11/16 20:26:59 martin Exp $	*/
2 
3 /*
4  * Copyright 1997 Piermont Information Systems Inc.
5  * All rights reserved.
6  *
7  * Written by Philip A. Nelson for Piermont Information Systems Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18  *    or promote products derived from this software without specific prior
19  *    written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /* run.c -- routines to interact with other programs. */
36 
37 /* XXX write return codes ignored. XXX */
38 
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <curses.h>
46 #include <termios.h>
47 #include <dirent.h>
48 #include <util.h>
49 #include <signal.h>
50 #include <err.h>
51 #include <sys/ioctl.h>
52 #include <sys/types.h>
53 #include <sys/wait.h>
54 #include <sys/stat.h>
55 #include "defs.h"
56 
57 #include "menu_defs.h"
58 #include "msg_defs.h"
59 
60 #define MAXBUF 256
61 
62 #if defined(DEBUG) && defined(DEBUG_SYSTEM)
63 static inline int
64 Xsystem(const char *y)
65 {
66 	printf ("%s\n", y);
67 	return 0;
68 }
69 #else
70 #define Xsystem(y) system(y)
71 #endif
72 
73 /*
74  * local prototypes
75  */
76 int log_flip (menudesc *, void *);
77 static int script_flip (menudesc *, void *);
78 
79 #define BUFSIZE 4096
80 
81 menu_ent logmenu [2] = {
82 	{ .opt_action=log_flip},
83 	{ .opt_action=script_flip}
84 };
85 
86 static void
87 log_menu_label(menudesc *m, int opt, void *arg)
88 {
89 	wprintw(m->mw, "%s: %s",
90 		msg_string(opt ? MSG_Scripting : MSG_Logging),
91 		msg_string((opt ? script != NULL : logfp != NULL) ?
92 		    MSG_On : MSG_Off));
93 }
94 
95 void
96 do_logging(void)
97 {
98 	int menu_no;
99 
100 	menu_no = new_menu(MSG_Logging_functions, logmenu, 2, -1, 12,
101 		0, 20, MC_SCROLL, NULL, log_menu_label, NULL,
102 		MSG_Pick_an_option, MSG_exit_menu_generic);
103 
104 	if (menu_no < 0) {
105 		(void)fprintf(stderr, "Dynamic menu creation failed.\n");
106 		if (logfp)
107 			(void)fprintf(logfp, "Dynamic menu creation failed.\n");
108 		exit(EXIT_FAILURE);
109 	}
110 	process_menu(menu_no, NULL);
111 	free_menu(menu_no);
112 }
113 
114 int
115 /*ARGSUSED*/
116 log_flip(menudesc *m, void *arg)
117 {
118 	time_t tloc;
119 
120 	(void)time(&tloc);
121 	if (logfp) {
122 		fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
123 		fflush(logfp);
124 		fclose(logfp);
125 		logfp = NULL;
126 	} else {
127 		logfp = fopen("/tmp/sysinst.log", "a");
128 		if (logfp != NULL) {
129 			fprintf(logfp,
130 			    "Log started at: %s\n", safectime(&tloc));
131 			fflush(logfp);
132 		} else {
133 			if (mainwin) {
134 				msg_fmt_display(MSG_openfail, "%s%s",
135 				    "log file", strerror(errno));
136 			} else {
137 				fprintf(stderr, "could not open /tmp/sysinst.log: %s\n",
138 				    strerror(errno));
139 				exit(1);
140 			}
141 		}
142 	}
143 	return(0);
144 }
145 
146 static int
147 /*ARGSUSED*/
148 script_flip(menudesc *m, void *arg)
149 {
150 	time_t tloc;
151 
152 	(void)time(&tloc);
153 	if (script) {
154 		scripting_fprintf(NULL, "# Script ended at: %s\n",
155 		    safectime(&tloc));
156 		fflush(script);
157 		fclose(script);
158 		script = NULL;
159 	} else {
160 		script = fopen("/tmp/sysinst.sh", "w");
161 		if (script != NULL) {
162 			scripting_fprintf(NULL, "#!/bin/sh\n");
163 			scripting_fprintf(NULL, "# Script started at: %s\n",
164 			    safectime(&tloc));
165 			fflush(script);
166 		} else {
167 			msg_fmt_display(MSG_openfail, "%s%s", "script file",
168 			    strerror(errno));
169 		}
170 	}
171 	return(0);
172 }
173 
174 int
175 collect(int kind, char **buffer, const char *name, ...)
176 {
177 	size_t nbytes;		/* Number of bytes in buffer. */
178 	size_t fbytes;		/* Number of bytes in file. */
179 	struct stat st;		/* stat information. */
180 	int ch;
181 	FILE *f;
182 	char fileorcmd[STRSIZE];
183 	va_list ap;
184 	char *cp;
185 
186 	va_start(ap, name);
187 	vsnprintf(fileorcmd, sizeof fileorcmd, name, ap);
188 	va_end(ap);
189 
190 	if (kind == T_FILE) {
191 		/* Get the file information. */
192 		if (stat(fileorcmd, &st)) {
193 			*buffer = NULL;
194 			return -1;
195 		}
196 		fbytes = (size_t)st.st_size;
197 
198 		/* Open the file. */
199 		f = fopen(fileorcmd, "r");
200 		if (f == NULL) {
201 			*buffer = NULL;
202 			return -1;
203 		}
204 	} else {
205 		/* Open the program. */
206 		f = popen(fileorcmd, "r");
207 		if (f == NULL) {
208 			*buffer = NULL;
209 			return -1;
210 		}
211 		fbytes = BUFSIZE;
212 	}
213 
214 	if (fbytes == 0)
215 		fbytes = BUFSIZE;
216 
217 	/* Allocate the buffer size. */
218 	*buffer = cp = malloc(fbytes + 1);
219 	if (!cp)
220 		nbytes =  -1;
221 	else {
222 		/* Read the buffer. */
223 		nbytes = 0;
224 		while (nbytes < fbytes && (ch = fgetc(f)) != EOF)
225 			cp[nbytes++] = ch;
226 		cp[nbytes] = 0;
227 	}
228 
229 	if (kind == T_FILE)
230 		fclose(f);
231 	else
232 		pclose(f);
233 
234 	return nbytes;
235 }
236 
237 
238 /*
239  * system(3), but with a debug wrapper.
240  * use only for curses sub-applications.
241  */
242 int
243 do_system(const char *execstr)
244 {
245 	register int ret;
246 
247 	/*
248 	 * The following may be more than one function call.  Can't just
249 	 * "return Xsystem (command);"
250 	 */
251 
252 	ret = Xsystem(execstr);
253 	return (ret);
254 
255 }
256 
257 static char **
258 make_argv(const char *cmd)
259 {
260 	char **argv = 0;
261 	int argc = 0;
262 	const char *cp;
263 	char *dp, *fn;
264 	DIR *dir;
265 	struct dirent *dirent;
266 	int l;
267 
268 	for (; *cmd != 0; cmd = cp + strspn(cp, " "), argc++) {
269 		if (*cmd == '\'')
270 			cp = strchr(++cmd, '\'');
271 		else
272 			cp = strchr(cmd, ' ');
273 		if (cp == NULL)
274 			cp = strchr(cmd, 0);
275 		argv = realloc(argv, (argc + 2) * sizeof *argv);
276 		if (argv == NULL)
277 			err(1, "realloc(argv) for %s", cmd);
278 		asprintf(argv + argc, "%.*s", (int)(cp - cmd), cmd);
279 		/* Hack to remove %xx encoded ftp password */
280 		dp = strstr(cmd, ":%");
281 		if (dp != NULL && dp < cp) {
282 			for (fn = dp + 4; *fn == '%'; fn += 3)
283 				continue;
284 			if (*fn == '@')
285 				memset(dp + 1, '*', fn - dp - 1);
286 		}
287 		if (*cp == '\'')
288 			cp++;
289 		if (cp[-1] != '*')
290 			continue;
291 		/* do limited filename globbing */
292 		dp = argv[argc];
293 		fn = strrchr(dp, '/');
294 		if (fn != NULL)
295 			*fn = 0;
296 		dir = opendir(dp);
297 		if (fn != NULL)
298 			*fn++ = '/';
299 		else
300 			fn = dp;
301 		if (dir == NULL)
302 			continue;
303 		l = strlen(fn) - 1;
304 		while ((dirent = readdir(dir))) {
305 			if (dirent->d_name[0] == '.')
306 				continue;
307 			if (strncmp(dirent->d_name, fn, l) != 0)
308 				continue;
309 			if (dp != argv[argc])
310 				argc++;
311 			argv = realloc(argv, (argc + 2) * sizeof *argv);
312 			if (argv == NULL)
313 				err(1, "realloc(argv) for %s", cmd);
314 			asprintf(argv + argc, "%.*s%s", (int)(fn - dp), dp,
315 				dirent->d_name);
316 		}
317 		if (dp != argv[argc])
318 			free(dp);
319 		closedir(dir);
320 	}
321 	argv[argc] = NULL;
322 	return argv;
323 }
324 
325 static void
326 free_argv(char **argv)
327 {
328 	char **n, *a;
329 
330 	for (n = argv; (a = *n++);)
331 		free(a);
332 	free(argv);
333 }
334 
335 static WINDOW *
336 show_cmd(const char *scmd, struct winsize *win)
337 {
338 	WINDOW *actionwin;
339 	int nrow;
340 
341 	wclear(stdscr);
342 	clearok(stdscr, 1);
343 	touchwin(stdscr);
344 	refresh();
345 
346 	mvaddstr(0, 4, msg_string(MSG_Status));
347 	standout();
348 	addstr(msg_string(MSG_Running));
349 	standend();
350 	mvaddstr(1, 4, msg_string(MSG_Command));
351 	standout();
352 	printw("%s", scmd);
353 	standend();
354 	addstr("\n\n");
355 	hline(0, win->ws_col);
356 	refresh();
357 
358 	nrow = getcury(stdscr) + 1;
359 
360 	actionwin = subwin(stdscr, win->ws_row - nrow, win->ws_col, nrow, 0);
361 	if (actionwin == NULL) {
362 		fprintf(stderr, "sysinst: failed to allocate output window.\n");
363 		exit(1);
364 	}
365 	scrollok(actionwin, TRUE);
366 	if (has_colors()) {
367 		wbkgd(actionwin, getbkgd(stdscr));
368 		wattrset(actionwin, getattrs(stdscr));
369 	}
370 
371 	wmove(actionwin, 0, 0);
372 	wrefresh(actionwin);
373 
374 	return actionwin;
375 }
376 
377 /*
378  * launch a program inside a subwindow, and report its return status when done
379  */
380 static int
381 launch_subwin(WINDOW **actionwin, char **args, struct winsize *win, int flags,
382     const char *scmd, const char **errstr)
383 {
384 	int n, i;
385 	int selectfailed;
386 	int status, master, slave;
387 	fd_set active_fd_set, read_fd_set;
388 	pid_t child, pid;
389 	char ibuf[MAXBUF];
390 	char pktdata;
391 	char *cp, *ncp;
392 	struct termios rtt, tt;
393 	struct timeval tmo;
394 	static int do_tioccons = 2;
395 
396 	(void)tcgetattr(STDIN_FILENO, &tt);
397 	if (openpty(&master, &slave, NULL, &tt, win) == -1) {
398 		*errstr = "openpty() failed";
399 		return -1;
400 	}
401 
402 	rtt = tt;
403 
404 	/* ignore tty signals until we're done with subprocess setup */
405 	ttysig_ignore = 1;
406 	ioctl(master, TIOCPKT, &ttysig_ignore);
407 
408 	/* Try to get console output into our pipe */
409 	if (do_tioccons) {
410 		if (ioctl(slave, TIOCCONS, &do_tioccons) == 0
411 		    && do_tioccons == 2) {
412 			/* test our output - we don't want it grabbed */
413 			write(1, " \b", 2);
414 			ioctl(master, FIONREAD, &do_tioccons);
415 			if (do_tioccons != 0) {
416 				do_tioccons = 0;
417 				ioctl(slave, TIOCCONS, &do_tioccons);
418 			} else
419 				do_tioccons = 1;
420 		}
421 	}
422 
423 	if (logfp)
424 		fflush(logfp);
425 	if (script)
426 		fflush(script);
427 
428 	child = fork();
429 	switch (child) {
430 	case -1:
431 		ttysig_ignore = 0;
432 		refresh();
433 		*errstr = "fork() failed";
434 		return -1;
435 	case 0:	/* child */
436 		(void)close(STDIN_FILENO);
437 		/* silently stop curses */
438 		(void)close(STDOUT_FILENO);
439 		(void)open("/dev/null", O_RDWR, 0);
440 		dup2(STDIN_FILENO, STDOUT_FILENO);
441 		endwin();
442 		(void)close(master);
443 		rtt = tt;
444 		rtt.c_lflag |= (ICANON|ECHO);
445 		(void)tcsetattr(slave, TCSANOW, &rtt);
446 		login_tty(slave);
447 		if (logfp) {
448 			fprintf(logfp, "executing: %s\n", scmd);
449 			fclose(logfp);
450 			logfp = NULL;
451 		}
452 		if (script) {
453 			fprintf(script, "%s\n", scmd);
454 			fclose(script);
455 			script = NULL;
456 		}
457 		if (strcmp(args[0], "cd") == 0 && strcmp(args[2], "&&") == 0) {
458 			target_chdir_or_die(args[1]);
459 			args += 3;
460 		}
461 		if (flags & RUN_XFER_DIR)
462 			target_chdir_or_die(xfer_dir);
463 		/*
464 		 * If target_prefix == "", the chroot will fail, but
465 		 * that's ok, since we don't need it then.
466 		 */
467 		if (flags & RUN_CHROOT && *target_prefix()
468 		    && chroot(target_prefix()) != 0)
469 			warn("chroot(%s) for %s", target_prefix(), *args);
470 		else {
471 			execvp(*args, args);
472 			warn("execvp %s", *args);
473 		}
474 		_exit(EXIT_FAILURE);
475 		// break; /* end of child */
476 	default:
477 		/*
478 		 * parent: we've set up the subprocess.
479 		 * forward tty signals to its process group.
480 		 */
481 		ttysig_forward = child;
482 		ttysig_ignore = 0;
483 		break;
484 	}
485 
486 	/*
487 	 * Now loop transferring program output to screen, and keyboard
488 	 * input to the program.
489 	 */
490 
491 	FD_ZERO(&active_fd_set);
492 	FD_SET(master, &active_fd_set);
493 	FD_SET(STDIN_FILENO, &active_fd_set);
494 
495 	for (selectfailed = 0;;) {
496 		if (selectfailed) {
497 			const char mmsg[] =
498 			    "select(2) failed but no child died?";
499 			if (logfp)
500 				(void)fprintf(logfp, mmsg);
501 			errx(1, mmsg);
502 		}
503 		read_fd_set = active_fd_set;
504 		tmo.tv_sec = flags & RUN_SILENT ? 20 : 2;
505 		tmo.tv_usec = 0;
506 		i = select(FD_SETSIZE, &read_fd_set, NULL, NULL, &tmo);
507 		if (i == 0 && *actionwin == NULL && (flags & RUN_SILENT) == 0)
508 			*actionwin = show_cmd(scmd, win);
509 		if (i < 0) {
510 			if (errno != EINTR) {
511 				warn("select");
512 				if (logfp)
513 					(void)fprintf(logfp,
514 					    "select failure: %s\n",
515 					    strerror(errno));
516 				selectfailed = 1;
517 			}
518 		} else for (i = 0; i < FD_SETSIZE; ++i) {
519 			if (!FD_ISSET(i, &read_fd_set))
520 				continue;
521 			n = read(i, ibuf, sizeof ibuf - 1);
522 			if (n <= 0) {
523 				if (n < 0)
524 					warn("read");
525 				continue;
526 			}
527 			ibuf[n] = 0;
528 			cp = ibuf;
529 			if (i == STDIN_FILENO) {
530 				(void)write(master, ibuf, (size_t)n);
531 				if (!(rtt.c_lflag & ECHO))
532 					continue;
533 			} else {
534 				pktdata = ibuf[0];
535 				if (pktdata != 0) {
536 					if (pktdata & TIOCPKT_IOCTL)
537 						memcpy(&rtt, ibuf, sizeof(rtt));
538 					continue;
539 				}
540 				cp += 1;
541 			}
542 			if (*cp == 0 || flags & RUN_SILENT)
543 				continue;
544 			if (logfp) {
545 				fprintf(logfp, "%s", cp);
546 				fflush(logfp);
547 			}
548 			if (*actionwin == NULL)
549 				*actionwin = show_cmd(scmd, win);
550 			/* posix curses is braindead wrt \r\n so... */
551 			for (ncp = cp; (ncp = strstr(ncp, "\r\n")); ncp += 2) {
552 				ncp[0] = '\n';
553 				ncp[1] = '\r';
554 			}
555 			waddstr(*actionwin, cp);
556 			wrefresh(*actionwin);
557 		}
558 		pid = wait4(child, &status, WNOHANG, 0);
559  		if (pid == child && (WIFEXITED(status) || WIFSIGNALED(status)))
560 			break;
561 	}
562 	close(master);
563 	close(slave);
564 	if (logfp)
565 		fflush(logfp);
566 
567 	/* from here on out, we take tty signals ourselves */
568 	ttysig_forward = 0;
569 
570 	reset_prog_mode();
571 
572 	if (WIFEXITED(status)) {
573 		*errstr = msg_string(MSG_Command_failed);
574 		return WEXITSTATUS(status);
575 	}
576 	if (WIFSIGNALED(status)) {
577 		*errstr = msg_string(MSG_Command_ended_on_signal);
578 		return WTERMSIG(status);
579 	}
580 	return 0;
581 }
582 
583 /*
584  * generic program runner.
585  * flags:
586  *	RUN_DISPLAY	display command name and output
587  *	RUN_FATAL	program errors are fatal
588  *	RUN_CHROOT	chroot to target before the exec
589  *	RUN_FULLSCREEN	display output only
590  *	RUN_SILENT	do not display program output
591  *	RUN_ERROR_OK	don't wait for key if program fails
592  *	RUN_PROGRESS	don't wait for key if program has output
593  * If both RUN_DISPLAY and RUN_SILENT are clear then the program name will
594  * be displayed as soon as it generates output.
595  * Steps are taken to collect console messages, they will be interleaved
596  * into the program output - but not upset curses.
597  */
598 
599 int
600 run_program(int flags, const char *cmd, ...)
601 {
602 	va_list ap;
603 	struct winsize win;
604 	int ret;
605 	WINDOW *actionwin = NULL;
606 	char *scmd;
607 	char **args;
608 	const char *errstr = NULL;
609 
610 	va_start(ap, cmd);
611 	vasprintf(&scmd, cmd, ap);
612 	va_end(ap);
613 	if (scmd == NULL)
614 		err(1, "vasprintf(&scmd, \"%s\", ...)", cmd);
615 
616 	args = make_argv(scmd);
617 
618 	/* Make curses save tty settings */
619 	def_prog_mode();
620 
621 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
622 	/* Apparently, we sometimes get 0x0 back, and that's not useful */
623 	if (win.ws_row == 0)
624 		win.ws_row = 24;
625 	if (win.ws_col == 0)
626 		win.ws_col = 80;
627 
628 	if ((flags & RUN_DISPLAY) != 0) {
629 		if (flags & RUN_FULLSCREEN) {
630 			wclear(stdscr);
631 			clearok(stdscr, 1);
632 			touchwin(stdscr);
633 			refresh();
634 			actionwin = stdscr;
635 		} else
636 			actionwin = show_cmd(scmd, &win);
637 	} else
638 		win.ws_row -= 4;
639 
640 	ret = launch_subwin(&actionwin, args, &win, flags, scmd, &errstr);
641 	fpurge(stdin);
642 
643 	/* If the command failed, show command name */
644 	if (actionwin == NULL && ret != 0 && !(flags & RUN_ERROR_OK))
645 		actionwin = show_cmd(scmd, &win);
646 
647 	if (actionwin != NULL) {
648 		int y, x;
649 		getyx(actionwin, y, x);
650 		if (actionwin != stdscr)
651 			mvaddstr(0, 4, msg_string(MSG_Status));
652 		if (ret != 0) {
653 			if (actionwin == stdscr && x != 0)
654 				addstr("\n");
655 			x = 1;	/* force newline below */
656 			standout();
657 			addstr(errstr);
658 			standend();
659 		} else {
660 			if (actionwin != stdscr) {
661 				standout();
662 				addstr(msg_string(MSG_Finished));
663 				standend();
664 			}
665 		}
666 		clrtoeol();
667 		refresh();
668 		if ((ret != 0 && !(flags & RUN_ERROR_OK)) ||
669 		    (y + x != 0 && !(flags & RUN_PROGRESS))) {
670 			if (actionwin != stdscr)
671 				move(getbegy(actionwin) - 2, 5);
672 			else if (x != 0)
673 				addstr("\n");
674 			addstr(msg_string(MSG_Hit_enter_to_continue));
675 			refresh();
676 			getchar();
677 		} else {
678 			if (y + x != 0) {
679 				/* give user 1 second to see messages */
680 				refresh();
681 				sleep(1);
682 			}
683 		}
684 	}
685 
686 	/* restore tty setting we saved earlier */
687 	reset_prog_mode();
688 
689 	/* clean things up */
690 	if (actionwin != NULL) {
691 		if (actionwin != stdscr)
692 			delwin(actionwin);
693 		if (errstr == 0 || !(flags & RUN_NO_CLEAR)) {
694 			wclear(stdscr);
695 			touchwin(stdscr);
696 			clearok(stdscr, 1);
697 			refresh();
698 		}
699 	}
700 
701 	free(scmd);
702 	free_argv(args);
703 
704 	if (ret != 0 && flags & RUN_FATAL)
705 		exit(ret);
706 	return ret;
707 }
708