1309554Ssobomax/*-
2309554Ssobomax * Copyright (c) 2005 Andrey Simonenko
3309554Ssobomax * All rights reserved.
4309554Ssobomax *
5309554Ssobomax * Redistribution and use in source and binary forms, with or without
6309554Ssobomax * modification, are permitted provided that the following conditions
7309554Ssobomax * are met:
8309554Ssobomax * 1. Redistributions of source code must retain the above copyright
9309554Ssobomax *    notice, this list of conditions and the following disclaimer.
10309554Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
11309554Ssobomax *    notice, this list of conditions and the following disclaimer in the
12309554Ssobomax *    documentation and/or other materials provided with the distribution.
13309554Ssobomax *
14309554Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15309554Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16309554Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17309554Ssobomax * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18309554Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19309554Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20309554Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21309554Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22309554Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23309554Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24309554Ssobomax * SUCH DAMAGE.
25309554Ssobomax */
26309554Ssobomax
27309554Ssobomax#include <sys/cdefs.h>
28309554Ssobomax__FBSDID("$FreeBSD: stable/11/tools/regression/sockets/unix_cmsg/t_generic.c 339066 2018-10-01 17:26:41Z sobomax $");
29309554Ssobomax
30309554Ssobomax#include <sys/types.h>
31309554Ssobomax#include <sys/socket.h>
32309554Ssobomax#include <sys/un.h>
33309554Ssobomax#include <stdarg.h>
34309554Ssobomax#include <stdbool.h>
35309554Ssobomax
36309554Ssobomax#include "uc_common.h"
37309554Ssobomax#include "t_generic.h"
38309554Ssobomax
39309554Ssobomaxint
40309554Ssobomaxt_generic(int (*client_func)(int), int (*server_func)(int))
41309554Ssobomax{
42309554Ssobomax	int fd, rv, rv_client;
43309554Ssobomax
44309554Ssobomax	switch (uc_client_fork()) {
45309554Ssobomax	case 0:
46309554Ssobomax		fd = uc_socket_create();
47309554Ssobomax		if (fd < 0)
48309554Ssobomax			rv = -2;
49309554Ssobomax		else {
50309554Ssobomax			rv = client_func(fd);
51309554Ssobomax			if (uc_socket_close(fd) < 0)
52309554Ssobomax				rv = -2;
53309554Ssobomax		}
54309554Ssobomax		uc_client_exit(rv);
55309554Ssobomax		break;
56309554Ssobomax	case 1:
57309554Ssobomax		fd = uc_socket_create();
58309554Ssobomax		if (fd < 0)
59309554Ssobomax			rv = -2;
60309554Ssobomax		else {
61309554Ssobomax			rv = server_func(fd);
62309554Ssobomax			rv_client = uc_client_wait();
63309554Ssobomax			if (rv == 0 || (rv == -2 && rv_client != 0))
64309554Ssobomax				rv = rv_client;
65309554Ssobomax			if (uc_socket_close(fd) < 0)
66309554Ssobomax				rv = -2;
67309554Ssobomax		}
68309554Ssobomax		break;
69309554Ssobomax	default:
70309554Ssobomax		rv = -2;
71309554Ssobomax	}
72309554Ssobomax	return (rv);
73309554Ssobomax}
74