Sync with the latest code of phone profile
[platform/framework/web/data-provider-master.git] / src / main.c
index f28a334..631f45c 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/signalfd.h>
+#include <ctype.h>
 
 #include <Ecore.h>
 #include <Ecore_X.h>
-#include <Evas.h>
-#include <Ecore_Evas.h>
 #include <glib.h>
 #include <glib-object.h>
 #include <aul.h>
@@ -55,6 +55,7 @@
 #include "notification_service.h"
 #include "utility_service.h"
 #include "badge_service.h"
+#include "file_service.h"
 
 #if defined(FLOG)
 FILE *__file_log_fp;
@@ -64,9 +65,10 @@ static inline int app_create(void)
 {
        int ret;
 
-       if (access(SLAVE_LOG_PATH, R_OK|W_OK) != 0) {
-               if (mkdir(SLAVE_LOG_PATH, 755) < 0)
+       if (access(SLAVE_LOG_PATH, R_OK | W_OK) != 0) {
+               if (mkdir(SLAVE_LOG_PATH, 0755) < 0) {
                        ErrPrint("Failed to create %s (%s)\n", SLAVE_LOG_PATH, strerror(errno));
+               }
        }
 
        /*!
@@ -117,6 +119,29 @@ static inline int app_create(void)
        DbgPrint("Server initialized: %d\n", ret);
 
        event_init();
+
+       if (util_service_is_enabled(SERVICE_SHORTCUT)) {
+               shortcut_service_init();
+       }
+
+       if (util_service_is_enabled(SERVICE_NOTIFICATION)) {
+               notification_service_init();
+       }
+
+       if (util_service_is_enabled(SERVICE_BADGE)) {
+               badge_service_init();
+       }
+
+       if (util_service_is_enabled(SERVICE_UTILITY)) {
+               utility_service_init();
+       }
+
+       script_init();
+
+       if (util_service_is_enabled(SERVICE_FILE)) {
+               file_service_init();
+       }
+
        return 0;
 }
 
@@ -124,25 +149,58 @@ static inline int app_terminate(void)
 {
        int ret;
 
-       event_fini();
+       if (util_service_is_enabled(SERVICE_FILE)) {
+               ret = file_service_fini();
+               DbgPrint("Finalize the file service: %d\n", ret);
+       }
+
+       ret = server_fini();
+       DbgPrint("Finalize server: %d\n", ret);
+
+       ret = dead_fini();
+       DbgPrint("dead signal handler finalized: %d\n", ret);
+
+       if (util_service_is_enabled(SERVICE_UTILITY)) {
+               ret = utility_service_fini();
+               DbgPrint("utility: %d\n", ret);
+       }
+
+       if (util_service_is_enabled(SERVICE_BADGE)) {
+               ret = badge_service_fini();
+               DbgPrint("badge: %d\n", ret);
+       }
+
+       if (util_service_is_enabled(SERVICE_NOTIFICATION)) {
+               ret = notification_service_fini();
+               DbgPrint("noti: %d\n", ret);
+       }
+
+       if (util_service_is_enabled(SERVICE_SHORTCUT)) {
+               ret = shortcut_service_fini();
+               DbgPrint("shortcut: %d\n", ret);
+       }
+
+       ret = event_fini();
+       DbgPrint("event: %d\n", ret);
 
        ret = setting_fini();
        DbgPrint("Finalize setting : %d\n", ret);
 
-       xmonitor_fini();
-
-       instance_fini();
+       ret = instance_fini();
+       DbgPrint("Finalizing instances: %d\n", ret);
 
        ret = package_fini();
        DbgPrint("Finalize package info: %d\n", ret);
 
-       client_fini();
+       ret = script_fini();
+       DbgPrint("script: %d\n", ret);
 
-       ret = server_fini();
-       DbgPrint("Finalize dbus: %d\n", ret);
+       ret = buffer_handler_fini();
+       DbgPrint("buffer handler: %d\n", ret);
 
-       ret = dead_fini();
-       DbgPrint("dead signal handler finalized: %d\n", ret);
+       xmonitor_fini();
+
+       client_fini();
 
        ret = io_fini();
        DbgPrint("IO finalized: %d\n", ret);
@@ -154,35 +212,98 @@ static inline int app_terminate(void)
        return 0;
 }
 
-static void signal_handler(int signum, siginfo_t *info, void *unused)
+static Eina_Bool signal_cb(void *data, Ecore_Fd_Handler *handler)
 {
+       struct signalfd_siginfo fdsi;
+       ssize_t size;
        int fd;
 
-       CRITICAL_LOG("Terminated(SIGTERM)\n");
-       fd = creat("/tmp/.stop.provider", 0644);
-       if (fd > 0)
-               close(fd);
+       fd = ecore_main_fd_handler_fd_get(handler);
+       if (fd < 0) {
+               ErrPrint("Unable to get FD\n");
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       size = read(fd, &fdsi, sizeof(fdsi));
+       if (size != sizeof(fdsi)) {
+               ErrPrint("Unable to get siginfo: %s\n", strerror(errno));
+               return ECORE_CALLBACK_CANCEL;
+       }
+
+       if (fdsi.ssi_signo == SIGTERM) {
+               int cfd;
+
+               CRITICAL_LOG("Terminated(SIGTERM)\n");
+
+               cfd = creat("/tmp/.stop.provider", 0644);
+               if (cfd < 0 || close(cfd) < 0) {
+                       ErrPrint("stop.provider: %s\n", strerror(errno));
+               }
+
+               vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
+               //exit(0);
+               ecore_main_loop_quit();
+       } else if (fdsi.ssi_signo == SIGUSR1) {
+               /*!
+                * Turn off auto-reactivation
+                * Terminate all slaves
+                */
+               CRITICAL_LOG("USRS1, Deactivate ALL\n");
+               slave_deactivate_all(0, 1);
+       } else if (fdsi.ssi_signo == SIGUSR2) {
+               /*!
+                * Turn on auto-reactivation
+                * Launch all slaves again
+                */
+               CRITICAL_LOG("USR2, Activate ALL\n");
+               slave_activate_all();
+       } else {
+               CRITICAL_LOG("Unknown SIG[%d] received\n", fdsi.ssi_signo);
+       }
 
-       exit(0);
+       return ECORE_CALLBACK_RENEW;
 }
 
 int main(int argc, char *argv[])
 {
-       struct sigaction act;
        int ret;
+       int restart_count = 0;
+       sigset_t mask;
+       Ecore_Fd_Handler *signal_handler = NULL;
+
+       conf_init();
+       conf_loader();
+
+       /*!
+        * \note
+        * Clear old contents files before start the master provider.
+        */
+       (void)util_unlink_files(ALWAYS_PATH);
+       (void)util_unlink_files(READER_PATH);
+       (void)util_unlink_files(IMAGE_PATH);
+       (void)util_unlink_files(SLAVE_LOG_PATH);
+
+       if (util_free_space(IMAGE_PATH) < MINIMUM_SPACE) {
+               util_remove_emergency_disk();
+               util_prepare_emergency_disk();
+       }
+
+       util_setup_log_disk();
 
        /*!
         * How could we care this return values?
         * Is there any way to print something on the screen?
         */
        ret = critical_log_init(util_basename(argv[0]));
-       if (ret < 0)
+       if (ret < 0) {
                ErrPrint("Failed to init the critical log\n");
+       }
 
 #if defined(FLOG)
        __file_log_fp = fopen("/tmp/live.log", "w+t");
-       if (!__file_log_fp)
+       if (!__file_log_fp) {
                __file_log_fp = fdopen(1, "w+t");
+       }
 #endif
        /* appcore_agent_terminate */
        if (ecore_init() <= 0) {
@@ -191,20 +312,35 @@ int main(int argc, char *argv[])
                return -EFAULT;
        }
 
-       act.sa_sigaction = signal_handler;
-       act.sa_flags = SA_SIGINFO;
+       sigemptyset(&mask);
 
-       ret = sigemptyset(&act.sa_mask);
-       if (ret < 0)
+       ret = sigaddset(&mask, SIGTERM);
+       if (ret < 0) {
                CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
+       }
 
-       ret = sigaddset(&act.sa_mask, SIGTERM);
-       if (ret < 0)
+       ret = sigaddset(&mask, SIGUSR1);
+       if (ret < 0) {
+               CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
+       }
+
+       ret = sigaddset(&mask, SIGUSR2);
+       if (ret < 0) {
+               CRITICAL_LOG("Failed to do sigemptyset: %s\n", strerror(errno));
+       }
+
+       ret = sigprocmask(SIG_BLOCK, &mask, NULL);
+       if (ret < 0) {
                CRITICAL_LOG("Failed to mask the SIGTERM: %s\n", strerror(errno));
+       }
 
-       ret = sigaction(SIGTERM, &act, NULL);
-       if (ret < 0)
-               CRITICAL_LOG("Failed to add sigaction: %s\n", strerror(errno));
+       ret = signalfd(-1, &mask, 0);
+       if (ret < 0) {
+               CRITICAL_LOG("Failed to initiate the signalfd: %s\n", strerror(errno));
+       } else {
+               signal_handler = ecore_main_fd_handler_add(ret, ECORE_FD_READ, signal_cb, NULL, NULL, NULL);
+               CRITICAL_LOG("Signal handler initiated: %d\n", ret);
+       }
 
        if (ecore_x_init(NULL) <= 0) {
                CRITICAL_LOG("Failed to ecore x init\n");
@@ -215,69 +351,44 @@ int main(int argc, char *argv[])
 
        ecore_app_args_set(argc, (const char **)argv);
 
-       if (evas_init() <= 0) {
-               CRITICAL_LOG("Failed to init evas return count is below than 0\n");
-               ecore_x_shutdown();
-               ecore_shutdown();
-               critical_log_fini();
-               return -EFAULT;
-       }
-
-       if (ecore_evas_init() <= 0) {
-               CRITICAL_LOG("Failed to init ecore_evas\n");
-               evas_shutdown();
-               ecore_x_shutdown();
-               ecore_shutdown();
-               critical_log_fini();
-               return -EFAULT;
-       }
-
-#if !GLIB_CHECK_VERSION (2, 36, 0)
+#if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
        g_type_init();
 #endif
 
-       conf_loader();
-
        /*!
-        * \note
-        * Clear old contents files before start the master provider.
+        * \note
+        * conf_update_size requires ecore_x_init.
         */
-       (void)util_unlink_files(ALWAYS_PATH);
-       (void)util_unlink_files(READER_PATH);
-       (void)util_unlink_files(IMAGE_PATH);
-       (void)util_unlink_files(SLAVE_LOG_PATH);
-
-       shortcut_service_init();
-       notification_service_init();
-       badge_service_init();
-       utility_service_init();
-       script_init();
+       conf_update_size();
 
        app_create();
 
+       vconf_get_int(VCONFKEY_MASTER_RESTART_COUNT, &restart_count);
+       restart_count++;
+       vconf_set_int(VCONFKEY_MASTER_RESTART_COUNT, restart_count);
+
        vconf_set_bool(VCONFKEY_MASTER_STARTED, 1);
        ecore_main_loop_begin();
        vconf_set_bool(VCONFKEY_MASTER_STARTED, 0);
 
        app_terminate();
 
-       script_fini();
-       utility_service_fini();
-       badge_service_fini();
-       notification_service_fini();
-       shortcut_service_fini();
+       ecore_x_shutdown();
 
-       ecore_evas_shutdown();
-       evas_shutdown();
+       if (signal_handler) {
+               ecore_main_fd_handler_del(signal_handler);
+       }
 
-       ecore_x_shutdown();
        ecore_shutdown();
        critical_log_fini();
 
 #if defined(FLOG)
-       if (__file_log_fp)
+       if (__file_log_fp) {
                fclose(__file_log_fp);
+       }
 #endif
+
+       conf_reset();
        return 0;
 }