xref: /freebsd-src/tools/regression/sockets/unix_socket/unix_socket.c (revision f305e6eaf8d98d9c2ca7ca97791e84521f25b801)
13d11b6c8SRobert Watson /*-
23d11b6c8SRobert Watson  * Copyright (c) 2006 Robert N. M. Watson
33d11b6c8SRobert Watson  * All rights reserved.
43d11b6c8SRobert Watson  *
53d11b6c8SRobert Watson  * Redistribution and use in source and binary forms, with or without
63d11b6c8SRobert Watson  * modification, are permitted provided that the following conditions
73d11b6c8SRobert Watson  * are met:
83d11b6c8SRobert Watson  * 1. Redistributions of source code must retain the above copyright
93d11b6c8SRobert Watson  *    notice, this list of conditions and the following disclaimer.
103d11b6c8SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
113d11b6c8SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
123d11b6c8SRobert Watson  *    documentation and/or other materials provided with the distribution.
133d11b6c8SRobert Watson  *
143d11b6c8SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153d11b6c8SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163d11b6c8SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173d11b6c8SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183d11b6c8SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193d11b6c8SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203d11b6c8SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213d11b6c8SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223d11b6c8SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233d11b6c8SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243d11b6c8SRobert Watson  * SUCH DAMAGE.
253d11b6c8SRobert Watson  *
263d11b6c8SRobert Watson  * $FreeBSD$
273d11b6c8SRobert Watson  */
283d11b6c8SRobert Watson 
293d11b6c8SRobert Watson /*
303d11b6c8SRobert Watson  * Simple UNIX domain socket regression test: create and tear down various
313d11b6c8SRobert Watson  * supported and unsupported socket types.
323d11b6c8SRobert Watson  */
333d11b6c8SRobert Watson 
343d11b6c8SRobert Watson #include <sys/types.h>
353d11b6c8SRobert Watson #include <sys/socket.h>
363d11b6c8SRobert Watson #include <sys/un.h>
373d11b6c8SRobert Watson 
383d11b6c8SRobert Watson #include <err.h>
393d11b6c8SRobert Watson #include <errno.h>
403d11b6c8SRobert Watson #include <unistd.h>
413d11b6c8SRobert Watson 
423d11b6c8SRobert Watson int
43*f305e6eaSEnji Cooper main(void)
443d11b6c8SRobert Watson {
453d11b6c8SRobert Watson 	int sock, socks[2];
463d11b6c8SRobert Watson 
473d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
483d11b6c8SRobert Watson 	if (sock < 0)
493d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_STREAM, 0)");
503d11b6c8SRobert Watson 	close(sock);
513d11b6c8SRobert Watson 
523d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
533d11b6c8SRobert Watson 	if (sock < 0)
543d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_DGRAM, 0)");
553d11b6c8SRobert Watson 	close(sock);
563d11b6c8SRobert Watson 
573d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_RAW, 0);
583d11b6c8SRobert Watson 	if (sock >= 0) {
593d11b6c8SRobert Watson 		close(sock);
603d11b6c8SRobert Watson 		errx(-1, "socket(PF_LOCAL, SOCK_RAW, 0) returned %d", sock);
613d11b6c8SRobert Watson 	}
623d11b6c8SRobert Watson 	if (errno != EPROTONOSUPPORT)
633d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_RAW, 0)");
643d11b6c8SRobert Watson 
653d11b6c8SRobert Watson 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) < 0)
663d11b6c8SRobert Watson 		err(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks)");
673d11b6c8SRobert Watson 	if (socks[0] < 0)
683d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [0] < 0");
693d11b6c8SRobert Watson 	if (socks[1] < 0)
703d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [1] < 1");
713d11b6c8SRobert Watson 	close(socks[0]);
723d11b6c8SRobert Watson 	close(socks[1]);
733d11b6c8SRobert Watson 
743d11b6c8SRobert Watson 	if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) < 0)
753d11b6c8SRobert Watson 		err(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks)");
763d11b6c8SRobert Watson 	if (socks[0] < 0)
773d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [0] < 0");
783d11b6c8SRobert Watson 	if (socks[1] < 0)
793d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [1] < 1");
803d11b6c8SRobert Watson 	close(socks[0]);
813d11b6c8SRobert Watson 	close(socks[1]);
823d11b6c8SRobert Watson 
833d11b6c8SRobert Watson 	return (0);
843d11b6c8SRobert Watson }
85