Welcome to little lamb

Code » anopa » commit 93f7b12

start/stop: Add manual trigger of timeout

author Olivier Brunel
2015-04-04 13:26:35 UTC
committer Olivier Brunel
2015-04-04 13:26:35 UTC
parent d720115ddbdfea3501b24a5baa09cc2dd4307266

start/stop: Add manual trigger of timeout

One can instantly trigger the "active" service to be timed out by sending
SIGINT (e.g. ^C) when its name is shown as DRAW_CUR_WAITING.

doc/aa-start.pod +33 -0
src/anopa/start-stop.c +12 -0
src/anopa/start-stop.h +1 -0
src/libanopa/exec_oneshot.c +4 -0

diff --git a/doc/aa-start.pod b/doc/aa-start.pod
index f75488a..3b541f6 100644
--- a/doc/aa-start.pod
+++ b/doc/aa-start.pod
@@ -79,6 +79,39 @@ Note that using a value of 0 as time out (either via I<timeout> file or
 B<--timeout> option) means it never times out, and B<aa-start>(1) will wait for
 it forever; Use with caution.
 
+=head2 Which services are we waiting for?
+
+When waiting for a service, and if there is no other activity (i.e. no services
+being started or showing messages, etc) then B<aa-start>(1) will show an
+indication of the service being waited for, including how much time has elapsed
+since it was started and its timeout, e.g:
+
+    [23s/5m] foobar
+
+Indicates that service I<foobar> is being waited for, it was started 23 seconds
+ago and will timeout when reaching 5 minutes. In case more than one service is
+being waited for, the "index" of the service and the number of services being
+waited for will be prefixed, and it will go through all services, e.g:
+
+    [1/2; 23s/5m] foo
+
+for a couple of seconds, then:
+
+    [2/2; 42s/2m30] bar
+
+Looping through them again until some activity occurs. The service whose name is
+currently being shown is called the "active" service.
+
+=head2 Manual trigger of time out
+
+When the name of the "active" service is shown, if B<aa-start>(1) receives
+signal INT (e.g. you pressed Ctrl+C) it will then reset the service's timeout
+to 1 second, effectively causing it to be processed as timed out immediately.
+
+This could be useful if you know a service won't work and eventually time out,
+and do not wish to wait until it actually gets there. This also works for
+services with an infinite timeout (i.e. set to 0).
+
 =head1 STARTING A LONG-RUN SERVICE
 
 When starting a long-run service, B<aa-start>(1) first connects to the I<event>
diff --git a/src/anopa/start-stop.c b/src/anopa/start-stop.c
index 747ed11..47a8ce2 100644
--- a/src/anopa/start-stop.c
+++ b/src/anopa/start-stop.c
@@ -36,6 +36,7 @@ int cols = 80;
 int is_utf8 = 0;
 int ioloop = 1;
 int si_password = -1;
+int si_active = -1;
 
 /* aa-start.c */
 void check_essential (int si);
@@ -56,6 +57,7 @@ clear_draw ()
     {
         aa_is_flush (AA_OUT, ANSI_CLEAR_BEFORE ANSI_START_LINE);
         draw &= ~DRAW_NEED_WAITING;
+        si_active = -1;
     }
 
     if (draw & DRAW_CUR_PROGRESS)
@@ -217,6 +219,7 @@ draw_waiting (int already_drawn)
     aa_is_flush (AA_OUT, aa_service_name (aa_service (si)));
 
     draw |= DRAW_CUR_WAITING;
+    si_active = si;
 }
 
 static int
@@ -869,6 +872,14 @@ handle_signals (aa_mode mode)
                     break;
                 }
 
+            case SIGINT:
+                if (si_active > -1)
+                    /* set the timeout for the "active" service (i.e. the one
+                     * whose name was shown as DRAW_CUR_WAITING) which will in
+                     * effect cause it to be timed out instantly */
+                    aa_service (si_active)->secs_timeout = 1;
+                break;
+
             case SIGTERM:
             case SIGQUIT:
                 ioloop = 0;
@@ -1167,6 +1178,7 @@ mainloop (aa_mode mode, aa_scan_cb scan_cb)
     sigaddset (&set, SIGCHLD);
     sigaddset (&set, SIGTERM);
     sigaddset (&set, SIGQUIT);
+    sigaddset (&set, SIGINT);
     sigaddset (&set, SIGWINCH);
     if (selfpipe_trapset (&set) < 0)
         strerr_diefu1sys (ERR_IO, "trap signals");
diff --git a/src/anopa/start-stop.h b/src/anopa/start-stop.h
index 7c14ddb..d6e307f 100644
--- a/src/anopa/start-stop.h
+++ b/src/anopa/start-stop.h
@@ -31,6 +31,7 @@ extern int cols;
 extern int is_utf8;
 extern int ioloop;
 extern int si_password;
+extern int si_active;
 
 enum
 {
diff --git a/src/libanopa/exec_oneshot.c b/src/libanopa/exec_oneshot.c
index f544a04..7db9b15 100644
--- a/src/libanopa/exec_oneshot.c
+++ b/src/libanopa/exec_oneshot.c
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <signal.h>
 #include <skalibs/allreadwrite.h>
 #include <skalibs/djbunix.h>
 #include <skalibs/selfpipe.h>
@@ -161,6 +162,9 @@ _exec_oneshot (int si, aa_mode mode)
         uint32 e;
 
         selfpipe_finish ();
+        /* Ignore SIGINT to make sure one can ^C to timeout a service without
+         * issue. Mostly useful when e.g. aa-start is ran from terminal. */
+        sigset (SIGINT, SIG_IGN);
         fd_close (p_int[0]);
         fd_close (p_in[1]);
         fd_close (p_out[0]);