Skip to content

Commit 72bb83d

Browse files
committed
mctpd: Add bug_warn, abort on BUG during tests
Existing BUG warnings now call bug_warn(). In normal operation the program code is unchanged (print a warning to logs), in test-mctpd it will abort. Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
1 parent cd44fbf commit 72bb83d

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

src/mctp-ops.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <unistd.h>
1111
#include <linux/netlink.h>
12+
#include <err.h>
1213

1314
#include "mctp.h"
1415
#include "mctp-ops.h"
@@ -51,6 +52,11 @@ static int mctp_op_close(int sd)
5152
return close(sd);
5253
}
5354

55+
static void mctp_bug_warn(const char* fmt, va_list args)
56+
{
57+
vwarnx(fmt, args);
58+
}
59+
5460
const struct mctp_ops mctp_ops = {
5561
.mctp = {
5662
.socket = mctp_op_mctp_socket,
@@ -68,6 +74,7 @@ const struct mctp_ops mctp_ops = {
6874
.recvfrom = mctp_op_recvfrom,
6975
.close = mctp_op_close,
7076
},
77+
.bug_warn = mctp_bug_warn,
7178
};
7279

7380
void mctp_ops_init(void) { }

src/mctp-ops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <sys/socket.h>
11+
#include <stdarg.h>
1112

1213
#define _GNU_SOURCE
1314

@@ -26,6 +27,7 @@ struct socket_ops {
2627
struct mctp_ops {
2728
struct socket_ops mctp;
2829
struct socket_ops nl;
30+
void (*bug_warn)(const char* fmt, va_list args);
2931
};
3032

3133
extern const struct mctp_ops mctp_ops;

src/mctpd.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,20 @@ static const sd_bus_vtable bus_endpoint_obmc_vtable[];
250250
static const sd_bus_vtable bus_endpoint_cc_vtable[];
251251
static const sd_bus_vtable bus_endpoint_uuid_vtable[];
252252

253+
__attribute__((format(printf, 1, 2)))
254+
static void bug_warn(const char* fmt, ...) {
255+
char *bug_fmt = NULL;
256+
va_list ap;
257+
258+
asprintf(&bug_fmt, "BUG: %s", fmt);
259+
260+
va_start(ap, fmt);
261+
mctp_ops.bug_warn(bug_fmt, ap);
262+
va_end(ap);
263+
264+
free(bug_fmt);
265+
}
266+
253267
mctp_eid_t local_addr(const struct ctx *ctx, int ifindex) {
254268
mctp_eid_t *eids, ret = 0;
255269
size_t num;
@@ -465,7 +479,7 @@ static const char *path_from_peer(const struct peer *peer)
465479
{
466480

467481
if (!peer->published) {
468-
warnx("BUG: %s on peer %s", __func__, peer_tostr(peer));
482+
bug_warn("%s on peer %s", __func__, peer_tostr(peer));
469483
return NULL;
470484
}
471485
return peer->path;
@@ -527,7 +541,7 @@ static int read_message(struct ctx *ctx, int sd, uint8_t **ret_buf, size_t *ret_
527541
goto out;
528542
}
529543
if ((size_t)len != buf_size) {
530-
warnx("BUG: incorrect recvfrom %zd, expected %zu", len, buf_size);
544+
bug_warn("incorrect recvfrom %zd, expected %zu", len, buf_size);
531545
rc = -EPROTO;
532546
goto out;
533547
}
@@ -568,7 +582,7 @@ static int reply_message(struct ctx *ctx, int sd, const void *resp, size_t resp_
568582

569583
if (reply_addr.smctp_addr.s_addr == 0 ||
570584
reply_addr.smctp_addr.s_addr == 0xff) {
571-
warnx("BUG: reply_message can't take EID %d",
585+
bug_warn("reply_message can't take EID %d",
572586
reply_addr.smctp_addr.s_addr);
573587
return -EPROTO;
574588
}
@@ -581,7 +595,7 @@ static int reply_message(struct ctx *ctx, int sd, const void *resp, size_t resp_
581595
}
582596

583597
if ((size_t)len != resp_len) {
584-
warnx("BUG: short sendto %zd, expected %zu", len, resp_len);
598+
bug_warn("short sendto %zd, expected %zu", len, resp_len);
585599
return -EPROTO;
586600
}
587601
return 0;
@@ -808,7 +822,7 @@ static int cb_listen_control_msg(sd_event_source *s, int sd, uint32_t revents,
808822
errx(EXIT_FAILURE, "Control socket returned EOF");
809823

810824
if (addr.smctp_base.smctp_type != MCTP_CTRL_HDR_MSG_TYPE) {
811-
warnx("BUG: Wrong message type for listen socket");
825+
bug_warn("Wrong message type for listen socket");
812826
rc = -EINVAL;
813827
goto out;
814828
}
@@ -1168,7 +1182,7 @@ static int endpoint_query_addr(struct ctx *ctx,
11681182
}
11691183

11701184
if (req_len == 0) {
1171-
warnx("BUG: zero length request");
1185+
bug_warn("zero length request");
11721186
rc = -EPROTO;
11731187
goto out;
11741188
}
@@ -1184,7 +1198,7 @@ static int endpoint_query_addr(struct ctx *ctx,
11841198
goto out;
11851199
}
11861200
if ((size_t)rc != req_len) {
1187-
warnx("BUG: incorrect sendto %zd, expected %zu", rc, req_len);
1201+
bug_warn("incorrect sendto %zd, expected %zu", rc, req_len);
11881202
rc = -EPROTO;
11891203
goto out;
11901204
}
@@ -1233,7 +1247,7 @@ static int endpoint_query_peer(const struct peer *peer,
12331247
struct sockaddr_mctp_ext addr = {0};
12341248

12351249
if (peer->state != REMOTE) {
1236-
warnx("BUG: %s bad peer %s", __func__, peer_tostr(peer));
1250+
bug_warn("%s bad peer %s", __func__, peer_tostr(peer));
12371251
return -EPROTO;
12381252
}
12391253

@@ -1347,7 +1361,7 @@ static int add_peer(struct ctx *ctx, const dest_phys *dest, mctp_eid_t eid,
13471361

13481362
n = lookup_net(ctx, net);
13491363
if (!n) {
1350-
warnx("BUG: %s Bad net %u", __func__, net);
1364+
bug_warn("%s Bad net %u", __func__, net);
13511365
return -EPROTO;
13521366
}
13531367

@@ -1395,13 +1409,13 @@ static int add_peer(struct ctx *ctx, const dest_phys *dest, mctp_eid_t eid,
13951409
static int check_peer_struct(const struct peer *peer, const struct net *n)
13961410
{
13971411
if (n->net != peer->net) {
1398-
warnx("BUG: Mismatching net %d vs peer net %u",
1412+
bug_warn("Mismatching net %d vs peer net %u",
13991413
n->net, peer->net);
14001414
return -1;
14011415
}
14021416

14031417
if (peer != n->peers[peer->eid]) {
1404-
warnx("BUG: Bad peer: net %u eid %02x", peer->net, peer->eid);
1418+
bug_warn("Bad peer: net %u eid %02x", peer->net, peer->eid);
14051419
return -1;
14061420
}
14071421

@@ -1417,12 +1431,12 @@ static int remove_peer(struct peer *peer)
14171431

14181432
n = lookup_net(peer->ctx, peer->net);
14191433
if (!n) {
1420-
warnx("BUG: %s: Bad net %u", __func__, peer->net);
1434+
bug_warn("%s: Bad net %u", __func__, peer->net);
14211435
return -EPROTO;
14221436
}
14231437

14241438
if (check_peer_struct(peer, n) != 0) {
1425-
warnx("BUG: %s: Inconsistent state", __func__);
1439+
bug_warn("%s: Inconsistent state", __func__);
14261440
return -EPROTO;
14271441
}
14281442

@@ -1450,7 +1464,7 @@ static int remove_peer(struct peer *peer)
14501464
}
14511465

14521466
if (idx == ctx->num_peers) {
1453-
warnx("BUG: peer net %u, eid %d not found on remove!",
1467+
bug_warn("peer net %u, eid %d not found on remove!",
14541468
peer->net, peer->eid);
14551469
return -EPROTO;
14561470
}
@@ -1494,12 +1508,12 @@ static int change_peer_eid(struct peer *peer, mctp_eid_t new_eid)
14941508

14951509
n = lookup_net(peer->ctx, peer->net);
14961510
if (!n) {
1497-
warnx("BUG: %s: Bad net %u", __func__, peer->net);
1511+
bug_warn("%s: Bad net %u", __func__, peer->net);
14981512
return -EPROTO;
14991513
}
15001514

15011515
if (check_peer_struct(peer, n) != 0) {
1502-
warnx("BUG: %s: Inconsistent state", __func__);
1516+
bug_warn("%s: Inconsistent state", __func__);
15031517
return -EPROTO;
15041518
}
15051519

@@ -1524,7 +1538,7 @@ static int peer_set_mtu(struct ctx *ctx, struct peer *peer, uint32_t mtu) {
15241538

15251539
ifname = mctp_nl_if_byindex(ctx->nl, peer->phys.ifindex);
15261540
if (!ifname) {
1527-
warnx("BUG %s: no interface for ifindex %d",
1541+
bug_warn("%s: no interface for ifindex %d",
15281542
__func__, peer->phys.ifindex);
15291543
return -EPROTO;
15301544
}
@@ -1555,13 +1569,13 @@ static int endpoint_assign_eid(struct ctx *ctx, sd_bus_error *berr, const dest_p
15551569

15561570
net = mctp_nl_net_byindex(ctx->nl, dest->ifindex);
15571571
if (!net) {
1558-
warnx("BUG: No net known for ifindex %d", dest->ifindex);
1572+
bug_warn("No net known for ifindex %d", dest->ifindex);
15591573
return -EPROTO;
15601574
}
15611575

15621576
n = lookup_net(ctx, net);
15631577
if (!n) {
1564-
warnx("BUG: Unknown net %d", net);
1578+
bug_warn("Unknown net %d", net);
15651579
return -EPROTO;
15661580
}
15671581

@@ -2231,7 +2245,7 @@ static int peer_route_update(struct peer *peer, uint16_t type)
22312245

22322246
link = mctp_nl_if_byindex(peer->ctx->nl, peer->phys.ifindex);
22332247
if (!link) {
2234-
warnx("BUG %s: Unknown ifindex %d", __func__, peer->phys.ifindex);
2248+
bug_warn("%s: Unknown ifindex %d", __func__, peer->phys.ifindex);
22352249
return -ENODEV;
22362250
}
22372251

@@ -2242,7 +2256,7 @@ static int peer_route_update(struct peer *peer, uint16_t type)
22422256
return mctp_nl_route_del(peer->ctx->nl, peer->eid, link);
22432257
}
22442258

2245-
warnx("BUG %s: bad type %d", __func__, type);
2259+
bug_warn("%s: bad type %d", __func__, type);
22462260
return -EPROTO;
22472261
}
22482262

@@ -3166,18 +3180,18 @@ static int del_local_eid(struct ctx *ctx, uint32_t net, int eid)
31663180

31673181
peer = find_peer_by_addr(ctx, eid, net);
31683182
if (!peer) {
3169-
warnx("BUG: local eid %d net %d to delete is missing", eid, net);
3183+
bug_warn("local eid %d net %d to delete is missing", eid, net);
31703184
return -ENOENT;
31713185
}
31723186

31733187
if (peer->state != LOCAL) {
3174-
warnx("BUG: local eid %d net %d to delete is incorrect", eid, net);
3188+
bug_warn("local eid %d net %d to delete is incorrect", eid, net);
31753189
return -EPROTO;
31763190
}
31773191

31783192
peer->local_count--;
31793193
if (peer->local_count < 0) {
3180-
warnx("BUG: local eid %d net %d bad refcount %d",
3194+
bug_warn("local eid %d net %d bad refcount %d",
31813195
eid, net, peer->local_count);
31823196
}
31833197

@@ -3218,7 +3232,7 @@ static int prune_old_nets(struct ctx *ctx)
32183232
for (size_t p = 0; p < 256; p++) {
32193233
// Sanity check that no peers are used
32203234
if (ctx->nets[i]->peers[p]) {
3221-
warnx("BUG: stale entry for eid %zd in deleted net %d",
3235+
bug_warn("stale entry for eid %zd in deleted net %d",
32223236
p, net->net);
32233237
}
32243238
}
@@ -3293,13 +3307,13 @@ static int change_net_interface(struct ctx *ctx, int ifindex, uint32_t old_net)
32933307

32943308
if (new_net == old_net) {
32953309
// Logic below may assume they differ
3296-
warnx("BUG: %s called with new=old=%d", __func__, old_net);
3310+
bug_warn("%s called with new=old=%d", __func__, old_net);
32973311
return -EPROTO;
32983312
}
32993313

33003314
old_n = lookup_net(ctx, old_net);
33013315
if (!old_n) {
3302-
warnx("BUG: %s: Bad old net %d", __func__, old_net);
3316+
bug_warn("%s: Bad old net %d", __func__, old_net);
33033317
return -EPROTO;
33043318
}
33053319
new_n = lookup_net(ctx, new_net);
@@ -3322,13 +3336,13 @@ static int change_net_interface(struct ctx *ctx, int ifindex, uint32_t old_net)
33223336
}
33233337

33243338
if (peer->net != old_net) {
3325-
warnx("BUG: %s: Mismatch old net %d vs %d, new net %d",
3339+
bug_warn("%s: Mismatch old net %d vs %d, new net %d",
33263340
__func__, peer->net, old_net, new_net);
33273341
continue;
33283342

33293343
}
33303344
if (check_peer_struct(peer, old_n) != 0) {
3331-
warnx("BUG: %s: Inconsistent state", __func__);
3345+
bug_warn("%s: Inconsistent state", __func__);
33323346
return -EPROTO;
33333347
}
33343348

@@ -3382,7 +3396,7 @@ static int add_local_eid(struct ctx *ctx, uint32_t net, int eid)
33823396

33833397
rc = add_peer(ctx, &local_phys, eid, net, &peer);
33843398
if (rc < 0) {
3385-
warn("BUG: Error adding local eid %d net %d", eid, net);
3399+
bug_warn("Error adding local eid %d net %d", eid, net);
33863400
return rc;
33873401
}
33883402
peer->state = LOCAL;
@@ -3462,7 +3476,7 @@ static int add_net(struct ctx *ctx, uint32_t net_id)
34623476
struct net *net, **tmp;
34633477

34643478
if (lookup_net(ctx, net_id) != NULL) {
3465-
warnx("BUG: add_net for existing net %d", net_id);
3479+
bug_warn("add_net for existing net %d", net_id);
34663480
return -EEXIST;
34673481
}
34683482

tests/mctp-ops-test.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ static int mctp_op_close(int sd)
216216
return close(sd);
217217
}
218218

219+
static void mctp_bug_warn(const char* fmt, va_list args)
220+
{
221+
vwarnx(fmt, args);
222+
warnx("Aborting on bug in tests");
223+
abort();
224+
}
225+
219226
const struct mctp_ops mctp_ops = {
220227
.mctp = {
221228
.socket = mctp_op_mctp_socket,
@@ -233,6 +240,7 @@ const struct mctp_ops mctp_ops = {
233240
.recvfrom = mctp_op_recvfrom,
234241
.close = mctp_op_close,
235242
},
243+
.bug_warn = mctp_bug_warn,
236244
};
237245

238246
void mctp_ops_init(void)

0 commit comments

Comments
 (0)