chiark / gitweb /
b199a68972f89e72f5f76834a0b8f0def52e538d
[elogind.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 "util.h"
23 #include "label.h"
24
25 static int apply_timestamp(const char *path, struct timespec *ts) {
26         struct timespec twice[2];
27         struct stat st;
28
29         assert(path);
30         assert(ts);
31
32         if (stat(path, &st) >= 0) {
33                 /* Is the timestamp file already newer than the OS? If so, there's nothing to do. */
34                 if (st.st_mtim.tv_sec > ts->tv_sec ||
35                     (st.st_mtim.tv_sec == ts->tv_sec && st.st_mtim.tv_nsec >= ts->tv_nsec))
36                         return 0;
37
38                 /* It is older? Then let's update it */
39                 twice[0] = *ts;
40                 twice[1] = *ts;
41
42                 if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
43
44                         if (errno == EROFS) {
45                                 log_debug("Can't update timestamp file %s, file system is read-only.", path);
46                                 return 0;
47                         }
48
49                         log_error("Failed to update timestamp on %s: %m", path);
50                         return -errno;
51                 }
52
53         } else if (errno == ENOENT) {
54                 _cleanup_close_ int fd = -1;
55                 int r;
56
57                 /* The timestamp file doesn't exist yet? Then let's create it. */
58
59                 r = label_context_set(path, S_IFREG);
60                 if (r < 0) {
61                         log_error("Failed to set SELinux context for %s: %s",
62                                   path, strerror(-r));
63                         return r;
64                 }
65
66                 fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
67                 label_context_clear();
68
69                 if (fd < 0) {
70
71                         if (errno == EROFS) {
72                                 log_debug("Can't create timestamp file %s, file system is read-only.", path);
73                                 return 0;
74                         }
75
76                         log_error("Failed to create timestamp file %s: %m", path);
77                         return -errno;
78                 }
79
80                 twice[0] = *ts;
81                 twice[1] = *ts;
82
83                 if (futimens(fd, twice) < 0) {
84                         log_error("Failed to update timestamp on %s: %m", path);
85                         return -errno;
86                 }
87         } else {
88                 log_error("Failed to stat() timestamp file %s: %m", path);
89                 return -errno;
90         }
91
92         return 0;
93 }
94
95 int main(int argc, char *argv[]) {
96         struct stat st;
97         int r, q = 0;
98
99         log_set_target(LOG_TARGET_AUTO);
100         log_parse_environment();
101         log_open();
102
103         if (stat("/usr", &st) < 0) {
104                 log_error("Failed to stat /usr: %m");
105                 return EXIT_FAILURE;
106         }
107
108         r = label_init(NULL);
109         if (r < 0) {
110                 log_error("SELinux setup failed: %s", strerror(-r));
111                 goto finish;
112         }
113
114         r = apply_timestamp("/etc/.updated", &st.st_mtim);
115         q = apply_timestamp("/var/.updated", &st.st_mtim);
116
117 finish:
118         return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
119 }