+ pid_t pid = spawn(argv);
+ if (pid < 0)
+ bb_simple_perror_msg_and_die(*argv);
+ return pid;
+}
+
+int safe_waitpid(int pid, int *wstat, int options)
+{
+ int r;
+
+ do
+ r = waitpid(pid, wstat, options);
+ while ((r == -1) && (errno == EINTR));
+ return r;
+}
+
+int wait_any_nohang(int *wstat)
+{
+ return safe_waitpid(-1, wstat, WNOHANG);
+}
+
+// Wait for the specified child PID to exit, returning child's error return.
+int wait4pid(int pid)
+{
+ int status;
+
+ if (pid <= 0) {
+ /*errno = ECHILD; -- wrong. */
+ /* we expect errno to be already set from failed [v]fork/exec */
+ return -1;
+ }
+ if (safe_waitpid(pid, &status, 0) == -1)
+ return -1;
+ if (WIFEXITED(status))
+ return WEXITSTATUS(status);
+ if (WIFSIGNALED(status))
+ return WTERMSIG(status) + 1000;
+ return 0;
+}
+
+#if ENABLE_FEATURE_PREFER_APPLETS
+void save_nofork_data(struct nofork_save_area *save)
+{
+ memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
+ save->applet_name = applet_name;
+ save->xfunc_error_retval = xfunc_error_retval;
+ save->option_mask32 = option_mask32;
+ save->die_sleep = die_sleep;
+ save->saved = 1;
+}
+
+void restore_nofork_data(struct nofork_save_area *save)
+{
+ memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp));
+ applet_name = save->applet_name;
+ xfunc_error_retval = save->xfunc_error_retval;
+ option_mask32 = save->option_mask32;
+ die_sleep = save->die_sleep;
+}
+
+int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **argv)
+{
+ int rc, argc;
+
+ applet_name = APPLET_NAME(applet_no);
+ xfunc_error_retval = EXIT_FAILURE;
+
+ /* Special flag for xfunc_die(). If xfunc will "die"
+ * in NOFORK applet, xfunc_die() sees negative
+ * die_sleep and longjmp here instead. */
+ die_sleep = -1;