2 * Copyright (C) 2011 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
21 * Author: Colin Walters <walters@verbum.org>
26 #include "glib-unix.h"
38 res = g_unix_open_pipe (pipefd, FD_CLOEXEC, &error);
40 g_assert_no_error (error);
42 write (pipefd[1], "hello", sizeof ("hello"));
43 memset (buf, 0, sizeof (buf));
44 bytes_read = read (pipefd[0], buf, sizeof(buf) - 1);
45 g_assert_cmpint (bytes_read, >, 0);
50 g_assert (g_str_has_prefix (buf, "hello"));
59 res = g_unix_set_fd_nonblocking (123456, TRUE, &error);
60 g_assert_cmpint (errno, ==, EBADF);
62 g_assert_error (error, G_UNIX_ERROR, 0);
63 g_clear_error (&error);
66 static gboolean sig_received = FALSE;
69 on_sig_received (gpointer user_data)
71 GMainLoop *loop = user_data;
72 g_main_loop_quit (loop);
74 return G_SOURCE_REMOVE;
78 sig_not_received (gpointer data)
80 GMainLoop *loop = data;
82 g_error ("Timed out waiting for signal");
83 return G_SOURCE_REMOVE;
87 exit_mainloop (gpointer data)
89 GMainLoop *loop = data;
90 g_main_loop_quit (loop);
91 return G_SOURCE_REMOVE;
95 test_signal (int signum)
99 mainloop = g_main_loop_new (NULL, FALSE);
101 sig_received = FALSE;
102 g_unix_signal_add (signum, on_sig_received, mainloop);
103 kill (getpid (), signum);
104 g_assert (!sig_received);
105 g_timeout_add (5000, sig_not_received, mainloop);
106 g_main_loop_run (mainloop);
107 g_assert (sig_received);
108 sig_received = FALSE;
110 /* Ensure we don't get double delivery */
111 g_timeout_add (500, exit_mainloop, mainloop);
112 g_main_loop_run (mainloop);
113 g_assert (!sig_received);
114 g_main_loop_unref (mainloop);
121 test_signal (SIGHUP);
127 test_signal (SIGTERM);
131 test_sighup_add_remove (void)
136 mainloop = g_main_loop_new (NULL, FALSE);
138 sig_received = FALSE;
139 id = g_unix_signal_add (SIGHUP, on_sig_received, mainloop);
140 g_source_remove (id);
141 kill (getpid (), SIGHUP);
142 g_assert (!sig_received);
143 g_main_loop_unref (mainloop);
151 g_test_init (&argc, &argv, NULL);
153 g_test_add_func ("/glib-unix/pipe", test_pipe);
154 g_test_add_func ("/glib-unix/error", test_error);
155 g_test_add_func ("/glib-unix/sighup", test_sighup);
156 g_test_add_func ("/glib-unix/sigterm", test_sigterm);
157 g_test_add_func ("/glib-unix/sighup_again", test_sighup);
158 g_test_add_func ("/glib-unix/sighup_add_remove", test_sighup_add_remove);