Debian Codegraph @

src/update-done/update-done.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4
5 #include "alloc-util.h"
6 #include "build.h"
7 #include "chase.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "format-table.h"
12 #include "label-util.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "options.h"
16 #include "parse-argument.h"
17 #include "pretty-print.h"
18 #include "string-util.h"
19 #include "time-util.h"
20
21 static char *arg_root = NULL;
22
23 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
24
25 static int save_timestamp(const char *dir, struct timespec *ts) {
26 _cleanup_free_ char *message = NULL, *dirpath = NULL;
27 _cleanup_close_ int fd = -EBADF;
28 int r;
29
30 /*
31 * We store the timestamp both as mtime of the file and in the file itself,
32 * to support filesystems which cannot store nanosecond-precision timestamps.
33 */
34
35 fd = chase_and_open(dir, arg_root,
36 CHASE_PREFIX_ROOT | CHASE_WARN | CHASE_MUST_BE_DIRECTORY,
37 O_DIRECTORY | O_CLOEXEC | O_CREAT,
38 &dirpath);
39 if (fd < 0)
40 return log_error_errno(fd, "Failed to open %s%s: %m", strempty(arg_root), dir);
41
42 if (asprintf(&message,
43 "# This file was created by systemd-update-done. The timestamp below is the\n"
44 "# modification time of /usr/ for which the most recent updates of %s have\n"
45 "# been applied. See man:systemd-update-done.service(8) for details.\n"
46 "TIMESTAMP_NSEC=" NSEC_FMT "\n",
47 dir,
48 timespec_load_nsec(ts)) < 0)
49 return log_oom();
50
51 r = write_string_file_full(fd, ".updated", message,
52 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL,
53 ts, NULL);
54 if (r == -EROFS && !arg_root)
55 log_debug_errno(r, "Cannot create \"%s/.updated\", file system is read-only.", dirpath);
56 else if (r < 0)
57 return log_error_errno(r, "Failed to write \"%s/.updated\": %m", dirpath);
58 else
59 log_debug("%s/.updated updated to TIMESTAMP_NSEC="NSEC_FMT, dirpath, timespec_load_nsec(ts));
60
61 return 0;
62 }
63
64 static int help(void) {
65 _cleanup_free_ char *link = NULL;
66 _cleanup_(table_unrefp) Table *options = NULL;
67 int r;
68
69 r = terminal_urlify_man("systemd-update-done", "8", &link);
70 if (r < 0)
71 return log_oom();
72
73 r = option_parser_get_help_table(&options);
74 if (r < 0)
75 return r;
76
77 printf("%s [OPTIONS...]\n"
78 "\n%sMark /etc/ and /var/ as fully updated.%s\n"
79 "\n%sOptions:%s\n",
80 program_invocation_short_name,
81 ansi_highlight(),
82 ansi_normal(),
83 ansi_underline(),
84 ansi_normal());
85 r = table_print_or_warn(options);
86 if (r < 0)
87 return r;
88
89 printf("\nSee the %s for details.\n", link);
90 return 0;
91 }
92
93 static int parse_argv(int argc, char *argv[]) {
94 int r;
95
96 assert(argc >= 0);
97 assert(argv);
98
99 OptionParser opts = { argc, argv };
100
101 FOREACH_OPTION_OR_RETURN(c, &opts)
102 switch (c) {
103 OPTION_COMMON_HELP:
104 return help();
105
106 OPTION_COMMON_VERSION:
107 return version();
108
109 OPTION_LONG("root", "PATH", "Operate on root directory PATH"):
110 r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root);
111 if (r < 0)
112 return r;
113 break;
114 }
115
116 if (option_parser_get_n_args(&opts) > 0)
117 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
118
119 return 1;
120 }
121
122
123 static int run(int argc, char *argv[]) {
124 struct stat st;
125 int r;
126
127 r = parse_argv(argc, argv);
128 if (r <= 0)
129 return r;
130
131 log_setup();
132
133 r = chase_and_stat("/usr", arg_root,
134 CHASE_PREFIX_ROOT | CHASE_WARN | CHASE_MUST_BE_DIRECTORY,
135 /* ret_path= */ NULL,
136 &st);
137 if (r < 0)
138 return log_error_errno(r, "Failed to stat %s/usr/: %m", strempty(arg_root));
139
140 r = mac_init();
141 if (r < 0)
142 return r;
143
144 r = 0;
145 RET_GATHER(r, save_timestamp("/etc/", &st.st_mtim));
146 RET_GATHER(r, save_timestamp("/var/", &st.st_mtim));
147 return r;
148 }
149
150 DEFINE_MAIN_FUNCTION(run);