1 /*****************************************************************************\
2  *  cli_filter.c - driver for the cli_filter plugin
3  *****************************************************************************
4  *  Copyright (C) 2017-2019 Regents of the University of California
5  *  Produced at Lawrence Berkeley National Laboratory (cf, DISCLAIMER).
6  *  Written by Douglas Jacobsen <dmjacobsen@lbl.gov>
7  *  All rights reserved.
8  *
9  *  This file is part of SLURM, a resource management program.
10  *  For details, see <https://slurm.schedmd.com/>.
11  *  Please also read the included file: DISCLAIMER.
12  *
13  *  SLURM is free software; you can redistribute it and/or modify it under
14  *  the terms of the GNU General Public License as published by the Free
15  *  Software Foundation; either version 2 of the License, or (at your option)
16  *  any later version.
17  *
18  *  In addition, as a special exception, the copyright holders give permission
19  *  to link the code of portions of this program with the OpenSSL library under
20  *  certain conditions as described in each individual source file, and
21  *  distribute linked combinations including the two. You must obey the GNU
22  *  General Public License in all respects for all of the code used other than
23  *  OpenSSL. If you modify file(s) with this exception, you may extend this
24  *  exception to your version of the file(s), but you are not obligated to do
25  *  so. If you do not wish to do so, delete this exception statement from your
26  *  version.  If you delete this exception statement from all source files in
27  *  the program, then also delete it here.
28  *
29  *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
30  *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
31  *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
32  *  details.
33  *
34  *  You should have received a copy of the GNU General Public License along
35  *  with SLURM; if not, write to the Free Software Foundation, Inc.,
36  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
37 \*****************************************************************************/
38 
39 #include <inttypes.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 
46 #include "slurm/slurm.h"
47 #include "slurm/slurm_errno.h"
48 
49 #include "src/common/cli_filter.h"
50 #include "src/common/macros.h"
51 #include "src/common/plugin.h"
52 #include "src/common/plugrack.h"
53 #include "src/common/slurm_opt.h"
54 #include "src/common/slurm_protocol_api.h"
55 #include "src/common/timers.h"
56 #include "src/common/xmalloc.h"
57 #include "src/common/xstring.h"
58 
59 typedef struct cli_filter_ops {
60 	int		(*setup_defaults)(slurm_opt_t *opt, bool early);
61 	int		(*pre_submit)	 (slurm_opt_t *opt, int offset);
62 	void		(*post_submit)	 (int offset, uint32_t jobid,
63 					  uint32_t stepid);
64 } cli_filter_ops_t;
65 
66 /*
67  * Must be synchronized with cli_filter_ops_t above.
68  */
69 static const char *syms[] = {
70 	"setup_defaults",
71 	"pre_submit",
72 	"post_submit"
73 };
74 
75 static int g_context_cnt = -1;
76 static cli_filter_ops_t *ops = NULL;
77 static plugin_context_t **g_context = NULL;
78 static char *clifilter_plugin_list = NULL;
79 static pthread_mutex_t g_context_lock = PTHREAD_MUTEX_INITIALIZER;
80 static bool init_run = false;
81 
82 /*
83  * Initialize the cli filter plugin.
84  *
85  * Returns a SLURM errno.
86  */
cli_filter_plugin_init(void)87 extern int cli_filter_plugin_init(void)
88 {
89 	int rc = SLURM_SUCCESS;
90 	char *last = NULL, *names;
91 	char *plugin_type = "cli_filter";
92 	char *type;
93 
94 	if (init_run && (g_context_cnt >= 0))
95 		return rc;
96 
97 	slurm_mutex_lock(&g_context_lock);
98 	if (g_context_cnt >= 0)
99 		goto fini;
100 
101 	clifilter_plugin_list = slurm_get_cli_filter_plugins();
102 	g_context_cnt = 0;
103 	if ((clifilter_plugin_list == NULL) || (clifilter_plugin_list[0] == '\0'))
104 		goto fini;
105 
106 	names = clifilter_plugin_list;
107 	while ((type = strtok_r(names, ",", &last))) {
108 		xrecalloc(ops, g_context_cnt + 1, sizeof(cli_filter_ops_t));
109 		xrecalloc(g_context, g_context_cnt + 1,
110 			  sizeof(plugin_context_t *));
111 		/* Permit both prefix and no-prefix for plugin names. */
112 		if (xstrncmp(type, "cli_filter/", 11) == 0)
113 			type += 11;
114 		type = xstrdup_printf("cli_filter/%s", type);
115 		g_context[g_context_cnt] = plugin_context_create(
116 			plugin_type, type, (void **)&ops[g_context_cnt],
117 			syms, sizeof(syms));
118 		if (!g_context[g_context_cnt]) {
119 			error("cannot create %s context for %s",
120 			      plugin_type, type);
121 			rc = SLURM_ERROR;
122 			xfree(type);
123 			break;
124 		}
125 
126 		xfree(type);
127 		g_context_cnt++;
128 		names = NULL; /* for next strtok_r() iteration */
129 	}
130 	init_run = true;
131 
132 fini:
133 	slurm_mutex_unlock(&g_context_lock);
134 
135 	if (rc != SLURM_SUCCESS)
136 		cli_filter_plugin_fini();
137 
138 	return rc;
139 }
140 
141 /*
142  * Terminate the cli filter plugin. Free memory.
143  *
144  * Returns a SLURM errno.
145  */
cli_filter_plugin_fini(void)146 extern int cli_filter_plugin_fini(void)
147 {
148 	int i, j, rc = SLURM_SUCCESS;
149 
150 	slurm_mutex_lock(&g_context_lock);
151 	if (g_context_cnt < 0)
152 		goto fini;
153 
154 	init_run = false;
155 	for (i = 0; i < g_context_cnt; i++) {
156 		if (g_context[i]) {
157 			j = plugin_context_destroy(g_context[i]);
158 			if (j != SLURM_SUCCESS)
159 				rc = j;
160 		}
161 	}
162 	xfree(ops);
163 	xfree(g_context);
164 	xfree(clifilter_plugin_list);
165 	g_context_cnt = -1;
166 
167 fini:
168 	slurm_mutex_unlock(&g_context_lock);
169 	return rc;
170 }
171 
172 /*
173  **************************************************************************
174  *                          P L U G I N   C A L L S                       *
175  **************************************************************************
176  */
177 
cli_filter_plugin_setup_defaults(slurm_opt_t * opt,bool early)178 extern int cli_filter_plugin_setup_defaults(slurm_opt_t *opt, bool early)
179 {
180 	DEF_TIMERS;
181 	int i, rc;
182 
183 	START_TIMER;
184 	rc = cli_filter_plugin_init();
185 	slurm_mutex_lock(&g_context_lock);
186 	for (i = 0; ((i < g_context_cnt) && (rc == SLURM_SUCCESS)); i++)
187 		rc = (*(ops[i].setup_defaults))(opt, early);
188 	slurm_mutex_unlock(&g_context_lock);
189 	END_TIMER2(__func__);
190 
191 	return rc;
192 }
193 
cli_filter_plugin_pre_submit(slurm_opt_t * opt,int offset)194 extern int cli_filter_plugin_pre_submit(slurm_opt_t *opt, int offset)
195 {
196 	DEF_TIMERS;
197 	int i, rc;
198 
199 	START_TIMER;
200 	rc = cli_filter_plugin_init();
201 	slurm_mutex_lock(&g_context_lock);
202 	for (i = 0; ((i < g_context_cnt) && (rc == SLURM_SUCCESS)); i++)
203 		rc = (*(ops[i].pre_submit))(opt, offset);
204 	slurm_mutex_unlock(&g_context_lock);
205 	END_TIMER2(__func__);
206 
207 	return rc;
208 }
209 
cli_filter_plugin_post_submit(int offset,uint32_t jobid,uint32_t stepid)210 extern void cli_filter_plugin_post_submit(int offset, uint32_t jobid,
211 					  uint32_t stepid)
212 {
213 	DEF_TIMERS;
214 	int i, rc;
215 
216 	START_TIMER;
217 	rc = cli_filter_plugin_init();
218 	slurm_mutex_lock(&g_context_lock);
219 	for (i = 0; ((i < g_context_cnt) && (rc == SLURM_SUCCESS)); i++)
220 		(*(ops[i].post_submit))(offset, jobid, stepid);
221 	slurm_mutex_unlock(&g_context_lock);
222 	END_TIMER2(__func__);
223 }
224