| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "nacl_io/kernel_proxy.h" | 5 #include "nacl_io/kernel_proxy.h" |
| 6 | 6 |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| 11 #include <limits.h> | 11 #include <limits.h> |
| 12 #include <poll.h> | 12 #include <poll.h> |
| 13 #include <pthread.h> | 13 #include <pthread.h> |
| 14 #include <stdio.h> | 14 #include <stdio.h> |
| 15 #include <string.h> | 15 #include <string.h> |
| 16 #include <sys/time.h> | 16 #include <sys/time.h> |
| 17 | 17 |
| 18 #include <iterator> | 18 #include <iterator> |
| 19 #include <string> | 19 #include <string> |
| 20 | 20 |
| 21 #include "nacl_io/host_resolver.h" | 21 #include "nacl_io/host_resolver.h" |
| 22 #include "nacl_io/kernel_handle.h" | 22 #include "nacl_io/kernel_handle.h" |
| 23 #include "nacl_io/kernel_wrap_real.h" | 23 #include "nacl_io/kernel_wrap_real.h" |
| 24 #include "nacl_io/mount.h" | 24 #include "nacl_io/mount.h" |
| 25 #include "nacl_io/mount_dev.h" | 25 #include "nacl_io/mount_dev.h" |
| 26 #include "nacl_io/mount_html5fs.h" | 26 #include "nacl_io/mount_html5fs.h" |
| 27 #include "nacl_io/mount_http.h" | 27 #include "nacl_io/mount_http.h" |
| 28 #include "nacl_io/mount_mem.h" | 28 #include "nacl_io/mount_mem.h" |
| 29 #include "nacl_io/mount_node.h" | 29 #include "nacl_io/mount_node.h" |
| 30 #include "nacl_io/mount_node_tcp.h" |
| 31 #include "nacl_io/mount_node_udp.h" |
| 30 #include "nacl_io/mount_passthrough.h" | 32 #include "nacl_io/mount_passthrough.h" |
| 31 #include "nacl_io/osmman.h" | 33 #include "nacl_io/osmman.h" |
| 32 #include "nacl_io/ossocket.h" | 34 #include "nacl_io/ossocket.h" |
| 33 #include "nacl_io/osstat.h" | 35 #include "nacl_io/osstat.h" |
| 34 #include "nacl_io/path.h" | 36 #include "nacl_io/path.h" |
| 35 #include "nacl_io/pepper_interface.h" | 37 #include "nacl_io/pepper_interface.h" |
| 36 #include "nacl_io/typed_mount_factory.h" | 38 #include "nacl_io/typed_mount_factory.h" |
| 37 #include "sdk_util/auto_lock.h" | 39 #include "sdk_util/auto_lock.h" |
| 38 #include "sdk_util/ref_object.h" | 40 #include "sdk_util/ref_object.h" |
| 39 | 41 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 assert(result == 0); | 77 assert(result == 0); |
| 76 | 78 |
| 77 // Open the first three in order to get STDIN, STDOUT, STDERR | 79 // Open the first three in order to get STDIN, STDOUT, STDERR |
| 78 open("/dev/stdin", O_RDONLY); | 80 open("/dev/stdin", O_RDONLY); |
| 79 open("/dev/stdout", O_WRONLY); | 81 open("/dev/stdout", O_WRONLY); |
| 80 open("/dev/stderr", O_WRONLY); | 82 open("/dev/stderr", O_WRONLY); |
| 81 | 83 |
| 82 #ifdef PROVIDES_SOCKET_API | 84 #ifdef PROVIDES_SOCKET_API |
| 83 host_resolver_.Init(ppapi_); | 85 host_resolver_.Init(ppapi_); |
| 84 #endif | 86 #endif |
| 87 |
| 88 StringMap_t args; |
| 89 socket_mount_.reset(new MountSocket()); |
| 90 socket_mount_->Init(0, args, ppapi); |
| 85 } | 91 } |
| 86 | 92 |
| 87 int KernelProxy::open_resource(const char* path) { | 93 int KernelProxy::open_resource(const char* path) { |
| 88 ScopedMount mnt; | 94 ScopedMount mnt; |
| 89 Path rel; | 95 Path rel; |
| 90 | 96 |
| 91 Error error = AcquireMountAndRelPath(path, &mnt, &rel); | 97 Error error = AcquireMountAndRelPath(path, &mnt, &rel); |
| 92 if (error) { | 98 if (error) { |
| 93 errno = error; | 99 errno = error; |
| 94 return -1; | 100 return -1; |
| (...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 int KernelProxy::bind(int fd, const struct sockaddr* addr, socklen_t len) { | 906 int KernelProxy::bind(int fd, const struct sockaddr* addr, socklen_t len) { |
| 901 if (NULL == addr) { | 907 if (NULL == addr) { |
| 902 errno = EFAULT; | 908 errno = EFAULT; |
| 903 return -1; | 909 return -1; |
| 904 } | 910 } |
| 905 | 911 |
| 906 ScopedKernelHandle handle; | 912 ScopedKernelHandle handle; |
| 907 if (AcquireSocketHandle(fd, &handle) == -1) | 913 if (AcquireSocketHandle(fd, &handle) == -1) |
| 908 return -1; | 914 return -1; |
| 909 | 915 |
| 910 errno = EINVAL; | 916 Error err = handle->socket_node()->Bind(addr, len); |
| 911 return -1; | 917 if (err != 0) { |
| 918 errno = err; |
| 919 return -1; |
| 920 } |
| 921 |
| 922 return 0; |
| 912 } | 923 } |
| 913 | 924 |
| 914 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) { | 925 int KernelProxy::connect(int fd, const struct sockaddr* addr, socklen_t len) { |
| 915 if (NULL == addr) { | 926 if (NULL == addr) { |
| 916 errno = EFAULT; | 927 errno = EFAULT; |
| 917 return -1; | 928 return -1; |
| 918 } | 929 } |
| 919 | 930 |
| 920 ScopedKernelHandle handle; | 931 ScopedKernelHandle handle; |
| 921 if (AcquireSocketHandle(fd, &handle) == -1) | 932 if (AcquireSocketHandle(fd, &handle) == -1) |
| 922 return -1; | 933 return -1; |
| 923 | 934 |
| 924 errno = EACCES; | 935 Error err = handle->socket_node()->Connect(addr, len); |
| 925 return -1; | 936 if (err != 0) { |
| 937 errno = err; |
| 938 return -1; |
| 939 } |
| 940 |
| 941 return 0; |
| 926 } | 942 } |
| 927 | 943 |
| 928 struct hostent* KernelProxy::gethostbyname(const char* name) { | 944 struct hostent* KernelProxy::gethostbyname(const char* name) { |
| 929 return host_resolver_.gethostbyname(name); | 945 return host_resolver_.gethostbyname(name); |
| 930 } | 946 } |
| 931 | 947 |
| 932 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) { | 948 int KernelProxy::getpeername(int fd, struct sockaddr* addr, socklen_t* len) { |
| 933 if (NULL == addr || NULL == len) { | 949 if (NULL == addr || NULL == len) { |
| 934 errno = EFAULT; | 950 errno = EFAULT; |
| 935 return -1; | 951 return -1; |
| 936 } | 952 } |
| 937 | 953 |
| 938 ScopedKernelHandle handle; | 954 ScopedKernelHandle handle; |
| 939 if (AcquireSocketHandle(fd, &handle) == -1) | 955 if (AcquireSocketHandle(fd, &handle) == -1) |
| 940 return -1; | 956 return -1; |
| 941 | 957 |
| 942 errno = EINVAL; | 958 Error err = handle->socket_node()->GetPeerName(addr, len); |
| 943 return -1; | 959 if (err != 0) { |
| 960 errno = err; |
| 961 return -1; |
| 962 } |
| 963 |
| 964 return 0; |
| 944 } | 965 } |
| 945 | 966 |
| 946 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) { | 967 int KernelProxy::getsockname(int fd, struct sockaddr* addr, socklen_t* len) { |
| 947 if (NULL == addr || NULL == len) { | 968 if (NULL == addr || NULL == len) { |
| 948 errno = EFAULT; | 969 errno = EFAULT; |
| 949 return -1; | 970 return -1; |
| 950 } | 971 } |
| 951 | 972 |
| 952 ScopedKernelHandle handle; | 973 ScopedKernelHandle handle; |
| 953 if (AcquireSocketHandle(fd, &handle) == -1) | 974 if (AcquireSocketHandle(fd, &handle) == -1) |
| 954 return -1; | 975 return -1; |
| 955 | 976 |
| 956 errno = EINVAL; | 977 Error err = handle->socket_node()->GetSockName(addr, len); |
| 957 return -1; | 978 if (err != 0) { |
| 979 errno = err; |
| 980 return -1; |
| 981 } |
| 982 |
| 983 return 0; |
| 958 } | 984 } |
| 959 | 985 |
| 960 int KernelProxy::getsockopt(int fd, | 986 int KernelProxy::getsockopt(int fd, |
| 961 int lvl, | 987 int lvl, |
| 962 int optname, | 988 int optname, |
| 963 void* optval, | 989 void* optval, |
| 964 socklen_t* len) { | 990 socklen_t* len) { |
| 965 if (NULL == optval || NULL == len) { | 991 if (NULL == optval || NULL == len) { |
| 966 errno = EFAULT; | 992 errno = EFAULT; |
| 967 return -1; | 993 return -1; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 990 int flags) { | 1016 int flags) { |
| 991 if (NULL == buf) { | 1017 if (NULL == buf) { |
| 992 errno = EFAULT; | 1018 errno = EFAULT; |
| 993 return -1; | 1019 return -1; |
| 994 } | 1020 } |
| 995 | 1021 |
| 996 ScopedKernelHandle handle; | 1022 ScopedKernelHandle handle; |
| 997 if (AcquireSocketHandle(fd, &handle) == -1) | 1023 if (AcquireSocketHandle(fd, &handle) == -1) |
| 998 return -1; | 1024 return -1; |
| 999 | 1025 |
| 1000 errno = EINVAL; | 1026 int out_len = 0; |
| 1001 return -1; | 1027 Error err = handle->socket_node()->Recv(buf, len, flags, &out_len); |
| 1028 if (err != 0) { |
| 1029 errno = err; |
| 1030 return -1; |
| 1031 } |
| 1032 |
| 1033 return static_cast<ssize_t>(out_len); |
| 1002 } | 1034 } |
| 1003 | 1035 |
| 1004 ssize_t KernelProxy::recvfrom(int fd, | 1036 ssize_t KernelProxy::recvfrom(int fd, |
| 1005 void* buf, | 1037 void* buf, |
| 1006 size_t len, | 1038 size_t len, |
| 1007 int flags, | 1039 int flags, |
| 1008 struct sockaddr* addr, | 1040 struct sockaddr* addr, |
| 1009 socklen_t* addrlen) { | 1041 socklen_t* addrlen) { |
| 1010 // According to the manpage, recvfrom with a null addr is identical to recv. | 1042 // According to the manpage, recvfrom with a null addr is identical to recv. |
| 1011 if (NULL == addr) { | 1043 if (NULL == addr) { |
| 1012 return recv(fd, buf, len, flags); | 1044 return recv(fd, buf, len, flags); |
| 1013 } | 1045 } |
| 1014 | 1046 |
| 1015 if (NULL == buf || NULL == addrlen) { | 1047 if (NULL == buf || NULL == addrlen) { |
| 1016 errno = EFAULT; | 1048 errno = EFAULT; |
| 1017 return -1; | 1049 return -1; |
| 1018 } | 1050 } |
| 1019 | 1051 |
| 1020 ScopedKernelHandle handle; | 1052 ScopedKernelHandle handle; |
| 1021 if (AcquireSocketHandle(fd, &handle) == -1) | 1053 if (AcquireSocketHandle(fd, &handle) == -1) |
| 1022 return -1; | 1054 return -1; |
| 1023 | 1055 |
| 1024 errno = EINVAL; | 1056 int out_len = 0; |
| 1025 return -1; | 1057 Error err = handle->socket_node()->RecvFrom(buf, |
| 1058 len, |
| 1059 flags, |
| 1060 addr, |
| 1061 addrlen, |
| 1062 &out_len); |
| 1063 if (err != 0) { |
| 1064 errno = err; |
| 1065 return -1; |
| 1066 } |
| 1067 |
| 1068 return static_cast<ssize_t>(out_len); |
| 1026 } | 1069 } |
| 1027 | 1070 |
| 1028 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) { | 1071 ssize_t KernelProxy::recvmsg(int fd, struct msghdr* msg, int flags) { |
| 1029 if (NULL == msg ) { | 1072 if (NULL == msg ) { |
| 1030 errno = EFAULT; | 1073 errno = EFAULT; |
| 1031 return -1; | 1074 return -1; |
| 1032 } | 1075 } |
| 1033 | 1076 |
| 1034 ScopedKernelHandle handle; | 1077 ScopedKernelHandle handle; |
| 1035 if (AcquireSocketHandle(fd, &handle) == -1) | 1078 if (AcquireSocketHandle(fd, &handle) == -1) |
| 1036 return -1; | 1079 return -1; |
| 1037 | 1080 |
| 1038 errno = EOPNOTSUPP; | 1081 errno = EOPNOTSUPP; |
| 1039 return -1; | 1082 return -1; |
| 1040 } | 1083 } |
| 1041 | 1084 |
| 1042 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) { | 1085 ssize_t KernelProxy::send(int fd, const void* buf, size_t len, int flags) { |
| 1043 if (NULL == buf) { | 1086 if (NULL == buf) { |
| 1044 errno = EFAULT; | 1087 errno = EFAULT; |
| 1045 return -1; | 1088 return -1; |
| 1046 } | 1089 } |
| 1047 | 1090 |
| 1048 ScopedKernelHandle handle; | 1091 ScopedKernelHandle handle; |
| 1049 if (AcquireSocketHandle(fd, &handle) == -1) | 1092 if (AcquireSocketHandle(fd, &handle) == -1) |
| 1050 return -1; | 1093 return -1; |
| 1051 | 1094 |
| 1052 errno = EINVAL; | 1095 int out_len = 0; |
| 1053 return -1; | 1096 Error err = handle->socket_node()->Send(buf, len, flags, &out_len); |
| 1097 if (err != 0) { |
| 1098 errno = err; |
| 1099 return -1; |
| 1100 } |
| 1101 |
| 1102 return static_cast<ssize_t>(out_len); |
| 1054 } | 1103 } |
| 1055 | 1104 |
| 1056 ssize_t KernelProxy::sendto(int fd, | 1105 ssize_t KernelProxy::sendto(int fd, |
| 1057 const void* buf, | 1106 const void* buf, |
| 1058 size_t len, | 1107 size_t len, |
| 1059 int flags, | 1108 int flags, |
| 1060 const struct sockaddr* addr, | 1109 const struct sockaddr* addr, |
| 1061 socklen_t addrlen) { | 1110 socklen_t addrlen) { |
| 1062 // According to the manpage, sendto with a null addr is identical to send. | 1111 // According to the manpage, sendto with a null addr is identical to send. |
| 1063 if (NULL == addr) { | 1112 if (NULL == addr) { |
| 1064 return send(fd, buf, len, flags); | 1113 return send(fd, buf, len, flags); |
| 1065 } | 1114 } |
| 1066 | 1115 |
| 1067 if (NULL == buf) { | 1116 if (NULL == buf) { |
| 1068 errno = EFAULT; | 1117 errno = EFAULT; |
| 1069 return -1; | 1118 return -1; |
| 1070 } | 1119 } |
| 1071 | 1120 |
| 1072 ScopedKernelHandle handle; | 1121 ScopedKernelHandle handle; |
| 1073 if (AcquireSocketHandle(fd, &handle) == -1) | 1122 if (AcquireSocketHandle(fd, &handle) == -1) |
| 1074 return -1; | 1123 return -1; |
| 1075 | 1124 |
| 1076 errno = EINVAL; | 1125 int out_len = 0; |
| 1077 return -1; | 1126 Error err = |
| 1127 handle->socket_node()->SendTo(buf, len, flags, addr, addrlen, &out_len); |
| 1128 |
| 1129 if (err != 0) { |
| 1130 errno = err; |
| 1131 return -1; |
| 1132 } |
| 1133 |
| 1134 return static_cast<ssize_t>(out_len); |
| 1078 } | 1135 } |
| 1079 | 1136 |
| 1080 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) { | 1137 ssize_t KernelProxy::sendmsg(int fd, const struct msghdr* msg, int flags) { |
| 1081 if (NULL == msg) { | 1138 if (NULL == msg) { |
| 1082 errno = EFAULT; | 1139 errno = EFAULT; |
| 1083 return -1; | 1140 return -1; |
| 1084 } | 1141 } |
| 1085 | 1142 |
| 1086 ScopedKernelHandle handle; | 1143 ScopedKernelHandle handle; |
| 1087 if (AcquireSocketHandle(fd, &handle) == -1) | 1144 if (AcquireSocketHandle(fd, &handle) == -1) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1107 | 1164 |
| 1108 errno = EINVAL; | 1165 errno = EINVAL; |
| 1109 return -1; | 1166 return -1; |
| 1110 } | 1167 } |
| 1111 | 1168 |
| 1112 int KernelProxy::shutdown(int fd, int how) { | 1169 int KernelProxy::shutdown(int fd, int how) { |
| 1113 ScopedKernelHandle handle; | 1170 ScopedKernelHandle handle; |
| 1114 if (AcquireSocketHandle(fd, &handle) == -1) | 1171 if (AcquireSocketHandle(fd, &handle) == -1) |
| 1115 return -1; | 1172 return -1; |
| 1116 | 1173 |
| 1117 errno = EINVAL; | 1174 Error err = handle->socket_node()->Shutdown(how); |
| 1118 return -1; | 1175 if (err != 0) { |
| 1176 errno = err; |
| 1177 return -1; |
| 1178 } |
| 1179 |
| 1180 return 0; |
| 1119 } | 1181 } |
| 1120 | 1182 |
| 1121 int KernelProxy::socket(int domain, int type, int protocol) { | 1183 int KernelProxy::socket(int domain, int type, int protocol) { |
| 1122 if (AF_INET != domain && AF_INET6 != domain) { | 1184 if (AF_INET != domain && AF_INET6 != domain) { |
| 1123 errno = EAFNOSUPPORT; | 1185 errno = EAFNOSUPPORT; |
| 1124 return -1; | 1186 return -1; |
| 1125 } | 1187 } |
| 1126 | 1188 |
| 1127 if (SOCK_STREAM != type && SOCK_DGRAM != type) { | 1189 MountNodeSocket* sock = NULL; |
| 1128 errno = EPROTONOSUPPORT; | 1190 switch (type) { |
| 1129 return -1; | 1191 case SOCK_DGRAM: |
| 1192 sock = new MountNodeUDP(socket_mount_.get()); |
| 1193 break; |
| 1194 |
| 1195 case SOCK_STREAM: |
| 1196 sock = new MountNodeTCP(socket_mount_.get()); |
| 1197 break; |
| 1198 |
| 1199 default: |
| 1200 errno = EPROTONOSUPPORT; |
| 1201 return -1; |
| 1130 } | 1202 } |
| 1131 | 1203 |
| 1132 errno = EACCES; | 1204 ScopedMountNode node(sock); |
| 1133 return -1; | 1205 if (sock->Init(S_IREAD | S_IWRITE) == 0) { |
| 1206 ScopedKernelHandle handle(new KernelHandle(socket_mount_, node)); |
| 1207 return AllocateFD(handle); |
| 1208 } |
| 1209 |
| 1210 // If we failed to init, assume we don't have access. |
| 1211 return EACCES; |
| 1134 } | 1212 } |
| 1135 | 1213 |
| 1136 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) { | 1214 int KernelProxy::socketpair(int domain, int type, int protocol, int* sv) { |
| 1137 if (NULL == sv) { | 1215 if (NULL == sv) { |
| 1138 errno = EFAULT; | 1216 errno = EFAULT; |
| 1139 return -1; | 1217 return -1; |
| 1140 } | 1218 } |
| 1141 | 1219 |
| 1142 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support | 1220 // Catch-22: We don't support AF_UNIX, but any other AF doesn't support |
| 1143 // socket pairs. Thus, this function always fails. | 1221 // socket pairs. Thus, this function always fails. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1168 errno = ENOTSOCK; | 1246 errno = ENOTSOCK; |
| 1169 return -1; | 1247 return -1; |
| 1170 } | 1248 } |
| 1171 | 1249 |
| 1172 return 0; | 1250 return 0; |
| 1173 } | 1251 } |
| 1174 | 1252 |
| 1175 #endif // PROVIDES_SOCKET_API | 1253 #endif // PROVIDES_SOCKET_API |
| 1176 | 1254 |
| 1177 } // namespace_nacl_io | 1255 } // namespace_nacl_io |
| OLD | NEW |