xref: /minix3/minix/lib/libc/sys/shutdown.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <sys/cdefs.h>
2*433d6423SLionel Sambuc #include "namespace.h"
3*433d6423SLionel Sambuc 
4*433d6423SLionel Sambuc #include <errno.h>
5*433d6423SLionel Sambuc #include <stdio.h>
6*433d6423SLionel Sambuc #include <sys/ioctl.h>
7*433d6423SLionel Sambuc #include <sys/socket.h>
8*433d6423SLionel Sambuc #include <sys/un.h>
9*433d6423SLionel Sambuc 
10*433d6423SLionel Sambuc #include <net/gen/in.h>
11*433d6423SLionel Sambuc #include <net/gen/tcp.h>
12*433d6423SLionel Sambuc #include <net/gen/tcp_io.h>
13*433d6423SLionel Sambuc 
14*433d6423SLionel Sambuc #define DEBUG 0
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc static int _tcp_shutdown(int sock, int how);
17*433d6423SLionel Sambuc static int _uds_shutdown(int sock, int how);
18*433d6423SLionel Sambuc 
19*433d6423SLionel Sambuc int shutdown(int sock, int how)
20*433d6423SLionel Sambuc {
21*433d6423SLionel Sambuc 	int r;
22*433d6423SLionel Sambuc 	struct sockaddr_un uds_addr;
23*433d6423SLionel Sambuc 	nwio_tcpconf_t tcpconf;
24*433d6423SLionel Sambuc 
25*433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
26*433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
27*433d6423SLionel Sambuc 	{
28*433d6423SLionel Sambuc 		if (r == -1)
29*433d6423SLionel Sambuc 		{
30*433d6423SLionel Sambuc 			/* Bad file descriptor */
31*433d6423SLionel Sambuc 			return -1;
32*433d6423SLionel Sambuc 		}
33*433d6423SLionel Sambuc 		return _tcp_shutdown(sock, how);
34*433d6423SLionel Sambuc 	}
35*433d6423SLionel Sambuc 
36*433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGUDSADDR, &uds_addr);
37*433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
38*433d6423SLionel Sambuc 	{
39*433d6423SLionel Sambuc 		if (r == -1)
40*433d6423SLionel Sambuc 		{
41*433d6423SLionel Sambuc 			/* Bad file descriptor */
42*433d6423SLionel Sambuc 			return -1;
43*433d6423SLionel Sambuc 		}
44*433d6423SLionel Sambuc 		return _uds_shutdown(sock, how);
45*433d6423SLionel Sambuc 	}
46*433d6423SLionel Sambuc 
47*433d6423SLionel Sambuc #if DEBUG
48*433d6423SLionel Sambuc 	fprintf(stderr, "shutdown: not implemented for fd %d\n", sock);
49*433d6423SLionel Sambuc #endif
50*433d6423SLionel Sambuc 	errno= ENOSYS;
51*433d6423SLionel Sambuc 	return -1;
52*433d6423SLionel Sambuc }
53*433d6423SLionel Sambuc 
54*433d6423SLionel Sambuc static int _tcp_shutdown(int sock, int how)
55*433d6423SLionel Sambuc {
56*433d6423SLionel Sambuc 	int r;
57*433d6423SLionel Sambuc 
58*433d6423SLionel Sambuc 	if (how == SHUT_WR || how == SHUT_RDWR)
59*433d6423SLionel Sambuc 	{
60*433d6423SLionel Sambuc 		r= ioctl(sock, NWIOTCPSHUTDOWN, NULL);
61*433d6423SLionel Sambuc 		if (r == -1)
62*433d6423SLionel Sambuc 			return -1;
63*433d6423SLionel Sambuc 		if (how == SHUT_WR)
64*433d6423SLionel Sambuc 			return 0;
65*433d6423SLionel Sambuc 	}
66*433d6423SLionel Sambuc 
67*433d6423SLionel Sambuc 	/* We can't shutdown the read side of the socket. */
68*433d6423SLionel Sambuc 	errno= ENOSYS;
69*433d6423SLionel Sambuc 	return -1;
70*433d6423SLionel Sambuc }
71*433d6423SLionel Sambuc 
72*433d6423SLionel Sambuc static int _uds_shutdown(int sock, int how)
73*433d6423SLionel Sambuc {
74*433d6423SLionel Sambuc 	return ioctl(sock, NWIOSUDSSHUT, &how);
75*433d6423SLionel Sambuc }
76