Beispiel #1
0
/* Tallocs *output off ctx; return false if command fails. */
bool run_command(const void *ctx, unsigned int *time_ms, char **output,
		 const char *fmt, ...)
{
	va_list ap;
	char *cmd;
	bool ok;
	unsigned int default_time = default_timeout_ms;

	if (!time_ms)
		time_ms = &default_time;
	else if (*time_ms == 0) {
		*output = talloc_strdup(ctx, "\n== TIMED OUT ==\n");
		return false;
	}

	va_start(ap, fmt);
	cmd = talloc_vasprintf(ctx, fmt, ap);
	va_end(ap);

	*output = run_with_timeout(ctx, cmd, &ok, time_ms);
	if (ok)
		return true;
	if (!*output)
		err(1, "Problem running child");
	if (*time_ms == 0)
		*output = talloc_asprintf_append(*output,
						 "\n== TIMED OUT ==\n");
	return false;
}
Beispiel #2
0
char* read_body(int sock, char* buf, int bufsize)
{
	struct recv_arg rvarg;
	int sum_read;
	int empty_buf_start;
	int granularity;

	granularity = 1024;
	empty_buf_start = 0;
	sum_read = 0;

	if (buf)
	{
		buf = (char*)realloc(buf, granularity + bufsize);
		sum_read = empty_buf_start = bufsize;
	}
	else
		buf = (char*)malloc(granularity);
	bufsize += granularity;

	do
	{
		memset(&rvarg, 0, sizeof(struct recv_arg));
		rvarg.s = sock;
		rvarg.buf = buf + empty_buf_start;
		rvarg.len = bufsize - sum_read;
		rvarg.flags = 0;
		/* First, peek at the available data. */

		if (run_with_timeout(30, recv_with_timeout_callback, (void*)&rvarg))
		{
			free(buf);
			return NULL;
		}

		if (rvarg.res > 0)
		{
			sum_read += rvarg.res;
			empty_buf_start += rvarg.res;
		}
		if (sum_read >= bufsize)
		{
			buf = (char*)realloc (buf, bufsize + granularity);
			bufsize += granularity;
		}
	}
	while (rvarg.res > 0);

	if (sum_read == bufsize)
		buf = (char*)realloc (buf, bufsize + 1);
	buf[sum_read]='\0';

	if (rvarg.res == -1)
	{
		free(buf);
		return NULL;
	}
		
	return buf;
}
Beispiel #3
0
bool
ssl_connect_wget (int fd, const char *hostname)
{
  SSL *conn;
  struct scwt_context scwt_ctx;
  struct openssl_transport_context *ctx;

  DEBUGP (("Initiating SSL handshake.\n"));

  assert (ssl_ctx != NULL);
  conn = SSL_new (ssl_ctx);
  if (!conn)
    goto error;
#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
  /* If the SSL library was build with support for ServerNameIndication
     then use it whenever we have a hostname.  If not, don't, ever. */
  if (! is_valid_ip_address (hostname))
    {
      if (! SSL_set_tlsext_host_name (conn, hostname))
        {
          DEBUGP (("Failed to set TLS server-name indication."));
          goto error;
        }
    }
#endif

#ifndef FD_TO_SOCKET
# define FD_TO_SOCKET(X) (X)
#endif
  if (!SSL_set_fd (conn, FD_TO_SOCKET (fd)))
    goto error;
  SSL_set_connect_state (conn);

  scwt_ctx.ssl = conn;
  if (run_with_timeout(opt.read_timeout, ssl_connect_with_timeout_callback,
                       &scwt_ctx)) {
    DEBUGP (("SSL handshake timed out.\n"));
    goto timeout;
  }
  if (scwt_ctx.result <= 0 || conn->state != SSL_ST_OK)
    goto error;

  ctx = xnew0 (struct openssl_transport_context);
  ctx->conn = conn;

  /* Register FD with Wget's transport layer, i.e. arrange that our
     functions are used for reading, writing, and polling.  */
  fd_register_transport (fd, &openssl_transport, ctx);
  DEBUGP (("Handshake successful; connected socket %d to SSL handle 0x%0*lx\n",
           fd, PTR_FORMAT (conn)));
  return true;

 error:
  DEBUGP (("SSL handshake failed.\n"));
  print_errors ();
 timeout:
  if (conn)
    SSL_free (conn);
  return false;
}
Beispiel #4
0
void main()
{
	int ret = -1;
	sleep_tm = 5;
	ret = run_with_timeout(3, sleep_time, &sleep_tm);
	printf("ret = %d \n", ret);
	//schedule();
	//printf("%lu \n", get_system_uptime(NULL));
}
Beispiel #5
0
static int
openssl_read (int fd, char *buf, int bufsize, void *arg)
{
  struct openssl_read_args args;
  args.fd = fd;
  args.buf = buf;
  args.bufsize = bufsize;
  args.ctx = (struct openssl_transport_context*) arg;

  if (run_with_timeout(opt.read_timeout, openssl_read_callback, &args)) {
    return -1;
  }
  return args.retval;
}
Beispiel #6
0
Datei: host.c Projekt: Red54/axtu
static struct hostent *
gethostbyname_with_timeout (const char *host_name, double timeout)
{
  struct ghbnwt_context ctx;
  ctx.host_name = host_name;
  if (run_with_timeout (timeout, gethostbyname_with_timeout_callback, &ctx))
    {
      SET_H_ERRNO (HOST_NOT_FOUND);
      errno = ETIMEDOUT;
      return NULL;
    }
  if (!ctx.hptr)
    errno = 0;
  return ctx.hptr;
}
Beispiel #7
0
Datei: host.c Projekt: Red54/axtu
static int
getaddrinfo_with_timeout (const char *node, const char *service,
			  const struct addrinfo *hints, struct addrinfo **res,
			  double timeout)
{
  struct gaiwt_context ctx;
  ctx.node = node;
  ctx.service = service;
  ctx.hints = hints;
  ctx.res = res;

  if (run_with_timeout (timeout, getaddrinfo_with_timeout_callback, &ctx))
    {
      errno = ETIMEDOUT;
      return EAI_SYSTEM;
    }
  return ctx.exit_code;
}
Beispiel #8
0
static int
connect_with_timeout (int fd, const struct sockaddr *addr, socklen_t addrlen,
                      double timeout)
{
  struct cwt_context ctx;
  ctx.fd = fd;
  ctx.addr = addr;
  ctx.addrlen = addrlen;

  if (run_with_timeout (timeout, connect_with_timeout_callback, &ctx))
    {
      errno = ETIMEDOUT;
      return -1;
    }
  if (ctx.result == -1 && errno == EINTR)
    errno = ETIMEDOUT;
  return ctx.result;
}
Beispiel #9
0
char* receive_response(int sock, char** body, int* body_size)
{
	long bufsize = 512;
	char *buf = (char*)malloc (bufsize);
	int tail = 0;
	*body_size = 0;

	while (1)
	{
		const char *end;
		int remain;
		struct recv_arg rvarg;

		memset(&rvarg, 0, sizeof(struct recv_arg));
		rvarg.s = sock;
		rvarg.buf = buf + tail;
		rvarg.len = bufsize - 1 - tail;
		rvarg.flags = 0;
		/* First, peek at the available data. */

		if (run_with_timeout(30, recv_with_timeout_callback, (void*)&rvarg))
		{
			free (buf);
			return NULL;
		}
		else if (rvarg.res < 0)
		{
			free (buf);
			return NULL;
		}

		end = response_head_terminator(buf, buf + tail, rvarg.res);
		if (end)
		{
			/* The data contains the terminator: we'll drain the data up
			to the end of the terminator.  */
			remain = end - (buf + tail);
			*body = (char*)malloc(rvarg.res - remain + 1);
			memcpy(*body, end, rvarg.res - remain);
			(*body)[rvarg.res - remain] = '\0';
			*body_size = rvarg.res - remain;

			if (remain == 0)
			{
				/* No more data needs to be read. */
				buf[tail] = '\0';
				return buf;
			}
			if (bufsize - 1 < tail + remain)
			{
				bufsize = tail + remain + 1;
				buf = (char*)realloc(buf, bufsize);
				buf[tail] = '\0';
				return buf;
			}
		}

		tail += rvarg.res;
		buf[tail] = '\0';

		if (rvarg.res == 0)
		{
			if (tail == 0)
			{
				/* EOF without anything having been read */
				free (buf);
				/*errno = 0;*/
				return NULL;
			}
			else
				/* EOF seen: return the data we've read. */
				return buf;
		}

		/* Keep looping until all the data arrives. */
		if (tail == bufsize - 1)
		{
			bufsize <<= 1;
			buf = (char*)realloc (buf, bufsize);
		}
	}
}