1/*
2     This file is part of libmicrohttpd
3     Copyright (C) 2008 Christian Grothoff
4
5     libmicrohttpd is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published
7     by the Free Software Foundation; either version 2, or (at your
8     option) any later version.
9
10     libmicrohttpd is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with libmicrohttpd; see the file COPYING.  If not, write to the
17     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18     Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file socat.c
23 * @brief  Code to fork-exec zzuf and start the socat process
24 * @author Christian Grothoff
25 */
26
27#include <errno.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <signal.h>
31
32#ifdef _WIN32
33#ifndef WIN32_LEAN_AND_MEAN
34#define WIN32_LEAN_AND_MEAN 1
35#endif /* !WIN32_LEAN_AND_MEAN */
36#include <windows.h>
37#endif
38
39
40/**
41 * A larger loop count will run more random tests --
42 * which would be good, except that it may take too
43 * long for most user's patience.  So this small
44 * value is the default.
45 */
46#define LOOP_COUNT 10
47
48#define CURL_TIMEOUT 50L
49
50static pid_t zzuf_pid;
51
52static void
53zzuf_socat_start ()
54{
55  int status;
56  char *const args[] = {
57    "zzuf",
58    "--ratio=0.0:0.75",
59    "-n",
60    "-A",
61    "--",
62    "socat",
63    "-lf",
64    "/dev/null",
65    "TCP4-LISTEN:11081,reuseaddr,fork",
66    "TCP4:127.0.0.1:11080",
67    NULL,
68  };
69  zzuf_pid = fork ();
70  if (zzuf_pid == -1)
71    {
72      fprintf (stderr, "fork failed: %s\n", strerror (errno));
73      exit (1);
74    }
75  if (zzuf_pid != 0)
76    {
77      sleep (1);                /* allow zzuf and socat to start */
78      status = 0;
79      if (0 < waitpid (zzuf_pid, &status, WNOHANG))
80        {
81          if (WIFEXITED (status))
82            fprintf (stderr,
83                     "zzuf died with status code %d!\n",
84                     WEXITSTATUS (status));
85          if (WIFSIGNALED (status))
86            fprintf (stderr,
87                     "zzuf died from signal %d!\n", WTERMSIG (status));
88          exit (1);
89        }
90      return;
91    }
92  setpgrp ();
93  execvp ("zzuf", args);
94  fprintf (stderr, "execution of `zzuf' failed: %s\n", strerror (errno));
95  zzuf_pid = 0;                 /* fork failed */
96  exit (1);
97}
98
99
100static void
101zzuf_socat_stop ()
102{
103  int status;
104  if (zzuf_pid != 0)
105    {
106      if (0 != killpg (zzuf_pid, SIGINT))
107        fprintf (stderr, "Failed to killpg: %s\n", strerror (errno));
108      kill (zzuf_pid, SIGINT);
109      waitpid (zzuf_pid, &status, 0);
110      sleep (1);                /* allow socat to also die in peace */
111    }
112}
113
114/* end of socat.c */
115