Fixed fork() detection
[guacd.git] / src / daemon.c
1
2 /*
3  *  Guacamole - Clientless Remote Desktop
4  *  Copyright (C) 2010  Michael Jumper
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Affero General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25
26 #ifdef __MINGW32__
27 #include <winsock2.h>
28 #else
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #endif
32
33 #ifdef HAVE_LIBPTHREAD
34 #include <pthread.h>
35 #elif defined(__MINGW32__)
36 #include <windows.h>
37 #include <process.h>
38 #endif
39
40 #include <errno.h>
41
42 #include <guacamole/client.h>
43 #include <guacamole/log.h>
44
45 /* Windows / MINGW32 handles closing sockets differently */ 
46 #ifdef __MINGW32__
47 #define CLOSE_SOCKET(socket) closesocket(socket)
48 #else
49 #define CLOSE_SOCKET(socket) close(socket)
50 #endif
51
52
53 /* Cross-platform strerror()/errno clone */
54 char error[65536];
55 char* lasterror() {
56 #ifdef __MINGW32__
57     snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
58     return error;
59 #else
60     return strerror(errno);
61 #endif
62 }
63
64
65 typedef struct client_thread_data {
66
67     int fd;
68
69 } client_thread_data;
70
71
72 void* start_client_thread(void* data) {
73
74     guac_client* client;
75     client_thread_data* thread_data = (client_thread_data*) data;
76
77     GUAC_LOG_INFO("Spawning client");
78
79     /* Load and start client */
80     client = guac_get_client(thread_data->fd); 
81
82     if (client == NULL) {
83         GUAC_LOG_ERROR("Client retrieval failed");
84         return NULL;
85     }
86
87     guac_start_client(client);
88
89     guac_free_client(client);
90
91     /* Close socket */
92     if (CLOSE_SOCKET(thread_data->fd) < 0) {
93         GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
94         free(data);
95         return NULL;
96     }
97
98     GUAC_LOG_INFO("Client finished");
99     free(data);
100     return NULL;
101
102 }
103
104 int main(int argc, char* argv[]) {
105
106     /* Server */
107     int socket_fd;
108     struct sockaddr_in server_addr;
109     int opt_on = 1;
110
111     /* Client */
112     struct sockaddr_in client_addr;
113     unsigned int client_addr_len;
114     int connected_socket_fd;
115
116     /* Arguments */
117     int listen_port = 4822; /* Default port */
118     int opt;
119
120     /* Daemon Process */
121     pid_t daemon_pid;
122
123 #ifdef __MINGW32__
124     /* Structure for holding winsock version info */
125     WSADATA wsadata;
126 #endif
127
128     /* Parse arguments */
129     while ((opt = getopt(argc, argv, "l:")) != -1) {
130         if (opt == 'l') {
131             listen_port = atoi(optarg);
132             if (listen_port <= 0) {
133                 fprintf(stderr, "Invalid port: %s\n", optarg);
134                 exit(EXIT_FAILURE);
135             }
136         }
137         else {
138             fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
139             exit(EXIT_FAILURE);
140         }
141     }
142
143     /* Get binding address */
144     memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
145     server_addr.sin_family = AF_INET;
146     server_addr.sin_addr.s_addr = INADDR_ANY;
147     server_addr.sin_port = htons(listen_port);
148
149 #ifdef __MINGW32__
150     /* If compiling for Windows, init winsock. */
151     if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
152         fprintf(stderr, "Error creating socket.");
153         exit(EXIT_FAILURE);
154     }
155 #endif
156
157     /* Get socket */
158     socket_fd = socket(AF_INET, SOCK_STREAM, 0);
159     if (socket_fd < 0) {
160         fprintf(stderr, "Error opening socket: %s\n", lasterror());
161         exit(EXIT_FAILURE);
162     }
163
164     if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
165         fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
166     }
167
168     /* Bind socket to address */
169     if (bind(socket_fd, (struct sockaddr*) &server_addr,
170                 sizeof(server_addr)) < 0) {
171         fprintf(stderr, "Error binding socket: %s\n", lasterror());
172         exit(EXIT_FAILURE);
173     } 
174
175     /* Fork into background */
176 #ifdef HAVE_FORK
177     daemon_pid = fork();
178
179     /* If error, fail */
180     if (daemon_pid == -1) {
181         fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
182         exit(EXIT_FAILURE);
183     }
184
185     /* If parent, exit */
186     else if (daemon_pid != 0) {
187         exit(EXIT_SUCCESS);
188     }
189 #else
190     GUAC_LOG_INFO("fork() not defined at compile time.");
191     GUAC_LOG_INFO("guacd running in foreground only.");
192 #endif
193
194     /* Otherwise, this is the daemon */
195     GUAC_LOG_INFO("Started, listening on port %i", listen_port);
196
197     /* Daemon loop */
198     for (;;) {
199
200 #ifdef HAVE_LIBPTHREAD
201         pthread_t thread;
202 #endif
203         client_thread_data* data;
204
205         /* Listen for connections */
206         if (listen(socket_fd, 5) < 0) {
207             GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
208             return 3;
209         }
210
211         /* Accept connection */
212         client_addr_len = sizeof(client_addr);
213         connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
214         if (connected_socket_fd < 0) {
215             GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
216             return 3;
217         }
218
219         data = malloc(sizeof(client_thread_data));
220         data->fd = connected_socket_fd;
221
222 #ifdef HAVE_LIBPTHREAD
223         if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
224             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
225             return 3;
226         }
227 #elif __MINGW32__
228         if (_beginthread(start_client_thread, 0, (void*) data) == -1L) { 
229             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
230             return 3;
231         }
232 #else
233 #warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
234         GUAC_LOG_INFO("Thread support not present at compile time.");
235         GUAC_LOG_INFO("guacd handling one connection at a time.");
236         start_client_thread(data);
237 #endif
238
239     }
240
241     /* Close socket */
242     if (CLOSE_SOCKET(socket_fd) < 0) {
243         GUAC_LOG_ERROR("Could not close socket: %s", lasterror());
244         return 3;
245     }
246
247     return 0;
248
249 }
250