2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacd.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
43 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
51 #include <guacamole/client.h>
52 #include <guacamole/error.h>
56 typedef struct client_thread_data {
63 void __guacd_log_info(guac_client* client, const char* format, va_list args) {
64 vsyslog(LOG_INFO, format, args);
67 void __guacd_log_error(guac_client* client, const char* format, va_list args) {
68 vsyslog(LOG_ERR, format, args);
71 void* start_client_thread(void* data) {
74 guac_client_plugin* plugin;
75 guac_instruction* select;
76 guac_instruction* connect;
79 client_thread_data* thread_data = (client_thread_data*) data;
81 /* Open guac_socket */
82 guac_socket* socket = guac_socket_open(thread_data->fd);
84 /* Get protocol from select instruction */
85 select = guac_protocol_expect_instruction(
86 socket, GUAC_USEC_TIMEOUT, "select");
90 syslog(LOG_ERR, "Error reading \"select\": %s",
91 guac_status_string(guac_error));
94 guac_socket_close(socket);
99 /* Validate args to select */
100 if (select->argc != 1) {
103 syslog(LOG_ERR, "Bad number of arguments to \"select\" (%i)",
107 guac_socket_close(socket);
112 syslog(LOG_INFO, "Protocol \"%s\" selected", select->argv[0]);
114 /* Get plugin from protocol in select */
115 plugin = guac_client_plugin_open(select->argv[0]);
116 guac_instruction_free(select);
118 if (plugin == NULL) {
121 syslog(LOG_ERR, "Error loading client plugin: %s",
122 guac_status_string(guac_error));
125 guac_socket_close(socket);
130 /* Send args response */
131 if (guac_protocol_send_args(socket, plugin->args)
132 || guac_socket_flush(socket)) {
135 syslog(LOG_ERR, "Error sending \"args\": %s",
136 guac_status_string(guac_error));
138 if (guac_client_plugin_close(plugin))
139 syslog(LOG_ERR, "Error closing client plugin");
141 guac_socket_close(socket);
146 /* Get args from connect instruction */
147 connect = guac_protocol_expect_instruction(
148 socket, GUAC_USEC_TIMEOUT, "connect");
149 if (connect == NULL) {
152 syslog(LOG_ERR, "Error reading \"connect\": %s",
153 guac_status_string(guac_error));
155 if (guac_client_plugin_close(plugin))
156 syslog(LOG_ERR, "Error closing client plugin");
158 guac_socket_close(socket);
163 /* Load and init client */
164 client = guac_client_plugin_get_client(plugin, socket,
165 connect->argc, connect->argv);
166 guac_instruction_free(connect);
168 if (client == NULL) {
170 syslog(LOG_ERR, "Error instantiating client: %s",
171 guac_status_string(guac_error));
173 if (guac_client_plugin_close(plugin))
174 syslog(LOG_ERR, "Error closing client plugin");
176 guac_socket_close(socket);
181 /* Set up logging in client */
182 client->log_info_handler = __guacd_log_info;
183 client->log_error_handler = __guacd_log_error;
185 /* Start client threads */
186 syslog(LOG_INFO, "Starting client");
187 if (guac_start_client(client))
188 syslog(LOG_ERR, "Client finished abnormally");
190 syslog(LOG_INFO, "Client finished normally");
193 guac_client_free(client);
194 if (guac_client_plugin_close(plugin))
195 syslog(LOG_ERR, "Error closing client plugin");
198 guac_socket_close(socket);
199 close(thread_data->fd);
206 int main(int argc, char* argv[]) {
210 struct sockaddr_in server_addr;
214 struct sockaddr_in client_addr;
215 socklen_t client_addr_len;
216 int connected_socket_fd;
219 int listen_port = 4822; /* Default port */
222 char* pidfile = NULL;
227 /* Parse arguments */
228 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
230 listen_port = atoi(optarg);
231 if (listen_port <= 0) {
232 fprintf(stderr, "Invalid port: %s\n", optarg);
236 else if (opt == 'p') {
237 pidfile = strdup(optarg);
240 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
245 /* Get binding address */
246 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
247 server_addr.sin_family = AF_INET;
248 server_addr.sin_addr.s_addr = INADDR_ANY;
249 server_addr.sin_port = htons(listen_port);
252 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
254 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
258 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
259 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno));
262 /* Bind socket to address */
263 if (bind(socket_fd, (struct sockaddr*) &server_addr,
264 sizeof(server_addr)) < 0) {
265 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
269 /* Fork into background */
274 if (daemon_pid == -1) {
275 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
279 /* If parent, write PID file and exit */
280 else if (daemon_pid != 0) {
282 if (pidfile != NULL) {
284 /* Attempt to open pidfile and write PID */
285 FILE* pidf = fopen(pidfile, "w");
287 fprintf(pidf, "%d\n", daemon_pid);
291 /* Warn on failure */
293 fprintf(stderr, "WARNING: Could not write PID file: %s\n", strerror(errno));
302 daemon_pid = getpid();
303 syslog(LOG_INFO, "fork() not defined at compile time.");
304 syslog(LOG_INFO, "guacd running in foreground only.");
307 /* Otherwise, this is the daemon */
308 syslog(LOG_INFO, "Listening on port %i", listen_port);
311 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
312 syslog(LOG_ERR, "Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
315 /* Ignore SIGCHLD (force automatic removal of children) */
316 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
317 syslog(LOG_ERR, "Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
326 guac_thread_t thread;
328 client_thread_data* data;
330 /* Listen for connections */
331 if (listen(socket_fd, 5) < 0) {
332 syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
336 /* Accept connection */
337 client_addr_len = sizeof(client_addr);
338 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
339 if (connected_socket_fd < 0) {
340 syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
344 data = malloc(sizeof(client_thread_data));
345 data->fd = connected_socket_fd;
348 * Once connection is accepted, send child into background, whether through
349 * fork() or through creating a thread. If thead support is not present on
350 * the platform, guacd will still work, but will only be able to handle one
351 * connection at a time.
359 * Note that we prefer fork() over threads for connection-handling
360 * processes as they give each connection its own memory area, and
361 * isolate the main daemon and other connections from errors in any
362 * particular client plugin.
369 syslog(LOG_ERR, "Error forking child process: %s\n", strerror(errno));
371 /* If child, start client, and exit when finished */
372 else if (child_pid == 0) {
373 start_client_thread(data);
377 /* If parent, close reference to child's descriptor */
378 else if (close(connected_socket_fd) < 0) {
379 syslog(LOG_ERR, "Error closing daemon reference to child descriptor: %s", strerror(errno));
384 if (guac_thread_create(&thread, start_client_thread, (void*) data))
385 syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
392 if (close(socket_fd) < 0) {
393 syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));