Blame src/testzzuf/socat.c

Packit 875988
/*
Packit 875988
     This file is part of libmicrohttpd
Packit 875988
     Copyright (C) 2008,2016 Christian Grothoff
Packit 875988
Packit 875988
     libmicrohttpd is free software; you can redistribute it and/or modify
Packit 875988
     it under the terms of the GNU General Public License as published
Packit 875988
     by the Free Software Foundation; either version 2, or (at your
Packit 875988
     option) any later version.
Packit 875988
Packit 875988
     libmicrohttpd is distributed in the hope that it will be useful, but
Packit 875988
     WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 875988
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 875988
     General Public License for more details.
Packit 875988
Packit 875988
     You should have received a copy of the GNU General Public License
Packit 875988
     along with libmicrohttpd; see the file COPYING.  If not, write to the
Packit 875988
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit 875988
     Boston, MA 02110-1301, USA.
Packit 875988
*/
Packit 875988
Packit 875988
/**
Packit 875988
 * @file socat.c
Packit 875988
 * @brief  Code to fork-exec zzuf and start the socat process
Packit 875988
 * @author Christian Grothoff
Packit 875988
 */
Packit 875988
Packit 875988
#include <errno.h>
Packit 875988
#include <sys/types.h>
Packit 875988
#include <sys/wait.h>
Packit 875988
#include <signal.h>
Packit 875988
Packit 875988
#ifdef _WIN32
Packit 875988
#ifndef WIN32_LEAN_AND_MEAN
Packit 875988
#define WIN32_LEAN_AND_MEAN 1
Packit 875988
#endif /* !WIN32_LEAN_AND_MEAN */
Packit 875988
#include <windows.h>
Packit 875988
#endif
Packit 875988
Packit 875988
Packit 875988
/**
Packit 875988
 * A larger loop count will run more random tests --
Packit 875988
 * which would be good, except that it may take too
Packit 875988
 * long for most user's patience.  So this small
Packit 875988
 * value is the default.
Packit 875988
 */
Packit 875988
#define LOOP_COUNT 10
Packit 875988
Packit 875988
#define CURL_TIMEOUT 50L
Packit 875988
Packit 875988
static pid_t zzuf_pid;
Packit 875988
Packit 875988
static void
Packit 875988
zzuf_socat_start ()
Packit 875988
{
Packit 875988
  int status;
Packit 875988
  char *const args[] = {
Packit 875988
    "zzuf",
Packit 875988
    "--ratio=0.0:0.75",
Packit 875988
    "-n",
Packit 875988
    "-A",
Packit 875988
    "socat",
Packit 875988
    "-lf",
Packit 875988
    "/dev/null",
Packit 875988
    "TCP4-LISTEN:11081,reuseaddr,fork",
Packit 875988
    "TCP4:127.0.0.1:11080",
Packit 875988
    NULL,
Packit 875988
  };
Packit 875988
  zzuf_pid = fork ();
Packit 875988
  if (zzuf_pid == -1)
Packit 875988
    {
Packit 875988
      fprintf (stderr, "fork failed: %s\n", strerror (errno));
Packit 875988
      exit (1);
Packit 875988
    }
Packit 875988
  if (zzuf_pid != 0)
Packit 875988
    {
Packit 875988
      (void)sleep (1);                /* allow zzuf and socat to start */
Packit 875988
      status = 0;
Packit 875988
      if (0 < waitpid (zzuf_pid, &status, WNOHANG))
Packit 875988
        {
Packit 875988
          if (WIFEXITED (status))
Packit 875988
            fprintf (stderr,
Packit 875988
                     "zzuf died with status code %d!\n",
Packit 875988
                     WEXITSTATUS (status));
Packit 875988
          if (WIFSIGNALED (status))
Packit 875988
            fprintf (stderr,
Packit 875988
                     "zzuf died from signal %d!\n", WTERMSIG (status));
Packit 875988
          exit (1);
Packit 875988
        }
Packit 875988
      return;
Packit 875988
    }
Packit 875988
  setpgid (0, 0);
Packit 875988
  execvp ("zzuf", args);
Packit 875988
  fprintf (stderr, "execution of `zzuf' failed: %s\n", strerror (errno));
Packit 875988
  zzuf_pid = 0;                 /* fork failed */
Packit 875988
  exit (1);
Packit 875988
}
Packit 875988
Packit 875988
Packit 875988
static void
Packit 875988
zzuf_socat_stop ()
Packit 875988
{
Packit 875988
  int status;
Packit 875988
  if (zzuf_pid != 0)
Packit 875988
    {
Packit 875988
      if (0 != killpg (zzuf_pid, SIGINT))
Packit 875988
        fprintf (stderr, "Failed to killpg: %s\n", strerror (errno));
Packit 875988
      kill (zzuf_pid, SIGINT);
Packit 875988
      waitpid (zzuf_pid, &status, 0);
Packit 875988
      (void)sleep (1);                /* allow socat to also die in peace */
Packit 875988
    }
Packit 875988
}
Packit 875988
Packit 875988
/* end of socat.c */