]> git.ipfire.org Git - thirdparty/util-linux.git/blob - term-utils/scriptlive.c
b7abe361064a2b7f635b84567c1d2a5995bfb8c7
[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(_(" -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)
112 break;
113
114 delay = replay_step_get_delay(ss->step);
115 if (timerisset(delay)) {
116 /* wait until now+delay in mainloop */
117 struct timeval now, target;
118
119 gettime_monotonic(&now);
120 timeradd(&now, delay, &target);
121
122 ul_pty_set_mainloop_time(ss->pty, &target);
123 break;
124 } else {
125 /* no delay -- immediately write */
126 rc = replay_emit_step_data(ss->setup, ss->step, fd);
127 fdatasync(fd);
128 }
129 } while (rc == 0);
130
131 return rc;
132 }
133
134 static int mainloop_cb(void *data)
135 {
136 struct scriptlive *ss = (struct scriptlive *) data;
137 int rc = 0;
138
139 /* emit previous waiting step */
140 if (ss->step && !replay_step_is_empty(ss->step)) {
141 int fd = ul_pty_get_childfd(ss->pty);;
142
143 rc = replay_emit_step_data(ss->setup, ss->step, fd);
144 fdatasync(fd);
145 if (rc)
146 return rc;
147 }
148
149 return process_next_step(ss);
150 }
151
152 int
153 main(int argc, char *argv[])
154 {
155 static const struct timeval mindelay = { .tv_sec = 0, .tv_usec = 100 };
156 struct timeval maxdelay;
157
158 const char *log_in = NULL, *log_io = NULL, *log_tm = NULL,
159 *shell = NULL, *command = NULL;
160 double divi = 1;
161 int diviopt = FALSE, idx;
162 int ch, caught_signal = 0;
163 struct ul_pty_callbacks *cb;
164 struct scriptlive ss = { .pty = NULL };
165 pid_t child;
166
167 static const struct option longopts[] = {
168 { "timing", required_argument, 0, 't' },
169 { "log-timing", required_argument, 0, 'T' },
170 { "log-in", required_argument, 0, 'I'},
171 { "log-io", required_argument, 0, 'B'},
172 { "divisor", required_argument, 0, 'd' },
173 { "maxdelay", required_argument, 0, 'm' },
174 { "version", no_argument, 0, 'V' },
175 { "help", no_argument, 0, 'h' },
176 { NULL, 0, 0, 0 }
177 };
178 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
179 { 'B', 'I' },
180 { 0 }
181 };
182 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
183 /* Because we use space as a separator, we can't afford to use any
184 * locale which tolerates a space in a number. In any case, script.c
185 * sets the LC_NUMERIC locale to C, anyway.
186 */
187 setlocale(LC_ALL, "");
188 setlocale(LC_NUMERIC, "C");
189
190 bindtextdomain(PACKAGE, LOCALEDIR);
191 textdomain(PACKAGE);
192 close_stdout_atexit();
193
194 replay_init_debug();
195 timerclear(&maxdelay);
196
197 while ((ch = getopt_long(argc, argv, "B:I:T:t:d:m:Vh", longopts, NULL)) != -1) {
198
199 err_exclusive_options(ch, longopts, excl, excl_st);
200
201 switch(ch) {
202 case 't':
203 case 'T':
204 log_tm = optarg;
205 break;
206 case 'I':
207 log_in = optarg;
208 break;
209 case 'B':
210 log_io = optarg;
211 break;
212 case 'd':
213 diviopt = TRUE;
214 divi = getnum(optarg);
215 break;
216 case 'm':
217 strtotimeval_or_err(optarg, &maxdelay, _("failed to parse maximal delay argument"));
218 break;
219 case 'V':
220 print_version(EXIT_SUCCESS);
221 case 'h':
222 usage();
223 default:
224 errtryhelp(EXIT_FAILURE);
225 }
226 }
227 argc -= optind;
228 argv += optind;
229 idx = 0;
230
231 if (!isatty(STDIN_FILENO))
232 errx(EXIT_FAILURE, _("stdin is not terminal"));
233
234 if (!log_tm && idx < argc)
235 log_tm = argv[idx++];
236 if (!log_in && !log_io && idx < argc)
237 log_in = argv[idx++];
238
239 if (!diviopt)
240 divi = idx < argc ? getnum(argv[idx]) : 1;
241
242 if (!log_tm)
243 errx(EXIT_FAILURE, _("timing file not specified"));
244 if (!(log_in || log_io))
245 errx(EXIT_FAILURE, _("stdin typescript file not specified"));
246
247 ss.setup = replay_new_setup();
248
249 if (replay_set_timing_file(ss.setup, log_tm) != 0)
250 err(EXIT_FAILURE, _("cannot open %s"), log_tm);
251
252 if (log_in && replay_associate_log(ss.setup, "I", log_in) != 0)
253 err(EXIT_FAILURE, _("cannot open %s"), log_in);
254
255 if (log_io && replay_associate_log(ss.setup, "IO", log_io) != 0)
256 err(EXIT_FAILURE, _("cannot open %s"), log_io);
257
258 replay_set_default_type(ss.setup, 'I');
259 replay_set_crmode(ss.setup, REPLAY_CRMODE_NEVER);
260
261 if (divi != 1)
262 replay_set_delay_div(ss.setup, divi);
263 if (timerisset(&maxdelay))
264 replay_set_delay_max(ss.setup, &maxdelay);
265 replay_set_delay_min(ss.setup, &mindelay);
266
267 shell = getenv("SHELL");
268 if (shell == NULL)
269 shell = _PATH_BSHELL;
270
271 fprintf(stdout, _(">>> scriptlive: Starting your typescript execution by %s.\n"), shell);
272
273 ul_pty_init_debug(0);
274
275 ss.pty = ul_new_pty(isatty(STDIN_FILENO));
276 if (!ss.pty)
277 err(EXIT_FAILURE, _("failed to allocate PTY handler"));
278
279 ul_pty_set_callback_data(ss.pty, (void *) &ss);
280 cb = ul_pty_get_callbacks(ss.pty);
281 cb->child_sigstop = callback_child_sigstop;
282 cb->mainloop = mainloop_cb;
283
284 if (ul_pty_setup(ss.pty))
285 err(EXIT_FAILURE, _("failed to create pseudo-terminal"));
286
287 fflush(stdout); /* ??? */
288
289 switch ((int) (child = fork())) {
290 case -1: /* error */
291 ul_pty_cleanup(ss.pty);
292 err(EXIT_FAILURE, _("cannot create child process"));
293 break;
294
295 case 0: /* child */
296 {
297 const char *shname;
298
299 ul_pty_init_slave(ss.pty);
300
301 signal(SIGTERM, SIG_DFL); /* because /etc/csh.login */
302
303 shname = strrchr(shell, '/');
304 shname = shname ? shname + 1 : shell;
305
306 if (command)
307 execl(shell, shname, "-c", command, NULL);
308 else
309 execl(shell, shname, "-i", NULL);
310 err(EXIT_FAILURE, "failed to execute %s", shell);
311 break;
312 }
313 default:
314 break;
315 }
316
317 /* parent */
318 ul_pty_set_child(ss.pty, child);
319
320 /* read the first step and set initial delay for pty main loop; the
321 * next steps will be processed by mainloop_cb() */
322 process_next_step(&ss);
323
324 /* this is the main loop */
325 ul_pty_proxy_master(ss.pty);
326
327 /* all done; cleanup and kill */
328 caught_signal = ul_pty_get_delivered_signal(ss.pty);
329
330 if (!caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1)
331 ul_pty_wait_for_child(ss.pty); /* final wait */
332
333 if (caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1) {
334 fprintf(stderr, _("\nSession terminated, killing shell..."));
335 kill(child, SIGTERM);
336 sleep(2);
337 kill(child, SIGKILL);
338 fprintf(stderr, " ...killed.\n");
339 }
340
341 ul_pty_cleanup(ss.pty);
342 ul_free_pty(ss.pty);
343 replay_free_setup(ss.setup);
344
345 fprintf(stdout, _("\n>>> scriptlive: done.\n"));
346 return EXIT_SUCCESS;
347 }