-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlpjs_dispatchd.c
1568 lines (1357 loc) · 49.3 KB
/
lpjs_dispatchd.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
/***************************************************************************
* Description:
* This is the main controller daemon that runs on the head node.
* It listens for socket connections and takes requests for information
* from lpjs-nodes (for node status), lpjs-jobs (for job status),
* job submissions from lpjs-submit, and job completion reports from
* lpjs-chaperone.
*
* History:
* Date Name Modification
* 2021-09-23 Jason Bacon Begin
***************************************************************************/
// System headers
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/types.h> // inet_ntoa()
#include <arpa/inet.h> // inet_ntoa()
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <fcntl.h> // open()
#include <pwd.h> // getpwnam()
#include <grp.h> // getgrnam()
#include <dirent.h> // opendir(), ...
// Addons
#include <munge.h>
#include <xtend/proc.h>
#include <xtend/file.h> // xt_rmkdir()
#include <xtend/string.h> // strisint()
// Project headers
#include "lpjs.h"
#include "node-list.h"
#include "job-list.h"
#include "config.h"
#include "scheduler.h"
#include "network.h"
#include "misc.h"
#include "lpjs_dispatchd.h"
int main(int argc,char *argv[])
{
// Terminates process if malloc() fails, no check required
node_list_t *node_list = node_list_new();
uid_t daemon_uid;
gid_t daemon_gid;
// Must be global for signal handler
// FIXME: Maybe use ucontext to pass these to handler
extern FILE *Log_stream;
extern FILE *Job_history_stream;
extern node_list_t *Node_list;
Node_list = node_list;
Log_stream = stderr;
// Silence compiler warnings about initialization
daemon_uid = getuid();
daemon_gid = getgid();
for (int arg = 1; arg < argc; ++arg)
{
if ( strcmp(argv[arg],"--daemonize") == 0 )
{
// Redirect lpjs_log() output from stderr to file
if ( (Log_stream = lpjs_log_output(LPJS_DISPATCHD_LOG, "w")) == NULL )
return EX_CANTCREAT;
/*
* Code run after this must not attempt to write to stdout or
* stderr since they will be closed. Use lpjs_log() for all
* informative messages.
* FIXME: Prevent unchecked log growth
*/
xt_daemonize(0, 0);
}
else if ( strcmp(argv[arg],"--log-output") == 0 )
{
/*
* Redirect lpjs_log() output without daemonizing via fork().
* Used by some platforms for services.
*/
if ( (Log_stream = lpjs_log_output(LPJS_DISPATCHD_LOG, "w")) == NULL )
return EX_CANTCREAT;
}
else if ( strcmp(argv[arg], "--user") == 0 )
{
/*
* Set uid under which daemon should run, instead of root.
* Just determine user for now. Create system files before
* giving up root privs.
*/
// pw_ent points to internal static object
// OK since dispatchd is not multithreaded
struct passwd *pw_ent;
char *user_name = argv[++arg];
if ( (pw_ent = getpwnam(user_name)) == NULL )
{
lpjs_log("%s(): Error: User %s does not exist.\n",
__FUNCTION__, user_name);
return EX_NOUSER;
}
daemon_uid = pw_ent->pw_uid;
}
else if ( strcmp(argv[arg], "--group") == 0 )
{
// gr_ent points to internal static object
// OK since dispatchd is not multithreaded
struct group *gr_ent;
char *group_name = argv[++arg];
if ( (gr_ent = getgrnam(group_name)) == NULL )
{
lpjs_log("%s(): Error: Group %s does not exist.\n",
__FUNCTION__, group_name);
return EX_NOUSER;
}
daemon_gid = gr_ent->gr_gid;
}
else
{
fprintf (stderr, "Usage: %s [--daemonize|--log-output] [--user username] [--group groupname]\n", argv[0]);
return EX_USAGE;
}
}
Job_history_stream = lpjs_log_output(LPJS_JOB_HISTORY, "a");
// Make log file writable to daemon owner after root creates it
chown(LPJS_LOG_DIR, daemon_uid, daemon_gid);
chown(LPJS_DISPATCHD_LOG, daemon_uid, daemon_gid);
// Go where daemon has write permissions, so a core can be dumped
// in the event of a crash
chdir(LPJS_LOG_DIR);
// Parent of all new job directories
if ( xt_rmkdir(LPJS_PENDING_DIR, 0755) != 0 )
{
fprintf(stderr, "Cannot create %s: %s\n", LPJS_PENDING_DIR, strerror(errno));
return EX_CANTCREAT;
}
// Parent of all running job directories
if ( xt_rmkdir(LPJS_RUNNING_DIR, 0755) != 0 )
{
fprintf(stderr, "Cannot create %s: %s\n", LPJS_RUNNING_DIR, strerror(errno));
return EX_CANTCREAT;
}
// Make spool dir writable to daemon owner after root creates it
chown(LPJS_PENDING_DIR, daemon_uid, daemon_gid);
chown(LPJS_RUNNING_DIR, daemon_uid, daemon_gid);
chown(LPJS_SPOOL_DIR "/next-job", daemon_uid, daemon_gid);
// Just in case somebody borked perms
chmod(PREFIX "/var", 0755);
chmod(LPJS_PENDING_DIR, 0755);
chmod(LPJS_RUNNING_DIR, 0755);
chmod(LPJS_SPOOL_DIR, 0755);
chmod(LPJS_SPOOL_DIR "/next-job", 0755);
chmod(LPJS_COMPD_LOG, 0755);
chmod(LPJS_DISPATCHD_LOG, 0755);
chmod(LPJS_JOB_HISTORY, 0755);
/*
* systemd needs a pid file for forking daemons. BSD systems don't
* require this for rc scripts, so we don't bother with it. PIDs
* are found dynamically there.
*/
#ifdef __linux__
int status;
extern char Pid_path[PATH_MAX + 1];
if ( xt_rmkdir(LPJS_RUN_DIR, 0755) != 0 )
return EX_CANTCREAT;
snprintf(Pid_path, PATH_MAX + 1, "%s/lpjs_dispatchd.pid", LPJS_RUN_DIR);
status = xt_create_pid_file(Pid_path, Log_stream);
if ( status != EX_OK )
return status;
chown(LPJS_RUN_DIR, daemon_uid, daemon_gid);
chown(Pid_path, daemon_uid, daemon_gid);
#endif
// setgid() must be done while still running as root, so do setuid() after
if ( daemon_gid != 0 )
{
lpjs_log("%s(): Setting daemon_gid to %u.\n",
__FUNCTION__, daemon_gid);
if ( setgid(daemon_gid) != 0 )
{
lpjs_log("%s(): setgid() failed: %s\n",
__FUNCTION__, strerror(errno));
return EX_NOPERM;
}
}
if ( daemon_uid != 0 )
{
lpjs_log("%s(): Setting daemon_uid to %u.\n",
__FUNCTION__, daemon_uid);
if ( setuid(daemon_uid) != 0 )
{
lpjs_log("%s(): Error: setuid() failed: %s\n",
__FUNCTION__, strerror(errno));
return EX_NOPERM;
}
}
// Read etc/lpjs/config, created by lpjs-admin
lpjs_load_config(node_list, LPJS_CONFIG_ALL, Log_stream);
/*
* bind(): address already in use during testing with frequent restarts.
* Best approach is to ensure that client completes a close
* before the server closes.
* https://hea-www.harvard.edu/~fine/Tech/addrinuse.html
* Copy saved in ./bind-address-already-in-use.pdf
* FIXME: Does this handler actually help? FDs are closed
* upon process termination anyway.
*/
signal(SIGINT, lpjs_dispatchd_terminate_handler);
signal(SIGTERM, lpjs_dispatchd_terminate_handler);
/*
* dispatchd shouldn't be trying to write to broken pipes, but
* we don't want it to terminate due to minor bugs.
*/
signal(SIGPIPE, lpjs_dispatchd_sigpipe);
return lpjs_process_events(node_list);
}
/***************************************************************************
* Description:
* Listen for messages on LPJS_TCP_PORT and respond with either info
* (lpjs-nodes, lpjs-jobs, etc.) or actions (lpjs-submit).
*
* History:
* Date Name Modification
* 2021-09-25 Jason Bacon Begin
***************************************************************************/
int lpjs_process_events(node_list_t *node_list)
{
int listen_fd;
struct sockaddr_in server_address = { 0 };
// job_list_new() terminates process if malloc fails, no need to check
job_list_t *pending_jobs = job_list_new(),
*running_jobs = job_list_new();
lpjs_load_job_list(pending_jobs, node_list, LPJS_PENDING_DIR);
lpjs_load_job_list(running_jobs, node_list, LPJS_RUNNING_DIR);
/*
* Step 1: Create a socket for listening for new connections.
*/
listen_fd = lpjs_listen(&server_address);
/*
* Step 2: Accept new connections, and create a separate socket
* for communication with each new compute node.
*/
while ( true )
{
fd_set read_fds;
int nfds, highest_fd;
// poll() is generally preferable to select(), because it checks
// only fds explicitly listed, while select() checks every fd from
// 0 to the highest. However, in this case, every fd is examined
// individually after select() returns, so irrelevant I/O events,
// which are rare anyway, won't hurt anything. select() is a
// little more convenient here.
// FIXME: Might this erase pending messages?
FD_ZERO(&read_fds);
FD_SET(listen_fd, &read_fds);
highest_fd = listen_fd;
for (unsigned c = 0; c < node_list_get_compute_node_count(node_list); ++c)
{
node_t *node = node_list_get_compute_nodes_ae(node_list, c);
//lpjs_debug("%s(): Checking node %s, fd = %d...\n",
// __FUNCTION__, node_get_hostname(node), node_get_msg_fd(node));
if ( node_get_msg_fd(node) != NODE_MSG_FD_NOT_OPEN )
{
FD_SET(node_get_msg_fd(node), &read_fds);
if ( node_get_msg_fd(node) > highest_fd )
highest_fd = node_get_msg_fd(node);
}
}
/*
* The nfds (# of file descriptors) argument to select is a
* bit confusing. It's actually the highest descriptor + 1,
* not the number of open descriptors. E.g., to check only
* descriptors 3 and 8, nfds must be 9, not 2. This is
* different from the analogous poll() function, which takes
* an array of open descriptors.
*/
nfds = highest_fd + 1;
lpjs_log("%s(): Waiting for input events...\n", __FUNCTION__);
if ( select(nfds, &read_fds, NULL, NULL, LPJS_NO_SELECT_TIMEOUT) > 0 )
{
//lpjs_debug("%s(): Checking comp fds...\n", __FUNCTION__);
// compd doesn't presently initiate conversations on
// the persistend socket. It is used only for dispatchd
// to send new jobs to compd. This function only serves
// to check for lost connections with compd daemons.
lpjs_check_comp_fds(&read_fds, node_list, running_jobs);
//lpjs_debug("%s(): Checking listen fd...\n", __FUNCTION__);
// Check FD_ISSET before calling function to avoid overhead
if ( FD_ISSET(listen_fd, &read_fds) )
lpjs_check_listen_fd(listen_fd, &read_fds,
node_list, pending_jobs, running_jobs);
}
else
lpjs_log("%s(): Bug: select() returned 0. This should never happen with no timeout.\n");
}
// Never actually get here, but make the compiler happy
return EX_OK;
}
/***************************************************************************
* Description:
* Record job info such as command, exit status, run time, etc.
* Incoming message is sent by lpjs-chaperone when its child
* (a dispatched computational process) terminates.
*
* History:
* Date Name Modification
* 2021-09-28 Jason Bacon Begin
***************************************************************************/
void lpjs_log_job(job_list_t *job_list, const char *hostname,
unsigned long job_id, int exit_status, size_t peak_rss)
{
extern FILE *Job_history_stream;
job_t *job;
size_t index;
if ( (index = job_list_find_job_id(job_list, job_id)) != JOB_LIST_NOT_FOUND )
{
if ( (job = job_list_get_jobs_ae(job_list, index)) != NULL )
{
// phys_mib_per_processor is MiB and peak_rss if KiB
fprintf(Job_history_stream, "%s %lu %d %zu %zu %d %d %s %s %s/%s\n",
xt_str_localtime("%m-%d %H:%M:%S"),
job_id, exit_status, peak_rss,
job_get_phys_mib_per_processor(job) * 1024,
job_get_processors_per_job(job),
job_get_threads_per_process(job),
hostname, job_get_user_name(job),
job_get_submit_dir(job), job_get_script_name(job));
fflush(Job_history_stream);
}
}
}
/***************************************************************************
* Description:
* Check all connected sockets for messages
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Factor out from lpjs_process_events()
***************************************************************************/
void lpjs_check_comp_fds(fd_set *read_fds, node_list_t *node_list,
job_list_t *running_jobs)
{
// Terminates process if malloc() fails, no check required
node_t *node = node_new();
int fd;
ssize_t bytes;
char *munge_payload;
uid_t uid;
gid_t gid;
// Top priority: Active compute nodes (move existing jobs along)
// Second priority: New compute node checkins (make resources available)
// Lowest priority: User commands
for (unsigned c = 0; c < node_list_get_compute_node_count(node_list); ++c)
{
node = node_list_get_compute_nodes_ae(node_list, c);
fd = node_get_msg_fd(node);
if ( (fd != NODE_MSG_FD_NOT_OPEN) && FD_ISSET(fd, read_fds) )
{
// lpjs_debug("Activity on fd %d\n", fd);
/*
* select() returns when a peer has closed the connection.
* lpjs_recv() will return 0 in this case.
*/
// FIXME: Verify that lost connections are handled properly
bytes = lpjs_recv_munge(fd, &munge_payload,
0, 0, &uid, &gid,
lpjs_dispatchd_safe_close);
if ( bytes < 1 )
{
lpjs_log("%s(): Lost connection to %s. Closing %d...\n",
__FUNCTION__, node_get_hostname(node), fd);
lpjs_wait_close(fd);
node_set_msg_fd(node, NODE_MSG_FD_NOT_OPEN);
node_set_state(node, "down");
}
else
{
// At present, compd never messages dispatchd after checkin
switch(munge_payload[0])
{
default:
lpjs_log("%s(): Error: Invalid notification on fd %d: %d\n",
__FUNCTION__, fd, munge_payload[0]);
}
free(munge_payload);
}
}
}
}
/***************************************************************************
* Description:
* Create listener socket
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Factor out from lpjs_process_events()
***************************************************************************/
int lpjs_listen(struct sockaddr_in *server_address)
{
int listen_fd;
/*
* Create a socket endpoint to pair with the endpoint on the client.
* This only creates a file descriptor. It is not yet bound to
* any network interface and port.
* AF_INET and PF_INET have the same value, but PF_INET is more
* correct according to BSD and Linux man pages, which indicate
* that a protocol family should be specified. In theory, a
* protocol family can support more than one address family.
* SOCK_STREAM indicates a reliable stream oriented protocol,
* such as TCP, vs. unreliable unordered datagram protocols like UDP.
*/
if ((listen_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
lpjs_log("%s(): Error: Can't create listener socket.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
/*
* Port on which to listen for new connections from compute nodes.
* Convert 16-bit port number from host byte order to network byte order.
*/
server_address->sin_port = htons(LPJS_IP_TCP_PORT);
// AF_INET = inet4, AF_INET6 = inet6
server_address->sin_family = AF_INET;
/*
* Listen on all local network interfaces for now (INADDR_ANY).
* We may allow the user to specify binding to a specific IP address
* in the future, for multihomed servers acting as gateways, etc.
* Convert 32-bit host address to network byte order.
*/
server_address->sin_addr.s_addr = htonl(INADDR_ANY);
// Bind socket fd and server address
while ( bind(listen_fd, (struct sockaddr *)server_address,
sizeof (*server_address)) < 0 )
{
lpjs_log("%s(): Error: bind() failed: %s\n", __FUNCTION__, strerror(errno));
lpjs_log("%s(): Retry in 10 seconds...\n", __FUNCTION__);
sleep(10);
}
lpjs_log("%s(): Bound to port %d...\n", __FUNCTION__, LPJS_IP_TCP_PORT);
/*
* Create queue for incoming connection requests
*/
if (listen(listen_fd, LPJS_CONNECTION_QUEUE_MAX) != 0)
{
lpjs_log("%s(): Error: listen() failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
return listen_fd;
}
/***************************************************************************
* Description
* Process events arriving on the listening socket
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Factor out from lpjs_process_events()
***************************************************************************/
int lpjs_check_listen_fd(int listen_fd, fd_set *read_fds,
node_list_t *node_list,
job_list_t *pending_jobs, job_list_t *running_jobs)
{
int msg_fd,
chaperone_status,
exit_status;
ssize_t bytes;
size_t peak_rss;
char *munge_payload,
*p,
*hostname,
chaperone_hostname[LPJS_HOSTNAME_MAX + 1],
outgoing_msg[LPJS_MSG_LEN_MAX + 1];
socklen_t address_len = sizeof (struct sockaddr_in);
uid_t munge_uid;
gid_t munge_gid;
unsigned long job_id;
node_t *node;
int items;
job_t *job;
struct sockaddr_in client_address = { 0 };
bytes = 0;
/* Accept a connection request */
if ((msg_fd = accept(listen_fd,
(struct sockaddr *)&client_address, &address_len)) == -1)
{
lpjs_log("%s(): Error: accept() failed, even though select indicated listen_fd.\n",
__FUNCTION__);
return -1;
}
else
{
lpjs_log("%s(): Accepted connection. fd = %d addr = %s port = %u\n",
__FUNCTION__, msg_fd, inet_ntoa(client_address.sin_addr),
client_address.sin_port);
/* Read a message through the socket */
// FIXME: Timeouts temporarily disabled to debug hung connections
// &munge_payload, 0, LPJS_CONNECT_TIMEOUT,
bytes = lpjs_recv_munge(msg_fd,
&munge_payload, 0, 0,
&munge_uid, &munge_gid,
lpjs_dispatchd_safe_close);
lpjs_debug("%s(): Got %zd byte message.\n", __FUNCTION__, bytes);
if ( bytes == LPJS_RECV_TIMEOUT )
{
lpjs_log("%s(): Error: lpjs_recv_munge() timed out after %dus: %s, closing %d.\n",
__FUNCTION__, LPJS_CONNECT_TIMEOUT, strerror(errno), msg_fd);
lpjs_dispatchd_safe_close(msg_fd);
// Nothing to free if munge_decode() failed, since it
// allocates the buffer
// free(munge_payload);
return LPJS_RECV_TIMEOUT;
}
else if ( bytes == LPJS_RECV_FAILED )
{
lpjs_log("%s(): Error: lpjs_recv_munge() failed (%zd bytes): %s, closing %d.\n",
__FUNCTION__, bytes, strerror(errno), msg_fd);
lpjs_dispatchd_safe_close(msg_fd);
// Nothing to free if munge_decode() failed, since it
// allocates the buffer
// free(munge_payload);
return LPJS_RECV_FAILED;
}
// bytes must be at least 1, or no mem is allocated
else if ( bytes < 1 )
{
lpjs_log("%s(): Bug: Invalid return code from lpjs_recv_munge(): %d\n",
__FUNCTION__, bytes);
return LPJS_RECV_FAILED;
}
/* Process request */
switch(munge_payload[0])
{
case LPJS_DISPATCHD_REQUEST_COMPD_CHECKIN:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_COMPD_CHECKIN fd = %d\n",
__FUNCTION__, msg_fd);
lpjs_process_compute_node_checkin(msg_fd, munge_payload,
node_list, munge_uid, munge_gid);
lpjs_dispatch_jobs(node_list, pending_jobs, running_jobs);
// This connection is sustained, don't close it
break;
case LPJS_DISPATCHD_REQUEST_NODE_LIST:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_NODE_STATUS fd = %d\n",
__FUNCTION__, msg_fd);
node_list_send_status(msg_fd, node_list);
// lpjs_dispatchd_safe_close(msg_fd);
// node_list_send_status() sends EOT,
// so don't use safe_close here.
// lpjs_debug("%s(): Closing %d.\n", __FUNCTION__, msg_fd);
lpjs_wait_close(msg_fd);
break;
case LPJS_DISPATCHD_REQUEST_PAUSE:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_PAUSE fd = %d\n",
__FUNCTION__, msg_fd);
node_list_set_state(node_list, munge_payload + 1, munge_uid, msg_fd);
lpjs_wait_close(msg_fd);
break;
case LPJS_DISPATCHD_REQUEST_RESUME:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_RESUME fd = %d\n",
__FUNCTION__, msg_fd);
node_list_set_state(node_list, munge_payload + 1, munge_uid, msg_fd);
lpjs_wait_close(msg_fd);
// New resources might be available
lpjs_dispatch_jobs(node_list, pending_jobs, running_jobs);
break;
case LPJS_DISPATCHD_REQUEST_JOB_LIST:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_JOB_STATUS fd = %d\n",
__FUNCTION__, msg_fd);
// FIXME: Keep list sorted at all times instead of
// just for "lpjs jobs" output?
job_list_sort(running_jobs);
// FIXME: factor out to lpjs_send_job_list(), check
// all messages for success
snprintf(outgoing_msg, LPJS_MSG_LEN_MAX + 1, "%zu running:\n\n",
job_list_get_count(running_jobs));
if ( lpjs_send_munge(msg_fd, outgoing_msg,
lpjs_dispatchd_safe_close) != LPJS_MSG_SENT )
{
lpjs_log("%s(): Error: Failed to send \"Running\".\n", __FUNCTION__);
break;
}
job_list_send_params(msg_fd, running_jobs);
snprintf(outgoing_msg, LPJS_MSG_LEN_MAX + 1, "\n%zu pending:\n\n",
job_list_get_count(pending_jobs));
if ( lpjs_send_munge(msg_fd, outgoing_msg,
lpjs_dispatchd_safe_close) != LPJS_MSG_SENT )
{
lpjs_log("%s(): Failed to send \"Pending\".\n", __FUNCTION__);
break;
}
job_list_send_params(msg_fd, pending_jobs);
// Need to send EOT after job list
lpjs_dispatchd_safe_close(msg_fd);
break;
case LPJS_DISPATCHD_REQUEST_SUBMIT:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_SUBMIT fd = %d\n",
__FUNCTION__, msg_fd);
lpjs_submit(msg_fd, munge_payload, node_list,
pending_jobs, running_jobs,
munge_uid, munge_gid);
lpjs_wait_close(msg_fd);
lpjs_dispatch_jobs(node_list, pending_jobs, running_jobs);
break;
case LPJS_DISPATCHD_REQUEST_CANCEL:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_CANCEL fd = %d\n",
__FUNCTION__, msg_fd);
lpjs_cancel(msg_fd, munge_payload + 1, node_list,
pending_jobs, running_jobs,
munge_uid, munge_gid);
lpjs_wait_close(msg_fd);
// Resources might become available here
lpjs_dispatch_jobs(node_list, pending_jobs, running_jobs);
break;
case LPJS_DISPATCHD_REQUEST_CHAPERONE_STATUS:
// This is a temporary connection from the chaperone
// for just this message. Don't keep it open.
// FIXME: Seeing if this causes stalls
// lpjs_dispatchd_safe_close(msg_fd);
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_CHAPERONE_STATUS fd = %d\n",
__FUNCTION__, msg_fd);
// FIXME: %s is unsafe. Send hostname first and use strsep().
sscanf(munge_payload+1, "%lu %d %s",
&job_id, &chaperone_status, chaperone_hostname);
lpjs_debug("%s(): job_id = %lu status = %d hostname = %s\n",
__FUNCTION__, job_id, chaperone_status,
chaperone_hostname);
// Errors that occur before exec()ing script
switch(chaperone_status)
{
case LPJS_CHAPERONE_OK:
lpjs_log("%s(): Chaperone status OK.\n",__FUNCTION__);
// FIXME: Anything to do here?
break;
case LPJS_CHAPERONE_SCRIPT_FAILED:
lpjs_log("%s(): Error: Job script failed to start: %d\n",
__FUNCTION__, chaperone_status);
// Don't try to restart a script that failed
// Either the user needs to fix it, or something
// is not installed properly
adjust_resources(node_list, pending_jobs, hostname, job_id, NODE_RESOURCE_RELEASE);
lpjs_remove_pending_job(pending_jobs, job_id);
break;
default: // LPJS_CHAPERONE_OSERR and the rest...
lpjs_log("%s(): Error %d detected on %s.\n"
"See chaperone_status_t in network.h.\n",
__FUNCTION__, chaperone_status, chaperone_hostname);
lpjs_log("%s(): Releasing resourcesfor job %lu...\n",
__FUNCTION__, job_id);
adjust_resources(node_list, pending_jobs, hostname,
job_id, NODE_RESOURCE_RELEASE);
// FIXME: Node should not come back up from here when daemons
// are restarted. It should require "lpjs nodes up nodename"
// node_set_state(node, "malfunction");
lpjs_log("%s(): Setting %s state to down...\n",
__FUNCTION__, chaperone_hostname);
node = node_list_find_hostname(node_list, chaperone_hostname);
if ( node == NULL )
lpjs_log("%s(): Bug: No such node in list.\n",
__FUNCTION__);
else
node_set_state(node, "down");
lpjs_debug("%s(): Done.\n");
// FIXME: Make sure job state is reset, but don't remove
break;
}
break;
case LPJS_DISPATCHD_REQUEST_JOB_STARTED:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_JOB_STARTED fd = %d\n",
__FUNCTION__, msg_fd);
lpjs_send_munge(msg_fd, LPJS_NODE_AUTHORIZED_MSG,
lpjs_wait_close);
// lpjs_debug("%s(): Auth sent.\n", __FUNCTION__);
// This is a temporary connection from the chaperone
// for just this message. Don't keep it open.
// Don't sent EOT, but wait for other end to close
lpjs_wait_close(msg_fd);
/*
* No change in node status, don't try to dispatch jobs.
* Resources were allocated at dispatch time.
*/
// Job compute node and PIDs are in text form following
// the one byte LPJS_DISPATCHD_REQUEST_JOB_STARTED
lpjs_update_job(node_list, munge_payload + 1,
pending_jobs, running_jobs);
break;
case LPJS_DISPATCHD_REQUEST_JOB_COMPLETE:
lpjs_log("%s(): LPJS_DISPATCHD_REQUEST_JOB_COMPLETE fd = %d\n",
__FUNCTION__, msg_fd);
p = munge_payload + 1;
hostname = strsep(&p, " ");
lpjs_debug("%s(): hostname = %s ", __FUNCTION__, hostname);
node = node_list_find_hostname(node_list, hostname);
if ( node == NULL )
{
lpjs_log("%s(): Error: Invalid hostname in job completion report.\n",
__FUNCTION__);
break;
}
if ( (items = sscanf(p, "%lu %d %zu", &job_id,
&exit_status, &peak_rss)) != 3 )
{
lpjs_log("%s(): Error: Got %d items reading job_id, processors, mem, status, peak_rss.\n",
items);
break;
}
lpjs_debug("%s(): job_id = %lu status = %d peak-RSS = %zu\n",
__FUNCTION__, job_id, exit_status, peak_rss);
adjust_resources(node_list, running_jobs, hostname, job_id, NODE_RESOURCE_RELEASE);
/*
* Write a completed job record to accounting log
* Note the job completion in the main log
* Do this before removing from running_jobs
*/
lpjs_log_job(running_jobs, hostname, job_id, exit_status, peak_rss);
if ( (job = lpjs_remove_running_job(running_jobs,
job_id)) != NULL )
job_free(&job);
else
lpjs_log("%s(): Error: remove_running_job returned NULL. This is a bug.\n",
__FUNCTION__);
// FIXME: Don't dispatch pending jobs that have been canceled
lpjs_dispatch_jobs(node_list, pending_jobs, running_jobs);
// This is a temporary connection from the chaperone
// for just this message. Don't keep it open.
// Don't sent EOT, but wait for other end to close
lpjs_wait_close(msg_fd);
break;
default:
lpjs_log("%s(): Error: Invalid request code byte on listen_fd: %d\n",
__FUNCTION__, munge_payload[0]);
} // switch
free(munge_payload);
}
return bytes;
}
/***************************************************************************
* Description:
* Process a compute node checkin request
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Factor out from lpjs_process_events()
***************************************************************************/
void lpjs_process_compute_node_checkin(int msg_fd,
char *munge_payload,
node_list_t *node_list,
uid_t munge_uid, gid_t munge_gid)
{
// Terminates process if malloc() fails, no check required
node_t *new_node = node_new();
extern FILE *Log_stream;
char *p, *compd_version;
// FIXME: Check for duplicate checkins. We should not get
// a checkin request while one is already open
// lpjs_log("Munge credential message length = %zd\n", bytes);
// lpjs_log("munge msg: %s\n", incoming_msg);
lpjs_log("%s(): Checkin from munge_uid %d, munge_gid %d\n",
__FUNCTION__, munge_uid, munge_gid);
/*
* Validate version of connecting client.
* Payload format = "%c%s %s", command, lpjs-version, content
* FIXME: Just doing this for compd checkins as a test.
* Add to other connections as needed.
* FIXME: Be less strict about version matching after development
* slows and communication protocols are more stable.
*/
p = munge_payload + 1;
compd_version = strsep(&p, " ");
lpjs_debug("compd_version = %s dispatchd_version = %s\n",
compd_version, VERSION);
if ( strcmp(compd_version, VERSION) != 0 )
{
lpjs_send_munge(msg_fd, LPJS_WRONG_VERSION_MSG, lpjs_dispatchd_safe_close);
lpjs_log("%s(): Warning: Checkin request from node with wrong LPJS version: %s\n",
__FUNCTION__, compd_version);
lpjs_dispatchd_safe_close(msg_fd);
return; // FIXME: Return status?
}
// FIXME: Record username of compd checkin. If not root, then only
// that user can submit jobs to the node.
/*
* Get specs from node and add msg_fd
*/
// Extract specs from incoming_msg + 1
// node_recv_specs(new_node, msg_fd);
// +1 to skip command code
node_str_to_specs(new_node, p);
// Keep in sync with node_list_send_status()
node_print_status_header(Log_stream);
node_print_status(new_node, Log_stream);
// Make sure node name is valid
// Note: For real security, only authorized
// nodes should be allowed to pass through
// the network firewall.
bool valid_node = false;
for (unsigned c = 0; c < node_list_get_compute_node_count(node_list); ++c)
{
node_t *node = node_list_get_compute_nodes_ae(node_list, c);
// If config has short hostnames, just match that
int valid_hostname_len = strlen(node_get_hostname(node));
if ( memcmp(node_get_hostname(node), node_get_hostname(new_node), valid_hostname_len) == 0 )
valid_node = true;
}
if ( ! valid_node )
{
lpjs_send_munge(msg_fd, LPJS_NODE_NOT_AUTHORIZED_MSG, lpjs_dispatchd_safe_close);
lpjs_log("%s(): Warning: Unauthorized checkin request from host %s.\n",
__FUNCTION__, node_get_hostname(new_node));
lpjs_dispatchd_safe_close(msg_fd);
}
else
{
lpjs_send_munge(msg_fd, LPJS_NODE_AUTHORIZED_MSG, lpjs_dispatchd_safe_close);
node_set_msg_fd(new_node, msg_fd);
// Nodes were added to node_list by lpjs_load_config()
// Just update the fields here
node_list_update_compute(node_list, new_node);
}
}
/***************************************************************************
* Description:
* Add a new submission to the queue
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Factor out from lpjs_process_events()
***************************************************************************/
int lpjs_submit(int msg_fd, const char *incoming_msg,
node_list_t *node_list,
job_list_t *pending_jobs, job_list_t *running_jobs,
uid_t munge_uid, gid_t munge_gid)
{
char script_path[PATH_MAX + 1],
*end,
*script_text;
// Terminates process if malloc() fails, no check required
job_t *submission = job_new(),
*job;
int c, job_array_index;
// Payload from lpjs submit is a job description in JOB_SPEC_FORMAT
job_read_from_string(submission, incoming_msg + 1, &end);
if ( strcmp(job_get_user_name(submission), "root") == 0 )
{
lpjs_log("%s(): Error: Rejecting job submission from root.\n",
__FUNCTION__);
// msg_fd is closed below
lpjs_send_munge(msg_fd, "Error: Cannot run jobs as root.\n", lpjs_no_close);
}
else
{
// Should only be a newline between job specs and script
script_text = end + 1;
snprintf(script_path, PATH_MAX + 1, "%s/%s",
job_get_submit_dir(submission), job_get_script_name(submission));
for (c = 0; c < job_get_job_count(submission); ++c)
{
lpjs_log("%s(): Submit script %s:%s from %d, %d\n", __FUNCTION__,
job_get_submit_node(submission), script_path, munge_uid,
munge_gid);
job_array_index = c + 1; // Job arrays are 1-based
// Create a separate job_t object for each member of the job array
// job_dup() terminates process if malloc() fails
job = job_dup(submission);
lpjs_queue_job(msg_fd, pending_jobs, job, job_array_index, script_text);
}
}
lpjs_dispatchd_safe_close(msg_fd);