Thread support for MinGW32
[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 #define CLOSE_SOCKET(socket) closesocket(socket)
29 #else
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #define CLOSE_SOCKET(socket) close(socket)
33 #endif
34
35 #ifdef __HAVE_PTHREAD_H__
36 #include <pthread.h>
37 #elif defined(__MINGW32)
38 #include <windows.h>
39 #include <process.h>
40 #endif
41
42 #include <errno.h>
43
44 char error[65536];
45
46 char* lasterror() {
47 #ifdef __MINGW32__
48     snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError());
49     return error;
50 #else
51     return strerror(errno);
52 #endif
53 }
54
55 #include <guacamole/client.h>
56 #include <guacamole/log.h>
57
58 typedef struct client_thread_data {
59
60     int fd;
61
62 } client_thread_data;
63
64
65 void* start_client_thread(void* data) {
66
67     guac_client* client;
68     client_thread_data* thread_data = (client_thread_data*) data;
69
70     GUAC_LOG_INFO("Spawning client");
71
72     /* Load and start client */
73     client = guac_get_client(thread_data->fd); 
74
75     if (client == NULL) {
76         GUAC_LOG_ERROR("Client retrieval failed");
77         return NULL;
78     }
79
80     guac_start_client(client);
81
82     guac_free_client(client);
83
84     /* Close socket */
85     if (CLOSE_SOCKET(thread_data->fd) < 0) {
86         GUAC_LOG_ERROR("Error closing connection: %s", lasterror());
87         free(data);
88         return NULL;
89     }
90
91     GUAC_LOG_INFO("Client finished");
92     free(data);
93     return NULL;
94
95 }
96
97 int main(int argc, char* argv[]) {
98
99     /* Server */
100     int socket_fd;
101     struct sockaddr_in server_addr;
102     int opt_on = 1;
103
104     /* Client */
105     struct sockaddr_in client_addr;
106     unsigned int client_addr_len;
107     int connected_socket_fd;
108
109     /* Arguments */
110     int listen_port = 4822; /* Default port */
111     int opt;
112
113     /* Daemon Process */
114     pid_t daemon_pid;
115
116     /* Parse arguments */
117     while ((opt = getopt(argc, argv, "l:")) != -1) {
118         if (opt == 'l') {
119             listen_port = atoi(optarg);
120             if (listen_port <= 0) {
121                 fprintf(stderr, "Invalid port: %s\n", optarg);
122                 exit(EXIT_FAILURE);
123             }
124         }
125         else {
126             fprintf(stderr, "USAGE: %s [-l LISTENPORT]\n", argv[0]);
127             exit(EXIT_FAILURE);
128         }
129     }
130
131     /* Get binding address */
132     memset(&server_addr, 0, sizeof(server_addr)); /* Zero struct */
133     server_addr.sin_family = AF_INET;
134     server_addr.sin_addr.s_addr = INADDR_ANY;
135     server_addr.sin_port = htons(listen_port);
136
137 #ifdef __MINGW32__
138     WSADATA wsadata;
139     if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
140         fprintf(stderr, "Error creating socket.");
141         exit(EXIT_FAILURE);
142     }
143 #endif
144
145     /* Get socket */
146     socket_fd = socket(AF_INET, SOCK_STREAM, 0);
147     if (socket_fd < 0) {
148         fprintf(stderr, "Error opening socket: %s\n", lasterror());
149         exit(EXIT_FAILURE);
150     }
151
152     if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) {
153         fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
154     }
155
156     /* Bind socket to address */
157     if (bind(socket_fd, (struct sockaddr*) &server_addr,
158                 sizeof(server_addr)) < 0) {
159         fprintf(stderr, "Error binding socket: %s\n", lasterror());
160         exit(EXIT_FAILURE);
161     } 
162
163     /* Fork into background */
164 #ifdef fork
165     daemon_pid = fork();
166
167     /* If error, fail */
168     if (daemon_pid == -1) {
169         fprintf(stderr, "Error forking daemon process: %s\n", lasterror());
170         exit(EXIT_FAILURE);
171     }
172
173     /* If parent, exit */
174     else if (daemon_pid != 0) {
175         exit(EXIT_SUCCESS);
176     }
177 #else
178     GUAC_LOG_INFO("fork() not defined at compile time.");
179     GUAC_LOG_INFO("guacd running in foreground only.");
180 #endif
181
182     /* Otherwise, this is the daemon */
183     GUAC_LOG_INFO("Started, listening on port %i", listen_port);
184
185     /* Daemon loop */
186     for (;;) {
187
188 #ifdef pthread_t
189         pthread_t thread;
190 #endif
191         client_thread_data* data;
192
193         /* Listen for connections */
194         if (listen(socket_fd, 5) < 0) {
195             GUAC_LOG_ERROR("Could not listen on socket: %s", lasterror());
196             return 3;
197         }
198
199         /* Accept connection */
200         client_addr_len = sizeof(client_addr);
201         connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
202         if (connected_socket_fd < 0) {
203             GUAC_LOG_ERROR("Could not accept client connection: %s", lasterror());
204             return 3;
205         }
206
207         data = malloc(sizeof(client_thread_data));
208         data->fd = connected_socket_fd;
209
210 #ifdef pthread_t
211         if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
212             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
213             return 3;
214         }
215 #elif __MINGW32__
216         if (_beginthread(start_client_thread, 0, (void*) data) == -1L) { 
217             GUAC_LOG_ERROR("Could not create client thread: %s", lasterror());
218             return 3;
219         }
220 #else
221 #warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
222         GUAC_LOG_INFO("Thread support not present at compile time.");
223         GUAC_LOG_INFO("guacd handling one connection at a time.");
224         start_client_thread(data);
225 #endif
226
227     }
228
229     /* Close socket */
230     if (CLOSE_SOCKET(socket_fd) < 0) {
231         GUAC_LOG_ERROR("Could not close socket: %s", lasterror());
232         return 3;
233     }
234
235     return 0;
236
237 }
238