Tizen 2.4 migration and fix compile warning
[platform/core/system/system-popup.git] / src / launcher / app.c
1 /*
2  * popup-launcher
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include "launcher.h"
21
22 static int launch_app(char *appname,
23                 char *key1, char *value1,
24                 char *key2, char *value2,
25                 char *key3, char *value3)
26 {
27         int ret;
28         bundle *b;
29
30         if (!appname)
31                 return -EINVAL;
32
33         b = bundle_create();
34         if (!b) {
35                 return -ENOMEM;
36         }
37
38         if (key1 && value1) {
39                 ret = bundle_add(b, key1, value1);
40                 if (ret < 0)
41                         goto out;
42         }
43
44         if (key2 && value2) {
45                 ret = bundle_add(b, key2, value2);
46                 if (ret < 0)
47                         goto out;
48         }
49
50         if (key3 && value3) {
51                 ret = bundle_add(b, key3, value3);
52                 if (ret < 0)
53                         goto out;
54         }
55
56         ret = aul_launch_app(appname, b);
57         if (ret <= 0)
58                 _E("FAIL: aul_launch_app(%s)", appname);
59
60 out:
61         bundle_free(b);
62         return ret;
63 }
64
65 static int launch_app_raw(char *path, char **argv)
66 {
67         pid_t pid;
68         int ret;
69
70         if (!path || access(path, X_OK) != 0) {
71                 _E("Cannot execute (%s)", path);
72                 return -EINVAL;
73         }
74
75         pid = fork();
76         if (pid < 0) {
77                 _E("Failed to fork (%d)", errno);
78                 return -errno;
79         }
80
81         if (pid > 0) { /* Parent */
82                 return pid;
83         }
84
85         /* Child */
86         ret = execvp(path, argv);
87
88         /* Failed */
89         _E("Failed execvp(ret: %d, errno: %d)", ret, errno);
90         return -errno;
91 }
92
93 DBusMessage *launch_app_no_param(E_DBus_Object *obj,
94                                 DBusMessage *msg, char *appname)
95 {
96         DBusMessageIter iter;
97         DBusMessage *reply;
98         int ret;
99
100         if (!appname) {
101                 ret = -EINVAL;
102                 goto out;
103         }
104
105         _I("launch app (%s)", appname);
106
107         ret = launch_app(appname, NULL, NULL, NULL, NULL, NULL, NULL);
108         if (ret < 0)
109                 _E("FAIL: launch_app(): %d", ret);
110
111 out:
112         reply = dbus_message_new_method_return(msg);
113         dbus_message_iter_init_append(reply, &iter);
114         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
115
116         return reply;
117 }
118
119 DBusMessage *launch_app_single_param(E_DBus_Object *obj,
120                                 DBusMessage *msg, char *appname)
121 {
122         DBusError err;
123         DBusMessageIter iter;
124         DBusMessage *reply;
125         char *key;
126         char *value;
127         int ret;
128
129         if (!appname) {
130                 ret = -EINVAL;
131                 goto out;
132         }
133
134         _I("launch app (%s)", appname);
135
136         dbus_error_init(&err);
137
138         if (!dbus_message_get_args(msg, &err,
139                     DBUS_TYPE_STRING, &key,
140                     DBUS_TYPE_STRING, &value,
141                         DBUS_TYPE_INVALID)) {
142                 _E("there is no message");
143                 ret = -EINVAL;
144                 goto out;
145         }
146
147         if (!key || !value) {
148                 _E("message is invalid!");
149                 ret = -EINVAL;
150                 goto out;
151         }
152
153         ret = launch_app(appname, key, value, NULL, NULL, NULL, NULL);
154         if (ret < 0)
155                 _E("FAIL: launch_app(): %d", ret);
156
157 out:
158         reply = dbus_message_new_method_return(msg);
159         dbus_message_iter_init_append(reply, &iter);
160         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
161
162         return reply;
163 }
164
165 DBusMessage *terminate_app_by_pid(E_DBus_Object *obj, DBusMessage *msg)
166 {
167         DBusError err;
168         DBusMessageIter iter;
169         DBusMessage *reply;
170         pid_t pid;
171         int ret;
172
173         dbus_error_init(&err);
174
175         if (!dbus_message_get_args(msg, &err,
176                     DBUS_TYPE_INT32, &pid,
177                         DBUS_TYPE_INVALID)) {
178                 _E("there is no message");
179                 ret = -EINVAL;
180                 goto out;
181         }
182
183         if (pid < 0) {
184                 _E("message is invalid!");
185                 ret = -EINVAL;
186                 goto out;
187         }
188
189         ret = aul_terminate_pid(pid);
190         if (ret < 0)
191                 _E("FAIL: aul_terminate_pid(): %d", ret);
192
193 out:
194         reply = dbus_message_new_method_return(msg);
195         dbus_message_iter_init_append(reply, &iter);
196         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
197
198         return reply;
199 }
200
201 DBusMessage *launch_system_servant_app(E_DBus_Object *obj,      DBusMessage *msg, char **argv)
202 {
203         DBusMessageIter iter;
204         DBusMessage *reply;
205         int ret;
206
207         if (!argv) {
208                 ret = -EINVAL;
209                 goto out;
210         }
211
212         _I("launch app raw(%s)", argv[0]);
213
214         ret = launch_app_raw(argv[0], argv);
215         if (ret < 0)
216                 _E("FAIL: launch_app_raw: %d", ret);
217
218 out:
219         reply = dbus_message_new_method_return(msg);
220         dbus_message_iter_init_append(reply, &iter);
221         dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
222
223         return reply;
224 }
225