Debian Codegraph @

liblomiri-app-launch/application-impl-legacy.cpp

raw
1 /*
2 * Copyright © 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ted Gould <[email protected]>
18 */
19
20 #include "application-impl-legacy.h"
21 #include "application-info-desktop.h"
22 #include "registry-impl.h"
23 #include "string-util.h"
24
25 #include <regex>
26 #include <lomiri/util/GlibMemory.h>
27
28 using namespace lomiri::util;
29
30 namespace lomiri
31 {
32 namespace app_launch
33 {
34 namespace app_impls
35 {
36
37 /** Path that snapd puts desktop files, we don't want to read those directly
38 in the Legacy backend. We want to use the snap backend. */
39 const std::string snappyDesktopPath{"/var/lib/snapd"};
40
41 /***********************************
42 Prototypes
43 ***********************************/
44 std::tuple<std::string, std::shared_ptr<GKeyFile>, std::string> keyfileForApp(const AppID::AppName& name);
45
46 /** Helper function to put on shared_ptr's for keyfiles */
47 void clear_keyfile(GKeyFile* keyfile)
48 {
49 if (keyfile != nullptr)
50 {
51 g_key_file_free(keyfile);
52 }
53 }
54
55 Legacy::Legacy(const AppID::AppName& appname, const std::shared_ptr<Registry::Impl>& registry)
56 : Base(registry)
57 , _appname(appname)
58 {
59 std::tie(_basedir, _keyfile, desktopPath_) = keyfileForApp(appname);
60
61 std::string rootDir = "";
62 auto rootenv = g_getenv("LOMIRI_APP_LAUNCH_LEGACY_ROOT");
63 if (rootenv != nullptr && /* Check that we have an alternate root available */
64 g_str_has_prefix(_basedir.c_str(), rootenv))
65 { /* And check that we found this in that root */
66 rootDir = rootenv;
67 }
68
69 auto flags = app_info::DesktopFlags::ALLOW_NO_DISPLAY;
70
71 appinfo_ = std::make_shared<app_info::Desktop>(appId(), _keyfile, _basedir, rootDir, flags, registry_);
72
73 if (!_keyfile)
74 {
75 throw std::runtime_error{"Unable to find keyfile for legacy application: " + appname.value()};
76 }
77
78 if (std::equal(snappyDesktopPath.begin(), snappyDesktopPath.end(), _basedir.begin()))
79 {
80 throw std::runtime_error{"Looking like a legacy app, but should be a Snap: " + appname.value()};
81 }
82
83 g_debug("Application Legacy object for app '%s'", appname.value().c_str());
84 }
85
86 std::tuple<std::string, std::shared_ptr<GKeyFile>, std::string> keyfileForApp(const AppID::AppName& name)
87 {
88 auto desktopName = name.value() + ".desktop";
89 std::string desktopPath;
90 auto keyfilecheck = [desktopName, &desktopPath](const std::string& dir) -> std::shared_ptr<GKeyFile> {
91 auto fullname = unique_gchar(g_build_filename(dir.c_str(), "applications", desktopName.c_str(), nullptr));
92 if (!g_file_test(fullname.get(), G_FILE_TEST_EXISTS))
93 {
94 return {};
95 }
96 desktopPath = fullname.get();
97
98 auto keyfile = unique_glib(g_key_file_new());
99
100 GError* error = nullptr;
101 g_key_file_load_from_file(keyfile.get(), fullname.get(), G_KEY_FILE_NONE, &error);
102
103 if (error != nullptr)
104 {
105 g_debug("Unable to load keyfile '%s' because: %s", desktopName.c_str(), error->message);
106 g_error_free(error);
107 return {};
108 }
109
110 return keyfile;
111 };
112
113 std::string basedir = g_get_user_data_dir();
114 auto retval = keyfilecheck(basedir);
115
116 auto systemDirs = g_get_system_data_dirs();
117 for (auto i = 0; !retval && systemDirs[i] != nullptr; i++)
118 {
119 basedir = systemDirs[i];
120 retval = keyfilecheck(basedir);
121 }
122
123 return std::make_tuple(basedir, retval, desktopPath);
124 }
125
126 std::shared_ptr<Application::Info> Legacy::info()
127 {
128 return appinfo_;
129 }
130
131 std::vector<std::shared_ptr<Application::Instance>> Legacy::instances()
132 {
133 auto vbase = registry_->jobs()->instances(appId(), "application-legacy");
134 return std::vector<std::shared_ptr<Application::Instance>>(vbase.begin(), vbase.end());
135 }
136
137 /** Grabs all the environment for a legacy app. Mostly this consists of
138 the exec line. Also we set the path if that is specified in the desktop
139 file. We can also set an AppArmor profile if requested. */
140 std::list<std::pair<std::string, std::string>> Legacy::launchEnv(const std::string& instance)
141 {
142 std::list<std::pair<std::string, std::string>> retval;
143
144 retval.emplace_back(std::make_pair("APP_DESKTOP_FILE_PATH", desktopPath_));
145
146 info();
147
148 auto execline = appinfo_->execLine().value();
149
150 auto snappath = getenv("SNAP");
151 if (snappath != nullptr)
152 {
153 /* This means we're inside a snap, and if we're in a snap then
154 the legacy application is in a snap. We need to try and set
155 up the proper environment for that app */
156 retval.emplace_back(std::make_pair("SNAP", snappath));
157
158 const char* legacyexec = getenv("LOMIRI_APP_LAUNCH_SNAP_LEGACY_EXEC");
159 if (legacyexec == nullptr)
160 {
161 legacyexec = "/snap/bin/lomiri-session.legacy-exec";
162 }
163
164 execline = std::string{legacyexec} + " " + execline;
165 }
166
167 retval.emplace_back(std::make_pair("APP_EXEC", execline));
168
169 /* Honor the 'Path' key if it is in the desktop file */
170 if (g_key_file_has_key(_keyfile.get(), "Desktop Entry", "Path", nullptr))
171 {
172 auto path = unique_gchar(g_key_file_get_string(_keyfile.get(), "Desktop Entry", "Path", nullptr));
173 retval.emplace_back(std::make_pair("APP_DIR", path.get()));
174 }
175
176 /* If they've asked for an Apparmor profile, let's use it! */
177 auto apparmor =
178 unique_gchar(g_key_file_get_string(_keyfile.get(), "Desktop Entry", "X-Lomiri-AppArmor-Profile", nullptr));
179 if (apparmor)
180 {
181 retval.emplace_back(std::make_pair("APP_EXEC_POLICY", apparmor.get()));
182
183 retval.splice(retval.end(), confinedEnv(_appname, "/usr/share"));
184 }
185 else
186 {
187 retval.emplace_back(std::make_pair("APP_EXEC_POLICY", "unconfined"));
188 }
189
190 retval.emplace_back(std::make_pair("INSTANCE_ID", instance));
191
192 return retval;
193 }
194
195 /** Create an UpstartInstance for this AppID using the UpstartInstance launch
196 function.
197
198 \param urls URLs to pass to the application
199 */
200 std::shared_ptr<Application::Instance> Legacy::launch(const std::vector<Application::URL>& urls)
201 {
202 auto instance = getInstance(appinfo_);
203 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this, instance]() {
204 return launchEnv(instance);
205 };
206 return registry_->jobs()->launch(appId(), "application-legacy", instance, urls, jobs::manager::launchMode::STANDARD,
207 envfunc);
208 }
209
210 /** Create an UpstartInstance for this AppID using the UpstartInstance launch
211 function with a testing environment.
212
213 \param urls URLs to pass to the application
214 */
215 std::shared_ptr<Application::Instance> Legacy::launchTest(const std::vector<Application::URL>& urls)
216 {
217 auto instance = getInstance(appinfo_);
218 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this, instance]() {
219 return launchEnv(instance);
220 };
221 return registry_->jobs()->launch(appId(), "application-legacy", instance, urls, jobs::manager::launchMode::TEST,
222 envfunc);
223 }
224
225 std::shared_ptr<Application::Instance> Legacy::findInstance(const std::string& instanceid)
226 {
227 return registry_->jobs()->existing(appId(), "application-legacy", instanceid, std::vector<Application::URL>{});
228 }
229
230 } // namespace app_impls
231 } // namespace app_launch
232 } // namespace lomiri