/
hook_func.c
6614 lines (5852 loc) · 188 KB
/
hook_func.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) 1994-2016 Altair Engineering, Inc.
* For more information, contact Altair at www.altair.com.
*
* This file is part of the PBS Professional ("PBS Pro") software.
*
* Open Source License Information:
*
* PBS Pro is free software. You can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* PBS Pro 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Commercial License Information:
*
* The PBS Pro software is licensed under the terms of the GNU Affero General
* Public License agreement ("AGPL"), except where a separate commercial license
* agreement for PBS Pro version 14 or later has been executed in writing with Altair.
*
* Altair’s dual-license business model allows companies, individuals, and
* organizations to create proprietary derivative works of PBS Pro and distribute
* them - whether embedded or bundled with other software - under a commercial
* license agreement.
*
* Use of Altair’s trademarks, including but not limited to "PBS™",
* "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
* trademark licensing policies.
*
*/
/**
* @file hook_func.c
*
* @brief
* hook_func.c - contains functions to record accounting information
*
* Functions included are:
*
* hook_action_tid_set
* hook_action_tid_get
* hook_track_save
* send_rescdef
* hook_track_recov
* mgr_hook_create
* mgr_hook_delete
* py_compile_and_run
* mgr_hook_import
* mgr_hook_export
* copy_hook
* mgr_hook_set
* mgr_hook_unset
* status_hook
* req_stat_hook
* set_exec_time
* set_hold_types
* set_attribute
* set_job_varlist
* set_job_reslist
* attribute_jobmap_init
* attribute_jobmap_clear
* attribute_jobmap_restore
* do_runjob_accept_actions
* do_runjob_reject_actions
* write_hook_reject_debug_output_and_close
* write_hook_accept_debug_output_and_close
* process_hooks
* recreate_request
* add_mom_hook_action
* delete_mom_hook_action
* find_mom_hook_action
* add_pending_mom_hook_action
* delete_pending_mom_hook_action
* has_pending_mom_action_delete
* sync_mom_hookfiles_count
* collapse_hook_tr
* sync_mom_hookfiles
* mk_deferred_hook_info
* post_sendhookRPP
* check_add_hook_mcast_info
* del_deferred_hook_cmds
* sync_mom_hookfilesRPP
* kill_sync_hook_process
* post_sync_mom_hookfiles
* bg_sync_mom_hookfiles
* add_pending_mom_allhooks_action
* next_sync_mom_hookfiles
* mark_mom_hooks_seen
* mom_hooks_seen_count
* bg_delete_mom_hooks
* get_hook_rescdef_checksum
*/
#include <pbs_config.h> /* the master config generated by configure */
#ifndef WIN32
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <memory.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pbs_ifl.h"
#include "libpbs.h"
#include "list_link.h"
#include "work_task.h"
#include "attribute.h"
#include "batch_request.h"
#include "hook.h"
#include "hook_func.h"
#include "log.h"
#include "server_limits.h"
#include "attribute.h"
#include "credential.h"
#include "batch_request.h"
#include "job.h"
#include "reservation.h"
#include "queue.h"
#include "pbs_nodes.h"
#include "svrfunc.h"
#include <pbs_python.h> /* for python interpreter */
#include <signal.h>
#include "hook_func.h"
#include "net_connect.h"
#include "sched_cmds.h"
#include "rpp.h"
#include "reservation.h"
#include "cmds.h"
#include "server.h"
/* External functions */
extern void disable_svr_prov();
extern void set_srv_prov_attributes();
extern int should_retry_route(int);
extern char server_host[PBS_MAXHOSTNAME+1];
/* Local Private Functions */
/* Global Data items */
/* Local Data */
static char merr[] = "malloc failed";
static int do_sync_mom_hookfiles = 1;
static int sync_mom_hookfiles_proc_running = 0;
static int mom_hooks_seen = 0; /* # of mom hooks seen */
static long long int hook_action_tid = 0LL; /* transaction id of the next */
static int g_hook_replies_expected = 0; /* used only in TPP mode */
static int g_hook_replies_recvd = 0; /* used only in TPP mode */
static time_t g_sync_hook_time = 0; /* time when mom hook files were last sent */
static long long int g_sync_hook_tid = 0LL; /* identifies the latest group of hook updates to send out */
static pid_t g_sync_hook_pid = -1; /* pid of the child sync_mom_hookfiles() process (non-TPP only) */
/* process sending out mom hook files */
static unsigned long hook_rescdef_checksum = 0;
/* mom hook action(s) to keep */
/* track */
#define GROW_MOMHOOK_ARRAY_AMT 10
#define CONN_RETRY 3
#define IS_SPECIAL_CHAR(c) ((c == '"') || (c == '\'') || (c == ',') || (c == '\\'))
#define VALID_HOOK_CONFIG_SUFFIX ".json .py .txt .xml .ini"
#define SYNC_MOM_HOOKFILES_TIMEOUT_TPP 120 /* 2 minutes */
#define SYNC_MOM_HOOKFILES_TIMEOUT 900 /* 15 minutes */
extern char *msg_daemonname;
extern char *path_priv;
extern char *path_hooks;
extern char *path_hooks_workdir;
extern char *path_hooks_tracking;
extern char path_log[];
extern char *log_file;
extern pbs_net_t pbs_server_addr;
extern unsigned int pbs_server_port_dis;
extern char *msg_badexit;
extern char *msg_err_malloc;
extern char *msg_manager;
extern char *msg_man_cre;
extern char *msg_man_del;
extern char *msg_man_set;
extern char *msg_man_uns;
extern char *msg_noattr;
extern char *msg_internal;
extern char *msg_norelytomom;
extern pbs_list_head svr_allhooks;
extern pbs_list_head svr_queuejob_hooks;
extern pbs_list_head svr_modifyjob_hooks;
extern pbs_list_head svr_resvsub_hooks;
extern pbs_list_head svr_movejob_hooks;
extern pbs_list_head svr_runjob_hooks;
extern pbs_list_head svr_provision_hooks;
extern pbs_list_head svr_execjob_begin_hooks;
extern pbs_list_head svr_execjob_prologue_hooks;
extern pbs_list_head svr_execjob_epilogue_hooks;
extern pbs_list_head svr_execjob_preterm_hooks;
extern pbs_list_head svr_execjob_end_hooks;
extern pbs_list_head svr_exechost_periodic_hooks;
extern pbs_list_head svr_exechost_startup_hooks;
extern pbs_list_head svr_execjob_launch_hooks;
extern pbs_list_head svr_execjob_attach_hooks;
extern time_t time_now;
extern struct python_interpreter_data svr_interp_data;
extern pbs_list_head task_list_event;
extern struct work_task *add_mom_deferred_list(int stream, mominfo_t *minfo, void (*func)(), char *msgid, void *parm1, void *parm2);
extern char *path_rescdef;
extern char *path_hooks_rescdef;
extern int comp_resc_gt;
extern int comp_resc_lt;
extern int comp_resc_nc;
struct def_hk_cmd_info {
int index;
int event;
long long int tid; /* transaction id */
};
/* structures required for TPP mcast communication
* of hooks to moms
*/
typedef struct {
int mconn;
char *msgid;
char *hookname;
int action;
} hook_mcast_info_t;
/* global array of mcast information structs */
hook_mcast_info_t *g_hook_mcast_array = NULL;
int g_hook_mcast_array_len = 0;
extern int get_msgid(char **id);
/**
* @brief
* hook_action_tid_set - Sets the value of the global 'hook_action_tid' variable to the given
* 'newval'.
* @see
* main, hook_track_recov and bg_sync_mom_hookfiles.
*
* @param[in] newval - the new value.
*
* @return void
*/
void
hook_action_tid_set(long long int newval)
{
hook_action_tid = newval;
snprintf(log_buffer, sizeof(log_buffer),
"hook_action_tid=%lld", hook_action_tid);
log_event(PBSEVENT_DEBUG4, PBS_EVENTCLASS_SERVER,
LOG_INFO, "hook_action_tid_set", log_buffer);
}
/**
* @brief
* Returns the value of the global 'hook_action_tid' variable.
*
* @see
* sync_mom_hookfilesRPP and bg_sync_mom_hookfiles.
*
* @return long long int - the 'hook_action_tid' value.
*/
long long int
hook_action_tid_get(void)
{
return (hook_action_tid);
}
/**
* @brief
* attrlist_add - Add <name> = <val> attributes to list headed by 'atl'
*
* @param[in,out] atl - attribute list headed by atl.
* @param[in] name - name, in the name value pair.
* @param[in] val - val, in the name value pair.
*
* @return int
* @retval 0 - for success
* @retval 1 - otherwise.
*/
static int
attrlist_add(pbs_list_head *atl, char *name, char *val)
{
svrattrl *pal2;
if ((name == NULL) || (val == NULL)) {
log_err(-1, __func__, "name or val param is NULL");
return (1);
}
if ((name[0] == '\0') || (val[0] == '\0')) {
sprintf(log_buffer,
"(%s,%s) - name or val parameter is empty", name, val);
log_err(errno, __func__, log_buffer);
return (1);
}
pal2 = attrlist_create(name, (char *)0, (int)strlen(val)+1);
if (pal2 == (svrattrl *)0) {
sprintf(log_buffer,
"(%s,%s) - failed to create attribute list", name, val);
log_err(errno, __func__, log_buffer);
return (1);
}
strcpy(pal2->al_value, val);
append_link(atl, &pal2->al_link, pal2);
return (0);
}
/* Mom Hooks tracking functions */
/**
* @brief
* hook_track_save - Save the mom hooks pending actions data to a path_hooks_tracking file.
* File format is:
* <mom_hostname>:<mom_port> <hookname> <action> <tid>
*
* @par Note:
* If 'minfo' is not NULL and k != -1, then data from action array attached
* to 'minfo' at index 'k' is appended to path_hooks_tracking file.
*
* If 'minfo' is not NULL and k == -1, then no data is saved
* as hook_track_save() returns immediately.
*
* If 'minfo' is NULL and whether or not k != -1 or k == -1, then data
* from action array attached to each mom in the system, gets
* appended to path_hooks_tracking file.
*
* @param[in] minfo - used in conjunction with 'k' below:
* if not NULL and
* k != -1, then append only the array
* data attached to 'minfo'indexed at 'k' to
* path_hooks_tracking file.
*
* @param[in] k - used in conjunction with 'minfo' above.
*
* @return void
*
*/
void
hook_track_save(void *minfo, int k)
{
int i, j;
FILE *fp = NULL;
mominfo_t **minfo_array = NULL;
int minfo_array_size;
mominfo_t *minfo_array_tmp[1];
char msg[HOOK_MSG_SIZE+1];
if ((minfo != NULL) && (k == -1))
return;
if (minfo != NULL) {
fp = fopen(path_hooks_tracking, "a");
minfo_array_tmp[0] = minfo;
minfo_array = (mominfo_t **)minfo_array_tmp;
minfo_array_size = 1;
} else {
fp = fopen(path_hooks_tracking, "w");
minfo_array = mominfo_array;
minfo_array_size = mominfo_array_size;
}
if (fp == NULL) {
snprintf(log_buffer, sizeof(log_buffer),
"Failed to open hook tracking file %s",
path_hooks_tracking);
log_err(errno, __func__, log_buffer);
return;
}
#ifdef WIN32
secure_file2(path_hooks_tracking, "Administrators",
READS_MASK|WRITES_MASK|STANDARD_RIGHTS_REQUIRED,
"Everyone", READS_MASK|READ_CONTROL);
#endif
if (lock_file(fp, F_WRLCK, path_hooks_tracking, LOCK_RETRY_DEFAULT,
msg, sizeof(msg)) != 0) {
log_err(errno, __func__, msg);
fclose(fp);
return; /* failed to lock */
}
for (i=0; i < minfo_array_size; i++) {
if (minfo_array[i] == NULL)
continue;
for (j=0; j < minfo_array[i]->mi_num_action; j++) {
if (((minfo == NULL) || (k == j)) &&
(minfo_array[i]->mi_action[j] != NULL)) {
fprintf(fp, "%s:%d %s %d %lld\n", minfo_array[i]->mi_host,
minfo_array[i]->mi_port,
minfo_array[i]->mi_action[j]->hookname,
minfo_array[i]->mi_action[j]->action,
minfo_array[i]->mi_action[j]->tid);
}
}
}
(void)fflush(fp);
if (lock_file(fp, F_UNLCK, path_hooks_tracking, LOCK_RETRY_DEFAULT,
msg, sizeof(msg)) != 0)
log_err(errno, __func__, msg);
fclose(fp);
}
/**
*
* @brief
* send_rescdef - checks if server's resourcedef file has a newer timestamp than the
* resourcedef file currently known to hooks, and if so sets up the
* necessary mom hook actions to send the resourcedef file to each of the
* mom hosts on the next sync_mom_hookfiles() call.
*
* @param[in] force - if set to '1', means to skip checking for timetamps
* and just force setting up the mom hook actions to
* send the resourcedef file to each of the mom hosts.
*
* @return void
*/
void
send_rescdef(int force)
{
int st;
int st2;
struct stat sbuf;
struct stat sbuf2;
if (mom_hooks_seen <= 0) {
if (mom_hooks_seen < 0) { /* should not happen */
log_event(PBSEVENT_DEBUG2, PBS_EVENTCLASS_HOOK,
LOG_INFO, __func__,
"mom_hooks_seen went negative, resetting to 0");
mom_hooks_seen = 0;
}
return;
}
st=stat(path_rescdef, &sbuf);
st2=stat(path_hooks_rescdef, &sbuf2);
if ((st == 0) && (sbuf.st_size > 0) &&
(force || ((st2 == -1) && (errno == ENOENT)) ||
((st2 == 0) && (sbuf.st_mtime > sbuf2.st_mtime)))) {
st = copy_file_internal(path_rescdef, path_hooks_rescdef);
if (st != 0) {
snprintf(log_buffer, sizeof(log_buffer),
"warning: Failed to copy %s %s (error %d)",
path_rescdef, path_hooks_rescdef, st);
log_event(PBSEVENT_DEBUG2, PBS_EVENTCLASS_SERVER,
LOG_ERR, __func__, log_buffer);
} else {
add_pending_mom_hook_action(NULL, PBS_RESCDEF,
MOM_HOOK_ACTION_SEND_RESCDEF);
do_sync_mom_hookfiles = 1;
}
} else if (((st == -1) || (sbuf.st_size == 0)) && (st2 == 0)) {
/* server_priv/resourcedef disappeared and yet */
/* server_priv/hooks/resourcedef still exists */
add_pending_mom_hook_action(NULL, PBS_RESCDEF,
MOM_HOOK_ACTION_DELETE_RESCDEF);
do_sync_mom_hookfiles = 1;
(void)unlink(path_hooks_rescdef);
}
hook_rescdef_checksum = crc_file(path_hooks_rescdef);
}
/**
* @brief
* hook_track_recov - Recover the tracking data in path_hooks_tracking for mom hooks.
*
* @par Functionality
* The tracking data contains the pending mom hook actions, and in a file
* has the format:
* <mom_name> <port_number> <hook_name> <action> <tid>
*
* @note
* This is to recover any pending mom hook actions when the
* server restarts.
*
* This will also look for the highest value <tid> it has recovered
* from the hooks tracking file, and uses that as the new value of
* the global variable hook_action_tid.
*
* If a hook-related file (*.HK, *.PY, resourcedef) was sent to the
* remote mom, and the mom was able to get the file, and if the child
* server goes away before it can record the result in the hooks
* tracking file, then the main server would be left with still the old
* tracking entry for the hook action, and is retried again by the
* next spawned child server. If the main server dies in the middle
* of the transaction, when it restarts, it will be able to recover any
* hook action results by the child server. And if there are still
* pending actions, then they are retried by the next child server.
*
* @return void
*/
void
hook_track_recov(void)
{
int i, j;
FILE *fp = NULL;
char linebuf[BUFSIZ];
int linenum;
char *mom_name;
char *hook_name;
int port_num;
char *port_num_str;
int action;
char *action_str;
long long int hook_tid;
char *p;
char *p2;
char msg[HOOK_MSG_SIZE+1];
int msg_len = HOOK_MSG_SIZE;
mominfo_t *minfo = NULL;
struct stat sbuf;
long long int max_tid_recov = 0LL; /* holds maximum tid number seen */
/* in mom hooks tracking file */
if (stat(path_hooks_tracking, &sbuf) != 0) {
snprintf(log_buffer, sizeof(log_buffer),
"Failed to stat %s errno=%d", path_hooks_tracking,
errno);
log_event(PBSEVENT_DEBUG3, PBS_EVENTCLASS_SERVER,
LOG_ERR, __func__, log_buffer);
return;
}
fp = fopen(path_hooks_tracking, "r");
if (fp == NULL) {
snprintf(log_buffer, sizeof(log_buffer),
"Failed to open file %s errno=%d",
path_hooks_tracking, errno);
log_event(PBSEVENT_DEBUG3, PBS_EVENTCLASS_SERVER,
LOG_ERR, __func__, log_buffer);
return;
}
if (lock_file(fp, F_RDLCK, path_hooks_tracking, LOCK_RETRY_DEFAULT,
msg, sizeof(msg)) != 0) {
log_err(errno, __func__, msg);
fclose(fp);
return; /* failed to lock */
}
/* we now have a lock on the file */
/* Need to clear out old entries */
for (i=0; i < mominfo_array_size; i++) {
if (mominfo_array[i] == NULL)
continue;
for (j=0; j < mominfo_array[i]->mi_num_action; j++) {
if (mominfo_array[i]->mi_action[j] != NULL) {
free(mominfo_array[i]->mi_action[j]);
mominfo_array[i]->mi_action[j] = NULL;
}
}
}
/* We'll recover what we can, and just ignore errors */
/* encountered processing the hook tracking file entries */
linenum = 0;
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
linenum++;
if ((p = strrchr(linebuf, '\n')) != NULL) {
*p = '\0';
} else {
snprintf(msg, msg_len-1,
"warning: line %d is too long", linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
/* ignore initial white space; skip blank lines */
p = linebuf;
/* skip whitespace */
while ((*p != '\0') && isspace(*p))
p++;
if (*p == '\0')
continue; /* empty line */
mom_name = p;
if ((p2 = strchr(linebuf, ':')) == NULL) {
snprintf(msg, msg_len-1,
"warning: line %d: missing ':'", linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
if (*(p2+1) == '\0') {
snprintf(msg, msg_len-1,
"warning: line %d: no <port num>",
linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
*p2 = '\0';
p2++;
port_num_str = p2;
/* skip non-white space */
while ((*p2 != '\0') && !isspace(*p2))
p2++;
if (*p2 == '\0') {
snprintf(msg, msg_len-1,
"warning: line %d: no <hook name>",
linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
*p2 = '\0';
port_num = atoi(port_num_str);
/* skip whitespace */
p2++;
while ((*p2 != '\0') && isspace(*p2))
p2++;
if (*p2 == '\0') {
snprintf(msg, msg_len-1,
"warning: line %d: no <hook name>",
linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
hook_name = p2;
while ((*p2 != '\0') && !isspace(*p2))
p2++;
if (*p2 == '\0') {
snprintf(msg, msg_len-1,
"warning: line %d: no <hook action>", linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
*p2++ = '\0';
/* skip whitespace */
while ((*p2 != '\0') && isspace(*p2))
p2++;
action_str = p2;
/* get non-whitespace characters */
while ((*p2 != '\0') && !isspace(*p2))
p2++;
if (*p2 == '\0') {
snprintf(msg, msg_len-1,
"warning: line %d: no <tid>", linenum);
log_err(PBSE_SYSTEM, __func__, msg);
continue;
}
*p2 = '\0';
action = atoi(action_str);
p2++;
/* skip whitespace */
while ((*p2 != '\0') && isspace(*p2))
p2++;
/* preserve the recovered tid value */
/* and use it to store into mom hook */
/* action data structure */
#if defined(WIN32) /* Windows */
hook_tid = _atoi64(p2);
#elif defined(__hpux) /* HP-UX */
hook_tid = strToL(p2, (char **)NULL, 10);
#else
hook_tid = atoll(p2);
#endif
if (hook_tid > max_tid_recov)
max_tid_recov = hook_tid;
/* optimization: use previous result if match */
if ((minfo == NULL) ||
(strcasecmp(minfo->mi_host, mom_name) != 0) ||
(minfo->mi_port != port_num)) {
minfo=find_mom_entry(mom_name, port_num);
}
if (minfo != NULL) {
(void)add_mom_hook_action(&minfo->mi_action,
&minfo->mi_num_action, hook_name, action, 1,
hook_tid);
#ifdef WIN32
/* Below is only for pbs_send_hooks.exe on Windows and */
/* it is not for the main server. So the main server */
/* would not recreate a mom entry for a mom that got */
/* deleted. pbs_send_hooks is a "pseudo-server" where */
/* it doesn't have the mom entries initially */
} else if ((msg_daemonname != NULL) &&
((strcmp(msg_daemonname, "PBS_send_hooks") == 0) ||
(strcmp(msg_daemonname, "PBS_start_provision") == 0))) {
/* a pseudo server on Windows where the mom entry */
/* needs to be recreated. */
minfo = create_mom_entry(mom_name, port_num);
if (minfo == NULL) {
snprintf(log_buffer, sizeof(log_buffer),
"warning: failed to create mom entry to (%s,%s) errno %d",
mom_name, port_num, errno);
log_err(PBSE_SYSTEM, __func__, log_buffer);
continue;
}
(void)add_mom_hook_action(&minfo->mi_action,
&minfo->mi_num_action, hook_name, action, 1,
hook_tid);
#endif
}
}
hook_action_tid_set(max_tid_recov);
if (fp != NULL) {
if (lock_file(fp, F_UNLCK, path_hooks_tracking,
LOCK_RETRY_DEFAULT, msg, sizeof(msg)) != 0) {
log_err(errno, __func__, msg);
}
fclose(fp);
}
}
/*
************************************************************************
* Hook-related Qmgr operations.
************************************************************************
*/
/**
* @brief
* mgr_hook_create - Processes a "create hook" qmgr request.
*
* @see
* req_manager
*
* @param[in] preq - the requestor info via batch request structure
*
* @note
* Returns a reply to the sender of the batch_request.
*/
void
mgr_hook_create(struct batch_request *preq)
{
svrattrl *plist, *plx;
hook *phook = NULL;
hook *phook2 = NULL;
char hook_msg[HOOK_MSG_SIZE];
char *hook_user_val = NULL;
char *hook_fail_action_val = NULL;
char *hook_freq_val = NULL;
if (strlen(preq->rq_ind.rq_manager.rq_objname) == 0) {
reply_text(preq, PBSE_HOOKERROR, "no hook name specified");
return;
}
memset(hook_msg, '\0', HOOK_MSG_SIZE);
if ((phook2=find_hook(preq->rq_ind.rq_manager.rq_objname)) != NULL) {
if (phook2->pending_delete) {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"hook name \'%s\' is pending delete, try another name",
preq->rq_ind.rq_manager.rq_objname);
} else {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"hook name \'%s\' already registered, try another name",
preq->rq_ind.rq_manager.rq_objname);
}
goto mgr_hook_create_error;
}
phook = hook_alloc();
if (phook == NULL) {
req_reject(PBSE_INTERNAL, 0, preq);
return;
}
if (set_hook_name(phook, preq->rq_ind.rq_manager.rq_objname,
hook_msg, sizeof(hook_msg)) != 0) {
goto mgr_hook_create_error;
}
plist = (svrattrl *)GET_NEXT(preq->rq_ind.rq_manager.rq_attr);
plx = plist;
while (plx) {
if (strcasecmp(plx->al_name, HOOKATT_TYPE) == 0) {
if (set_hook_type(phook, plx->al_value,
hook_msg, sizeof(hook_msg), 0) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_ENABLED) == 0) {
if (set_hook_enabled(phook, plx->al_value,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_DEBUG) == 0) {
if (set_hook_debug(phook, plx->al_value,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_USER) == 0) {
/* setting hook user value must be a deferred action, */
/* as it is dependent on event having */
/* execjob_prologue, execjob_epilogue, or */
/* execjob_preterm being set. The event set action */
/* could appear after this set user action. */
if (hook_user_val != NULL)
free(hook_user_val);
hook_user_val = strdup(plx->al_value);
if (hook_user_val == NULL) {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"strdup(%s) failed: errno %d",
plx->al_value, errno);
goto mgr_hook_create_error;
}
} else if (strcasecmp(plx->al_name, HOOKATT_FAIL_ACTION) == 0) {
/* setting hook fail_action value must be a deferred action, */
/* as it is dependent on event mom hook event */
/* being set. The event set action */
/* could appear after this set fail_action. */
if (hook_fail_action_val != NULL)
free(hook_fail_action_val);
hook_fail_action_val = strdup(plx->al_value);
if (hook_fail_action_val == NULL) {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"strdup(%s) failed: errno %d",
plx->al_value, errno);
goto mgr_hook_create_error;
}
} else if (strcasecmp(plx->al_name, HOOKATT_EVENT) == 0) {
if (set_hook_event(phook, plx->al_value,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_ORDER) == 0) {
if (set_hook_order(phook, plx->al_value,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_ALARM) == 0) {
if (set_hook_alarm(phook, plx->al_value,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
} else if (strcasecmp(plx->al_name, HOOKATT_FREQ) == 0) {
/* setting hook freq value must be a deferred action, */
/* as it is dependent on event having */
/* exechost_periodic being set. The event set action */
/* could appear after this set freq action. */
if (hook_freq_val != NULL)
free(hook_freq_val);
hook_freq_val = strdup(plx->al_value);
if (hook_freq_val == NULL) {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"strdup(%s) failed: errno %d",
plx->al_value, errno);
goto mgr_hook_create_error;
}
} else {
snprintf(hook_msg, sizeof(hook_msg)-1, "%s - %s",
msg_noattr, plx->al_name);
goto mgr_hook_create_error;
}
plx = (svrattrl *)GET_NEXT(plx->al_link);
}
/* Now do the deferred set actions */
if (hook_user_val != NULL) {
if (set_hook_user(phook, hook_user_val,
hook_msg, sizeof(hook_msg), 1) != 0)
goto mgr_hook_create_error;
free(hook_user_val);
hook_user_val = NULL;
}
/* Now do the deferred set fail_action */
if (hook_fail_action_val != NULL) {
if (set_hook_fail_action(phook, hook_fail_action_val,
hook_msg, sizeof(hook_msg), 1) != 0)
goto mgr_hook_create_error;
free(hook_fail_action_val);
hook_fail_action_val = NULL;
}
if (hook_freq_val != NULL) {
if (set_hook_freq(phook, hook_freq_val,
hook_msg, sizeof(hook_msg)) != 0)
goto mgr_hook_create_error;
free(hook_freq_val);
hook_freq_val = NULL;
}
sprintf(log_buffer, msg_manager, msg_man_cre,
preq->rq_user, preq->rq_host);
log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_HOOK, LOG_INFO,
preq->rq_ind.rq_manager.rq_objname, log_buffer);
if (hook_save(phook) != 0) {
snprintf(hook_msg, HOOK_MSG_SIZE-1,
"Failed to store '%s' permanently.",
preq->rq_ind.rq_manager.rq_objname);
goto mgr_hook_create_error;
}
if (phook->event & MOM_EVENTS) {
add_pending_mom_hook_action(NULL, phook->hook_name,
MOM_HOOK_ACTION_SEND_ATTRS);
mom_hooks_seen++;
if (mom_hooks_seen == 1) {
/* used to be no mom hooks in the system, but now */
/* one is introduced. So see if resourcedef file */
/* changed and need to be flagged to be sent to */
/* the moms */
send_rescdef(0);
}
}
reply_ack(preq); /*create completely successful*/
return;
mgr_hook_create_error:
if (hook_user_val != NULL)
free(hook_user_val);
if (hook_fail_action_val != NULL)
free(hook_fail_action_val);
if (hook_freq_val != NULL)
free(hook_freq_val);
if (phook)
hook_purge(phook, pbs_python_ext_free_python_script);
reply_text(preq, PBSE_HOOKERROR, hook_msg);
log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_HOOK, LOG_INFO, __func__, hook_msg);
}
/**
* @brief
* mgr_hook_delete - Processes a "delete hook" qmgr request.
*
* @see
* req_manager
*
* @param[in] preq - a batch_request structure representing the request.
*/
void
mgr_hook_delete(struct batch_request *preq)
{
hook *phook;
char *hookname;
char hook_msg[HOOK_MSG_SIZE];
memset(hook_msg, '\0', HOOK_MSG_SIZE);
if (strlen(preq->rq_ind.rq_manager.rq_objname) == 0) {
reply_text(preq, PBSE_HOOKERROR, "no hook name specified");
return;
}
hookname = preq->rq_ind.rq_manager.rq_objname;
phook = find_hook(hookname);
if ((phook == (hook *)0) || phook->pending_delete) {
snprintf(hook_msg, HOOK_MSG_SIZE-1, "%s does not exist!",
hookname);
log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_HOOK, LOG_INFO,