run.c revision 1.16 1 /* $NetBSD: run.c,v 1.16 2024/10/04 15:11:09 rillig 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 size_t abytes; /* allocated size of buffer */
180 struct stat st; /* stat information. */
181 int ch;
182 FILE *f;
183 char fileorcmd[STRSIZE];
184 va_list ap;
185 char *cp;
186
187 va_start(ap, name);
188 vsnprintf(fileorcmd, sizeof fileorcmd, name, ap);
189 va_end(ap);
190
191 if (kind == T_FILE) {
192 /* Get the file information. */
193 if (stat(fileorcmd, &st)) {
194 *buffer = NULL;
195 return -1;
196 }
197 fbytes = (size_t)st.st_size;
198
199 /* Open the file. */
200 f = fopen(fileorcmd, "r");
201 if (f == NULL) {
202 if (logfp)
203 fprintf(logfp, "%s: failed to open %s\n",
204 __func__, fileorcmd);
205 *buffer = NULL;
206 return -1;
207 }
208 } else {
209 /* Open the program. */
210 f = popen(fileorcmd, "r");
211 if (f == NULL) {
212 if (logfp)
213 fprintf(logfp, "%s: failed to open %s\n",
214 __func__, fileorcmd);
215 *buffer = NULL;
216 return -1;
217 }
218 fbytes = 0;
219 }
220
221 if (fbytes == 0)
222 abytes = BUFSIZE;
223 else
224 abytes = fbytes+1;
225
226 /* Allocate the buffer size. */
227 *buffer = cp = malloc(abytes);
228 if (!cp)
229 nbytes = -1;
230 else {
231 /* Read the buffer. */
232 nbytes = 0;
233 while ((ch = fgetc(f)) != EOF) {
234 if (nbytes >= abytes-1) {
235 if (fbytes > 0 || abytes >= 512*BUFSIZE) {
236 free(cp);
237 *buffer = cp = NULL;
238 nbytes = -1;
239 break;
240 }
241 abytes *= 2;
242 *buffer = cp = realloc(cp, abytes);
243 if (!cp) {
244 nbytes = -1;
245 break;
246 }
247
248 }
249 cp[nbytes++] = ch;
250 }
251 if (cp)
252 cp[nbytes] = 0;
253 }
254
255 if (kind == T_FILE)
256 fclose(f);
257 else
258 pclose(f);
259
260 if (nbytes <= 0 && logfp)
261 fprintf(logfp, "%s: failed for %s\n", __func__, fileorcmd);
262
263 return nbytes;
264 }
265
266
267 /*
268 * system(3), but with a debug wrapper.
269 * use only for curses sub-applications.
270 */
271 int
272 do_system(const char *execstr)
273 {
274 register int ret;
275
276 /*
277 * The following may be more than one function call. Can't just
278 * "return Xsystem (command);"
279 */
280
281 ret = Xsystem(execstr);
282 return (ret);
283
284 }
285
286 static char **
287 make_argv(char *cmd)
288 {
289 char **argv = 0;
290 int argc = 0;
291 char *cp, *dp, *fn;
292 DIR *dir;
293 struct dirent *dirent;
294 int l;
295
296 for (; *cmd != 0; cmd = cp + strspn(cp, " "), argc++) {
297 if (*cmd == '\'')
298 cp = strchr(++cmd, '\'');
299 else
300 cp = strchr(cmd, ' ');
301 if (cp == NULL)
302 cp = strchr(cmd, 0);
303 argv = realloc(argv, (argc + 2) * sizeof *argv);
304 if (argv == NULL)
305 err(1, "realloc(argv) for %s", cmd);
306 asprintf(argv + argc, "%.*s", (int)(cp - cmd), cmd);
307 /* Hack to remove %xx encoded ftp password */
308 dp = strstr(cmd, ":%");
309 if (dp != NULL && dp < cp) {
310 for (fn = dp + 4; *fn == '%'; fn += 3)
311 continue;
312 if (*fn == '@')
313 memset(dp + 1, '*', fn - dp - 1);
314 }
315 if (*cp == '\'')
316 cp++;
317 if (cp[-1] != '*')
318 continue;
319 /* do limited filename globbing */
320 dp = argv[argc];
321 fn = strrchr(dp, '/');
322 if (fn != NULL)
323 *fn = 0;
324 dir = opendir(dp);
325 if (fn != NULL)
326 *fn++ = '/';
327 else
328 fn = dp;
329 if (dir == NULL)
330 continue;
331 l = strlen(fn) - 1;
332 while ((dirent = readdir(dir))) {
333 if (dirent->d_name[0] == '.')
334 continue;
335 if (strncmp(dirent->d_name, fn, l) != 0)
336 continue;
337 if (dp != argv[argc])
338 argc++;
339 argv = realloc(argv, (argc + 2) * sizeof *argv);
340 if (argv == NULL)
341 err(1, "realloc(argv) for %s", cmd);
342 asprintf(argv + argc, "%.*s%s", (int)(fn - dp), dp,
343 dirent->d_name);
344 }
345 if (dp != argv[argc])
346 free(dp);
347 closedir(dir);
348 }
349 argv[argc] = NULL;
350 return argv;
351 }
352
353 static void
354 free_argv(char **argv)
355 {
356 char **n, *a;
357
358 for (n = argv; (a = *n++);)
359 free(a);
360 free(argv);
361 }
362
363 static WINDOW *
364 show_cmd(const char *scmd, struct winsize *win)
365 {
366 WINDOW *actionwin;
367 int nrow;
368
369 wclear(stdscr);
370 clearok(stdscr, 1);
371 touchwin(stdscr);
372 refresh();
373
374 mvaddstr(0, 4, msg_string(MSG_Status));
375 standout();
376 addstr(msg_string(MSG_Running));
377 standend();
378 mvaddstr(1, 4, msg_string(MSG_Command));
379 standout();
380 printw("%s", scmd);
381 standend();
382 addstr("\n\n");
383 hline(0, win->ws_col);
384 refresh();
385
386 nrow = getcury(stdscr) + 1;
387
388 actionwin = subwin(stdscr, win->ws_row - nrow, win->ws_col, nrow, 0);
389 if (actionwin == NULL) {
390 fprintf(stderr, "sysinst: failed to allocate output window.\n");
391 exit(1);
392 }
393 scrollok(actionwin, TRUE);
394 if (has_colors()) {
395 wbkgd(actionwin, getbkgd(stdscr));
396 wattrset(actionwin, getattrs(stdscr));
397 }
398
399 wmove(actionwin, 0, 0);
400 wrefresh(actionwin);
401
402 return actionwin;
403 }
404
405 /*
406 * launch a program inside a subwindow, and report its return status when done
407 */
408 static int
409 launch_subwin(WINDOW **actionwin, char **args, struct winsize *win, int flags,
410 const char *scmd, const char **errstr)
411 {
412 int n, i;
413 int selectfailed;
414 int status, master, slave;
415 fd_set active_fd_set, read_fd_set;
416 pid_t child, pid;
417 char ibuf[MAXBUF];
418 char pktdata;
419 char *cp, *ncp;
420 struct termios rtt, tt;
421 struct timeval tmo;
422 static int do_tioccons = 2;
423
424 (void)tcgetattr(STDIN_FILENO, &tt);
425 if (openpty(&master, &slave, NULL, &tt, win) == -1) {
426 *errstr = "openpty() failed";
427 return -1;
428 }
429
430 rtt = tt;
431
432 /* ignore tty signals until we're done with subprocess setup */
433 ttysig_ignore = 1;
434 ioctl(master, TIOCPKT, &ttysig_ignore);
435
436 /* Try to get console output into our pipe */
437 if (do_tioccons) {
438 if (ioctl(slave, TIOCCONS, &do_tioccons) == 0
439 && do_tioccons == 2) {
440 /* test our output - we don't want it grabbed */
441 write(1, " \b", 2);
442 ioctl(master, FIONREAD, &do_tioccons);
443 if (do_tioccons != 0) {
444 do_tioccons = 0;
445 ioctl(slave, TIOCCONS, &do_tioccons);
446 } else
447 do_tioccons = 1;
448 }
449 }
450
451 if (logfp)
452 fflush(logfp);
453 if (script)
454 fflush(script);
455
456 child = fork();
457 switch (child) {
458 case -1:
459 ttysig_ignore = 0;
460 refresh();
461 *errstr = "fork() failed";
462 return -1;
463 case 0: /* child */
464 (void)close(STDIN_FILENO);
465 /* silently stop curses */
466 (void)close(STDOUT_FILENO);
467 (void)open("/dev/null", O_RDWR, 0);
468 dup2(STDIN_FILENO, STDOUT_FILENO);
469 endwin();
470 (void)close(master);
471 rtt = tt;
472 rtt.c_lflag |= (ICANON|ECHO);
473 (void)tcsetattr(slave, TCSANOW, &rtt);
474 login_tty(slave);
475 if (logfp) {
476 fprintf(logfp, "executing: %s\n", scmd);
477 fclose(logfp);
478 logfp = NULL;
479 }
480 if (script) {
481 fprintf(script, "%s\n", scmd);
482 fclose(script);
483 script = NULL;
484 }
485 if (strcmp(args[0], "cd") == 0 && strcmp(args[2], "&&") == 0) {
486 target_chdir_or_die(args[1]);
487 args += 3;
488 }
489 if (flags & RUN_XFER_DIR)
490 target_chdir_or_die(xfer_dir);
491 /*
492 * If target_prefix == "", the chroot will fail, but
493 * that's ok, since we don't need it then.
494 */
495 if (flags & RUN_CHROOT && *target_prefix()
496 && chroot(target_prefix()) != 0)
497 warn("chroot(%s) for %s", target_prefix(), *args);
498 else {
499 execvp(*args, args);
500 warn("execvp %s", *args);
501 }
502 _exit(EXIT_FAILURE);
503 // break; /* end of child */
504 default:
505 /*
506 * parent: we've set up the subprocess.
507 * forward tty signals to its process group.
508 */
509 ttysig_forward = child;
510 ttysig_ignore = 0;
511 break;
512 }
513
514 /*
515 * Now loop transferring program output to screen, and keyboard
516 * input to the program.
517 */
518
519 FD_ZERO(&active_fd_set);
520 FD_SET(master, &active_fd_set);
521 FD_SET(STDIN_FILENO, &active_fd_set);
522
523 for (selectfailed = 0;;) {
524 if (selectfailed) {
525 const char mmsg[] =
526 "select(2) failed but no child died?";
527 if (logfp)
528 (void)fprintf(logfp, mmsg);
529 errx(1, mmsg);
530 }
531 read_fd_set = active_fd_set;
532 tmo.tv_sec = flags & RUN_SILENT ? 20 : 2;
533 tmo.tv_usec = 0;
534 i = select(FD_SETSIZE, &read_fd_set, NULL, NULL, &tmo);
535 if (i == 0 && *actionwin == NULL && (flags & RUN_SILENT) == 0)
536 *actionwin = show_cmd(scmd, win);
537 if (i < 0) {
538 if (errno != EINTR) {
539 warn("select");
540 if (logfp)
541 (void)fprintf(logfp,
542 "select failure: %s\n",
543 strerror(errno));
544 selectfailed = 1;
545 }
546 } else for (i = 0; i < FD_SETSIZE; ++i) {
547 if (!FD_ISSET(i, &read_fd_set))
548 continue;
549 n = read(i, ibuf, sizeof ibuf - 1);
550 if (n <= 0) {
551 if (n < 0)
552 warn("read");
553 continue;
554 }
555 ibuf[n] = 0;
556 cp = ibuf;
557 if (i == STDIN_FILENO) {
558 (void)write(master, ibuf, (size_t)n);
559 if (!(rtt.c_lflag & ECHO))
560 continue;
561 } else {
562 pktdata = ibuf[0];
563 if (pktdata != 0) {
564 if (pktdata & TIOCPKT_IOCTL)
565 memcpy(&rtt, ibuf, sizeof(rtt));
566 continue;
567 }
568 cp += 1;
569 }
570 if (*cp == 0 || flags & RUN_SILENT)
571 continue;
572 if (logfp) {
573 fprintf(logfp, "%s", cp);
574 fflush(logfp);
575 }
576 if (*actionwin == NULL)
577 *actionwin = show_cmd(scmd, win);
578 /* posix curses is braindead wrt \r\n so... */
579 for (ncp = cp; (ncp = strstr(ncp, "\r\n")); ncp += 2) {
580 ncp[0] = '\n';
581 ncp[1] = '\r';
582 }
583 waddstr(*actionwin, cp);
584 wrefresh(*actionwin);
585 }
586 pid = wait4(child, &status, WNOHANG, 0);
587 if (pid == child && (WIFEXITED(status) || WIFSIGNALED(status)))
588 break;
589 }
590 close(master);
591 close(slave);
592 if (logfp)
593 fflush(logfp);
594
595 /* from here on out, we take tty signals ourselves */
596 ttysig_forward = 0;
597
598 reset_prog_mode();
599
600 if (WIFEXITED(status)) {
601 *errstr = msg_string(MSG_Command_failed);
602 return WEXITSTATUS(status);
603 }
604 if (WIFSIGNALED(status)) {
605 *errstr = msg_string(MSG_Command_ended_on_signal);
606 return WTERMSIG(status);
607 }
608 return 0;
609 }
610
611 /*
612 * generic program runner.
613 * flags:
614 * RUN_DISPLAY display command name and output
615 * RUN_FATAL program errors are fatal
616 * RUN_CHROOT chroot to target before the exec
617 * RUN_FULLSCREEN display output only
618 * RUN_SILENT do not display program output
619 * RUN_ERROR_OK don't wait for key if program fails
620 * RUN_PROGRESS don't wait for key if program has output
621 * If both RUN_DISPLAY and RUN_SILENT are clear then the program name will
622 * be displayed as soon as it generates output.
623 * Steps are taken to collect console messages, they will be interleaved
624 * into the program output - but not upset curses.
625 */
626
627 int
628 run_program(int flags, const char *cmd, ...)
629 {
630 va_list ap;
631 struct winsize win;
632 int ret;
633 WINDOW *actionwin = NULL;
634 char *scmd;
635 char **args;
636 const char *errstr = NULL;
637
638 va_start(ap, cmd);
639 vasprintf(&scmd, cmd, ap);
640 va_end(ap);
641 if (scmd == NULL)
642 err(1, "vasprintf(&scmd, \"%s\", ...)", cmd);
643
644 args = make_argv(scmd);
645
646 /* Make curses save tty settings */
647 def_prog_mode();
648
649 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
650 /* Apparently, we sometimes get 0x0 back, and that's not useful */
651 if (win.ws_row == 0)
652 win.ws_row = 24;
653 if (win.ws_col == 0)
654 win.ws_col = 80;
655
656 if ((flags & RUN_DISPLAY) != 0) {
657 if (flags & RUN_STDSCR) {
658 actionwin = stdscr;
659 wmove(stdscr, msg_row()+2, 0);
660 wrefresh(stdscr);
661 } else if (flags & RUN_FULLSCREEN) {
662 wclear(stdscr);
663 clearok(stdscr, 1);
664 touchwin(stdscr);
665 refresh();
666 actionwin = stdscr;
667 } else {
668 actionwin = show_cmd(scmd, &win);
669 }
670 } else
671 win.ws_row -= 4;
672
673 ret = launch_subwin(&actionwin, args, &win, flags, scmd, &errstr);
674 fpurge(stdin);
675
676 /* If the command failed, show command name */
677 if (actionwin == NULL && ret != 0 && !(flags & RUN_ERROR_OK))
678 actionwin = show_cmd(scmd, &win);
679
680 if (actionwin != NULL) {
681 int y, x;
682 getyx(actionwin, y, x);
683 if (actionwin != stdscr)
684 mvaddstr(0, 4, msg_string(MSG_Status));
685 if (ret != 0) {
686 if (actionwin == stdscr && x != 0)
687 addstr("\n");
688 x = 1; /* force newline below */
689 standout();
690 addstr(errstr);
691 standend();
692 } else {
693 if (actionwin != stdscr) {
694 standout();
695 addstr(msg_string(MSG_Finished));
696 standend();
697 }
698 }
699 clrtoeol();
700 refresh();
701 if ((ret != 0 && !(flags & RUN_ERROR_OK)) ||
702 (y + x != 0 && !(flags & RUN_PROGRESS))) {
703 if (actionwin != stdscr)
704 move(getbegy(actionwin) - 2, 5);
705 else if (x != 0)
706 addstr("\n");
707 addstr(msg_string(MSG_Hit_enter_to_continue));
708 refresh();
709 getchar();
710 } else {
711 if (y + x != 0) {
712 /* give user 1 second to see messages */
713 refresh();
714 sleep(1);
715 }
716 }
717 }
718
719 /* restore tty setting we saved earlier */
720 reset_prog_mode();
721
722 /* clean things up */
723 if (actionwin != NULL) {
724 if (actionwin != stdscr)
725 delwin(actionwin);
726 if (errstr == 0 || !(flags & RUN_NO_CLEAR)) {
727 wclear(stdscr);
728 touchwin(stdscr);
729 clearok(stdscr, 1);
730 refresh();
731 }
732 }
733
734 free(scmd);
735 free_argv(args);
736
737 if (ret != 0 && flags & RUN_FATAL)
738 exit(ret);
739 return ret;
740 }
741