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.
23 * David PHAM-VAN <d.pham-van@ulteo.com> Ulteo SAS - http://www.ulteo.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
44 #include <sys/types.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
54 #include <guacamole/client.h>
55 #include <guacamole/error.h>
60 void guacd_handle_connection(int fd) {
63 guac_client_plugin* plugin;
64 guac_instruction* select;
65 guac_instruction* connect;
67 /* Open guac_socket */
68 guac_socket* socket = guac_socket_open(fd);
70 /* Get protocol from select instruction */
71 select = guac_protocol_expect_instruction(
72 socket, GUACD_USEC_TIMEOUT, "select");
76 guacd_log_guac_error("Error reading \"select\"");
79 guac_socket_close(socket);
83 /* Validate args to select */
84 if (select->argc != 1) {
87 guacd_log_error("Bad number of arguments to \"select\" (%i)",
91 guac_socket_close(socket);
95 guacd_log_info("Protocol \"%s\" selected", select->argv[0]);
97 /* Get plugin from protocol in select */
98 plugin = guac_client_plugin_open(select->argv[0]);
99 guac_instruction_free(select);
101 if (plugin == NULL) {
104 guacd_log_guac_error("Error loading client plugin");
107 guac_socket_close(socket);
111 /* Send args response */
112 if (guac_protocol_send_args(socket, plugin->args)
113 || guac_socket_flush(socket)) {
116 guacd_log_guac_error("Error sending \"args\"");
118 if (guac_client_plugin_close(plugin))
119 guacd_log_guac_error("Error closing client plugin");
121 guac_socket_close(socket);
125 /* Get args from connect instruction */
126 connect = guac_protocol_expect_instruction(
127 socket, GUACD_USEC_TIMEOUT, "connect");
128 if (connect == NULL) {
131 guacd_log_guac_error("Error reading \"connect\"");
133 if (guac_client_plugin_close(plugin))
134 guacd_log_guac_error("Error closing client plugin");
136 guac_socket_close(socket);
140 /* Load and init client */
141 client = guac_client_plugin_get_client(plugin, socket,
142 connect->argc, connect->argv,
143 guacd_client_log_info, guacd_client_log_error);
145 guac_instruction_free(connect);
147 if (client == NULL) {
149 guacd_log_guac_error("Error instantiating client");
151 if (guac_client_plugin_close(plugin))
152 guacd_log_guac_error("Error closing client plugin");
154 guac_socket_close(socket);
158 /* Start client threads */
159 guacd_log_info("Starting client");
160 if (guacd_client_start(client))
161 guacd_log_error("Client finished abnormally");
163 guacd_log_info("Client finished normally");
166 guac_client_free(client);
167 if (guac_client_plugin_close(plugin))
168 guacd_log_error("Error closing client plugin");
171 guac_socket_close(socket);
177 int main(int argc, char* argv[]) {
181 struct addrinfo* addresses;
182 struct addrinfo* current_address;
183 char bound_address[1024];
187 struct addrinfo hints = {
188 .ai_family = AF_UNSPEC,
189 .ai_socktype = SOCK_STREAM,
190 .ai_protocol = IPPROTO_TCP
194 struct sockaddr_in client_addr;
195 socklen_t client_addr_len;
196 int connected_socket_fd;
199 char* listen_address = NULL; /* Default address of INADDR_ANY */
200 char* listen_port = "4822"; /* Default port */
201 char* pidfile = NULL;
211 /* Parse arguments */
212 while ((opt = getopt(argc, argv, "l:b:p:f")) != -1) {
214 listen_port = strdup(optarg);
216 else if (opt == 'b') {
217 listen_address = strdup(optarg);
219 else if (opt == 'f') {
222 else if (opt == 'p') {
223 pidfile = strdup(optarg);
227 fprintf(stderr, "USAGE: %s"
229 " [-b LISTENADDRESS]"
237 /* Set up logging prefix */
238 strncpy(log_prefix, basename(argv[0]), sizeof(log_prefix));
241 /* Get addresses for binding */
242 if ((retval = getaddrinfo(listen_address, listen_port, &hints, &addresses))) {
243 guacd_log_error("Error parsing given address or port: %s",
244 gai_strerror(retval));
249 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
251 guacd_log_error("Error opening socket: %s", strerror(errno));
255 /* Allow socket reuse */
256 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
257 guacd_log_info("Unable to set socket options for reuse: %s", strerror(errno));
260 /* Attempt binding of each address until success */
261 current_address = addresses;
262 while (current_address != NULL) {
266 /* Resolve hostname */
267 if ((retval = getnameinfo(current_address->ai_addr,
268 current_address->ai_addrlen,
269 bound_address, sizeof(bound_address),
270 bound_port, sizeof(bound_port),
271 NI_NUMERICHOST | NI_NUMERICSERV)))
272 guacd_log_error("Unable to resolve host: %s",
273 gai_strerror(retval));
275 /* Attempt to bind socket to address */
277 current_address->ai_addr,
278 current_address->ai_addrlen) == 0) {
280 guacd_log_info("Successfully bound socket to "
281 "host %s, port %s", bound_address, bound_port);
283 /* Done if successful bind */
288 /* Otherwise log information regarding bind failure */
290 guacd_log_info("Unable to bind socket to "
291 "host %s, port %s: %s",
292 bound_address, bound_port, strerror(errno));
294 current_address = current_address->ai_next;
298 /* If unable to bind to anything, fail */
299 if (current_address == NULL) {
300 guacd_log_error("Unable to bind socket to any addresses.");
306 /* Fork into background */
310 if (daemon_pid == -1) {
311 guacd_log_error("Error forking daemon process: %s", strerror(errno));
315 /* If parent, write PID file and exit */
316 else if (daemon_pid != 0) {
318 if (pidfile != NULL) {
320 /* Attempt to open pidfile and write PID */
321 FILE* pidf = fopen(pidfile, "w");
323 fprintf(pidf, "%d\n", daemon_pid);
327 /* Warn on failure */
329 guacd_log_error("Could not write PID file: %s", strerror(errno));
340 openlog(NULL, LOG_PID, LOG_DAEMON);
343 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
344 guacd_log_info("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
347 /* Ignore SIGCHLD (force automatic removal of children) */
348 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
349 guacd_log_info("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
352 /* Log listening status */
354 "Listening on host %s, port %s", bound_address, bound_port);
357 freeaddrinfo(addresses);
364 /* Listen for connections */
365 if (listen(socket_fd, 5) < 0) {
366 guacd_log_error("Could not listen on socket: %s", strerror(errno));
370 /* Accept connection */
371 client_addr_len = sizeof(client_addr);
372 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
373 if (connected_socket_fd < 0) {
374 guacd_log_error("Could not accept client connection: %s", strerror(errno));
379 * Once connection is accepted, send child into background.
381 * Note that we prefer fork() over threads for connection-handling
382 * processes as they give each connection its own memory area, and
383 * isolate the main daemon and other connections from errors in any
384 * particular client plugin.
391 guacd_log_error("Error forking child process: %s", strerror(errno));
393 /* If child, start client, and exit when finished */
394 else if (child_pid == 0) {
395 guacd_handle_connection(connected_socket_fd);
396 close(connected_socket_fd);
400 /* If parent, close reference to child's descriptor */
401 else if (close(connected_socket_fd) < 0) {
402 guacd_log_error("Error closing daemon reference to child descriptor: %s", strerror(errno));
408 if (close(socket_fd) < 0) {
409 guacd_log_error("Could not close socket: %s", strerror(errno));