4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <sys/types.h>
34 #include <semaphore.h>
40 #include "../include/mm_sound_common.h"
41 #include "../include/mm_sound_utils.h"
42 #include "include/mm_sound_mgr_focus.h"
43 #include "include/mm_sound_mgr_focus_dbus.h"
44 #include "include/mm_sound_mgr_focus_socket.h"
46 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
54 static int _get_option(int argc, char **argv, server_arg *arg);
55 static int _usage(int argc, char **argv);
57 static GMainLoop *g_mainloop;
59 static void _mainloop_run()
61 g_mainloop = g_main_loop_new(NULL, TRUE);
62 if (g_mainloop == NULL)
63 debug_error("g_main_loop_new() failed");
65 g_main_loop_run(g_mainloop);
68 static int _get_option(int argc, char **argv, server_arg *arg)
71 static struct option long_options[] = {
74 {"testmode", 0, 0, 'T'},
77 memset(arg, 0, sizeof(server_arg));
84 c = getopt_long (argc, argv, "SFLHRUP:Tiurd", long_options, &opt_idx);
88 case 'S': /* Start daemon */
91 case 'T': /* Test mode */
94 case 'H': /* help msg */
96 return _usage(argc, argv);
100 return _usage(argc, argv);
104 static int _usage(int argc, char **argv)
106 fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
107 fprintf(stderr, "\t%-20s: start focus server.\n", "--start,-S");
108 fprintf(stderr, "\t%-20s: help message.\n", "--help,-H");
113 static void _generate_ready_file(const char *path)
118 debug_error("path is NULL");
122 if ((fd = creat(path, 0644)) != -1) {
123 debug_warning("ready file(%s) file was created", path);
126 debug_error("cannot create ready file(%s), errno(%d)", path, errno);
130 static int g_socket_fd = -1;
131 static struct sigaction _int_old_action;
132 static struct sigaction _abrt_old_action;
133 static struct sigaction _segv_old_action;
134 static struct sigaction _term_old_action;
135 static struct sigaction _sys_old_action;
136 static struct sigaction _xcpu_old_action;
138 static void _signal_handler(int signo)
140 debug_warning("ENTER, sig.num(%d)", signo);
142 MMSoundMgrFocusDbusFini();
143 MMSoundMgrFocusFini();
144 MMSoundMgrFocusSocketFini(g_socket_fd);
147 mm_sound_gcov_flush();
150 /* signal block -------------- */
151 sigset_t old_mask, all_mask;
152 sigfillset(&all_mask);
153 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
155 sigprocmask(SIG_SETMASK, &old_mask, NULL);
156 /* signal unblock ------------ */
160 sigaction(SIGINT, &_int_old_action, NULL);
164 sigaction(SIGABRT, &_abrt_old_action, NULL);
168 sigaction(SIGSEGV, &_segv_old_action, NULL);
172 sigaction(SIGTERM, &_term_old_action, NULL);
176 sigaction(SIGSYS, &_sys_old_action, NULL);
180 sigaction(SIGXCPU, &_xcpu_old_action, NULL);
186 debug_warning("LEAVE");
189 static void _signal_initialize(void)
191 struct sigaction _action;
192 _action.sa_handler = _signal_handler;
193 _action.sa_flags = SA_NOCLDSTOP;
195 sigemptyset(&_action.sa_mask);
197 sigaction(SIGINT, &_action, &_int_old_action);
198 sigaction(SIGABRT, &_action, &_abrt_old_action);
199 sigaction(SIGSEGV, &_action, &_segv_old_action);
200 sigaction(SIGTERM, &_action, &_term_old_action);
201 sigaction(SIGSYS, &_action, &_sys_old_action);
202 sigaction(SIGXCPU, &_action, &_xcpu_old_action);
206 static void _signal_finalize(void)
208 sigaction(SIGINT, &_int_old_action, NULL);
209 sigaction(SIGABRT, &_abrt_old_action, NULL);
210 sigaction(SIGSEGV, &_segv_old_action, NULL);
211 sigaction(SIGTERM, &_term_old_action, NULL);
212 sigaction(SIGSYS, &_sys_old_action, NULL);
213 sigaction(SIGXCPU, &_xcpu_old_action, NULL);
216 int main(int argc, char **argv)
218 server_arg serveropt;
219 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
224 if (_get_option(argc, argv, &serveropt))
227 debug_warning("focus_server [%d] init ", getpid());
229 _signal_initialize();
231 /* Daemon process create */
232 if (!serveropt.testmode && serveropt.startserver) {
233 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
234 daemon(0, 0); //chdir to ("/"), and close stdio
238 /* focus Server Starts!!!*/
239 debug_warning("focus_server [%d] start ", getpid());
241 signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
243 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
245 if ((pid = fork()) < 0) {
246 fprintf(stderr, "Sub Fork Error\n");
248 } else if (pid == 0) {
250 } else if (pid > 0) {
252 fprintf(stderr, "Killed by signal [%05X]\n", ret);
253 fprintf(stderr, "Daemon is run againg\n");
259 mm_sound_gcov_set_prefix();
262 if (serveropt.startserver) {
263 /* Change the file mode mask */
266 if (MMSoundMgrFocusSocketInit(&socket_fd)) {
267 debug_error("focus_server [%d] terminating, due to the error of socket init.", getpid());
270 if (MMSoundMgrFocusSocketReadyToWork(socket_fd)) {
271 debug_error("focus_server [%d] terminating, due to the error of thread init.", getpid());
274 MMSoundMgrFocusDbusInit();
275 MMSoundMgrFocusInit();
276 g_socket_fd = socket_fd;
279 debug_warning("focus_server [%d] initialization complete...now, start running!!", getpid());
281 if (serveropt.startserver) {
282 unlink(PA_READY); // remove pa_ready file after focus-server init.
284 /* FIXME : This code is moved from sound_server temporally for TV migration
285 As other modules which has dependancy on this file is cleared,
286 this code will be removed */
287 /* broadcast if we're ready */
289 if (LwipcEventDone(SOUND_SERVER_READY) < 0)
290 debug_error("cannot create SOUND_SERVER_READY(sound_server_ready)");
292 debug_warning("SOUND_SERVER_READY(%s) event was created", SOUND_SERVER_READY);
294 _generate_ready_file(SOUND_SERVER_READY);
296 _generate_ready_file(FOCUS_SERVER_READY);
301 debug_warning("focus_server [%d] terminating ", getpid());
303 if (serveropt.startserver) {
304 MMSoundMgrFocusDbusFini();
305 MMSoundMgrFocusFini();
306 MMSoundMgrFocusSocketFini(socket_fd);
311 debug_warning("focus_server [%d] exit ----------------- END ", getpid());