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