Skip to content

Commit e0ecaae

Browse files
authored
Merge pull request #5264 from garlick/sdbus_logging
sdbus: make debug logging configurable
2 parents 00fd035 + a37cbf7 commit e0ecaae

File tree

3 files changed

+112
-26
lines changed

3 files changed

+112
-26
lines changed

src/modules/sdbus/sdbus.c

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ static void sdbus_recover (struct sdbus_ctx *ctx, const char *reason);
5656
static const double retry_min = 2;
5757
static const double retry_max = 60;
5858

59+
static bool sdbus_debug = false;
60+
61+
static __attribute__ ((format (printf, 2, 3)))
62+
void sdbus_log_debug (flux_t *h, const char *fmt, ...)
63+
{
64+
if (sdbus_debug) {
65+
va_list ap;
66+
67+
va_start (ap, fmt);
68+
flux_vlog (h, LOG_DEBUG, fmt, ap);
69+
va_end (ap);
70+
}
71+
}
72+
5973
static int authorize_request (const flux_msg_t *msg,
6074
uint32_t rank,
6175
flux_error_t *error)
@@ -173,13 +187,12 @@ static void log_msg_signal (flux_t *h,
173187

174188
if (path)
175189
(void)sd_bus_path_decode (path, prefix, &s);
176-
flux_log (h,
177-
LOG_DEBUG,
178-
"bus %s %s %s %s",
179-
disposition,
180-
sdmsg_typestr (m),
181-
s ? s : path,
182-
sd_bus_message_get_member (m));
190+
sdbus_log_debug (h,
191+
"bus %s %s %s %s",
192+
disposition,
193+
sdmsg_typestr (m),
194+
s ? s : path,
195+
sd_bus_message_get_member (m));
183196
free (s);
184197
}
185198

@@ -189,12 +202,11 @@ static void log_msg_method_reply (flux_t *h,
189202
sd_bus_message *m,
190203
struct call_info *info)
191204
{
192-
flux_log (h,
193-
LOG_DEBUG,
194-
"bus recv %s cookie=%ju %s",
195-
sdmsg_typestr (m),
196-
(uintmax_t)info->cookie,
197-
info->member);
205+
sdbus_log_debug (h,
206+
"bus recv %s cookie=%ju %s",
207+
sdmsg_typestr (m),
208+
(uintmax_t)info->cookie,
209+
info->member);
198210
}
199211

200212
static void sdbus_recv (struct sdbus_ctx *ctx, sd_bus_message *m)
@@ -287,7 +299,7 @@ static void sdbus_recv (struct sdbus_ctx *ctx, sd_bus_message *m)
287299
out:
288300
return;
289301
log_drop:
290-
flux_log (ctx->h, LOG_DEBUG, "bus drop %s", sdmsg_typestr (m));
302+
sdbus_log_debug (ctx->h, "bus drop %s", sdmsg_typestr (m));
291303
}
292304

293305
static void call_info_destroy (struct call_info *info)
@@ -352,12 +364,11 @@ static int handle_call_request (struct sdbus_ctx *ctx,
352364
goto error;
353365
}
354366

355-
flux_log (ctx->h,
356-
LOG_DEBUG,
357-
"bus send %s cookie=%ju %s",
358-
sdmsg_typestr (m),
359-
(uintmax_t)cookie,
360-
sd_bus_message_get_member (m));
367+
sdbus_log_debug (ctx->h,
368+
"bus send %s cookie=%ju %s",
369+
sdmsg_typestr (m),
370+
(uintmax_t)cookie,
371+
sd_bus_message_get_member (m));
361372

362373
if (!(info = call_info_create (m, cookie))
363374
|| flux_msg_aux_set (msg,
@@ -500,6 +511,53 @@ static void reconnect_cb (flux_t *h,
500511
flux_log_error (h, "error responding to sdbus.reconnect request");
501512
}
502513

514+
static int sdbus_configure (struct sdbus_ctx *ctx,
515+
const flux_conf_t *conf,
516+
flux_error_t *error)
517+
{
518+
flux_error_t conf_error;
519+
int debug = 0;
520+
521+
if (flux_conf_unpack (conf,
522+
&conf_error,
523+
"{s?{s?b}}",
524+
"systemd",
525+
"sdbus-debug", &debug) < 0) {
526+
errprintf (error,
527+
"error reading [systemd] config table: %s",
528+
conf_error.text);
529+
return -1;
530+
}
531+
sdbus_debug = (debug ? true : false);
532+
return 0;
533+
}
534+
535+
static void reload_cb (flux_t *h,
536+
flux_msg_handler_t *mh,
537+
const flux_msg_t *msg,
538+
void *arg)
539+
{
540+
struct sdbus_ctx *ctx = arg;
541+
const flux_conf_t *conf;
542+
flux_error_t error;
543+
const char *errstr = NULL;
544+
545+
if (flux_conf_reload_decode (msg, &conf) < 0) {
546+
errstr = "Failed to parse config-reload request";
547+
goto error;
548+
}
549+
if (sdbus_configure (ctx, conf, &error) < 0) {
550+
errstr = error.text;
551+
goto error;
552+
}
553+
if (flux_respond (h, msg, NULL) < 0)
554+
flux_log_error (h, "error responding to config-reload request");
555+
return;
556+
error:
557+
if (flux_respond_error (h, msg, errno, errstr) < 0)
558+
flux_log_error (h, "error responding to config-reload request");
559+
}
560+
503561
static struct flux_msg_handler_spec htab[] = {
504562
{ FLUX_MSGTYPE_REQUEST,
505563
"sdbus.disconnect",
@@ -526,6 +584,11 @@ static struct flux_msg_handler_spec htab[] = {
526584
reconnect_cb,
527585
0
528586
},
587+
{ FLUX_MSGTYPE_REQUEST,
588+
"sdbus.config-reload",
589+
reload_cb,
590+
0
591+
},
529592
FLUX_MSGHANDLER_TABLE_END,
530593
};
531594

@@ -696,18 +759,22 @@ struct sdbus_ctx *sdbus_ctx_create (flux_t *h, flux_error_t *error)
696759
{
697760
struct sdbus_ctx *ctx;
698761

699-
if (!(ctx = calloc (1, sizeof (*ctx)))
700-
|| !(ctx->f_conn = sdbus_connect (h, true, retry_min, retry_max))
762+
if (!(ctx = calloc (1, sizeof (*ctx))))
763+
goto error_create;
764+
if (sdbus_configure (ctx, flux_get_conf (h), error) < 0)
765+
goto error;
766+
if (!(ctx->f_conn = sdbus_connect (h, true, retry_min, retry_max))
701767
|| flux_future_then (ctx->f_conn, -1, connect_continuation, ctx) < 0
702768
|| flux_msg_handler_addvec (h, htab, ctx, &ctx->handlers) < 0
703769
|| !(ctx->requests = flux_msglist_create ())
704770
|| !(ctx->subscribers = flux_msglist_create ())
705771
|| flux_get_rank (h, &ctx->rank) < 0)
706-
goto error;
772+
goto error_create;
707773
ctx->h = h;
708774
return ctx;
709-
error:
775+
error_create:
710776
errprintf (error, "error creating sdbus context: %s", strerror (errno));
777+
error:
711778
sdbus_ctx_destroy (ctx);
712779
return NULL;
713780
}

t/t2407-sdbus.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,22 @@ bus_subscribe_cancel () {
111111
flux python -c "import flux; flux.Flux().rpc(\"sdbus.subscribe-cancel\",{\"matchtag\":$1},flags=flux.constants.FLUX_RPC_NORESPONSE)"
112112
}
113113

114+
test_expect_success 'enable sdbus-debug in configuration' '
115+
flux config load <<-EOT
116+
[systemd]
117+
sdbus-debug = true
118+
EOT
119+
'
114120
test_expect_success 'load sdbus module' '
115121
flux module load sdbus
116122
'
123+
test_expect_success 'sdbus reconfig fails with bad sdbus-debug value' '
124+
test_must_fail flux config load <<-EOT 2>config.err &&
125+
[systemd]
126+
sdbus-debug = 42
127+
EOT
128+
grep "Expected true or false" config.err
129+
'
117130

118131
test_expect_success 'sdbus list-units works' '
119132
count=$(bus_count_units "*") &&
@@ -299,7 +312,7 @@ test_expect_success 'create list script' '
299312
test_expect_success 'list from rank 0 is allowed' '
300313
flux python ./list.py >/dev/null
301314
'
302-
test_expect_success 'list-units-leader from rank 1 is restricted' '
315+
test_expect_success 'list from rank 1 is restricted' '
303316
test_must_fail flux exec -r 1 flux python ./list.py 2>list1.err &&
304317
grep "not allowed" list1.err
305318
'

t/t2408-sdbus-recovery.t

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ if ! busctl --user status >/dev/null; then
1818
test_done
1919
fi
2020

21-
test_under_flux 1 minimal
21+
mkdir -p config
22+
cat >config/config.toml <<EOF
23+
[systemd]
24+
sdbus-debug = true
25+
EOF
26+
27+
test_under_flux 1 minimal -o,--config-path=$(pwd)/config
2228

2329
flux setattr log-stderr-level 1
2430

0 commit comments

Comments
 (0)