Use patty_error_fmt() for errors in pattyd(8)

This commit is contained in:
XANTRONIX Development 2020-09-18 23:31:05 -05:00 committed by XANTRONIX Industrial
parent d1b507e81f
commit 7eca9a09c9

View file

@ -37,7 +37,9 @@ static int usage(int argc, char **argv, const char *message, ...) {
struct context { struct context {
char *config_file; char *config_file;
patty_daemon *daemon; patty_daemon *daemon;
patty_error e;
}; };
static int handle_sock(struct context *ctx, static int handle_sock(struct context *ctx,
@ -45,15 +47,15 @@ static int handle_sock(struct context *ctx,
int argc, int argc,
char **argv) { char **argv) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "%s: line %d: Invalid arguments for 'sock'\n", patty_error_fmt(&ctx->e, "line %d: Invalid arguments for 'sock'",
ctx->config_file, lineno); lineno);
goto error_invalid_args; goto error_invalid_args;
} }
if (patty_daemon_set_sock_path(ctx->daemon, argv[1]) < 0) { if (patty_daemon_set_sock_path(ctx->daemon, argv[1]) < 0) {
fprintf(stderr, "%s: line %d: Unable to set socket path to %s: %s\n", patty_error_fmt(&ctx->e, "line %d: Unable to set socket path to %s: %s",
ctx->config_file, lineno, argv[1], strerror(errno)); lineno, argv[1], strerror(errno));
goto error_set_sock_path; goto error_set_sock_path;
} }
@ -70,15 +72,15 @@ static int handle_pid(struct context *ctx,
int argc, int argc,
char **argv) { char **argv) {
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "%s: line %d: Invalid arguments for 'sock'\n", patty_error_fmt(&ctx->e, "line %d: Invalid arguments for 'sock'",
ctx->config_file, lineno); lineno);
goto error_invalid_args; goto error_invalid_args;
} }
if (patty_daemon_set_pidfile(ctx->daemon, argv[1]) < 0) { if (patty_daemon_set_pidfile(ctx->daemon, argv[1]) < 0) {
fprintf(stderr, "%s: line %d: Unable to set pidfile to %s: %s\n", patty_error_fmt(&ctx->e, "line %d: Unable to set pidfile to %s: %s",
ctx->config_file, lineno, argv[1], strerror(errno)); lineno, argv[1], strerror(errno));
goto error_set_pidfile; goto error_set_pidfile;
} }
@ -98,20 +100,21 @@ static int handle_if(struct context *ctx,
patty_error e; patty_error e;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "%s: line %d: No interface name provided\n", patty_error_fmt(&ctx->e, "line %d: No interface name provided",
ctx->config_file, lineno); lineno);
goto error_invalid; goto error_invalid;
} else if (argc < 3) { } else if (argc < 3) {
fprintf(stderr, "%s: line %d: No interface options provided\n", patty_error_fmt(&ctx->e, "line %d: No interface options provided",
ctx->config_file, lineno); lineno);
goto error_invalid; goto error_invalid;
} }
if ((iface = patty_bin_if_create(argc, argv, &e)) == NULL) { if ((iface = patty_bin_if_create(argc, argv, &e)) == NULL) {
fprintf(stderr, "%s: line %d: %s\n", patty_error_fmt(&ctx->e, "line %d: %s",
ctx->config_file, lineno, patty_error_string(&e)); lineno,
patty_error_set(&e)? patty_error_string(&e): strerror(errno));
goto error_invalid; goto error_invalid;
} else { } else {
@ -125,8 +128,7 @@ static int handle_if(struct context *ctx,
} }
if (patty_daemon_if_add(ctx->daemon, iface) < 0) { if (patty_daemon_if_add(ctx->daemon, iface) < 0) {
fprintf(stderr, "%s: line %d: Unable to create interface %s: %s\n", patty_error_fmt(&ctx->e, "line %d: Unable to create interface %s: %s",
ctx->config_file,
lineno, lineno,
patty_ax25_if_name(iface), patty_ax25_if_name(iface),
strerror(errno)); strerror(errno));
@ -148,30 +150,30 @@ static int handle_route(struct context *ctx,
int argc, int argc,
char **argv) { char **argv) {
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "%s: line %d: Invalid route declaration\n", patty_error_fmt(&ctx->e, "line %d: Invalid route declaration",
ctx->config_file, lineno); lineno);
goto error_invalid_route; goto error_invalid_route;
} }
if (strcmp(argv[1], "default") == 0) { if (strcmp(argv[1], "default") == 0) {
if (argc != 4 || strcmp(argv[2], "if") != 0) { if (argc != 4 || strcmp(argv[2], "if") != 0) {
fprintf(stderr, "%s: line %d: Invalid default route declaration\n", patty_error_fmt(&ctx->e, "line %d: Invalid default route declaration",
ctx->config_file, lineno); lineno);
goto error_invalid_route; goto error_invalid_route;
} }
if (patty_daemon_route_add_default(ctx->daemon, argv[3]) < 0) { if (patty_daemon_route_add_default(ctx->daemon, argv[3]) < 0) {
fprintf(stderr, "%s: line %d: Unable to add default route for interface %s: %s", patty_error_fmt(&ctx->e, "line %d: Unable to add default route for interface %s: %s",
ctx->config_file, lineno, argv[3], strerror(errno)); lineno, argv[3], strerror(errno));
goto error_daemon_route_add; goto error_daemon_route_add;
} }
} else if (strcmp(argv[1], "station") == 0) { } else if (strcmp(argv[1], "station") == 0) {
if (argc < 7 || strcmp(argv[3], "if") != 0 || strcmp(argv[5], "path") != 0) { if (argc < 7 || strcmp(argv[3], "if") != 0 || strcmp(argv[5], "path") != 0) {
fprintf(stderr, "%s: line %d: Invalid station route declaration\n", patty_error_fmt(&ctx->e, "line %d: Invalid station route declaration",
ctx->config_file, lineno); lineno);
goto error_invalid_route; goto error_invalid_route;
} }
@ -181,14 +183,14 @@ static int handle_route(struct context *ctx,
argv[2], argv[2],
(const char **)&argv[6], (const char **)&argv[6],
argc - 6) < 0) { argc - 6) < 0) {
fprintf(stderr, "%s: line %d: Unable to add route for interface %s: %s\n", patty_error_fmt(&ctx->e, "line %d: Unable to add route for interface %s: %s",
ctx->config_file, lineno, argv[4], strerror(errno)); lineno, argv[4], strerror(errno));
goto error_daemon_route_add; goto error_daemon_route_add;
} }
} else { } else {
fprintf(stderr, "%s: line %d: Invalid route type '%s'", patty_error_fmt(&ctx->e, "line %d: Invalid route type '%s'",
ctx->config_file, lineno, argv[1]); lineno, argv[1]);
goto error_invalid_route; goto error_invalid_route;
} }
@ -256,8 +258,8 @@ static int handle_config_line(patty_conf_file *file,
} }
} }
fprintf(stderr, "%s: line %d: Unknown configuration value '%s'", patty_error_fmt(&ctx->e, "line %d: Unknown configuration value '%s'",
ctx->config_file, lineno, argv[0]); lineno, argv[0]);
done: done:
free(argv); free(argv);
@ -364,6 +366,7 @@ int main(int argc, char **argv) {
.config_file = DEFAULT_CONFIG_FILE .config_file = DEFAULT_CONFIG_FILE
}; };
memset(&ctx.e, '\0', sizeof(ctx.e));
memset(flags, '\0', sizeof(flags)); memset(flags, '\0', sizeof(flags));
if ((ctx.daemon = patty_daemon_new()) == NULL) { if ((ctx.daemon = patty_daemon_new()) == NULL) {
@ -413,10 +416,12 @@ int main(int argc, char **argv) {
goto error_config; goto error_config;
} }
} else if (patty_conf_read(ctx.config_file, handle_config_line, &ctx) < 0) { } else if (patty_conf_read(ctx.config_file, handle_config_line, &ctx) < 0) {
if (errno) { fprintf(stderr, "%s: %s: %s: %s\n",
fprintf(stderr, "%s: %s: %s: %s\n", argv[0],
argv[0], "patty_conf_read()", ctx.config_file, strerror(errno)); "patty_conf_read()",
} ctx.config_file,
patty_error_set(&ctx.e)? patty_error_string(&ctx.e):
strerror(errno));
goto error_config; goto error_config;
} }