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)) {
107 if (guac_client_plugin_close(plugin)) {
108 /* TODO: LOG ERROR */
111 guac_socket_close(socket);
116 /* Get args from connect instruction */
118 guac_protocol_expect_instruction(socket, GUAC_USEC_TIMEOUT, "connect");
119 if (connect == NULL) {
121 if (guac_client_plugin_close(plugin)) {
122 /* TODO: LOG ERROR */
125 guac_socket_close(socket);
130 /* Load and init client */
131 client = guac_client_plugin_get_client(plugin, socket,
132 connect->argc, connect->argv);
133 guac_instruction_free(select);
135 if (client == NULL) {
137 if (guac_client_plugin_close(plugin)) {
138 /* TODO: LOG ERROR */
141 guac_socket_close(socket);
146 /* Start client threads */
147 guac_start_client(client);
150 guac_client_free(client);
151 if (guac_client_plugin_close(plugin)) {
152 /* TODO: LOG ERROR */
156 guac_socket_close(socket);
157 close(thread_data->fd);
164 int main(int argc, char* argv[]) {
168 struct sockaddr_in server_addr;
172 struct sockaddr_in client_addr;
173 socklen_t client_addr_len;
174 int connected_socket_fd;
177 int listen_port = 4822; /* Default port */
180 char* pidfile = NULL;
185 /* Parse arguments */
186 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
188 listen_port = atoi(optarg);
189 if (listen_port <= 0) {
190 fprintf(stderr, "Invalid port: %s\n", optarg);
194 else if (opt == 'p') {
195 pidfile = strdup(optarg);
198 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
203 /* Get binding address */
204 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
205 server_addr.sin_family = AF_INET;
206 server_addr.sin_addr.s_addr = INADDR_ANY;
207 server_addr.sin_port = htons(listen_port);
210 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
212 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
216 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
217 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno));
220 /* Bind socket to address */
221 if (bind(socket_fd, (struct sockaddr*) &server_addr,
222 sizeof(server_addr)) < 0) {
223 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
227 /* Fork into background */
232 if (daemon_pid == -1) {
233 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
237 /* If parent, write PID file and exit */
238 else if (daemon_pid != 0) {
240 if (pidfile != NULL) {
242 /* Attempt to open pidfile and write PID */
243 FILE* pidf = fopen(pidfile, "w");
245 fprintf(pidf, "%d\n", daemon_pid);
249 /* Warn on failure */
251 fprintf(stderr, "WARNING: Could not write PID file: %s\n", strerror(errno));
260 daemon_pid = getpid();
261 syslog(LOG_INFO, "fork() not defined at compile time.");
262 syslog(LOG_INFO, "guacd running in foreground only.");
265 /* Otherwise, this is the daemon */
266 syslog(LOG_INFO, "Started, listening on port %i", listen_port);
269 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
270 syslog(LOG_ERR, "Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
273 /* Ignore SIGCHLD (force automatic removal of children) */
274 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
275 syslog(LOG_ERR, "Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
284 guac_thread_t thread;
286 client_thread_data* data;
288 /* Listen for connections */
289 if (listen(socket_fd, 5) < 0) {
290 syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
294 /* Accept connection */
295 client_addr_len = sizeof(client_addr);
296 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
297 if (connected_socket_fd < 0) {
298 syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
302 data = malloc(sizeof(client_thread_data));
303 data->fd = connected_socket_fd;
306 * Once connection is accepted, send child into background, whether through
307 * fork() or through creating a thread. If thead support is not present on
308 * the platform, guacd will still work, but will only be able to handle one
309 * connection at a time.
317 * Note that we prefer fork() over threads for connection-handling
318 * processes as they give each connection its own memory area, and
319 * isolate the main daemon and other connections from errors in any
320 * particular client plugin.
327 syslog(LOG_ERR, "Error forking child process: %s\n", strerror(errno));
329 /* If child, start client, and exit when finished */
330 else if (child_pid == 0) {
331 start_client_thread(data);
335 /* If parent, close reference to child's descriptor */
336 else if (close(connected_socket_fd) < 0) {
337 syslog(LOG_ERR, "Error closing daemon reference to child descriptor: %s", strerror(errno));
342 if (guac_thread_create(&thread, start_client_thread, (void*) data))
343 syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
350 if (close(socket_fd) < 0) {
351 syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));