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