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