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