]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/scriptlive.c
6d263296bf62a5f119baf7f447685a16715eb643
[thirdparty/util-linux.git] / term-utils / scriptlive.c
1 /*
2 * Copyright (C) 2019, Karel Zak <kzak@redhat.com>
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <time.h>
21 #include <limits.h>
22 #include <math.h>
23 #include <sys/select.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <signal.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <paths.h>
32
33 #include "c.h"
34 #include "xalloc.h"
35 #include "closestream.h"
36 #include "nls.h"
37 #include "strutils.h"
38 #include "optutils.h"
39 #include "pty-session.h"
40 #include "script-playutils.h"
41 #include "monotonic.h"
42
43
44 #define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
45
46 struct scriptlive {
47 struct ul_pty *pty;
48 struct replay_setup *setup;
49 struct replay_step *step;
50 };
51
52 static void __attribute__((__noreturn__))
53 usage(void)
54 {
55 FILE *out = stdout;
56 fputs(USAGE_HEADER, out);
57 fprintf(out,
58 _(" %s [options]\n"),
59 program_invocation_short_name);
60 fprintf(out,
61 _(" %s [-t] timingfile [-I|-B] typescript\n"),
62 program_invocation_short_name);
63
64 fputs(USAGE_SEPARATOR, out);
65 fputs(_("Execute terminal typescript.\n"), out);
66
67 fputs(USAGE_OPTIONS, out);
68 fputs(_(" -t, --timing <file> script timing log file\n"), out);
69 fputs(_(" -T, --log-timing <file> alias to -t\n"), out);
70 fputs(_(" -I, --log-in <file> script stdin log file\n"), out);
71 fputs(_(" -B, --log-io <file> script stdin and stdout log file\n"), out);
72
73 fputs(USAGE_SEPARATOR, out);
74 fputs(_(" -c, --command <command> run command rather than interactive shell\n"), out);
75 fputs(_(" -d, --divisor <num> speed up or slow down execution with time divisor\n"), out);
76 fputs(_(" -m, --maxdelay <num> wait at most this many seconds between updates\n"), out);
77 printf(USAGE_HELP_OPTIONS(25));
78
79 printf(USAGE_MAN_TAIL("scriptlive(1)"));
80 exit(EXIT_SUCCESS);
81 }
82
83 static double
84 getnum(const char *s)
85 {
86 const double d = strtod_or_err(s, _("failed to parse number"));
87
88 if (isnan(d)) {
89 errno = EINVAL;
90 err(EXIT_FAILURE, "%s: %s", _("failed to parse number"), s);
91 }
92 return d;
93 }
94
95 static void callback_child_sigstop(
96 void *data __attribute__((__unused__)),
97 pid_t child)
98 {
99 kill(getpid(), SIGSTOP);
100 kill(child, SIGCONT);
101 }
102
103 static int process_next_step(struct scriptlive *ss)
104 {
105 int rc = 0, fd = ul_pty_get_childfd(ss->pty);
106
107 /* read next step(s) */
108 do {
109 struct timeval *delay;
110
111 rc = replay_get_next_step(ss->setup, "I", &ss->step);
112 if (rc == 1) {
113 ul_pty_write_eof_to_child(ss->pty);
114 rc = 0;
115 break;
116 }
117 if (rc)
118 break;
119
120 delay = replay_step_get_delay(ss->step);
121 if (timerisset(delay)) {
122 /* wait until now+delay in mainloop */
123 struct timeval now, target;
124
125 gettime_monotonic(&now);
126 timeradd(&now, delay, &target);
127
128 ul_pty_set_mainloop_time(ss->pty, &target);
129 break;
130 } else {
131 /* no delay -- immediately write */
132 rc = replay_emit_step_data(ss->setup, ss->step, fd);
133 fdatasync(fd);
134 }
135 } while (rc == 0);
136
137 return rc;
138 }
139
140 static int mainloop_cb(void *data)
141 {
142 struct scriptlive *ss = (struct scriptlive *) data;
143 int rc = 0;
144
145 /* emit previous waiting step */
146 if (ss->step && !replay_step_is_empty(ss->step)) {
147 int fd = ul_pty_get_childfd(ss->pty);;
148
149 rc = replay_emit_step_data(ss->setup, ss->step, fd);
150 fdatasync(fd);
151 if (rc)
152 return rc;
153 }
154
155 return process_next_step(ss);
156 }
157
158 int
159 main(int argc, char *argv[])
160 {
161 static const struct timeval mindelay = { .tv_sec = 0, .tv_usec = 100 };
162 struct timeval maxdelay;
163
164 const char *log_in = NULL, *log_io = NULL, *log_tm = NULL,
165 *shell = NULL, *command = NULL;
166 double divi = 1;
167 int diviopt = FALSE, idx;
168 int ch, caught_signal = 0;
169 struct ul_pty_callbacks *cb;
170 struct scriptlive ss = { .pty = NULL };
171 pid_t child;
172
173 static const struct option longopts[] = {
174 { "command", required_argument, 0, 'c' },
175 { "timing", required_argument, 0, 't' },
176 { "log-timing", required_argument, 0, 'T' },
177 { "log-in", required_argument, 0, 'I'},
178 { "log-io", required_argument, 0, 'B'},
179 { "divisor", required_argument, 0, 'd' },
180 { "maxdelay", required_argument, 0, 'm' },
181 { "version", no_argument, 0, 'V' },
182 { "help", no_argument, 0, 'h' },
183 { NULL, 0, 0, 0 }
184 };
185 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
186 { 'B', 'I' },
187 { 0 }
188 };
189 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
190 /* Because we use space as a separator, we can't afford to use any
191 * locale which tolerates a space in a number. In any case, script.c
192 * sets the LC_NUMERIC locale to C, anyway.
193 */
194 setlocale(LC_ALL, "");
195 setlocale(LC_NUMERIC, "C");
196
197 bindtextdomain(PACKAGE, LOCALEDIR);
198 textdomain(PACKAGE);
199 close_stdout_atexit();
200
201 replay_init_debug();
202 timerclear(&maxdelay);
203
204 while ((ch = getopt_long(argc, argv, "c:B:I:T:t:d:m:Vh", longopts, NULL)) != -1) {
205
206 err_exclusive_options(ch, longopts, excl, excl_st);
207
208 switch(ch) {
209 case 'c':
210 command = optarg;
211 break;
212 case 't':
213 case 'T':
214 log_tm = optarg;
215 break;
216 case 'I':
217 log_in = optarg;
218 break;
219 case 'B':
220 log_io = optarg;
221 break;
222 case 'd':
223 diviopt = TRUE;
224 divi = getnum(optarg);
225 break;
226 case 'm':
227 strtotimeval_or_err(optarg, &maxdelay, _("failed to parse maximal delay argument"));
228 break;
229 case 'V':
230 print_version(EXIT_SUCCESS);
231 case 'h':
232 usage();
233 default:
234 errtryhelp(EXIT_FAILURE);
235 }
236 }
237 argc -= optind;
238 argv += optind;
239 idx = 0;
240
241 if (!log_tm && idx < argc)
242 log_tm = argv[idx++];
243 if (!log_in && !log_io && idx < argc)
244 log_in = argv[idx++];
245
246 if (!diviopt)
247 divi = idx < argc ? getnum(argv[idx]) : 1;
248
249 if (!log_tm)
250 errx(EXIT_FAILURE, _("timing file not specified"));
251 if (!(log_in || log_io))
252 errx(EXIT_FAILURE, _("stdin typescript file not specified"));
253
254 ss.setup = replay_new_setup();
255
256 if (replay_set_timing_file(ss.setup, log_tm) != 0)
257 err(EXIT_FAILURE, _("cannot open %s"), log_tm);
258
259 if (log_in && replay_associate_log(ss.setup, "I", log_in) != 0)
260 err(EXIT_FAILURE, _("cannot open %s"), log_in);
261
262 if (log_io && replay_associate_log(ss.setup, "IO", log_io) != 0)
263 err(EXIT_FAILURE, _("cannot open %s"), log_io);
264
265 replay_set_default_type(ss.setup, 'I');
266 replay_set_crmode(ss.setup, REPLAY_CRMODE_NEVER);
267
268 if (divi != 1)
269 replay_set_delay_div(ss.setup, divi);
270 if (timerisset(&maxdelay))
271 replay_set_delay_max(ss.setup, &maxdelay);
272 replay_set_delay_min(ss.setup, &mindelay);
273
274 shell = getenv("SHELL");
275 if (shell == NULL)
276 shell = _PATH_BSHELL;
277
278 fprintf(stdout, _(">>> scriptlive: Starting your typescript execution by %s.\n"),
279 command ? command : shell);
280
281 ul_pty_init_debug(0);
282
283 ss.pty = ul_new_pty(isatty(STDIN_FILENO));
284 if (!ss.pty)
285 err(EXIT_FAILURE, _("failed to allocate PTY handler"));
286
287 ul_pty_set_callback_data(ss.pty, (void *) &ss);
288 cb = ul_pty_get_callbacks(ss.pty);
289 cb->child_sigstop = callback_child_sigstop;
290 cb->mainloop = mainloop_cb;
291
292 if (!isatty(STDIN_FILENO))
293 /* We keep ECHO flag for compatibility with script(1) */
294 ul_pty_slave_echo(ss.pty, 1);
295
296 if (ul_pty_setup(ss.pty))
297 err(EXIT_FAILURE, _("failed to create pseudo-terminal"));
298
299 fflush(stdout); /* ??? */
300
301 switch ((int) (child = fork())) {
302 case -1: /* error */
303 ul_pty_cleanup(ss.pty);
304 err(EXIT_FAILURE, _("cannot create child process"));
305 break;
306
307 case 0: /* child */
308 {
309 const char *shname;
310
311 ul_pty_init_slave(ss.pty);
312
313 signal(SIGTERM, SIG_DFL); /* because /etc/csh.login */
314
315 shname = strrchr(shell, '/');
316 shname = shname ? shname + 1 : shell;
317
318 if (access(shell, X_OK) == 0) {
319 if (command)
320 execl(shell, shname, "-c", command, NULL);
321 else
322 execl(shell, shname, "-i", NULL);
323 } else {
324 if (command)
325 execlp(shname, "-c", command, NULL);
326 else
327 execlp(shname, "-i", NULL);
328 }
329 err(EXIT_FAILURE, "failed to execute %s", shell);
330 break;
331 }
332 default:
333 break;
334 }
335
336 /* parent */
337 ul_pty_set_child(ss.pty, child);
338
339 /* read the first step and set initial delay for pty main loop; the
340 * next steps will be processed by mainloop_cb() */
341 process_next_step(&ss);
342
343 /* this is the main loop */
344 ul_pty_proxy_master(ss.pty);
345
346 /* all done; cleanup and kill */
347 caught_signal = ul_pty_get_delivered_signal(ss.pty);
348
349 if (!caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1)
350 ul_pty_wait_for_child(ss.pty); /* final wait */
351
352 if (caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1) {
353 fprintf(stderr, _("\nSession terminated, killing shell..."));
354 kill(child, SIGTERM);
355 sleep(2);
356 kill(child, SIGKILL);
357 fprintf(stderr, " ...killed.\n");
358 }
359
360 ul_pty_cleanup(ss.pty);
361 ul_free_pty(ss.pty);
362 replay_free_setup(ss.setup);
363
364 fprintf(stdout, _("\n>>> scriptlive: done.\n"));
365 return EXIT_SUCCESS;
366 }