Development discussion of WireGuard
 help / color / mirror / Atom feed
* [PATCH] wg: include filename in error messages
@ 2024-04-30 12:25 Tony Finch
  2024-04-30 12:36 ` Tony Finch
  0 siblings, 1 reply; 2+ messages in thread
From: Tony Finch @ 2024-04-30 12:25 UTC (permalink / raw)
  To: wireguard

When wg(8) is invoked from a script, it can be hard to tell what
caused an error message if the message does not contain enough
context.

Signed-off-by: Tony Finch <dot@dotat.at>
---
 src/config.c  |  8 ++++----
 src/setconf.c | 11 +++++++----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git src/config.c src/config.c
index 81ccb47..9f15477 100644
--- src/config.c
+++ src/config.c
@@ -123,7 +123,7 @@ static bool parse_keyfile(uint8_t key[static WG_KEY_LEN], const char *path)

 	f = fopen(path, "r");
 	if (!f) {
-		perror("fopen");
+		fprintf(stderr, "open %s: %s", path, strerror(errno));
 		return false;
 	}

@@ -135,19 +135,19 @@ static bool parse_keyfile(uint8_t key[static WG_KEY_LEN], const char *path)
 			goto out;
 		}

-		fprintf(stderr, "Invalid length key in key file\n");
+		fprintf(stderr, "Invalid length key in %s\n", path);
 		goto out;
 	}
 	dst[WG_KEY_LEN_BASE64 - 1] = '\0';

 	while ((c = getc(f)) != EOF) {
 		if (!char_is_space(c)) {
-			fprintf(stderr, "Found trailing character in key file: `%c'\n", c);
+			fprintf(stderr, "Found trailing character `%c' in %s\n", c, path);
 			goto out;
 		}
 	}
 	if (ferror(f) && errno) {
-		perror("getc");
+		fprintf(stderr, "read %s: %s", path, strerror(errno));
 		goto out;
 	}
 	ret = parse_key(key, dst);
diff --git src/setconf.c src/setconf.c
index 1c5b138..414785d 100644
--- src/setconf.c
+++ src/setconf.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  */

+#include <errno.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -102,6 +103,7 @@ int setconf_main(int argc, const char *argv[])
 {
 	struct wgdevice *device = NULL;
 	struct config_ctx ctx;
+	const char *path = NULL;
 	FILE *config_input = NULL;
 	char *config_buffer = NULL;
 	size_t config_buffer_len = 0;
@@ -112,9 +114,10 @@ int setconf_main(int argc, const char *argv[])
 		return 1;
 	}

-	config_input = fopen(argv[2], "r");
+	path = argv[2];
+	config_input = fopen(path, "r");
 	if (!config_input) {
-		perror("fopen");
+		fprintf(stderr, "open %s: %s", path, strerror(errno));
 		return 1;
 	}
 	if (!config_read_init(&ctx, !strcmp(argv[0], "addconf"))) {
@@ -123,13 +126,13 @@ int setconf_main(int argc, const char *argv[])
 	}
 	while (getline(&config_buffer, &config_buffer_len, config_input) >= 0) {
 		if (!config_read_line(&ctx, config_buffer)) {
-			fprintf(stderr, "Configuration parsing error\n");
+			fprintf(stderr, "Configuration parsing error in %s\n", path);
 			goto cleanup;
 		}
 	}
 	device = config_read_finish(&ctx);
 	if (!device) {
-		fprintf(stderr, "Invalid configuration\n");
+		fprintf(stderr, "Invalid configuration in %s\n", path);
 		goto cleanup;
 	}
 	strncpy(device->name, argv[1], IFNAMSIZ - 1);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] wg: include filename in error messages
  2024-04-30 12:25 [PATCH] wg: include filename in error messages Tony Finch
@ 2024-04-30 12:36 ` Tony Finch
  0 siblings, 0 replies; 2+ messages in thread
From: Tony Finch @ 2024-04-30 12:36 UTC (permalink / raw)
  To: wireguard

Yesterday when I was setting up wireguard using ifupdown, I got a "file
not found" error which mentioned neither the program nor the filename, so
I was at a loss. After making ifupdown more verbose the problem turned out
to be wireguard.

When I was preparing this patch I really wanted to change all the error
reporting to use err(3) because in my experience it's reasonably nice for
the programmer and the user. Dunno if err(3) counts as part of a sane libc
or if it's worth spending time on such a patch?

-- 
Tony Finch  <dot@dotat.at>  https://dotat.at/
Fair Isle, Faeroes: Variable 2 to 4 in west Faeroes at first,
otherwise southeasterly 4 to 6. Moderate, occasionally rough at first
in east Fair Isle. Showers, fog patches later. Good, occasionally very
poor later.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-11-18 13:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-30 12:25 [PATCH] wg: include filename in error messages Tony Finch
2024-04-30 12:36 ` Tony Finch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).