chiark / gitweb /
remove unused includes
[elogind.git] / src / fsck / fsck.c
index 9ecba99bb1a7b4c28f11d21658412945163709ab..cf4d37571728324415e711a7db6aa20d49a0f380 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <stdio.h>
 #include <stdbool.h>
-#include <string.h>
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -37,7 +36,6 @@
 #include "bus-util.h"
 #include "bus-error.h"
 #include "bus-common-errors.h"
-#include "fileio.h"
 #include "udev-util.h"
 #include "path-util.h"
 #include "socket-util.h"
@@ -132,7 +130,7 @@ static void test_files(void) {
 
 }
 
-static int process_progress(int fd, dev_t device_num) {
+static int process_progress(int fd, pid_t fsck_pid, dev_t device_num) {
         _cleanup_fclose_ FILE *f = NULL;
         usec_t last = 0;
         _cleanup_close_ int fsckd_fd = -1;
@@ -153,11 +151,13 @@ static int process_progress(int fd, dev_t device_num) {
 
         while (!feof(f)) {
                 int pass;
+                size_t buflen;
                 size_t cur, max;
-                ssize_t n;
+                ssize_t r;
                 usec_t t;
                 _cleanup_free_ char *device = NULL;
                 FsckProgress progress;
+                FsckdMessage fsckd_message;
 
                 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4)
                         break;
@@ -175,9 +175,19 @@ static int process_progress(int fd, dev_t device_num) {
                 progress.max = max;
                 progress.pass = pass;
 
-                n = send(fsckd_fd, &progress, sizeof(FsckProgress), 0);
-                if (n < 0 || (size_t) n < sizeof(FsckProgress))
+                r = send(fsckd_fd, &progress, sizeof(FsckProgress), 0);
+                if (r < 0 || (size_t) r < sizeof(FsckProgress))
                         log_warning_errno(errno, "Cannot communicate fsck progress to fsckd: %m");
+
+                /* get fsckd requests, only read when we have coherent size data */
+                r = ioctl(fsckd_fd, FIONREAD, &buflen);
+                if (r == 0 && (size_t) buflen >= sizeof(FsckdMessage)) {
+                        r = recv(fsckd_fd, &fsckd_message, sizeof(FsckdMessage), 0);
+                        if (r > 0 && fsckd_message.cancel == 1) {
+                                log_info("Request to cancel fsck from fsckd");
+                                kill(fsck_pid, SIGTERM);
+                        }
+                }
         }
 
         return 0;
@@ -187,6 +197,7 @@ int main(int argc, char *argv[]) {
         const char *cmdline[9];
         int i = 0, r = EXIT_FAILURE, q;
         pid_t pid;
+        int progress_rc;
         siginfo_t status;
         _cleanup_udev_unref_ struct udev *udev = NULL;
         _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
@@ -328,7 +339,7 @@ int main(int argc, char *argv[]) {
         progress_pipe[1] = safe_close(progress_pipe[1]);
 
         if (progress_pipe[0] >= 0) {
-                process_progress(progress_pipe[0], st.st_rdev);
+                progress_rc = process_progress(progress_pipe[0], pid, st.st_rdev);
                 progress_pipe[0] = -1;
         }
 
@@ -338,13 +349,14 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
+        if (status.si_code != CLD_EXITED || (status.si_status & ~1) || progress_rc != 0) {
 
-                if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
+                /* cancel will kill fsck (but process_progress returns 0) */
+                if ((progress_rc != 0 && status.si_code == CLD_KILLED) || status.si_code == CLD_DUMPED)
                         log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
                 else if (status.si_code == CLD_EXITED)
                         log_error("fsck failed with error code %i.", status.si_status);
-                else
+                else if (progress_rc != 0)
                         log_error("fsck failed due to unknown reason.");
 
                 if (status.si_code == CLD_EXITED && (status.si_status & 2) && root_directory)
@@ -355,7 +367,8 @@ int main(int argc, char *argv[]) {
                         start_target(SPECIAL_EMERGENCY_TARGET);
                 else {
                         r = EXIT_SUCCESS;
-                        log_warning("Ignoring error.");
+                        if (progress_rc != 0)
+                                log_warning("Ignoring error.");
                 }
 
         } else