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