forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdg-app-builtins-run.c
349 lines (287 loc) · 10.6 KB
/
xdg-app-builtins-run.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "config.h"
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "libgsystem.h"
#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
#include "xdg-app-dbus.h"
#include "xdg-app-run.h"
static char *opt_arch;
static char *opt_branch;
static char *opt_command;
static gboolean opt_devel;
static char *opt_runtime;
static char **opt_allow;
static char **opt_forbid;
static GOptionEntry options[] = {
{ "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to use", "ARCH" },
{ "command", 0, 0, G_OPTION_ARG_STRING, &opt_command, "Command to run", "COMMAND" },
{ "branch", 0, 0, G_OPTION_ARG_STRING, &opt_branch, "Branch to use", "BRANCH" },
{ "devel", 'd', 0, G_OPTION_ARG_NONE, &opt_devel, "Use development runtime", NULL },
{ "runtime", 0, 0, G_OPTION_ARG_STRING, &opt_runtime, "Runtime to use", "RUNTIME" },
{ "allow", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_allow, "Environment options to set to true", "KEY" },
{ "forbid", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_forbid, "Environment options to set to false", "KEY" },
{ NULL }
};
static void
add_extension_arg (const char *directory,
const char *type, const char *extension, const char *arch, const char *branch,
GPtrArray *argv_array, GCancellable *cancellable)
{
gs_free char *extension_ref;
gs_unref_object GFile *deploy = NULL;
gs_free char *full_directory = NULL;
gboolean is_app;
is_app = strcmp (type, "app") == 0;
full_directory = g_build_filename (is_app ? "/self" : "/usr", directory, NULL);
extension_ref = g_build_filename (type, extension, arch, branch, NULL);
deploy = xdg_app_find_deploy_dir_for_ref (extension_ref, cancellable, NULL);
if (deploy != NULL)
{
gs_unref_object GFile *files = g_file_get_child (deploy, "files");
g_ptr_array_add (argv_array, g_strdup ("-b"));
g_ptr_array_add (argv_array, g_strdup_printf ("%s=%s", full_directory, gs_file_get_path_cached (files)));
}
}
static gboolean
add_extension_args (GKeyFile *metakey, const char *full_ref,
GPtrArray *argv_array, GCancellable *cancellable, GError **error)
{
gs_strfreev gchar **groups = NULL;
gs_strfreev gchar **parts = NULL;
gboolean ret = FALSE;
int i;
ret = TRUE;
parts = g_strsplit (full_ref, "/", 0);
if (g_strv_length (parts) != 4)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to determine parts from ref: %s", full_ref);
goto out;
}
groups = g_key_file_get_groups (metakey, NULL);
for (i = 0; groups[i] != NULL; i++)
{
char *extension;
if (g_str_has_prefix (groups[i], "Extension ") &&
*(extension = (groups[i] + strlen ("Extension "))) != 0)
{
gs_free char *directory = g_key_file_get_string (metakey, groups[i], "directory", NULL);
if (directory == NULL)
continue;
if (g_key_file_get_boolean (metakey, groups[i],
"subdirectories", NULL))
{
gs_free char *prefix = g_strconcat (extension, ".", NULL);
gs_strfreev char **refs = NULL;
int i;
refs = xdg_app_list_deployed_refs (parts[0], prefix, parts[2], parts[3],
cancellable, error);
if (refs == NULL)
goto out;
for (i = 0; refs[i] != NULL; i++)
{
gs_free char *extended_dir = g_build_filename (directory, refs[i] + strlen (prefix), NULL);
add_extension_arg (extended_dir, parts[0], refs[i], parts[2], parts[3],
argv_array, cancellable);
}
}
else
add_extension_arg (directory, parts[0], extension, parts[2], parts[3],
argv_array, cancellable);
}
}
ret = TRUE;
out:
return ret;
}
gboolean
xdg_app_builtin_run (int argc, char **argv, GCancellable *cancellable, GError **error)
{
GOptionContext *context;
gboolean ret = FALSE;
gs_unref_object XdgAppDir *user_dir = NULL;
gs_unref_variant_builder GVariantBuilder *optbuilder = NULL;
gs_unref_object GFile *deploy_base = NULL;
gs_unref_object GFile *var = NULL;
gs_unref_object GFile *var_tmp = NULL;
gs_unref_object GFile *var_run = NULL;
gs_unref_object GFile *app_deploy = NULL;
gs_unref_object GFile *app_files = NULL;
gs_unref_object GFile *runtime_deploy = NULL;
gs_unref_object GFile *runtime_files = NULL;
gs_unref_object GFile *metadata = NULL;
gs_unref_object GFile *runtime_metadata = NULL;
gs_unref_object XdgAppSessionHelper *session_helper = NULL;
gs_free char *metadata_contents = NULL;
gs_free char *runtime_metadata_contents = NULL;
gs_free char *runtime = NULL;
gs_free char *default_command = NULL;
gs_free char *runtime_ref = NULL;
gs_free char *app_ref = NULL;
gs_free char *path = NULL;
gs_unref_keyfile GKeyFile *metakey = NULL;
gs_unref_keyfile GKeyFile *runtime_metakey = NULL;
gs_free_error GError *my_error = NULL;
gs_free_error GError *my_error2 = NULL;
gs_unref_ptrarray GPtrArray *argv_array = NULL;
gs_free char *monitor_path = NULL;
gsize metadata_size, runtime_metadata_size;
const char *app;
const char *branch = "master";
const char *command = "/bin/sh";
int i;
int rest_argv_start, rest_argc;
context = g_option_context_new ("APP [args...] - Run an app");
rest_argc = 0;
for (i = 1; i < argc; i++)
{
/* The non-option is the command, take it out of the arguments */
if (argv[i][0] != '-')
{
rest_argv_start = i;
rest_argc = argc - i;
argc = i;
break;
}
}
if (!xdg_app_option_context_parse (context, options, &argc, &argv, XDG_APP_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
goto out;
if (rest_argc == 0)
{
usage_error (context, "APP must be specified", error);
goto out;
}
app = argv[rest_argv_start];
if (opt_branch)
branch = opt_branch;
if (!xdg_app_is_valid_name (app))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid application name", app);
goto out;
}
if (!xdg_app_is_valid_branch (branch))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "'%s' is not a valid branch name", branch);
goto out;
}
app_ref = xdg_app_build_app_ref (app, branch, opt_arch);
user_dir = xdg_app_dir_get_user ();
app_deploy = xdg_app_find_deploy_dir_for_ref (app_ref, cancellable, error);
if (app_deploy == NULL)
goto out;
path = g_file_get_path (app_deploy);
g_debug ("Running application in %s", path);
metadata = g_file_get_child (app_deploy, "metadata");
if (!g_file_load_contents (metadata, cancellable, &metadata_contents, &metadata_size, NULL, error))
goto out;
metakey = g_key_file_new ();
if (!g_key_file_load_from_data (metakey, metadata_contents, metadata_size, 0, error))
goto out;
argv_array = g_ptr_array_new_with_free_func (g_free);
g_ptr_array_add (argv_array, g_strdup (HELPER));
g_ptr_array_add (argv_array, g_strdup ("-l"));
if (!add_extension_args (metakey, app_ref, argv_array, cancellable, error))
goto out;
if (opt_runtime)
runtime = opt_runtime;
else
{
runtime = g_key_file_get_string (metakey, "Application", opt_devel ? "sdk" : "runtime", error);
if (*error)
goto out;
}
runtime_ref = g_build_filename ("runtime", runtime, NULL);
runtime_deploy = xdg_app_find_deploy_dir_for_ref (runtime_ref, cancellable, error);
if (runtime_deploy == NULL)
goto out;
g_free (path);
path = g_file_get_path (runtime_deploy);
g_debug ("Using runtime in %s", path);
runtime_metadata = g_file_get_child (runtime_deploy, "metadata");
if (g_file_load_contents (runtime_metadata, cancellable, &runtime_metadata_contents, &runtime_metadata_size, NULL, NULL))
{
runtime_metakey = g_key_file_new ();
if (!g_key_file_load_from_data (runtime_metakey, runtime_metadata_contents, runtime_metadata_size, 0, error))
goto out;
if (!add_extension_args (runtime_metakey, runtime_ref, argv_array, cancellable, error))
goto out;
}
if (!xdg_app_dir_ensure_path (user_dir, cancellable, error))
goto out;
var = xdg_app_dir_get_app_data (user_dir, app);
if (!gs_file_ensure_directory (var, TRUE, cancellable, error))
goto out;
var_tmp = g_file_get_child (var, "tmp");
var_run = g_file_get_child (var, "run");
if (!g_file_make_symbolic_link (var_tmp, "/tmp", cancellable, &my_error) &&
!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
{
g_propagate_error (error, my_error);
my_error = NULL;
goto out;
}
if (!g_file_make_symbolic_link (var_run, "/run", cancellable, &my_error2) &&
!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
{
g_propagate_error (error, my_error2);
my_error2 = NULL;
goto out;
}
app_files = g_file_get_child (app_deploy, "files");
runtime_files = g_file_get_child (runtime_deploy, "files");
default_command = g_key_file_get_string (metakey, "Application", "command", error);
if (*error)
goto out;
if (opt_command)
command = opt_command;
else
command = default_command;
session_helper = xdg_app_session_helper_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
"org.freedesktop.XdgApp.SessionHelper",
"/org/freedesktop/XdgApp/SessionHelper",
NULL, NULL);
if (session_helper)
{
if (xdg_app_session_helper_call_request_monitor_sync (session_helper,
&monitor_path,
NULL, NULL))
{
g_ptr_array_add (argv_array, g_strdup ("-m"));
g_ptr_array_add (argv_array, monitor_path);
}
}
if (!xdg_app_run_verify_environment_keys ((const char **)opt_forbid, error))
goto out;
if (!xdg_app_run_verify_environment_keys ((const char **)opt_allow, error))
goto out;
xdg_app_run_add_environment_args (argv_array, metakey,
(const char **)opt_allow,
(const char **)opt_forbid);
g_ptr_array_add (argv_array, g_strdup ("-a"));
g_ptr_array_add (argv_array, g_file_get_path (app_files));
g_ptr_array_add (argv_array, g_strdup ("-v"));
g_ptr_array_add (argv_array, g_file_get_path (var));
g_ptr_array_add (argv_array, g_file_get_path (runtime_files));
g_ptr_array_add (argv_array, g_strdup (command));
for (i = 1; i < rest_argc; i++)
g_ptr_array_add (argv_array, g_strdup (argv[rest_argv_start + i]));
g_ptr_array_add (argv_array, NULL);
g_setenv ("XDG_DATA_DIRS", "/self/share:/usr/share", TRUE);
g_unsetenv ("LD_LIBRARY_PATH");
g_setenv ("PATH", "/self/bin:/usr/bin", TRUE);
if (execv (HELPER, (char **)argv_array->pdata) == -1)
{
g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "Unable to start app");
goto out;
}
/* Not actually reached... */
ret = TRUE;
out:
if (context)
g_option_context_free (context);
return ret;
}