aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/socat.c
blob: 07cbf4645add87cc658f7265988c07c1fe7d8566 (plain) (blame)
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
/*
     This file is part of libmicrohttpd
     Copyright (C) 2008,2016 Christian Grothoff

     libmicrohttpd is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     libmicrohttpd 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libmicrohttpd; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file socat.c
 * @brief  Code to fork-exec zzuf and start the socat process
 * @author Christian Grothoff
 */

#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif /* !WIN32_LEAN_AND_MEAN */
#include <windows.h>
#endif


/**
 * A larger loop count will run more random tests --
 * which would be good, except that it may take too
 * long for most user's patience.  So this small
 * value is the default.
 */
#define LOOP_COUNT 10

#define CURL_TIMEOUT 50L

static pid_t zzuf_pid;

static void
zzuf_socat_start ()
{
  int status;
  char *const args[] = {
    "zzuf",
    "--ratio=0.0:0.75",
    "-n",
    "-A",
    "socat",
    "-lf",
    "/dev/null",
    "TCP4-LISTEN:11081,reuseaddr,fork",
    "TCP4:127.0.0.1:11080",
    NULL,
  };
  zzuf_pid = fork ();
  if (zzuf_pid == -1)
  {
    fprintf (stderr, "fork failed: %s\n", strerror (errno));
    exit (1);
  }
  if (zzuf_pid != 0)
  {
    (void) sleep (1);                 /* allow zzuf and socat to start */
    status = 0;
    if (0 < waitpid (zzuf_pid, &status, WNOHANG))
    {
      if (WIFEXITED (status))
        fprintf (stderr,
                 "zzuf died with status code %d!\n",
                 WEXITSTATUS (status));
      if (WIFSIGNALED (status))
        fprintf (stderr,
                 "zzuf died from signal %d!\n", WTERMSIG (status));
      exit (1);
    }
    return;
  }
  setpgid (0, 0);
  execvp ("zzuf", args);
  fprintf (stderr, "execution of `zzuf' failed: %s\n", strerror (errno));
  exit (1);
}


static void
zzuf_socat_stop ()
{
  int status;
  if (zzuf_pid != 0)
  {
    if (0 != killpg (zzuf_pid, SIGINT))
      fprintf (stderr, "Failed to killpg: %s\n", strerror (errno));
    kill (zzuf_pid, SIGINT);
    waitpid (zzuf_pid, &status, 0);
    (void) sleep (1);                 /* allow socat to also die in peace */
  }
}

/* end of socat.c */