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.
32 #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"
44 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
52 static int _get_option(int argc, char **argv, server_arg *arg);
53 static int _usage(int argc, char **argv);
55 GMainLoop *g_mainloop;
57 static void _mainloop_run()
59 g_mainloop = g_main_loop_new(NULL, TRUE);
60 if (g_mainloop == NULL) {
61 debug_error("g_main_loop_new() failed\n");
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);
101 case 'S': /* Start daemon */
102 arg->startserver = 1;
104 case 'T': /* Test mode */
107 case 'H': /* help msg */
109 return _usage(argc, argv);
113 return _usage(argc, argv);
117 static int _usage(int argc, char **argv)
119 fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
120 fprintf(stderr, "\t%-20s: start focus server.\n", "--start,-S");
121 fprintf(stderr, "\t%-20s: help message.\n", "--help,-H");
126 static void _generate_ready_file(const char *path)
131 debug_error("path is NULL");
135 if ((fd = creat(path, 0644)) != -1) {
136 debug_warning("ready file(%s) file was created", path);
139 debug_error("cannot create ready file(%s), errno(%d)", path, errno);
143 int main(int argc, char **argv)
146 server_arg serveropt;
147 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
151 if (_get_option(argc, argv, &serveropt))
154 debug_warning("focus_server [%d] init \n", getpid());
156 if (serveropt.startserver) {
157 sem = _sem_create_n_wait();
159 /* Daemon process create */
160 if (!serveropt.testmode && serveropt.startserver) {
161 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
162 daemon(0,0); //chdir to ("/"), and close stdio
166 /* focus Server Starts!!!*/
167 debug_warning("focus_server [%d] start \n", getpid());
169 signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
171 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
173 if ((pid = fork()) < 0) {
174 fprintf(stderr, "Sub Fork Error\n");
176 } else if (pid == 0) {
178 } else if (pid > 0) {
180 fprintf(stderr, "Killed by signal [%05X]\n", ret);
181 fprintf(stderr, "Daemon is run againg\n");
185 if (serveropt.startserver) {
186 MMSoundMgrFocusDbusInit();
187 MMSoundMgrFocusInit();
190 debug_warning("focus_server [%d] initialization complete...now, start running!!\n", getpid());
192 if (serveropt.startserver) {
193 unlink(PA_READY); // remove pa_ready file after focus-server init.
196 if (sem_post(sem) == -1) {
197 debug_error("error sem post : %d", errno);
199 debug_msg("Ready to play booting sound!!!!");
203 /* FIXME : This code is moved from sound_server temporally for TV migration
204 As other modules which has dependancy on this file is cleared,
205 this code will be removed */
206 /* broadcast if we're ready */
208 if (LwipcEventDone(SOUND_SERVER_READY) < 0) {
209 debug_error("cannot create SOUND_SERVER_READY(sound_server_ready)");
211 debug_warning("SOUND_SERVER_READY(%s) event was created", SOUND_SERVER_READY);
214 _generate_ready_file(SOUND_SERVER_READY);
216 _generate_ready_file(FOCUS_SERVER_READY);
221 debug_warning("focus_server [%d] terminating \n", getpid());
223 if (serveropt.startserver) {
224 MMSoundMgrFocusDbusFini();
225 MMSoundMgrFocusFini();
228 debug_warning("focus_server [%d] exit ----------------- END \n", getpid());