summaryrefslogtreecommitdiff
path: root/focus_server/mm_sound_focus_server.c
blob: f6adf8fffcc47a52537037854834cc2d27a16e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <mm_debug.h>

#include <fcntl.h>
#include <semaphore.h>

#ifdef USE_LWIPC
#include <lwipc.h>
#endif

#include "../include/mm_sound_common.h"
#include "../include/mm_sound_utils.h"
#include "include/mm_sound_mgr_focus.h"
#include "include/mm_sound_mgr_focus_dbus.h"
#include "include/mm_sound_mgr_focus_socket.h"

#define USE_SYSTEM_SERVER_PROCESS_MONITORING

typedef struct {
	int startserver;
	int printlist;
	int testmode;
} server_arg;

static int _get_option(int argc, char **argv, server_arg *arg);
static int _usage(int argc, char **argv);

static GMainLoop *g_mainloop;

static void _mainloop_run()
{
	g_mainloop = g_main_loop_new(NULL, TRUE);
	if (g_mainloop == NULL)
		debug_error("g_main_loop_new() failed");

	g_main_loop_run(g_mainloop);
}

static int _get_option(int argc, char **argv, server_arg *arg)
{
	int c;
	static struct option long_options[] = {
		{"start", 0, 0, 'S'},
		{"help", 0, 0, 'H'},
		{"testmode", 0, 0, 'T'},
		{0, 0, 0, 0}
	};
	memset(arg, 0, sizeof(server_arg));

	arg->testmode = 0;

	while (1) {
		int opt_idx = 0;

		c = getopt_long (argc, argv, "SFLHRUP:Tiurd", long_options, &opt_idx);
		if (c == -1)
			break;
		switch (c) {
		case 'S': /* Start daemon */
			arg->startserver = 1;
			break;
		case 'T': /* Test mode */
			arg->testmode = 1;
			break;
		case 'H': /* help msg */
		default:
			return _usage(argc, argv);
		}
	}
	if (argc == 1)
		return _usage(argc, argv);
	return 0;
}

static int _usage(int argc, char **argv)
{
	fprintf(stderr, "Usage: %s [Options]\n", argv[0]);
	fprintf(stderr, "\t%-20s: start focus server.\n", "--start,-S");
	fprintf(stderr, "\t%-20s: help message.\n", "--help,-H");

	return 1;
}

static void _generate_ready_file(const char *path)
{
	int fd = -1;

	if (path == NULL) {
		debug_error("path is NULL");
		return;
	}

	if ((fd = creat(path, 0644)) != -1) {
		debug_warning("ready file(%s) file was created", path);
		close(fd);
	} else {
		debug_error("cannot create ready file(%s), errno(%d)", path, errno);
	}
}

static int g_socket_fd = -1;
static struct sigaction _int_old_action;
static struct sigaction _abrt_old_action;
static struct sigaction _segv_old_action;
static struct sigaction _term_old_action;
static struct sigaction _sys_old_action;
static struct sigaction _xcpu_old_action;

static void _signal_handler(int signo)
{
	debug_warning("ENTER, sig.num(%d)", signo);

	MMSoundMgrFocusDbusFini();
	MMSoundMgrFocusFini();
	MMSoundMgrFocusSocketFini(g_socket_fd);

#ifdef USE_GCOV
	mm_sound_gcov_flush();
#endif

	/* signal block -------------- */
	sigset_t old_mask, all_mask;
	sigfillset(&all_mask);
	sigprocmask(SIG_BLOCK, &all_mask, &old_mask);

	sigprocmask(SIG_SETMASK, &old_mask, NULL);
	/* signal unblock ------------ */

	switch (signo) {
	case SIGINT:
		sigaction(SIGINT, &_int_old_action, NULL);
		raise(signo);
		break;
	case SIGABRT:
		sigaction(SIGABRT, &_abrt_old_action, NULL);
		raise(signo);
		break;
	case SIGSEGV:
		sigaction(SIGSEGV, &_segv_old_action, NULL);
		raise(signo);
		break;
	case SIGTERM:
		sigaction(SIGTERM, &_term_old_action, NULL);
		raise(signo);
		break;
	case SIGSYS:
		sigaction(SIGSYS, &_sys_old_action, NULL);
		raise(signo);
		break;
	case SIGXCPU:
		sigaction(SIGXCPU, &_xcpu_old_action, NULL);
		raise(signo);
		break;
	default:
		break;
	}
	debug_warning("LEAVE");
}

static void _signal_initialize(void)
{
	struct sigaction _action;
	_action.sa_handler = _signal_handler;
	_action.sa_flags = SA_NOCLDSTOP;

	sigemptyset(&_action.sa_mask);

	sigaction(SIGINT, &_action, &_int_old_action);
	sigaction(SIGABRT, &_action, &_abrt_old_action);
	sigaction(SIGSEGV, &_action, &_segv_old_action);
	sigaction(SIGTERM, &_action, &_term_old_action);
	sigaction(SIGSYS, &_action, &_sys_old_action);
	sigaction(SIGXCPU, &_action, &_xcpu_old_action);

}

static void _signal_finalize(void)
{
	sigaction(SIGINT, &_int_old_action, NULL);
	sigaction(SIGABRT, &_abrt_old_action, NULL);
	sigaction(SIGSEGV, &_segv_old_action, NULL);
	sigaction(SIGTERM, &_term_old_action, NULL);
	sigaction(SIGSYS, &_sys_old_action, NULL);
	sigaction(SIGXCPU, &_xcpu_old_action, NULL);
}

int main(int argc, char **argv)
{
	server_arg serveropt;
#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
	int pid;
#endif
	int socket_fd = -1;

	if (_get_option(argc, argv, &serveropt))
		return 1;

	debug_warning("focus_server [%d] init ", getpid());

	_signal_initialize();

	/* Daemon process create */
	if (!serveropt.testmode && serveropt.startserver) {
#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
		daemon(0, 0); //chdir to ("/"), and close stdio
#endif
	}

	/* focus Server Starts!!!*/
	debug_warning("focus_server [%d] start ", getpid());

	signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE

#if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
	while (1) {
		if ((pid = fork()) < 0) {
			fprintf(stderr, "Sub Fork Error\n");
			return 2;
		} else if (pid == 0) {
			break;
		} else if (pid > 0) {
			wait(&ret);
			fprintf(stderr, "Killed by signal [%05X]\n", ret);
			fprintf(stderr, "Daemon is run againg\n");
		}
	}
#endif

#ifdef USE_GCOV
	mm_sound_gcov_set_prefix();
#endif

	if (serveropt.startserver) {
		/* Change the file mode mask */
		umask(0);

		if (MMSoundMgrFocusSocketInit(&socket_fd)) {
			debug_error("focus_server [%d] terminating, due to the error of socket init.", getpid());
			return 0;
		}
		if (MMSoundMgrFocusSocketReadyToWork(socket_fd)) {
			debug_error("focus_server [%d] terminating, due to the error of thread init.", getpid());
			return 0;
		}
		MMSoundMgrFocusDbusInit();
		MMSoundMgrFocusInit();
		g_socket_fd = socket_fd;
	}

	debug_warning("focus_server [%d] initialization complete...now, start running!!", getpid());

	if (serveropt.startserver) {
		unlink(PA_READY); // remove pa_ready file after focus-server init.
		/* FIXME : This code is moved from sound_server temporally for TV migration
					As other modules which has dependancy on this file is cleared,
					this code will be removed */
		/* broadcast if we're ready */
#ifdef USE_LWIPC
		if (LwipcEventDone(SOUND_SERVER_READY) < 0)
			debug_error("cannot create SOUND_SERVER_READY(sound_server_ready)");
		else
			debug_warning("SOUND_SERVER_READY(%s) event was created", SOUND_SERVER_READY);
#else
		_generate_ready_file(SOUND_SERVER_READY);
#endif
		_generate_ready_file(FOCUS_SERVER_READY);
		_mainloop_run();
	}

	debug_warning("focus_server [%d] terminating ", getpid());

	if (serveropt.startserver) {
		MMSoundMgrFocusDbusFini();
		MMSoundMgrFocusFini();
		MMSoundMgrFocusSocketFini(socket_fd);
	}

	_signal_finalize();

	debug_warning("focus_server [%d] exit ----------------- END ", getpid());

	return 0;
}