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_mgr_focus.h"
42 #include "include/mm_sound_mgr_focus_dbus.h"
43 #include "include/mm_sound_mgr_focus_socket.h"
45 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
53 static int _get_option(int argc, char **argv, server_arg *arg);
54 static int _usage(int argc, char **argv);
56 GMainLoop *g_mainloop;
58 static void _mainloop_run()
60 g_mainloop = g_main_loop_new(NULL, TRUE);
61 if (g_mainloop == NULL)
62 debug_error("g_main_loop_new() failed");
64 g_main_loop_run(g_mainloop);
67 static sem_t * _sem_create_n_wait()
71 if ((sem = sem_open("booting-sound", O_CREAT, 0660, 0)) == SEM_FAILED) {
72 debug_error("error creating sem : %d", errno);
76 debug_msg("returning sem [%p]", sem);
80 static int _get_option(int argc, char **argv, server_arg *arg)
83 static struct option long_options[] = {
86 {"testmode", 0, 0, 'T'},
89 memset(arg, 0, sizeof(server_arg));
96 c = getopt_long (argc, argv, "SFLHRUP:Tiurd", long_options, &opt_idx);
100 case 'S': /* Start daemon */
101 arg->startserver = 1;
103 case 'T': /* Test mode */
106 case 'H': /* help msg */
108 return _usage(argc, argv);
112 return _usage(argc, argv);
116 static int _usage(int argc, char **argv)
118 fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
119 fprintf(stderr, "\t%-20s: start focus server.\n", "--start,-S");
120 fprintf(stderr, "\t%-20s: help message.\n", "--help,-H");
125 static void _generate_ready_file(const char *path)
130 debug_error("path is NULL");
134 if ((fd = creat(path, 0644)) != -1) {
135 debug_warning("ready file(%s) file was created", path);
138 debug_error("cannot create ready file(%s), errno(%d)", path, errno);
142 int g_socket_fd = -1;
143 struct sigaction _int_old_action;
144 struct sigaction _abrt_old_action;
145 struct sigaction _segv_old_action;
146 struct sigaction _term_old_action;
147 struct sigaction _sys_old_action;
148 struct sigaction _xcpu_old_action;
149 static void _signal_handler(int signo)
151 debug_warning("ENTER, sig.num(%d)", signo);
153 MMSoundMgrFocusDbusFini();
154 MMSoundMgrFocusFini();
155 MMSoundMgrFocusSocketFini(g_socket_fd);
157 /* signal block -------------- */
158 sigset_t old_mask, all_mask;
159 sigfillset(&all_mask);
160 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
162 sigprocmask(SIG_SETMASK, &old_mask, NULL);
163 /* signal unblock ------------ */
167 sigaction(SIGINT, &_int_old_action, NULL);
171 sigaction(SIGABRT, &_abrt_old_action, NULL);
175 sigaction(SIGSEGV, &_segv_old_action, NULL);
179 sigaction(SIGTERM, &_term_old_action, NULL);
183 sigaction(SIGSYS, &_sys_old_action, NULL);
187 sigaction(SIGXCPU, &_xcpu_old_action, NULL);
193 debug_warning("LEAVE");
196 static void _signal_initialize(void)
198 struct sigaction _action;
199 _action.sa_handler = _signal_handler;
200 _action.sa_flags = SA_NOCLDSTOP;
202 sigemptyset(&_action.sa_mask);
204 sigaction(SIGINT, &_action, &_int_old_action);
205 sigaction(SIGABRT, &_action, &_abrt_old_action);
206 sigaction(SIGSEGV, &_action, &_segv_old_action);
207 sigaction(SIGTERM, &_action, &_term_old_action);
208 sigaction(SIGSYS, &_action, &_sys_old_action);
209 sigaction(SIGXCPU, &_action, &_xcpu_old_action);
213 static void _signal_finalize(void)
215 sigaction(SIGINT, &_int_old_action, NULL);
216 sigaction(SIGABRT, &_abrt_old_action, NULL);
217 sigaction(SIGSEGV, &_segv_old_action, NULL);
218 sigaction(SIGTERM, &_term_old_action, NULL);
219 sigaction(SIGSYS, &_sys_old_action, NULL);
220 sigaction(SIGXCPU, &_xcpu_old_action, NULL);
223 int main(int argc, char **argv)
226 server_arg serveropt;
227 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
232 if (_get_option(argc, argv, &serveropt))
235 debug_warning("focus_server [%d] init ", getpid());
237 _signal_initialize();
239 if (serveropt.startserver)
240 sem = _sem_create_n_wait();
242 /* Daemon process create */
243 if (!serveropt.testmode && serveropt.startserver) {
244 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
245 daemon(0, 0); //chdir to ("/"), and close stdio
249 /* focus Server Starts!!!*/
250 debug_warning("focus_server [%d] start ", getpid());
252 signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
254 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
256 if ((pid = fork()) < 0) {
257 fprintf(stderr, "Sub Fork Error\n");
259 } else if (pid == 0) {
261 } else if (pid > 0) {
263 fprintf(stderr, "Killed by signal [%05X]\n", ret);
264 fprintf(stderr, "Daemon is run againg\n");
268 if (serveropt.startserver) {
269 /* Change the file mode mask */
272 if (MMSoundMgrFocusSocketInit(&socket_fd)) {
273 debug_error("focus_server [%d] terminating, due to the error of socket init.", getpid());
276 if (MMSoundMgrFocusSocketReadyToWork(socket_fd)) {
277 debug_error("focus_server [%d] terminating, due to the error of thread init.", getpid());
280 MMSoundMgrFocusDbusInit();
281 MMSoundMgrFocusInit();
282 g_socket_fd = socket_fd;
285 debug_warning("focus_server [%d] initialization complete...now, start running!!", getpid());
287 if (serveropt.startserver) {
288 unlink(PA_READY); // remove pa_ready file after focus-server init.
291 if (sem_post(sem) == -1)
292 debug_error("error sem post : %d", errno);
294 debug_msg("Ready to play booting sound!!!!");
297 /* FIXME : This code is moved from sound_server temporally for TV migration
298 As other modules which has dependancy on this file is cleared,
299 this code will be removed */
300 /* broadcast if we're ready */
302 if (LwipcEventDone(SOUND_SERVER_READY) < 0)
303 debug_error("cannot create SOUND_SERVER_READY(sound_server_ready)");
305 debug_warning("SOUND_SERVER_READY(%s) event was created", SOUND_SERVER_READY);
307 _generate_ready_file(SOUND_SERVER_READY);
309 _generate_ready_file(FOCUS_SERVER_READY);
314 debug_warning("focus_server [%d] terminating ", getpid());
316 if (serveropt.startserver) {
317 MMSoundMgrFocusDbusFini();
318 MMSoundMgrFocusFini();
319 MMSoundMgrFocusSocketFini(socket_fd);
324 debug_warning("focus_server [%d] exit ----------------- END ", getpid());