1 /*****************************************************************************\
2 * slurmctld_plugstack.c - driver for slurmctld plugstack plugin
3 *****************************************************************************
4 * Copyright (C) 2012 SchedMD LLC
5 * Written by Morris Jette <jette@schedmd.com>
6 *
7 * This file is part of Slurm, a resource management program.
8 * For details, see <https://slurm.schedmd.com/>.
9 * Please also read the included file: DISCLAIMER.
10 *
11 * Slurm is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * In addition, as a special exception, the copyright holders give permission
17 * to link the code of portions of this program with the OpenSSL library under
18 * certain conditions as described in each individual source file, and
19 * distribute linked combinations including the two. You must obey the GNU
20 * General Public License in all respects for all of the code used other than
21 * OpenSSL. If you modify file(s) with this exception, you may extend this
22 * exception to your version of the file(s), but you are not obligated to do
23 * so. If you do not wish to do so, delete this exception statement from your
24 * version. If you delete this exception statement from all source files in
25 * the program, then also delete it here.
26 *
27 * Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
28 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
30 * details.
31 *
32 * You should have received a copy of the GNU General Public License along
33 * with Slurm; if not, write to the Free Software Foundation, Inc.,
34 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 \*****************************************************************************/
36
37 #include <inttypes.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42
43 #include "slurm/slurm.h"
44 #include "slurm/slurm_errno.h"
45
46 #include "src/common/macros.h"
47 #include "src/common/plugin.h"
48 #include "src/common/plugrack.h"
49 #include "src/common/read_config.h"
50 #include "src/common/slurm_protocol_api.h"
51 #include "src/common/xmalloc.h"
52 #include "src/common/xstring.h"
53 #include "src/slurmctld/slurmctld_plugstack.h"
54
55 slurm_nonstop_ops_t nonstop_ops = { NULL, NULL, NULL };
56
57 typedef struct slurmctld_plugstack_ops {
58 void (*get_config) (config_plugin_params_t *p);
59 } slurmctld_plugstack_ops_t;
60
61 /*
62 * Must be synchronized with slurmctld_plugstack_t above.
63 */
64 static const char *syms[] = {
65 "slurmctld_plugstack_p_get_config"
66 };
67
68 static int g_context_cnt = -1;
69 static slurmctld_plugstack_ops_t *ops = NULL;
70 static plugin_context_t **g_context = NULL;
71 static char *slurmctld_plugstack_list = NULL;
72 static pthread_mutex_t g_context_lock = PTHREAD_MUTEX_INITIALIZER;
73 static bool init_run = false;
74
75 /*
76 * Initialize the slurmctld plugstack plugin.
77 *
78 * Returns a Slurm errno.
79 */
slurmctld_plugstack_init(void)80 extern int slurmctld_plugstack_init(void)
81 {
82 int rc = SLURM_SUCCESS;
83 char *last = NULL, *names;
84 char *plugin_type = "slurmctld_plugstack";
85 char *type;
86
87 if (init_run && (g_context_cnt >= 0))
88 return rc;
89
90 slurm_mutex_lock(&g_context_lock);
91 if (g_context_cnt >= 0)
92 goto fini;
93
94 slurmctld_plugstack_list = slurm_get_slurmctld_plugstack();
95 g_context_cnt = 0;
96 if ((slurmctld_plugstack_list == NULL) ||
97 (slurmctld_plugstack_list[0] == '\0'))
98 goto fini;
99
100 names = slurmctld_plugstack_list;
101 while ((type = strtok_r(names, ",", &last))) {
102 xrealloc(ops, (sizeof(slurmctld_plugstack_ops_t) *
103 (g_context_cnt + 1)));
104 xrealloc(g_context,
105 (sizeof(plugin_context_t *) * (g_context_cnt + 1)));
106 if (xstrncmp(type, "slurmctld/", 10) == 0)
107 type += 10; /* backward compatibility */
108 type = xstrdup_printf("slurmctld/%s", type);
109 g_context[g_context_cnt] = plugin_context_create(
110 plugin_type, type, (void **)&ops[g_context_cnt],
111 syms, sizeof(syms));
112 if (!g_context[g_context_cnt]) {
113 error("cannot create %s context for %s",
114 plugin_type, type);
115 rc = SLURM_ERROR;
116 xfree(type);
117 break;
118 }
119
120 xfree(type);
121 g_context_cnt++;
122 names = NULL; /* for next iteration */
123 }
124 init_run = true;
125
126 fini:
127 slurm_mutex_unlock(&g_context_lock);
128
129 if (rc != SLURM_SUCCESS)
130 slurmctld_plugstack_fini();
131
132 return rc;
133 }
134
135 /*
136 * Terminate the slurmctld plugstack plugin. Free memory.
137 *
138 * Returns a Slurm errno.
139 */
slurmctld_plugstack_fini(void)140 extern int slurmctld_plugstack_fini(void)
141 {
142 int i, j, rc = SLURM_SUCCESS;
143
144 slurm_mutex_lock(&g_context_lock);
145 if (g_context_cnt < 0)
146 goto fini;
147
148 init_run = false;
149 for (i=0; i<g_context_cnt; i++) {
150 if (g_context[i]) {
151 j = plugin_context_destroy(g_context[i]);
152 if (j != SLURM_SUCCESS)
153 rc = j;
154 }
155 }
156 xfree(ops);
157 xfree(g_context);
158 xfree(slurmctld_plugstack_list);
159 g_context_cnt = -1;
160
161 fini: slurm_mutex_unlock(&g_context_lock);
162 return rc;
163 }
164
165 /*
166 * Gets the configuration for all slurmctld plugins in a List of
167 * config_plugin_params_t elements. For each plugin this consists on:
168 * - Plugin name
169 * - List of key,pairs
170 * Returns List or NULL.
171 */
slurmctld_plugstack_g_get_config(void)172 extern List slurmctld_plugstack_g_get_config(void)
173 {
174 DEF_TIMERS;
175 int i, rc;
176 List conf_list = NULL;
177 config_plugin_params_t *p;
178
179 START_TIMER;
180 rc = slurmctld_plugstack_init();
181
182 if (g_context_cnt > 0)
183 conf_list = list_create(destroy_config_plugin_params);
184
185 slurm_mutex_lock(&g_context_lock);
186 for (i = 0; ((i < g_context_cnt) && (rc == SLURM_SUCCESS)); i++) {
187 p = xmalloc(sizeof(config_plugin_params_t));
188 p->key_pairs = list_create(destroy_config_key_pair);
189
190 (*(ops[i].get_config))(p);
191
192 if (!p->name)
193 destroy_config_plugin_params(p);
194 else
195 list_append(conf_list, p);
196 }
197 slurm_mutex_unlock(&g_context_lock);
198
199 END_TIMER2("slurmctld_plugstack_g_get_config");
200
201 return conf_list;
202 }
203