Re: [PATCH bpf] samples/bpf: Fix the error return code of xdp_redirect's main()

From: wanghai (M)
Date: Mon Jul 05 2021 - 03:45:13 EST



在 2021/7/2 18:29, Jesper Dangaard Brouer 写道:

On 16/06/2021 06.25, Wang Hai wrote:
Fix to return a negative error code from the error handling
case instead of 0, as done elsewhere in this function.

The main() function in C should never return a negative value on Unix POSIX systems.


There is a good explaination in exit(3p): `man 3p exit`

   The  value  of  status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process.

Thus, negative values are often seen as 255 in the $? program exit status variable $?.


Also explained in exit(3):

    The C standard specifies two constants, EXIT_SUCCESS=0 and EXIT_FAILURE=1.

I see the 'samples/bpf/xdp_redirect_user.c' in most places just use 0 or 1.

Got it, thanks for the explanation, I will fix it to return 1, just like the other error paths in samples/bpf/xdp_redirect_user.c

If bpf_map_update_elem() failed, main() should return a negative error.

Fixes: 832622e6bd18 ("xdp: sample program for new bpf_redirect helper")
Signed-off-by: Wang Hai <wanghai38@xxxxxxxxxx>
---
  samples/bpf/xdp_redirect_user.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/samples/bpf/xdp_redirect_user.c b/samples/bpf/xdp_redirect_user.c
index 41d705c3a1f7..c903f1ccc15e 100644
--- a/samples/bpf/xdp_redirect_user.c
+++ b/samples/bpf/xdp_redirect_user.c
@@ -213,5 +213,5 @@ int main(int argc, char **argv)
      poll_stats(2, ifindex_out);
    out:
-    return 0;
+    return ret;
  }


(Sorry, I didn't complain it time as I see this patch is already applied)

.