Revert "Modify the SMACK label for SDB shell."
[sdk/target/sdbd.git] / src / socket_local_server.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 // libs/cutils/socket_local_server.c
17
18 #include "sockets.h"
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <stddef.h>
25
26 #ifdef HAVE_WINSOCK
27
28 int socket_local_server(const char *name, int namespaceId, int type)
29 {
30     errno = ENOSYS;
31     return -1;
32 }
33
34 #else /* !HAVE_WINSOCK */
35
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <sys/select.h>
39 #include <sys/types.h>
40 #include <netinet/in.h>
41
42 #include "socket_local.h"
43
44 #define LISTEN_BACKLOG 4
45
46
47 /**
48  * Binds a pre-created socket(AF_LOCAL) 's' to 'name'
49  * returns 's' on success, -1 on fail
50  *
51  * Does not call listen()
52  */
53 int socket_local_server_bind(int s, const char *name, int namespaceId)
54 {
55     struct sockaddr_un addr;
56     socklen_t alen;
57     int n;
58     int err;
59
60     err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
61
62     if (err < 0) {
63         return -1;
64     }
65
66     /* basically: if this is a filesystem path, unlink first */
67 #ifndef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE
68     if (1) {
69 #else
70     if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED
71         || namespaceId == ANDROID_SOCKET_NAMESPACE_FILESYSTEM) {
72 #endif
73         /*ignore ENOENT*/
74         unlink(addr.sun_path);
75     }
76
77     n = 1;
78     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0 ) {
79         return -1;
80     }
81
82     if(bind(s, (struct sockaddr *) &addr, alen) < 0) {
83         return -1;
84     }
85
86     return s;
87
88 }
89
90
91 /** Open a server-side UNIX domain datagram socket in the Linux non-filesystem 
92  *  namespace
93  *
94  *  Returns fd on success, -1 on fail
95  */
96
97 int socket_local_server(const char *name, int namespace, int type)
98 {
99     int err;
100     int s;
101     
102     s = socket(AF_LOCAL, type, 0);
103     if (s < 0) return -1;
104
105     err = socket_local_server_bind(s, name, namespace);
106
107     if (err < 0) {
108         close(s);
109         return -1;
110     }
111
112     if (type == SOCK_STREAM) {
113         int ret;
114
115         ret = listen(s, LISTEN_BACKLOG);
116
117         if (ret < 0) {
118             close(s);
119             return -1;
120         }
121     }
122
123     return s;
124 }
125
126 #endif /* !HAVE_WINSOCK */