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