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