scriptlive: remove unnecessary variables

ul_pty code is able to do all necessary things for us, so don't waste
effort and keep the child variable in main() only.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2019-10-07 12:35:00 +02:00
parent 8cf448e9b6
commit 1401181a19
1 changed files with 8 additions and 22 deletions

View File

@ -44,9 +44,6 @@
#define SCRIPT_MIN_DELAY 0.0001 /* from original sripreplay.pl */
struct scriptlive {
pid_t child; /* shell */
int childstatus;
struct ul_pty *pty;
struct replay_setup *setup;
struct replay_step *step;
@ -94,17 +91,6 @@ getnum(const char *s)
return d;
}
static void callback_child_die(
void *data,
pid_t child __attribute__((__unused__)),
int status)
{
struct scriptlive *ss = (struct scriptlive *) data;
ss->child = (pid_t) -1;
ss->childstatus = status;
}
static void callback_child_sigstop(
void *data __attribute__((__unused__)),
pid_t child)
@ -175,7 +161,8 @@ main(int argc, char *argv[])
int diviopt = FALSE, idx;
int ch, caught_signal = 0;
struct ul_pty_callbacks *cb;
struct scriptlive ss = { .child = 0 };
struct scriptlive ss = { .pty = NULL };
pid_t child;
static const struct option longopts[] = {
{ "timing", required_argument, 0, 't' },
@ -291,7 +278,6 @@ main(int argc, char *argv[])
ul_pty_set_callback_data(ss.pty, (void *) &ss);
cb = ul_pty_get_callbacks(ss.pty);
cb->child_die = callback_child_die;
cb->child_sigstop = callback_child_sigstop;
cb->mainloop = mainloop_cb;
@ -300,7 +286,7 @@ main(int argc, char *argv[])
fflush(stdout); /* ??? */
switch ((int) (ss.child = fork())) {
switch ((int) (child = fork())) {
case -1: /* error */
ul_pty_cleanup(ss.pty);
err(EXIT_FAILURE, _("cannot create child process"));
@ -329,7 +315,7 @@ main(int argc, char *argv[])
}
/* parent */
ul_pty_set_child(ss.pty, ss.child);
ul_pty_set_child(ss.pty, child);
/* read the first step and set initial delay for pty main loop; the
* next steps will be processed by mainloop_cb() */
@ -341,14 +327,14 @@ main(int argc, char *argv[])
/* all done; cleanup and kill */
caught_signal = ul_pty_get_delivered_signal(ss.pty);
if (!caught_signal && ss.child != (pid_t)-1)
if (!caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1)
ul_pty_wait_for_child(ss.pty); /* final wait */
if (caught_signal && ss.child != (pid_t)-1) {
if (caught_signal && ul_pty_get_child(ss.pty) != (pid_t)-1) {
fprintf(stderr, _("\nSession terminated, killing shell..."));
kill(ss.child, SIGTERM);
kill(child, SIGTERM);
sleep(2);
kill(ss.child, SIGKILL);
kill(child, SIGKILL);
fprintf(stderr, " ...killed.\n");
}