xref: /freebsd/tools/regression/sockets/unix_socket/unix_socket.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
273d11b6c8SRobert Watson /*
283d11b6c8SRobert Watson  * Simple UNIX domain socket regression test: create and tear down various
293d11b6c8SRobert Watson  * supported and unsupported socket types.
303d11b6c8SRobert Watson  */
313d11b6c8SRobert Watson 
323d11b6c8SRobert Watson #include <sys/types.h>
333d11b6c8SRobert Watson #include <sys/socket.h>
343d11b6c8SRobert Watson #include <sys/un.h>
353d11b6c8SRobert Watson 
363d11b6c8SRobert Watson #include <err.h>
373d11b6c8SRobert Watson #include <errno.h>
383d11b6c8SRobert Watson #include <unistd.h>
393d11b6c8SRobert Watson 
403d11b6c8SRobert Watson int
main(void)41f305e6eaSEnji Cooper main(void)
423d11b6c8SRobert Watson {
433d11b6c8SRobert Watson 	int sock, socks[2];
443d11b6c8SRobert Watson 
453d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_STREAM, 0);
463d11b6c8SRobert Watson 	if (sock < 0)
473d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_STREAM, 0)");
483d11b6c8SRobert Watson 	close(sock);
493d11b6c8SRobert Watson 
503d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
513d11b6c8SRobert Watson 	if (sock < 0)
523d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_DGRAM, 0)");
533d11b6c8SRobert Watson 	close(sock);
543d11b6c8SRobert Watson 
553d11b6c8SRobert Watson 	sock = socket(PF_LOCAL, SOCK_RAW, 0);
563d11b6c8SRobert Watson 	if (sock >= 0) {
573d11b6c8SRobert Watson 		close(sock);
583d11b6c8SRobert Watson 		errx(-1, "socket(PF_LOCAL, SOCK_RAW, 0) returned %d", sock);
593d11b6c8SRobert Watson 	}
60*c084ac28SEnji Cooper 	if (errno != EPROTOTYPE)
613d11b6c8SRobert Watson 		err(-1, "socket(PF_LOCAL, SOCK_RAW, 0)");
623d11b6c8SRobert Watson 
633d11b6c8SRobert Watson 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) < 0)
643d11b6c8SRobert Watson 		err(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks)");
653d11b6c8SRobert Watson 	if (socks[0] < 0)
663d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [0] < 0");
673d11b6c8SRobert Watson 	if (socks[1] < 0)
683d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_STREAM, 0, socks) [1] < 1");
693d11b6c8SRobert Watson 	close(socks[0]);
703d11b6c8SRobert Watson 	close(socks[1]);
713d11b6c8SRobert Watson 
723d11b6c8SRobert Watson 	if (socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) < 0)
733d11b6c8SRobert Watson 		err(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks)");
743d11b6c8SRobert Watson 	if (socks[0] < 0)
753d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [0] < 0");
763d11b6c8SRobert Watson 	if (socks[1] < 0)
773d11b6c8SRobert Watson 		errx(-1, "socketpair(PF_LOCAL, SOCK_DGRAM, 0, socks) [1] < 1");
783d11b6c8SRobert Watson 	close(socks[0]);
793d11b6c8SRobert Watson 	close(socks[1]);
803d11b6c8SRobert Watson 
813d11b6c8SRobert Watson 	return (0);
823d11b6c8SRobert Watson }
83