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>
47 typedef int socklen_t;
48 typedef char* sockopt_p;
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 typedef void* sockopt_p;
57 #include <guacamole/client.h>
58 #include <guacamole/thread.h>
59 #include <guacamole/log.h>
61 /* Windows / MINGW32 handles closing sockets differently */
63 #define CLOSE_SOCKET(socket) closesocket(socket)
65 #define CLOSE_SOCKET(socket) close(socket)
69 /* Cross-platform strerror()/errno clone */
73 snprintf(error, sizeof(error)-1, "ERROR #%li", GetLastError());
76 return strerror(errno);
80 typedef struct client_thread_data {
87 void* start_client_thread(void* data) {
90 client_thread_data* thread_data = (client_thread_data*) data;
92 guac_log_info("Spawning client");
94 /* Load and start client */
95 client = guac_get_client(thread_data->fd);
98 guac_log_error("Client retrieval failed");
103 guac_start_client(client);
104 guac_free_client(client);
107 if (CLOSE_SOCKET(thread_data->fd) < 0) {
108 guac_log_error("Error closing connection: %s", lasterror());
113 guac_log_info("Client finished");
119 int main(int argc, char* argv[]) {
123 struct sockaddr_in server_addr;
127 struct sockaddr_in client_addr;
128 socklen_t client_addr_len;
129 int connected_socket_fd;
132 int listen_port = 4822; /* Default port */
135 char* pidfile = NULL;
141 /* Structure for holding winsock version info */
145 /* Parse arguments */
146 while ((opt = getopt(argc, argv, "l:p:")) != -1) {
148 listen_port = atoi(optarg);
149 if (listen_port <= 0) {
150 fprintf(stderr, "Invalid port: %s\n", optarg);
154 else if (opt == 'p') {
155 pidfile = strdup(optarg);
158 fprintf(stderr, "USAGE: %s [-l LISTENPORT] [-p PIDFILE]\n", argv[0]);
163 /* Get binding address */
164 memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
165 server_addr.sin_family = AF_INET;
166 server_addr.sin_addr.s_addr = INADDR_ANY;
167 server_addr.sin_port = htons(listen_port);
170 /* If compiling for Windows, init winsock. */
171 if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
172 fprintf(stderr, "Error creating socket.");
178 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
180 fprintf(stderr, "Error opening socket: %s\n", lasterror());
184 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (sockopt_p) &opt_on, sizeof(opt_on))) {
185 fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
188 /* Bind socket to address */
189 if (bind(socket_fd, (struct sockaddr*) &server_addr,
190 sizeof(server_addr)) < 0) {
191 fprintf(stderr, "Error binding socket: %s\n", lasterror());
195 /* Fork into background */
200 if (daemon_pid == -1) {
201 fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
205 /* If parent, write PID file and exit */
206 else if (daemon_pid != 0) {
208 if (pidfile != NULL) {
210 /* Attempt to open pidfile and write PID */
211 FILE* pidf = fopen(pidfile, "w");
213 fprintf(pidf, "%d\n", daemon_pid);
217 /* Warn on failure */
219 fprintf(stderr, "WARNING: Could not write PID file: %s\n", lasterror());
228 daemon_pid = getpid();
229 guac_log_info("fork() not defined at compile time.");
230 guac_log_info("guacd running in foreground only.");
233 /* Otherwise, this is the daemon */
234 guac_log_info("Started, listening on port %i", listen_port);
238 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
239 guac_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
242 /* Ignore SIGCHLD (force automatic removal of children) */
243 if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
244 guac_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
254 guac_thread_t thread;
256 client_thread_data* data;
258 /* Listen for connections */
259 if (listen(socket_fd, 5) < 0) {
260 guac_log_error("Could not listen on socket: %s", lasterror());
264 /* Accept connection */
265 client_addr_len = sizeof(client_addr);
266 connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
267 if (connected_socket_fd < 0) {
268 guac_log_error("Could not accept client connection: %s", lasterror());
272 data = malloc(sizeof(client_thread_data));
273 data->fd = connected_socket_fd;
276 * Once connection is accepted, send child into background, whether through
277 * fork() or through creating a thread. If thead support is not present on
278 * the platform, guacd will still work, but will only be able to handle one
279 * connection at a time.
287 * Note that we prefer fork() over threads for connection-handling
288 * processes as they give each connection its own memory area, and
289 * isolate the main daemon and other connections from errors in any
290 * particular client plugin.
297 guac_log_error("Error forking child process: %s\n", lasterror());
299 /* If child, start client, and exit when finished */
300 else if (child_pid == 0) {
301 start_client_thread(data);
307 if (guac_thread_create(&thread, start_client_thread, (void*) data))
308 guac_log_error("Could not create client thread: %s", lasterror());
315 if (CLOSE_SOCKET(socket_fd) < 0) {
316 guac_log_error("Could not close socket: %s", lasterror());