focus-server: Enhancement for session backward compatibility regarding unregistering...
[platform/core/multimedia/libmm-sound.git] / focus_server / mm_sound_focus_server.c
index 4260b01..1080d4e 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <error.h>
-#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <signal.h>
-#include <pthread.h>
 #include <getopt.h>
 
-#include <vconf.h>
-#include <mm_error.h>
 #include <mm_debug.h>
 
 #include <fcntl.h>
-#include <sys/stat.h>
 #include <semaphore.h>
-#include <time.h>
-#include <errno.h>
 
+#ifdef USE_LWIPC
+#include <lwipc.h>
+#endif
+
+#include <signal.h>
 
 #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_ipc.h"
 #include "include/mm_sound_mgr_focus_dbus.h"
 
-#include <glib.h>
-
 #define USE_SYSTEM_SERVER_PROCESS_MONITORING
 
 typedef struct {
@@ -57,46 +49,99 @@ typedef struct {
     int testmode;
 } server_arg;
 
-static int getOption(int argc, char **argv, server_arg *arg);
-static int usage(int argc, char **argv);
-
-static struct sigaction sigint_action;  /* Backup pointer of SIGINT handler */
-static struct sigaction sigabrt_action; /* Backup pointer of SIGABRT signal handler */
-static struct sigaction sigsegv_action; /* Backup pointer of SIGSEGV fault signal handler */
-static struct sigaction sigterm_action; /* Backup pointer of SIGTERM signal handler */
-static struct sigaction sigsys_action;  /* Backup pointer of SIGSYS signal handler */
-static void _exit_handler(int sig);
+static int _get_option(int argc, char **argv, server_arg *arg);
+static int _usage(int argc, char **argv);
 
 GMainLoop *g_mainloop;
 
-void* pulse_handle;
-
-void mainloop_run()
+static void _mainloop_run()
 {
        g_mainloop = g_main_loop_new(NULL, TRUE);
-       if(g_mainloop == NULL) {
+       if (g_mainloop == NULL) {
                debug_error("g_main_loop_new() failed\n");
        }
+
        g_main_loop_run(g_mainloop);
 }
 
+static sem_t* _sem_create_n_wait()
+{
+       sem_t* sem = NULL;
+
+       if ((sem = sem_open("booting-sound", O_CREAT, 0660, 0)) == SEM_FAILED) {
+               debug_error("error creating sem : %d", errno);
+               return NULL;
+       }
+
+       debug_msg("returning sem [%p]", sem);
+       return sem;
+}
+
+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;
+}
+
 int main(int argc, char **argv)
 {
+       sem_t* sem = NULL;
        server_arg serveropt;
-       struct sigaction action;
 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
        int pid;
 #endif
+#ifdef TIZEN_TV
+       int fd_ss_ready = -1;
+#endif
 
-       action.sa_handler = _exit_handler;
-       action.sa_flags = 0;
-       sigemptyset(&action.sa_mask);
-
-       if (getOption(argc, argv, &serveropt))
+       if (_get_option(argc, argv, &serveropt))
                return 1;
 
        debug_warning("focus_server [%d] init \n", getpid());
 
+       if (serveropt.startserver) {
+               sem = _sem_create_n_wait();
+       }
        /* Daemon process create */
        if (!serveropt.testmode && serveropt.startserver) {
 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
@@ -110,30 +155,19 @@ int main(int argc, char **argv)
        signal(SIGPIPE, SIG_IGN); //ignore SIGPIPE
 
 #if !defined(USE_SYSTEM_SERVER_PROCESS_MONITORING)
-       while(1)
-       {
-               if ((pid = fork()) < 0)
-               {
+       while (1) {
+               if ((pid = fork()) < 0) {
                        fprintf(stderr, "Sub Fork Error\n");
                        return 2;
-               }
-               else if(pid == 0)
-               {
+               } else if (pid == 0) {
                        break;
-               }
-               else if(pid > 0)
-               {
+               } else if (pid > 0) {
                        wait(&ret);
                        fprintf(stderr, "Killed by signal [%05X]\n", ret);
                        fprintf(stderr, "Daemon is run againg\n");
                }
        }
 #endif
-       sigaction(SIGABRT, &action, &sigabrt_action);
-       sigaction(SIGSEGV, &action, &sigsegv_action);
-       sigaction(SIGTERM, &action, &sigterm_action);
-       sigaction(SIGSYS, &action, &sigsys_action);
-
        if (serveropt.startserver) {
                MMSoundMgrFocusDbusInit();
                MMSoundMgrFocusInit();
@@ -142,9 +176,38 @@ int main(int argc, char **argv)
        debug_warning("focus_server [%d] initialization complete...now, start running!!\n", getpid());
 
        if (serveropt.startserver) {
-//             unlink(PA_READY); // remove pa_ready file after focus-server init.
+               unlink(PA_READY); // remove pa_ready file after focus-server init.
+
+               if (sem) {
+                       if (sem_post(sem) == -1) {
+                               debug_error("error sem post : %d", errno);
+                       } else {
+                               debug_msg("Ready to play booting sound!!!!");
+                       }
+               }
 
-               mainloop_run();
+#ifdef TIZEN_TV
+               /* 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
+               if ((fd_ss_ready = creat(SOUND_SERVER_READY, 0644)) != -1) {
+                       debug_warning("SOUND_SERVER_READY(%s) file was created", SOUND_SERVER_READY);
+                       close(fd_ss_ready);
+               } else {
+                       debug_error("cannot create SOUND_SERVER_READY(/tmp/.sound_server_ready)");
+               }
+#endif
+#endif
+
+               _mainloop_run();
        }
 
        debug_warning("focus_server [%d] terminating \n", getpid());
@@ -158,82 +221,3 @@ int main(int argc, char **argv)
 
        return 0;
 }
-
-static int getOption(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;
-}
-
-//__attribute__ ((destructor))
-static void _exit_handler(int sig)
-{
-       switch(sig)
-       {
-       case SIGINT:
-               sigaction(SIGINT, &sigint_action, NULL);
-               debug_error("signal(SIGINT) error");
-               break;
-       case SIGABRT:
-               sigaction(SIGABRT, &sigabrt_action, NULL);
-               debug_error("signal(SIGABRT) error");
-               break;
-       case SIGSEGV:
-               sigaction(SIGSEGV, &sigsegv_action, NULL);
-               debug_error("signal(SIGSEGV) error");
-               break;
-       case SIGTERM:
-               sigaction(SIGTERM, &sigterm_action, NULL);
-               debug_error("signal(SIGTERM) error");
-               break;
-       case SIGSYS:
-               sigaction(SIGSYS, &sigsys_action, NULL);
-               debug_error("signal(SIGSYS) error");
-               break;
-       default:
-               break;
-       }
-       raise(sig);
-}
-
-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;
-}
-