2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <app_manager.h>
26 #include <sys/types.h>
27 #include <dpl/wrt-dao-ro/WrtDatabase.h>
28 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
29 #include <dpl/wrt-dao-ro/feature_dao_read_only.h>
30 #include <dpl/wrt-dao-ro/common_dao_types.h>
31 #include <dpl/wrt-dao-ro/wrt_db_types.h>
32 #include <dpl/wrt-dao-rw/global_dao.h>
33 #include <dpl/wrt-dao-ro/global_dao_read_only.h>
34 #include <dpl/wrt-dao-ro/config_parser_data.h>
35 #include <dpl/exception.h>
38 #define TIMEOUT_DEFAULT 10
39 #define ROOT_DEFAULT_UID 0
40 #define ROOT_DEFAULT_GID 0
41 #define WEBAPP_DEFAULT_UID 5000
42 #define WEBAPP_DEFAULT_GID 5000
43 #define LOGGING_DEFAULT_GID 6509
44 #define RETURN_ERROR -1
46 static const char *program;
53 WrtDB::WrtDatabase::attachToThreadRW();
58 WrtDB::WrtDatabase::detachFromThread();
62 static std::unique_ptr<DBConnection> g_dbConnection;
66 char* guid; /**< the widget's id
67 * (read from its config.xml during installation)*/
68 char* name; /**< the widget's name
69 * (read from its config.xml during installation)*/
70 char* version; /**< the widget's varsion
71 * (read from its config.xml during installation)*/
72 char* pkg_id; /**< the widget's pkg id */
77 static void free_widget_info(widget_info* widget_info)
80 delete[] widget_info->guid;
81 delete[] widget_info->name;
82 delete[] widget_info->version;
83 delete[] widget_info->pkg_id;
84 delete[] widget_info->app_id;
89 static char* new_strdup(const char *str)
91 size_t size = strlen(str);
92 char* ret = new char[size + 1];
97 static bool attachDbConnection()
99 if (NULL == g_dbConnection.get()) {
101 g_dbConnection.reset(new DBConnection());
102 g_dbConnection->AttachDatabase();
104 Catch(DPL::DB::SqlConnection::Exception::Base) {
105 LogDebug("Fail to connect DB");
112 static bool display_widget_info()
114 if (!attachDbConnection()) {
118 WidgetDAOReadOnlyList widgetList;
121 widgetList = WrtDB::WidgetDAOReadOnly::getWidgetList();
122 } Catch (WrtDB::WidgetDAOReadOnly::Exception::DatabaseError) {
123 LogError("Fail to get WidgetList");
127 printf("%3s %32s %16s %64s %16s %24s\n",
128 "No", "Name", "Version", "GUID", "Package ID", "App ID");
129 printf("%3s %32s %16s %64s %16s %24s\n",
130 "--", "--", "----", "-------", "-----", "-----");
133 FOREACH(dao, widgetList) {
134 widget_info *info = new widget_info;
135 memset(info, 0x00, sizeof(widget_info));
137 WrtDB::WidgetGUID guid = (*dao)->getGUID();
138 DPL::Optional<DPL::String> version = (*dao)->getVersion();
139 WrtDB::TizenAppId appid = (*dao)->getTzAppId();
140 WrtDB::TizenPkgId pkgid = (*dao)->getTizenPkgId();
144 DPL::Optional<DPL::String> widget_name;
145 DPL::OptionalString dl = (*dao)->getDefaultlocale();
146 WrtDB::WidgetLocalizedInfo localizedInfo;
148 DPL::String languageTag(L"");
149 localizedInfo = (*dao)->getLocalizedInfo(languageTag);
151 localizedInfo = (*dao)->getLocalizedInfo(*dl);
154 widget_name = localizedInfo.name;
156 /*end get WidgetName*/
157 if (!widget_name.IsNull()) {
158 info->name = new_strdup(DPL::ToUTF8String(*widget_name).c_str());
160 if (!version.IsNull()) {
161 info->version = new_strdup(DPL::ToUTF8String(*version).c_str());
163 std::string installedWidgetVersionString;
164 installedWidgetVersionString = "";
165 info->version = new_strdup(installedWidgetVersionString.c_str());
167 if (!guid.IsNull()) {
168 info->guid = new_strdup(DPL::ToUTF8String(*guid).c_str());
171 info->app_id = new_strdup(DPL::ToUTF8String(appid).c_str());
172 info->pkg_id = new_strdup(DPL::ToUTF8String(pkgid).c_str());
174 printf("%3i %32s %16s %64s %16s %24s\n",
176 !info->name ? "[NULL]" : info->name,
177 !info->version ? "[NULL]" : info->version,
178 !info->guid ? "[NULL]" : info->guid,
179 !info->pkg_id ? "[NULL]" : info->pkg_id,
180 !info->app_id ? "[NULL]" : info->app_id);
182 free_widget_info(info);
188 static void print_help(FILE *stream, int /*exit_code*/)
190 fprintf(stream, "Usage : %s [ ... ]\n", program);
193 " -h --help Display this usage information.\n"
194 " -l --list Display installed widgets list\n"
195 " -s [tizen application ID] --start Launch widget with tizen application ID\n"
196 " -k [tizen application ID] --kill Kill widget with tizen application ID\n"
197 " -r [tizen application ID] --is-running Check whether widget is running by tizen application ID,\n"
198 " If widget is running, 0(zero) will be returned.\n"
199 " -d --debug Activate debug mode\n"
200 " -t [second] --timeout Set timeout of response from widget in debug mode\n"
201 " if you emit this option, 5 seconds is set in debug mode\n"
205 extern "C" int service_to_bundle(service_h service, bundle** data);
207 static void serviceReplyCallback(service_h /*request*/,
209 service_result_e /*result*/,
212 Ecore_Timer* serviceTimer = static_cast<Ecore_Timer*>(data);
213 if (serviceTimer != NULL) {
214 ecore_timer_del(serviceTimer);
218 service_to_bundle(reply, &b);
219 const char* port = appsvc_get_data(b, "port");
220 if (port != NULL && strlen(port) > 0) {
221 printf("port: %s\n", port);
222 printf("result: %s\n", "launched");
224 printf("result: %s\n", "failed");
226 ecore_main_loop_quit();
230 static Eina_Bool timerCallback(void* /*data*/)
232 printf("result: %s\n", "failed");
233 ecore_main_loop_quit();
237 int main(int argc, char* argv[])
239 UNHANDLED_EXCEPTION_HANDLER_BEGIN
241 int next_opt, opt_idx = 0;
242 int timeout = TIMEOUT_DEFAULT;
243 char applicationId[256] = "";
244 char temp_arg[256] = "";
247 bool isDebugMode = false;
248 bool dispHelp = false;
249 bool dispList = false;
250 Ecore_Timer* serviceTimer = NULL;
252 service_h serviceHandle = NULL;
253 int ret = SERVICE_ERROR_NONE;
257 static struct option long_options[] = {
258 { "help", no_argument, 0, 'h' },
259 { "list", no_argument, 0, 'l' },
260 { "start", required_argument, 0, 's' },
261 { "kill", required_argument, 0, 'k' },
262 { "is-running", required_argument, 0, 'r' },
263 { "debug", no_argument, 0, 'd' },
264 { "timeout", required_argument, 0, 't' },
268 if (argv[1] == NULL) {
269 /* exit if any argument doesn't exist */
270 print_help(stdout, 0);
274 if (ROOT_DEFAULT_UID == geteuid()) {
275 if (RETURN_ERROR == setuid(ROOT_DEFAULT_UID)) {
276 perror("Fail to set uid");
279 if (ROOT_DEFAULT_GID == getegid()) {
280 if (RETURN_ERROR == setgid(ROOT_DEFAULT_GID)) {
281 perror("Fail to set gid");
286 next_opt = getopt_long(argc,
288 "hls:k:r:dt:v:c:i:m:",
295 print_help(stdout, 0);
304 if (!display_widget_info()) {
305 printf("Fail to display the list of installed widgets");
314 strncpy(temp_arg, optarg, strlen(optarg));
319 timeout = atoi(optarg);
321 timeout = TIMEOUT_DEFAULT;
333 print_help(stdout, 0);
336 } while (next_opt != -1);
338 if ((op == 's') || (op == 'k') || (op == 'r')) {
341 if (NULL == g_dbConnection.get()) {
343 g_dbConnection.reset(new DBConnection());
344 g_dbConnection->AttachDatabase();
346 Catch(DPL::DB::SqlConnection::Exception::Base) {
347 LogDebug("Fail to connect DB");
351 DPL::OptionalString normal_str = DPL::FromUTF8String(temp_arg);
352 WrtDB::NormalizeAndTrimSpaceString(normal_str);
353 std::string normal_arg = DPL::ToUTF8String(*normal_str);
355 WidgetDAOReadOnlyList widgetList =
356 WrtDB::WidgetDAOReadOnly::getWidgetList();
357 FOREACH(dao, widgetList) {
358 WrtDB::TizenAppId tizenAppId = (*dao)->getTzAppId();
359 if (!strcmp(DPL::ToUTF8String(tizenAppId).c_str(),
362 temp = DPL::ToUTF8String(tizenAppId);
367 strncpy(applicationId, temp.c_str(), strlen(temp.c_str()));
369 printf("result: %s\n", "failed");
375 if (strlen(applicationId) <= 0) {
376 printf("result: %s\n", "failed");
381 ret = service_create(&serviceHandle);
382 if (SERVICE_ERROR_NONE != ret && NULL == serviceHandle) {
383 printf("result: %s\n", "failed");
388 ret = service_set_package(serviceHandle, applicationId);
389 if (SERVICE_ERROR_NONE != ret) {
390 printf("result: %s\n", "failed");
391 service_destroy(serviceHandle);
395 if (true == isDebugMode) {
397 ret = service_add_extra_data(serviceHandle, "debug", "true");
398 if (SERVICE_ERROR_NONE != ret) {
399 LogError("Fail to set debug mode [" << ret << "]");
400 service_destroy(serviceHandle);
405 snprintf(pid, sizeof(pid), "%d", getpid());
406 ret = service_add_extra_data(serviceHandle, "pid", pid);
407 if (SERVICE_ERROR_NONE != ret) {
408 LogError("Fail to set pid [" << ret << "]");
409 service_destroy(serviceHandle);
414 serviceTimer = ecore_timer_add(timeout, timerCallback, NULL);
415 ret = service_send_launch_request(serviceHandle,
416 serviceReplyCallback,
419 ret = service_send_launch_request(serviceHandle, NULL, NULL);
422 if (SERVICE_ERROR_NONE != ret) {
423 printf("result: %s\n", "");
424 service_destroy(serviceHandle);
428 service_destroy(serviceHandle);
430 if (true == isDebugMode) {
431 // wait response from callee
432 ecore_main_loop_begin();
435 // This text should be showed for IDE
436 printf("result: %s\n", "launched");
439 } else if (op == 'k') {
440 bool isRunning = false;
442 //checks whether the application is running
443 ret = app_manager_is_running(applicationId, &isRunning);
444 if (APP_MANAGER_ERROR_NONE != ret) {
445 printf("result: %s\n", "failed");
449 if (true == isRunning) {
450 // get app_context for running application
451 // app_context must be released with app_context_destroy
452 app_context_h appCtx = NULL;
453 ret = app_manager_get_app_context(applicationId, &appCtx);
454 if (APP_MANAGER_ERROR_NONE != ret) {
455 printf("result: %s\n", "failed");
459 // terminate app_context_h
460 ret = app_manager_terminate_app(appCtx);
461 if (APP_MANAGER_ERROR_NONE != ret) {
462 printf("result: %s\n", "failed");
463 app_context_destroy(appCtx);
466 printf("result: %s\n", "killed");
467 app_context_destroy(appCtx);
471 printf("result: %s\n", "App isn't running");
474 } else if (op == 'r') {
475 bool isRunning = false;
476 ret = app_manager_is_running(applicationId, &isRunning);
478 if (APP_MANAGER_ERROR_NONE != ret) {
479 printf("result: %s\n", "failed");
483 if (true == isRunning) {
484 printf("result: %s\n", "running");
487 printf("result: %s\n", "not running");
494 UNHANDLED_EXCEPTION_HANDLER_END