1433d6423SLionel Sambuc #include <sys/cdefs.h>
2433d6423SLionel Sambuc #include "namespace.h"
3c38dbb97SDavid van Moolenbroek #include <lib.h>
4433d6423SLionel Sambuc
5c38dbb97SDavid van Moolenbroek #include <string.h>
6433d6423SLionel Sambuc #include <errno.h>
7433d6423SLionel Sambuc #include <stdio.h>
8433d6423SLionel Sambuc #include <sys/ioctl.h>
9433d6423SLionel Sambuc #include <sys/socket.h>
10433d6423SLionel Sambuc #include <sys/un.h>
11433d6423SLionel Sambuc
12433d6423SLionel Sambuc #include <net/gen/in.h>
13433d6423SLionel Sambuc #include <net/gen/tcp.h>
14433d6423SLionel Sambuc #include <net/gen/tcp_io.h>
15433d6423SLionel Sambuc
16433d6423SLionel Sambuc #define DEBUG 0
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc static int _tcp_shutdown(int sock, int how);
19433d6423SLionel Sambuc static int _uds_shutdown(int sock, int how);
20433d6423SLionel Sambuc
21c38dbb97SDavid van Moolenbroek /*
22c38dbb97SDavid van Moolenbroek * Shut down socket send and receive operations.
23c38dbb97SDavid van Moolenbroek */
24c38dbb97SDavid van Moolenbroek static int
__shutdown(int fd,int how)25c38dbb97SDavid van Moolenbroek __shutdown(int fd, int how)
26c38dbb97SDavid van Moolenbroek {
27c38dbb97SDavid van Moolenbroek message m;
28c38dbb97SDavid van Moolenbroek
29c38dbb97SDavid van Moolenbroek memset(&m, 0, sizeof(m));
30c38dbb97SDavid van Moolenbroek m.m_lc_vfs_shutdown.fd = fd;
31c38dbb97SDavid van Moolenbroek m.m_lc_vfs_shutdown.how = how;
32c38dbb97SDavid van Moolenbroek
33c38dbb97SDavid van Moolenbroek return _syscall(VFS_PROC_NR, VFS_SHUTDOWN, &m);
34c38dbb97SDavid van Moolenbroek }
35c38dbb97SDavid van Moolenbroek
shutdown(int sock,int how)36433d6423SLionel Sambuc int shutdown(int sock, int how)
37433d6423SLionel Sambuc {
38433d6423SLionel Sambuc int r;
39433d6423SLionel Sambuc struct sockaddr_un uds_addr;
40433d6423SLionel Sambuc nwio_tcpconf_t tcpconf;
41433d6423SLionel Sambuc
42c38dbb97SDavid van Moolenbroek r = __shutdown(sock, how);
43*84ed480eSDavid van Moolenbroek if (r != -1 || (errno != ENOTSOCK && errno != ENOSYS))
44c38dbb97SDavid van Moolenbroek return r;
45c38dbb97SDavid van Moolenbroek
46433d6423SLionel Sambuc r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
47433d6423SLionel Sambuc if (r != -1 || errno != ENOTTY)
48433d6423SLionel Sambuc {
49433d6423SLionel Sambuc if (r == -1)
50433d6423SLionel Sambuc {
51433d6423SLionel Sambuc /* Bad file descriptor */
52433d6423SLionel Sambuc return -1;
53433d6423SLionel Sambuc }
54433d6423SLionel Sambuc return _tcp_shutdown(sock, how);
55433d6423SLionel Sambuc }
56433d6423SLionel Sambuc
57433d6423SLionel Sambuc r= ioctl(sock, NWIOGUDSADDR, &uds_addr);
58433d6423SLionel Sambuc if (r != -1 || errno != ENOTTY)
59433d6423SLionel Sambuc {
60433d6423SLionel Sambuc if (r == -1)
61433d6423SLionel Sambuc {
62433d6423SLionel Sambuc /* Bad file descriptor */
63433d6423SLionel Sambuc return -1;
64433d6423SLionel Sambuc }
65433d6423SLionel Sambuc return _uds_shutdown(sock, how);
66433d6423SLionel Sambuc }
67433d6423SLionel Sambuc
68c38dbb97SDavid van Moolenbroek errno = ENOTSOCK;
69433d6423SLionel Sambuc return -1;
70433d6423SLionel Sambuc }
71433d6423SLionel Sambuc
_tcp_shutdown(int sock,int how)72433d6423SLionel Sambuc static int _tcp_shutdown(int sock, int how)
73433d6423SLionel Sambuc {
74433d6423SLionel Sambuc int r;
75433d6423SLionel Sambuc
76433d6423SLionel Sambuc if (how == SHUT_WR || how == SHUT_RDWR)
77433d6423SLionel Sambuc {
78433d6423SLionel Sambuc r= ioctl(sock, NWIOTCPSHUTDOWN, NULL);
79433d6423SLionel Sambuc if (r == -1)
80433d6423SLionel Sambuc return -1;
81433d6423SLionel Sambuc if (how == SHUT_WR)
82433d6423SLionel Sambuc return 0;
83433d6423SLionel Sambuc }
84433d6423SLionel Sambuc
85433d6423SLionel Sambuc /* We can't shutdown the read side of the socket. */
86433d6423SLionel Sambuc errno= ENOSYS;
87433d6423SLionel Sambuc return -1;
88433d6423SLionel Sambuc }
89433d6423SLionel Sambuc
_uds_shutdown(int sock,int how)90433d6423SLionel Sambuc static int _uds_shutdown(int sock, int how)
91433d6423SLionel Sambuc {
92433d6423SLionel Sambuc return ioctl(sock, NWIOSUDSSHUT, &how);
93433d6423SLionel Sambuc }
94