| 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 <algorithm> |
| 21 |
#include <cerrno> |
| 22 |
#include <cstring> |
| 23 |
#include <map> |
| 24 |
#include <numeric> |
| 25 |
|
| 26 |
#include "application-impl-base.h" |
| 27 |
#include "registry-impl.h" |
| 28 |
#include "second-exec-core.h" |
| 29 |
#include "string-util.h" |
| 30 |
#include "utils.h" |
| 31 |
#include "lomiri-app-launch-trace.h" |
| 32 |
|
| 33 |
namespace lomiri |
| 34 |
{ |
| 35 |
namespace app_launch |
| 36 |
{ |
| 37 |
namespace app_impls |
| 38 |
{ |
| 39 |
|
| 40 |
Base::Base(const std::shared_ptr<Registry::Impl>& registry) |
| 41 |
: registry_(registry) |
| 42 |
{ |
| 43 |
g_debug("Application construction: %p", static_cast<void*>(this)); |
| 44 |
} |
| 45 |
|
| 46 |
Base::~Base() |
| 47 |
{ |
| 48 |
g_debug("Application deconstruction: %p", static_cast<void*>(this)); |
| 49 |
} |
| 50 |
|
| 51 |
bool Base::hasInstances() |
| 52 |
{ |
| 53 |
return !instances().empty(); |
| 54 |
} |
| 55 |
|
| 56 |
/** Function to create all the standard environment variables that we're |
| 57 |
building for everyone. Mostly stuff involving paths. |
| 58 |
|
| 59 |
\param package Name of the package |
| 60 |
\param pkgdir Directory that the package lives in |
| 61 |
*/ |
| 62 |
std::list<std::pair<std::string, std::string>> Base::confinedEnv(const std::string& package, const std::string& pkgdir) |
| 63 |
{ |
| 64 |
std::list<std::pair<std::string, std::string>> retval{{"LOMIRI_APPLICATION_ISOLATION", "1"}}; |
| 65 |
|
| 66 |
/* C Funcs can return null, which offends std::string */ |
| 67 |
auto cset = [&retval](const gchar* key, const gchar* value) { |
| 68 |
if (value != nullptr) |
| 69 |
{ |
| 70 |
g_debug("Setting '%s' to '%s'", key, value); |
| 71 |
retval.emplace_back(std::make_pair(key, value)); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
cset("XDG_CACHE_HOME", g_get_user_cache_dir()); |
| 76 |
cset("XDG_CONFIG_HOME", g_get_user_config_dir()); |
| 77 |
cset("XDG_DATA_HOME", g_get_user_data_dir()); |
| 78 |
cset("XDG_RUNTIME_DIR", g_get_user_runtime_dir()); |
| 79 |
|
| 80 |
/* Add the application's dir to the list of sources for data */ |
| 81 |
auto basedatadirs = unique_gchar(g_strjoinv(":", (gchar**)g_get_system_data_dirs())); |
| 82 |
auto datadirs = unique_gchar(g_strjoin(":", pkgdir.c_str(), basedatadirs.get(), nullptr)); |
| 83 |
cset("XDG_DATA_DIRS", datadirs.get()); |
| 84 |
|
| 85 |
/* Set TMPDIR to something sane and application-specific */ |
| 86 |
auto tmpdir = unique_gchar(g_strdup_printf("%s/confined/%s", g_get_user_runtime_dir(), package.c_str())); |
| 87 |
cset("TMPDIR", tmpdir.get()); |
| 88 |
g_debug("Creating '%s'", tmpdir.get()); |
| 89 |
g_mkdir_with_parents(tmpdir.get(), 0700); |
| 90 |
|
| 91 |
/* Do the same for nvidia */ |
| 92 |
auto nv_shader_cachedir = unique_gchar(g_strdup_printf("%s/%s", g_get_user_cache_dir(), package.c_str())); |
| 93 |
cset("__GL_SHADER_DISK_CACHE_PATH", nv_shader_cachedir.get()); |
| 94 |
|
| 95 |
return retval; |
| 96 |
} |
| 97 |
|
| 98 |
/** Generates an instance string based on the clock if we're a multi-instance |
| 99 |
application. */ |
| 100 |
std::string Base::getInstance(const std::shared_ptr<app_info::Desktop>& desktop) const |
| 101 |
{ |
| 102 |
if (!desktop) |
| 103 |
{ |
| 104 |
g_warning("Invalid desktop file passed to getInstance"); |
| 105 |
return {}; |
| 106 |
} |
| 107 |
|
| 108 |
if (desktop->singleInstance()) |
| 109 |
{ |
| 110 |
return {}; |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
return std::to_string(g_get_real_time()); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
std::shared_ptr<Application::Instance> Base::findInstance(const pid_t& pid) |
| 119 |
{ |
| 120 |
for (auto instance : instances()) |
| 121 |
{ |
| 122 |
if (instance->hasPid(pid)) |
| 123 |
{ |
| 124 |
return instance; |
| 125 |
} |
| 126 |
} |
| 127 |
return nullptr; |
| 128 |
} |
| 129 |
|
| 130 |
} // namespace app_impls |
| 131 |
} // namespace app_launch |
| 132 |
} // namespace lomiri |