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.
34 #include "../include/mm_sound_common.h"
35 #include "../include/mm_sound_utils.h"
36 #include "include/mm_sound_thread_pool.h"
37 #include "include/mm_sound_mgr_codec.h"
38 #include "include/mm_sound_mgr_ipc_dbus.h"
40 #include "../config.h" /* for PLUGIN_DIR */
42 #define PLUGIN_ENV "MM_SOUND_PLUGIN_PATH"
44 #define MAX_PLUGIN_DIR_PATH_LEN 256
46 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
49 char plugdir[MAX_PLUGIN_DIR_PATH_LEN];
55 static int _get_option(int argc, char **argv, server_arg *arg);
56 static int _usage(int argc, char **argv);
58 static struct sigaction sigint_action; /* Backup pointer of SIGINT handler */
59 static struct sigaction sigabrt_action; /* Backup pointer of SIGABRT signal handler */
60 static struct sigaction sigsegv_action; /* Backup pointer of SIGSEGV fault signal handler */
61 static struct sigaction sigterm_action; /* Backup pointer of SIGTERM signal handler */
62 static struct sigaction sigsys_action; /* Backup pointer of SIGSYS signal handler */
63 static void _exit_handler(int sig);
65 static GMainLoop *g_mainloop;
67 static gboolean _shutdown_cb(gpointer user_data)
69 debug_warning("TIMER : quit mainloop now!");
70 g_main_loop_quit(g_mainloop);
75 static void _mainloop_run()
77 g_mainloop = g_main_loop_new(NULL, TRUE);
78 if (g_mainloop == NULL) {
79 debug_error("g_main_loop_new() failed");
83 g_main_loop_run(g_mainloop);
86 static int _get_option(int argc, char **argv, server_arg *arg)
89 static struct option long_options[] = {
93 {"plugdir", 1, 0, 'P'},
94 {"testmode", 0, 0, 'T'},
97 memset(arg, 0, sizeof(server_arg));
99 MMSOUND_STRNCPY(arg->plugdir, PLUGIN_DIR, MAX_PLUGIN_DIR_PATH_LEN);
106 c = getopt_long(argc, argv, "SFLHRUP:Tiurd", long_options, &opt_idx);
110 case 'S': /* Start daemon */
111 arg->startserver = 1;
113 case 'L': /* list of plugins */
116 case 'P': /* Custom plugindir */
117 MMSOUND_STRNCPY(arg->plugdir, optarg, MAX_PLUGIN_DIR_PATH_LEN);
119 case 'T': /* Test mode */
122 case 'H': /* help msg */
124 return _usage(argc, argv);
128 return _usage(argc, argv);
132 //__attribute__ ((destructor))
133 static void _exit_handler(int sig)
136 mm_sound_gcov_flush();
141 sigaction(SIGINT, &sigint_action, NULL);
142 debug_error("signal(SIGINT) error");
145 sigaction(SIGABRT, &sigabrt_action, NULL);
146 debug_error("signal(SIGABRT) error");
149 sigaction(SIGSEGV, &sigsegv_action, NULL);
150 debug_error("signal(SIGSEGV) error");
153 sigaction(SIGTERM, &sigterm_action, NULL);
154 debug_error("signal(SIGTERM) error");
157 sigaction(SIGSYS, &sigsys_action, NULL);
158 debug_error("signal(SIGSYS) error");
166 static int _usage(int argc, char **argv)
168 fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
169 fprintf(stderr, "\t%-20s: start sound server.\n", "--start,-S");
170 fprintf(stderr, "\t%-20s: help message.\n", "--help,-H");
175 int main(int argc, char **argv)
177 server_arg serveropt;
178 struct sigaction action;
179 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
183 action.sa_handler = _exit_handler;
185 sigemptyset(&action.sa_mask);
187 if (_get_option(argc, argv, &serveropt))
190 debug_warning("sound_server [%d] init ", getpid());
192 /* Daemon process create */
193 if (!serveropt.testmode && serveropt.startserver) {
194 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
195 daemon(0, 0); //chdir to ("/"), and close stdio
199 /* Sound Server Starts!!!*/
200 debug_warning("sound_server [%d] start ", getpid());
202 signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
204 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
206 if ((pid = fork()) < 0) {
207 fprintf(stderr, "Sub Fork Error\n");
209 } else if (pid == 0) {
211 } else if (pid > 0) {
213 fprintf(stderr, "Killed by signal [%05X]\n", ret);
214 fprintf(stderr, "Daemon is run againg\n");
218 sigaction(SIGABRT, &action, &sigabrt_action);
219 sigaction(SIGSEGV, &action, &sigsegv_action);
220 sigaction(SIGTERM, &action, &sigterm_action);
221 sigaction(SIGSYS, &action, &sigsys_action);
224 mm_sound_gcov_set_prefix();
227 if (serveropt.startserver || serveropt.printlist) {
228 MMSoundMgrDbusInit();
229 if (MMSoundThreadPoolInit()) {
230 debug_error("failed to MMSoundThreadPoolInit()");
233 MMSoundMgrCodecInit(serveropt.plugdir, _shutdown_cb);
236 debug_warning("sound_server [%d] initialization complete...now, start running!!", getpid());
238 if (serveropt.startserver) {
244 debug_warning("sound_server [%d] terminating ", getpid());
246 if (serveropt.startserver || serveropt.printlist) {
247 MMSoundMgrCodecFini();
248 MMSoundThreadPoolFini();
249 MMSoundMgrDbusFini();
252 debug_warning("sound_server [%d] exit ----------------- END ", getpid());