Просмотр исходного кода

Userland+MenuApplets: Replace two more fork/exec with posix_spawn

Nico Weber 5 лет назад
Родитель
Сommit
9cc32d6e95
2 измененных файлов с 26 добавлено и 36 удалено
  1. 5 8
      MenuApplets/Clock/main.cpp
  2. 21 28
      Userland/watch.cpp

+ 5 - 8
MenuApplets/Clock/main.cpp

@@ -31,6 +31,7 @@
 #include <LibGUI/Window.h>
 #include <LibGUI/Window.h>
 #include <LibGfx/Font.h>
 #include <LibGfx/Font.h>
 #include <LibGfx/Palette.h>
 #include <LibGfx/Palette.h>
+#include <spawn.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <time.h>
 #include <time.h>
 
 
@@ -84,14 +85,10 @@ private:
         if (event.button() != GUI::MouseButton::Left)
         if (event.button() != GUI::MouseButton::Left)
             return;
             return;
 
 
-        pid_t pid = fork();
-        if (pid < 0) {
-            perror("fork");
-        } else if (pid == 0) {
-            execl("/bin/Calendar", "Calendar", nullptr);
-            perror("execl");
-            ASSERT_NOT_REACHED();
-        }
+        pid_t pid;
+        const char* argv[] = { "Calendar", nullptr };
+        if ((errno = posix_spawn(&pid, "/bin/Calendar", nullptr, nullptr, const_cast<char**>(argv), environ)))
+            perror("posix_spawn");
     }
     }
 
 
     void tick_clock()
     void tick_clock()

+ 21 - 28
Userland/watch.cpp

@@ -29,6 +29,7 @@
 #include <AK/Time.h>
 #include <AK/Time.h>
 #include <AK/Vector.h>
 #include <AK/Vector.h>
 #include <LibCore/ArgsParser.h>
 #include <LibCore/ArgsParser.h>
+#include <spawn.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <sys/time.h>
 #include <sys/time.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
@@ -85,37 +86,29 @@ void handle_signal(int signal)
 
 
 int run_command(const Vector<const char*>& command)
 int run_command(const Vector<const char*>& command)
 {
 {
-    child_pid = fork();
-    if (child_pid < 0) {
-        // We failed to fork, so we shall print an error message and update the exit code.
+    if ((errno = posix_spawnp(const_cast<pid_t*>(&child_pid), command[0], nullptr, nullptr, const_cast<char**>(command.data()), environ))) {
         exit_code = 1;
         exit_code = 1;
-        perror("fork");
+        perror("posix_spawn");
         return errno;
         return errno;
-    } else if (child_pid == 0) {
-        // We are in the child process, so we should run the command.
-        execvp(command[0], const_cast<char* const*>(command.data()));
-        perror("exec");
-        exit(1);
-    } else {
-        // We are still in the parent process, so we shall wait for the child to terminate,
-        // then return its exit code.
-        int status;
-        pid_t exited_pid;
-        do {
-            exited_pid = waitpid(child_pid, &status, 0);
-        } while (exited_pid < 0 && errno == EINTR);
-        ASSERT(exited_pid == child_pid);
-        child_pid = -1;
-        if (exited_pid < 0) {
-            perror("waitpid");
-            return 1;
-        }
-        if (WIFEXITED(status)) {
-            return WEXITSTATUS(status);
-        } else {
-            return 1;
-        }
     }
     }
+
+   // Wait for the child to terminate, then return its exit code.
+   int status;
+   pid_t exited_pid;
+   do {
+       exited_pid = waitpid(child_pid, &status, 0);
+   } while (exited_pid < 0 && errno == EINTR);
+   ASSERT(exited_pid == child_pid);
+   child_pid = -1;
+   if (exited_pid < 0) {
+       perror("waitpid");
+       return 1;
+   }
+   if (WIFEXITED(status)) {
+       return WEXITSTATUS(status);
+   } else {
+       return 1;
+   }
 }
 }
 
 
 int main(int argc, char** argv)
 int main(int argc, char** argv)