2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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.
21 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
26 #include <netinet/in.h>
29 #include <secom_socket.h>
39 int create_socket(const char *peer, struct sockaddr_un *addr)
44 len = sizeof(struct sockaddr_un);
47 if (strlen(peer) >= sizeof(addr->sun_path)) {
48 LOGE("peer %s is too long to remember it\\n", peer);
52 // We can believe this has no prob, because
53 // we already check the size of add.rsun_path
54 strcpy(addr->sun_path, peer);
55 addr->sun_family = AF_UNIX;
57 handle = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
59 LOGE("Failed to create a socket %s\n", strerror(errno));
68 int secom_create_client(const char *peer)
70 struct sockaddr_un addr;
75 handle = create_socket(peer, &addr);
79 state = connect(handle, (struct sockaddr*)&addr, sizeof(addr));
81 LOGE("Failed to connect to server [%s] %s\n", peer, strerror(errno));
82 if (close(handle) < 0)
83 LOGE("Failed to close a handle\n");
88 if (setsockopt(handle, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0)
89 LOGE("Failed to change sock opt : %s\n", strerror(errno));
96 int secom_create_server(const char *peer)
100 struct sockaddr_un addr;
102 handle = create_socket(peer, &addr);
103 if (handle < 0) return handle;
105 state = bind(handle, &addr, sizeof(addr));
107 LOGE("Failed to bind a socket %s\n", strerror(errno));
108 if (close(handle) < 0) {
109 LOGE("Failed to close a handle\n");
114 state = listen(handle, 5);
116 LOGE("Failed to listen a socket %s\n", strerror(errno));
117 if (close(handle) < 0) {
118 LOGE("Failed to close a handle\n");
123 if (chmod(peer, 0666) < 0) {
124 LOGE("Failed to change the permission of a socket (%s)\n", strerror(errno));
132 int secom_get_connection_handle(int server_handle)
134 struct sockaddr_un addr;
137 socklen_t size = sizeof(addr);
139 handle = accept(server_handle, (struct sockaddr*)&addr, &size);
141 LOGE("Failed to accept a new client %s\n", strerror(errno));
145 if (setsockopt(handle, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)) < 0) {
146 LOGE("Failed to change sock opt : %s\n", strerror(errno));
154 int secom_put_connection_handle(int conn_handle)
156 if (close(conn_handle) < 0) {
157 LOGE("Failed to close a handle\n");
165 int secom_send(int handle, const char *buffer, int size)
171 memset(&msg, 0, sizeof(msg));
172 iov.iov_base = (char*)buffer;
177 ret = sendmsg(handle, &msg, 0);
179 LOGE("Failed to send message [%s]\n", strerror(errno));
182 LOGD("Send done: %d\n", ret);
189 int secom_recv(int handle, char *buffer, int size, int *sender_pid)
192 struct cmsghdr *cmsg;
198 if (!sender_pid) sender_pid = &_pid;
201 memset(&msg, 0, sizeof(msg));
202 iov.iov_base = buffer;
206 msg.msg_control = control;
207 msg.msg_controllen = sizeof(control);
209 ret = recvmsg(handle, &msg, 0);
211 LOGE("Failed to recvmsg [%s] (%d)\n", strerror(errno), ret);
215 cmsg = CMSG_FIRSTHDR(&msg);
217 if (cmsg->cmsg_level == SOL_SOCKET
218 && cmsg->cmsg_type == SCM_CREDENTIALS)
221 cred = (struct ucred*)CMSG_DATA(cmsg);
222 *sender_pid = cred->pid;
225 cmsg = CMSG_NXTHDR(&msg, cmsg);
233 int secom_destroy(int handle)
235 if (close(handle) < 0) {
236 LOGE("Failed to close a handle\n");