Welcome to little lamb

Code » anopa » commit 3007931

enable: Add --set-init

author Olivier Brunel
2018-01-24 21:43:36 UTC
committer Olivier Brunel
2018-01-25 10:17:48 UTC
parent 6afffa53e701b801459d4a0b7157684fbdf53eb0

enable: Add --set-init

doc/aa-enable.pod +12 -4
doc/aa-stage1.pod +1 -2
doc/anopa.pod +11 -6
src/anopa/aa-enable.c +15 -2
src/scripts/aa-stage1 +4 -4

diff --git a/doc/aa-enable.pod b/doc/aa-enable.pod
index 651bb86..587d0b5 100644
--- a/doc/aa-enable.pod
+++ b/doc/aa-enable.pod
@@ -4,10 +4,10 @@ aa-enable - Enable services, i.e. copy servicedirs to repodir
 
 =head1 SYNOPSIS
 
-B<aa-enable> [B<-D>] [B<-O> I<FILE|FD>] [B<-r> I<REPODIR>] [B<-p> I<POSTSTART>]
-[B<-P> I<POSTSTOP>] [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>] [B<-u>]
-[B<-a>] [B<--no-supervise>] [B<-q>] [I<SERVICE...>]
+B<aa-enable> [B<-D>] [B<-O> I<FILE|FD>] [B<-r> I<REPODIR>] [B<-i> I<INIT>]
+[B<-p> I<POSTSTART>] [B<-P> I<POSTSTOP>] [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>] [B<-u>] [B<-a>] [B<--no-supervise>] [B<-q>] [I<SERVICE...>]
 
 =head1 OPTIONS
 
@@ -48,6 +48,14 @@ point to B<aa-stage3>(1).
 
 Show help screen and exit.
 
+=item B<-i, --set-init> I<INIT>
+
+Create a symlink I<REPODIR/.anopa/init> that points to I<INIT>. When using
+B<aa-stage1>(1) this is what will be executed to actually start all the services
+and bring the system to readiness.
+
+This would usually point to B<aa-stage2>(1).
+
 =item B<-k, --skip-down> I<SERVICE>
 
 When (if) enabling I<SERVICE> do not create a file I<down> even it there's none
diff --git a/doc/aa-stage1.pod b/doc/aa-stage1.pod
index 2cd091d..900433b 100644
--- a/doc/aa-stage1.pod
+++ b/doc/aa-stage1.pod
@@ -32,5 +32,4 @@ A service I<uncaught-logs> must exist and be a logger that will be used to catch
 all "uncaught logs" (i.e. anything not redirected to a service logger). A FIFO
 must be created as I</run/services/.scandir/uncaught-logs/fifo> and will be
 automatically started by B<s6-svscan>; It will also be used as trigger for
-B<aa-stage2>(1).
-
+the "real" init, I</run/services/.anopa/init> (usually B<aa-stage2>(1)).
diff --git a/doc/anopa.pod b/doc/anopa.pod
index 2a7b84a..12a7d2d 100644
--- a/doc/anopa.pod
+++ b/doc/anopa.pod
@@ -380,15 +380,20 @@ Refer to B<aa-enable>(1) for more on how it works and how the copy operation
 takes place.
 
 
-On boot, B<aa-hiercopy>(1) is used to copy the repository directory as runtime
-repository I</run/services>
+When booting, you would use B<aa-stage1>(1) as your init. It will use
+B<aa-hiercopy>(1) to copy the repository directory as runtime repository
+I</run/services>, then start B<s6-svscan> and I</run/services/.anopa/init>,
+which is the "real" init script aimed to start all services.
 
-Then, B<aa-start>(1) is used to start all services in order, getting the system
-up & ready. Refer to B<aa-start>(1) for more on how it works; Refer to
-B<aa-stage1>(1) and B<aa-stage2>(1) for more on this init process on boot works.
+It would usually be B<aa-stage2>(1), little wrapper around B<aa-start>(1), that
+will start all services in order, handling dependencies, getting the system up &
+ready.
+
+Refer to B<aa-start>(1) for more on how it works; Refer to B<aa-stage1>(1) and
+B<aa-stage2>(1) for more on this init process on boot works.
 
 You can also see B<aa-stage0>(1) and B<aa-stage4>(1) for how to use B<anopa> in
-your initramfs.
+your initramfs, as I</init> and I</shutdown> respectively.
 
 Note that this is of course only a possible solution to set up your system, you
 are of course free to organize things differently, using only the tool(s) you
diff --git a/src/anopa/aa-enable.c b/src/anopa/aa-enable.c
index 0ed1b31..3666c5c 100644
--- a/src/anopa/aa-enable.c
+++ b/src/anopa/aa-enable.c
@@ -59,6 +59,7 @@
 #define SYMLINKDIR      ".anopa"
 #define POST_START      SYMLINKDIR "/post-start"
 #define POST_STOP       SYMLINKDIR "/post-stop"
+#define INIT            SYMLINKDIR "/init"
 
 #define SOURCE_ETC      "/etc/anopa/services"
 #define SOURCE_USR      "/usr/lib/services"
@@ -211,6 +212,7 @@ dieusage (int rc)
             " -k, --skip-down SERVICE       Don't create file 'down' for SERVICE\n"
             " -u, --upgrade                 Upgrade service dirs instead of creating them\n"
             " -l, --listdir DIR             Use DIR to list services to enable\n"
+            " -i, --set-init TARGET         Create symlink REPODIR/.anopa/init to TARGET\n"
             " -p, --set-post-start TARGET   Create symlink REPODIR/.anopa/post-start to TARGET\n"
             " -P, --set-post-stop TARGET    Create symlink REPODIR/.anopa/post-stop to TARGET\n"
             " -f, --set-finish TARGET       Create s6-svscan symlink finish to TARGET\n"
@@ -233,6 +235,7 @@ main (int argc, char * const argv[])
     const char *path_list = NULL;
     const char *set_crash = NULL;
     const char *set_finish = NULL;
+    const char *set_init = NULL;
     const char *set_post_start = NULL;
     const char *set_post_stop = NULL;
     int i;
@@ -253,6 +256,7 @@ main (int argc, char * const argv[])
             { "double-output",      no_argument,        NULL,   'D' },
             { "set-finish",         required_argument,  NULL,   'f' },
             { "help",               no_argument,        NULL,   'h' },
+            { "set-init",           required_argument,  NULL,   'i' },
             { "skip-down",          required_argument,  NULL,   'k' },
             { "listdir",            required_argument,  NULL,   'l' },
             { "no-needs",           no_argument,        NULL,   'N' },
@@ -271,7 +275,7 @@ main (int argc, char * const argv[])
         };
         int c;
 
-        c = getopt_long (argc, argv, "ac:Df:hk:l:NO:P:p:qr:S:s:uVW", longopts, NULL);
+        c = getopt_long (argc, argv, "ac:Df:hi:k:l:NO:P:p:qr:S:s:uVW", longopts, NULL);
         if (c == -1)
             break;
         switch (c)
@@ -295,6 +299,10 @@ main (int argc, char * const argv[])
             case 'h':
                 dieusage (RC_OK);
 
+            case 'i':
+                set_init = optarg;
+                break;
+
             case 'k':
                 skip = optarg;
                 break;
@@ -450,9 +458,14 @@ main (int argc, char * const argv[])
 
     if (!(flags & AA_FLAG_UPGRADE_SERVICEDIR))
     {
-        if ((set_post_start || set_post_stop)
+        if ((set_init || set_post_start || set_post_stop)
                 && mkdir (SYMLINKDIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
             aa_put_err ("Failed to create " SYMLINKDIR, strerror (errno), 1);
+        if (set_init && symlink (set_init, INIT) < 0)
+        {
+            aa_put_err ("Failed to create symlink " INIT, strerror (errno), 1);
+            rc |= RC_ST_SYMLINK;
+        }
         if (set_post_start && symlink (set_post_start, POST_START) < 0)
         {
             aa_put_err ("Failed to create symlink " POST_START, strerror (errno), 1);
diff --git a/src/scripts/aa-stage1 b/src/scripts/aa-stage1
index ca55370..2339ef1 100755
--- a/src/scripts/aa-stage1
+++ b/src/scripts/aa-stage1
@@ -74,14 +74,14 @@ fdmove -c 2 1
 # close logger
 fdclose 3
 
-# Fork the stage2 script and have it ready to start as soon as the catch-all
-# logger is in place
+# Fork the "real" init (stage2) script and have it ready to start as soon as the
+# catch-all logger is in place
 background
 {
     s6-setsid
     redirfd -w 1 /run/services/uncaught-logs/fifo # (blocks until the logger reads)
-    @LIBEXECDIR@/aa-stage2
+    /run/services/.anopa/init
 }
 
-# Start the "real" stage 2 (as far as PID 1 goes)
+# Start the real init (as far as PID 1 goes)
 emptyenv -c s6-svscan -t0 /run/services/.scandir