1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "fileio-label.h"
9 #include "selinux-util.h"
10 #include "time-util.h"
11
12 #define MESSAGE \
13 "# This file was created by systemd-update-done. Its only \n" \
14 "# purpose is to hold a timestamp of the time this directory\n" \
15 "# was updated. See man:systemd-update-done.service(8).\n"
16
apply_timestamp(const char * path,struct timespec * ts)17 static int apply_timestamp(const char *path, struct timespec *ts) {
18 _cleanup_free_ char *message = NULL;
19 int r;
20
21 /*
22 * We store the timestamp both as mtime of the file and in the file itself,
23 * to support filesystems which cannot store nanosecond-precision timestamps.
24 */
25
26 if (asprintf(&message,
27 MESSAGE
28 "TIMESTAMP_NSEC=" NSEC_FMT "\n",
29 timespec_load_nsec(ts)) < 0)
30 return log_oom();
31
32 r = write_string_file_atomic_label_ts(path, message, ts);
33 if (r == -EROFS)
34 log_debug_errno(r, "Cannot create \"%s\", file system is read-only.", path);
35 else if (r < 0)
36 return log_error_errno(r, "Failed to write \"%s\": %m", path);
37 return 0;
38 }
39
main(int argc,char * argv[])40 int main(int argc, char *argv[]) {
41 struct stat st;
42 int r, q = 0;
43
44 log_setup();
45
46 if (stat("/usr", &st) < 0) {
47 log_error_errno(errno, "Failed to stat /usr: %m");
48 return EXIT_FAILURE;
49 }
50
51 r = mac_selinux_init();
52 if (r < 0)
53 return EXIT_FAILURE;
54
55 r = apply_timestamp("/etc/.updated", &st.st_mtim);
56 q = apply_timestamp("/var/.updated", &st.st_mtim);
57
58 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
59 }
60