-
Notifications
You must be signed in to change notification settings - Fork 3
/
spank_collect_script.c
340 lines (270 loc) · 9.2 KB
/
spank_collect_script.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2016, Yong Qin <[email protected]>. All rights reserved.
*
* spank_collect_script.c: SPANK plugin to collect job script.
*
* This plugin creates a separate directory for each day on a shared stoarge
* and copy the job script to that location. Another way to achieve this is to
* instrument a slurmctld prolog and collect the job script from the hash dirs
* within $StateSaveLocation.
*
* gcc -shared -fPIC -o spank_collect_script.so spank_collect_script.c
*
* plugstack.conf:
* required /etc/slurm/spank/spank_collect_script.so source=/var/slurm/spool
* target=shared_dir [uid=new_uid] [gid=new_gid]
*
* Note: new_uid and new_gid have to be what SlurmdUser can switch to
* (setuid/gid). They can also be ignore (optional arguments), or set to
* -1 to not to change from the current user/group.
*
*/
#include <errno.h>
#include <limits.h>
#include <linux/limits.h>
#include <slurm/spank.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
SPANK_PLUGIN (spank_collect_script, 1);
const char *myname = "spank_collect_script";
/* Convert a string to UID/GID. */
int _str2id (char * str, int *p2int) {
long int l;
char *p;
l = strtol(str, &p, 10);
if ((*p != '\0') || (l > UINT_MAX) || (l < 0)) {
return -1;
}
*p2int = l;
return 0;
}
/* Get current date string in "%F" ("%Y-%m-%d") format. */
int _get_datestr (char *ds, int len) {
time_t t;
struct tm *lt;
/* Obtain current time. */
t = time(NULL);
if (t == ((time_t) -1)) {
slurm_error("%s: Unable to get current time", myname);
return -1;
}
/* Convert to local time. */
lt = localtime(&t);
if (lt == NULL) {
slurm_error("%s: Unable to convert to local time", myname);
return -1;
}
/* Convert to string format. */
if (strftime(ds, len, "%F", lt) == 0) {
slurm_error("%s: Unable to convert to date string", myname);
return -1;
}
return 0;
}
/* Restore UID, GID, and free buffer before exit. */
int _clean_exit (uid_t ruid, gid_t rgid, char *buffer) {
if (rgid != -1)
setegid(rgid);
if (ruid != -1)
seteuid(ruid);
if (buffer != NULL)
free(buffer);
}
/* Make a copy of the current job script in slurm_spank_init(). */
int slurm_spank_init (spank_t sp, int ac, char **av) {
int i;
int rv;
/* Effective and real user/group. */
uid_t euid = -1;
gid_t egid = -1;
uid_t ruid = -1;
gid_t rgid = -1;
uint32_t jobid;
/* Date string in "%F" ("%Y-%m-%d") format. */
char ds[11];
/* Source and target base directory locations. */
char *source_base = NULL;
char *target_base = NULL;
/* Target directory location. */
char target_dir[PATH_MAX];
/* Source and target filenames. */
char source_file[PATH_MAX];
char target_file[PATH_MAX];
/* File handle for read and write. */
FILE *fd = NULL;
/* Buffer to store the source file. */
char *buffer = NULL;
/* Size of the buffer. */
long fsize = 0;
/* If not in a remote context no need to proceed. */
if (spank_remote(sp) != 1) return 0;
/* Processing command line arguments. */
for (i = 0; i < ac; i++) {
if (strncmp("source=", av[i], 7) == 0) {
source_base = av[i] + 7;
} else if (strncmp("target=", av[i], 7) == 0) {
target_base = av[i] + 7;
} else if (strncmp("uid=", av[i], 4) == 0) {
if (_str2id(av[i] + 4, &euid)) {
slurm_error("%s: Unable to conver string \"%s\" to UID", myname, av[i] + 4);
return -1;
}
if (euid == getuid()) {
euid = -1;
} else {
ruid = getuid();
}
} else if (strncmp("gid=", av[i], 4) == 0) {
if (_str2id(av[i] + 4, &egid)) {
slurm_error("%s: Unable to conver string \"%s\" to GID", myname, av[i] + 4);
return -1;
}
if (egid == getgid()) {
egid = -1;
} else {
rgid = getgid();
}
}
}
/* Sanity checking of the source and target's existence. */
if (source_base == NULL) {
slurm_error("%s: syntax: %s source=foo target=bar", myname, myname);
slurm_error("%s: missing source location", myname);
return -1;
}
if (access(source_base, F_OK) == -1) {
slurm_error("%s: %s does not exist", myname, source_base);
return -1;
}
if (target_base == NULL) {
slurm_error("%s: syntax: %s source=foo target=bar", myname, myname);
slurm_error("%s: missing target location", myname);
return -1;
}
if (access(target_base, F_OK) == -1) {
slurm_error("%s: %s does not exist", myname, target_base);
return -1;
}
if (spank_get_item(sp, S_JOB_ID, &jobid)) {
slurm_error("%s: Unable to get JOBID", myname);
return -1;
}
/* Construct current job script location. */
rv = snprintf(source_file, PATH_MAX, "%s/job%05d/slurm_script", source_base, jobid);
if (rv < 0 || rv > PATH_MAX - 1) {
slurm_error("%s: Unable to construct job script location: %s/job%05d/slurm_script", myname, source_base, jobid);
return -1;
}
/* If job script does not exist no need to proceed. */
if (access(source_file, F_OK) == -1) {
slurm_info("%s: Job script %s does not exist, ignore", myname, source_file);
return 0;
}
/* Open the source job script. */
fd = fopen(source_file, "rb");
if (fd == NULL) {
slurm_error("%s: Unable to open %s: %m", myname, source_file);
return -1;
}
/* Get the size of the source job script. */
if (fseek(fd, 0L, SEEK_END)) {
slurm_error("%s: Unable to fseek to end of %s: %m", myname, source_file);
return -1;
}
fsize = ftell(fd);
/* If source job script is empty no need to proceed. */
if (fsize == 0) {
slurm_info("%s: %s is empty", myname, source_file);
return 0;
}
if (fsize == -1) {
slurm_error("%s: error getting size of %s: %m", myname, source_file);
return -1;
}
if (fseek(fd, 0L, SEEK_SET)) {
slurm_error("%s: Unable to fseek to start of %s: %m", myname, source_file);
return -1;
}
/* Allocate buffer. */
buffer = malloc((fsize + 1) * sizeof(char));
if (buffer == NULL) {
slurm_error("%s: Unable to allocate buffer: %m", myname);
return -1;
}
/* Read the job script into buffer. */
fread(buffer, fsize, 1, fd);
buffer[fsize] = 0;
if (ferror(fd)) {
slurm_error("%s: Error on reading %s: %m", myname, source_file);
free(buffer);
return -1;
}
/* Close the source file. */
fclose(fd);
/* Obtain current date string. */
if (_get_datestr(ds, sizeof(ds))) {
slurm_error("%s: Unable to get current date string", myname);
free(buffer);
return -1;
}
/* Construct target directory location to store daily job scripts. */
rv = snprintf(target_dir, PATH_MAX, "%s/%s", target_base, ds);
if (rv < 0 || rv > PATH_MAX - 1) {
slurm_error("%s: Unable to contruct target directory: %s/%s", myname, target_base, ds);
free(buffer);
return -1;
}
/* Construct target file location to save current job script. */
rv = snprintf(target_file, PATH_MAX, "%s/%s/job%d", target_base, ds, jobid);
if (rv < 0 || rv > PATH_MAX - 1) {
slurm_error("%s: Unable to construct target_file: %s/job%d", myname, target_base, jobid);
free(buffer);
return -1;
}
/* Ignore if target file exists - to prevent overwritten by multiple job steps. */
if (access(target_file, F_OK) == 0) {
slurm_info("%s: %s exists, ignore", myname, target_file);
free(buffer);
return 0;
}
/* Switch user. */
if (egid != -1 && setegid(egid)) {
slurm_error("%s: Unable to setegid(%d): %m", myname, egid);
_clean_exit(ruid, rgid, buffer);
return -1;
}
if (euid != -1 && seteuid(euid)) {
slurm_error("%s: Unable to seteuid(%d): %m", myname, euid);
_clean_exit(ruid, rgid, buffer);
return -1;
}
/* Create target directory to store job scripts. If it doesn't exist, create it, otherwise ignore. */
if (mkdir(target_dir, 0750) && errno != EEXIST) {
slurm_error("%s: Unable to mkdir(%s): %m", myname, target_dir);
_clean_exit(ruid, rgid, buffer);
return -1;
}
/* Open the target file for write. */
fd = fopen(target_file, "wb");
if (fd == NULL) {
slurm_error("%s: Unable to open %s: %m", myname, target_file);
_clean_exit(ruid, rgid, buffer);
return -1;
}
/* Write buffer to the target job script file. */
fwrite(buffer, fsize, 1, fd);
if (ferror(fd)) {
slurm_error("%s: Error on writing %s: %m", myname, target_file);
_clean_exit(ruid, rgid, buffer);
return -1;
}
/* Close the target file. */
fclose(fd);
slurm_info("%s: Job script saved as %s", myname, target_file);
/* Clean exit. */
_clean_exit(ruid, rgid, buffer);
return 0;
}