f006462e75105892d5b9a4917b7185fb109e2443
[platform/core/connectivity/stc-manager.git] / plugin / stc-popup / stc-plugin-stc-popup.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include <string.h>
22
23 #include "stc-plugin-popup.h"
24 #include "stc-error.h"
25 #include "stc-manager-util.h"
26
27 #define APP_LAUNCHER_EXEC_PATH "/usr/bin/app_launcher"
28 #define STC_POPUP_PACKAGE_NAME "net.stc-popup"
29
30 #define MAX_SIZE_ERROR_BUFFER 256
31 #define STC_POPUP_START_TIMER 10
32
33 static void _no_wait_signal_handler(int sig)
34 {
35         pid_t child_pid = 0;
36         int state = 0;
37
38         child_pid = waitpid(-1, &state, WNOHANG);
39
40         STC_LOGD("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
41 }
42
43 static int _execute_file_no_wait(const char *file_path, char *const args[])
44 {
45         pid_t pid = 0;
46         int rv = 0;
47         errno = 0;
48         register unsigned int index = 0;
49
50         struct sigaction act;
51         int state = 0;
52         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
53
54         act.sa_handler = _no_wait_signal_handler;
55         sigemptyset(&act.sa_mask);
56         act.sa_flags = 0;
57
58         state = sigaction(SIGCHLD, &act, 0);
59         if (state != 0) {
60                 STC_LOGD("sigaction() : %d");
61                 return -1;
62         }
63
64         while (args[index] != NULL) {
65                 STC_LOGD("%s", args[index]);
66                 index++;
67         }
68
69         if (!(pid = fork())) {
70                 STC_LOGD("pid(%d), ppid (%d)", getpid(), getppid());
71                 STC_LOGD("Inside child, exec (%s) command", file_path);
72
73                 errno = 0;
74                 if (execvp(file_path, args) == -1) {
75                         STC_LOGE("Fail to execute command (%s)",
76                                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
77                         return -1;
78                 }
79         } else if (pid > 0) {
80                 STC_LOGE("Successfully launched child process");
81                 return rv;
82         }
83
84         STC_LOGD("failed to fork(%s)",
85                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
86         return -EIO;
87 }
88
89 static gboolean _start_stc_popup(gpointer user_data)
90 {
91         gboolean *launched = (gboolean *)user_data;
92         int ret;
93         const char *start_path = APP_LAUNCHER_EXEC_PATH;
94         char *const start_args[] = { APP_LAUNCHER_EXEC_PATH, "-s",
95                                      STC_POPUP_PACKAGE_NAME, NULL };
96
97         ret = _execute_file_no_wait(start_path, start_args);
98         if (ret < 0) {
99                 STC_LOGD("Failed to launch stc-popup, ret : %d", ret);
100                 *launched = FALSE;
101                 return TRUE;
102         }
103
104         *launched = FALSE;
105         return FALSE;
106 }
107
108 int stc_plugin_popup_initialize(void)
109 {
110         static gboolean stc_popup_launched = FALSE;
111
112         if (stc_popup_launched == FALSE)
113                 g_timeout_add_seconds(STC_POPUP_START_TIMER, _start_stc_popup,
114                                       &stc_popup_launched);
115         else
116                 return STC_ERROR_FAIL;
117
118         return STC_ERROR_NONE;
119 }
120
121 int stc_plugin_popup_deinitialize(void)
122 {
123         /* close stc popup application  */
124         return STC_ERROR_NONE;
125 }
126
127 int stc_plugin_popup_show(const char *content, const char *type,
128                           const char *app_id, const char *iftype,
129                           const char *limit)
130 {
131         return 0;
132 }
133
134 API stc_plugin_popup_s stc_plugin_popup = {
135         .initialize_plugin =
136                 stc_plugin_popup_initialize,
137         .deinitialize_plugin =
138                 stc_plugin_popup_deinitialize,
139         .show_popup = stc_plugin_popup_show
140 };