2 * Copyright (c) 2013 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.
17 * @file process_pool.c
18 * @author Tae-Jeong Lee (taejeong.lee@samsung.com)
20 * @brief process pool socket apis
24 #include <sys/socket.h>
25 #include <sys/types.h>
31 #include "process_pool.h"
32 #include "simple_util.h"
34 #define TMP_PATH "/tmp"
35 #define PROCESS_POOL_SERVER "wrt_process_pool_server"
36 #define MAX_PENDING_CONNECTIONS 10
37 #define CONNECT_RETRY_TIME 100 * 1000
38 #define CONNECT_RETRY_COUNT 3
40 int __create_process_pool_server()
42 struct sockaddr_un addr;
45 memset(&addr, 0x00, sizeof(struct sockaddr_un));
47 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
52 goto err_create_process_pool_server;
55 addr.sun_family = AF_UNIX;
56 snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%s", TMP_PATH, PROCESS_POOL_SERVER);
57 unlink(addr.sun_path);
59 _D("bind to %s", addr.sun_path);
60 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
63 goto err_create_process_pool_server;
66 _D("chmod to %s", addr.sun_path);
67 if (chmod(addr.sun_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0)
70 goto err_create_process_pool_server;
73 _D("listen to %s", addr.sun_path);
74 if (listen(fd, MAX_PENDING_CONNECTIONS) == -1)
77 goto err_create_process_pool_server;
80 _D("__create_process_pool_server done : %d", fd);
84 err_create_process_pool_server:
95 int __connect_process_pool_server()
97 struct sockaddr_un addr;
99 int retry = CONNECT_RETRY_COUNT;
101 int client_pid = getpid();
103 memset(&addr, 0x00, sizeof(struct sockaddr_un));
105 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
111 goto err_connect_process_pool_server;
114 addr.sun_family = AF_UNIX;
115 snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%s", TMP_PATH, PROCESS_POOL_SERVER);
118 _D("connect to %s", addr.sun_path);
119 while (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
121 if (errno != ETIMEDOUT || retry <= 0)
123 _E("connect error : %d", errno);
125 goto err_connect_process_pool_server;
128 usleep(CONNECT_RETRY_TIME);
130 _D("re-connect to %s (%d)", addr.sun_path, retry);
133 send_ret = send(fd, &client_pid, sizeof(client_pid), 0);
134 _D("send(%d) : %d", client_pid, send_ret);
140 goto err_connect_process_pool_server;
143 _D("__connect_process_pool_server done : %d", fd);
146 err_connect_process_pool_server:
157 int __accept_dummy_process(int server_fd, int* out_client_fd, int* out_client_pid)
159 int client_fd = -1, client_pid = 0, recv_ret = 0;
161 if (server_fd == -1 || out_client_fd == NULL || out_client_pid == NULL)
163 _E("arguments error!");
165 goto err__accept_dummy_process;
168 client_fd = accept(server_fd, NULL, NULL);
174 goto err__accept_dummy_process;
177 recv_ret = recv(client_fd, &client_pid, sizeof(client_pid), MSG_WAITALL);
183 goto err__accept_dummy_process;
186 *out_client_fd = client_fd;
187 *out_client_pid = client_pid;
189 return *out_client_fd;
191 err__accept_dummy_process:
201 void __refuse_dummy_process(int server_fd)
207 _E("arguments error!");
209 goto err__refuse_dummy_process;
212 client_fd = accept(server_fd, NULL, NULL);
218 goto err__refuse_dummy_process;
222 _D("refuse connection!");
226 err__refuse_dummy_process:
237 int __send_pkt_raw_data(int client_fd, app_pkt_t* pkt)
242 if (client_fd == -1 || pkt == NULL)
244 _E("arguments error!");
246 goto err__send_pkt_raw_data;
249 pkt_size = sizeof(pkt->cmd) + sizeof(pkt->len) + pkt->len;
251 send_ret = send(client_fd, pkt, pkt_size, 0);
252 _D("send(%d) : %d / %d", client_fd, send_ret, pkt_size);
258 goto err__send_pkt_raw_data;
260 else if (send_ret != pkt_size)
262 _E("send byte fail!");
264 goto err__send_pkt_raw_data;
269 err__send_pkt_raw_data: