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