]> git.proxmox.com Git - systemd.git/blob - src/update-done/update-done.c
Merge tag 'upstream/229'
[systemd.git] / src / update-done / update-done.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include "fd-util.h"
21 #include "io-util.h"
22 #include "selinux-util.h"
23 #include "util.h"
24
25 #define MESSAGE \
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"
29
30 static int apply_timestamp(const char *path, struct timespec *ts) {
31 struct timespec twice[2] = {
32 *ts,
33 *ts
34 };
35 struct stat st;
36
37 assert(path);
38 assert(ts);
39
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)
50 return 0;
51
52 /* It is older? Then let's update it */
53 if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
54
55 if (errno == EROFS)
56 return log_debug("Can't update timestamp file %s, file system is read-only.", path);
57
58 return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
59 }
60
61 } else if (errno == ENOENT) {
62 _cleanup_close_ int fd = -1;
63 int r;
64
65 /* The timestamp file doesn't exist yet? Then let's create it. */
66
67 r = mac_selinux_create_file_prepare(path, S_IFREG);
68 if (r < 0)
69 return log_error_errno(r, "Failed to set SELinux context for %s: %m", path);
70
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();
73
74 if (fd < 0) {
75 if (errno == EROFS)
76 return log_debug("Can't create timestamp file %s, file system is read-only.", path);
77
78 return log_error_errno(errno, "Failed to create timestamp file %s: %m", path);
79 }
80
81 (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
82
83 if (futimens(fd, twice) < 0)
84 return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
85 } else
86 log_error_errno(errno, "Failed to stat() timestamp file %s: %m", path);
87
88 return 0;
89 }
90
91 int main(int argc, char *argv[]) {
92 struct stat st;
93 int r, q = 0;
94
95 log_set_target(LOG_TARGET_AUTO);
96 log_parse_environment();
97 log_open();
98
99 if (stat("/usr", &st) < 0) {
100 log_error_errno(errno, "Failed to stat /usr: %m");
101 return EXIT_FAILURE;
102 }
103
104 r = mac_selinux_init(NULL);
105 if (r < 0) {
106 log_error_errno(r, "SELinux setup failed: %m");
107 goto finish;
108 }
109
110 r = apply_timestamp("/etc/.updated", &st.st_mtim);
111 q = apply_timestamp("/var/.updated", &st.st_mtim);
112
113 finish:
114 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
115 }