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 return log_debug("Can't update timestamp file %s, file system is read-only.", path);
52 return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
55 } else if (errno == ENOENT) {
56 _cleanup_close_ int fd = -1;
59 /* The timestamp file doesn't exist yet? Then let's create it. */
61 r = mac_selinux_create_file_prepare(path, S_IFREG);
63 return log_error_errno(r, "Failed to set SELinux context for %s: %m", path);
65 fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
66 mac_selinux_create_file_clear();
70 return log_debug("Can't create timestamp file %s, file system is read-only.", path);
72 return log_error_errno(errno, "Failed to create timestamp file %s: %m", path);
75 (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
80 if (futimens(fd, twice) < 0)
81 return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
83 log_error_errno(errno, "Failed to stat() timestamp file %s: %m", path);
88 int main(int argc, char *argv[]) {
92 log_set_target(LOG_TARGET_AUTO);
93 log_parse_environment();
96 if (stat("/usr", &st) < 0) {
97 log_error_errno(errno, "Failed to stat /usr: %m");
101 r = mac_selinux_init(NULL);
103 log_error_errno(r, "SELinux setup failed: %m");
107 r = apply_timestamp("/etc/.updated", &st.st_mtim);
108 q = apply_timestamp("/var/.updated", &st.st_mtim);
111 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;