~ted/ubuntu-app-launch/ld-library-path

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
/*
 * 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 <unistd.h>
#include <errno.h>
#include <string.h>

#include <glib.h>
#include <glib/gstdio.h>

#include "exec-line-exec-trace.h"
#include "helpers.h"

int
main (int argc, char * argv[])
{
	/* Make sure we have work to do */
	/* This string is quoted using desktop file quoting:
	   http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables */
	const gchar * app_exec = g_getenv("APP_EXEC");
	if (app_exec == NULL) {
		/* There should be no reason for this, a g_error() so that it gets
		   picked up by Apport and we can track it */
		g_error("No exec line given, nothing to do except fail");
		return 1;
	}

	g_setenv("LTTNG_UST_REGISTER_TIMEOUT", "0", FALSE); /* Set to zero if not set */
	tracepoint(upstart_app_launch, exec_start);

	/* URIs */
	const gchar * app_uris = g_getenv("APP_URIS");
	const gchar * app_desktop = g_getenv("APP_DESKTOP_FILE");

	/* Look to see if we have a directory defined that we
	   should be using for everything.  If so, change to it
	   and add it to the path */
	const gchar * appdir = g_getenv("APP_DIR");

	if (appdir != NULL) {
		if (g_chdir(appdir) != 0) {
			g_warning("Unable to change directory to '%s'", appdir);
		}
	}

	/* Protect against app directories that have ':' in them */
	if (appdir != NULL && strchr(appdir, ':') == NULL) {
		const gchar * path_path = g_getenv("PATH");
		if (path_path != NULL && path_path[0] == '\0')
			path_path = NULL;
		gchar * path_libpath = NULL;
		const gchar * path_joinable[4] = { 0 };

		const gchar * lib_path = g_getenv("LD_LIBRARY_PATH");
		if (lib_path != NULL && lib_path[0] == '\0')
			lib_path = NULL;
		gchar * lib_libpath = g_build_filename(appdir, "lib", NULL);
		const gchar * lib_joinable[4] = { 0 };

		const gchar * import_path = g_getenv("QML2_IMPORT_PATH");
		if (import_path != NULL && import_path[0] == '\0')
			import_path = NULL;
		gchar * import_libpath = NULL;
		const gchar * import_joinable[4] = { 0 };

		/* If we've got an architecture set insert that into the
		   path before everything else */
		const gchar * archdir = g_getenv("UPSTART_APP_LAUNCH_ARCH");
		if (archdir != NULL && strchr(archdir, ':') == NULL) {
			path_libpath = g_build_filename(appdir, "lib", archdir, "bin", NULL);
			import_libpath = g_build_filename(appdir, "lib", archdir, NULL);

			path_joinable[0] = path_libpath;
			path_joinable[1] = appdir;
			path_joinable[2] = path_path;

			lib_joinable[0] = import_libpath;
			lib_joinable[1] = lib_libpath;
			lib_joinable[2] = lib_path;

			/* Need to check whether the original is NULL because we're
			   appending instead of prepending */
			if (import_path == NULL) {
				import_joinable[0] = import_libpath;
			} else {
				import_joinable[0] = import_path;
				import_joinable[1] = import_libpath;
			}
		} else {
			path_joinable[0] = appdir;
			path_joinable[1] = path_path;

			lib_joinable[0] = lib_libpath;
			lib_joinable[1] = lib_path;

			import_joinable[0] = import_path;
		}

		gchar * newpath = g_strjoinv(":", (gchar**)path_joinable);
		g_setenv("PATH", newpath, TRUE);
		g_free(path_libpath);
		g_free(newpath);

		gchar * newlib = g_strjoinv(":", (gchar**)lib_joinable);
		g_setenv("LD_LIBRARY_PATH", newlib, TRUE);
		g_free(lib_libpath);
		g_free(newlib);

		if (import_joinable[0] != NULL) {
			gchar * newimport = g_strjoinv(":", (gchar**)import_joinable);
			g_setenv("QML2_IMPORT_PATH", newimport, TRUE);
			g_free(newimport);
		}
		g_free(import_libpath);
	}

	/* Parse the execiness of it all */
	GArray * newargv = desktop_exec_parse(app_exec, app_uris);
	if (newargv == NULL) {
		g_warning("Unable to parse exec line '%s'", app_exec);
		return 1;
	}

	tracepoint(upstart_app_launch, exec_parse_complete);

	/* Surface flinger check */
	if (g_getenv("USING_SURFACE_FLINGER") != NULL && app_desktop != NULL) {
		gchar * sf = g_strdup_printf("--desktop_file_hint=%s", app_desktop);
		g_array_append_val(newargv, sf);
	}

	/* Now exec */
	gchar ** nargv = (gchar**)g_array_free(newargv, FALSE);

	tracepoint(upstart_app_launch, exec_pre_exec);

	int execret = execvp(nargv[0], nargv);

	if (execret != 0) {
		gchar * execprint = g_strjoinv(" ", nargv);
		g_warning("Unable to exec '%s' in '%s': %s", execprint, appdir, strerror(errno));
		g_free(execprint);
	}

	return execret;
}