Upstream version 6.35.131.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalk_pkg_helper.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // On Tizen installing applications requires super-user powers primarily because
6 // some pieces of information of the application are put on globally accessible
7 // locations. This helper will run with super-user powers (via suid) and will
8 // be called by Crosswalk (now running as a normal user) so all the activities
9 // that required 'root' access are done by a small code base.
10
11 #include "base/files/file_path.h"
12 #include "base/file_util.h"
13 #include "xwalk/application/tools/tizen/xwalk_pkg_installer.h"
14
15 namespace {
16
17 int usage(const char* program) {
18   fprintf(stdout, "%s - Crosswalk Tizen Application Installation helper\n\n",
19           basename(program));
20   fprintf(stdout, "Usage: \n"
21           "\t%s --install <appid> <xml> <icon>\n"
22           "\t%s --uninstall <appid>\n",
23           program, program);
24   return 1;
25 }
26
27 }  // namespace
28
29 int main(int argc, char *argv[]) {
30   bool result = false;
31
32   if (argc <= 2)
33     return usage(argv[0]);
34
35   // When installing an application on Tizen, the libraries used require
36   // some steps to be run as root (UID 0) and fail otherwise, so we force
37   // this tool to assume the root UID.
38   if (setuid(0)) {
39     fprintf(stderr, "Make sure '%s' is set-user-ID-root\n", argv[0]);
40     return 1;;
41   }
42
43   PkgInstaller installer(argv[2]);
44   if (!strcmp(argv[1], "--install")) {
45     if (argc != 5)
46       return usage(argv[0]);
47
48     result = installer.InstallApplication(argv[3], argv[4]);
49   } else if (!strcmp(argv[1], "--uninstall")) {
50     if (argc != 3)
51       return usage(argv[0]);
52
53     result = installer.UninstallApplication();
54   } else {
55     return usage(argv[0]);
56   }
57
58   // Convetion is to return 0 on success.
59   return result ? 0 : 1;
60 }