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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <glib-unix.h>
#include "status.h"
static GMainLoop *loop;
static int exit_status = EXIT_SUCCESS;
static int from_dm_fd = -1, to_dm_fd = -1;
static GKeyFile *config;
static void
quit (int status)
{
exit_status = status;
g_main_loop_quit (loop);
}
static gboolean
sigint_cb (gpointer user_data)
{
status_notify ("UNITY-SYSTEM-COMPOSITOR TERMINATE SIGNAL=%d", SIGINT);
quit (EXIT_SUCCESS);
return TRUE;
}
static gboolean
sigterm_cb (gpointer user_data)
{
status_notify ("UNITY-SYSTEM-COMPOSITOR TERMINATE SIGNAL=%d", SIGTERM);
quit (EXIT_SUCCESS);
return TRUE;
}
typedef enum
{
USC_MESSAGE_PING = 0,
USC_MESSAGE_PONG = 1,
USC_MESSAGE_READY = 2,
USC_MESSAGE_SESSION_CONNECTED = 3,
USC_MESSAGE_SET_ACTIVE_SESSION = 4,
USC_MESSAGE_SET_NEXT_SESSION = 5,
} USCMessageID;
static void
write_message (guint16 id, const guint8 *payload, guint16 payload_length)
{
gsize data_length = 4 + payload_length;
guint8 *data = g_malloc (data_length);
data[0] = id >> 8;
data[1] = id & 0xFF;
data[2] = payload_length >> 8;
data[3] = payload_length & 0xFF;
if (payload)
memcpy (data + 4, payload, payload_length);
if (write (to_dm_fd, data, data_length) < 0)
fprintf (stderr, "Failed to write to daemon: %s\n", strerror (errno));
}
static gboolean
read_message_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
{
gchar header[4];
gsize n_read;
g_autoptr(GError) error = NULL;
if (g_io_channel_read_chars (channel, header, 4, &n_read, &error) != G_IO_STATUS_NORMAL)
{
g_printerr ("Failed to read header: %s\n", error->message);
return FALSE;
}
if (n_read != 4)
{
g_printerr ("Short read for header, %zi instead of expected 4\n", n_read);
return FALSE;
}
guint16 id = header[0] << 8 | header[1];
guint16 payload_length = header[2] << 8 | header[3];
gchar *payload = g_malloc0 (payload_length + 1);
if (g_io_channel_read_chars (channel, payload, payload_length, &n_read, &error) != G_IO_STATUS_NORMAL)
{
g_printerr ("Failed to read payload: %s\n", error->message);
return FALSE;
}
if (n_read != payload_length)
{
g_printerr ("Short read for payload, %zi instead of expected %d\n", n_read, payload_length);
return FALSE;
}
switch (id)
{
case USC_MESSAGE_PING:
status_notify ("UNITY-SYSTEM-COMPOSITOR PING");
break;
case USC_MESSAGE_SET_ACTIVE_SESSION:
status_notify ("UNITY-SYSTEM-COMPOSITOR SET-ACTIVE-SESSION ID=%s", (gchar *)payload);
break;
case USC_MESSAGE_SET_NEXT_SESSION:
status_notify ("UNITY-SYSTEM-COMPOSITOR SET-NEXT-SESSION ID=%s", (gchar *)payload);
break;
default:
g_printerr ("Ignoring message %d with %d octets\n", id, payload_length);
break;
}
free (payload);
return TRUE;
}
static void
request_cb (const gchar *name, GHashTable *params)
{
if (!name)
{
g_main_loop_quit (loop);
return;
}
if (strcmp (name, "PING") == 0)
write_message (USC_MESSAGE_PING, NULL, 0);
else if (strcmp (name, "PONG") == 0)
write_message (USC_MESSAGE_PONG, NULL, 0);
else if (strcmp (name, "READY") == 0)
write_message (USC_MESSAGE_READY, NULL, 0);
else if (strcmp (name, "CRASH") == 0)
kill (getpid (), SIGSEGV);
}
int
main (int argc, char **argv)
{
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
#endif
loop = g_main_loop_new (NULL, FALSE);
g_unix_signal_add (SIGINT, sigint_cb, NULL);
g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
status_connect (request_cb, "UNITY-SYSTEM-COMPOSITOR");
gboolean test = FALSE, container = FALSE;
int vt_number = -1;
const gchar *file = NULL;
for (int i = 1; i < argc; i++)
{
char *arg = argv[i];
if (strcmp (arg, "--from-dm-fd") == 0)
{
from_dm_fd = atoi (argv[i+1]);
i++;
}
else if (strcmp (arg, "--to-dm-fd") == 0)
{
to_dm_fd = atoi (argv[i+1]);
i++;
}
else if (strcmp (arg, "--vt") == 0)
{
vt_number = atoi (argv[i+1]);
i++;
}
else if (strcmp (arg, "--file") == 0)
{
file = argv[i+1];
i++;
}
else if (strcmp (arg, "--test") == 0)
test = TRUE;
else if (strcmp (arg, "--container") == 0)
container = TRUE;
else
return EXIT_FAILURE;
}
g_io_add_watch (g_io_channel_unix_new (from_dm_fd), G_IO_IN, read_message_cb, NULL);
g_autoptr(GString) status_text = g_string_new ("UNITY-SYSTEM-COMPOSITOR START");
if (file)
g_string_append_printf (status_text, " FILE=%s", file);
if (vt_number >= 0)
g_string_append_printf (status_text, " VT=%d", vt_number);
if (g_getenv ("XDG_VTNR"))
g_string_append_printf (status_text, " XDG_VTNR=%s", g_getenv ("XDG_VTNR"));
if (test)
g_string_append (status_text, " TEST=TRUE");
if (container)
g_string_append (status_text, " CONTAINER=TRUE");
status_notify ("%s", status_text->str);
config = g_key_file_new ();
g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
if (g_key_file_has_key (config, "unity-system-compositor-config", "return-value", NULL))
{
int return_value = g_key_file_get_integer (config, "unity-system-compositor-config", "return-value", NULL);
status_notify ("UNITY-SYSTEM-COMPOSITOR EXIT CODE=%d", return_value);
return return_value;
}
g_main_loop_run (loop);
return exit_status;
}
|