Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : : 3 : : #include <sys/stat.h> 4 : : #include <sys/types.h> 5 : : #include <unistd.h> 6 : : 7 : : #include "alloc-util.h" 8 : : #include "fileio-label.h" 9 : : #include "selinux-util.h" 10 : : #include "time-util.h" 11 : : 12 : : #define MESSAGE \ 13 : : "# This file was created by systemd-update-done. Its only \n" \ 14 : : "# purpose is to hold a timestamp of the time this directory\n" \ 15 : : "# was updated. See man:systemd-update-done.service(8).\n" 16 : : 17 : 0 : static int apply_timestamp(const char *path, struct timespec *ts) { 18 : 0 : _cleanup_free_ char *message = NULL; 19 : : int r; 20 : : 21 : : /* 22 : : * We store the timestamp both as mtime of the file and in the file itself, 23 : : * to support filesystems which cannot store nanosecond-precision timestamps. 24 : : */ 25 : : 26 [ # # ]: 0 : if (asprintf(&message, 27 : : MESSAGE 28 : : "TIMESTAMP_NSEC=" NSEC_FMT "\n", 29 : : timespec_load_nsec(ts)) < 0) 30 : 0 : return log_oom(); 31 : : 32 : 0 : r = write_string_file_atomic_label_ts(path, message, ts); 33 [ # # ]: 0 : if (r == -EROFS) 34 [ # # ]: 0 : return log_debug("Cannot create \"%s\", file system is read-only.", path); 35 [ # # ]: 0 : if (r < 0) 36 [ # # ]: 0 : return log_error_errno(r, "Failed to write \"%s\": %m", path); 37 : 0 : return 0; 38 : : } 39 : : 40 : 0 : int main(int argc, char *argv[]) { 41 : : struct stat st; 42 : 0 : int r, q = 0; 43 : : 44 : 0 : log_setup_service(); 45 : : 46 [ # # ]: 0 : if (stat("/usr", &st) < 0) { 47 [ # # ]: 0 : log_error_errno(errno, "Failed to stat /usr: %m"); 48 : 0 : return EXIT_FAILURE; 49 : : } 50 : : 51 : 0 : r = mac_selinux_init(); 52 [ # # ]: 0 : if (r < 0) { 53 [ # # ]: 0 : log_error_errno(r, "SELinux setup failed: %m"); 54 : 0 : return EXIT_FAILURE; 55 : : } 56 : : 57 : 0 : r = apply_timestamp("/etc/.updated", &st.st_mtim); 58 : 0 : q = apply_timestamp("/var/.updated", &st.st_mtim); 59 : : 60 [ # # # # ]: 0 : return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS; 61 : : }