]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/update-done/update-done.c
treewide: no need to negate errno for log_*_errno()
[thirdparty/systemd.git] / src / update-done / update-done.c
CommitLineData
8ea48dfc
LP
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 "util.h"
7dbb1d08 23#include "label.h"
8ea48dfc 24
4aa4d2ae
ZJS
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
8ea48dfc
LP
30static int apply_timestamp(const char *path, struct timespec *ts) {
31 struct timespec twice[2];
32 struct stat st;
33
34 assert(path);
35 assert(ts);
36
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))
41 return 0;
42
43 /* It is older? Then let's update it */
44 twice[0] = *ts;
45 twice[1] = *ts;
46
47 if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
48
49 if (errno == EROFS) {
50 log_debug("Can't update timestamp file %s, file system is read-only.", path);
51 return 0;
52 }
53
54 log_error("Failed to update timestamp on %s: %m", path);
55 return -errno;
56 }
57
58 } else if (errno == ENOENT) {
59 _cleanup_close_ int fd = -1;
7dbb1d08 60 int r;
8ea48dfc
LP
61
62 /* The timestamp file doesn't exist yet? Then let's create it. */
63
ecabcf8b 64 r = mac_selinux_create_file_prepare(path, S_IFREG);
7dbb1d08
ZJS
65 if (r < 0) {
66 log_error("Failed to set SELinux context for %s: %s",
67 path, strerror(-r));
68 return r;
69 }
70
8ea48dfc 71 fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
ecabcf8b 72 mac_selinux_create_file_clear();
7dbb1d08 73
8ea48dfc
LP
74 if (fd < 0) {
75
76 if (errno == EROFS) {
77 log_debug("Can't create timestamp file %s, file system is read-only.", path);
78 return 0;
79 }
80
81 log_error("Failed to create timestamp file %s: %m", path);
82 return -errno;
83 }
84
4aa4d2ae
ZJS
85 (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
86
8ea48dfc
LP
87 twice[0] = *ts;
88 twice[1] = *ts;
89
90 if (futimens(fd, twice) < 0) {
91 log_error("Failed to update timestamp on %s: %m", path);
92 return -errno;
93 }
94 } else {
95 log_error("Failed to stat() timestamp file %s: %m", path);
96 return -errno;
97 }
98
99 return 0;
100}
101
102int main(int argc, char *argv[]) {
103 struct stat st;
7dbb1d08 104 int r, q = 0;
8ea48dfc
LP
105
106 log_set_target(LOG_TARGET_AUTO);
107 log_parse_environment();
108 log_open();
109
110 if (stat("/usr", &st) < 0) {
111 log_error("Failed to stat /usr: %m");
112 return EXIT_FAILURE;
113 }
114
cc56fafe 115 r = mac_selinux_init(NULL);
7dbb1d08 116 if (r < 0) {
da927ba9 117 log_error_errno(r, "SELinux setup failed: %m");
7dbb1d08
ZJS
118 goto finish;
119 }
8ea48dfc 120
7dbb1d08 121 r = apply_timestamp("/etc/.updated", &st.st_mtim);
8ea48dfc 122 q = apply_timestamp("/var/.updated", &st.st_mtim);
8ea48dfc 123
7dbb1d08
ZJS
124finish:
125 return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
8ea48dfc 126}