summaryrefslogtreecommitdiff
path: root/glib/tests/unix.c
blob: ffcc6a11fcf42de6d67c17c9baa50262c1c76e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* 
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 *
 * Author: Colin Walters <walters@verbum.org> 
 */

#include "config.h"

#include "glib-unix.h"
#include <string.h>

static void
test_pipe (void)
{
  GError *error = NULL;
  int pipefd[2];
  char buf[1024];
  ssize_t bytes_read;
  gboolean res;

  res = g_unix_open_pipe (pipefd, FD_CLOEXEC, &error);
  g_assert (res);
  g_assert_no_error (error);

  write (pipefd[1], "hello", sizeof ("hello"));
  memset (buf, 0, sizeof (buf));
  bytes_read = read (pipefd[0], buf, sizeof(buf) - 1);
  g_assert_cmpint (bytes_read, >, 0);
  buf[bytes_read] = '\0';

  close (pipefd[0]);
  close (pipefd[1]);

  g_assert (g_str_has_prefix (buf, "hello"));
}

static void
test_error (void)
{
  GError *error = NULL;
  gboolean res;

  res = g_unix_set_fd_nonblocking (123456, TRUE, &error);
  g_assert_cmpint (errno, ==, EBADF);
  g_assert (!res);
  g_assert_error (error, G_UNIX_ERROR, 0);
  g_clear_error (&error);
}

static gboolean sig_received = FALSE;

static gboolean
on_sig_received (gpointer user_data)
{
  GMainLoop *loop = user_data;
  g_main_loop_quit (loop);
  sig_received = TRUE;
  return G_SOURCE_REMOVE;
}

static gboolean
sig_not_received (gpointer data)
{
  GMainLoop *loop = data;
  (void) loop;
  g_error ("Timed out waiting for signal");
  return G_SOURCE_REMOVE;
}

static gboolean
exit_mainloop (gpointer data)
{
  GMainLoop *loop = data;
  g_main_loop_quit (loop);
  return G_SOURCE_REMOVE;
}

static void
test_signal (int signum)
{
  GMainLoop *mainloop;
  int id;

  mainloop = g_main_loop_new (NULL, FALSE);

  sig_received = FALSE;
  g_unix_signal_add (signum, on_sig_received, mainloop);
  kill (getpid (), signum);
  g_assert (!sig_received);
  id = g_timeout_add (5000, sig_not_received, mainloop);
  g_main_loop_run (mainloop);
  g_assert (sig_received);
  sig_received = FALSE;
  g_source_remove (id);

  /* Ensure we don't get double delivery */
  g_timeout_add (500, exit_mainloop, mainloop);
  g_main_loop_run (mainloop);
  g_assert (!sig_received);
  g_main_loop_unref (mainloop);

}

static void
test_sighup (void)
{
  test_signal (SIGHUP);
}

static void
test_sigterm (void)
{
  test_signal (SIGTERM);
}

static void
test_sighup_add_remove (void)
{
  GMainLoop *mainloop;
  guint id;

  mainloop = g_main_loop_new (NULL, FALSE);

  sig_received = FALSE;
  id = g_unix_signal_add (SIGHUP, on_sig_received, mainloop);
  g_source_remove (id);
  kill (getpid (), SIGHUP);
  g_assert (!sig_received);
  g_main_loop_unref (mainloop);

}

typedef struct {
  GPid regular_pid;
  GPid catchall_pid;
  GMainLoop *loop;
  gboolean regular_exited;
  gboolean catchall_exited;
} CatchAllData;

static void
on_catchall_child (GPid   pid,
                   gint   estatus,
                   gpointer user_data)
{
  CatchAllData *data = user_data;
  GError *local_error = NULL;
  GError **error = &local_error;

  g_assert (pid == data->catchall_pid);

  g_spawn_check_exit_status (estatus, error);
  g_assert_no_error (local_error);

  data->catchall_exited = TRUE;
  if (data->regular_exited)
    g_main_loop_quit (data->loop);
}

static void
on_regular_child (GPid  pid,
                  gint  estatus,
                  gpointer user_data)
{
  CatchAllData *data = user_data;
  GError *local_error = NULL;
  GError **error = &local_error;

  g_assert (pid == data->regular_pid);

  g_spawn_check_exit_status (estatus, error);
  g_assert_no_error (local_error);

  data->regular_exited = TRUE;
  if (data->catchall_exited)
    g_main_loop_quit (data->loop);
}

static void
spawn_with_raw_fork (gchar  **child_argv,
                     pid_t   *out_pid)
{
  pid_t pid;

  pid = fork ();
  g_assert (pid >= 0);
  if (pid == 0)
    {
      execv (child_argv[0], child_argv+1);
      g_assert_not_reached ();
    }
  else
    *out_pid = pid;
}

static void
test_catchall (void)
{
  GMainLoop *mainloop;
  CatchAllData data;
  GSource *source;
  GPid pid;
  GError *local_error = NULL;
  GError **error = &local_error;
  char *child_args[] = { "/bin/true", "/bin/true", NULL };

  memset (&data, 0, sizeof (data));
  mainloop = g_main_loop_new (NULL, FALSE);
  data.loop = mainloop;

  source = g_child_watch_source_new (-1);
  g_source_set_callback (source, (GSourceFunc)on_catchall_child, &data, NULL);
  g_source_attach (source, NULL);
  g_source_unref (source);

  g_spawn_async (NULL, (char**)child_args, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
                 NULL, NULL, &pid, error);
  g_child_watch_add (pid, on_regular_child, &data);
  data.regular_pid = pid;

  spawn_with_raw_fork ((char**)child_args, &pid);
  data.catchall_pid = pid;

  g_main_loop_run (mainloop);

  g_source_destroy (source);
  g_main_loop_unref (mainloop);

  g_assert (data.regular_exited && data.catchall_exited);
}

int
main (int   argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/glib-unix/pipe", test_pipe);
  g_test_add_func ("/glib-unix/error", test_error);
  g_test_add_func ("/glib-unix/sighup", test_sighup);
  g_test_add_func ("/glib-unix/sigterm", test_sigterm);
  g_test_add_func ("/glib-unix/sighup_again", test_sighup);
  g_test_add_func ("/glib-unix/sighup_add_remove", test_sighup_add_remove);
  g_test_add_func ("/glib-unix/catchall", test_catchall);

  return g_test_run();
}