chiark / gitweb /
treewide: auto-convert the simple cases to log_*_errno()
[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 #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
30 static 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;
60                 int r;
61
62                 /* The timestamp file doesn't exist yet? Then let's create it. */
63
64                 r = mac_selinux_create_file_prepare(path, S_IFREG);
65                 if (r < 0) {
66                         log_error("Failed to set SELinux context for %s: %s",
67                                   path, strerror(-r));
68                         return r;
69                 }
70
71                 fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
72                 mac_selinux_create_file_clear();
73
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
85                 (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
86
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
102 int main(int argc, char *argv[]) {
103         struct stat st;
104         int r, q = 0;
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
115         r = mac_selinux_init(NULL);
116         if (r < 0) {
117                 log_error_errno(-r, "SELinux setup failed: %m");
118                 goto finish;
119         }
120
121         r = apply_timestamp("/etc/.updated", &st.st_mtim);
122         q = apply_timestamp("/var/.updated", &st.st_mtim);
123
124 finish:
125         return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
126 }