fixup! Enhance session backward compatibility
[platform/core/multimedia/libmm-sound.git] / focus_server / mm_sound_focus_server.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <getopt.h>
28
29 #include <mm_debug.h>
30
31 #include <fcntl.h>
32 #include <semaphore.h>
33
34 #ifdef USE_LWIPC
35 #include <lwipc.h>
36 #endif
37
38 #include <signal.h>
39
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
44 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
45
46 typedef struct {
47     int startserver;
48     int printlist;
49     int testmode;
50 } server_arg;
51
52 static int _get_option(int argc, char **argv, server_arg *arg);
53 static int _usage(int argc, char **argv);
54
55 GMainLoop *g_mainloop;
56
57 static void _mainloop_run()
58 {
59         g_mainloop = g_main_loop_new(NULL, TRUE);
60         if (g_mainloop == NULL) {
61                 debug_error("g_main_loop_new() failed\n");
62         }
63
64         g_main_loop_run(g_mainloop);
65 }
66
67 static sem_t* _sem_create_n_wait()
68 {
69         sem_t* sem = NULL;
70
71         if ((sem = sem_open("booting-sound", O_CREAT, 0660, 0)) == SEM_FAILED) {
72                 debug_error("error creating sem : %d", errno);
73                 return NULL;
74         }
75
76         debug_msg("returning sem [%p]", sem);
77         return sem;
78 }
79
80 static int _get_option(int argc, char **argv, server_arg *arg)
81 {
82         int c;
83         static struct option long_options[] = {
84                 {"start", 0, 0, 'S'},
85                 {"help", 0, 0, 'H'},
86                 {"testmode", 0, 0, 'T'},
87                 {0, 0, 0, 0}
88         };
89         memset(arg, 0, sizeof(server_arg));
90
91         arg->testmode = 0;
92
93         while (1) {
94                 int opt_idx = 0;
95
96                 c = getopt_long (argc, argv, "SFLHRUP:Tiurd", long_options, &opt_idx);
97                 if (c == -1)
98                         break;
99                 switch (c)
100                 {
101                 case 'S': /* Start daemon */
102                         arg->startserver = 1;
103                         break;
104                 case 'T': /* Test mode */
105                         arg->testmode = 1;
106                         break;
107                 case 'H': /* help msg */
108                 default:
109                         return _usage(argc, argv);
110                 }
111         }
112         if (argc == 1)
113                 return _usage(argc, argv);
114         return 0;
115 }
116
117 static int _usage(int argc, char **argv)
118 {
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");
122
123         return 1;
124 }
125
126 static void _generate_ready_file(const char *path)
127 {
128         int fd = -1;
129
130         if (path == NULL) {
131                 debug_error("path is NULL");
132                 return;
133         }
134
135         if ((fd = creat(path, 0644)) != -1) {
136                 debug_warning("ready file(%s) file was created", path);
137                 close(fd);
138         } else {
139                 debug_error("cannot create ready file(%s), errno(%d)", path, errno);
140         }
141 }
142
143 int main(int argc, char **argv)
144 {
145         sem_t* sem = NULL;
146         server_arg serveropt;
147 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
148         int pid;
149 #endif
150
151         if (_get_option(argc, argv, &serveropt))
152                 return 1;
153
154         debug_warning("focus_server [%d] init \n", getpid());
155
156         if (serveropt.startserver) {
157                 sem = _sem_create_n_wait();
158         }
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
163 #endif
164         }
165
166         /* focus Server Starts!!!*/
167         debug_warning("focus_server [%d] start \n", getpid());
168
169         signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
170
171 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
172         while (1) {
173                 if ((pid = fork()) < 0) {
174                         fprintf(stderr, "Sub Fork Error\n");
175                         return 2;
176                 } else if (pid == 0) {
177                         break;
178                 } else if (pid > 0) {
179                         wait(&ret);
180                         fprintf(stderr, "Killed by signal [%05X]\n", ret);
181                         fprintf(stderr, "Daemon is run againg\n");
182                 }
183         }
184 #endif
185         if (serveropt.startserver) {
186                 MMSoundMgrFocusDbusInit();
187                 MMSoundMgrFocusInit();
188         }
189
190         debug_warning("focus_server [%d] initialization complete...now, start running!!\n", getpid());
191
192         if (serveropt.startserver) {
193                 unlink(PA_READY); // remove pa_ready file after focus-server init.
194
195                 if (sem) {
196                         if (sem_post(sem) == -1) {
197                                 debug_error("error sem post : %d", errno);
198                         } else {
199                                 debug_msg("Ready to play booting sound!!!!");
200                         }
201                 }
202
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 */
207 #ifdef USE_LWIPC
208                 if (LwipcEventDone(SOUND_SERVER_READY) < 0) {
209                         debug_error("cannot create SOUND_SERVER_READY(sound_server_ready)");
210                 } else {
211                         debug_warning("SOUND_SERVER_READY(%s) event was created", SOUND_SERVER_READY);
212                 }
213 #else
214                 _generate_ready_file(SOUND_SERVER_READY);
215 #endif
216                 _generate_ready_file(FOCUS_SERVER_READY);
217
218                 _mainloop_run();
219         }
220
221         debug_warning("focus_server [%d] terminating \n", getpid());
222
223         if (serveropt.startserver) {
224                 MMSoundMgrFocusDbusFini();
225                 MMSoundMgrFocusFini();
226         }
227
228         debug_warning("focus_server [%d] exit ----------------- END \n", getpid());
229
230         return 0;
231 }