| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <glib.h> |
| 9 |
|
| 10 |
#include <errno.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
int |
| 15 |
main (void) |
| 16 |
{ |
| 17 |
/* Small buffer to find partial read/write bugs */ |
| 18 |
gchar buffer[sizeof (gint) - 1]; |
| 19 |
gsize written; |
| 20 |
gssize count; |
| 21 |
gint i; |
| 22 |
|
| 23 |
g_print ("37\n\n{ \"command\" : \"init\", \"version\": 1 }"); |
| 24 |
for (i = 0; TRUE; i++) |
| 25 |
{ |
| 26 |
count = read (0, buffer, sizeof (buffer)); |
| 27 |
if (count < 0) |
| 28 |
{ |
| 29 |
g_printerr ("mock-echo: failed to read: %s", g_strerror (errno)); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
if (count == 0) |
| 33 |
return 0; |
| 34 |
|
| 35 |
written = 0; |
| 36 |
while (written < count) |
| 37 |
{ |
| 38 |
count = write (1, buffer + written, count - written); |
| 39 |
if (count < 0) |
| 40 |
{ |
| 41 |
if (errno != EAGAIN) |
| 42 |
{ |
| 43 |
g_printerr ("mock-echo: failed to write: %s", g_strerror (errno)); |
| 44 |
return 1; |
| 45 |
} |
| 46 |
} |
| 47 |
written += count; |
| 48 |
} |
| 49 |
|
| 50 |
/* Slow short reads and writes for the first, 10 and then accelerate */ |
| 51 |
if (i < 3) |
| 52 |
g_usleep (G_USEC_PER_SEC / 10); |
| 53 |
else if (i < 30) |
| 54 |
g_usleep (G_USEC_PER_SEC / 100); |
| 55 |
} |
| 56 |
} |