4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
8 * Jaewon Lim <jaewon81.lim@samsung.com>
9 * Woojin Jung <woojin2.jung@samsung.com>
10 * Juyoung Kim <j0.kim@samsung.com>
11 * Cherepanov Vitaliy <v.cherepanov@samsung.com>
12 * Nikita Kalyazin <n.kalyazin@samsung.com>
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
18 * http://www.apache.org/licenses/LICENSE-2.0
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
28 * - Samsung RnD Institute Russia
32 #include <stdio.h> // for fopen, fprintf
33 #include <stdlib.h> // for atexit
34 #include <sys/types.h> // for open
36 #include <sys/stat.h> // for open
37 #include <sys/socket.h> // for socket
38 #include <sys/un.h> // for sockaddr_un
39 #include <arpa/inet.h> // for sockaddr_in, socklen_t
41 #include <signal.h> // for signal
42 #include <unistd.h> // for unlink
43 #include <fcntl.h> // for open, fcntl
47 #include "da_protocol.h"
54 #define SINGLETON_LOCKFILE "/tmp/da_manager.lock"
55 #define PORTFILE "/tmp/port.da"
56 #define UDS_NAME "/tmp/da.socket"
58 #define INIT_PORT 8001
59 #define LIMIT_PORT 8100
62 // initialize global variable
63 __da_manager manager =
65 .host_server_socket = -1,
66 .target_server_socket = -1,
69 .app_launch_timerfd = -1,
70 .connect_timeout_timerfd = -1,
71 .sampling_thread = -1,
73 .transfer_thread = -1,
81 .data_socket_mutex = PTHREAD_MUTEX_INITIALIZER
96 // =============================================================================
98 // =============================================================================
100 static void write_int(FILE *fp, int code)
102 fprintf(fp, "%d", code);
105 // =============================================================================
107 // =============================================================================
109 static void _close_server_socket(void)
111 LOGI("close_server_socket\n");
112 // close server socket
113 if(manager.host_server_socket != -1)
114 close(manager.host_server_socket);
115 if(manager.target_server_socket != -1)
116 close(manager.target_server_socket);
119 static void _unlink_files(void)
121 LOGI("unlink files start\n");
123 unlink(SINGLETON_LOCKFILE);
124 LOGI("unlink files done\n");
127 void unlink_portfile(void)
132 // =============================================================================
134 // =============================================================================
136 // return 0 for normal case
137 static int makeTargetServerSocket()
139 struct sockaddr_un serverAddrUn;
141 if(manager.target_server_socket != -1)
142 return -1; // should be never happend
144 // remove existed unix domain socket file
147 manager.target_server_socket = socket(AF_UNIX,
148 SOCK_STREAM | SOCK_CLOEXEC, 0);
149 if (manager.target_server_socket < 0)
151 LOGE("Target server socket creation failed\n");
155 fd_setup_attributes(manager.target_server_socket);
157 memset(&serverAddrUn, '\0', sizeof(serverAddrUn));
158 serverAddrUn.sun_family = AF_UNIX;
159 sprintf(serverAddrUn.sun_path, "%s", UDS_NAME);
161 if (-1 == bind(manager.target_server_socket, (struct sockaddr*) &serverAddrUn,
162 sizeof(serverAddrUn)))
164 LOGE("Target server socket binding failed\n");
168 if(chmod(serverAddrUn.sun_path, 0777) < 0)
170 LOGE("Failed to change mode for socket file : errno(%d)\n", errno);
174 if (-1 == listen(manager.target_server_socket, 5))
176 LOGE("Target server socket listening failed\n");
180 LOGI("Created TargetSock %d\n", manager.target_server_socket);
184 // return port number for normal case
185 // return negative value for error case
186 static int makeHostServerSocket()
188 struct sockaddr_in serverAddrIn;
192 if(manager.host_server_socket != -1)
193 return -1; // should be never happened
195 manager.host_server_socket = socket(PF_INET,
196 SOCK_STREAM | SOCK_CLOEXEC,
198 if (manager.host_server_socket < 0)
200 LOGE("Host server socket creation failed\n");
204 if(setsockopt(manager.host_server_socket,
205 SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
207 LOGE("Failed to set socket option : errno(%d)\n", errno);
210 memset(&serverAddrIn, 0, sizeof(serverAddrIn));
211 serverAddrIn.sin_family = AF_INET;
212 serverAddrIn.sin_addr.s_addr = htonl(INADDR_ANY);
214 // bind address to server socket
215 for(port = INIT_PORT; port < LIMIT_PORT; port++)
217 serverAddrIn.sin_port = htons(port);
218 if (0 == bind(manager.host_server_socket,
219 (struct sockaddr*) &serverAddrIn, sizeof(serverAddrIn)))
223 if(port == LIMIT_PORT)
225 LOGE("Host server socket binding failed\n");
229 // enter listen state from client
230 if (-1 == listen(manager.host_server_socket, 5))
232 LOGE("Host server socket listening failed\n");
236 LOGI("Created HostSock %d\n", manager.host_server_socket);
240 // =============================================================================
241 // initializing / finalizing functions
242 // =============================================================================
244 static bool ensure_singleton(const char *lockfile)
246 if (access(lockfile, F_OK) == 0) /* File exists */
249 int lockfd = open(lockfile, O_WRONLY | O_CREAT, 0600);
251 LOGE("singleton lock file creation failed");
254 /* To prevent race condition, also check for lock availiability. */
255 bool locked = flock(lockfd, LOCK_EX | LOCK_NB) == 0;
258 LOGE("another instance of daemon is already running");
264 static void inititialize_manager_targets(void)
269 static bool initialize_pthread_sigmask()
273 sigemptyset(&newsigmask);
274 sigaddset(&newsigmask, SIGALRM);
275 sigaddset(&newsigmask, SIGUSR1);
276 return pthread_sigmask(SIG_BLOCK, &newsigmask, NULL) == 0;
279 // return 0 for normal case
280 static int initializeManager(FILE *portfile)
282 if (init_buf() != 0) {
283 LOGE("Cannot init buffer\n");
287 if (initialize_system_info() < 0) {
288 write_int(portfile, ERR_INITIALIZE_SYSTEM_INFO_FAILED);
291 // make server socket
292 if (makeTargetServerSocket() != 0) {
293 write_int(portfile, ERR_TARGET_SERVER_SOCKET_CREATE_FAILED);
296 if (!initialize_pthread_sigmask()) {
297 write_int(portfile, ERR_SIGNAL_MASK_SETTING_FAILED);
301 int port = makeHostServerSocket();
303 write_int(portfile, ERR_HOST_SERVER_SOCKET_CREATE_FAILED);
306 write_int(portfile, port);
309 LOGI("SUCCESS to write port\n");
311 inititialize_manager_targets();
313 // initialize sendMutex
314 pthread_mutex_init(&(manager.host.data_socket_mutex), NULL);
320 static int finalizeManager()
322 LOGI("Finalize daemon\n");
323 LOGI("finalize system info\n");
324 finalize_system_info();
326 // close host client socket
327 if(manager.host.control_socket != -1){
328 LOGI("close host control socket (%d)\n", manager.host.control_socket);
329 close(manager.host.control_socket);
331 if(manager.host.data_socket != -1){
332 LOGI("close host data socket (%d)\n", manager.host.data_socket);
333 close(manager.host.data_socket);
340 static void remove_buf_modules(void)
342 LOGI("rmmod buffer start\n");
343 if (system("cd /opt/swap/sdk && ./stop.sh")) {
344 LOGW("Cannot remove swap modules\n");
346 LOGI("rmmod buffer done\n");
349 static void terminate(int sig)
351 LOGI("terminate! sig = %d\n", sig);
352 if (!stop_all_in_process()) {
353 // we are up there if signal accept and stop_all func was not
356 // so we need stop_all firstly (if profiling was
357 // not started it will be dummy call) and release other sources
360 _close_server_socket();
362 remove_buf_modules();
364 LOGW("Terminating due signal %s\n", strsignal(sig));
365 signal(sig, SIG_DFL);
370 // we are there if stop_all function was called by some reasons
372 // if stop_all called we cannot call remove_buf_modules and
373 // other funcs because of threads are not stopped yet
374 LOGW("Stop in progress\n");
376 LOGW("ignore signal %s\n", strsignal(sig));
377 signal(sig, SIG_IGN);
382 static void terminate0()
388 static void setup_signals()
390 struct sigaction sigact = {
391 .sa_handler = terminate,
394 sigemptyset(&sigact.sa_mask);
395 sigaction(SIGTERM, &sigact, 0);
396 sigaction(SIGINT, &sigact, 0);
398 signal(SIGHUP, SIG_IGN);
399 signal(SIGPIPE,SIG_IGN);
405 if (!ensure_singleton(SINGLETON_LOCKFILE))
410 LOGI("da_started\n");
417 LOGI("--- daemonized (pid %d) ---\n", getpid());
419 FILE *portfile = fopen(PORTFILE, "w");
421 LOGE("cannot create portfile");
425 int err = initializeManager(portfile);
430 //init all file descriptors
431 init_system_file_descriptors();
433 //FIX ME remove samplingThread it is only for debug
434 //samplingThread(NULL);
436 LOGI("daemon loop finished\n");
440 close_system_file_descriptors();
442 //DO NOT USE THIS FUNCTION FOR RELEASE IT IS TOO SLOW
443 #ifdef MALLOC_DEBUG_LEVEL
444 msg_swap_free_all_data(&prof_session.user_space_inst);
447 LOGI("main finished\n");
448 print_malloc_list(NULL, 0);