From 6a2b32ff26aad4cd7081861636bb5a9bda719c50 Mon Sep 17 00:00:00 2001 From: Manvendra Bhangui Date: Thu, 14 Nov 2024 16:18:18 +0530 Subject: [PATCH] supervise: use envdir to set environment variables if variable directory exists --- daemontools-x/doc/ChangeLog | 3 ++ daemontools-x/supervise.8 | 7 +++++ daemontools-x/supervise.c | 60 +++++++++++++++++++++++++++++++++---- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/daemontools-x/doc/ChangeLog b/daemontools-x/doc/ChangeLog index f3ba3e474..69f37a5ea 100644 --- a/daemontools-x/doc/ChangeLog +++ b/daemontools-x/doc/ChangeLog @@ -31,6 +31,9 @@ 20. daemontools.spec, debian/control: use libqmail-devel > 1.3 for pathexec_env_plus() function in libqmail 21. envdir.c: added -p option to set PATH variable before the call to execve +- 14/11/2024 +22. supervise.c: use envdir to set environment variables if variable directory + exists * Tue Aug 06 2024 12:48:42 +0000 Manvendra Bhangui 1.1.5-1.1%{?dist} Release 1.1.5-1.1 Start 05/02/2024 End 06/08/2024 diff --git a/daemontools-x/supervise.8 b/daemontools-x/supervise.8 index 0b9b40d58..c259629a8 100644 --- a/daemontools-x/supervise.8 +++ b/daemontools-x/supervise.8 @@ -57,6 +57,13 @@ in such cases to make \fBsvc\fR(8) command operate and control self pipe trick to handle all SIGCHLD events reliably. This requires the use of two file descriptors for the selfpipe. +\fBsupervise\fR can set environment variables for \fI./run\fR using +\fIenvdir\fR(8) if the directory \fI./variables\fR exists. Files in +\fI./variables\fR directory must be compatible as environment variables for +\fBenvdir\fR(8). If the directory \fI./variables\fR doesn't have execute +permissions for \fBothers\fR group, all existing environment variables will +be cleared before setting environment variables for \fI./run\fR. + .TS l l. how Description diff --git a/daemontools-x/supervise.c b/daemontools-x/supervise.c index bb2ea9f29..43d0feb83 100644 --- a/daemontools-x/supervise.c +++ b/daemontools-x/supervise.c @@ -1,4 +1,4 @@ -/*- $Id: supervise.c,v 1.49 2024-11-05 22:55:30+05:30 Cprogrammer Exp mbhangui $ */ +/*- $Id: supervise.c,v 1.50 2024-11-14 16:14:40+05:30 Cprogrammer Exp mbhangui $ */ #include #include #include @@ -19,6 +19,7 @@ #include "iopause.h" #include "taia.h" #include "deepsleep.h" +#include #ifdef USE_RUNFS #include "stralloc.h" #include "str.h" @@ -73,6 +74,8 @@ static pid_t childpid = 0; /*- 0 means down */ static int grandchild = 0; static pid_t svpid; const char *run[4] = { "./run", 0 , 0, 0}; +const char *envdir1[6] = { "envdir", "./variables", "./run", 0, 0, 0}; +const char *envdir2[7] = { "envdir", "-c" , "./variables", "./run", 0, 0, 0}; const char *shutdown[4] = { "./shutdown", 0, 0, 0}; /*- ./shutdown, pid, dir, parent_id, NULL */ const char *alert[6] = { "./alert", 0, 0, 0, 0, 0 }; /*- ./alert, alert pid, child_exit_value, signal_value, dir, parent_id, NULL */ @@ -459,6 +462,7 @@ void trystart(const char *how) { pid_t f, pgid; + static int run_type = -1; struct stat st; char strnum1[FMT_ULONG], strnum2[FMT_ULONG]; @@ -467,6 +471,30 @@ trystart(const char *how) if (use_runfs && fchdir(fddir) == -1) strerr_die2sys(111, fatal.s, "unable to switch back to service directory: "); #endif + if (run_type == -1) { + if (lstat("./variables", &st)) { + if (errno != error_noent) + strerr_die2sys(111, fatal.s, "unable to stat ./variables:"); + run_type = 0; + } else + if ((st.st_mode & S_IFMT) == S_IFDIR) { + /*- + * run-type + * 0 - start ./run + * 1 - start ./run with environment in ./variables + * without clearing existing env variables. This + * requires ./variables to have execute permissions for + * others + * 2 - start ./run with environment in ./variables + * after clearing existing env variables. This requires + * no execute permission on ./variables for others + */ + if (st.st_mode & S_IXOTH) + run_type = 1; + else + run_type = 2; + } + } if (!is_subreaper) { if (stat(*run, &st) == -1) strerr_die4sys(111, fatal.s, "unable to stat ", *run, ": "); @@ -495,10 +523,27 @@ trystart(const char *how) #endif if ((do_setpgid || is_subreaper) && setpgid(0, 0) == -1) strerr_die2sys(111, fatal.s, "unable to set process group id: "); - run[1] = dir; - run[2] = how; - execve(*run, (char **) run, environ); - strerr_die2sys(111, fatal.s, "unable to start run: "); + switch (run_type) + { + case 0: + run[1] = dir; + run[2] = how; + execve(*run, (char **) run, environ); + strerr_die4sys(111, fatal.s, "unable to start ", *run, ": "); + break; + case 1: + envdir1[3] = dir; + envdir1[4] = how; + pathexec_run(*envdir1, (char **) envdir1, environ); + strerr_die4sys(111, fatal.s, "unable to start ", *envdir1, ": "); + break; + case 2: + envdir2[4] = dir; + envdir2[5] = how; + pathexec_run(*envdir2, (char **) envdir2, environ); + strerr_die4sys(111, fatal.s, "unable to start ", *envdir2, ": "); + break; + } } grandchild = 0; flagpaused = 0; @@ -1192,13 +1237,16 @@ main(int argc, char **argv) void getversion_supervise_c() { - const char *x = "$Id: supervise.c,v 1.49 2024-11-05 22:55:30+05:30 Cprogrammer Exp mbhangui $"; + const char *x = "$Id: supervise.c,v 1.50 2024-11-14 16:14:40+05:30 Cprogrammer Exp mbhangui $"; x++; } /* * $Log: supervise.c,v $ + * Revision 1.50 2024-11-14 16:14:40+05:30 Cprogrammer + * use envdir to set environment variables if variable directory exists + * * Revision 1.49 2024-11-05 22:55:30+05:30 Cprogrammer * execute shutdown script on SIGTERM * terminate on single SIGTERM instead of the earlier two for logger