]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/update-done/update-done.c
Merge pull request #33079 from poettering/watchdog-no-disarm
[thirdparty/systemd.git] / src / update-done / update-done.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
8ea48dfc 2
ca78ad1d
ZJS
3#include <sys/stat.h>
4#include <sys/types.h>
5#include <unistd.h>
6
5a1d6763 7#include "alloc-util.h"
872c4039 8#include "fileio-label.h"
d7b8eec7 9#include "selinux-util.h"
ca78ad1d 10#include "time-util.h"
8ea48dfc 11
4aa4d2ae 12#define MESSAGE \
fb8b0869
IS
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" \
cc7de2ba 15 "# was updated. See man:systemd-update-done.service(8).\n"
4aa4d2ae 16
8ea48dfc 17static int apply_timestamp(const char *path, struct timespec *ts) {
872c4039 18 _cleanup_free_ char *message = NULL;
fb8b0869 19 int r;
8ea48dfc 20
fb8b0869
IS
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.
fb8b0869 24 */
8ea48dfc 25
872c4039
ZJS
26 if (asprintf(&message,
27 MESSAGE
28 "TIMESTAMP_NSEC=" NSEC_FMT "\n",
29 timespec_load_nsec(ts)) < 0)
30 return log_oom();
8ea48dfc 31
872c4039
ZJS
32 r = write_string_file_atomic_label_ts(path, message, ts);
33 if (r == -EROFS)
1eee15c3
FR
34 log_debug_errno(r, "Cannot create \"%s\", file system is read-only.", path);
35 else if (r < 0)
872c4039 36 return log_error_errno(r, "Failed to write \"%s\": %m", path);
8ea48dfc
LP
37 return 0;
38}
39
40int main(int argc, char *argv[]) {
41 struct stat st;
7dbb1d08 42 int r, q = 0;
8ea48dfc 43
d2acb93d 44 log_setup();
8ea48dfc
LP
45
46 if (stat("/usr", &st) < 0) {
755bde37 47 log_error_errno(errno, "Failed to stat /usr: %m");
8ea48dfc
LP
48 return EXIT_FAILURE;
49 }
50
a452c807 51 r = mac_init();
a9ba0e32 52 if (r < 0)
fd1bff7d 53 return EXIT_FAILURE;
8ea48dfc 54
7dbb1d08 55 r = apply_timestamp("/etc/.updated", &st.st_mtim);
8ea48dfc 56 q = apply_timestamp("/var/.updated", &st.st_mtim);
8ea48dfc 57
7dbb1d08 58 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
8ea48dfc 59}