1 /*
2 * Copyright 2006 The Android Open Source Project
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
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/select.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25
26 #include "osi/include/osi.h"
27 #include "osi/include/socket_utils/socket_local.h"
28 #include "osi/include/socket_utils/sockets.h"
29
30 #define LISTEN_BACKLOG 4
31
32 /* Documented in header file. */
osi_socket_make_sockaddr_un(const char * name,int namespaceId,struct sockaddr_un * p_addr,socklen_t * alen)33 int osi_socket_make_sockaddr_un(const char* name, int namespaceId, struct sockaddr_un* p_addr,
34 socklen_t* alen) {
35 memset(p_addr, 0, sizeof(*p_addr));
36 size_t namelen;
37
38 switch (namespaceId) {
39 case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
40 #if defined(__linux__)
41 namelen = strlen(name);
42
43 // Test with length +1 for the *initial* '\0'.
44 if ((namelen + 1) > sizeof(p_addr->sun_path)) {
45 goto error;
46 }
47
48 /*
49 * Note: The path in this case is *not* supposed to be
50 * '\0'-terminated. ("man 7 unix" for the gory details.)
51 */
52
53 p_addr->sun_path[0] = 0;
54 memcpy(p_addr->sun_path + 1, name, namelen);
55 #else
56 /* this OS doesn't have the Linux abstract namespace */
57
58 namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
59 /* unix_path_max appears to be missing on linux */
60 if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
61 goto error;
62 }
63
64 strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
65 strcat(p_addr->sun_path, name);
66 #endif
67 break;
68
69 case ANDROID_SOCKET_NAMESPACE_RESERVED:
70 namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
71 /* unix_path_max appears to be missing on linux */
72 if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
73 goto error;
74 }
75
76 strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
77 strcat(p_addr->sun_path, name);
78 break;
79
80 case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
81 namelen = strlen(name);
82 /* unix_path_max appears to be missing on linux */
83 if (namelen > sizeof(*p_addr) - offsetof(struct sockaddr_un, sun_path) - 1) {
84 goto error;
85 }
86
87 strcpy(p_addr->sun_path, name);
88 break;
89 default:
90 // invalid namespace id
91 return -1;
92 }
93
94 p_addr->sun_family = AF_LOCAL;
95 *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
96 return 0;
97 error:
98 return -1;
99 }
100
101 /**
102 * connect to peer named "name" on fd
103 * returns same fd or -1 on error.
104 * fd is not closed on error. that's your job.
105 *
106 * Used by AndroidSocketImpl
107 */
osi_socket_local_client_connect(int fd,const char * name,int namespaceId,int)108 int osi_socket_local_client_connect(int fd, const char* name, int namespaceId, int /* type */) {
109 struct sockaddr_un addr;
110 socklen_t alen;
111 int err;
112
113 err = osi_socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
114
115 if (err < 0) {
116 goto error;
117 }
118
119 OSI_NO_INTR(err = connect(fd, (struct sockaddr*)&addr, alen));
120 if (err < 0) {
121 goto error;
122 }
123
124 return fd;
125
126 error:
127 return -1;
128 }
129
130 /**
131 * connect to peer named "name"
132 * returns fd or -1 on error
133 */
osi_socket_local_client(const char * name,int namespaceId,int type)134 int osi_socket_local_client(const char* name, int namespaceId, int type) {
135 int s;
136
137 s = socket(AF_LOCAL, type, 0);
138 if (s < 0) {
139 return -1;
140 }
141
142 if (0 > osi_socket_local_client_connect(s, name, namespaceId, type)) {
143 close(s);
144 return -1;
145 }
146
147 return s;
148 }
149