Export sys/plan9_helpers.h from glibc (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.19-akaros / sysdeps / akaros / getsockopt.c
1 /* Copyright (c) 2015 Google, Inc.
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details. */
4
5 #include <errno.h>
6 #include <sys/socket.h>
7
8 #include <sys/plan9_helpers.h>
9
10 static int sol_socket_gso(Rock *r, int optname, void *optval, socklen_t *optlen)
11 {
12         switch (optname) {
13                 case (SO_TYPE):
14                         if (*optlen < 4) {
15                                 __set_errno(EINVAL);
16                                 return -1;
17                         }
18                         *(int*)optval = r->stype;
19                         *optlen = 4;
20                         break;
21                 default:
22                         __set_errno(ENOPROTOOPT);
23                         return -1;
24         };
25         return 0;
26 }
27
28 int __getsockopt(int sockfd, int level, int optname, void *optval,
29                  socklen_t *optlen)
30 {
31         Rock *r = _sock_findrock(sockfd, 0);
32         if (!r) {
33                 /* could be EBADF too, we can't tell */
34                 __set_errno(ENOTSOCK);
35                 return -1;
36         }
37         switch (level) {
38                 case (SOL_SOCKET):
39                         return sol_socket_gso(r, optname, optval, optlen);
40                 default:
41                         __set_errno(ENOPROTOOPT);
42                         return -1;
43         };
44 }
45 weak_alias(__getsockopt, getsockopt)