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* start_client_thread(void* data) {
66 guac_client_plugin* plugin;
67 client_thread_data* thread_data = (client_thread_data*) data;
69 guac_instruction* select;
70 guac_instruction* connect;
72 /* Open guac_socket */
73 socket = guac_socket_open(thread_data->fd);
75 /* Get protocol from select instruction */
77 guac_protocol_expect_instruction(socket, GUAC_USEC_TIMEOUT, "select");
80 guac_socket_close(socket);
85 /* Validate args to select */
86 if (select->argc != 1) {
87 guac_error = GUAC_STATUS_BAD_ARGUMENT;
88 guac_socket_close(socket);
93 /* Get plugin from protocol in select */
94 plugin = guac_client_plugin_open(select->argv[0]);
95 guac_instruction_free(select);
99 guac_socket_close(socket);
104 /* Send args response */
105 if (guac_protocol_send_args(socket, plugin->args)
106 || guac_socket_flush(socket)) {
108 if (guac_client_plugin_close(plugin)) {
109 /* TODO: LOG ERROR */
112 guac_socket_close(socket);
117 /* Get args from connect instruction */
119 guac_protocol_expect_instruction(socket, GUAC_USEC_TIMEOUT, "connect");
120 if (connect == NULL) {
122 if (guac_client_plugin_close(plugin)) {
123 /* TODO: LOG ERROR */
126 guac_socket_close(socket);
131 /* Load and init client */
132 client = guac_client_plugin_get_client(plugin, socket,
133 connect->argc, connect->argv);
134 guac_instruction_free(select);
136 if (client == NULL) {
138 if (guac_client_plugin_close(plugin)) {
139 /* TODO: LOG ERROR */
142 guac_socket_close(socket);
147 /* Start client threads */
148 guac_start_client(client);
151 guac_client_free(client);
152 if (guac_client_plugin_close(plugin)) {
153 /* TODO: LOG ERROR */
157 guac_socket_close(socket);
158 close(thread_data->fd);
165 int main(int argc, char* argv[]) {
169 struct sockaddr_in server_addr;
173 struct sockaddr_in client_addr;
174 socklen_t client_addr_len;
175 int connected_socket_fd;
178 int listen_port = 4822; /* Default port */
181 char* pidfile = NULL;
186 /* Parse arguments */
187 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
189 listen_port = atoi(optarg);
190 if (listen_port <= 0) {
191 fprintf(stderr, "Invalid port: %s\n", optarg);
195 else if (opt == 'p') {
196 pidfile = strdup(optarg);
199 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
204 /* Get binding address */
205 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
206 server_addr.sin_family = AF_INET;
207 server_addr.sin_addr.s_addr = INADDR_ANY;
208 server_addr.sin_port = htons(listen_port);
211 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
213 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
217 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
218 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno));
221 /* Bind socket to address */
222 if (bind(socket_fd, (struct sockaddr*) &server_addr,
223 sizeof(server_addr)) < 0) {
224 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
228 /* Fork into background */
233 if (daemon_pid == -1) {
234 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
238 /* If parent, write PID file and exit */
239 else if (daemon_pid != 0) {
241 if (pidfile != NULL) {
243 /* Attempt to open pidfile and write PID */
244 FILE* pidf = fopen(pidfile, "w");
246 fprintf(pidf, "%d\n", daemon_pid);
250 /* Warn on failure */
252 fprintf(stderr, "WARNING: Could not write PID file: %s\n", strerror(errno));
261 daemon_pid = getpid();
262 syslog(LOG_INFO, "fork() not defined at compile time.");
263 syslog(LOG_INFO, "guacd running in foreground only.");
266 /* Otherwise, this is the daemon */
267 syslog(LOG_INFO, "Started, listening on port %i", listen_port);
270 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
271 syslog(LOG_ERR, "Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
274 /* Ignore SIGCHLD (force automatic removal of children) */
275 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
276 syslog(LOG_ERR, "Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
285 guac_thread_t thread;
287 client_thread_data* data;
289 /* Listen for connections */
290 if (listen(socket_fd, 5) < 0) {
291 syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
295 /* Accept connection */
296 client_addr_len = sizeof(client_addr);
297 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
298 if (connected_socket_fd < 0) {
299 syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
303 data = malloc(sizeof(client_thread_data));
304 data->fd = connected_socket_fd;
307 * Once connection is accepted, send child into background, whether through
308 * fork() or through creating a thread. If thead support is not present on
309 * the platform, guacd will still work, but will only be able to handle one
310 * connection at a time.
318 * Note that we prefer fork() over threads for connection-handling
319 * processes as they give each connection its own memory area, and
320 * isolate the main daemon and other connections from errors in any
321 * particular client plugin.
328 syslog(LOG_ERR, "Error forking child process: %s\n", strerror(errno));
330 /* If child, start client, and exit when finished */
331 else if (child_pid == 0) {
332 start_client_thread(data);
336 /* If parent, close reference to child's descriptor */
337 else if (close(connected_socket_fd) < 0) {
338 syslog(LOG_ERR, "Error closing daemon reference to child descriptor: %s", strerror(errno));
343 if (guac_thread_create(&thread, start_client_thread, (void*) data))
344 syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
351 if (close(socket_fd) < 0) {
352 syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));