summaryrefslogtreecommitdiff
path: root/src/launchpad-parser/launchpad_parser_plugin.cc
blob: e47adf1f8e3518a3a6ad85515eccfa207250a68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <pkgmgr_installer_info.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <fstream>
#include <string>

#include "launchpad-parser/launchpad_parser_plugin.hh"
#include "launchpad-parser/log_private.hh"

#define LOADERS_DIRECTORY_PATH "/opt/share/loaders/"

namespace launchpad_parser_plugin {

std::string LaunchpadParser::GetFilePath(const std::string& id) {
  return "/opt/share/loaders/" + id + ".loader";
}

int LaunchpadParser::WriteToFile(const std::string& pkgid) {
  if (access(LOADERS_DIRECTORY_PATH, F_OK) != 0)
    mkdir(LOADERS_DIRECTORY_PATH, 0644);

  LOGI("write to file (%zu)", loader_list_.size());
  for (auto& i : loader_list_) {
    std::ofstream out_file;
    LOGI("write ID (%s)", i->GetId().c_str());
    out_file.open(GetFilePath(i->GetId()));
    out_file << "[LOADER]\n";
    out_file << "NAME " + i->GetId() + "\n";
    out_file << "EXE  /usr/bin/launchpad-loader \n";
    out_file << "APP_TYPE  capp|c++app \n";
    out_file << "DETECTION_METHOD  TIMEOUT|VISIBILITY \n";
    out_file << "TIMEOUT    5000 \n";
    out_file << "TTL    " + std::to_string(i->GetTimeToLive()) + "\n";
    out_file << "OWNER    " + pkgid + "\n";
    out_file << "EXTRA    loader_type  common-loader \n";
    if (i->GetPreloadLib().size() > 0) {
      out_file << "EXTRA_ARRAY    preload \n";
      for (auto& lib : i->GetPreloadLib()) {
        out_file << "EXTRA_ARRAY_VAL    " LIBDIR "/" + lib + "\n";
      }
    }
    out_file.close();
  }
  return 0;
}

bool LaunchpadParser::IsValidId(const std::string& loader_id,
    const std::string& pkgid) {
  std::string needle("../");
  size_t found = loader_id.find(needle);
  if (found != std::string::npos) {
    _E("Invalid loader_id(%s)", loader_id.c_str());
    return false;
  }

  std::ifstream in_file(GetFilePath(loader_id).c_str());
  if (!in_file.good())
    return true;
  std::string key, val;
  in_file >> key;
  while (in_file >> key >> val) {
    if (key == "OWNER") {
      LOGI("key (%s), val (%s)", key.c_str(), val.c_str());
      if (val == pkgid)
        return true;
    }
  }
  return false;
}

int LaunchpadParser::Install(xmlDocPtr doc, const std::string& pkgid) {
  pkgmgr_privilege_level level;
  pkgmgr_installer_info_get_privilege_level(&level);
  if (level != PM_PRIVILEGE_PLATFORM) {
    LOGE("Privilege level(%d) is not platform", static_cast<int>(level));
    return -1;
  }

  xmlNode* root = xmlDocGetRootElement(doc);
  if (root == nullptr)
    return -1;

  for (xmlNode* node = root->children; node; node = node->next) {
    if (!node->name)
      continue;

    std::string name = std::string(reinterpret_cast<const char*>(node->name));
    if (name != "provides-appdefined-loader")
      continue;

    xmlChar* val = xmlGetProp(node, reinterpret_cast<const xmlChar*>("id"));
    if (val == nullptr)
      continue;

    std::shared_ptr<LoaderInfo> info =
        std::make_shared<LoaderInfo>(std::string(reinterpret_cast<char*>(val)));
    xmlFree(val);
    if (!IsValidId(info->GetId(), pkgid))
      return -1;

    xmlChar* ttl = xmlGetProp(node,
        reinterpret_cast<const xmlChar*>("time-to-live"));
    if (ttl) {
      info->SetTimeToLive(std::stoi(std::string(reinterpret_cast<char*>(ttl))));
      xmlFree(ttl);
    }

    for (xmlNode* iter = node->children; iter; iter = iter->next) {
      if (!iter->name)
        continue;
      std::string child_name =
          std::string(reinterpret_cast<const char*>(iter->name));
      if (child_name == "preload-library") {
        xmlChar* libname = xmlGetProp(iter,
            reinterpret_cast<const xmlChar*>("name"));
        if (!libname)
          continue;
        info->AddPreloadLib(std::string(reinterpret_cast<char*>(libname)));
        xmlFree(libname);
      }
    }
    loader_list_.push_back(info);
  }
  WriteToFile(pkgid);

  return 0;
}

int LaunchpadParser::Upgrade(xmlDocPtr doc, const std::string& pkgid) {
  if (UnInstall(doc, pkgid) != 0)
    return -1;

  return Install(doc, pkgid);
}

int LaunchpadParser::UnInstall(xmlDocPtr doc, const std::string& pkgid) {
  xmlNode* root = xmlDocGetRootElement(doc);
  if (root == nullptr)
    return -1;

  for (xmlNode* node = root->children; node; node = node->next) {
    if (!node->name)
      continue;

    std::string name = std::string(reinterpret_cast<const char*>(node->name));
    if (name != "provides-appdefined-loader")
      continue;

    xmlChar* val = xmlGetProp(node, reinterpret_cast<const xmlChar*>("id"));
    if (!val)
      return -1;

    std::string id = std::string(reinterpret_cast<char*>(val));
    xmlFree(val);
    if (!IsValidId(id, pkgid))
      return -1;
    remove(GetFilePath(id).c_str());
  }
  return 0;
}

}   // namspace launchpad_parser_plugin