]>
git.proxmox.com Git - systemd.git/blob - src/update-done/update-done.c
2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "selinux-util.h"
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] = {
40 if (stat(path
, &st
) >= 0) {
41 /* Is the timestamp file already newer than the OS? If
42 * so, there's nothing to do. We ignore the nanosecond
43 * component of the timestamp, since some file systems
44 * do not support any better accuracy than 1s and we
45 * have no way to identify the accuracy
46 * available. Most notably ext4 on small disks (where
47 * 128 byte inodes are used) does not support better
48 * accuracy than 1s. */
49 if (st
.st_mtim
.tv_sec
> ts
->tv_sec
)
52 /* It is older? Then let's update it */
53 if (utimensat(AT_FDCWD
, path
, twice
, AT_SYMLINK_NOFOLLOW
) < 0) {
56 return log_debug("Can't update timestamp file %s, file system is read-only.", path
);
58 return log_error_errno(errno
, "Failed to update timestamp on %s: %m", path
);
61 } else if (errno
== ENOENT
) {
62 _cleanup_close_
int fd
= -1;
65 /* The timestamp file doesn't exist yet? Then let's create it. */
67 r
= mac_selinux_create_file_prepare(path
, S_IFREG
);
69 return log_error_errno(r
, "Failed to set SELinux context for %s: %m", path
);
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();
76 return log_debug("Can't create timestamp file %s, file system is read-only.", path
);
78 return log_error_errno(errno
, "Failed to create timestamp file %s: %m", path
);
81 (void) loop_write(fd
, MESSAGE
, strlen(MESSAGE
), false);
83 if (futimens(fd
, twice
) < 0)
84 return log_error_errno(errno
, "Failed to update timestamp on %s: %m", path
);
86 log_error_errno(errno
, "Failed to stat() timestamp file %s: %m", path
);
91 int main(int argc
, char *argv
[]) {
95 log_set_target(LOG_TARGET_AUTO
);
96 log_parse_environment();
99 if (stat("/usr", &st
) < 0) {
100 log_error_errno(errno
, "Failed to stat /usr: %m");
104 r
= mac_selinux_init(NULL
);
106 log_error_errno(r
, "SELinux setup failed: %m");
110 r
= apply_timestamp("/etc/.updated", &st
.st_mtim
);
111 q
= apply_timestamp("/var/.updated", &st
.st_mtim
);
114 return r
< 0 || q
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;