journal-gatewayd: convert to the new option parser

Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-24 09:25:27 +02:00
parent ed6cf6bed8
commit bcbf53b858
+47 -78
View File
@@ -1,7 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <getopt.h>
#include <microhttpd.h>
#include <stdlib.h>
#include <string.h>
@@ -18,6 +17,7 @@
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "glob-util.h"
#include "hostname-setup.h"
#include "hostname-util.h"
@@ -28,6 +28,7 @@
#include "main-func.h"
#include "memory-util.h"
#include "microhttpd-util.h"
#include "options.h"
#include "os-util.h"
#include "output-mode.h"
#include "parse-util.h"
@@ -1090,92 +1091,52 @@ static mhd_result request_handler(
static int help(void) {
_cleanup_free_ char *link = NULL;
_cleanup_(table_unrefp) Table *options = NULL;
int r;
r = terminal_urlify_man("systemd-journal-gatewayd.service", "8", &link);
if (r < 0)
return log_oom();
printf("%s [OPTIONS...] ...\n\n"
"HTTP server for journal events.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --cert=CERT.PEM Server certificate in PEM format\n"
" --key=KEY.PEM Server key in PEM format\n"
" --trust=CERT.PEM Certificate authority certificate in PEM format\n"
" --system Serve system journal\n"
" --user Serve the user journal for the current user\n"
" -m --merge Serve all available journals\n"
" -D --directory=PATH Serve journal files in directory\n"
" --file=PATH Serve this journal file\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
link);
r = option_parser_get_help_table(&options);
if (r < 0)
return r;
printf("%s [OPTIONS...] ...\n\n"
"HTTP server for journal events.\n\n",
program_invocation_short_name);
r = table_print_or_warn(options);
if (r < 0)
return r;
printf("\nSee the %s for details.\n", link);
return 0;
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_KEY,
ARG_CERT,
ARG_TRUST,
ARG_USER,
ARG_SYSTEM,
ARG_MERGE,
ARG_FILE,
};
int r, c;
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "key", required_argument, NULL, ARG_KEY },
{ "cert", required_argument, NULL, ARG_CERT },
{ "trust", required_argument, NULL, ARG_TRUST },
{ "user", no_argument, NULL, ARG_USER },
{ "system", no_argument, NULL, ARG_SYSTEM },
{ "merge", no_argument, NULL, 'm' },
{ "directory", required_argument, NULL, 'D' },
{ "file", required_argument, NULL, ARG_FILE },
{}
};
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "hD:", options, NULL)) >= 0)
OptionParser state = { argc, argv };
const char *arg;
int r;
FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
switch (c) {
case 'h':
OPTION_COMMON_HELP:
return help();
case ARG_VERSION:
OPTION_COMMON_VERSION:
return version();
case ARG_KEY:
if (arg_key_pem)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Key file specified twice");
r = read_full_file_full(
AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
NULL,
&arg_key_pem, NULL);
if (r < 0)
return log_error_errno(r, "Failed to read key file: %m");
assert(arg_key_pem);
break;
case ARG_CERT:
OPTION_LONG("cert", "CERT.PEM", "Server certificate in PEM format"):
if (arg_cert_pem)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Certificate file specified twice");
r = read_full_file_full(
AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
AT_FDCWD, arg, UINT64_MAX, SIZE_MAX,
READ_FULL_FILE_CONNECT_SOCKET,
NULL,
&arg_cert_pem, NULL);
@@ -1184,13 +1145,27 @@ static int parse_argv(int argc, char *argv[]) {
assert(arg_cert_pem);
break;
case ARG_TRUST:
OPTION_LONG("key", "KEY.PEM", "Server key in PEM format"):
if (arg_key_pem)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Key file specified twice");
r = read_full_file_full(
AT_FDCWD, arg, UINT64_MAX, SIZE_MAX,
READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
NULL,
&arg_key_pem, NULL);
if (r < 0)
return log_error_errno(r, "Failed to read key file: %m");
assert(arg_key_pem);
break;
OPTION_LONG("trust", "CERT.PEM", "Certificate authority certificate in PEM format"):
#if HAVE_GNUTLS
if (arg_trust_pem)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"CA certificate file specified twice");
r = read_full_file_full(
AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
AT_FDCWD, arg, UINT64_MAX, SIZE_MAX,
READ_FULL_FILE_CONNECT_SOCKET,
NULL,
&arg_trust_pem, NULL);
@@ -1203,38 +1178,32 @@ static int parse_argv(int argc, char *argv[]) {
"Option --trust= is not available.");
#endif
case ARG_SYSTEM:
OPTION_LONG("system", NULL, "Serve system journal"):
arg_journal_type |= SD_JOURNAL_SYSTEM;
break;
case ARG_USER:
OPTION_LONG("user", NULL, "Serve the user journal for the current user"):
arg_journal_type |= SD_JOURNAL_CURRENT_USER;
break;
case 'm':
OPTION('m', "merge", NULL, "Serve all available journals"):
arg_merge = true;
break;
case 'D':
r = free_and_strdup_warn(&arg_directory, optarg);
OPTION('D', "directory", "PATH", "Serve journal files in directory"):
r = free_and_strdup_warn(&arg_directory, arg);
if (r < 0)
return r;
break;
case ARG_FILE:
r = glob_extend(&arg_file, optarg, GLOB_NOCHECK);
OPTION_LONG("file", "PATH", "Serve this journal file"):
r = glob_extend(&arg_file, arg, GLOB_NOCHECK);
if (r < 0)
return log_error_errno(r, "Failed to add paths: %m");
break;
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (optind < argc)
if (option_parser_get_n_args(&state) > 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program does not take arguments.");