<commit_before>#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <fcntl.h>

#include "zdtmtst.h"

const char *test_doc	= "Multi-client - server app";
const char *test_author	= "<NAME> <<EMAIL>>";

#define PROCS_DEF	4
#define PROCS_MAX	64
unsigned int num_procs = PROCS_DEF;
TEST_OPTION(num_procs, uint, "# processes to create "
	    "(default " __stringify(PROCS_DEF)
	    ", max " __stringify(PROCS_MAX) ")", 0);

char *filename;
TEST_OPTION(filename, string, "file name", 1);

#define ACCEPT_TIMEOUT 100	/* max delay for the child to connect */

static int fill_sock_name(struct sockaddr_un *name, const char *filename)
{
	if (strlen(filename) >= sizeof(name->sun_path))
		return -1;

	name->sun_family = AF_LOCAL;
	strcpy(name->sun_path, filename);
	return 0;
}

static int setup_srv_sock(void)
{
	struct sockaddr_un name;
	int sock;

	if (fill_sock_name(&name, filename) < 0) {
		pr_perror("filename \"%s\" is too long", filename);
		return -1;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		pr_perror("can't create socket");
		return -1;
	}

	if (bind(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
		pr_perror("can't bind to socket \"%s\"", filename);
		goto err;
	}

	if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
		pr_perror("can't make socket \"%s\" non-blocking", filename);
		goto err;
	}

	if (listen(sock, 1) < 0) {
		pr_perror("can't listen on a socket \"%s\"", filename);
		goto err;
	}

	return sock;
err:
	close(sock);
	return -1;
}

static int accept_one_conn(int sock)
{
	int acc_sock;
	fd_set fds;
	struct timeval timeout = {
		.tv_sec = ACCEPT_TIMEOUT,
	};

	FD_ZERO(&fds);
	FD_SET(sock, &fds);

	switch (select(FD_SETSIZE, &fds, NULL, NULL, &timeout)) {
	case 1:
		break;
	case 0:
		pr_perror("timeout accepting a connection");
		return -1;
	default:
		pr_perror("error while waiting for a connection");
		return -1;
	}

	acc_sock = accept(sock, NULL, NULL);
	if (acc_sock < 0)
		pr_perror("error accepting a connection");
	return acc_sock;
}

static int setup_clnt_sock(void)
{
	struct sockaddr_un name;
	int sock;
	int ret = 0;

	if (fill_sock_name(&name, filename) < 0) {
		ret = -errno;
		pr_perror("filename \"%s\" is too long", filename);
		return ret;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		ret = -errno;<commit_msg>zdtm unix_sock: don't use errno<commit_after>#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <fcntl.h>

#include "zdtmtst.h"

const char *test_doc	= "Multi-client - server app";
const char *test_author	= "<NAME> <<EMAIL>>";

#define PROCS_DEF	4
#define PROCS_MAX	64
unsigned int num_procs = PROCS_DEF;
TEST_OPTION(num_procs, uint, "# processes to create "
	    "(default " __stringify(PROCS_DEF)
	    ", max " __stringify(PROCS_MAX) ")", 0);

char *filename;
TEST_OPTION(filename, string, "file name", 1);

#define ACCEPT_TIMEOUT 100	/* max delay for the child to connect */

static int fill_sock_name(struct sockaddr_un *name, const char *filename)
{
	if (strlen(filename) >= sizeof(name->sun_path))
		return -1;

	name->sun_family = AF_LOCAL;
	strcpy(name->sun_path, filename);
	return 0;
}

static int setup_srv_sock(void)
{
	struct sockaddr_un name;
	int sock;

	if (fill_sock_name(&name, filename) < 0) {
		pr_perror("filename \"%s\" is too long", filename);
		return -1;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		pr_perror("can't create socket");
		return -1;
	}

	if (bind(sock, (struct sockaddr *) &name, SUN_LEN(&name)) < 0) {
		pr_perror("can't bind to socket \"%s\"", filename);
		goto err;
	}

	if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
		pr_perror("can't make socket \"%s\" non-blocking", filename);
		goto err;
	}

	if (listen(sock, 1) < 0) {
		pr_perror("can't listen on a socket \"%s\"", filename);
		goto err;
	}

	return sock;
err:
	close(sock);
	return -1;
}

static int accept_one_conn(int sock)
{
	int acc_sock;
	fd_set fds;
	struct timeval timeout = {
		.tv_sec = ACCEPT_TIMEOUT,
	};

	FD_ZERO(&fds);
	FD_SET(sock, &fds);

	switch (select(FD_SETSIZE, &fds, NULL, NULL, &timeout)) {
	case 1:
		break;
	case 0:
		pr_perror("timeout accepting a connection");
		return -1;
	default:
		pr_perror("error while waiting for a connection");
		return -1;
	}

	acc_sock = accept(sock, NULL, NULL);
	if (acc_sock < 0)
		pr_perror("error accepting a connection");
	return acc_sock;
}

static int setup_clnt_sock(void)
{
	struct sockaddr_un name;
	int sock;
	int ret = 0;

	if (fill_sock_name(&name, filename) < 0) {
		pr_perror("filename \"%s\" is too long", filename);
		return -1;
	}

	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (sock < 0) {
		ret = -errno;
{'fraction_non_alphanumeric': 0.15548841893252768, 'fraction_numerical': 0.011883182275931522, 'mean_word_length': 4.227368421052631, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1993152', 'n_tokens_mistral': 2083, 'n_tokens_neox': 1966, 'n_words': 523}