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