Use weak __thread declarations in parlib-compat (XCC)
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.19-akaros / sysdeps / akaros / listen.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 /* posix */
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include <signal.h>
19
20 /* socket extensions */
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <sys/un.h>
25
26 #include <sys/plan9_helpers.h>
27
28 /* Prepare to accept connections on socket FD.
29    N connection requests will be queued before further requests are refused.
30    Returns 0 on success, -1 for errors.  */
31 int __listen(int fd, int backlog)
32 {
33         Rock *r;
34         int n, cfd;
35         char msg[128];
36         struct sockaddr_in *lip;
37         struct sockaddr_un *lunix;
38
39         r = _sock_findrock(fd, 0);
40         if (r == 0) {
41                 errno = ENOTSOCK;
42                 return -1;
43         }
44
45         switch (r->domain) {
46                 case PF_INET:
47                         cfd = _sock_open_ctlfd(r);
48                         if (cfd < 0) {
49                                 errno = EBADF;
50                                 return -1;
51                         }
52                         lip = (struct sockaddr_in *)&r->addr;
53                         if (lip->sin_port >= 0) {
54                                 if (write(cfd, "bind 0", 6) < 0) {
55                                         errno = EINVAL; //EGREG;
56                                         close(cfd);
57                                         return -1;
58                                 }
59                                 snprintf(msg, sizeof msg, "announce %d", ntohs(lip->sin_port));
60                         } else
61                                 strcpy(msg, "announce *");
62                         n = write(cfd, msg, strlen(msg));
63                         if (n < 0) {
64                                 errno = EOPNOTSUPP;     /* Improve error reporting!!! */
65                                 close(cfd);
66                                 return -1;
67                         }
68                         close(cfd);
69                         return 0;
70                 case PF_UNIX:
71                         if (r->other < 0) {
72                                 errno = EINVAL; //EGREG;
73                                 return -1;
74                         }
75                         lunix = (struct sockaddr_un *)&r->addr;
76                         if (_sock_srv(lunix->sun_path, r->other) < 0) {
77                                 r->other = -1;
78                                 return -1;
79                         }
80                         r->other = -1;
81                         return 0;
82                 default:
83                         errno = EAFNOSUPPORT;
84                         return -1;
85         }
86 }
87
88 weak_alias(__listen, listen)