aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/mds-base.c30
-rw-r--r--src/mds-base.h19
2 files changed, 49 insertions, 0 deletions
diff --git a/src/mds-base.c b/src/mds-base.c
index 12eac19..1b2e767 100644
--- a/src/mds-base.c
+++ b/src/mds-base.c
@@ -42,6 +42,8 @@ int argc = 0;
char** argv = NULL;
int is_respawn = -1;
int is_reexec = 0;
+int on_init_fork = 0;
+char* on_init_sh = NULL;
pthread_t master_thread;
volatile sig_atomic_t terminating = 0;
@@ -82,6 +84,10 @@ int __attribute__((weak)) parse_cmdline(void)
is_reexec = 1;
else if (startswith(arg, "--alarm=")) /* Schedule an alarm signal for forced abort. */
alarm((unsigned)min(atoi(arg + strlen("--alarm=")), 60)); /* At most 1 minute. */
+ else if (strequals(arg, "--on-init-fork")) /* Fork process when initialised. */
+ on_init_fork = 1;
+ else if (startswith(arg, "--on-init-sh=")) /* Run a command when initialised. */
+ on_init_sh = arg + strlen("--on-init-sh=");
}
if (is_reexec)
{
@@ -131,6 +137,30 @@ int __attribute__((weak)) connect_to_display(void)
return 1;
}
+/**
+ * This function should be called when the server has
+ * been properly initialised but before initialisation
+ * of anything that is removed at forking is initialised
+ */
+void __attribute__((weak)) server_initialised(void)
+{
+ pid_t r;
+ if (on_init_fork && (r = fork()))
+ {
+ if (r == (pid_t)-1)
+ {
+ perror(*argv);
+ eprint("while forking at completed initialisation");
+ exit(1);
+ }
+ else
+ exit(0);
+ }
+
+ if (on_init_sh != NULL)
+ system(on_init_sh);
+}
+
/**
* This function is called when a signal that
diff --git a/src/mds-base.h b/src/mds-base.h
index a4298da..c8666a1 100644
--- a/src/mds-base.h
+++ b/src/mds-base.h
@@ -91,6 +91,18 @@ extern int is_respawn;
extern int is_reexec;
/**
+ * Whether to fork the process when the
+ * server has been properly initialised
+ */
+extern int on_init_fork;
+
+/**
+ * Command the run (`NULL` for none) when
+ * the server has been properly initialised
+ */
+extern char* on_init_sh;
+
+/**
* The thread that runs the master loop
*/
extern pthread_t master_thread;
@@ -138,6 +150,13 @@ int parse_cmdline(void); /* __attribute__((weak)) */
*/
int connect_to_display(void); /* __attribute__((weak)) */
+/**
+ * This function should be called when the server has
+ * been properly initialised but before initialisation
+ * of anything that is removed at forking is initialised
+ */
+void server_initialised(void); /* __attribute__((weak)) */
+
/**
* This function should be implemented by the actual server implementation