As we're running each plugin in a fork, we need to call our cleanup code so we don't leak memory. To this extent, we now have the global boolean rc_in_plugin and the cleanup code can act accordingly.

This commit is contained in:
Roy Marples 2007-07-04 16:02:01 +00:00
parent 46156cc8cd
commit f28763d492
4 changed files with 27 additions and 9 deletions

View File

@ -19,6 +19,8 @@
#include "rc-plugin.h" #include "rc-plugin.h"
#include "strlist.h" #include "strlist.h"
bool rc_in_plugin = false;
typedef struct plugin typedef struct plugin
{ {
char *name; char *name;
@ -49,6 +51,10 @@ void rc_plugin_load (void)
int i; int i;
plugin_t *plugin = plugins; plugin_t *plugin = plugins;
/* Don't load plugins if we're in one */
if (rc_in_plugin)
return;
/* Ensure some sanity here */ /* Ensure some sanity here */
rc_plugin_unload (); rc_plugin_unload ();
@ -103,6 +109,10 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
{ {
plugin_t *plugin = plugins; plugin_t *plugin = plugins;
/* Don't run plugins if we're in one */
if (rc_in_plugin)
return;
while (plugin) { while (plugin) {
if (plugin->hook) { if (plugin->hook) {
int i; int i;
@ -135,12 +145,13 @@ void rc_plugin_run (rc_hook_t hook, const char *value)
if (pid == 0) { if (pid == 0) {
int retval; int retval;
rc_in_plugin = true;
close (pfd[0]); close (pfd[0]);
rc_environ_fd = fdopen (pfd[1], "w"); rc_environ_fd = fdopen (pfd[1], "w");
retval = plugin->hook (hook, value); retval = plugin->hook (hook, value);
fclose (rc_environ_fd); fclose (rc_environ_fd);
rc_environ_fd = NULL; rc_environ_fd = NULL;
_exit (retval); exit (retval);
} else { } else {
char buffer[RC_LINEBUFFER]; char buffer[RC_LINEBUFFER];
char *token; char *token;

View File

@ -8,6 +8,10 @@
#ifndef __LIBRC_PLUGIN_H__ #ifndef __LIBRC_PLUGIN_H__
#define __LIBRC_PLUGIN_H__ #define __LIBRC_PLUGIN_H__
/* A simple flag to say if we're in a plugin proccess or not.
* Mainly used in atexit code. */
extern bool rc_in_plugin;
void rc_plugin_load (); void rc_plugin_load ();
void rc_plugin_unload (); void rc_plugin_unload ();
void rc_plugin_run (rc_hook_t, const char *value); void rc_plugin_run (rc_hook_t, const char *value);

View File

@ -83,7 +83,7 @@ static void cleanup (void)
rc_plugin_unload (); rc_plugin_unload ();
if (termios_orig) { if (! rc_in_plugin && termios_orig) {
tcsetattr (STDIN_FILENO, TCSANOW, termios_orig); tcsetattr (STDIN_FILENO, TCSANOW, termios_orig);
free (termios_orig); free (termios_orig);
} }
@ -103,10 +103,12 @@ static void cleanup (void)
rc_strlist_free (types); rc_strlist_free (types);
/* Clean runlevel start, stop markers */ /* Clean runlevel start, stop markers */
if (rc_is_dir (RC_SVCDIR "softscripts.new")) if (! rc_in_plugin) {
rc_rm_dir (RC_SVCDIR "softscripts.new", true); if (rc_is_dir (RC_SVCDIR "softscripts.new"))
if (rc_is_dir (RC_SVCDIR "softscripts.old")) rc_rm_dir (RC_SVCDIR "softscripts.new", true);
rc_rm_dir (RC_SVCDIR "softscripts.old", true); if (rc_is_dir (RC_SVCDIR "softscripts.old"))
rc_rm_dir (RC_SVCDIR "softscripts.old", true);
}
free (applet); free (applet);
} }

View File

@ -234,7 +234,7 @@ static void start_services (char **list) {
static void cleanup (void) static void cleanup (void)
{ {
if (prefix_locked) if (! rc_in_plugin && prefix_locked)
unlink (PREFIX_LOCK); unlink (PREFIX_LOCK);
/* Flush our buffered output if any */ /* Flush our buffered output if any */
@ -259,7 +259,7 @@ static void cleanup (void)
rc_strlist_free (tmplist); rc_strlist_free (tmplist);
free (ibsave); free (ibsave);
if (in_control ()) { if (! rc_in_plugin && in_control ()) {
if (rc_service_state (applet, rc_service_stopping)) { if (rc_service_state (applet, rc_service_stopping)) {
/* If the we're shutting down, do it cleanly */ /* If the we're shutting down, do it cleanly */
if ((softlevel && if ((softlevel &&
@ -287,7 +287,8 @@ static void cleanup (void)
if (mtime_test) if (mtime_test)
{ {
unlink (mtime_test); if (! rc_in_plugin)
unlink (mtime_test);
free (mtime_test); free (mtime_test);
} }
free (exclusive); free (exclusive);