-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathbroker.c
1810 lines (1628 loc) · 55 KB
/
broker.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 2014 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\************************************************************/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <libgen.h>
#include <signal.h>
#include <locale.h>
#include <inttypes.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#include <sys/resource.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/syscall.h>
#ifdef HAVE_ARGZ_ADD
#include <argz.h>
#else
#include "src/common/libmissing/argz.h"
#endif
#include <flux/core.h>
#include <jansson.h>
#if HAVE_VALGRIND
# if HAVE_VALGRIND_H
# include <valgrind.h>
# elif HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
# endif
#endif
#include <flux/taskmap.h>
#include "src/common/libczmqcontainers/czmq_containers.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/cleanup.h"
#include "src/common/libidset/idset.h"
#include "src/common/libutil/ipaddr.h"
#include "src/common/libutil/fsd.h"
#include "src/common/libutil/errno_safe.h"
#include "src/common/libutil/errprintf.h"
#include "src/common/libutil/intree.h"
#include "src/common/libutil/basename.h"
#include "src/common/librouter/subhash.h"
#include "src/common/libfluxutil/method.h"
#include "ccan/array_size/array_size.h"
#include "ccan/str/str.h"
#include "ccan/ptrint/ptrint.h"
#ifndef HAVE_STRLCPY
#include "src/common/libmissing/strlcpy.h"
#endif
#ifndef HAVE_STRLCAT
#include "src/common/libmissing/strlcat.h"
#endif
#include "module.h"
#include "modhash.h"
#include "brokercfg.h"
#include "groups.h"
#include "overlay.h"
#include "service.h"
#include "attr.h"
#include "log.h"
#include "runat.h"
#include "heaptrace.h"
#include "exec.h"
#include "boot_config.h"
#include "boot_pmi.h"
#include "publisher.h"
#include "state_machine.h"
#include "shutdown.h"
#include "broker.h"
static int broker_request_sendmsg_new_internal (broker_ctx_t *ctx,
flux_msg_t **msg);
static void h_internal_watcher (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
void *arg);
static int overlay_recv_cb (flux_msg_t **msg, overlay_where_t where, void *arg);
static void signal_cb (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
void *arg);
static int broker_handle_signals (broker_ctx_t *ctx);
static flux_msg_handler_t **broker_add_services (broker_ctx_t *ctx);
static void broker_remove_services (flux_msg_handler_t *handlers[]);
static void set_proctitle (uint32_t rank);
static int create_rundir (attr_t *attrs);
static int check_statedir (attr_t *attrs);
static int create_runat_phases (broker_ctx_t *ctx);
static int handle_event (broker_ctx_t *ctx, const flux_msg_t *msg);
static void init_attrs (attr_t *attrs, pid_t pid, struct flux_msg_cred *cred);
static void init_attrs_post_boot (attr_t *attrs);
static void init_attrs_starttime (attr_t *attrs, double starttime);
static int init_local_uri_attr (struct overlay *ov, attr_t *attrs);
static int init_critical_ranks_attr (struct overlay *ov, attr_t *attrs);
static int execute_parental_notifications (struct broker *ctx);
static struct optparse_option opts[] = {
{ .name = "verbose", .key = 'v', .has_arg = 2, .arginfo = "[LEVEL]",
.usage = "Be annoyingly informative by degrees", },
{ .name = "setattr", .key = 'S', .has_arg = 1, .arginfo = "ATTR=VAL",
.usage = "Set broker attribute", },
{ .name = "config-path",.key = 'c', .has_arg = 1, .arginfo = "PATH",
.usage = "Set broker config from PATH (default: none)", },
OPTPARSE_TABLE_END,
};
void parse_command_line_arguments (int argc, char *argv[], broker_ctx_t *ctx)
{
int optindex;
const char *arg;
if (!(ctx->opts = optparse_create ("flux-broker"))
|| optparse_add_option_table (ctx->opts, opts) != OPTPARSE_SUCCESS)
log_msg_exit ("error setting up option parsing");
if ((optindex = optparse_parse_args (ctx->opts, argc, argv)) < 0)
exit (1);
ctx->verbose = optparse_get_int (ctx->opts, "verbose", 0);
optparse_get_str (ctx->opts, "config-path", NULL);
while ((arg = optparse_getopt_next (ctx->opts, "setattr"))) {
char *val, *attr;
if (!(attr = strdup (arg)))
log_err_exit ("out of memory duplicating optarg");
if ((val = strchr (attr, '=')))
*val++ = '\0';
if (attr_add (ctx->attrs, attr, val, 0) < 0)
if (attr_set (ctx->attrs, attr, val) < 0)
log_err_exit ("setattr %s=%s", attr, val);
free (attr);
}
if (optindex < argc) {
int e;
if ((e = argz_create (argv + optindex,
&ctx->init_shell_cmd,
&ctx->init_shell_cmd_len)) != 0)
log_errn_exit (e, "argz_create");
}
}
static int increase_rlimits (void)
{
struct rlimit rlim;
/* Increase number of open files to max to prevent potential failures
* due to file descriptor exhaustion (e.g. failure to open /dev/urandom)
*/
if (getrlimit (RLIMIT_NOFILE, &rlim) < 0) {
log_err ("getrlimit");
return -1;
}
rlim.rlim_cur = rlim.rlim_max;
if (setrlimit (RLIMIT_NOFILE, &rlim) < 0) {
log_err ("Failed to increase nofile limit");
return -1;
}
return 0;
}
int main (int argc, char *argv[])
{
broker_ctx_t ctx;
sigset_t old_sigmask;
struct sigaction old_sigact_int;
struct sigaction old_sigact_term;
flux_msg_handler_t **handlers = NULL;
const flux_conf_t *conf;
const char *method;
flux_error_t error;
setlocale (LC_ALL, "");
memset (&ctx, 0, sizeof (ctx));
log_init (argv[0]);
ctx.exit_rc = 1;
if (!(ctx.sigwatchers = zlist_new ())
|| !(ctx.services = service_switch_create ())
|| !(ctx.attrs = attr_create ())
|| !(ctx.sub = subhash_create ()))
log_msg_exit ("Out of memory in early initialization");
/* Record the instance owner: the effective uid of the broker. */
ctx.cred.userid = getuid ();
/* Set default rolemask for messages sent with flux_send()
* on the broker's internal handle. */
ctx.cred.rolemask = FLUX_ROLE_OWNER | FLUX_ROLE_LOCAL;
init_attrs (ctx.attrs, getpid (), &ctx.cred);
const char *hostname = getenv ("FLUX_FAKE_HOSTNAME");
if (hostname)
strlcpy (ctx.hostname, hostname, sizeof (ctx.hostname));
else if (gethostname (ctx.hostname, sizeof (ctx.hostname)) < 0)
log_err_exit ("gethostname");
parse_command_line_arguments (argc, argv, &ctx);
/* Block all signals but those that we want to generate core dumps.
* Save old mask and actions for SIGINT, SIGTERM.
*/
sigset_t sigmask;
sigfillset (&sigmask);
sigdelset (&sigmask, SIGSEGV);
sigdelset (&sigmask, SIGFPE);
sigdelset (&sigmask, SIGILL);
sigdelset (&sigmask, SIGABRT);
sigdelset (&sigmask, SIGFPE);
sigdelset (&sigmask, SIGSYS);
sigdelset (&sigmask, SIGTRAP);
sigdelset (&sigmask, SIGXCPU);
sigdelset (&sigmask, SIGXFSZ);
if (sigprocmask (SIG_SETMASK, &sigmask, &old_sigmask) < 0
|| sigaction (SIGINT, NULL, &old_sigact_int) < 0
|| sigaction (SIGTERM, NULL, &old_sigact_term) < 0)
log_err_exit ("error setting signal mask");
/* Set up two interthread flux_t handles, connected back to back.
* ctx.h is used conventionally within the broker for RPCs, message
* handlers, etc. ctx.h_internal belongs to the broker's routing logic
* and is accessed using flux_send() and flux_recv() only. Both handles
* share a reactor.
*
* N.B. since both handles are in the same thread, synchronous RPCs on
* ctx.h will deadlock. The main broker reactor must run in order
* to move messages from the interthread queue to the routing logic.
* Careful with flux_attr_get(), which hides a synchronous RPC if the
* requested value is not cached.
*/
if (!(ctx.reactor = flux_reactor_create (0))
|| !(ctx.h = flux_open ("interthread://broker", 0))
|| flux_set_reactor (ctx.h, ctx.reactor) < 0
|| !(ctx.h_internal = flux_open ("interthread://broker", 0))
|| flux_set_reactor (ctx.h_internal, ctx.reactor) < 0
|| !(ctx.w_internal = flux_handle_watcher_create (ctx.reactor,
ctx.h_internal,
FLUX_POLLIN,
h_internal_watcher,
&ctx))) {
log_err ("error setting up broker reactor/flux_t handle");
goto cleanup;
}
flux_watcher_start (ctx.w_internal);
const char *val;
if (attr_get (ctx.attrs, "broker.sd-notify", &val, NULL) == 0
&& !streq (val, "0")) {
#if !HAVE_LIBSYSTEMD
log_err ("broker.sd_notify is set but Flux was not built"
" with systemd support.");
goto cleanup;
#else
ctx.sd_notify = true;
#endif
}
/* Initialize module infrastructure.
*/
if (!(ctx.modhash = modhash_create (&ctx))) {
log_err ("error creating broker module hash");
goto cleanup;
}
/* Parse config.
*/
if (!(ctx.config = brokercfg_create (ctx.h,
optparse_get_str (ctx.opts,
"config-path",
NULL),
ctx.attrs,
ctx.modhash)))
goto cleanup;
conf = flux_get_conf (ctx.h);
if (increase_rlimits () < 0)
goto cleanup;
/* Prepare signal handling
*/
if (broker_handle_signals (&ctx) < 0) {
log_err ("broker_handle_signals");
goto cleanup;
}
if (!(ctx.overlay = overlay_create (ctx.h,
ctx.hostname,
ctx.attrs,
NULL,
overlay_recv_cb,
&ctx))) {
log_err ("overlay_create");
goto cleanup;
}
/* Arrange for the publisher to route event messages.
*/
if (!(ctx.publisher = publisher_create (&ctx,
(publisher_send_f)handle_event,
&ctx))) {
log_err ("error setting up event publishing service");
goto cleanup;
}
if (create_rundir (ctx.attrs) < 0)
goto cleanup;
if (check_statedir (ctx.attrs) < 0)
goto cleanup;
/* Record the broker start time. This time will also be used to
* capture how long network bootstrap takes.
*/
flux_reactor_now_update (ctx.reactor);
ctx.starttime = flux_reactor_now (ctx.reactor);
init_attrs_starttime (ctx.attrs, ctx.starttime);
/* Execute broker network bootstrap.
* Default method is pmi.
* If [bootstrap] is defined in configuration, use static configuration.
*/
if (attr_get (ctx.attrs, "broker.boot-method", &method, NULL) < 0) {
if (flux_conf_unpack (conf, NULL, "{s:{}}", "bootstrap") == 0)
method = "config";
else
method = NULL;
}
if (!method || !streq (method, "config")) {
if (boot_pmi (ctx.hostname, ctx.overlay, ctx.attrs) < 0) {
log_msg ("bootstrap failed");
goto cleanup;
}
}
else {
if (boot_config (ctx.h, ctx.hostname, ctx.overlay, ctx.attrs) < 0) {
log_msg ("bootstrap failed");
goto cleanup;
}
}
init_attrs_post_boot (ctx.attrs);
ctx.rank = overlay_get_rank (ctx.overlay);
ctx.size = overlay_get_size (ctx.overlay);
if (ctx.size == 0)
log_err_exit ("internal error: instance size is zero!");
/* Must be called after overlay setup */
if (overlay_register_attrs (ctx.overlay) < 0) {
log_err ("registering overlay attributes");
goto cleanup;
}
if (ctx.verbose) {
flux_reactor_now_update (ctx.reactor);
log_msg ("boot: rank=%d size=%d time %.3fs",
ctx.rank,
ctx.size,
flux_reactor_now (ctx.reactor) - ctx.starttime);
}
/* Initialize logging.
* OK to call flux_log*() after this.
*/
logbuf_initialize (ctx.h, ctx.rank, ctx.attrs);
/* Allow flux_get_rank(), flux_get_size(), flux_get_hostybyrank(), etc.
* to work in the broker without causing a synchronous RPC to self that
* would deadlock.
*/
if (attr_cache_immutables (ctx.attrs, ctx.h) < 0) {
log_err ("error priming broker attribute cache");
goto cleanup;
}
if (!(ctx.groups = groups_create (&ctx))) {
log_err ("groups_create");
goto cleanup;
}
if (ctx.verbose) {
const char *parent = overlay_get_parent_uri (ctx.overlay);
const char *child = overlay_get_bind_uri (ctx.overlay);
log_msg ("parent: %s", parent ? parent : "none");
log_msg ("child: %s", child ? child : "none");
}
set_proctitle (ctx.rank);
if (init_local_uri_attr (ctx.overlay, ctx.attrs) < 0 // used by runat
|| init_critical_ranks_attr (ctx.overlay, ctx.attrs) < 0)
goto cleanup;
if (create_runat_phases (&ctx) < 0)
goto cleanup;
/* Wire up the overlay.
*/
if (ctx.rank > 0) {
if (ctx.verbose)
log_msg ("initializing overlay connect");
if (overlay_connect (ctx.overlay) < 0) {
log_err ("overlay_connect");
goto cleanup;
}
}
/* Register internal services
*/
if (attr_register_handlers (ctx.attrs, ctx.h) < 0) {
log_err ("attr_register_handlers");
goto cleanup;
}
if (heaptrace_initialize (ctx.h) < 0) {
log_err ("heaptrace_initialize");
goto cleanup;
}
if (exec_initialize (ctx.h, ctx.rank, ctx.attrs) < 0) {
log_err ("exec_initialize");
goto cleanup;
}
if (flux_aux_set (ctx.h,
"flux::uuid",
(char *)overlay_get_uuid (ctx.overlay),
NULL) < 0) {
log_err ("error adding broker uuid to aux container");
goto cleanup;
}
if (!(handlers = broker_add_services (&ctx))) {
log_err ("broker_add_services");
goto cleanup;
}
/* overlay_control_start() calls flux_sync_create(), thus
* requires event.subscribe to have a handler before running.
*/
if (overlay_control_start (ctx.overlay) < 0) {
log_err ("error initializing overlay control messages");
goto cleanup;
}
/* Configure broker state machine
*/
if (!(ctx.state_machine = state_machine_create (&ctx))) {
log_err ("error creating broker state machine");
goto cleanup;
}
state_machine_post (ctx.state_machine, "start");
/* Create shutdown mechanism
*/
if (!(ctx.shutdown = shutdown_create (&ctx))) {
log_err ("error creating shutdown mechanism");
goto cleanup;
}
/* Load the local connector module.
* Other modules will be loaded in rc1 using flux module,
* which uses the local connector.
* The shutdown protocol unloads it.
*/
if (ctx.verbose > 1)
log_msg ("loading connector-local");
if (modhash_load (ctx.modhash,
NULL,
"connector-local",
NULL,
NULL,
&error) < 0) {
log_err ("load_module connector-local: %s", error.text);
goto cleanup;
}
if (ctx.rank == 0 && execute_parental_notifications (&ctx) < 0)
goto cleanup;
/* Event loop
*/
if (ctx.verbose > 1)
log_msg ("entering event loop");
/* Once we enter the reactor, default exit_rc is now 0 */
ctx.exit_rc = 0;
if (flux_reactor_run (ctx.reactor, 0) < 0)
log_err ("flux_reactor_run");
if (ctx.verbose > 1)
log_msg ("exited event loop");
cleanup:
if (ctx.verbose > 1)
log_msg ("cleaning up");
/* Restore default sigmask and actions for SIGINT, SIGTERM
*/
if (sigprocmask (SIG_SETMASK, &old_sigmask, NULL) < 0
|| sigaction (SIGINT, &old_sigact_int, NULL) < 0
|| sigaction (SIGTERM, &old_sigact_term, NULL) < 0)
log_err ("error restoring signal mask");
/* Unregister builtin services
*/
attr_destroy (ctx.attrs);
if (modhash_destroy (ctx.modhash) > 0) {
if (ctx.exit_rc == 0)
ctx.exit_rc = 1;
}
zlist_destroy (&ctx.sigwatchers);
shutdown_destroy (ctx.shutdown);
state_machine_destroy (ctx.state_machine);
overlay_destroy (ctx.overlay);
groups_destroy (ctx.groups);
service_switch_destroy (ctx.services);
broker_remove_services (handlers);
publisher_destroy (ctx.publisher);
brokercfg_destroy (ctx.config);
runat_destroy (ctx.runat);
flux_watcher_destroy (ctx.w_internal);
flux_close (ctx.h_internal);
flux_close (ctx.h);
flux_reactor_destroy (ctx.reactor);
subhash_destroy (ctx.sub);
free (ctx.init_shell_cmd);
optparse_destroy (ctx.opts);
return ctx.exit_rc;
}
static void init_attrs_broker_pid (attr_t *attrs, pid_t pid)
{
char *attrname = "broker.pid";
char pidval[32];
snprintf (pidval, sizeof (pidval), "%u", pid);
if (attr_add (attrs,
attrname,
pidval,
ATTR_IMMUTABLE) < 0)
log_err_exit ("attr_add %s", attrname);
}
static void init_attrs_rc_paths (attr_t *attrs)
{
if (attr_add (attrs,
"broker.rc1_path",
flux_conf_builtin_get ("rc1_path", FLUX_CONF_AUTO),
0) < 0)
log_err_exit ("attr_add rc1_path");
if (attr_add (attrs,
"broker.rc3_path",
flux_conf_builtin_get ("rc3_path", FLUX_CONF_AUTO),
0) < 0)
log_err_exit ("attr_add rc3_path");
}
static void init_attrs_shell_paths (attr_t *attrs)
{
if (attr_add (attrs,
"conf.shell_pluginpath",
flux_conf_builtin_get ("shell_pluginpath", FLUX_CONF_AUTO),
0) < 0)
log_err_exit ("attr_add conf.shell_pluginpath");
if (attr_add (attrs,
"conf.shell_initrc",
flux_conf_builtin_get ("shell_initrc", FLUX_CONF_AUTO),
0) < 0)
log_err_exit ("attr_add conf.shell_initrc");
}
static void init_attrs_starttime (attr_t *attrs, double starttime)
{
char buf[32];
snprintf (buf, sizeof (buf), "%.2f", starttime);
if (attr_add (attrs, "broker.starttime", buf, ATTR_IMMUTABLE) < 0)
log_err_exit ("error setting broker.starttime attribute");
}
/* Initialize attributes after bootstrap since these attributes may depend
* on whether this instance is a job or not.
*/
static void init_attrs_post_boot (attr_t *attrs)
{
const char *val;
bool instance_is_job;
/* Use the jobid attribute instead of FLUX_JOB_ID in the current
* environment to determine if this instance was run as a job. This
* is because the jobid attribute is only set by PMI, whereas
* FLUX_JOB_ID could leak from the calling environment, e.g.
* `flux run flux start --test-size=2`.
*/
instance_is_job = attr_get (attrs, "jobid", NULL, NULL) == 0;
/* Set the parent-uri attribute IFF this instance was run as a job
* in the enclosing instance. "parent" in this context reflects
* a hierarchy of resource allocation.
*/
if (instance_is_job)
val = getenv ("FLUX_URI");
else
val = NULL;
if (attr_add (attrs, "parent-uri", val, ATTR_IMMUTABLE) < 0)
log_err_exit ("setattr parent-uri");
unsetenv ("FLUX_URI");
/* Unset FLUX_PROXY_REMOTE since once a new broker starts we're no
* longer technically running under the influence of flux-proxy(1).
*/
unsetenv ("FLUX_PROXY_REMOTE");
if (instance_is_job) {
val = getenv ("FLUX_KVS_NAMESPACE");
if (attr_add (attrs, "parent-kvs-namespace", val, ATTR_IMMUTABLE) < 0)
log_err_exit ("setattr parent-kvs-namespace");
}
unsetenv ("FLUX_KVS_NAMESPACE");
}
static void init_attrs (attr_t *attrs, pid_t pid, struct flux_msg_cred *cred)
{
init_attrs_broker_pid (attrs, pid);
init_attrs_rc_paths (attrs);
init_attrs_shell_paths (attrs);
/* Allow version to be changed by instance owner for testing
*/
if (attr_add (attrs, "version", FLUX_CORE_VERSION_STRING, 0) < 0)
log_err_exit ("attr_add version");
char tmp[32];
snprintf (tmp, sizeof (tmp), "%ju", (uintmax_t)cred->userid);
if (attr_add (attrs, "security.owner", tmp, ATTR_IMMUTABLE) < 0)
log_err_exit ("attr_add owner");
}
static void set_proctitle (uint32_t rank)
{
#ifdef PR_SET_NAME
static char proctitle[32];
snprintf (proctitle, sizeof (proctitle), "flux-broker-%"PRIu32, rank);
(void)prctl (PR_SET_NAME, proctitle, 0, 0, 0);
#endif
}
static bool is_interactive_shell (const char *argz, size_t argz_len)
{
bool result = false;
/* If no command is specified, then an interactive shell will be run
*/
if (argz == NULL)
return true;
/* O/w, if command is plain "$SHELL", e.g. bash, zsh, csh, etc.
* then assume interactive shell.
*/
if (argz_count (argz, argz_len) == 1) {
char *shell;
char *cmd = argz_next (argz, argz_len, NULL);
while ((shell = getusershell ())) {
if (streq (cmd, shell) || streq (cmd, basename_simple (shell))) {
result = true;
break;
}
}
endusershell ();
}
return result;
}
static int create_runat_rc2 (struct runat *r, const char *argz, size_t argz_len)
{
if (is_interactive_shell (argz, argz_len)) { // run interactive shell
/* Check if stdin is a tty and error out if not to avoid
* confusing users with what appears to be a hang.
*/
if (!isatty (STDIN_FILENO))
log_msg_exit ("stdin is not a tty - can't run interactive shell");
if (runat_push_shell (r, "rc2", argz, 0) < 0)
return -1;
}
else if (argz_count (argz, argz_len) == 1) { // run shell -c "command"
if (runat_push_shell_command (r, "rc2", argz, 0) < 0)
return -1;
}
else { // direct exec
if (runat_push_command (r, "rc2", argz, argz_len, 0) < 0)
return -1;
}
return 0;
}
static int create_runat_phases (broker_ctx_t *ctx)
{
const char *jobid = NULL;
const char *rc1, *rc3, *local_uri;
bool rc2_none = false;
/* jobid may be NULL */
(void) attr_get (ctx->attrs, "jobid", &jobid, NULL);
if (attr_get (ctx->attrs, "local-uri", &local_uri, NULL) < 0) {
log_err ("local-uri is not set");
return -1;
}
if (attr_get (ctx->attrs, "broker.rc1_path", &rc1, NULL) < 0) {
log_err ("broker.rc1_path is not set");
return -1;
}
if (attr_get (ctx->attrs, "broker.rc3_path", &rc3, NULL) < 0) {
log_err ("broker.rc3_path is not set");
return -1;
}
if (attr_get (ctx->attrs, "broker.rc2_none", NULL, NULL) == 0)
rc2_none = true;
if (!(ctx->runat = runat_create (ctx->h,
local_uri,
jobid,
ctx->sd_notify))) {
log_err ("runat_create");
return -1;
}
/* rc1 - initialization
*/
if (rc1 && strlen (rc1) > 0) {
if (runat_push_shell_command (ctx->runat,
"rc1",
rc1,
RUNAT_FLAG_LOG_STDIO) < 0) {
log_err ("runat_push_shell_command rc1");
return -1;
}
}
/* rc2 - initial program
*/
if (ctx->rank == 0 && !rc2_none) {
if (create_runat_rc2 (ctx->runat,
ctx->init_shell_cmd,
ctx->init_shell_cmd_len) < 0) {
log_err ("create_runat_rc2");
return -1;
}
}
/* rc3 - finalization
*/
if (rc3 && strlen (rc3) > 0) {
if (runat_push_shell_command (ctx->runat,
"rc3",
rc3,
RUNAT_FLAG_LOG_STDIO) < 0) {
log_err ("runat_push_shell_command rc3");
return -1;
}
}
return 0;
}
static int checkdir (const char *name, const char *path)
{
struct stat sb;
if (stat (path, &sb) < 0) {
log_err ("cannot stat %s %s", name, path);
return -1;
}
if (sb.st_uid != getuid ()) {
errno = EPERM;
log_err ("%s %s is not owned by instance owner", name, path);
return -1;
}
if (!S_ISDIR (sb.st_mode)) {
errno = ENOTDIR;
log_err ("%s %s", name, path);
return -1;
}
if ((sb.st_mode & S_IRWXU) != S_IRWXU) {
log_msg ("%s %s does not have owner=rwx permissions", name, path);
errno = EPERM;
return -1;
}
return 0;
}
/* Validate statedir, if set.
* Ensure that the attribute cannot change from this point forward.
*/
static int check_statedir (attr_t *attrs)
{
const char *statedir;
if (attr_get (attrs, "statedir", &statedir, NULL) < 0) {
if (attr_add (attrs, "statedir", NULL, ATTR_IMMUTABLE) < 0) {
log_err ("error creating statedir broker attribute");
return -1;
}
}
else {
if (checkdir ("statedir", statedir) < 0)
return -1;
if (attr_set_flags (attrs, "statedir", ATTR_IMMUTABLE) < 0) {
log_err ("error setting statedir broker attribute flags");
return -1;
}
}
return 0;
}
static int create_rundir_symlinks (const char *run_dir, flux_error_t *error)
{
char path[1024];
size_t size = sizeof (path);
const char *target;
if (strlcpy (path, run_dir, size) >= size
|| strlcat (path, "/bin", size) >= size)
goto overflow;
if (mkdir (path, 0755) < 0) {
errprintf (error, "mkdir %s: %s", path, strerror (errno));
return -1;
}
cleanup_push_string (cleanup_directory_recursive, path);
if (strlcat (path, "/flux", size) >= size)
goto overflow;
if (executable_is_intree () == 1)
target = ABS_TOP_BUILDDIR "/src/cmd/flux";
else
target = X_BINDIR "/flux";
if (symlink (target, path) < 0) {
errprintf (error, "symlink %s: %s", path, strerror (errno));
return -1;
}
return 0;
overflow:
errprintf (error, "buffer overflow");
errno = EOVERFLOW;
return -1;
}
/* Handle global rundir attribute.
*/
static int create_rundir (attr_t *attrs)
{
const char *tmpdir;
const char *run_dir = NULL;
char path[1024];
int len;
bool do_cleanup = true;
int rc = -1;
/* If rundir attribute isn't set, then create a temp directory
* and use that as rundir. If directory was set, try to create it if
* it doesn't exist. If directory was pre-existing, do not schedule
* the dir for auto-cleanup at broker exit.
*/
if (attr_get (attrs, "rundir", &run_dir, NULL) < 0) {
if (!(tmpdir = getenv ("TMPDIR")))
tmpdir = "/tmp";
len = snprintf (path, sizeof (path), "%s/flux-XXXXXX", tmpdir);
if (len >= sizeof (path)) {
log_msg ("rundir buffer overflow");
goto done;
}
if (!(run_dir = mkdtemp (path))) {
log_err ("cannot create directory in %s", tmpdir);
goto done;
}
if (attr_add (attrs, "rundir", run_dir, 0) < 0) {
log_err ("error setting rundir broker attribute");
goto done;
}
}
else if (mkdir (run_dir, 0700) < 0) {
if (errno != EEXIST) {
log_err ("error creating rundir %s ", run_dir);
goto done;
}
/* Do not cleanup directory if we did not create it here
*/
do_cleanup = false;
}
/* Ensure created or existing directory is writeable:
*/
if (checkdir ("rundir", run_dir) < 0)
goto done;
/* Ensure that AF_UNIX sockets can be created in rundir - see #3925.
*/
struct sockaddr_un sa;
size_t path_limit = sizeof (sa.sun_path) - sizeof ("/local-9999");
size_t path_length = strlen (run_dir);
if (path_length > path_limit) {
log_msg ("rundir length of %zu bytes exceeds max %zu"
" to allow for AF_UNIX socket creation.",
path_length,
path_limit);
goto done;
}
/* rundir is now fixed, so make the attribute immutable, and
* schedule the dir for cleanup at exit if we created it here.
*/
if (attr_set_flags (attrs, "rundir", ATTR_IMMUTABLE) < 0) {
log_err ("error setting rundir broker attribute flags");
goto done;
}
/* Create $rundir/bin/flux so flux-relay can be found - see #5583.
*/
flux_error_t error;
if (create_rundir_symlinks (run_dir, &error) < 0) {
if (errno != EEXIST)
log_err ("error creating rundir symlinks: %s", error.text);
// if this fails, soldier on
}
rc = 0;
done:
if (do_cleanup && run_dir != NULL)
cleanup_push_string (cleanup_directory_recursive, run_dir);
return rc;
}
static int init_local_uri_attr (struct overlay *ov, attr_t *attrs)
{
const char *uri;
if (attr_get (attrs, "local-uri", &uri, NULL) < 0) {
uint32_t rank = overlay_get_rank (ov);
const char *rundir;
char buf[1024];
if (attr_get (attrs, "rundir", &rundir, NULL) < 0) {
log_msg ("rundir attribute is not set");
return -1;
}
if (snprintf (buf, sizeof (buf), "local://%s/local-%d",
rundir, rank) >= sizeof (buf)) {
log_msg ("buffer overflow while building local-uri");
return -1;
}
if (attr_add (attrs, "local-uri", buf, ATTR_IMMUTABLE) < 0) {
log_err ("setattr local-uri");
return -1;
}
}
else {
char path[1024];
if (!strstarts (uri, "local://")) {
log_msg ("local-uri is malformed");
return -1;
}
if (snprintf (path, sizeof (path), "%s", uri + 8) >= sizeof (path)) {
log_msg ("buffer overflow while checking local-uri");
return -1;
}
if (checkdir ("local-uri directory", dirname (path)) < 0)
return -1;
/* see #3925 */
struct sockaddr_un sa;
size_t path_limit = sizeof (sa.sun_path) - 1;
size_t path_length = strlen (uri + 8);
if (path_length > path_limit) {
log_msg ("local-uri length of %zu bytes exceeds max %zu"
" AF_UNIX socket path length",