forked from hpcugent/slurm-spank-lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua.c
1495 lines (1267 loc) · 37.7 KB
/
lua.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* Copyright (C) 2007-2008 Lawrence Livermore National Security, LLC.
* Produced at Lawrence Livermore National Laboratory.
* Written by Mark Grondona <mgrondona@llnl.gov>.
*
* UCRL-CODE-235358
*
* This file is part of chaos-spankings, a set of spank plugins for SLURM.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <glob.h>
#include <setjmp.h> /* need longjmp for lua_atpanic */
#include <libgen.h> /* basename(3) */
#include <sys/types.h>
#include <sys/wait.h>
#include <slurm/spank.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "lib/list.h"
SPANK_PLUGIN (lua, 1)
/* Name of spank_t lightuserdata reference in
* spank table passed to lua spank functions.
*/
#define SPANK_REFNAME "spank"
/*
* This module keeps a list of options provided by the lua
* script so that it can easily map the option val (s_opt.val)
* back to the lua fucntion and val (l_function and l_val)
*
* This is only necessary because SLURM's spank option callbacks
* do not provide any state or context besides a plugin-specific
* option value.
*
*/
struct lua_script_option {
struct lua_script * script;
int l_val;
char * l_function;
struct spank_option s_opt;
};
/*
* Global script_option_list declaration:
*/
static List script_option_list = NULL;
/*
* Structure describing an individual lua script
* and a list of such scripts
*/
struct lua_script {
char *path;
lua_State *L;
int fail_on_error;
};
List lua_script_list = NULL;
/*
* Tell lua_atpanic where to longjmp on exceptions:
*/
static jmp_buf panicbuf;
static int spank_atpanic (lua_State *L) { longjmp (panicbuf, 0); }
/*
* Lua scripts pass string versions of spank_item_t to get/set_time.
* This table maps the name to item and vice versa.
*/
#define SPANK_ITEM(x) { (x), #x }
#define SPANK_ITEM_END { 0, NULL }
static struct s_item_name {
spank_item_t item;
const char *name;
} spank_item_table [] = {
SPANK_ITEM(S_JOB_UID),
SPANK_ITEM(S_JOB_GID),
SPANK_ITEM(S_JOB_ID),
SPANK_ITEM(S_JOB_STEPID),
SPANK_ITEM(S_JOB_NNODES),
SPANK_ITEM(S_JOB_NODEID),
SPANK_ITEM(S_JOB_LOCAL_TASK_COUNT),
SPANK_ITEM(S_JOB_TOTAL_TASK_COUNT),
SPANK_ITEM(S_JOB_NCPUS),
SPANK_ITEM(S_JOB_ARGV),
SPANK_ITEM(S_JOB_ENV),
SPANK_ITEM(S_TASK_ID),
SPANK_ITEM(S_TASK_GLOBAL_ID),
SPANK_ITEM(S_TASK_EXIT_STATUS),
SPANK_ITEM(S_TASK_PID),
SPANK_ITEM(S_JOB_PID_TO_GLOBAL_ID),
SPANK_ITEM(S_JOB_PID_TO_LOCAL_ID),
SPANK_ITEM(S_JOB_LOCAL_TO_GLOBAL_ID),
SPANK_ITEM(S_JOB_GLOBAL_TO_LOCAL_ID),
SPANK_ITEM(S_JOB_SUPPLEMENTARY_GIDS),
SPANK_ITEM(S_SLURM_VERSION),
SPANK_ITEM(S_SLURM_VERSION_MAJOR),
SPANK_ITEM(S_SLURM_VERSION_MINOR),
SPANK_ITEM(S_SLURM_VERSION_MICRO),
SPANK_ITEM(S_STEP_CPUS_PER_TASK),
SPANK_ITEM(S_JOB_ALLOC_CORES),
SPANK_ITEM(S_JOB_ALLOC_MEM),
SPANK_ITEM(S_STEP_ALLOC_CORES),
SPANK_ITEM(S_STEP_ALLOC_MEM),
SPANK_ITEM_END
};
/*****************************************************************************
*
* Lua script interface functions:
*
****************************************************************************/
static int l_spank_error_msg (lua_State *L, const char *msg)
{
lua_pushnil (L);
lua_pushstring (L, msg);
return (2);
}
static int l_spank_error (lua_State *L, spank_err_t e)
{
return l_spank_error_msg (L, spank_strerror (e));
}
/*
* Get lua function return code.
* Functions must return -1 for failure, anything else
* indicates success (Including no return code, which
* would be nil at top of stack created by lua_pcall()).
*
*/
static int lua_script_rc (lua_State *L)
{
int rc = 0;
if (lua_isnumber (L, -1))
rc = lua_tonumber (L, -1);
/* Clean up the stack */
lua_pop (L, 0);
return (rc);
}
/*
* Return spank_t handle as lightuserdata from the spank table
* at index [index] on the Lua stack.
*/
static spank_t lua_getspank (lua_State *L, int index)
{
spank_t sp;
if (!lua_istable (L, 1))
luaL_error (L, "Invalid argument expected table got %s",
luaL_typename (L, 1));
lua_getfield (L, index, SPANK_REFNAME);
if (!lua_islightuserdata (L, -1))
return (NULL);
sp = lua_touserdata (L, -1);
/* Pop lightuserdata off the stack */
lua_pop (L, 1);
return (sp);
}
/*
* Convert a spank item by name to a spank_item_t enum (as integer).
*/
static int name_to_item (const char *name)
{
struct s_item_name *s = spank_item_table;
while (s->name != NULL) {
if (strcmp (s->name, name) == 0) {
return (s->item);
}
s++;
}
return (-1);
}
/*
* Generic function to push a spank item with a numeric (int) representation
* on to the lua stack.
*/
static int l_spank_get_item_int (lua_State *L, spank_t sp, spank_item_t item)
{
spank_err_t err;
int val;
err = spank_get_item (sp, item, &val);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushnumber (L, val);
return (1);
}
/*
* Generic function to push a spank item with a numeric (long) representation
* on to the lua stack.
*/
static int l_spank_get_item_long (lua_State *L, spank_t sp, spank_item_t item)
{
spank_err_t err;
long val;
err = spank_get_item (sp, item, &val);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushnumber (L, val);
return (1);
}
/*
* Generic function to push a spank item with a string representation
* on to the lua stack.
*/
static int
l_spank_get_item_string (lua_State *L, spank_t sp, spank_item_t item)
{
spank_err_t err;
const char *s;
err = spank_get_item (sp, item, &s);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushstring (L, s);
return (1);
}
/*
* Return S_JOB_ARGV as an array on the lua stack
*/
static int l_spank_get_item_argv (lua_State *L, spank_t sp)
{
spank_err_t err;
const char **av;
int i, ac;
err = spank_get_item (sp, S_JOB_ARGV, &ac, &av);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_newtable (L);
for (i = 0; i < ac; i++) {
lua_pushstring (L, av[i]);
lua_rawseti (L, -2, i+1);
}
return (1);
}
/*
* Set a single environment entry as an item in the table
* at the top of the stack such that t[NAME] = VALUE.
*/
static void set_env_table_entry (lua_State *L, int i, const char *entry)
{
const char *val = strchr (entry, '=');
if (val == NULL) {
lua_pushstring (L, entry);
lua_pushstring (L, "");
}
else {
lua_pushlstring (L, entry, val - entry);
lua_pushstring (L, val+1);
}
lua_settable (L, i);
}
/*
* Copy S_JOB_ENV to a table on the Lua stack.
*/
static int l_spank_get_item_env (lua_State *L, spank_t sp)
{
spank_err_t err;
const char **env;
const char **p;
int t;
err = spank_get_item (sp, S_JOB_ENV, &env);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_newtable (L);
t = lua_gettop (L);
for (p = env; *p != NULL; p++)
set_env_table_entry (L, t, *p);
return (1);
}
/*
* Copy GID list as array on the Lua stack.
*/
static int l_spank_get_item_gids (lua_State *L, spank_t sp)
{
spank_err_t err;
gid_t *gids;
int i, ngids;
err = spank_get_item (sp, S_JOB_SUPPLEMENTARY_GIDS, &gids, &ngids);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_newtable (L);
for (i = 0; i < ngids; i++) {
lua_pushnumber (L, gids[i]);
lua_rawseti (L, -2, i+1);
}
return (1);
}
static int
l_spank_id_query (lua_State *L, spank_t sp, spank_item_t item)
{
spank_err_t err;
long rv, id;
id = luaL_checknumber (L, -1);
lua_pop (L, 1);
err = spank_get_item (sp, item, id, &rv);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushnumber (L, rv);
return (1);
}
static int l_spank_get_exit_status (lua_State *L, spank_t sp)
{
spank_err_t err;
int status;
err = spank_get_item (sp, S_TASK_EXIT_STATUS, &status);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushnumber (L, status);
/*
* Now push WEXITSTATUS or nil if !WIFEXITED and
* WTERMSIG, WCOREDUMP or nil if !WIFSIGNALED
*/
if (WIFEXITED (status))
lua_pushnumber (L, WEXITSTATUS (status));
else
lua_pushnil (L);
if (WIFSIGNALED (status)) {
lua_pushnumber (L, WTERMSIG (status));
lua_pushnumber (L, WCOREDUMP (status));
}
else {
lua_pushnil (L);
lua_pushnil (L);
}
/* Returns 4 values: status, exitcode, termsig, coredumped */
return (4);
}
static int l_spank_get_item (lua_State *L)
{
spank_t sp;
int item;
sp = lua_getspank (L, 1);
item = name_to_item (lua_tostring (L, 2));
if (item < 0)
return luaL_error (L,"Invalid spank item %s", lua_tostring (L, 2));
switch (item) {
case S_JOB_UID:
case S_JOB_GID:
case S_JOB_ID:
case S_JOB_STEPID:
case S_JOB_NNODES:
case S_JOB_NODEID:
case S_JOB_LOCAL_TASK_COUNT:
case S_JOB_TOTAL_TASK_COUNT:
case S_JOB_NCPUS:
case S_TASK_ID:
case S_TASK_GLOBAL_ID:
case S_TASK_PID:
case S_STEP_CPUS_PER_TASK:
return l_spank_get_item_int (L, sp, item);
case S_JOB_ALLOC_MEM:
case S_STEP_ALLOC_MEM:
return l_spank_get_item_long (L, sp, item);
case S_JOB_ALLOC_CORES:
case S_STEP_ALLOC_CORES:
case S_SLURM_VERSION:
case S_SLURM_VERSION_MAJOR:
case S_SLURM_VERSION_MINOR:
case S_SLURM_VERSION_MICRO:
return l_spank_get_item_string (L, sp, item);
case S_JOB_ARGV:
return l_spank_get_item_argv (L, sp);
case S_JOB_ENV:
return l_spank_get_item_env (L, sp);
case S_JOB_SUPPLEMENTARY_GIDS:
return l_spank_get_item_gids (L, sp);
case S_JOB_PID_TO_GLOBAL_ID:
case S_JOB_PID_TO_LOCAL_ID:
case S_JOB_LOCAL_TO_GLOBAL_ID:
case S_JOB_GLOBAL_TO_LOCAL_ID:
return l_spank_id_query (L, sp, item);
case S_TASK_EXIT_STATUS:
return l_spank_get_exit_status (L, sp);
}
return luaL_error (L, "Unhandled spank item: %s", lua_tostring (L, 2));
}
typedef spank_err_t (*setenv_f) (spank_t, const char *, const char *, int);
typedef spank_err_t (*getenv_f) (spank_t, const char *, char *, int);
typedef spank_err_t (*unsetenv_f) (spank_t, const char *);
static int l_do_getenv (lua_State *L, getenv_f fn)
{
spank_err_t err;
spank_t sp;
const char *var;
char buf[4096];
sp = lua_getspank (L, 1);
var = luaL_checkstring (L, 2);
err = (*fn) (sp, var, buf, sizeof (buf));
lua_pop (L, 0);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushstring (L, buf);
return (1);
}
static int l_do_setenv (lua_State *L, setenv_f fn)
{
spank_t sp;
const char *var;
const char *val;
int overwrite;
int err;
sp = lua_getspank (L, 1);
var = luaL_checkstring (L, 2);
val = luaL_checkstring (L, 3);
overwrite = lua_tonumber (L, 4); /* 0 by default */
err = (*fn) (sp, var, val, overwrite);
lua_pop (L, 0);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushboolean (L, 1);
return (1);
}
static int l_do_unsetenv (lua_State *L, unsetenv_f fn)
{
int err = (*fn) (lua_getspank (L, 1), luaL_checkstring (L, 2));
lua_pop (L, 2);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushboolean (L, 1);
return (1);
}
static int l_spank_setenv (lua_State *L)
{
return l_do_setenv (L, spank_setenv);
}
static int l_spank_getenv (lua_State *L)
{
return l_do_getenv (L, spank_getenv);
}
static int l_spank_unsetenv (lua_State *L)
{
return l_do_unsetenv (L, spank_unsetenv);
}
static void * sym_lookup (const char *name)
{
static void *h = NULL;
if (h == NULL)
h = dlopen (NULL, 0);
return dlsym (h, name);
}
static int l_spank_job_control_setenv (lua_State *L)
{
setenv_f f = sym_lookup ("spank_job_control_setenv");
if (f == NULL)
return l_spank_error_msg (L,
"spank_job_control_setenv not implemented in this version");
return l_do_setenv (L, f);
}
static int l_spank_job_control_getenv (lua_State *L)
{
getenv_f f = sym_lookup ("spank_job_control_getenv");
if (f == NULL)
return l_spank_error_msg (L,
"spank_job_control_getenv not implemented in this version");
return l_do_getenv (L, f);
}
static int l_spank_job_control_unsetenv (lua_State *L)
{
unsetenv_f f = sym_lookup ("spank_job_control_unsetenv");
if (f == NULL)
return l_spank_error_msg (L,
"spank_job_control_unsetenv not implemented in this version");
return l_do_unsetenv (L, f);
}
static int s_opt_find (struct lua_script_option *o, int *pval)
{
return (o->s_opt.val == *pval);
}
static int lua_spank_option_callback (int val, const char *optarg, int remote)
{
lua_State *L;
struct lua_script_option *o;
o = list_find_first (
script_option_list,
(ListFindF) s_opt_find,
&val);
if (o == NULL)
return (-1);
if (o->l_function == NULL)
return (0);
L = o->script->L;
lua_getglobal (L, o->l_function);
lua_pushnumber (L, o->l_val);
lua_pushstring (L, optarg);
lua_pushboolean (L, remote);
slurm_debug ("spank/lua: %s: callback %s for option %s optarg=%s",
o->script->path, o->l_function, o->s_opt.name,
optarg ? optarg : "nil");
if (lua_pcall (L, 3, 1, 0) != 0) {
slurm_error ("Failed to call lua callback function %s: %s",
o->l_function, lua_tostring (L, -1));
lua_pop (L, 1);
return (-1);
}
return lua_script_rc (L);
}
static struct lua_script_option *
lua_script_option_create (struct lua_script *script, int i)
{
struct lua_script_option *o = malloc (sizeof (*o));
struct lua_State *L = script->L;
if (o == NULL)
luaL_error (L, "Unable to create lua script option: Out of memory");
o->s_opt.cb = (spank_opt_cb_f) lua_spank_option_callback;
o->script = script;
/*
* Option name:
*/
lua_getfield (L, i, "name");
if (lua_isnil (L, -1))
luaL_error (L, "Required field \"name\" missing from spank option table");
o->s_opt.name = strdup (lua_tostring (L, -1));
lua_pop (L, 1);
/*
* Option arginfo (optional):
*/
lua_getfield (L, i, "arginfo");
if (lua_isnil (L, -1))
o->s_opt.arginfo = NULL;
else
o->s_opt.arginfo = strdup (lua_tostring (L, -1));
lua_pop (L, 1);
/*
* Option usage (required):
*/
lua_getfield (L, i, "usage");
if (lua_isnil (L, -1))
luaL_error (L, "Required field \"usage\" missing from spank option table");
o->s_opt.usage = strdup (lua_tostring (L, -1));
lua_pop (L, 1);
/*
* Option has_arg (optional):
*/
lua_getfield (L, i, "has_arg");
if (lua_isnil (L, -1))
o->s_opt.has_arg = 0;
else
o->s_opt.has_arg = lua_tonumber (L, -1);
lua_pop (L, 1);
/*
* Option val (optional):
*/
lua_getfield (L, i, "val");
if (lua_isnil (L, -1))
o->l_val = 0;
else
o->l_val = lua_tonumber (L, -1);
lua_pop (L, 1);
/*
* Option callback function name:
*/
lua_getfield (L, i, "cb");
if (!lua_isnil (L, -1)) {
o->l_function = strdup (lua_tostring (L, -1));
/*
* Check for existence of callback function
*/
lua_getglobal (L, o->l_function);
if (!lua_isfunction (L, -1))
luaL_error (L, "Unable to find spank option cb function %s",
o->l_function);
lua_pop (L, 1);
}
else
o->l_function = NULL;
lua_pop (L, 1);
return (o);
}
static void lua_script_option_destroy (struct lua_script_option *o)
{
if (o->s_opt.name)
free (o->s_opt.name);
if (o->s_opt.arginfo)
free (o->s_opt.arginfo);
if (o->s_opt.usage)
free (o->s_opt.usage);
if (o->l_function)
free (o->l_function);
free (o);
}
static int lua_script_option_register (struct lua_script *script,
spank_t sp, int index)
{
spank_err_t err;
struct lua_State *L = script->L;
struct lua_script_option *opt = lua_script_option_create (script, index);
if (!script_option_list)
script_option_list = list_create ((ListDelF)lua_script_option_destroy);
opt->s_opt.val = list_count (script_option_list);
list_push (script_option_list, opt);
err = spank_option_register (sp, &opt->s_opt);
if (err != ESPANK_SUCCESS)
return l_spank_error (L, err);
lua_pushboolean (L, 1);
return (1);
}
static int find_script_by_state (struct lua_script *s, lua_State *L)
{
return (s->L == L);
}
static int l_spank_option_register (lua_State *L)
{
int rc;
spank_t sp;
struct lua_script *script;
sp = lua_getspank (L, 1);
if (!lua_istable (L, 2))
return luaL_error (L,
"Expected table argument to spank_option_register");
script = list_find_first (lua_script_list,
(ListFindF) find_script_by_state,
(void *) L);
rc = lua_script_option_register (script, sp, 2);
lua_pop (L, 2);
return (rc);
}
#if HAVE_SPANK_OPTION_GETOPT
static int l_spank_option_getopt (lua_State *L)
{
spank_t sp;
spank_err_t err;
char *optarg;
struct lua_script *script;
struct lua_script_option *opt;
sp = lua_getspank (L, 1);
if (!lua_istable (L, 2))
return luaL_error (L,
"Expected table argument to spank_option_getopt");
script = list_find_first (lua_script_list,
(ListFindF) find_script_by_state,
(void *) L);
if (!script)
return luaL_error (L,
"Unable to determine script from lua state!");
opt = lua_script_option_create (script, 2);
err = spank_option_getopt (sp, &opt->s_opt, &optarg);
lua_script_option_destroy (opt);
lua_pop (L, 2);
if (err != ESPANK_SUCCESS) {
if (err == ESPANK_ERROR)
return l_spank_error_msg (L, "Option unused");
else
return l_spank_error (L, err);
}
if (optarg == NULL)
lua_pushboolean (L, 1);
else
lua_pushstring (L, optarg);
return (1);
}
#endif /* if HAVE_SPANK_OPTION_GETOPT */
static int l_spank_remote (lua_State *L)
{
spank_t sp;
int val;
sp = lua_getspank (L, 1);
switch (spank_remote (sp)) {
case 0:
lua_pushstring (L, "no");
break;
case 1:
lua_pushstring (L, "yes");
break;
default:
lua_pushstring (L, "error");
break;
}
return (1);
}
static int l_spank_context (lua_State *L)
{
switch (spank_context ()) {
case S_CTX_LOCAL:
lua_pushstring (L, "local");
break;
case S_CTX_REMOTE:
lua_pushstring (L, "remote");
break;
case S_CTX_ALLOCATOR:
lua_pushstring (L, "allocator");
break;
#if HAVE_S_CTX_SLURMD
case S_CTX_SLURMD:
lua_pushstring (L, "slurmd");
break;
#endif
#if HAVE_S_CTX_JOB_SCRIPT
case S_CTX_JOB_SCRIPT:
lua_pushstring (L, "job_script");
break;
#endif
case S_CTX_ERROR:
default:
lua_pushstring (L, "error");
break;
}
return (1);
}
/*****************************************************************************
* SPANK table
****************************************************************************/
static const struct luaL_Reg spank_functions [] = {
{ "remote", l_spank_remote },
{ "register_option", l_spank_option_register },
{ "get_item", l_spank_get_item },
#if HAVE_SPANK_OPTION_GETOPT
{ "getopt", l_spank_option_getopt },
#endif
{ "getenv", l_spank_getenv },
{ "setenv", l_spank_setenv },
{ "unsetenv", l_spank_unsetenv },
{ "job_control_setenv", l_spank_job_control_setenv },
{ "job_control_getenv", l_spank_job_control_getenv },
{ "job_control_unsetenv", l_spank_job_control_unsetenv },
{ NULL, NULL },
};
static int lua_spank_table_create (lua_State *L, spank_t sp, int ac, char **av)
{
const char *str;
int i;
lua_newtable (L);
#if LUA_VERSION_NUM >= 502
luaL_setfuncs(L, spank_functions, 0);
#else
luaL_register (L, NULL, spank_functions);
#endif
/* Register spank handle as light userdata inside spank table:
*/
lua_pushlightuserdata (L, sp);
lua_setfield (L, -2, SPANK_REFNAME);
l_spank_context (L);
lua_setfield (L, -2, "context");
if (spank_get_item (sp, S_SLURM_VERSION, &str) == ESPANK_SUCCESS) {
lua_pushstring (L, str);
lua_setfield (L, -2, "slurm_version");
}
lua_newtable (L);
for (i = 1; i < ac; i++) {
lua_pushstring (L, av[i]);
lua_rawseti (L, -2, i);
}
lua_setfield (L, -2, "args");
return (0);
}
static int lua_spank_call (struct lua_script *s, spank_t sp, const char *fn,
int ac, char **av)
{
struct lua_State *L = s->L;
/*
* Missing functions are not an error
*/
lua_getglobal (L, fn);
if (lua_isnil (L, -1)) {
lua_pop (L, 1);
return (0);
}
/*
* Create spank object to pass to spank functions
*/
lua_spank_table_create (L, sp, ac, av);
if (lua_pcall (L, 1, 1, 0)) {
slurm_error ("spank/lua: %s: %s", fn, lua_tostring (L, -1));
return (s->fail_on_error ? -1 : 0);
}
return lua_script_rc (L);
}
/*
* Convert '%' to '%%" on the msg string on top of the stack.
*/
static const char *l_string_sanitized (lua_State *L)
{
int err;
const char fn[] = "local s = ... return string.gsub(s, '%%', '%%%%')";
err = luaL_loadstring (L, fn);
if (err) {
slurm_error ("spank/lua: loadstring (%s): %s", fn, lua_tostring(L, -1));
return (NULL);
}
/*
* Move string to top of stack:
*/
lua_pushvalue (L, 2);
lua_remove (L, 2);
/*
* Call gsub, throwing away 2nd return value (number of matches),
* leaving modified string on top of stack:
*/
err = lua_pcall (L, 1, 1, 0);
if (err) {
slurm_error ("spank/lua: sanitize msg: %s", lua_tostring (L, -1));
return (NULL);
}
return (lua_tostring (L, -1));
}
static int l_spank_log_msg (lua_State *L)
{
int level = luaL_checknumber (L, 1);
const char *msg;
msg = l_string_sanitized (L);
if (!msg)
return (0);
if (level == -1) {
slurm_error ("%s", msg);
lua_pushnumber (L, -1);
return (1);
}
if (level == 0)
slurm_info ("%s", msg);
else if (level == 1)
slurm_verbose ("%s", msg);
else if (level == 2)
slurm_debug ("%s", msg);
return (0);
}
static int SPANK_table_create (lua_State *L)
{
lua_newtable (L);
lua_pushcfunction (L, l_spank_log_msg);
lua_setfield (L, -2, "_log_msg");
/*
* Create more user-friendly lua versions of SLURM log functions
* with lua.
*/
#if LUA_VERSION_NUM >= 502
luaL_loadstring (L, "SPANK._log_msg (-1, string.format(table.unpack({...})))");
#else
luaL_loadstring (L, "SPANK._log_msg (-1, string.format(unpack({...})))");
#endif
lua_setfield (L, -2, "log_error");
#if LUA_VERSION_NUM >= 502
luaL_loadstring (L, "SPANK._log_msg (0, string.format(table.unpack({...})))");
#else
luaL_loadstring (L, "SPANK._log_msg (0, string.format(unpack({...})))");