crash: added mutex in lookup
[platform/core/pim/pims-ipc.git] / src / pims-ipc-worker.c
1 /*
2  * PIMS IPC
3  *
4  * Copyright (c) 2012 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <poll.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/eventfd.h>
26 #include <fcntl.h>
27 #include <glib.h>
28
29 #include "pims-internal.h"
30 #include "pims-ipc-data-internal.h"
31 #include "pims-ipc-data.h"
32 #include "pims-socket.h"
33 #include "pims-ipc-utils.h"
34 #include "pims-ipc-worker.h"
35
36 #define PIMS_IPC_WORKER_THREAD_WAIT_TIME 100 /* milliseconds */
37
38 typedef struct {
39         pims_ipc_svc_client_disconnected_cb callback;
40         void * user_data;
41 } pims_ipc_svc_client_disconnected_cb_t;
42
43 /* idle_worker_pool SHOULD handle on main thread */
44 static GList *idle_worker_pool;
45 static GHashTable *worker_cb_table; /* call_id, cb_data */
46 static __thread pims_ipc_svc_client_disconnected_cb_t _client_disconnected_cb = {NULL, NULL};
47
48 static int unique_sequence_number;
49 static GHashTable *worker_client_info_map; /* key : worker_id, data : pims_ipc_client_info_s* */
50 static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid);
51 static pthread_mutex_t _worker_client_mutex = PTHREAD_MUTEX_INITIALIZER; /* for worker_client_info_map */
52
53 int worker_wait_idle_worker_ready(pims_ipc_worker_data_s *worker_data)
54 {
55         struct timespec timeout = {0};
56
57         clock_gettime(CLOCK_REALTIME, &timeout);
58         timeout.tv_nsec += PIMS_IPC_WORKER_THREAD_WAIT_TIME * 1000000;
59         timeout.tv_sec += timeout.tv_nsec / 1000000000L;
60         timeout.tv_nsec = timeout.tv_nsec % 1000000000L;
61
62         pthread_mutex_lock(&worker_data->ready_mutex);
63
64         if (!worker_data->fd) {
65                 WARN("worker fd is null, wait until worker thread create done.");
66                 if (pthread_cond_timedwait(&worker_data->ready, &worker_data->ready_mutex, &timeout)) {
67                         ERR("Get idle worker timeout Fail!");
68                         pthread_mutex_unlock(&worker_data->ready_mutex);
69                         return -1;
70                 }
71         }
72
73         pthread_mutex_unlock(&worker_data->ready_mutex);
74         return 0;
75 }
76
77 pims_ipc_worker_data_s* worker_get_idle_worker(pims_ipc_svc_s *ipc_svc,
78                 const char *client_id)
79 {
80         pims_ipc_worker_data_s *worker_data;
81
82         RETV_IF(NULL == client_id, NULL);
83         RETVM_IF(NULL == idle_worker_pool, NULL, "There is no idle worker");
84
85         worker_data = g_hash_table_lookup(ipc_svc->client_worker_map, client_id);
86         if (worker_data)
87                 return worker_data;
88
89         worker_data = idle_worker_pool->data;
90
91         idle_worker_pool = g_list_delete_link(idle_worker_pool, idle_worker_pool);
92
93         if (worker_data)
94                 g_hash_table_insert(ipc_svc->client_worker_map, g_strdup(client_id), worker_data);
95
96         return worker_data;
97 }
98
99 pims_ipc_worker_data_s* worker_find(pims_ipc_svc_s *ipc_svc, const char *client_id)
100 {
101         pims_ipc_worker_data_s *worker_data;
102
103         RETV_IF(NULL == client_id, NULL);
104
105         if (FALSE == g_hash_table_lookup_extended(ipc_svc->client_worker_map, client_id,
106                                 NULL, (gpointer*)&worker_data)) {
107                 ERR("g_hash_table_lookup_extended(%s) Fail", client_id);
108                 return NULL;
109         }
110
111         return worker_data;
112 }
113
114 void worker_stop_client_worker(pims_ipc_svc_s *ipc_svc, const char *client_id)
115 {
116         pims_ipc_worker_data_s *worker_data;
117
118         worker_data = worker_find(ipc_svc, client_id);
119
120         /* remove client_fd */
121         g_hash_table_remove(ipc_svc->client_worker_map, client_id);
122
123         /* stop worker thread */
124         if (worker_data) {
125                 worker_data->stop_thread = TRUE;
126                 worker_data->client_fd = -1;
127                 write_command(worker_data->fd, 1);
128                 DBG("write command to worker terminate(worker_fd:%d)", worker_data->fd);
129         }
130 }
131
132
133 void worker_free_raw_data(void *data)
134 {
135         pims_ipc_raw_data_s *raw_data = data;
136
137         if (NULL == raw_data)
138                 return;
139
140         free(raw_data->client_id);
141         free(raw_data->call_id);
142         free(raw_data->data);
143         free(raw_data);
144 }
145
146 void worker_free_data(gpointer data)
147 {
148         pims_ipc_worker_data_s *worker_data = data;
149
150         pthread_mutex_lock(&worker_data->queue_mutex);
151         if (worker_data->list)
152                 g_list_free_full(worker_data->list, worker_free_raw_data);
153         pthread_mutex_unlock(&worker_data->queue_mutex);
154
155         pthread_cond_destroy(&worker_data->ready);
156         pthread_mutex_destroy(&worker_data->ready_mutex);
157
158         free(worker_data);
159 }
160
161
162 int worker_push_raw_data(pims_ipc_worker_data_s *worker_data, int client_fd,
163                 pims_ipc_raw_data_s *data)
164 {
165         pthread_mutex_lock(&worker_data->queue_mutex);
166         worker_data->list = g_list_append(worker_data->list, data);
167         worker_data->client_fd = client_fd;
168         pthread_mutex_unlock(&worker_data->queue_mutex);
169
170         return TRUE;
171 }
172
173 static gboolean worker_pop_raw_data(pims_ipc_worker_data_s *worker,
174                 pims_ipc_raw_data_s **data)
175 {
176         if (!worker)
177                 return FALSE;
178
179         pthread_mutex_lock(&worker->queue_mutex);
180         if (!worker->list) {
181                 pthread_mutex_unlock(&worker->queue_mutex);
182                 *data = NULL;
183                 return FALSE;
184         }
185
186         *data = g_list_first(worker->list)->data;
187         worker->list = g_list_delete_link(worker->list, g_list_first(worker->list));
188         pthread_mutex_unlock(&worker->queue_mutex);
189
190         return TRUE;
191 }
192
193 int worker_set_callback(char *call_id, pims_ipc_svc_cb_s *cb_data)
194 {
195         return g_hash_table_insert(worker_cb_table, call_id, cb_data);
196 }
197
198 static void __run_callback(int client_pid, char *call_id, pims_ipc_data_h dhandle_in,
199                 pims_ipc_data_h *dhandle_out)
200 {
201         pims_ipc_svc_cb_s *cb_data = NULL;
202
203         VERBOSE("Call id [%s]", call_id);
204
205         cb_data = g_hash_table_lookup(worker_cb_table, call_id);
206         if (cb_data == NULL) {
207                 VERBOSE("No Data for %s", call_id);
208                 return;
209         }
210
211         /* TODO: client_pid is not valide pims_ipc_h */
212         cb_data->callback((pims_ipc_h)client_pid, dhandle_in, dhandle_out, cb_data->user_data);
213 }
214
215 static void __make_raw_data(const char *call_id, int seq_no, pims_ipc_data_h data,
216                 pims_ipc_raw_data_s **out)
217 {
218         pims_ipc_data_s *data_in = data;
219         pims_ipc_raw_data_s *raw_data = NULL;
220
221         RET_IF(NULL == out);
222         RET_IF(NULL == call_id);
223
224         raw_data = calloc(1, sizeof(pims_ipc_raw_data_s));
225         if (NULL == raw_data) {
226                 ERR("calloc() Fail(%d)", errno);
227                 return;
228         }
229
230         raw_data->call_id = g_strdup(call_id);
231         raw_data->call_id_len = strlen(raw_data->call_id);
232         raw_data->seq_no = seq_no;
233
234         if (data_in && 0 < data_in->buf_size) {
235                 raw_data->has_data = TRUE;
236                 raw_data->data = calloc(1, data_in->buf_size+1);
237                 if (NULL == raw_data->data) {
238                         ERR("calloc() Fail");
239                         free(raw_data->call_id);
240                         free(raw_data);
241                         return;
242                 }
243                 memcpy(raw_data->data, data_in->buf, data_in->buf_size);
244                 raw_data->data_len = data_in->buf_size;
245         } else {
246                 raw_data->has_data = FALSE;
247                 raw_data->data_len = 0;
248                 raw_data->data = NULL;
249         }
250         *out = raw_data;
251         return;
252 }
253
254 static int __send_raw_data(int fd, const char *client_id, pims_ipc_raw_data_s *data)
255 {
256         int ret = 0;
257         unsigned int len, total_len, client_id_len;
258
259         RETV_IF(NULL == data, -1);
260         RETV_IF(NULL == client_id, -1);
261
262         client_id_len = strlen(client_id);
263
264         len = sizeof(total_len) + sizeof(client_id_len) + client_id_len + sizeof(data->seq_no)
265                 + data->call_id_len + sizeof(data->call_id) + sizeof(data->has_data);
266         total_len = len;
267
268         if (data->has_data) {
269                 len += sizeof(data->data_len);
270                 total_len = len + data->data_len;
271         }
272
273         INFO("client_id: %s, call_id : %s, seq no :%d, len:%d, total len :%d", client_id,
274                         data->call_id, data->seq_no, len, total_len);
275
276         int length = 0;
277         char buf[len+1];
278         memset(buf, 0x0, len+1);
279
280         memcpy(buf, &total_len, sizeof(total_len));
281         length += sizeof(total_len);
282
283         memcpy(buf+length, &client_id_len, sizeof(client_id_len));
284         length += sizeof(client_id_len);
285         memcpy(buf+length, client_id, client_id_len);
286         length += client_id_len;
287
288         memcpy(buf+length, &(data->seq_no), sizeof(data->seq_no));
289         length += sizeof(data->seq_no);
290
291         memcpy(buf+length, &(data->call_id_len), sizeof(data->call_id_len));
292         length += sizeof(data->call_id_len);
293         memcpy(buf+length, data->call_id, data->call_id_len);
294         length += data->call_id_len;
295
296         memcpy(buf+length, &(data->has_data), sizeof(data->has_data));
297         length += sizeof(data->has_data);
298
299         if (data->has_data) {
300                 memcpy(buf+length, &(data->data_len), sizeof(data->data_len));
301                 length += sizeof(data->data_len);
302                 ret = socket_send(fd, buf, length);
303
304                 /* send data */
305                 if (ret > 0)
306                         ret += socket_send_data(fd, data->data, data->data_len);
307         } else {
308                 ret = socket_send(fd, buf, length);
309         }
310
311         return ret;
312 }
313
314 static int _get_pid_from_fd(int fd)
315 {
316         struct ucred uc;
317         socklen_t uc_len = sizeof(uc);
318
319         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len) < 0)
320                 ERR("getsockopt() Failed(%d)", errno);
321
322         DBG("Client PID(%d)", uc.pid);
323         return uc.pid;
324 }
325
326 static int __worker_loop_handle_raw_data(pims_ipc_worker_data_s *worker_data)
327 {
328         int disconnected = FALSE;
329         pims_ipc_data_h data_in = NULL;
330         pims_ipc_data_h data_out = NULL;
331         pims_ipc_raw_data_s *result = NULL;
332         pims_ipc_raw_data_s *raw_data = NULL;
333
334         if (FALSE == worker_pop_raw_data(worker_data, &raw_data))
335                 return disconnected;
336
337         int client_pid = _get_pid_from_fd(worker_data->client_fd);
338
339         if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_CREATE, raw_data->call_id)) {
340                 client_register_info(worker_data, client_pid);
341
342         } else if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_DESTROY, raw_data->call_id)) {
343                 disconnected = TRUE;
344         } else {
345                 data_in = pims_ipc_data_steal_unmarshal(raw_data->data, raw_data->data_len);
346
347                 __run_callback(client_pid, raw_data->call_id, data_in, &data_out);
348                 pims_ipc_data_destroy(data_in);
349         }
350
351         if (data_out) {
352                 __make_raw_data(raw_data->call_id, raw_data->seq_no, data_out, &result);
353                 pims_ipc_data_destroy(data_out);
354         } else
355                 __make_raw_data(raw_data->call_id, raw_data->seq_no, NULL, &result);
356
357         if (worker_data->client_fd != -1)
358                 __send_raw_data(worker_data->client_fd, raw_data->client_id, result);
359         worker_free_raw_data(raw_data);
360         worker_free_raw_data(result);
361
362         return disconnected;
363 }
364
365 static void* __worker_loop(void *data)
366 {
367         int ret;
368         pthread_t pid;
369         int worker_fd;
370         int disconnected = FALSE;
371         pims_ipc_worker_data_s *worker_data = data;
372
373         RETV_IF(NULL == data, NULL);
374
375         worker_fd = eventfd(0, 0);
376         if (worker_fd == -1)
377                 return NULL;
378
379         INFO("worker Created ********** worker_fd = %d ***********", worker_fd);
380
381         pid = pthread_self();
382         worker_data->client_fd = -1;
383         worker_data->stop_thread = FALSE;
384         pthread_mutex_lock(&worker_data->ready_mutex);
385         worker_data->fd = worker_fd;
386         pthread_cond_signal(&worker_data->ready);
387         pthread_mutex_unlock(&worker_data->ready_mutex);
388
389         struct pollfd pollfds[1];
390         pollfds[0].fd = worker_fd;
391         pollfds[0].events = POLLIN;
392         pollfds[0].revents = 0;
393
394         while (!worker_data->stop_thread) {
395                 ret = poll(pollfds, 1, 3000); /* waiting command from router */
396                 if (-1 == ret) {
397                         if (errno != EINTR)
398                                 ERR("poll() Fail(%d)", errno);
399                         continue;
400                 }
401                 if (worker_data->stop_thread)
402                         break;
403
404                 if (0 == ret)
405                         continue;
406
407                 if (pollfds[0].revents & POLLIN) {
408                         uint64_t dummy;
409                         read_command(pollfds[0].fd, &dummy);
410
411                         disconnected = __worker_loop_handle_raw_data(worker_data);
412                 }
413         }
414
415         if (!disconnected)
416                 ERR("client fd closed, worker_fd : %d", worker_fd);
417         INFO("task thread terminated --------------------------- (worker_fd : %d)", worker_fd);
418
419         int flag = fcntl(worker_data->client_fd, F_GETFL, 0);
420         if (0 == (FD_CLOEXEC & flag)) {
421                 int client_pid = _get_pid_from_fd(worker_data->client_fd);
422                 pthread_mutex_lock(&_worker_client_mutex);
423                 g_hash_table_remove(worker_client_info_map, GINT_TO_POINTER(client_pid));
424                 DBG("client pid(%u) is removed", client_pid);
425                 pthread_mutex_unlock(&_worker_client_mutex);
426         } else {
427                 DBG("fd(%d) is already closed", worker_data->client_fd);
428         }
429
430         worker_free_data(worker_data);
431         close(worker_fd);
432
433         if (_client_disconnected_cb.callback)
434                 _client_disconnected_cb.callback((pims_ipc_h)pid, _client_disconnected_cb.user_data);
435
436         return NULL;
437 }
438
439 void worker_start_idle_worker(pims_ipc_svc_s *ipc_data)
440 {
441         int i;
442         pims_ipc_worker_data_s *worker_data;
443
444         for (i = g_list_length(idle_worker_pool); i < ipc_data->workers_max_count; i++) {
445                 worker_data = calloc(1, sizeof(pims_ipc_worker_data_s));
446                 if (NULL == worker_data) {
447                         ERR("calloc() Fail(%d)", errno);
448                         continue;
449                 }
450                 pthread_mutex_init(&worker_data->queue_mutex, 0);
451                 pthread_mutex_init(&worker_data->ready_mutex, NULL);
452                 pthread_cond_init(&worker_data->ready, NULL);
453
454                 utils_launch_thread(__worker_loop, worker_data);
455                 idle_worker_pool = g_list_append(idle_worker_pool, worker_data);
456         }
457 }
458
459 void worker_stop_idle_worker()
460 {
461         GList *cursor;
462         pims_ipc_worker_data_s *worker_data;
463
464         cursor = idle_worker_pool;
465         while (cursor) {
466                 worker_data = cursor->data;
467                 worker_data->stop_thread = TRUE;
468                 write_command(worker_data->fd, 1);
469                 cursor = cursor->next;
470         }
471 }
472
473 void worker_init()
474 {
475         worker_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
476         WARN_IF(NULL == worker_cb_table, "worker cb table is NULL");
477 }
478
479 void worker_deinit()
480 {
481         g_list_free(idle_worker_pool);
482         idle_worker_pool = NULL;
483
484         g_hash_table_destroy(worker_cb_table);
485         worker_cb_table = NULL;
486 }
487
488 API void pims_ipc_svc_set_client_disconnected_cb(
489                 pims_ipc_svc_client_disconnected_cb callback, void *user_data)
490 {
491         if (_client_disconnected_cb.callback) {
492                 ERR("already registered");
493                 return;
494         }
495         _client_disconnected_cb.callback = callback;
496         _client_disconnected_cb.user_data = user_data;
497 }
498
499 void client_destroy_info(gpointer p)
500 {
501         pims_ipc_client_info_s *client_info = p;
502
503         if (NULL == client_info)
504                 return;
505         free(client_info->smack);
506         free(client_info->uid);
507         free(client_info->client_session);
508         free(client_info);
509 }
510
511 void client_init(void)
512 {
513         pthread_mutex_init(&_worker_client_mutex, 0);
514
515         pthread_mutex_lock(&_worker_client_mutex);
516         unique_sequence_number = 0;
517         if (worker_client_info_map) {
518                 ERR("worker_client_info_map already exists");
519                 pthread_mutex_unlock(&_worker_client_mutex);
520                 return;
521         }
522
523         worker_client_info_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
524                         client_destroy_info);
525         pthread_mutex_unlock(&_worker_client_mutex);
526 }
527
528 void client_deinit(void)
529 {
530         DBG("----------destroied");
531         pthread_mutex_lock(&_worker_client_mutex);
532         g_hash_table_destroy(worker_client_info_map);
533         worker_client_info_map = NULL;
534         pthread_mutex_unlock(&_worker_client_mutex);
535         pthread_mutex_destroy(&_worker_client_mutex);
536 }
537
538 static int _create_client_info(int fd, pims_ipc_client_info_s **p_client_info)
539 {
540         int ret;
541         pid_t pid;
542         char errmsg[1024] = {0};
543
544         pims_ipc_client_info_s *client_info = calloc(1, sizeof(pims_ipc_client_info_s));
545         if (NULL == client_info) {
546                 ERR("calloc() return NULL");
547                 return -1;
548         }
549
550         ret = cynara_creds_socket_get_client(fd, CLIENT_METHOD_SMACK, &(client_info->smack));
551         if (CYNARA_API_SUCCESS != ret) {
552                 cynara_strerror(ret, errmsg, sizeof(errmsg));
553                 ERR("cynara_creds_socket_get_client() Fail(%d,%s)", ret, errmsg);
554                 client_destroy_info(client_info);
555                 return -1;
556         }
557
558         ret = cynara_creds_socket_get_user(fd, USER_METHOD_UID, &(client_info->uid));
559         if (CYNARA_API_SUCCESS != ret) {
560                 cynara_strerror(ret, errmsg, sizeof(errmsg));
561                 ERR("cynara_creds_socket_get_user() Fail(%d,%s)", ret, errmsg);
562                 client_destroy_info(client_info);
563                 return -1;
564         }
565
566         ret = cynara_creds_socket_get_pid(fd, &pid);
567         if (CYNARA_API_SUCCESS != ret) {
568                 cynara_strerror(ret, errmsg, sizeof(errmsg));
569                 ERR("cynara_creds_socket_get_pid() Fail(%d,%s)", ret, errmsg);
570                 client_destroy_info(client_info);
571                 return -1;
572         }
573
574         client_info->client_session = cynara_session_from_pid(pid);
575         if (NULL == client_info->client_session) {
576                 ERR("cynara_session_from_pid() return NULL");
577                 client_destroy_info(client_info);
578                 return -1;
579         }
580         *p_client_info = client_info;
581
582         return 0;
583 }
584
585 static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid)
586 {
587         pims_ipc_client_info_s *client_info = NULL;
588         int ret = 0;
589
590         ret = _create_client_info(worker_data->client_fd, &client_info);
591         if (ret < 0) {
592                 ERR("_create_client_info() Fail(%d)", ret);
593                 return -1;
594         }
595         pthread_mutex_lock(&_worker_client_mutex);
596         g_hash_table_insert(worker_client_info_map, GINT_TO_POINTER(client_pid), client_info);
597         DBG("-------inserted:pid(%d), info(%p)", client_pid, client_info);
598         pthread_mutex_unlock(&_worker_client_mutex);
599
600         return 0;
601 }
602
603 int client_get_unique_sequence_number(void)
604 {
605         return unique_sequence_number++;
606 }
607
608 pims_ipc_client_info_s* client_clone_info(pims_ipc_client_info_s *client_info)
609 {
610         if (NULL == client_info) {
611                 ERR("client_info is NULL");
612                 return NULL;
613         }
614
615         pims_ipc_client_info_s *clone = calloc(1, sizeof(pims_ipc_client_info_s));
616         if (NULL == clone) {
617                 ERR("calloc() Fail");
618                 return NULL;
619         }
620
621         if (client_info->smack) {
622                 clone->smack = strdup(client_info->smack);
623                 if (NULL == clone->smack) {
624                         ERR("strdup() Fail");
625                         client_destroy_info(clone);
626                         return NULL;
627                 }
628         }
629
630         if (client_info->uid) {
631                 clone->uid = strdup(client_info->uid);
632                 if (NULL == clone->uid) {
633                         ERR("strdup() Fail");
634                         client_destroy_info(clone);
635                         return NULL;
636                 }
637         }
638
639         if (client_info->client_session) {
640                 clone->client_session = strdup(client_info->client_session);
641                 if (NULL == clone->client_session) {
642                         ERR("strdup() Fail");
643                         client_destroy_info(clone);
644                         return NULL;
645                 }
646         }
647
648         return clone;
649 }
650
651 pims_ipc_client_info_s* client_get_info(int client_pid)
652 {
653         pims_ipc_client_info_s *client_info = NULL;
654
655         pthread_mutex_lock(&_worker_client_mutex);
656         client_info = g_hash_table_lookup(worker_client_info_map, GINT_TO_POINTER(client_pid));
657         pthread_mutex_unlock(&_worker_client_mutex);
658         DBG("Get client_info(%p) from pid(%d)", client_info, client_pid);
659         return client_info;
660 }
661