]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-done/update-done.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / update-done / update-done.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "fileio-label.h"
23 #include "selinux-util.h"
24 #include "util.h"
25
26 #define MESSAGE \
27 "# This file was created by systemd-update-done. Its only \n" \
28 "# purpose is to hold a timestamp of the time this directory\n" \
29 "# was updated. See man:systemd-update-done.service(8).\n"
30
31 static int apply_timestamp(const char *path, struct timespec *ts) {
32 _cleanup_free_ char *message = NULL;
33 int r;
34
35 /*
36 * We store the timestamp both as mtime of the file and in the file itself,
37 * to support filesystems which cannot store nanosecond-precision timestamps.
38 */
39
40 if (asprintf(&message,
41 MESSAGE
42 "TIMESTAMP_NSEC=" NSEC_FMT "\n",
43 timespec_load_nsec(ts)) < 0)
44 return log_oom();
45
46 r = write_string_file_atomic_label_ts(path, message, ts);
47 if (r == -EROFS)
48 return log_debug("Cannot create \"%s\", file system is read-only.", path);
49 if (r < 0)
50 return log_error_errno(r, "Failed to write \"%s\": %m", path);
51 return 0;
52 }
53
54 int main(int argc, char *argv[]) {
55 struct stat st;
56 int r, q = 0;
57
58 log_set_target(LOG_TARGET_AUTO);
59 log_parse_environment();
60 log_open();
61
62 if (stat("/usr", &st) < 0) {
63 log_error_errno(errno, "Failed to stat /usr: %m");
64 return EXIT_FAILURE;
65 }
66
67 r = mac_selinux_init();
68 if (r < 0) {
69 log_error_errno(r, "SELinux setup failed: %m");
70 goto finish;
71 }
72
73 r = apply_timestamp("/etc/.updated", &st.st_mtim);
74 q = apply_timestamp("/var/.updated", &st.st_mtim);
75
76 finish:
77 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
78 }