From 6736de4a3caf9a0b3c888c6cc05103ab1b86907d Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Mon, 14 Dec 2015 05:09:53 +0000
Subject: [PATCH 13/36] sysv-generator: add support for executing scripts under
 /etc/rcS.d/

To be compatible, all services translated from scripts under /etc/rcS.d would
run before services translated from scripts under /etc/rcN.d.

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 src/sysv-generator/sysv-generator.c | 47 ++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c
index b5925a4..ea06d6a 100644
--- a/src/sysv-generator/sysv-generator.c
+++ b/src/sysv-generator/sysv-generator.c
@@ -44,7 +44,8 @@
 
 typedef enum RunlevelType {
         RUNLEVEL_UP,
-        RUNLEVEL_DOWN
+        RUNLEVEL_DOWN,
+        RUNLEVEL_SYSINIT
 } RunlevelType;
 
 static const struct {
@@ -59,6 +60,9 @@ static const struct {
         { "rc4.d",  SPECIAL_MULTI_USER_TARGET, RUNLEVEL_UP },
         { "rc5.d",  SPECIAL_GRAPHICAL_TARGET,  RUNLEVEL_UP },
 
+        /* Debian style rcS.d, also adopted by OE */
+        { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT},
+
         /* Standard SysV runlevels for shutdown */
         { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
         { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN }
@@ -67,7 +71,7 @@ static const struct {
            directories in this order, and we want to make sure that
            sysv_start_priority is known when we first load the
            unit. And that value we only know from S links. Hence
-           UP must be read before DOWN */
+           UP/SYSINIT must be read before DOWN */
 };
 
 static const char *arg_dest = "/tmp";
@@ -86,6 +90,8 @@ typedef struct SysvStub {
         bool has_lsb;
         bool reload;
         bool loaded;
+        bool default_dependencies;
+        bool from_rcsd;
 } SysvStub;
 
 static void free_sysvstub(SysvStub *s) {
@@ -194,6 +198,9 @@ static int generate_unit_file(SysvStub *s) {
         if (s->description)
                 fprintf(f, "Description=%s\n", s->description);
 
+        if (!s->default_dependencies)
+                fprintf(f, "DefaultDependencies=no\n");
+
         STRV_FOREACH(p, s->before)
                 fprintf(f, "Before=%s\n", *p);
         STRV_FOREACH(p, s->after)
@@ -711,17 +720,31 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
                 if (s->has_lsb && other->has_lsb)
                         continue;
 
-                if (other->sysv_start_priority < s->sysv_start_priority) {
-                        r = strv_extend(&s->after, other->name);
+                /* All scripts under /etc/rcS.d should execute before scripts under
+                 * /etc/rcN.d */
+                if (!other->from_rcsd && s->from_rcsd) {
+                        r = strv_extend(&s->before, other->name);
                         if (r < 0)
                                 return log_oom();
 
-                } else if (other->sysv_start_priority > s->sysv_start_priority) {
-                        r = strv_extend(&s->before, other->name);
+                } else if (other->from_rcsd && !s->from_rcsd) {
+                        r = strv_extend(&s->after, other->name);
                         if (r < 0)
                                 return log_oom();
-                } else
-                        continue;
+                } else {
+                        if (other->sysv_start_priority < s->sysv_start_priority) {
+                                r = strv_extend(&s->after, other->name);
+                                if (r < 0)
+                                        return log_oom();
+                        }
+                        else if (other->sysv_start_priority > s->sysv_start_priority) {
+                                r = strv_extend(&s->before, other->name);
+                                if (r < 0)
+                                        return log_oom();
+                        }
+                        else
+                                continue;
+                }
 
                 /* FIXME: Maybe we should compare the name here lexicographically? */
         }
@@ -788,6 +811,8 @@ static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
                                 return log_oom();
 
                         service->sysv_start_priority = -1;
+                        service->default_dependencies = true;
+                        service->from_rcsd = false;
                         service->name = name;
                         service->path = fpath;
                         name = fpath = NULL;
@@ -871,9 +896,11 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
 
                                 if (de->d_name[0] == 'S')  {
 
-                                        if (rcnd_table[i].type == RUNLEVEL_UP)
+                                        if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
                                                 service->sysv_start_priority = MAX(a*10 + b, service->sysv_start_priority);
-
+                                                service->default_dependencies = (rcnd_table[i].type == RUNLEVEL_SYSINIT)?false:true;
+                                                service->from_rcsd = (rcnd_table[i].type == RUNLEVEL_SYSINIT)?true:false;
+                                        }
                                         r = set_ensure_allocated(&runlevel_services[i], NULL);
                                         if (r < 0) {
                                                 log_oom();
@@ -887,7 +914,8 @@ static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_servic
                                         }
 
                                 } else if (de->d_name[0] == 'K' &&
-                                           (rcnd_table[i].type == RUNLEVEL_DOWN)) {
+                                           (rcnd_table[i].type == RUNLEVEL_DOWN ||
+                                            rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
 
                                         r = set_ensure_allocated(&shutdown_services, NULL);
                                         if (r < 0) {
-- 
1.8.3.1

