1*1fab62b1SMaxim Sobolev /*-
2*1fab62b1SMaxim Sobolev * Copyright (c) 2005 Andrey Simonenko
3*1fab62b1SMaxim Sobolev * All rights reserved.
4*1fab62b1SMaxim Sobolev *
5*1fab62b1SMaxim Sobolev * Redistribution and use in source and binary forms, with or without
6*1fab62b1SMaxim Sobolev * modification, are permitted provided that the following conditions
7*1fab62b1SMaxim Sobolev * are met:
8*1fab62b1SMaxim Sobolev * 1. Redistributions of source code must retain the above copyright
9*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer.
10*1fab62b1SMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright
11*1fab62b1SMaxim Sobolev * notice, this list of conditions and the following disclaimer in the
12*1fab62b1SMaxim Sobolev * documentation and/or other materials provided with the distribution.
13*1fab62b1SMaxim Sobolev *
14*1fab62b1SMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fab62b1SMaxim Sobolev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fab62b1SMaxim Sobolev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fab62b1SMaxim Sobolev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fab62b1SMaxim Sobolev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fab62b1SMaxim Sobolev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fab62b1SMaxim Sobolev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fab62b1SMaxim Sobolev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fab62b1SMaxim Sobolev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fab62b1SMaxim Sobolev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fab62b1SMaxim Sobolev * SUCH DAMAGE.
25*1fab62b1SMaxim Sobolev */
26*1fab62b1SMaxim Sobolev
27*1fab62b1SMaxim Sobolev #include <sys/types.h>
28*1fab62b1SMaxim Sobolev #include <sys/socket.h>
29*1fab62b1SMaxim Sobolev #include <sys/un.h>
30*1fab62b1SMaxim Sobolev #include <stdarg.h>
31*1fab62b1SMaxim Sobolev #include <stdbool.h>
32*1fab62b1SMaxim Sobolev
33*1fab62b1SMaxim Sobolev #include "uc_common.h"
34*1fab62b1SMaxim Sobolev #include "t_generic.h"
35*1fab62b1SMaxim Sobolev
36*1fab62b1SMaxim Sobolev int
t_generic(int (* client_func)(int),int (* server_func)(int))37*1fab62b1SMaxim Sobolev t_generic(int (*client_func)(int), int (*server_func)(int))
38*1fab62b1SMaxim Sobolev {
39*1fab62b1SMaxim Sobolev int fd, rv, rv_client;
40*1fab62b1SMaxim Sobolev
41*1fab62b1SMaxim Sobolev switch (uc_client_fork()) {
42*1fab62b1SMaxim Sobolev case 0:
43*1fab62b1SMaxim Sobolev fd = uc_socket_create();
44*1fab62b1SMaxim Sobolev if (fd < 0)
45*1fab62b1SMaxim Sobolev rv = -2;
46*1fab62b1SMaxim Sobolev else {
47*1fab62b1SMaxim Sobolev rv = client_func(fd);
48*1fab62b1SMaxim Sobolev if (uc_socket_close(fd) < 0)
49*1fab62b1SMaxim Sobolev rv = -2;
50*1fab62b1SMaxim Sobolev }
51*1fab62b1SMaxim Sobolev uc_client_exit(rv);
52*1fab62b1SMaxim Sobolev break;
53*1fab62b1SMaxim Sobolev case 1:
54*1fab62b1SMaxim Sobolev fd = uc_socket_create();
55*1fab62b1SMaxim Sobolev if (fd < 0)
56*1fab62b1SMaxim Sobolev rv = -2;
57*1fab62b1SMaxim Sobolev else {
58*1fab62b1SMaxim Sobolev rv = server_func(fd);
59*1fab62b1SMaxim Sobolev rv_client = uc_client_wait();
60*1fab62b1SMaxim Sobolev if (rv == 0 || (rv == -2 && rv_client != 0))
61*1fab62b1SMaxim Sobolev rv = rv_client;
62*1fab62b1SMaxim Sobolev if (uc_socket_close(fd) < 0)
63*1fab62b1SMaxim Sobolev rv = -2;
64*1fab62b1SMaxim Sobolev }
65*1fab62b1SMaxim Sobolev break;
66*1fab62b1SMaxim Sobolev default:
67*1fab62b1SMaxim Sobolev rv = -2;
68*1fab62b1SMaxim Sobolev }
69*1fab62b1SMaxim Sobolev return (rv);
70*1fab62b1SMaxim Sobolev }
71