13 #include "x-authority.h"
15 static GMainLoop *loop;
17 static GKeyFile *config;
19 /* Path to lock file */
20 static gchar *lock_path = NULL;
22 /* Path to authority database to use */
23 static gchar *auth_path = NULL;
25 /* ID to use for test reporting */
28 /* Display number being served */
29 static int display_number = 0;
32 static XServer *xserver = NULL;
40 g_object_unref (xserver);
51 sighup_cb (gpointer user_data)
53 status_notify ("%s DISCONNECT-CLIENTS", id);
58 sigint_cb (gpointer user_data)
60 status_notify ("%s TERMINATE SIGNAL=%d", id, SIGINT);
66 sigterm_cb (gpointer user_data)
68 status_notify ("%s TERMINATE SIGNAL=%d", id, SIGTERM);
74 client_connected_cb (XServer *server, XClient *client)
76 gchar *auth_error = NULL;
78 status_notify ("%s ACCEPT-CONNECT", id);
81 x_client_send_failed (client, auth_error);
83 x_client_send_success (client);
88 client_disconnected_cb (XServer *server, XClient *client)
90 g_signal_handlers_disconnect_matched (client, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, NULL);
94 vnc_data_cb (GIOChannel *channel, GIOCondition condition, gpointer data)
101 status = g_io_channel_read_chars (channel, buffer, 1023, &n_read, &error);
103 g_warning ("Error reading from VNC client: %s", error->message);
104 g_clear_error (&error);
106 if (status == G_IO_STATUS_NORMAL)
108 buffer[n_read] = '\0';
109 if (g_str_has_suffix (buffer, "\n"))
110 buffer[n_read-1] = '\0';
111 status_notify ("%s VNC-CLIENT-CONNECT VERSION=\"%s\"", id, buffer);
118 request_cb (const gchar *name, GHashTable *params)
122 g_main_loop_quit (loop);
126 if (strcmp (name, "INDICATE-READY") == 0)
130 handler = signal (SIGUSR1, SIG_IGN);
131 if (handler == SIG_IGN)
133 status_notify ("%s INDICATE-READY", id);
134 kill (getppid (), SIGUSR1);
136 signal (SIGUSR1, handler);
139 else if (strcmp (name, "START-VNC") == 0)
141 /* Send server protocol version to client */
142 g_print ("RFB 003.007\n");
147 main (int argc, char **argv)
150 gboolean use_inetd = FALSE;
151 gboolean has_option = FALSE;
152 gchar *geometry = g_strdup ("640x480");
154 gchar *lock_filename;
158 #if !defined(GLIB_VERSION_2_36)
162 loop = g_main_loop_new (NULL, FALSE);
164 g_unix_signal_add (SIGINT, sigint_cb, NULL);
165 g_unix_signal_add (SIGTERM, sigterm_cb, NULL);
166 g_unix_signal_add (SIGHUP, sighup_cb, NULL);
168 for (i = 1; i < argc; i++)
174 display_number = atoi (arg + 1);
176 else if (strcmp (arg, "-auth") == 0)
178 auth_path = argv[i+1];
181 else if (strcmp (arg, "-nolisten") == 0)
183 char *protocol = argv[i+1];
185 if (strcmp (protocol, "tcp") == 0)
186 ;//listen_tcp = FALSE;
187 else if (strcmp (protocol, "unix") == 0)
188 ;//listen_unix = FALSE;
190 else if (strcmp (arg, "-geometry") == 0)
193 geometry = g_strdup (argv[i+1]);
196 else if (strcmp (arg, "-depth") == 0)
198 depth = atoi (argv[i+1]);
201 else if (strcmp (arg, "-inetd") == 0)
205 else if (strcmp (arg, "-option") == 0)
211 g_printerr ("Unrecognized option: %s\n"
212 "Use: %s [:<display>] [option]\n"
213 "-auth file Select authorization file\n"
214 "-nolisten protocol Don't listen on protocol\n"
215 "-geometry WxH Set framebuffer width & height\n"
216 "-depth D Set framebuffer depth\n"
217 "-inetd Xvnc is launched by inetd\n",
223 id = g_strdup_printf ("XSERVER-%d", display_number);
225 status_connect (request_cb, id);
227 xserver = x_server_new (display_number);
228 g_signal_connect (xserver, "client-connected", G_CALLBACK (client_connected_cb), NULL);
229 g_signal_connect (xserver, "client-disconnected", G_CALLBACK (client_disconnected_cb), NULL);
231 status_notify ("%s START GEOMETRY=%s DEPTH=%d OPTION=%s", id, geometry, depth, has_option ? "TRUE" : "FALSE");
233 config = g_key_file_new ();
234 g_key_file_load_from_file (config, g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), G_KEY_FILE_NONE, NULL);
238 if (!g_io_add_watch (g_io_channel_unix_new (STDIN_FILENO), G_IO_IN, vnc_data_cb, NULL))
243 g_printerr ("Only supported in -inetd mode\n");
247 lock_filename = g_strdup_printf (".X%d-lock", display_number);
248 lock_path = g_build_filename (g_getenv ("LIGHTDM_TEST_ROOT"), "tmp", lock_filename, NULL);
249 g_free (lock_filename);
250 lock_file = open (lock_path, O_CREAT | O_EXCL | O_WRONLY, 0444);
254 "Fatal server error:\n"
255 "Server is already active for display %d\n"
256 " If this server is no longer running, remove %s\n"
257 " and start again.\n", display_number, lock_path);
262 pid_string = g_strdup_printf ("%10ld", (long) getpid ());
263 if (write (lock_file, pid_string, strlen (pid_string)) < 0)
265 g_warning ("Error writing PID file: %s", strerror (errno));
270 if (!x_server_start (xserver))
273 g_main_loop_run (loop);