]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-done/update-done.c
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/>.
24 #include "selinux-util.h"
28 "This file was created by systemd-update-done. Its only \n" \
29 "purpose is to hold a timestamp of the time this directory\n" \
30 "was updated. See systemd-update-done.service(8).\n"
32 static int apply_timestamp(const char *path
, struct timespec
*ts
) {
33 struct timespec twice
[2] = {
42 if (stat(path
, &st
) >= 0) {
43 /* Is the timestamp file already newer than the OS? If
44 * so, there's nothing to do. We ignore the nanosecond
45 * component of the timestamp, since some file systems
46 * do not support any better accuracy than 1s and we
47 * have no way to identify the accuracy
48 * available. Most notably ext4 on small disks (where
49 * 128 byte inodes are used) does not support better
50 * accuracy than 1s. */
51 if (st
.st_mtim
.tv_sec
> ts
->tv_sec
)
54 /* It is older? Then let's update it */
55 if (utimensat(AT_FDCWD
, path
, twice
, AT_SYMLINK_NOFOLLOW
) < 0) {
58 return log_debug("Can't update timestamp file %s, file system is read-only.", path
);
60 return log_error_errno(errno
, "Failed to update timestamp on %s: %m", path
);
63 } else if (errno
== ENOENT
) {
64 _cleanup_close_
int fd
= -1;
67 /* The timestamp file doesn't exist yet? Then let's create it. */
69 r
= mac_selinux_create_file_prepare(path
, S_IFREG
);
71 return log_error_errno(r
, "Failed to set SELinux context for %s: %m", path
);
73 fd
= open(path
, O_CREAT
|O_EXCL
|O_WRONLY
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0644);
74 mac_selinux_create_file_clear();
78 return log_debug("Can't create timestamp file %s, file system is read-only.", path
);
80 return log_error_errno(errno
, "Failed to create timestamp file %s: %m", path
);
83 (void) loop_write(fd
, MESSAGE
, strlen(MESSAGE
), false);
85 if (futimens(fd
, twice
) < 0)
86 return log_error_errno(errno
, "Failed to update timestamp on %s: %m", path
);
88 log_error_errno(errno
, "Failed to stat() timestamp file %s: %m", path
);
93 int main(int argc
, char *argv
[]) {
97 log_set_target(LOG_TARGET_AUTO
);
98 log_parse_environment();
101 if (stat("/usr", &st
) < 0) {
102 log_error_errno(errno
, "Failed to stat /usr: %m");
106 r
= mac_selinux_init(NULL
);
108 log_error_errno(r
, "SELinux setup failed: %m");
112 r
= apply_timestamp("/etc/.updated", &st
.st_mtim
);
113 q
= apply_timestamp("/var/.updated", &st
.st_mtim
);
116 return r
< 0 || q
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;