mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-10-05 21:29:20 +02:00
glib-unix: New API to watch some Unix signals
This new API allows watching a few select Unix signals; looking through the list on my system, I didn't see anything else that I think it'd reasonable to watch. We build on the previous patch to make the child watch helper thread that existed on Unix handle these signals in the threaded case. In the non-threaded case, they're just global variables. https://bugzilla.gnome.org/show_bug.cgi?id=644941
This commit is contained in:
@@ -171,7 +171,11 @@ sort_LDADD = $(progs_ldadd)
|
||||
if OS_UNIX
|
||||
|
||||
TEST_PROGS += unix
|
||||
unix_LDADD = $(progs_ldadd)
|
||||
unix_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
||||
|
||||
TEST_PROGS += unix-nothreads
|
||||
unix_nothreads_SOURCES = unix.c
|
||||
unix_nothreads_LDADD = $(progs_ldadd)
|
||||
|
||||
# some testing of gtester funcitonality
|
||||
XMLLINT=xmllint
|
||||
|
@@ -48,13 +48,94 @@ test_pipe (void)
|
||||
g_assert (g_str_has_prefix (buf, "hello"));
|
||||
}
|
||||
|
||||
static gboolean sighup_received = FALSE;
|
||||
|
||||
static gboolean
|
||||
on_sighup_received (gpointer user_data)
|
||||
{
|
||||
GMainLoop *loop = user_data;
|
||||
g_main_loop_quit (loop);
|
||||
sighup_received = TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
sighup_not_received (gpointer data)
|
||||
{
|
||||
GMainLoop *loop = data;
|
||||
(void) loop;
|
||||
g_error ("Timed out waiting for SIGHUP");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
exit_mainloop (gpointer data)
|
||||
{
|
||||
GMainLoop *loop = data;
|
||||
g_main_loop_quit (loop);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
test_sighup (void)
|
||||
{
|
||||
GMainLoop *mainloop;
|
||||
|
||||
mainloop = g_main_loop_new (NULL, FALSE);
|
||||
|
||||
sighup_received = FALSE;
|
||||
g_unix_signal_add_watch_full (SIGHUP,
|
||||
G_PRIORITY_DEFAULT,
|
||||
on_sighup_received,
|
||||
mainloop,
|
||||
NULL);
|
||||
kill (getpid (), SIGHUP);
|
||||
g_assert (!sighup_received);
|
||||
g_timeout_add (5000, sighup_not_received, mainloop);
|
||||
g_main_loop_run (mainloop);
|
||||
g_assert (sighup_received);
|
||||
sighup_received = FALSE;
|
||||
|
||||
/* Ensure we don't get double delivery */
|
||||
g_timeout_add (500, exit_mainloop, mainloop);
|
||||
g_main_loop_run (mainloop);
|
||||
g_assert (!sighup_received);
|
||||
}
|
||||
|
||||
static void
|
||||
test_sighup_add_remove (void)
|
||||
{
|
||||
GMainLoop *mainloop;
|
||||
guint id;
|
||||
|
||||
mainloop = g_main_loop_new (NULL, FALSE);
|
||||
|
||||
sighup_received = FALSE;
|
||||
id = g_unix_signal_add_watch_full (SIGHUP,
|
||||
G_PRIORITY_DEFAULT,
|
||||
on_sighup_received,
|
||||
mainloop,
|
||||
NULL);
|
||||
g_source_remove (id);
|
||||
kill (getpid (), SIGHUP);
|
||||
g_assert (!sighup_received);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
#ifdef TEST_THREADED
|
||||
g_thread_init (NULL);
|
||||
#endif
|
||||
|
||||
g_test_add_func ("/glib-unix/pipe", test_pipe);
|
||||
g_test_add_func ("/glib-unix/sighup", test_sighup);
|
||||
g_test_add_func ("/glib-unix/sighup_again", test_sighup);
|
||||
g_test_add_func ("/glib-unix/sighup_add_remove", test_sighup_add_remove);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
|
Reference in New Issue
Block a user