1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 "This file was created by systemd-update-done. Its only \n" \
27 "purpose is to hold a timestamp of the time this directory\n" \
28 "was updated. See systemd-update-done.service(8).\n"
30 static int apply_timestamp(const char *path, struct timespec *ts) {
31 struct timespec twice[2];
37 if (stat(path, &st) >= 0) {
38 /* Is the timestamp file already newer than the OS? If so, there's nothing to do. */
39 if (st.st_mtim.tv_sec > ts->tv_sec ||
40 (st.st_mtim.tv_sec == ts->tv_sec && st.st_mtim.tv_nsec >= ts->tv_nsec))
43 /* It is older? Then let's update it */
47 if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
50 log_debug("Can't update timestamp file %s, file system is read-only.", path);
54 log_error("Failed to update timestamp on %s: %m", path);
58 } else if (errno == ENOENT) {
59 _cleanup_close_ int fd = -1;
62 /* The timestamp file doesn't exist yet? Then let's create it. */
64 r = mac_selinux_create_file_prepare(path, S_IFREG);
66 log_error("Failed to set SELinux context for %s: %s",
71 fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
72 mac_selinux_create_file_clear();
77 log_debug("Can't create timestamp file %s, file system is read-only.", path);
81 log_error("Failed to create timestamp file %s: %m", path);
85 (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
90 if (futimens(fd, twice) < 0) {
91 log_error("Failed to update timestamp on %s: %m", path);
95 log_error("Failed to stat() timestamp file %s: %m", path);
102 int main(int argc, char *argv[]) {
106 log_set_target(LOG_TARGET_AUTO);
107 log_parse_environment();
110 if (stat("/usr", &st) < 0) {
111 log_error("Failed to stat /usr: %m");
115 r = mac_selinux_init(NULL);
117 log_error("SELinux setup failed: %s", strerror(-r));
121 r = apply_timestamp("/etc/.updated", &st.st_mtim);
122 q = apply_timestamp("/var/.updated", &st.st_mtim);
125 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;