]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
scriptlive: remove unnecessary variables
authorKarel Zak <kzak@redhat.com>
Mon, 7 Oct 2019 10:35:00 +0000 (12:35 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 8 Oct 2019 11:11:54 +0000 (13:11 +0200)
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>
term-utils/scriptlive.c

index d229e6d6fa49408cc376f0a75ce4a39f6dfc0150..b7abe361064a2b7f635b84567c1d2a5995bfb8c7 100644 (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");
        }