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>
50 #include <guacamole/client.h>
51 #include <guacamole/thread.h>
52 #include <guacamole/log.h>
56 typedef struct client_thread_data {
63 void* start_client_thread(void* data) {
66 client_thread_data* thread_data = (client_thread_data*) data;
68 guac_log_info("Spawning client");
70 /* Load and start client */
71 client = guac_get_client(thread_data->fd);
74 guac_log_error("Client retrieval failed");
79 guac_start_client(client);
80 guac_free_client(client);
83 if (close(thread_data->fd) < 0) {
84 guac_log_error("Error closing connection: %s", strerror(errno));
89 guac_log_info("Client finished");
95 int main(int argc, char* argv[]) {
99 struct sockaddr_in server_addr;
103 struct sockaddr_in client_addr;
104 socklen_t client_addr_len;
105 int connected_socket_fd;
108 int listen_port = 4822; /* Default port */
111 char* pidfile = NULL;
116 /* Parse arguments */
117 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
119 listen_port = atoi(optarg);
120 if (listen_port <= 0) {
121 fprintf(stderr, "Invalid port: %s\n", optarg);
125 else if (opt == 'p') {
126 pidfile = strdup(optarg);
129 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
134 /* Get binding address */
135 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
136 server_addr.sin_family = AF_INET;
137 server_addr.sin_addr.s_addr = INADDR_ANY;
138 server_addr.sin_port = htons(listen_port);
141 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
143 fprintf(stderr, "Error opening socket: %s\n", strerror(errno));
147 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (void*) &opt_on, sizeof(opt_on))) {
148 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", strerror(errno));
151 /* Bind socket to address */
152 if (bind(socket_fd, (struct sockaddr*) &server_addr,
153 sizeof(server_addr)) < 0) {
154 fprintf(stderr, "Error binding socket: %s\n", strerror(errno));
158 /* Fork into background */
163 if (daemon_pid == -1) {
164 fprintf(stderr, "Error forking daemon process: %s\n", strerror(errno));
168 /* If parent, write PID file and exit */
169 else if (daemon_pid != 0) {
171 if (pidfile != NULL) {
173 /* Attempt to open pidfile and write PID */
174 FILE* pidf = fopen(pidfile, "w");
176 fprintf(pidf, "%d\n", daemon_pid);
180 /* Warn on failure */
182 fprintf(stderr, "WARNING: Could not write PID file: %s\n", strerror(errno));
191 daemon_pid = getpid();
192 guac_log_info("fork() not defined at compile time.");
193 guac_log_info("guacd running in foreground only.");
196 /* Otherwise, this is the daemon */
197 guac_log_info("Started, listening on port %i", listen_port);
200 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
201 guac_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
204 /* Ignore SIGCHLD (force automatic removal of children) */
205 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
206 guac_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
215 guac_thread_t thread;
217 client_thread_data* data;
219 /* Listen for connections */
220 if (listen(socket_fd, 5) < 0) {
221 guac_log_error("Could not listen on socket: %s", strerror(errno));
225 /* Accept connection */
226 client_addr_len = sizeof(client_addr);
227 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
228 if (connected_socket_fd < 0) {
229 guac_log_error("Could not accept client connection: %s", strerror(errno));
233 data = malloc(sizeof(client_thread_data));
234 data->fd = connected_socket_fd;
237 * Once connection is accepted, send child into background, whether through
238 * fork() or through creating a thread. If thead support is not present on
239 * the platform, guacd will still work, but will only be able to handle one
240 * connection at a time.
248 * Note that we prefer fork() over threads for connection-handling
249 * processes as they give each connection its own memory area, and
250 * isolate the main daemon and other connections from errors in any
251 * particular client plugin.
258 guac_log_error("Error forking child process: %s\n", strerror(errno));
260 /* If child, start client, and exit when finished */
261 else if (child_pid == 0) {
262 start_client_thread(data);
266 /* If parent, close reference to child's descriptor */
267 else if (close(connected_socket_fd) < 0) {
268 guac_log_error("Error closing daemon reference to child descriptor: %s", strerror(errno));
273 if (guac_thread_create(&thread, start_client_thread, (void*) data))
274 guac_log_error("Could not create client thread: %s", strerror(errno));
281 if (close(socket_fd) < 0) {
282 guac_log_error("Could not close socket: %s", strerror(errno));