From 7746573a80be57d39de3c8c9b5f8b407ed9ec31d Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 25 Jan 2022 04:30:24 +0100 Subject: [PATCH] fix some cases of rc == 0 on error When genl_socket_talk() fails, return rc != 0. While testing the new osmo-upf program, I noticed that I failed to get a "Operation not permitted" error when forgetting to set cap_net_admin on the osmo-upf binary. It looked like everything should work, but doesn't. It is possible to catch these error cases without this patch, by monitoring errno. That may well be the intention of the API? I'm still submitting this patch because it seems better to return rc != 0. Returning -2 is an arbitrary choice. Other places return -1, so -2 makes these cases distinguishable. Not really necessary. Here is a code example that also catches all error cases without this patch: errno = 0; rc = gtp_add_tunnel(genl_id, nl, t); if (errno) { rc = -errno; } else if (rc) { rc = -EINVAL; } else { tun->active = true; } return rc; Related: SYS#5599 Change-Id: I22fd69709e023572c6c616a4184340a554456faf --- src/gtp-genl.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/gtp-genl.c b/src/gtp-genl.c index f12f872..88c0f80 100644 --- a/src/gtp-genl.c +++ b/src/gtp-genl.c @@ -77,8 +77,10 @@ int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) GTP_CMD_NEWPDP); gtp_build_payload(nlh, t); - if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) + if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) { perror("genl_socket_talk"); + return -2; + } return 0; } @@ -94,8 +96,10 @@ int gtp_del_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t) GTP_CMD_DELPDP); gtp_build_payload(nlh, t); - if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) + if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0) { perror("genl_socket_talk"); + return -2; + } return 0; } @@ -200,7 +204,7 @@ int gtp_list_tunnel(int genl_id, struct mnl_socket *nl) if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) { perror("genl_socket_talk"); - return 0; + return -2; } return 0;