Fix PLM issue(P130121-3243) and Add Daemon termination
[platform/core/uifw/tts.git] / client / tts.c
old mode 100644 (file)
new mode 100755 (executable)
index 3b31941..3b99caf
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved 
+*  Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd All Rights Reserved 
 *  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
@@ -14,6 +14,9 @@
 
 #include <sys/wait.h>
 #include <Ecore.h>
+#include <sys/stat.h>
+#include <sys/types.h> 
+#include <dirent.h>
 
 #include "tts_main.h"
 #include "tts_client.h"
@@ -94,7 +97,8 @@ int tts_destroy(tts_h tts)
                ret = tts_dbus_request_finalize(client->uid);
                if (0 != ret) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request finalize");
-               }   
+               }
+               g_is_daemon_started = false;
        case TTS_STATE_CREATED:
                /* Free resources */
                tts_client_destroy(tts);
@@ -232,6 +236,7 @@ int tts_unprepare(tts_h tts)
        if (0 != ret) {
                SLOG(LOG_WARN, TAG_TTSC, "[ERROR] Fail to request finalize");
        }
+       g_is_daemon_started = false;
 
        client->before_state = client->current_state;
        client->current_state = TTS_STATE_CREATED;
@@ -831,14 +836,14 @@ int tts_unset_state_changed_cb(tts_h tts)
 int tts_set_utterance_started_cb(tts_h tts, tts_utterance_started_cb callback, void* user_data)
 {
        if (NULL == tts || NULL == callback) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : Input parameter is null");
                return TTS_ERROR_INVALID_PARAMETER;
        }
 
        tts_client_s* client = tts_client_get(tts);
 
        if (NULL == client) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set utt started cb : A handle is not valid");
                return TTS_ERROR_INVALID_PARAMETER;
        }
 
@@ -989,74 +994,78 @@ int tts_unset_error_cb(tts_h tts)
        return 0;
 }
 
-static bool _tts_is_alive()
+int __get_cmd_line(char *file, char *buf) 
 {
        FILE *fp = NULL;
-       char buff[256];
-       char cmd[256];
+       int i;
 
-       memset(buff, '\0', sizeof(char) * 256);
-       memset(cmd, '\0', sizeof(char) * 256);
-
-       if ((fp = popen("ps -eo \"cmd\"", "r")) == NULL) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] popen error");
-               return FALSE;
+       fp = fopen(file, "r");
+       if (fp == NULL) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get command line");
+               return -1;
        }
 
-       while(fgets(buff, 255, fp)) {
-               sscanf(buff, "%s", cmd);
+       memset(buf, 0, 256);
+       fgets(buf, 256, fp);
+       fclose(fp);
 
-               if (0 == strncmp(cmd, "[tts-daemon]", strlen("[tts-daemon]")) ||
-                       0 == strncmp(cmd, "tts-daemon", strlen("tts-daemon")) ||
-                       0 == strncmp(cmd, "/usr/bin/tts-daemon", strlen("/usr/bin/tts-daemon"))) {
-                       SLOG(LOG_DEBUG, TAG_TTSC, "tts-daemon is ALIVE !!");
-                       fclose(fp);
-                       return TRUE;
-               }
-       }
+       return 0;
+}
 
-       fclose(fp);
+static bool _tts_is_alive()
+{
+       DIR *dir;
+       struct dirent *entry;
+       struct stat filestat;
+       
+       int pid;
+       char cmdLine[256];
+       char tempPath[256];
 
-       SLOG(LOG_DEBUG, TAG_TTSC, "THERE IS NO tts-daemon !!");
+       dir  = opendir("/proc");
+       if (NULL == dir) {
+               SLOG(LOG_ERROR, TAG_TTSC, "process checking is FAILED");
+               return FALSE;
+       }
 
-       return FALSE;
-}
+       while ((entry = readdir(dir)) != NULL) {
+               if (0 != lstat(entry->d_name, &filestat))
+                       continue;
 
+               if (!S_ISDIR(filestat.st_mode))
+                       continue;
 
-static void __my_sig_child(int signo, siginfo_t *info, void *data)
-{
-       int status;
-       pid_t child_pid, child_pgid;
+               pid = atoi(entry->d_name);
+               if (pid <= 0) continue;
 
-       child_pgid = getpgid(info->si_pid);
-       SLOG(LOG_DEBUG, TAG_TTSC, "Signal handler: dead pid = %d, pgid = %d", info->si_pid, child_pgid);
+               sprintf(tempPath, "/proc/%d/cmdline", pid);
+               if (0 != __get_cmd_line(tempPath, cmdLine)) {
+                       continue;
+               }
 
-       while (0 < (child_pid = waitpid(-1, &status, WNOHANG))) {
-               if(child_pid == child_pgid)
-                       killpg(child_pgid, SIGKILL);
+               if ( 0 == strncmp(cmdLine, "[tts-daemon]", strlen("[tts-daemon]")) ||
+                       0 == strncmp(cmdLine, "tts-daemon", strlen("tts-daemon")) ||
+                       0 == strncmp(cmdLine, "/usr/bin/tts-daemon", strlen("/usr/bin/tts-daemon"))) {
+                               SLOG(LOG_DEBUG, TAG_TTSC, "tts-daemon is ALIVE !!");
+                               closedir(dir);
+                               return TRUE;
+               }
        }
 
-       return;
+       SLOG(LOG_DEBUG, TAG_TTSC, "THERE IS NO tts-daemon !!");
+
+       closedir(dir);
+       return FALSE;
 }
 
 static int __tts_check_tts_daemon()
 {
-       if (TRUE == _tts_is_alive())
+       if (TRUE == _tts_is_alive()) {
                return 0;
+       }
        
        /* fork-exec tts-daemom */
        int pid, i;
-       struct sigaction act, dummy;
-
-       act.sa_handler = NULL;
-       act.sa_sigaction = __my_sig_child;
-       sigemptyset(&act.sa_mask);
-       act.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
-       
-       if (sigaction(SIGCHLD, &act, &dummy) < 0) {
-               SLOG(LOG_ERROR, TAG_TTSC, "Cannot make a signal handler");
-               return -1;
-       }
 
        pid = fork();