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
45 #include <attr/xattr.h> // for fsetxattr
47 #include "da_protocol.h"
53 #define SINGLETON_LOCKFILE "/tmp/da_manager.lock"
54 #define PORTFILE "/tmp/port.da"
55 #define UDS_NAME "/tmp/da.socket"
57 #define INIT_PORT 8001
58 #define LIMIT_PORT 8100
61 // initialize global variable
62 __da_manager manager =
64 .host_server_socket = -1,
65 .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
99 // =============================================================================
101 // =============================================================================
103 static void write_int(FILE *fp, int code)
105 fprintf(fp, "%d", code);
108 // =============================================================================
110 // =============================================================================
112 static void _close_server_socket(void)
114 LOGI("close_server_socket\n");
115 // close server socket
116 if(manager.host_server_socket != -1)
117 close(manager.host_server_socket);
118 if(manager.target_server_socket != -1)
119 close(manager.target_server_socket);
122 static void _unlink_files(void)
124 LOGI("unlink files start\n");
126 unlink(SINGLETON_LOCKFILE);
127 LOGI("unlink files done\n");
130 void unlink_portfile(void)
135 // =============================================================================
137 // =============================================================================
139 // return 0 for normal case
140 static int makeTargetServerSocket()
142 struct sockaddr_un serverAddrUn;
144 if(manager.target_server_socket != -1)
145 return -1; // should be never happend
147 // remove existed unix domain socket file
150 if ((manager.target_server_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
152 LOGE("Target server socket creation failed\n");
156 fd_setup_smack_attributes(manager.target_server_socket);
158 memset(&serverAddrUn, '\0', sizeof(serverAddrUn));
159 serverAddrUn.sun_family = AF_UNIX;
160 sprintf(serverAddrUn.sun_path, "%s", UDS_NAME);
162 if (-1 == bind(manager.target_server_socket, (struct sockaddr*) &serverAddrUn,
163 sizeof(serverAddrUn)))
165 LOGE("Target server socket binding failed\n");
169 if(chmod(serverAddrUn.sun_path, 0777) < 0)
171 LOGE("Failed to change mode for socket file : errno(%d)\n", errno);
175 if (-1 == listen(manager.target_server_socket, 5))
177 LOGE("Target server socket listening failed\n");
181 LOGI("Created TargetSock %d\n", manager.target_server_socket);
185 // return port number for normal case
186 // return negative value for error case
187 static int makeHostServerSocket()
189 struct sockaddr_in serverAddrIn;
193 if(manager.host_server_socket != -1)
194 return -1; // should be never happened
196 if ((manager.host_server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
198 LOGE("Host server socket creation failed\n");
202 if(setsockopt(manager.host_server_socket,
203 SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
205 LOGE("Failed to set socket option : errno(%d)\n", errno);
208 memset(&serverAddrIn, 0, sizeof(serverAddrIn));
209 serverAddrIn.sin_family = AF_INET;
210 serverAddrIn.sin_addr.s_addr = htonl(INADDR_ANY);
212 // bind address to server socket
213 for(port = INIT_PORT; port < LIMIT_PORT; port++)
215 serverAddrIn.sin_port = htons(port);
216 if (0 == bind(manager.host_server_socket,
217 (struct sockaddr*) &serverAddrIn, sizeof(serverAddrIn)))
221 if(port == LIMIT_PORT)
223 LOGE("Host server socket binding failed\n");
227 // enter listen state from client
228 if (-1 == listen(manager.host_server_socket, 5))
230 LOGE("Host server socket listening failed\n");
234 LOGI("Created HostSock %d\n", manager.host_server_socket);
238 // =============================================================================
239 // initializing / finalizing functions
240 // =============================================================================
242 static bool ensure_singleton(const char *lockfile)
244 if (access(lockfile, F_OK) == 0) /* File exists */
247 int lockfd = open(lockfile, O_WRONLY | O_CREAT, 0600);
249 LOGE("singleton lock file creation failed");
252 /* To prevent race condition, also check for lock availiability. */
253 bool locked = flock(lockfd, LOCK_EX | LOCK_NB) == 0;
256 LOGE("another instance of daemon is already running");
262 static void inititialize_manager_targets(__da_manager * mng)
265 __da_target_info target_init_value = {
274 for (index = 0; index < MAX_TARGET_COUNT; index++)
275 mng->target[index] = target_init_value;
277 manager.target_count = 0;
280 static bool initialize_pthread_sigmask()
284 sigemptyset(&newsigmask);
285 sigaddset(&newsigmask, SIGALRM);
286 sigaddset(&newsigmask, SIGUSR1);
287 return pthread_sigmask(SIG_BLOCK, &newsigmask, NULL) == 0;
290 // return 0 for normal case
291 static int initializeManager(FILE *portfile)
293 if (init_buf() != 0) {
294 LOGE("Cannot init buffer\n");
298 if (initialize_system_info() < 0) {
299 write_int(portfile, ERR_INITIALIZE_SYSTEM_INFO_FAILED);
302 // make server socket
303 if (makeTargetServerSocket() != 0) {
304 write_int(portfile, ERR_TARGET_SERVER_SOCKET_CREATE_FAILED);
307 if (!initialize_pthread_sigmask()) {
308 write_int(portfile, ERR_SIGNAL_MASK_SETTING_FAILED);
312 int port = makeHostServerSocket();
314 write_int(portfile, ERR_HOST_SERVER_SOCKET_CREATE_FAILED);
317 write_int(portfile, port);
320 LOGI("SUCCESS to write port\n");
322 inititialize_manager_targets(&manager);
324 // initialize sendMutex
325 pthread_mutex_init(&(manager.host.data_socket_mutex), NULL);
331 static int finalizeManager()
333 LOGI("Finalize daemon\n");
334 LOGI("finalize system info\n");
335 finalize_system_info();
337 // close host client socket
338 if(manager.host.control_socket != -1){
339 LOGI("close host control socket (%d)\n", manager.host.control_socket);
340 close(manager.host.control_socket);
342 if(manager.host.data_socket != -1){
343 LOGI("close host data socket (%d)\n", manager.host.data_socket);
344 close(manager.host.data_socket);
351 static void remove_buf_modules(void)
353 LOGI("rmmod buffer start\n");
354 if (system("cd /opt/swap/sdk && ./stop.sh")) {
355 LOGW("Cannot remove swap modules\n");
357 LOGI("rmmod buffer done\n");
360 static void terminate(int sig)
362 LOGI("terminate! sig = %d\n", sig);
363 if (!stop_all_in_process()) {
364 // we are up there if signal accept and stop_all func was not
367 // so we need stop_all firstly (if profiling was
368 // not started it will be dummy call) and release other sources
371 _close_server_socket();
373 remove_buf_modules();
375 LOGW("Terminating due signal %s\n", strsignal(sig));
376 signal(sig, SIG_DFL);
381 // we are there if stop_all function was called by some reasons
383 // if stop_all called we cannot call remove_buf_modules and
384 // other funcs because of threads are not stopped yet
385 LOGW("Stop in progress\n");
387 LOGW("ignore signal %s\n", strsignal(sig));
388 signal(sig, SIG_IGN);
393 static void terminate0()
399 static void setup_signals()
401 struct sigaction sigact = {
402 .sa_handler = terminate,
405 sigemptyset(&sigact.sa_mask);
406 sigaction(SIGTERM, &sigact, 0);
407 sigaction(SIGINT, &sigact, 0);
409 signal(SIGHUP, SIG_IGN);
410 signal(SIGPIPE,SIG_IGN);
416 if (!ensure_singleton(SINGLETON_LOCKFILE))
421 LOGI("da_started\n");
428 LOGI("--- daemonized (pid %d) ---\n", getpid());
430 FILE *portfile = fopen(PORTFILE, "w");
432 LOGE("cannot create portfile");
436 int err = initializeManager(portfile);
441 //init all file descriptors
442 init_system_file_descriptors();
444 //FIX ME remove samplingThread it is only for debug
445 //samplingThread(NULL);
447 LOGI("daemon loop finished\n");
451 close_system_file_descriptors();
453 LOGI("main finished\n");