Semi-nack based on my function formatting "clarifications" :-)
On Tue, Apr 26, 2011 at 2:29 PM, Russell Bryant
<russell(a)russellbryant.net> wrote:
Make this code conform to coding guidelines so that it's more
consistent
with the rest of the code base.
Signed-off-by: Russell Bryant <russell(a)russellbryant.net>
---
src/lib/mainloop.c | 309 +++++++++++++++++++++++++---------------------------
1 files changed, 146 insertions(+), 163 deletions(-)
diff --git a/src/lib/mainloop.c b/src/lib/mainloop.c
index f62661d..ada63de 100644
--- a/src/lib/mainloop.c
+++ b/src/lib/mainloop.c
@@ -1,16 +1,16 @@
-/*
+/*
* Copyright (C) 2009 Andrew Beekhof <andrew(a)beekhof.net>
- *
+ *
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
- *
+ *
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -36,28 +36,25 @@
static GHashTable *mainloop_process_table = NULL;
-static gboolean
-mainloop_trigger_prepare(GSource* source, gint *timeout)
+static gboolean mainloop_trigger_prepare(GSource *source, gint *timeout)
{
- mainloop_trigger_t *trig = (mainloop_trigger_t*)source;
+ mainloop_trigger_t *trig = (mainloop_trigger_t *) source;
return trig->trigger;
}
-static gboolean
-mainloop_trigger_check(GSource* source)
+static gboolean mainloop_trigger_check(GSource *source)
{
- mainloop_trigger_t *trig = (mainloop_trigger_t*)source;
+ mainloop_trigger_t *trig = (mainloop_trigger_t *) source;
return trig->trigger;
}
-static gboolean
-mainloop_trigger_dispatch(GSource *source, GSourceFunc callback, gpointer userdata)
+static gboolean mainloop_trigger_dispatch(GSource *source, GSourceFunc callback,
gpointer userdata)
{
- mainloop_trigger_t *trig = (mainloop_trigger_t*)source;
+ mainloop_trigger_t *trig = (mainloop_trigger_t *) source;
trig->trigger = FALSE;
if(callback) {
- return callback(trig->user_data);
+ return callback(trig->user_data);
}
return TRUE;
}
@@ -69,21 +66,20 @@ static GSourceFuncs mainloop_trigger_funcs = {
NULL
};
-static mainloop_trigger_t *
-mainloop_setup_trigger(
- GSource *source, int priority, gboolean (*dispatch)(gpointer user_data), gpointer
userdata)
+static mainloop_trigger_t *mainloop_setup_trigger(GSource *source, int priority,
+ gboolean (*dispatch)(gpointer user_data), gpointer userdata)
{
mainloop_trigger_t *trigger = NULL;
- trigger = (mainloop_trigger_t*) source;
+ trigger = (mainloop_trigger_t *) source;
trigger->id = 0;
trigger->trigger = FALSE;
trigger->user_data = userdata;
if(dispatch) {
- g_source_set_callback(source, dispatch, trigger, NULL);
+ g_source_set_callback(source, dispatch, trigger, NULL);
}
-
+
g_source_set_priority(source, priority);
g_source_set_can_recurse(source, FALSE);
@@ -91,9 +87,8 @@ mainloop_setup_trigger(
return trigger;
}
-mainloop_trigger_t *
-mainloop_add_trigger(
- int priority, gboolean (*dispatch)(gpointer user_data), gpointer userdata)
+mainloop_trigger_t *mainloop_add_trigger(int priority,
+ gboolean (*dispatch)(gpointer user_data), gpointer userdata)
{
GSource *source = NULL;
@@ -104,51 +99,47 @@ mainloop_add_trigger(
return mainloop_setup_trigger(source, priority, dispatch, userdata);
}
-void
-mainloop_set_trigger(mainloop_trigger_t* source)
+void mainloop_set_trigger(mainloop_trigger_t *source)
{
source->trigger = TRUE;
}
-gboolean
-mainloop_destroy_trigger(mainloop_trigger_t* source)
+gboolean mainloop_destroy_trigger(mainloop_trigger_t *source)
{
source->trigger = FALSE;
if (source->id > 0) {
- g_source_remove(source->id);
+ g_source_remove(source->id);
}
return TRUE;
}
-typedef struct signal_s
-{
- mainloop_trigger_t trigger; /* must be first */
- void (*handler)(int sig);
- int signal;
-
+typedef struct signal_s {
+ mainloop_trigger_t trigger; /**< must be first */
+ void (*handler)(int sig);
+ int signal;
} mainloop_signal_t;
static mainloop_signal_t *mainloop_signals[NSIG];
-static gboolean
-mainloop_signal_dispatch(GSource *source, GSourceFunc callback, gpointer userdata)
+static gboolean mainloop_signal_dispatch(GSource *source, GSourceFunc callback,
+ gpointer userdata)
{
mainloop_signal_t *sig = (mainloop_signal_t*)source;
#if __linux__
mh_info("Invoking handler for signal %d: %s",
- sig->signal, strsignal(sig->signal));
+ sig->signal, strsignal(sig->signal));
#endif
sig->trigger.trigger = FALSE;
if(sig->handler) {
- sig->handler(sig->signal);
+ sig->handler(sig->signal);
}
return TRUE;
}
-static void mainloop_signal_handler(int sig)
+static void mainloop_signal_handler(int sig)
{
if(sig > 0 && sig < NSIG && mainloop_signals[sig] != NULL) {
- mainloop_set_trigger((mainloop_trigger_t*)mainloop_signals[sig]);
+ mainloop_set_trigger((mainloop_trigger_t*)mainloop_signals[sig]);
}
}
@@ -159,27 +150,27 @@ static GSourceFuncs mainloop_signal_funcs = {
NULL
};
-gboolean mainloop_signal(int sig, void (*dispatch)(int sig))
+gboolean mainloop_signal(int sig, void (*dispatch)(int sig))
{
#if __linux__
sigset_t mask;
struct sigaction sa;
- struct sigaction old;
+ struct sigaction old;
if(sigemptyset(&mask) < 0) {
- mh_perror(LOG_ERR, "Call to sigemptyset failed");
- return FALSE;
+ mh_perror(LOG_ERR, "Call to sigemptyset failed");
+ return FALSE;
}
-
+
sa.sa_handler = dispatch;
sa.sa_flags = SA_RESTART;
sa.sa_mask = mask;
-
+
if(sigaction(sig, &sa, &old) < 0) {
- mh_perror(LOG_ERR, "Could not install signal handler for signal %d",
sig);
- return FALSE;
+ mh_perror(LOG_ERR, "Could not install signal handler for signal %d",
sig);
+ return FALSE;
}
-
+
return TRUE;
#else
mh_perror(LOG_ERR, "Could not install signal handler for signal %d", sig);
@@ -187,65 +178,63 @@ gboolean mainloop_signal(int sig, void (*dispatch)(int sig))
#endif
}
-gboolean
-mainloop_add_signal(int sig, void (*dispatch)(int sig))
+gboolean mainloop_add_signal(int sig, void (*dispatch)(int sig))
{
GSource *source = NULL;
int priority = G_PRIORITY_HIGH - 1;
if(sig == SIGTERM) {
- /* TERM is higher priority than other signals,
- * signals are higher priority than other ipc.
- * Yes, minus: smaller is "higher"
- */
- priority--;
+ /* TERM is higher priority than other signals,
+ * signals are higher priority than other ipc.
+ * Yes, minus: smaller is "higher"
+ */
+ priority--;
}
-
+
if(sig >= NSIG || sig < 0) {
- mh_err("Signal %d is out of range", sig);
- return FALSE;
-
+ mh_err("Signal %d is out of range", sig);
+ return FALSE;
+
} else if(mainloop_signals[sig] != NULL) {
- mh_err("Signal handler for %d is already installed", sig);
- return FALSE;
+ mh_err("Signal handler for %d is already installed", sig);
+ return FALSE;
}
-
+
MH_ASSERT(sizeof(mainloop_signal_t) > sizeof(GSource));
source = g_source_new(&mainloop_signal_funcs, sizeof(mainloop_signal_t));
mainloop_signals[sig] = (mainloop_signal_t*)mainloop_setup_trigger(source, priority,
NULL, NULL);
if(mainloop_signals[sig] == NULL) {
- return FALSE;
+ return FALSE;
}
mainloop_signals[sig]->handler = dispatch;
mainloop_signals[sig]->signal = sig;
if(mainloop_signal(sig, mainloop_signal_handler) == FALSE) {
- mainloop_signal_t *tmp = mainloop_signals[sig];
- mainloop_signals[sig] = NULL;
+ mainloop_signal_t *tmp = mainloop_signals[sig];
+ mainloop_signals[sig] = NULL;
- mainloop_destroy_trigger((mainloop_trigger_t*)tmp);
- return FALSE;
+ mainloop_destroy_trigger((mainloop_trigger_t*)tmp);
+ return FALSE;
}
-
+
return TRUE;
}
-gboolean
-mainloop_destroy_signal(int sig)
+gboolean mainloop_destroy_signal(int sig)
{
mainloop_signal_t *tmp = NULL;
-
+
if(sig >= NSIG || sig < 0) {
- mh_err("Signal %d is out of range", sig);
- return FALSE;
+ mh_err("Signal %d is out of range", sig);
+ return FALSE;
} else if(mainloop_signal(sig, NULL) == FALSE) {
- mh_perror(LOG_ERR, "Could not uninstall signal handler for signal %d",
sig);
- return FALSE;
+ mh_perror(LOG_ERR, "Could not uninstall signal handler for signal %d",
sig);
+ return FALSE;
} else if(mainloop_signals[sig] == NULL) {
- return TRUE;
+ return TRUE;
}
tmp = mainloop_signals[sig];
@@ -255,54 +244,51 @@ mainloop_destroy_signal(int sig)
}
-static gboolean
-mainloop_fd_prepare(GSource* source, gint *timeout)
+static gboolean mainloop_fd_prepare(GSource *source, gint *timeout)
{
return FALSE;
}
-static gboolean
-mainloop_fd_check(GSource* source)
+static gboolean mainloop_fd_check(GSource* source)
{
- mainloop_fd_t *trig = (mainloop_fd_t*)source;
+ mainloop_fd_t *trig = (mainloop_fd_t *) source;
if (trig->gpoll.revents) {
- return TRUE;
+ return TRUE;
}
return FALSE;
}
-static gboolean
-mainloop_fd_dispatch(GSource *source, GSourceFunc callback, gpointer userdata)
+static gboolean mainloop_fd_dispatch(GSource *source, GSourceFunc callback, gpointer
userdata)
{
- mainloop_fd_t *trig = (mainloop_fd_t*)source;
+ mainloop_fd_t *trig = (mainloop_fd_t *) source;
mh_trace("%p", source);
/*
- * Is output now unblocked?
+ * Is output now unblocked?
*
* If so, turn off OUTPUT_EVENTS to avoid going into
* a tight poll(2) loop.
*/
if (trig->gpoll.revents & G_IO_OUT) {
- trig->gpoll.events &= ~G_IO_OUT;
+ trig->gpoll.events &= ~G_IO_OUT;
}
-
+
if (trig->dispatch != NULL
- && trig->dispatch(trig->gpoll.fd, trig->user_data) == FALSE) {
- g_source_remove_poll(source, &trig->gpoll);
- g_source_unref(source); /* Really? */
- return FALSE;
+ && trig->dispatch(trig->gpoll.fd, trig->user_data) == FALSE) {
+ g_source_remove_poll(source, &trig->gpoll);
+ g_source_unref(source); /* Really? */
+ return FALSE;
}
return TRUE;
}
static void
-mainloop_fd_destroy(GSource* source)
+mainloop_fd_destroy(GSource *source)
{
- mainloop_fd_t *trig = (mainloop_fd_t*)source;
+ mainloop_fd_t *trig = (mainloop_fd_t *) source;
mh_trace("%p", source);
if (trig->dnotify) {
- trig->dnotify(trig->user_data);
+ trig->dnotify(trig->user_data);
}
}
@@ -315,8 +301,8 @@ static GSourceFuncs mainloop_fd_funcs = {
mainloop_fd_t*
mainloop_add_fd(int priority, int fd,
- gboolean (*dispatch)(int fd, gpointer userdata),
- GDestroyNotify notify, gpointer userdata)
+ gboolean (*dispatch)(int fd, gpointer userdata),
+ GDestroyNotify notify, gpointer userdata)
{
GSource *source = NULL;
mainloop_fd_t *fd_source = NULL;
@@ -341,47 +327,47 @@ mainloop_add_fd(int priority, int fd,
g_source_set_priority(source, priority);
g_source_set_can_recurse(source, FALSE);
g_source_add_poll(source, &fd_source->gpoll);
-
+
fd_source->id = g_source_attach(source, NULL);
return fd_source;
}
gboolean
-mainloop_destroy_fd(mainloop_fd_t* source)
+mainloop_destroy_fd(mainloop_fd_t *source)
{
g_source_remove_poll((GSource*)source, &source->gpoll);
g_source_remove(source->id);
source->id = 0;
g_source_unref((GSource*)source);
-
+
return TRUE;
}
static gboolean child_timeout_callback(gpointer p)
{
- pid_t pid = (pid_t)GPOINTER_TO_INT(p);
- mainloop_child_t *pinfo = (mainloop_child_t
*)g_hash_table_lookup(mainloop_process_table, p);
+ pid_t pid = (pid_t) GPOINTER_TO_INT(p);
+ mainloop_child_t *pinfo = (mainloop_child_t *)
g_hash_table_lookup(mainloop_process_table, p);
if (pinfo == NULL) {
- return FALSE;
+ return FALSE;
}
pinfo->timerid = 0;
if (pinfo->timeout) {
- mh_crit("%s process (PID %d) will not die!", pinfo->desc,
(int)pid);
- return FALSE;
+ mh_crit("%s process (PID %d) will not die!", pinfo->desc,
(int)pid);
+ return FALSE;
}
-
+
pinfo->timeout = TRUE;
mh_warn("%s process (PID %d) timed out", pinfo->desc, (int)pid);
#if __linux__
if (kill(pinfo->pid, SIGKILL) < 0) {
- if (errno == ESRCH) {
- /* Nothing left to do */
- return FALSE;
- }
- mh_perror(LOG_ERR, "kill(%d, KILL) failed", pid);
+ if (errno == ESRCH) {
+ /* Nothing left to do */
+ return FALSE;
+ }
+ mh_perror(LOG_ERR, "kill(%d, KILL) failed", pid);
}
pinfo->timerid = g_timeout_add(5000, child_timeout_callback, p);
@@ -392,14 +378,13 @@ static gboolean child_timeout_callback(gpointer p)
/* Create/Log a new tracked process
* To track a process group, use -pid
*/
-void
-mainloop_add_child(pid_t pid, int timeout, const char *desc, void * privatedata,
- void (*callback)(mainloop_child_t* p, int status, int signo, int
exitcode))
+void mainloop_add_child(pid_t pid, int timeout, const char *desc, void * privatedata,
+ void (*callback)(mainloop_child_t *p, int status, int signo, int exitcode))
{
- mainloop_child_t* p = g_new(mainloop_child_t, 1);
+ mainloop_child_t* p = g_new(mainloop_child_t, 1);
if (mainloop_process_table == NULL) {
- mainloop_process_table = g_hash_table_new_full(
- g_direct_hash, g_direct_equal, NULL, NULL/*TODO: Add destructor */);
+ mainloop_process_table = g_hash_table_new_full(
+ g_direct_hash, g_direct_equal, NULL, NULL/*TODO: Add destructor */);
}
p->pid = pid;
@@ -408,66 +393,64 @@ mainloop_add_child(pid_t pid, int timeout, const char *desc, void *
privatedata,
p->desc = strdup(desc);
p->privatedata = privatedata;
p->callback = callback;
-
+
if(timeout) {
- p->timerid = g_timeout_add(
- timeout, child_timeout_callback, GINT_TO_POINTER(pid));
+ p->timerid = g_timeout_add(
+ timeout, child_timeout_callback, GINT_TO_POINTER(pid));
}
-
+
g_hash_table_insert(mainloop_process_table, GINT_TO_POINTER(abs(pid)), p);
}
#if __linux__
-static void
-child_death_dispatch(int sig)
+static void child_death_dispatch(int sig)
{
int status = 0;
while(TRUE) {
- pid_t pid = wait3(&status, WNOHANG, NULL);
- if (pid > 0) {
- int signo = 0, exitcode = 0;
-
- mainloop_child_t *p = g_hash_table_lookup(mainloop_process_table,
GINT_TO_POINTER(pid));
- mh_trace("Managed process %d exited: %p", pid, p);
- if (p == NULL) {
- continue;
- }
-
- if (WIFEXITED(status)) {
- exitcode = WEXITSTATUS(status);
- mh_trace("Managed process %d (%s) exited with rc=%d", pid,
p->desc, exitcode);
-
- } else if (WIFSIGNALED(status)) {
- signo = WTERMSIG(status);
- mh_trace("Managed process %d (%s) exited with signal=%d", pid,
p->desc, signo);
- }
+ pid_t pid = wait3(&status, WNOHANG, NULL);
+ if (pid > 0) {
+ int signo = 0, exitcode = 0;
+
+ mainloop_child_t *p = g_hash_table_lookup(mainloop_process_table,
GINT_TO_POINTER(pid));
+ mh_trace("Managed process %d exited: %p", pid, p);
+ if (p == NULL) {
+ continue;
+ }
+
+ if (WIFEXITED(status)) {
+ exitcode = WEXITSTATUS(status);
+ mh_trace("Managed process %d (%s) exited with rc=%d", pid,
p->desc, exitcode);
+
+ } else if (WIFSIGNALED(status)) {
+ signo = WTERMSIG(status);
+ mh_trace("Managed process %d (%s) exited with signal=%d", pid,
p->desc, signo);
+ }
#ifdef WCOREDUMP
- if (WCOREDUMP(status)) {
- mh_err("Managed process %d (%s) dumped core", pid,
p->desc);
- }
+ if (WCOREDUMP(status)) {
+ mh_err("Managed process %d (%s) dumped core", pid,
p->desc);
+ }
#endif
- if (p->timerid != 0) {
- mh_trace("Removing timer %d", p->timerid);
- g_source_remove(p->timerid);
- p->timerid = 0;
- }
- p->callback(p, status, signo, exitcode);
- g_hash_table_remove(mainloop_process_table, GINT_TO_POINTER(pid));
- mh_trace("Removed process entry for %d", pid);
- return;
-
- } else {
- if(errno != ECHILD) {
- mh_perror(LOG_ERR, "wait3() failed");
- }
- break;
- }
+ if (p->timerid != 0) {
+ mh_trace("Removing timer %d", p->timerid);
+ g_source_remove(p->timerid);
+ p->timerid = 0;
+ }
+ p->callback(p, status, signo, exitcode);
+ g_hash_table_remove(mainloop_process_table, GINT_TO_POINTER(pid));
+ mh_trace("Removed process entry for %d", pid);
+ return;
+
+ } else {
+ if(errno != ECHILD) {
+ mh_perror(LOG_ERR, "wait3() failed");
+ }
+ break;
+ }
}
}
#endif
-void
-mainloop_track_children(int priority)
+void mainloop_track_children(int priority)
{
#if __linux__
mainloop_add_signal(SIGCHLD, child_death_dispatch);
--
1.7.4.4
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari