From 1e01c729d4cefd22ba302d67169533e8a2913d41 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" <mgrondona@llnl.gov> Date: Mon, 9 Jan 2012 13:24:05 -0800 Subject: [PATCH] slurmstepd: Add new mode to run spank job prolog/epilog The spank_job_prolog() and spank_job_epilog() spank calls need to be run in a different address space from slurmd. This not allows reinitializing the spank plugin stack on each run of the prolog or epilog, but also ensures that any static data in plugins does not propagate to each invocation of the job prolog and epilog (e.g. global variables). Additionally, it is much safer to run these plugins in a new process because we may be calling prolog/epilog for multiple jobs at the same time. This patch runs spank_job_prolog() or spank_job_epilog() from slurmstepd when slurmstepd is invoked as slurmstepd spank [prolog|epilog] The environment variables SLURM_JOBID and SLURM_UID are used to set the jobid and uid for the prolog/epilog. Spank plugin options may also be passed through the current environment. --- src/slurmd/slurmstepd/slurmstepd.c | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/slurmd/slurmstepd/slurmstepd.c b/src/slurmd/slurmstepd/slurmstepd.c index ba1f7af37a8..c8988cb4d3a 100644 --- a/src/slurmd/slurmstepd/slurmstepd.c +++ b/src/slurmd/slurmstepd/slurmstepd.c @@ -54,6 +54,7 @@ #include "src/common/switch.h" #include "src/common/xmalloc.h" #include "src/common/xsignal.h" +#include "src/common/plugstack.h" #include "src/slurmd/common/slurmstepd_init.h" #include "src/slurmd/common/setproctitle.h" @@ -178,6 +179,28 @@ ending: } +static int get_jobid_uid_from_env (uint32_t *jobidp, uid_t *uidp) +{ + const char *val; + char *p; + + if (!(val = getenv ("SLURM_JOBID"))) + return error ("Unable to get SLURM_JOBID in env!"); + + *jobidp = (uint32_t) strtoul (val, &p, 10); + if (*p != '\0') + return error ("Invalid SLURM_JOBID=%s", val); + + if (!(val = getenv ("SLURM_UID"))) + return error ("Unable to get SLURM_UID in env!"); + + *uidp = (uid_t) strtoul (val, &p, 10); + if (*p != '\0') + return error ("Invalid SLURM_UID=%s", val); + + return (0); +} + /* * Process special "modes" of slurmstepd passed as cmdline arguments. */ @@ -188,6 +211,31 @@ static int process_cmdline (int argc, char *argv[]) _dump_user_env(); exit(0); } + if ((argc == 3) && (strcmp(argv[1], "spank") == 0)) { + const char *mode = argv[2]; + uid_t uid = (uid_t) -1; + uint32_t jobid = (uint32_t) -1; + log_options_t opts = { LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG2, LOG_LEVEL_QUIET, 1, 0}; + log_alter (opts, 0, NULL); + + if (slurm_conf_init(NULL) != SLURM_SUCCESS) + fatal ("Failed to read slurm config"); + + if (get_jobid_uid_from_env (&jobid, &uid) < 0) + fatal ("spank environment invalid"); + + if (strcmp (mode, "prolog") == 0) { + if (spank_job_prolog (jobid, uid) < 0) + exit (1); + exit (0); + } + if (strcmp (mode, "epilog") == 0) { + if (spank_job_epilog (jobid, uid) < 0) + exit (1); + exit (0); + } + fatal ("Invalid slurmstepd spank mode specified"); + } return (0); } -- GitLab