~ted/ubuntu-app-launch/fix-profiling-flags

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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

#include <json-glib/json-glib.h>
#include <click.h>
#include <upstart.h>
#include "helpers.h"

/* Take an app ID and validate it and then break it up
   and spit it out.  These are newly allocated strings */
gboolean
app_id_to_triplet (const gchar * app_id, gchar ** package, gchar ** application, gchar ** version)
{
	/* 'Parse' the App ID */
	gchar ** app_id_segments = g_strsplit(app_id, "_", 4);
	if (g_strv_length(app_id_segments) != 3) {
		g_debug("Unable to parse Application ID: %s", app_id);
		g_strfreev(app_id_segments);
		return FALSE;
	}

	if (package != NULL) {
		*package = app_id_segments[0];
	} else {
		g_free(app_id_segments[0]);
	}

	if (application != NULL) {
		*application = app_id_segments[1];
	} else {
		g_free(app_id_segments[1]);
	}

	if (version != NULL) {
		*version = app_id_segments[2];
	} else {
		g_free(app_id_segments[2]);
	}

	g_free(app_id_segments);
	return TRUE;
}

/* Take a manifest, parse it, find the application and
   and then return the path to the desktop file */
gchar *
manifest_to_desktop (const gchar * app_dir, const gchar * app_id)
{
	gchar * package = NULL;
	gchar * application = NULL;
	gchar * version = NULL;
	ClickDB * db = NULL;
	ClickUser * user = NULL;
	JsonObject * manifest = NULL;
	gchar * desktoppath = NULL;
	GError * error = NULL;

	if (!app_id_to_triplet(app_id, &package, &application, &version)) {
		g_warning("Unable to parse triplet: %s", app_id);
		return NULL;
	}

	db = click_db_new();
	/* If TEST_CLICK_DB is unset, this reads the system database. */
	click_db_read(db, g_getenv("TEST_CLICK_DB"), &error);
	if (error != NULL) {
		g_warning("Unable to read Click database: %s", error->message);
		goto manifest_out;
	}
	/* If TEST_CLICK_USER is unset, this uses the current user name. */
	user = click_user_new_for_user(db, g_getenv("TEST_CLICK_USER"), &error);
	if (error != NULL) {
		g_warning("Unable to read Click database: %s", error->message);
		goto manifest_out;
	}
	manifest = click_user_get_manifest(user, package, &error);
	if (error != NULL) {
		g_warning("Unable to get manifest for '%s': %s", package, error->message);
		goto manifest_out;
	}

	if (!json_object_has_member(manifest, "version")) {
		g_warning("Manifest '%s' doesn't have a version", package);
		goto manifest_out;
	}

	if (g_strcmp0(json_object_get_string_member(manifest, "version"), version) != 0) {
		g_warning("Manifest '%s' version '%s' doesn't match AppID version '%s'", package, json_object_get_string_member(manifest, "version"), version);
		goto manifest_out;
	}

	if (!json_object_has_member(manifest, "hooks")) {
		g_warning("Manifest '%s' doesn't have an hooks section", package);
		goto manifest_out;
	}

	JsonObject * appsobj = json_object_get_object_member(manifest, "hooks");
	if (appsobj == NULL) {
		g_warning("Manifest '%s' has an hooks section that is not a JSON object", package);
		goto manifest_out;
	}

	if (!json_object_has_member(appsobj, application)) {
		g_warning("Manifest '%s' doesn't have the application '%s' defined", package, application);
		goto manifest_out;
	}

	JsonObject * appobj = json_object_get_object_member(appsobj, application);
	if (appobj == NULL) {
		g_warning("Manifest '%s' has a definition for application '%s' that is not an object", package, application);
		goto manifest_out;
	}

	gchar * filename = NULL;
	if (json_object_has_member(appobj, "desktop")) {
		filename = g_strdup(json_object_get_string_member(appobj, "desktop"));
	} else {
		filename = g_strdup_printf("%s.desktop", application);
	}

	desktoppath = g_build_filename(app_dir, filename, NULL);
	g_free(filename);

	if (!g_file_test(desktoppath, G_FILE_TEST_EXISTS)) {
		g_warning("Application desktop file '%s' doesn't exist", desktoppath);
		g_free(desktoppath);
		desktoppath = NULL;
	}

manifest_out:
	if (error != NULL)
		g_error_free(error);
	if (manifest != NULL)
		json_object_unref(manifest);
	g_clear_object(&user);
	g_clear_object(&db);
	g_free(package);
	g_free(application);
	g_free(version);

	return desktoppath;
}

/* Take a desktop file, make sure that it makes sense and
   then return the exec line */
gchar *
desktop_to_exec (GKeyFile * desktop_file, const gchar * from)
{
	GError * error = NULL;

	if (!g_key_file_has_group(desktop_file, "Desktop Entry")) {
		g_warning("Desktop file '%s' does not have a 'Desktop Entry' group", from);
		return NULL;
	}

	gchar * type = g_key_file_get_string(desktop_file, "Desktop Entry", "Type", &error);
	if (error != NULL) {
		g_warning("Desktop file '%s' unable to get type: %s", from, error->message);
		g_error_free(error);
		g_free(type);
		return NULL;
	}

	if (g_strcmp0(type, "Application") != 0) {
		g_warning("Desktop file '%s' has a type of '%s' instead of 'Application'", from, type);
		g_free(type);
		return NULL;
	}
	g_free(type);

	if (g_key_file_has_key(desktop_file, "Desktop Entry", "NoDisplay", NULL)) {
		gboolean nodisplay = g_key_file_get_boolean(desktop_file, "Desktop Entry", "NoDisplay", NULL);
		if (nodisplay) {
			g_warning("Desktop file '%s' is set to not display, not copying", from);
			return NULL;
		}
	}

	if (g_key_file_has_key(desktop_file, "Desktop Entry", "Hidden", NULL)) {
		gboolean hidden = g_key_file_get_boolean(desktop_file, "Desktop Entry", "Hidden", NULL);
		if (hidden) {
			g_warning("Desktop file '%s' is set to be hidden, not copying", from);
			return NULL;
		}
	}

	if (g_key_file_has_key(desktop_file, "Desktop Entry", "Terminal", NULL)) {
		gboolean terminal = g_key_file_get_boolean(desktop_file, "Desktop Entry", "Terminal", NULL);
		if (terminal) {
			g_warning("Desktop file '%s' is set to run in a terminal, not copying", from);
			return NULL;
		}
	}

	if (!g_key_file_has_key(desktop_file, "Desktop Entry", "Exec", NULL)) {
		g_warning("Desktop file '%s' has no 'Exec' key", from);
		return NULL;
	}

	gchar * exec = g_key_file_get_string(desktop_file, "Desktop Entry", "Exec", NULL);
	return exec;
}

/* Convert a URI into a file */
static gchar *
uri2file (const gchar * uri)
{
	GError * error = NULL;
	gchar * retval = g_filename_from_uri(uri, NULL, &error);

	if (error != NULL) {
		g_warning("Unable to resolve '%s' to a filename: %s", uri, error->message);
		g_error_free(error);
	}

	if (retval == NULL) {
		retval = g_strdup("");
	}

	g_debug("Converting URI '%s' to file '%s'", uri, retval);
	return retval;
}

/* Put the list of files into the argument array */
static inline void
file_list_handling (GArray * outarray, gchar ** list, gchar * (*dup_func) (const gchar * in))
{
	/* No URLs, cool, this is a noop */
	if (list == NULL || list[0] == NULL) {
		return;
	}

	int i;
	for (i = 0; list[i] != NULL; i++) {
		gchar * entry = dup_func(list[i]);

		/* No NULLs */
		if (entry != NULL && entry[0] != '\0') {
			g_array_append_val(outarray, entry);
		} else {
			g_free(entry);
		}
	}
}

/* Parse a desktop exec line and return the next string */
static void
desktop_exec_segment_parse (GArray * finalarray, const gchar * execsegment, gchar ** uri_list)
{
	/* No NULL strings */
	if (execsegment == NULL || execsegment[0] == '\0')
		return;

	/* Handle %F and %U as an argument on their own as per the spec */
	if (g_strcmp0(execsegment, "%U") == 0) {
		return file_list_handling(finalarray, uri_list, g_strdup);
	}
	if (g_strcmp0(execsegment, "%F") == 0) {
		return file_list_handling(finalarray, uri_list, uri2file);
	}

	/* Start looking at individual codes */
	gchar ** execsplit = g_strsplit(execsegment, "%", 0);

	/* If we didn't have any codes, just exit here */
	if (execsplit[1] == NULL) {
		g_strfreev(execsplit);
		gchar * dup = g_strdup(execsegment);
		g_array_append_val(finalarray, dup);
		return;
	}

	int i;

	gboolean previous_percent = FALSE;
	GArray * outarray = g_array_new(TRUE, FALSE, sizeof(const gchar *));
	g_array_append_val(outarray, execsplit[0]);
	gchar * single_file = NULL;

	/* The variables allowed in an exec line from the Freedesktop.org Desktop
	   File specification: http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables */
	for (i = 1; execsplit[i] != NULL; i++) {
		const gchar * skipchar = &(execsplit[i][1]);

		/* Handle the case of %%F printing "%F" */
		if (previous_percent) {
			g_array_append_val(outarray, execsplit[i]);
			previous_percent = FALSE;
			continue;
		}

		switch (execsplit[i][0]) {
		case '\0': {
			const gchar * percent = "%";
			g_array_append_val(outarray, percent); /* %% is the literal */
			previous_percent = TRUE;
			break;
		}
		case 'd':
		case 'D':
		case 'n':
		case 'N':
		case 'v':
		case 'm':
			/* Deprecated */
			g_array_append_val(outarray, skipchar);
			break;
		case 'f':
			if (uri_list != NULL && uri_list[0] != NULL) {
				if (single_file == NULL)
					single_file = uri2file(uri_list[0]);
				g_array_append_val(outarray, single_file);
			}

			g_array_append_val(outarray, skipchar);
			break;
		case 'F':
			g_warning("Exec line segment has a '%%F' that isn't its own argument '%s', ignoring.", execsegment);
			g_array_append_val(outarray, skipchar);
			break;
		case 'i':
		case 'c':
		case 'k':
			/* Perhaps?  Not sure anyone uses these */
			g_array_append_val(outarray, skipchar);
			break;
		case 'U':
			g_warning("Exec line segment has a '%%U' that isn't its own argument '%s', ignoring.", execsegment);
			g_array_append_val(outarray, skipchar);
			break;
		case 'u':
			if (uri_list != NULL && uri_list[0] != NULL) {
				g_array_append_val(outarray, uri_list[0]);
			}

			g_array_append_val(outarray, skipchar);
			break;
		default:
			g_warning("Desktop Exec line code '%%%c' unknown, skipping.", execsplit[i][0]);
			g_array_append_val(outarray, skipchar);
			break;
		}
	}

	gchar * output = g_strjoinv(NULL, (gchar **)outarray->data);
	g_array_free(outarray, TRUE);

	if (output != NULL && output[0] != '\0') {
		g_array_append_val(finalarray, output);
	} else {
		g_free(output);
	}

	g_free(single_file);
	g_strfreev(execsplit);
}

/* Take a full exec line, split it out, parse the segments and return
   it to the caller */
GArray *
desktop_exec_parse (const gchar * execline, const gchar * urilist)
{
	GError * error = NULL;
	gchar ** splitexec = NULL;
	gchar ** splituris = NULL;
	gint execitems = 0;

	/* This returns from desktop file style quoting to straight strings with
	   the appropriate characters split by the spaces that were meant for
	   splitting.  Trickier than it sounds.  But now we should be able to assume
	   that each string in the array is expected to be its own parameter. */
	g_shell_parse_argv(execline, &execitems, &splitexec, &error);

	if (error != NULL) {
		g_warning("Unable to parse exec line '%s': %s", execline, error->message);
		g_error_free(error);
		return NULL;
	}

	if (urilist != NULL && urilist[0] != '\0') {
		g_shell_parse_argv(urilist, NULL, &splituris, &error);

		if (error != NULL) {
			g_warning("Unable to parse URIs '%s': %s", urilist, error->message);
			g_error_free(error);
			/* Continuing without URIs */
			splituris = NULL;
		}
	}


	GArray * newargv = g_array_new(TRUE, FALSE, sizeof(gchar *));
	int i;
	for (i = 0; i < execitems; i++) {
		desktop_exec_segment_parse(newargv, splitexec[i], splituris);
	}
	g_strfreev(splitexec);

	if (splituris != NULL) {
		g_strfreev(splituris);
	}

	/* Each string here should be its own param */

	return newargv;
}

/* Set environment various variables to make apps work under
 * confinement according to:
 * https://wiki.ubuntu.com/SecurityTeam/Specifications/ApplicationConfinement
 */
void
set_confined_envvars (EnvHandle * handle, const gchar * package, const gchar * app_dir)
{
	g_return_if_fail(package != NULL);
	g_return_if_fail(app_dir != NULL);

	g_debug("Setting 'UBUNTU_APPLICATION_ISOLATION' to '1'");
	env_handle_add(handle, "UBUNTU_APPLICATION_ISOLATION", "1");

	/* Make sure the XDG base dirs are set for the application using
	 * the user's current values/system defaults. We could set these to
	 * what is expected in the AppArmor profile, but that might be too
	 * brittle if someone uses different base dirs.
	 */
	g_debug("Setting 'XDG_CACHE_HOME' using g_get_user_cache_dir()");
	env_handle_add(handle, "XDG_CACHE_HOME", g_get_user_cache_dir());

	g_debug("Setting 'XDG_CONFIG_HOME' using g_get_user_config_dir()");
	env_handle_add(handle, "XDG_CONFIG_HOME", g_get_user_config_dir());

	g_debug("Setting 'XDG_DATA_HOME' using g_get_user_data_dir()");
	env_handle_add(handle, "XDG_DATA_HOME", g_get_user_data_dir());

	g_debug("Setting 'XDG_RUNTIME_DIR' using g_get_user_runtime_dir()");
	env_handle_add(handle, "XDG_RUNTIME_DIR", g_get_user_runtime_dir());

	/* Add the application's dir to the list of sources for data */
	const gchar * basedatadirs = g_getenv("XDG_DATA_DIRS");
	if (basedatadirs == NULL || basedatadirs[0] == '\0') {
		basedatadirs = "/usr/local/share:/usr/share";
	}
	gchar * datadirs = g_strjoin(":", app_dir, basedatadirs, NULL);
	env_handle_add(handle, "XDG_DATA_DIRS", datadirs);
	g_free(datadirs);

	/* Set TMPDIR to something sane and application-specific */
	gchar * tmpdir = g_strdup_printf("%s/confined/%s", g_get_user_runtime_dir(), package);
	g_debug("Setting 'TMPDIR' to '%s'", tmpdir);
	env_handle_add(handle, "TMPDIR", tmpdir);
	g_debug("Creating '%s'", tmpdir);
	g_mkdir_with_parents(tmpdir, 0700);
	g_free(tmpdir);

	/* Do the same for nvidia */
	gchar * nv_shader_cachedir = g_strdup_printf("%s/%s", g_get_user_cache_dir(), package);
	g_debug("Setting '__GL_SHADER_DISK_CACHE_PATH' to '%s'", nv_shader_cachedir);
	env_handle_add(handle, "__GL_SHADER_DISK_CACHE_PATH", nv_shader_cachedir);
	g_free(nv_shader_cachedir);

	return;
}

static void
unity_signal_cb (GDBusConnection * con, const gchar * sender, const gchar * path, const gchar * interface, const gchar * signal, GVariant * params, gpointer user_data)
{
	GMainLoop * mainloop = (GMainLoop *)user_data;
	g_main_loop_quit(mainloop);
}

struct _handshake_t {
	GDBusConnection * con;
	GMainLoop * mainloop;
	guint signal_subscribe;
	guint timeout;
};

static gboolean
unity_too_slow_cb (gpointer user_data)
{
	handshake_t * handshake = (handshake_t *)user_data;
	g_main_loop_quit(handshake->mainloop);
	handshake->timeout = 0;
	return G_SOURCE_REMOVE;
}

handshake_t *
starting_handshake_start (const gchar *   app_id)
{
	GError * error = NULL;
	handshake_t * handshake = g_new0(handshake_t, 1);

	handshake->mainloop = g_main_loop_new(NULL, FALSE);
	handshake->con = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);

	if (error != NULL) {
		g_critical("Unable to connect to session bus: %s", error->message);
		g_error_free(error);
		g_free(handshake);
		return NULL;
	}

	/* Set up listening for the unfrozen signal from Unity */
	handshake->signal_subscribe = g_dbus_connection_signal_subscribe(handshake->con,
		NULL, /* sender */
		"com.canonical.UbuntuAppLaunch", /* interface */
		"UnityStartingSignal", /* signal */
		"/", /* path */
		app_id, /* arg0 */
		G_DBUS_SIGNAL_FLAGS_NONE,
		unity_signal_cb, handshake->mainloop,
		NULL); /* user data destroy */

	/* Send unfreeze to to Unity */
	g_dbus_connection_emit_signal(handshake->con,
		NULL, /* destination */
		"/", /* path */
		"com.canonical.UbuntuAppLaunch", /* interface */
		"UnityStartingBroadcast", /* signal */
		g_variant_new("(s)", app_id),
		&error);

	/* Really, Unity? */
	handshake->timeout = g_timeout_add_seconds(1, unity_too_slow_cb, handshake);

	return handshake;
}

void
starting_handshake_wait (handshake_t * handshake)
{
	if (handshake == NULL)
		return;

	g_main_loop_run(handshake->mainloop);

	if (handshake->timeout != 0)
		g_source_remove(handshake->timeout);
	g_main_loop_unref(handshake->mainloop);
	g_dbus_connection_signal_unsubscribe(handshake->con, handshake->signal_subscribe);
	g_object_unref(handshake->con);

	g_free(handshake);
}

EnvHandle *
env_handle_start (void)
{
	GVariantBuilder * builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
	return (EnvHandle *)builder;
}

void
env_handle_add (EnvHandle * handle, const gchar * variable, const gchar * value)
{
	g_return_if_fail(handle != NULL);
	gchar * combinedstr = g_strdup_printf("%s=%s", variable, value);
	GVariant * env = g_variant_new_take_string(combinedstr);
	g_variant_builder_add_value((GVariantBuilder*)handle, env);
}

void
env_handle_finish (EnvHandle * handle)
{
	g_return_if_fail(handle != NULL);
	/* Check to see if we can get the job environment */
	const gchar * job_name = g_getenv("UPSTART_JOB");
	const gchar * instance_name = g_getenv("UPSTART_INSTANCE");
	g_return_if_fail(job_name != NULL);

	/* Get a bus, let's go! */
	GDBusConnection * bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
	g_return_if_fail(bus != NULL);

	GVariantBuilder builder; /* Target: (assb) */
	g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);

	/* Setup the job properties */
	g_variant_builder_open(&builder, G_VARIANT_TYPE_ARRAY);
	g_variant_builder_add_value(&builder, g_variant_new_string(job_name));
	if (instance_name != NULL)
		g_variant_builder_add_value(&builder, g_variant_new_string(instance_name));
	g_variant_builder_close(&builder);

	/* The value itself */
	g_variant_builder_add_value(&builder, g_variant_builder_end((GVariantBuilder*)handle));

	/* Do we want to replace?  Yes, we do! */
	g_variant_builder_add_value(&builder, g_variant_new_boolean(TRUE));

	GError * error = NULL;
	GVariant * reply = g_dbus_connection_call_sync(bus,
		DBUS_SERVICE_UPSTART,
		DBUS_PATH_UPSTART,
		DBUS_INTERFACE_UPSTART,
		"SetEnvList",
		g_variant_builder_end(&builder),
		NULL, /* reply */
		G_DBUS_CALL_FLAGS_NONE,
		-1, /* timeout */
		NULL, /* cancelable */
		&error); /* error */

	if (reply != NULL) {
		g_variant_unref(reply);
	}

	if (error != NULL) {
		g_warning("Unable to set environment variables: %s", error->message);
		g_error_free(error);
	}

	g_object_unref(bus);
}