~ted/ubuntu-app-launch/snap-icon-unbreak

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
/*
 * Copyright © 2014 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>
 */

#define _POSIX_C_SOURCE 200212L

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>

void
sigchild_handler (int signal)
{
	fprintf(stderr, "XMir has closed unexpectedly\n");
	exit(EXIT_FAILURE);
}

struct sigaction sigchild_action = {
	.sa_handler = sigchild_handler,
	.sa_flags = SA_NOCLDWAIT
};

int
main (int argc, char * argv[])
{
	if (argc < 3) {
		fprintf(stderr, "xmir-helper needs more arguments: xmir-helper $(appid) $(thing to exec) ... \n");
		return EXIT_FAILURE;
	}

	/* Make nice variables for the things we need */
	char * appid = argv[1];
	char * xmir = getenv("UBUNTU_APP_LAUNCH_XMIR_PATH");
	if (xmir == NULL) {
		xmir = "/usr/bin/Xmir";
	}

	/* Build a socket pair to get the connection back from XMir */
	int sockets[2];
	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets) != 0) {
		fprintf(stderr, "Unable to create socketpair for communicating with XMir\n");
		return EXIT_FAILURE;
	}

	/* Give them nice names, the compiler will optimize out */
	int xmirsocket = sockets[0];
	int helpersocket = sockets[1];

	/* Watch for the child dying */
	if (sigaction(SIGCHLD, &sigchild_action, NULL) != 0) {
		fprintf(stderr, "Unable to setup child signal handler\n");
		return EXIT_FAILURE;
	}

	/* Start XMir */
	if (fork() == 0) {
		/* XMir start here */
		/* GOAL: XMir -displayfd ${xmirsocket} -mir ${appid} */
		char socketbuf[16] = {0};
		snprintf(socketbuf, 16, "%d", xmirsocket);

		char * xmirexec[6] = {
			xmir,
			"-displayfd",
			socketbuf,
			"-mir",
			appid,
			NULL
		};

		printf("Executing XMir on PID: %d", getpid());

		return execv(xmir, xmirexec);
	}

	/* Wait to get the display number from XMir */
	char readbuf[16] = {0};
	if (read(helpersocket, readbuf, 16) == 0) {
		fprintf(stderr, "Not reading anything from XMir\n");
		return 1;
	}

	int i;
	for (i = 0; i < sizeof(readbuf); i++) {
		if (readbuf[i] == '\n') {
			readbuf[i] = '\0';
			break;
		}
	}

	char displaynumber[16] = {0};
	snprintf(displaynumber, 16, ":%s", readbuf);

	/* Set up the display variable */
	setenv("DISPLAY", displaynumber, 1);

	/* Now that we have everything setup, we can execute */
	char ** nargv = &argv[2];
	int execret = execvp(nargv[0], nargv);
	return execret;
}