Example #1
0
/*
 * Creates a fan-in funnel from num_writers socketpairs into a single
 * reader
 *
 *   writers
 *    \\|//
 *   reader
 */
static bool test_multi_write(const tal_t *ctx)
{
	struct write_state ws[num_writers];
	struct read_state sink;
	struct reader_state rs[num_writers];
	int fds[2];

	sink.pos = 0;
	sink.lock = io_lock_new(ctx);
	memset(&sink.reads, 0, sizeof(sink.reads));

	for (size_t i=0; i<num_writers; i++) {
		socketpair(AF_LOCAL, SOCK_STREAM, 0, fds);

		rs[i].read_state = &sink;
		rs[i].count = 0;
		rs[i].buf = -1;

		ws[i].id = (u8)(i+'a');
		ws[i].count = 0;

		rs[i].upstream = io_new_conn(ctx, fds[1], writer, &ws[i]);
		io_new_conn(ctx, fds[0], reader_locked, &rs[i]);
	}
	io_loop(NULL, NULL);

	/* Now check that we were serialized correctly, i.e., num_writers sets of num_writes identical numbers */
	for (size_t i=0; i<num_writers; i++) {
		for (size_t j=1; j<num_writes; j++)
			if (sink.reads[i*num_writes+j] != sink.reads[i*num_writes+j-1])
				return false;
	}

	return true;
}
Example #2
0
static void init_conn(int fd, struct data *d)
{
	int fd2;

	ok1(d->state == 0);
	d->state++;
	idler = io_new_conn(fd, io_wait(d, read_buf, d));
	io_set_finish(idler, finish_idle, d);

	/* This will wake us up, as read will fail. */
	fd2 = open("/dev/null", O_RDONLY);
	ok1(fd2 >= 0);
	io_set_finish(io_new_conn(fd2, io_read(idler, 1, never, NULL)),
		      finish_waker, d);
}
Example #3
0
static void init_conn(int fd, struct packet *pkt)
{
	ok1(pkt->state == 0);
	pkt->state++;

	io_set_finish(io_new_conn(fd, io_read_packet(pkt, io_close_cb, pkt)),
		      finish_ok, pkt);
}
Example #4
0
static void init_conn(int fd, struct data *d)
{
	ok1(d->state == 0);
	d->state++;
	/* Empty read should run immediately... */
	io_set_finish(io_new_conn(fd, io_read(NULL, 0, write_buf, d)),
		      finish_ok, d);
}
Example #5
0
static void init_conn(int fd, struct data *d)
{
	ok1(d->state == 0);
	d->state++;
	io_set_finish(io_new_conn(fd, io_write(d->buf, d->bytes,
					       io_close_cb, d)),
		      finish_ok, d);
}
int main(void)
{
	int fds[2];
	struct io_conn *conn;

	plan_tests(3);

	ok1(pipe(fds) == 0);
	conn = io_new_conn(fds[0], io_idle());
	io_new_conn(fds[1], io_write("EASYTEST", 8, wake_it, conn));

	ok1(io_loop() == NULL);
	ok1(memcmp(inbuf, "EASYTEST", sizeof(inbuf)) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Example #7
0
static void init_conn(int fd, struct data *d)
{
	ok1(d->state == 0);
	d->state++;

	io_set_finish(io_new_conn(fd,
				  io_read(d->buf, sizeof(d->buf), io_close_cb, d)),
		      finish_ok, d);
}
static void init_conn(int fd, struct data *d)
{
	ok1(d->state == 0);
	d->state++;
	d->bytes = sizeof(d->buf);

	io_set_finish(io_new_conn(fd,
				  io_read_partial(d->buf, &d->bytes, io_close_cb, d)),
		      finish_ok, d);
}
Example #9
0
static void init_conn(int fd, struct data *d)
{
	struct io_conn *conn;

	ok1(d->state == 0);
	d->state++;

	conn = io_new_conn(fd, io_read(d->buf, sizeof(d->buf), no_timeout, d));
	io_set_finish(conn, finish_ok, d);
	io_timeout(conn, time_from_usec(d->timeout_usec), timeout, d);
}
Example #10
0
static struct io_plan *init_idle(struct io_conn *conn, struct data *d)
{
	int fd2;

	ok1(d->state == 0);
	d->state++;
	idler = conn;
	io_set_finish(conn, finish_idle, d);

	/* This will wake us up, as read will fail. */
	fd2 = open("/dev/null", O_RDONLY);
	ok1(fd2 >= 0);
	io_set_finish(io_new_conn(NULL, fd2, init_waker, d), finish_waker, d);

	return io_wait(conn, d, read_buf, d);
}
Example #11
0
int main(void)
{
	int fds[2];

	plan_tests(8);

	pipe(fds);
	ok1(io_poll_override(mypoll) == poll);

	io_new_conn(NULL, fds[0], setup_conn, NULL);
	ok1(io_loop(NULL, NULL) == NULL);
	close(fds[1]);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Example #12
0
int main(void)
{
	int status;

	plan_tests(3);

	if (fork() == 0) {
		int fds[2];

		ok1(pipe(fds) == 0);
		io_new_conn(NULL, fds[0], setup_waiter, &status);
		io_loop(NULL, NULL);
		exit(1);
	}

	ok1(wait(&status) != -1);
	ok1(WIFSIGNALED(status));
	ok1(WTERMSIG(status) == SIGABRT);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Example #13
0
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename)
{
	struct sockaddr_un addr;
	int fd, old_umask;

	if (streq(rpc_filename, ""))
		return;

	if (streq(rpc_filename, "/dev/tty")) {
		fd = open(rpc_filename, O_RDWR);
		if (fd == -1)
			err(1, "Opening %s", rpc_filename);
		io_new_conn(dstate, fd, jcon_connected, dstate);
		return;
	}

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
		errx(1, "rpc filename '%s' too long", rpc_filename);
	strcpy(addr.sun_path, rpc_filename);
	addr.sun_family = AF_UNIX;

	/* Of course, this is racy! */
	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0)
		errx(1, "rpc filename '%s' in use", rpc_filename);
	unlink(rpc_filename);

	/* This file is only rw by us! */
	old_umask = umask(0177);
	if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
		err(1, "Binding rpc socket to '%s'", rpc_filename);
	umask(old_umask);

	if (listen(fd, 1) != 0)
		err(1, "Listening on '%s'", rpc_filename);

	io_new_listener(dstate, fd, incoming_jcon_connected, dstate);
}
Example #14
0
int main(int argc, char *argv[])
{
	struct client client;
	unsigned int i, j;
	struct sockaddr_un addr;
	struct timespec start, end;
	int fd, wake[2];
	char buf;

	addr.sun_family = AF_UNIX;
	sprintf(addr.sun_path, "/tmp/run-different-speed.sock.%u", getpid());

	if (pipe(wake) != 0 || pipe(timeout) != 0)
		err(1, "Creating pipes");

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd < 0)
		err(1, "Creating socket");

	if (bind(fd, (void *)&addr, sizeof(addr)) != 0)
		err(1, "Binding to %s", addr.sun_path);

	if (listen(fd, NUM_CONNS) != 0)
		err(1, "Listening on %s", addr.sun_path);

	for (i = 0; i < NUM_CHILDREN; i++) {
		switch (fork()) {
		case -1:
			err(1, "forking");
		case 0:
			close(wake[1]);
			create_clients(&addr, wake[0]);
			break;
		}
		for (j = 0; j < NUM_CONNS; j++) {
			int ret = accept(fd, NULL, 0);
			if (ret < 0)
				err(1, "Accepting fd");
			/* For efficiency, we share client structure */
			io_new_conn(ret,
				    io_read(client.request_buffer, REQUEST_SIZE,
					    write_reply, &client));
		}
	}

	io_new_conn(timeout[0], io_read(&buf, 1, do_timeout, &buf));

	close(wake[0]);
	for (i = 0; i < NUM_CHILDREN; i++)
		write(wake[1], "1", 1);

	signal(SIGALRM, sigalarm);
	alarm(10);
	start = time_now();
	io_loop();
	end = time_now();
	close(fd);

	printf("%u connections complete (%u ns per conn)\n",
	       completed,
	       (int)time_to_nsec(time_divide(time_sub(end, start), completed)));
	return 0;
}
Example #15
0
static void init_conn(int fd, int *state)
{
	ok1(*state == 0);
	(*state)++;
	io_set_finish(io_new_conn(fd, io_close()), finish_ok, state);
}