libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / sys-minix / recvmsg.c
blob057d7f1d579801ecbc09095308a769400a2e561f
1 #include <sys/cdefs.h>
2 #include "namespace.h"
4 #undef NDEBUG
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <sys/ioc_net.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 #include <sys/un.h>
13 #define DEBUG 0
15 static ssize_t _uds_recvmsg_conn(int sock, struct msghdr *msg, int flags);
16 static ssize_t _uds_recvmsg_dgram(int sock, struct msghdr *msg, int flags);
18 ssize_t recvmsg(int sock, struct msghdr *msg, int flags)
20 int r;
21 int uds_sotype;
23 if (msg == NULL) {
24 errno= EFAULT;
25 return -1;
28 r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype);
29 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL)) {
30 if (r == -1) {
31 return r;
34 if (uds_sotype == SOCK_DGRAM) {
35 return _uds_recvmsg_dgram(sock, msg, flags);
36 } else {
37 return _uds_recvmsg_conn(sock, msg, flags);
41 #if DEBUG
42 fprintf(stderr, "recvmsg: not implemented for fd %d\n", sock);
43 #endif
45 errno= ENOSYS;
46 return -1;
49 static ssize_t _uds_recvmsg_conn(int sock, struct msghdr *msg, int flags)
51 int r, rc;
53 if (flags != 0) {
54 #if DEBUG
55 fprintf(stderr, "recvmsg(uds): flags not implemented\n");
56 #endif
57 errno= ENOSYS;
58 return -1;
61 r= readv(sock, msg->msg_iov, msg->msg_iovlen);
63 if (r >= 0 && msg->msg_name && msg->msg_namelen > 0) {
64 getpeername(sock, msg->msg_name, &msg->msg_namelen);
67 /* get control data */
68 if (r >= 0 && msg->msg_control && msg->msg_controllen > 0) {
69 struct msg_control msg_ctrl;
71 memset(&msg_ctrl, '\0', sizeof(struct msg_control));
72 msg_ctrl.msg_controllen = msg->msg_controllen;
73 rc = ioctl(sock, NWIOGUDSCTRL, &msg_ctrl);
74 if (rc == -1) {
75 return rc;
78 if (msg_ctrl.msg_controllen <= msg->msg_controllen) {
79 memcpy(msg->msg_control, msg_ctrl.msg_control,
80 msg_ctrl.msg_controllen);
81 msg->msg_controllen = msg_ctrl.msg_controllen;
85 msg->msg_flags = 0;
87 return r;
90 static ssize_t _uds_recvmsg_dgram(int sock, struct msghdr *msg, int flags)
92 int r, rc;
94 if (flags != 0) {
95 #if DEBUG
96 fprintf(stderr, "recvmsg(uds): flags not implemented\n");
97 #endif
98 errno= ENOSYS;
99 return -1;
102 r= readv(sock, msg->msg_iov, msg->msg_iovlen);
104 if (r >= 0 && msg->msg_name &&
105 msg->msg_namelen >= sizeof(struct sockaddr_un))
107 rc= ioctl(sock, NWIOGUDSFADDR, msg->msg_name);
108 if (rc == -1) {
109 return rc;
111 msg->msg_namelen= sizeof(struct sockaddr_un);
114 /* get control data */
115 if (r >= 0 && msg->msg_control && msg->msg_controllen > 0) {
116 struct msg_control msg_ctrl;
118 memset(&msg_ctrl, '\0', sizeof(struct msg_control));
119 msg_ctrl.msg_controllen = msg->msg_controllen;
120 rc = ioctl(sock, NWIOGUDSCTRL, &msg_ctrl);
121 if (rc == -1) {
122 return rc;
125 if (msg_ctrl.msg_controllen <= msg->msg_controllen) {
126 memcpy(msg->msg_control, msg_ctrl.msg_control,
127 msg_ctrl.msg_controllen);
128 msg->msg_controllen = msg_ctrl.msg_controllen;
132 msg->msg_flags = 0;
134 return r;