Wait for plugins to finish before moving on.

This commit is contained in:
Roy Marples 2007-10-22 19:33:42 +00:00
parent 6b0c28039d
commit 9dddb43eb4
4 changed files with 32 additions and 45 deletions

View File

@ -5,6 +5,8 @@
Released under the GPLv2
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
@ -99,6 +101,21 @@ void rc_plugin_load (void)
closedir (dp);
}
int rc_waitpid (pid_t pid)
{
int status = 0;
pid_t savedpid = pid;
int retval = -1;
errno = 0;
while ((pid = waitpid (savedpid, &status, 0)) > 0) {
if (pid == savedpid)
retval = WIFEXITED (status) ? WEXITSTATUS (status) : EXIT_FAILURE;
}
return (retval);
}
void rc_plugin_run (rc_hook_t hook, const char *value)
{
plugin_t *plugin = plugins;
@ -176,6 +193,8 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
free (buffer);
close (pfd[0]);
rc_waitpid (pid);
}
}
plugin = plugin->next;

View File

@ -12,6 +12,7 @@
* Mainly used in atexit code. */
extern bool rc_in_plugin;
int rc_waitpid (pid_t pid);
void rc_plugin_load ();
void rc_plugin_unload ();
void rc_plugin_run (rc_hook_t, const char *value);

View File

@ -629,21 +629,6 @@ static void remove_pid (pid_t pid)
}
}
static int wait_pid (pid_t pid)
{
int status = 0;
pid_t savedpid = pid;
int retval = -1;
errno = 0;
while ((pid = waitpid (savedpid, &status, 0)) > 0) {
if (pid == savedpid)
retval = WIFEXITED (status) ? WEXITSTATUS (status) : EXIT_FAILURE;
}
return (retval);
}
static void handle_signal (int sig)
{
int serrno = errno;
@ -1223,7 +1208,7 @@ int main (int argc, char **argv)
if (going_down) {
pid_t pid = rc_service_stop (service);
if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
wait_pid (pid);
rc_waitpid (pid);
continue;
}
@ -1289,7 +1274,7 @@ int main (int argc, char **argv)
if (! found) {
pid_t pid = rc_service_stop (service);
if (pid > 0 && ! rc_env_bool ("RC_PARALLEL"))
wait_pid (pid);
rc_waitpid (pid);
}
}
@ -1385,7 +1370,7 @@ interactive_option:
add_pid (pid);
if (! rc_env_bool ("RC_PARALLEL")) {
wait_pid (pid);
rc_waitpid (pid);
remove_pid (pid);
}
}

View File

@ -13,7 +13,6 @@
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
@ -132,9 +131,8 @@ static void handle_signal (int sig)
if (signal_pipe[1] > -1) {
if (write (signal_pipe[1], &sig, sizeof (sig)) == -1)
eerror ("%s: send: %s", service, strerror (errno));
} else {
wait (0);
}
} else
rc_waitpid (-1);
break;
case SIGWINCH:
@ -356,21 +354,6 @@ static int write_prefix (const char *buffer, size_t bytes, bool *prefixed) {
return (ret);
}
static int wait_pid (pid_t pid)
{
int status = 0;
pid_t savedpid = pid;
int retval = -1;
errno = 0;
while ((pid = waitpid (savedpid, &status, 0)) > 0) {
if (pid == savedpid)
retval = WIFEXITED (status) ? WEXITSTATUS (status) : EXIT_FAILURE;
}
return (retval);
}
static bool svc_exec (const char *arg1, const char *arg2)
{
bool execok;
@ -419,7 +402,6 @@ static bool svc_exec (const char *arg1, const char *arg2)
* good for us */
close (master_tty);
dup2 (fileno (stdin), 0);
dup2 (slave_tty, 1);
dup2 (slave_tty, 2);
if (slave_tty > 2)
@ -457,14 +439,14 @@ static bool svc_exec (const char *arg1, const char *arg2)
}
if (s > 0) {
/* Only SIGCHLD signals come down this pipe */
if (FD_ISSET (signal_pipe[0], &rset))
break;
if (master_tty >= 0 && FD_ISSET (master_tty, &rset)) {
bytes = read (master_tty, buffer, RC_LINEBUFFER);
write_prefix (buffer, bytes, &prefixed);
}
/* Only SIGCHLD signals come down this pipe */
if (FD_ISSET (signal_pipe[0], &rset))
break;
}
}
@ -479,7 +461,7 @@ static bool svc_exec (const char *arg1, const char *arg2)
master_tty = -1;
}
execok = wait_pid (service_pid) == 0 ? true : false;
execok = rc_waitpid (service_pid) == 0 ? true : false;
service_pid = 0;
return (execok);
@ -650,7 +632,7 @@ static void svc_start (bool deps)
if (rc_service_state (svc) & RC_SERVICE_STOPPED) {
pid_t pid = rc_service_start (svc);
if (! rc_env_bool ("RC_PARALLEL"))
wait_pid (pid);
rc_waitpid (pid);
}
}
@ -857,7 +839,7 @@ static void svc_stop (bool deps)
{
pid_t pid = rc_service_stop (svc);
if (! rc_env_bool ("RC_PARALLEL"))
wait_pid (pid);
rc_waitpid (pid);
rc_strlist_add (&tmplist, svc);
}
}