update-done.c revision d7b8eec7dc7fe307d3a08b32cf1a9ad4276ce6d5
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering This file is part of systemd.
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering Copyright 2014 Lennart Poettering
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering systemd is free software; you can redistribute it and/or modify it
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering under the terms of the GNU Lesser General Public License as published by
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering (at your option) any later version.
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering systemd is distributed in the hope that it will be useful, but
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering Lesser General Public License for more details.
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering You should have received a copy of the GNU Lesser General Public License
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
4aa4d2ae9717d0f8656528a3197bbc0c256380b1Zbigniew Jędrzejewski-Szmek "This file was created by systemd-update-done. Its only \n" \
4aa4d2ae9717d0f8656528a3197bbc0c256380b1Zbigniew Jędrzejewski-Szmek "purpose is to hold a timestamp of the time this directory\n" \
4aa4d2ae9717d0f8656528a3197bbc0c256380b1Zbigniew Jędrzejewski-Szmek "was updated. See systemd-update-done.service(8).\n"
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poetteringstatic int apply_timestamp(const char *path, struct timespec *ts) {
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering /* Is the timestamp file already newer than the OS? If so, there's nothing to do. */
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering (st.st_mtim.tv_sec == ts->tv_sec && st.st_mtim.tv_nsec >= ts->tv_nsec))
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering /* It is older? Then let's update it */
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_debug("Can't update timestamp file %s, file system is read-only.", path);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering /* The timestamp file doesn't exist yet? Then let's create it. */
ecabcf8b6edcc856ec2fd5bd43fc675a8fe04731Lennart Poettering r = mac_selinux_create_file_prepare(path, S_IFREG);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_error_errno(r, "Failed to set SELinux context for %s: %m", path);
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_debug("Can't create timestamp file %s, file system is read-only.", path);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_error_errno(errno, "Failed to create timestamp file %s: %m", path);
4aa4d2ae9717d0f8656528a3197bbc0c256380b1Zbigniew Jędrzejewski-Szmek (void) loop_write(fd, MESSAGE, strlen(MESSAGE), false);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering return log_error_errno(errno, "Failed to update timestamp on %s: %m", path);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering log_error_errno(errno, "Failed to stat() timestamp file %s: %m", path);
755bde375f4db393ad06e73340bfcf4d0cf91bb2Lennart Poettering log_error_errno(errno, "Failed to stat /usr: %m");
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "SELinux setup failed: %m");
7dbb1d08f66cd44b1296be3ee8e3629b989e19a8Zbigniew Jędrzejewski-Szmek r = apply_timestamp("/etc/.updated", &st.st_mtim);
8ea48dfcd33e8db0c01bf8c57c3bbcfdc3c86d4bLennart Poettering q = apply_timestamp("/var/.updated", &st.st_mtim);
7dbb1d08f66cd44b1296be3ee8e3629b989e19a8Zbigniew Jędrzejewski-Szmek return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;