2 * Copyright (C) 2006, The Android Open Source Project
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.
18 #include <netinet/in.h>
22 #include <sys/socket.h>
24 #include <sys/select.h>
25 #include <sys/types.h>
28 #include "osi/include/socket_utils/sockets.h"
29 #include "osi/include/socket_utils/socket_local.h"
31 #define LISTEN_BACKLOG 4
33 /* Only the bottom bits are really the socket type; there are flags too. */
34 #define SOCK_TYPE_MASK 0xf
37 * Binds a pre-created socket(AF_LOCAL) 's' to 'name'
38 * returns 's' on success, -1 on fail
40 * Does not call listen()
42 int osi_socket_local_server_bind(int s, const char *name, int namespaceId) {
43 struct sockaddr_un addr;
48 err = osi_socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
54 /* basically: if this is a filesystem path, unlink first */
55 #if !defined(__linux__)
58 if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED ||
59 namespaceId == ANDROID_SOCKET_NAMESPACE_FILESYSTEM) {
62 unlink(addr.sun_path);
66 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
68 if (bind(s, (struct sockaddr *)&addr, alen) < 0) {
75 /** Open a server-side UNIX domain datagram socket in the Linux non-filesystem
78 * Returns fd on success, -1 on fail
80 int osi_socket_local_server(const char *name, int namespace, int type) {
84 s = socket(AF_LOCAL, type, 0);
87 err = osi_socket_local_server_bind(s, name, namespace);
94 if ((type & SOCK_TYPE_MASK) == SOCK_STREAM) {
97 ret = listen(s, LISTEN_BACKLOG);