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
46 #include <attr/xattr.h> // for fsetxattr
49 #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,
80 .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 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");
157 // set smack attribute for certification
158 fsetxattr(manager.target_server_socket, "security.SMACK64IPIN", "*", 1, 0);
159 fsetxattr(manager.target_server_socket, "security.SMACK64IPOUT", "*", 1, 0);
160 #endif /* LOCALTEST */
162 bzero(&serverAddrUn, sizeof(serverAddrUn));
163 serverAddrUn.sun_family = AF_UNIX;
164 sprintf(serverAddrUn.sun_path, "%s", UDS_NAME);
166 if (-1 == bind(manager.target_server_socket, (struct sockaddr*) &serverAddrUn,
167 sizeof(serverAddrUn)))
169 LOGE("Target server socket binding failed\n");
173 if(chmod(serverAddrUn.sun_path, 0777) < 0)
175 LOGE("Failed to change mode for socket file : errno(%d)\n", errno);
179 if (-1 == listen(manager.target_server_socket, 5))
181 LOGE("Target server socket listening failed\n");
185 LOGI("Created TargetSock %d\n", manager.target_server_socket);
189 // return port number for normal case
190 // return negative value for error case
191 static int makeHostServerSocket()
193 struct sockaddr_in serverAddrIn;
197 if(manager.host_server_socket != -1)
198 return -1; // should be never happened
200 if ((manager.host_server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
202 LOGE("Host server socket creation failed\n");
206 if(setsockopt(manager.host_server_socket,
207 SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
209 LOGE("Failed to set socket option : errno(%d)\n", errno);
212 memset(&serverAddrIn, 0, sizeof(serverAddrIn));
213 serverAddrIn.sin_family = AF_INET;
214 serverAddrIn.sin_addr.s_addr = htonl(INADDR_ANY);
216 // bind address to server socket
217 for(port = INIT_PORT; port < LIMIT_PORT; port++)
219 serverAddrIn.sin_port = htons(port);
220 if (0 == bind(manager.host_server_socket,
221 (struct sockaddr*) &serverAddrIn, sizeof(serverAddrIn)))
225 if(port == LIMIT_PORT)
227 LOGE("Host server socket binding failed\n");
231 // enter listen state from client
232 if (-1 == listen(manager.host_server_socket, 5))
234 LOGE("Host server socket listening failed\n");
238 LOGI("Created HostSock %d\n", manager.host_server_socket);
242 // =============================================================================
243 // initializing / finalizing functions
244 // =============================================================================
246 static bool ensure_singleton(const char *lockfile)
248 if (access(lockfile, F_OK) == 0) /* File exists */
251 int lockfd = open(lockfile, O_WRONLY | O_CREAT, 0600);
253 LOGE("singleton lock file creation failed");
256 /* To prevent race condition, also check for lock availiability. */
257 bool locked = flock(lockfd, LOCK_EX | LOCK_NB) == 0;
260 LOGE("another instance of daemon is already running");
266 static void inititialize_manager_targets(__da_manager * mng)
269 __da_target_info target_init_value = {
279 for (index = 0; index < MAX_TARGET_COUNT; index++)
280 mng->target[index] = target_init_value;
282 manager.target_count = 0;
285 static bool initialize_pthread_sigmask()
289 sigemptyset(&newsigmask);
290 sigaddset(&newsigmask, SIGALRM);
291 sigaddset(&newsigmask, SIGUSR1);
292 return pthread_sigmask(SIG_BLOCK, &newsigmask, NULL) == 0;
295 // return 0 for normal case
296 static int initializeManager(FILE *portfile)
298 if (init_buf() != 0) {
299 LOGE("Cannot init buffer\n");
303 if (initialize_system_info() < 0) {
304 write_int(portfile, ERR_INITIALIZE_SYSTEM_INFO_FAILED);
307 // make server socket
308 if (makeTargetServerSocket() != 0) {
309 write_int(portfile, ERR_TARGET_SERVER_SOCKET_CREATE_FAILED);
312 if (!initialize_pthread_sigmask()) {
313 write_int(portfile, ERR_SIGNAL_MASK_SETTING_FAILED);
317 int port = makeHostServerSocket();
319 write_int(portfile, ERR_HOST_SERVER_SOCKET_CREATE_FAILED);
322 write_int(portfile, port);
325 LOGI("SUCCESS to write port\n");
327 inititialize_manager_targets(&manager);
329 // initialize sendMutex
330 pthread_mutex_init(&(manager.host.data_socket_mutex), NULL);
336 static int finalizeManager()
338 LOGI("Finalize daemon\n");
340 LOGI("finalize system info\n");
341 finalize_system_info();
344 // close host client socket
345 if(manager.host.control_socket != -1){
346 LOGI("close host control socket (%d)\n", manager.host.control_socket);
347 close(manager.host.control_socket);
349 if(manager.host.data_socket != -1){
350 LOGI("close host data socket (%d)\n", manager.host.data_socket);
351 close(manager.host.data_socket);
361 static void remove_buf_modules(void)
363 LOGI("rmmod buffer start\n");
364 if (system("cd /opt/swap/sdk && ./stop.sh")) {
365 LOGW("Cannot remove swap modules\n");
367 LOGI("rmmod buffer done\n");
370 static void terminate(int sig)
373 _close_server_socket();
374 remove_buf_modules();
376 LOGW("Terminating due signal %s\n", strsignal(sig));
377 signal(sig, SIG_DFL);
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 LOGI("main finished\n");