Welcome to little lamb

Code » anopa » commit 9814f93

enable: Add --set-crash/--set-finish

author Olivier Brunel
2015-02-27 17:39:45 UTC
committer Olivier Brunel
2015-04-04 12:47:35 UTC
parent 03ed2b667ac442f572207213992ae2b657b5da8f

enable: Add --set-crash/--set-finish

doc/aa-enable.pod +14 -2
src/anopa/aa-enable.c +28 -1

diff --git a/doc/aa-enable.pod b/doc/aa-enable.pod
index 2caa8c1..46ca81f 100644
--- a/doc/aa-enable.pod
+++ b/doc/aa-enable.pod
@@ -4,13 +4,19 @@ aa-enable - Enable services, i.e. copy servicedirs to repodir
 
 =head1 SYNOPSIS
 
-B<aa-enable> [B<-D>] [B<-r> I<REPODIR>] [B<-k> I<SERVICE>] [B<-S> I<SOURCEDIR>]
-[B<-s> I<SOURCEDIR>] [B<-l> I<LISTDIR>] [B<-n>] [B<-w>] [I<SERVICE...>]
+B<aa-enable> [B<-D>] [B<-r> I<REPODIR>] [B<-c> I<CRASH>] [B<-f> I<FINISH>]
+[B<-k> I<SERVICE>] [B<-S> I<SOURCEDIR>] [B<-s> I<SOURCEDIR>] [B<-l> I<LISTDIR>]
+[B<-n>] [B<-w>] [I<SERVICE...>]
 
 =head1 OPTIONS
 
 =over
 
+=item B<-c, --set-crash> I<CRASH>
+
+Create a symlink I<REPODIR/.scandir/.s6-svscan/crash> that points to I<CRASH>.
+This is what B<s6-svscan> will exec into if it crashes.
+
 =item B<-D, --double-output>
 
 Enable double-output mode. Instead of using stdout for regular output, and
@@ -18,6 +24,12 @@ stderr for warnings and errors, everything is sent both to stdout and stderr.
 This is intended to redirect stderr to a log file, so full output can be both
 shown on console and logged.
 
+=item B<-f, --set-finish> I<FINISH>
+
+Create a symlink I<REPODIR/.scandir/.s6-svscan/finish> that points to I<FINISH>.
+This is what B<s6-svscan> will exec into when it's done. This would usually
+point to B<aa-stage3>(1).
+
 =item B<-h, --help>
 
 Show help screen and exit.
diff --git a/src/anopa/aa-enable.c b/src/anopa/aa-enable.c
index 74a4b3f..50d0686 100644
--- a/src/anopa/aa-enable.c
+++ b/src/anopa/aa-enable.c
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <skalibs/bytestr.h>
 #include <skalibs/stralloc.h>
 #include <skalibs/genalloc.h>
@@ -28,6 +29,10 @@
 #include "util.h"
 
 
+#define SVSCANDIR       ".scandir/.s6-svscan"
+#define SCANDIR_CRASH   SVSCANDIR "/crash"
+#define SCANDIR_FINISH  SVSCANDIR "/finish"
+
 #define SOURCE_ETC      "/etc/anopa/services"
 #define SOURCE_USR      "/usr/lib/services"
 
@@ -144,6 +149,8 @@ dieusage (int rc)
             " -s, --source DIR              Add DIR as source directories\n"
             " -k, --skip-down SERVICE       Don't create file 'down' for SERVICE\n"
             " -l, --listdir DIR             Use DIR to list services to enable\n"
+            " -f, --set-finish TARGET       Create s6-svscan symlink finish to TARGET\n"
+            " -c, --set-crash TARGET        Create s6-svscan symlink crash to TARGET\n"
             " -n, --no-needs                Don't auto-enable services from 'needs'\n"
             " -w, --no-wants                Don't auto-enable services from 'wants'\n"
             " -h, --help                    Show this help screen and exit\n"
@@ -157,6 +164,8 @@ main (int argc, char * const argv[])
     PROG = "aa-enable";
     const char *path_repo = "/run/services";
     const char *path_list = NULL;
+    const char *set_crash = NULL;
+    const char *set_finish = NULL;
     int mode_both = 0;
     int i;
     int r;
@@ -169,7 +178,9 @@ main (int argc, char * const argv[])
     for (;;)
     {
         struct option longopts[] = {
+            { "set-crash",          required_argument,  NULL,   'c' },
             { "double-output",      no_argument,        NULL,   'D' },
+            { "set-finish",         required_argument,  NULL,   'f' },
             { "help",               no_argument,        NULL,   'h' },
             { "skip-down",          required_argument,  NULL,   'k' },
             { "listdir",            required_argument,  NULL,   'l' },
@@ -183,15 +194,23 @@ main (int argc, char * const argv[])
         };
         int c;
 
-        c = getopt_long (argc, argv, "Dhk:l:nr:S:s:Vw", longopts, NULL);
+        c = getopt_long (argc, argv, "c:Df:hk:l:nr:S:s:Vw", longopts, NULL);
         if (c == -1)
             break;
         switch (c)
         {
+            case 'c':
+                set_crash = optarg;
+                break;
+
             case 'D':
                 mode_both = 1;
                 break;
 
+            case 'f':
+                set_finish = optarg;
+                break;
+
             case 'h':
                 dieusage (0);
 
@@ -274,10 +293,18 @@ main (int argc, char * const argv[])
         enable_service (names.s + offset, 1 + offset);
     }
 
+    aa_bs_noflush (AA_OUT, "\n");
     aa_put_title (1, PROG, "Completed", 1);
     aa_show_stat_nb (nb_enabled, "Enabled", ANSI_HIGHLIGHT_GREEN_ON);
     aa_show_stat_names (names.s, &ga_failed, "Failed", ANSI_HIGHLIGHT_RED_ON);
 
+    if ((set_crash || set_finish) && mkdir (SVSCANDIR, S_IRWXU) < 0)
+        aa_put_err ("Failed to create " SVSCANDIR, error_str (errno), 1);
+    if (set_crash && symlink (set_crash, SCANDIR_CRASH) < 0)
+        aa_put_err ("Failed to create symlink " SCANDIR_CRASH, error_str (errno), 1);
+    if (set_finish && symlink (set_finish, SCANDIR_FINISH) < 0)
+        aa_put_err ("Failed to create symlink " SCANDIR_FINISH, error_str (errno), 1);
+
     genalloc_free (int, &ga_failed);
     genalloc_free (int, &ga_next);
     stralloc_free (&sa_pl);