aboutsummaryrefslogtreecommitdiff
path: root/src/testzzuf/socat.c
blob: 9b351197a4c71365b8a4994cacdde634f0b206d6 (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
113
114
/*
     This file is part of libmicrohttpd
     (C) 2008 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., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, 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)
    {
      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;
    }
  setpgrp ();
  execvp ("zzuf", args);
  fprintf (stderr, "execution of `zzuf' failed: %s\n", strerror (errno));
  zzuf_pid = 0;                 /* fork failed */
  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);
      sleep (1);                /* allow socat to also die in peace */
    }
}

/* end of socat.c */