4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
8 * Vyacheslav Cherkashin <v.cherkashin@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
23 * - Samsung RnD Institute Russia
28 #define _GNU_SOURCE /* for accept4() */
29 #include <sys/socket.h>
33 #include "daemon.h" // for manager (it is need delete)
38 static struct target *target_malloc(void);
39 static void target_free(struct target *t);
41 struct target *target_ctor(void)
48 t->socket = UNKNOWN_FD;
49 t->event_fd = UNKNOWN_FD;
58 void target_dtor(struct target *t)
64 if (t->event_fd != -1)
68 if (t->socket != UNKNOWN_FD)
76 int target_accept(struct target *t, int sockfd)
80 sock = accept4(sockfd, NULL, NULL, SOCK_CLOEXEC);
81 if (sock == UNKNOWN_FD)
85 fd_setup_attributes(sock);
92 int target_send_msg(struct target *t, struct msg_target_t *msg)
94 return send_msg_to_sock(t->socket, msg);
97 int target_recv_msg(struct target *t, struct msg_target_t *msg)
99 return recv_msg_from_sock(t->socket, msg);
103 int target_start(struct target *t, void *(*start_routine) (void *))
105 return pthread_create(&t->recv_thread, NULL, start_routine, (void *)t);
108 int target_wait(struct target *t)
110 return pthread_join(t->recv_thread, NULL);
114 pid_t target_get_pid(struct target *t)
119 void target_set_pid(struct target *t, pid_t pid)
124 pid_t target_get_ppid(struct target *t)
129 void target_set_ppid(struct target *t, pid_t ppid)
134 static int target_cnt = 0;
135 static pthread_mutex_t ts_mutex = PTHREAD_MUTEX_INITIALIZER;
136 static int target_use[MAX_TARGET_COUNT] = {0};
137 static struct target target_array[MAX_TARGET_COUNT];
139 static void target_array_lock(void)
141 pthread_mutex_lock(&ts_mutex);
144 static void target_array_unlock(void)
146 pthread_mutex_unlock(&ts_mutex);
149 static struct target *target_malloc(void)
152 struct target *t = NULL;
155 for (i = 0; i < MAX_TARGET_COUNT; i++) {
156 if (target_use[i] == 0) {
158 t = &target_array[i];
162 target_array_unlock();
167 static void target_free(struct target *t)
170 target_use[t - target_array] = 0;
171 target_array_unlock();
175 void target_cnt_set(int cnt)
180 int target_cnt_get(void)
185 int target_cnt_sub_and_fetch(void)
187 return __sync_sub_and_fetch(&target_cnt, 1);
191 struct target *target_get(int i)
193 return &target_array[i];
203 int target_send_msg_to_all(struct msg_target_t *msg)
209 for (i = 0; i < MAX_TARGET_COUNT; ++i) {
210 if (target_use[i] == 0)
214 if (target_send_msg(t, msg))
217 target_array_unlock();
222 void target_wait_all(void)
228 for (i = 0; i < MAX_TARGET_COUNT; ++i) {
229 if (target_use[i] == 0)
234 LOGI("join recv thread [%d] is started\n", i);
236 LOGI("join recv thread %d. done\n", i);
238 target_array_unlock();
241 uint64_t target_get_total_alloc(pid_t pid)
248 for (i = 0; i < MAX_TARGET_COUNT; i++) {
249 if (target_use[i] == 0)
253 if (target_get_pid(t) == pid) {