update-done: add basic argument parsing and --help

We certainly want to reject calls with any args specified. Previously
we would just silently ignore any args.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-03-19 16:47:02 +01:00
parent bca9a6e2be
commit 20f7f0a891
2 changed files with 73 additions and 2 deletions

View File

@@ -3,7 +3,7 @@
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
<refentry id="systemd-update-done.service">
<refentry id="systemd-update-done.service" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>systemd-update-done.service</title>
@@ -18,7 +18,7 @@
<refnamediv>
<refname>systemd-update-done.service</refname>
<refname>systemd-update-done</refname>
<refpurpose>Mark <filename>/etc/</filename> and <filename>/var/</filename> fully updated</refpurpose>
<refpurpose>Mark <filename>/etc/</filename> and <filename>/var/</filename> as fully updated</refpurpose>
</refnamediv>
<refsynopsisdiv>
@@ -63,6 +63,16 @@
reboot where the kernel switch is not specified anymore.</para>
</refsect1>
<refsect1>
<title>Options</title>
<para>The following options are understood:</para>
<variablelist>
<xi:include href="standard-options.xml" xpointer="help" />
</variablelist>
</refsect1>
<refsect1>
<title>See Also</title>
<para><simplelist type="inline">

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@@ -8,6 +9,7 @@
#include "fileio.h"
#include "main-func.h"
#include "path-util.h"
#include "pretty-print.h"
#include "selinux-util.h"
#include "time-util.h"
@@ -41,10 +43,69 @@ static int save_timestamp(const char *dir, struct timespec *ts) {
return 0;
}
static int help(void) {
_cleanup_free_ char *link = NULL;
int r;
r = terminal_urlify_man("systemd-update-done", "8", &link);
if (r < 0)
return log_oom();
printf("%1$s [OPTIONS...]\n\n"
"%5$sMark /etc/ and /var/ as fully updated.%6$s\n"
"\n%3$sOptions:%4$s\n"
" -h --help Show this help\n"
"\nSee the %2$s for details.\n",
program_invocation_short_name,
link,
ansi_underline(),
ansi_normal(),
ansi_highlight(),
ansi_normal());
return 0;
}
static int parse_argv(int argc, char *argv[]) {
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{},
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
switch (c) {
case 'h':
return help();
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (optind < argc)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
return 1;
}
static int run(int argc, char *argv[]) {
struct stat st;
int r;
r = parse_argv(argc, argv);
if (r <= 0)
return r;
log_setup();
if (stat("/usr", &st) < 0)