Browse Source

Add support for export-service-name load option

This will result in the DINIT_SERVICE environment variable being
set in the environment, which allows commands to reference the
current dinit service name.

Fixes https://github.com/davmac314/dinit/issues/53
Daniel Kolesa 1 year ago
parent
commit
7aaa3d3dcf

+ 8 - 5
doc/manpages/dinit-service.5.m4

@@ -349,11 +349,14 @@ This directive can be specified multiple times to set additional options.
 .TP
 \fBload\-options\fR = \fIload_option\fR...
 Specifies options for interpreting other settings when loading this service description.
-Currently there is only one available option, \fBexport-passwd-vars\fR, which specifies that
-the environment variables `\fBUSER\fR', `\fBLOGNAME\fR' (same as `\fBUSER\fR'),
-`\fBHOME\fR', `\fBSHELL\fR', `\fBUID\fR', and `\fBGID\fR' should be exported into the
-service's load environment (that is, overriding any global environment including the
-global environment file, but being overridable by the service's environment file).
+Currently there are two available options. One is \fBexport-passwd-vars\fR, which
+specifies that the environment variables `\fBUSER\fR', `\fBLOGNAME\fR' (same as
+`\fBUSER\fR'), `\fBHOME\fR', `\fBSHELL\fR', `\fBUID\fR', and `\fBGID\fR' should
+be exported into the service's load environment (that is, overriding any global
+environment including the global environment file, but being overridable by the
+service's environment file). The other is \fBexport-service-name\fR, which will
+set the environment variable `\fBDINIT_SERVICE\fR' containing the name of the
+current service.
 .TP
 \fBinittab\-id\fR = \fIid-string\fR
 When this service is started, if this setting (or the \fBinittab\-line\fR setting) has a

+ 3 - 0
src/igr-tests/environ/checkenv.sh

@@ -2,6 +2,9 @@
 
 if [ "$TEST_VAR_ONE" = "hello" ]; then
     echo "$DINIT_SOCKET_PATH" >> ./env-record
+    if [ "$2" = "$DINIT_SERVICE" ]; then
+        echo "$DINIT_SERVICE" >> ./env-record
+    fi
 fi
 
 if [ "$1" = "hello" ]; then

+ 1 - 1
src/igr-tests/environ/run-test.sh

@@ -19,7 +19,7 @@ rm -f ./env-record
 
 STATUS=FAIL
 if [ -e env-record ]; then
-   if [ "$(cat env-record)" = "$(echo $DINIT_SOCKET_PATH; echo gotenv1; echo hello; echo gotenv2; echo goodbye; echo 3; echo 2; echo 1)" ]; then
+   if [ "$(cat env-record)" = "$(echo $DINIT_SOCKET_PATH; echo checkenv; echo gotenv1; echo hello; echo gotenv2; echo goodbye; echo 3; echo 2; echo 1)" ]; then
        STATUS=PASS
    fi
 fi

+ 2 - 1
src/igr-tests/environ/sd/checkenv

@@ -1,3 +1,4 @@
 type = process
-command = ./checkenv.sh $TEST_VAR_ONE
+command = ./checkenv.sh $TEST_VAR_ONE $DINIT_SERVICE
 restart = false
+load-options = export-service-name

+ 4 - 0
src/includes/load-service.h

@@ -804,6 +804,7 @@ class service_settings_wrapper
     string env_file;
 
     bool export_passwd_vars = false;
+    bool export_service_name = false;
 
     service_type_t service_type = service_type_t::INTERNAL;
     list<dep_type> depends;
@@ -1176,6 +1177,9 @@ void process_service_line(settings_wrapper &settings, const char *name, string &
             if (option_txt == "export-passwd-vars") {
                 settings.export_passwd_vars = true;
             }
+            else if (option_txt == "export-service-name") {
+                settings.export_service_name = true;
+            }
             else if (option_txt == "sub-vars") {
                 // noop: for backwards compatibility only
                 // we don't support no-sub-vars anymore, however

+ 7 - 0
src/load-service.cc

@@ -364,6 +364,13 @@ service_record * dirload_service_set::load_reload_service(const char *name, serv
             fill_environment_userinfo(settings.run_as_uid, name, srv_env);
         }
 
+        /* set service name in environment if desired */
+        if (settings.export_service_name) {
+            std::string envname = "DINIT_SERVICE=";
+            envname += name;
+            srv_env.set_var(std::move(envname));
+        }
+
         // this mapping is temporary, for load substitutions
         // the reason for this is that the environment actually *may* change
         // after load, e.g. through dinitctl setenv (either from the outside