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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 2000 Tor Lillqvist
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/* A test program for the main loop and IO channel code.
* Just run it. Optional parameter is number of sub-processes.
*/
/* We are using g_io_channel_read() which is deprecated */
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif
#include "config.h"
#include <glib.h>
#include <stdio.h>
#ifdef G_OS_WIN32
#include <io.h>
#include <fcntl.h>
#include <process.h>
#define STRICT
#include <windows.h>
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#endif
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
static int nrunning;
static GMainLoop *main_loop;
/* Larger than the circular buffer in giowin32.c on purpose */
#define BUFSIZE 5000
static int nkiddies;
static char *exec_name;
static struct {
int fd;
int seq;
} *seqtab;
static GIOError
read_all (int fd,
GIOChannel *channel,
char *buffer,
guint nbytes,
guint *bytes_read)
{
guint left = nbytes;
gsize nb;
GIOError error = G_IO_ERROR_NONE;
char *bufp = buffer;
/* g_io_channel_read() doesn't necessarily return all the
* data we want at once.
*/
*bytes_read = 0;
while (left)
{
error = g_io_channel_read (channel, bufp, left, &nb);
if (error != G_IO_ERROR_NONE)
{
g_test_message ("io-channel-basic: ...from %d: %d", fd, error);
if (error == G_IO_ERROR_AGAIN)
continue;
break;
}
if (nb == 0)
return error;
left -= nb;
bufp += nb;
*bytes_read += nb;
}
return error;
}
static void
shutdown_source (gpointer data)
{
guint *fd_ptr = data;
if (*fd_ptr != 0)
{
g_source_remove (*fd_ptr);
*fd_ptr = 0;
nrunning--;
if (nrunning == 0)
g_main_loop_quit (main_loop);
}
}
static gboolean
recv_message (GIOChannel *channel,
GIOCondition cond,
gpointer data)
{
gint fd = g_io_channel_unix_get_fd (channel);
gboolean retval = TRUE;
g_debug ("io-channel-basic: ...from %d:%s%s%s%s", fd,
(cond & G_IO_ERR) ? " ERR" : "",
(cond & G_IO_HUP) ? " HUP" : "",
(cond & G_IO_IN) ? " IN" : "",
(cond & G_IO_PRI) ? " PRI" : "");
if (cond & (G_IO_ERR | G_IO_HUP))
{
shutdown_source (data);
retval = FALSE;
}
if (cond & G_IO_IN)
{
char buf[BUFSIZE];
guint nbytes = 0;
guint nb;
guint j;
int i, seq;
GIOError error;
error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
if (error == G_IO_ERROR_NONE)
{
if (nb == 0)
{
g_debug ("io-channel-basic: ...from %d: EOF", fd);
shutdown_source (data);
return FALSE;
}
g_assert_cmpuint (nb, ==, sizeof (nbytes));
for (i = 0; i < nkiddies; i++)
if (seqtab[i].fd == fd)
{
g_assert_cmpint (seq, ==, seqtab[i].seq);
seqtab[i].seq++;
break;
}
error =
read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
}
if (error != G_IO_ERROR_NONE)
return FALSE;
if (nb == 0)
{
g_debug ("io-channel-basic: ...from %d: EOF", fd);
shutdown_source (data);
return FALSE;
}
g_assert_cmpuint (nb, ==, sizeof (nbytes));
g_assert_cmpuint (nbytes, <, BUFSIZE);
g_debug ("io-channel-basic: ...from %d: %d bytes", fd, nbytes);
if (nbytes > 0)
{
error = read_all (fd, channel, buf, nbytes, &nb);
if (error != G_IO_ERROR_NONE)
return FALSE;
if (nb == 0)
{
g_debug ("io-channel-basic: ...from %d: EOF", fd);
shutdown_source (data);
return FALSE;
}
for (j = 0; j < nbytes; j++)
g_assert_cmpint (buf[j], ==, ' ' + (char) ((nbytes + j) % 95));
g_debug ("io-channel-basic: ...from %d: OK", fd);
}
}
return retval;
}
#ifdef G_OS_WIN32
static gboolean
recv_windows_message (GIOChannel *channel,
GIOCondition cond,
gpointer data)
{
GIOError error;
MSG msg;
gsize nb;
while (1)
{
error = g_io_channel_read (channel, (gchar *) &msg, sizeof (MSG), &nb);
if (error != G_IO_ERROR_NONE)
{
g_test_message ("io-channel-basic: ...reading Windows message: G_IO_ERROR_%s",
(error == G_IO_ERROR_AGAIN ? "AGAIN" : (error == G_IO_ERROR_INVAL ? "INVAL" : (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
if (error == G_IO_ERROR_AGAIN)
continue;
}
break;
}
g_test_message ("io-channel-basic: ...Windows message for 0x%p: %d,%" G_GUINTPTR_FORMAT ",%" G_GINTPTR_FORMAT,
msg.hwnd, msg.message, msg.wParam, (gintptr) msg.lParam);
return TRUE;
}
LRESULT CALLBACK window_procedure (HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam);
LRESULT CALLBACK
window_procedure (HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam)
{
g_test_message ("io-channel-basic: window_procedure for 0x%p: %d,%" G_GUINTPTR_FORMAT ",%" G_GINTPTR_FORMAT,
hwnd, message, wparam, (gintptr) lparam);
return DefWindowProc (hwnd, message, wparam, lparam);
}
#endif
static void
spawn_process (int children_nb)
{
GIOChannel *my_read_channel;
gchar *cmdline;
int i;
#ifdef G_OS_WIN32
gint64 start, end;
GPollFD pollfd;
int pollresult;
ATOM klass;
static WNDCLASS wcl;
HWND hwnd;
GIOChannel *windows_messages_channel;
wcl.style = 0;
wcl.lpfnWndProc = window_procedure;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hInstance = GetModuleHandle (NULL);
wcl.hIcon = NULL;
wcl.hCursor = NULL;
wcl.hbrBackground = NULL;
wcl.lpszMenuName = NULL;
wcl.lpszClassName = "io-channel-basic";
klass = RegisterClass (&wcl);
g_assert_cmpint (klass, !=, 0);
hwnd = CreateWindow (MAKEINTATOM(klass), "io-channel-basic", 0, 0, 0, 10, 10,
NULL, NULL, wcl.hInstance, NULL);
g_assert_nonnull (hwnd);
windows_messages_channel =
g_io_channel_win32_new_messages ((guint) (guintptr) hwnd);
g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
#endif
nkiddies = (children_nb > 0 ? children_nb : 1);
seqtab = g_malloc (nkiddies * 2 * sizeof (int));
for (i = 0; i < nkiddies; i++)
{
guint *id;
int pipe_to_sub[2], pipe_from_sub[2];
if (pipe (pipe_to_sub) == -1 || pipe (pipe_from_sub) == -1)
{
perror ("pipe");
exit (1);
}
seqtab[i].fd = pipe_from_sub[0];
seqtab[i].seq = 0;
my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
id = g_new (guint, 1);
*id = g_io_add_watch_full (my_read_channel,
G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
recv_message,
id, g_free);
nrunning++;
#ifdef G_OS_WIN32
/* Spawn new Win32 process */
cmdline =
g_strdup_printf ("%d:%d:0x%p", pipe_to_sub[0], pipe_from_sub[1], hwnd);
_spawnl (_P_NOWAIT, exec_name, exec_name, "--child", cmdline, NULL);
#else
/* Spawn new Unix process */
cmdline = g_strdup_printf ("%s --child %d:%d &",
exec_name, pipe_to_sub[0], pipe_from_sub[1]);
g_assert_no_errno (system (cmdline));
#endif
g_free (cmdline);
/* Closing pipes */
close (pipe_to_sub[0]);
close (pipe_from_sub[1]);
#ifdef G_OS_WIN32
start = g_get_monotonic_time();
g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
end = g_get_monotonic_time();
g_test_message ("io-channel-basic: had to wait %" G_GINT64_FORMAT "s, result:%d",
(end - start) / 1000000, pollresult);
#endif
g_io_channel_unref (my_read_channel);
}
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);
g_main_loop_unref (main_loop);
g_free (seqtab);
}
static void
run_process (int argc, char *argv[])
{
int readfd, writefd;
gint64 dt;
char buf[BUFSIZE];
int buflen, i, j, n;
#ifdef G_OS_WIN32
HWND hwnd;
#endif
/* Extract parameters */
sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
#ifdef G_OS_WIN32
sscanf (argv[2] + n, ":0x%p", &hwnd);
#endif
dt = g_get_monotonic_time();
srand (dt ^ (dt / 1000) ^ readfd ^ (writefd << 4));
for (i = 0; i < 20 + rand () % 10; i++)
{
g_usleep ((100 + rand () % 10) * 2500);
buflen = rand () % BUFSIZE;
for (j = 0; j < buflen; j++)
buf[j] = ' ' + ((buflen + j) % 95);
g_debug ("io-channel-basic: child writing %d+%d bytes to %d",
(int) (sizeof (i) + sizeof (buflen)), buflen, writefd);
g_assert_cmpint (write (writefd, &i, sizeof (i)), ==, sizeof (i));
g_assert_cmpint (write (writefd, &buflen, sizeof (buflen)), ==, sizeof (buflen));
g_assert_cmpint (write (writefd, buf, buflen), ==, buflen);
#ifdef G_OS_WIN32
if (i % 10 == 0)
{
int msg = WM_USER + (rand () % 100);
WPARAM wparam = rand ();
LPARAM lparam = rand ();
g_test_message ("io-channel-basic: child posting message %d,%" G_GUINTPTR_FORMAT ",%" G_GINTPTR_FORMAT " to 0x%p",
msg, wparam, (gintptr) lparam, hwnd);
PostMessage (hwnd, msg, wparam, lparam);
}
#endif
}
g_debug ("io-channel-basic: child exiting, closing %d", writefd);
close (writefd);
}
static void
test_io_basics (void)
{
spawn_process (1);
#ifndef G_OS_WIN32
spawn_process (5);
#endif
}
int
main (int argc, char *argv[])
{
/* Get executable name */
exec_name = argv[0];
/* Run the tests */
g_test_init (&argc, &argv, NULL);
/* Run subprocess, if it is the case */
if (argc > 2)
{
run_process (argc, argv);
return 0;
}
g_test_add_func ("/gio/io-basics", test_io_basics);
return g_test_run ();
}
|