2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 // libs/cutils/socket_local_server.c
28 int socket_local_server(const char *name, int namespaceId, int type)
34 #else /* !HAVE_WINSOCK */
36 #include <sys/socket.h>
38 #include <sys/select.h>
39 #include <sys/types.h>
40 #include <netinet/in.h>
42 #include "socket_local.h"
44 #define LISTEN_BACKLOG 4
48 * Binds a pre-created socket(AF_LOCAL) 's' to 'name'
49 * returns 's' on success, -1 on fail
51 * Does not call listen()
53 int socket_local_server_bind(int s, const char *name, int namespaceId)
55 struct sockaddr_un addr;
60 err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
66 /* basically: if this is a filesystem path, unlink first */
67 #ifndef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
70 if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED
71 || namespaceId == ANDROID_SOCKET_NAMESPACE_FILESYSTEM) {
74 unlink(addr.sun_path);
78 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0 ) {
82 if(bind(s, (struct sockaddr *) &addr, alen) < 0) {
91 /** Open a server-side UNIX domain datagram socket in the Linux non-filesystem
94 * Returns fd on success, -1 on fail
97 int socket_local_server(const char *name, int namespace, int type)
102 s = socket(AF_LOCAL, type, 0);
103 if (s < 0) return -1;
105 err = socket_local_server_bind(s, name, namespace);
112 if (type == SOCK_STREAM) {
115 ret = listen(s, LISTEN_BACKLOG);
126 #endif /* !HAVE_WINSOCK */