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>
47 #include <netinet/in.h>
52 #include <guacamole/client.h>
53 #include <guacamole/error.h>
58 void guacd_handle_connection(int fd) {
61 guac_client_plugin* plugin;
62 guac_instruction* select;
63 guac_instruction* connect;
65 /* Open guac_socket */
66 guac_socket* socket = guac_socket_open(fd);
68 /* Get protocol from select instruction */
69 select = guac_protocol_expect_instruction(
70 socket, GUACD_USEC_TIMEOUT, "select");
74 guacd_log_guac_error("Error reading \"select\"");
77 guac_socket_close(socket);
81 /* Validate args to select */
82 if (select->argc != 1) {
85 guacd_log_error("Bad number of arguments to \"select\" (%i)",
89 guac_socket_close(socket);
93 guacd_log_info("Protocol \"%s\" selected", select->argv[0]);
95 /* Get plugin from protocol in select */
96 plugin = guac_client_plugin_open(select->argv[0]);
97 guac_instruction_free(select);
102 guacd_log_guac_error("Error loading client plugin");
105 guac_socket_close(socket);
109 /* Send args response */
110 if (guac_protocol_send_args(socket, plugin->args)
111 || guac_socket_flush(socket)) {
114 guacd_log_guac_error("Error sending \"args\"");
116 if (guac_client_plugin_close(plugin))
117 guacd_log_guac_error("Error closing client plugin");
119 guac_socket_close(socket);
123 /* Get args from connect instruction */
124 connect = guac_protocol_expect_instruction(
125 socket, GUACD_USEC_TIMEOUT, "connect");
126 if (connect == NULL) {
129 guacd_log_guac_error("Error reading \"connect\"");
131 if (guac_client_plugin_close(plugin))
132 guacd_log_guac_error("Error closing client plugin");
134 guac_socket_close(socket);
138 /* Load and init client */
139 client = guac_client_plugin_get_client(plugin, socket,
140 connect->argc, connect->argv,
141 guacd_client_log_info, guacd_client_log_error);
143 guac_instruction_free(connect);
145 if (client == NULL) {
147 guacd_log_guac_error("Error instantiating client");
149 if (guac_client_plugin_close(plugin))
150 guacd_log_guac_error("Error closing client plugin");
152 guac_socket_close(socket);
156 /* Start client threads */
157 guacd_log_info("Starting client");
158 if (guacd_client_start(client))
159 guacd_log_error("Client finished abnormally");
161 guacd_log_info("Client finished normally");
164 guac_client_free(client);
165 if (guac_client_plugin_close(plugin))
166 guacd_log_error("Error closing client plugin");
169 guac_socket_close(socket);
175 int main(int argc, char* argv[]) {
179 struct addrinfo* addresses;
180 struct addrinfo* current_address;
181 char bound_address[1024];
185 struct addrinfo hints = {
186 .ai_family = AF_UNSPEC,
187 .ai_socktype = SOCK_STREAM,
188 .ai_protocol = IPPROTO_TCP
192 struct sockaddr_in client_addr;
193 socklen_t client_addr_len;
194 int connected_socket_fd;
197 char* listen_address = NULL; /* Default address of INADDR_ANY */
198 char* listen_port = "4822"; /* Default port */
199 char* pidfile = NULL;
208 /* Parse arguments */
209 while ((opt = getopt(argc, argv, "l:b:p:")) != -1) {
211 listen_port = strdup(optarg);
213 else if (opt == 'b') {
214 listen_address = strdup(optarg);
216 else if (opt == 'p') {
217 pidfile = strdup(optarg);
221 fprintf(stderr, "USAGE: %s"
223 " [-b LISTENADDRESS]"
224 " [-p PIDFILE]\n", argv[0]);
230 /* Get addresses for binding */
231 if ((retval = getaddrinfo(listen_address, listen_port, &hints, &addresses))) {
232 guacd_log_error("Error parsing given address or port: %s",
233 gai_strerror(retval));
238 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
240 guacd_log_error("Error opening socket: %s", strerror(errno));
244 /* Allow socket reuse */
245 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
246 guacd_log_info("Unable to set socket options for reuse: %s", strerror(errno));
249 /* Attempt binding of each address until success */
250 current_address = addresses;
251 while (current_address != NULL) {
255 /* Resolve hostname */
256 if ((retval = getnameinfo(current_address->ai_addr,
257 current_address->ai_addrlen,
258 bound_address, sizeof(bound_address),
259 bound_port, sizeof(bound_port),
260 NI_NUMERICHOST | NI_NUMERICSERV)))
261 guacd_log_error("Unable to resolve host: %s",
262 gai_strerror(retval));
264 /* Attempt to bind socket to address */
266 current_address->ai_addr,
267 current_address->ai_addrlen) == 0) {
269 guacd_log_error("Successfully bound socket to "
270 "host %s, port %s", bound_address, bound_port);
272 /* Done if successful bind */
277 /* Otherwise log error */
279 guacd_log_error("Error binding socket to "
280 "host %s, port %s: %s",
281 bound_address, bound_port, strerror(errno));
283 current_address = current_address->ai_next;
287 /* If unable to bind to anything, fail */
288 if (current_address == NULL) {
289 guacd_log_error("Unable to bind socket to any addresses.");
293 /* Fork into background */
297 if (daemon_pid == -1) {
298 guacd_log_error("Error forking daemon process: %s", strerror(errno));
302 /* If parent, write PID file and exit */
303 else if (daemon_pid != 0) {
305 if (pidfile != NULL) {
307 /* Attempt to open pidfile and write PID */
308 FILE* pidf = fopen(pidfile, "w");
310 fprintf(pidf, "%d\n", daemon_pid);
314 /* Warn on failure */
316 guacd_log_error("Could not write PID file: %s", strerror(errno));
326 openlog(NULL, LOG_PID, LOG_DAEMON);
329 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
330 guacd_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
333 /* Ignore SIGCHLD (force automatic removal of children) */
334 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
335 guacd_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
338 /* Log listening status */
340 "Listening on host %s, port %s", bound_address, bound_port);
343 freeaddrinfo(addresses);
350 /* Listen for connections */
351 if (listen(socket_fd, 5) < 0) {
352 guacd_log_error("Could not listen on socket: %s", strerror(errno));
356 /* Accept connection */
357 client_addr_len = sizeof(client_addr);
358 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
359 if (connected_socket_fd < 0) {
360 guacd_log_error("Could not accept client connection: %s", strerror(errno));
365 * Once connection is accepted, send child into background.
367 * Note that we prefer fork() over threads for connection-handling
368 * processes as they give each connection its own memory area, and
369 * isolate the main daemon and other connections from errors in any
370 * particular client plugin.
377 guacd_log_error("Error forking child process: %s", strerror(errno));
379 /* If child, start client, and exit when finished */
380 else if (child_pid == 0) {
381 guacd_handle_connection(connected_socket_fd);
382 close(connected_socket_fd);
386 /* If parent, close reference to child's descriptor */
387 else if (close(connected_socket_fd) < 0) {
388 guacd_log_error("Error closing daemon reference to child descriptor: %s", strerror(errno));
394 if (close(socket_fd) < 0) {
395 guacd_log_error("Could not close socket: %s", strerror(errno));