Allow compile even if fork() and pthreads not present.

This commit is contained in:
Michael Jumper 2011-02-08 19:58:26 -08:00
parent 55f5755fc3
commit d7b1bc8dae
3 changed files with 34 additions and 11 deletions

1
guacd/.gitignore vendored
View File

@ -1,6 +1,7 @@
# Compiled proxy
guacd
guacd.exe
# Object code
*.o

View File

@ -9,7 +9,8 @@ AC_PROG_CC
# Checks for libraries.
AC_CHECK_LIB([guac], [guac_get_client],, AC_MSG_ERROR("libguac must be installed first"))
AC_CHECK_LIB([pthread], [pthread_create],, AC_MSG_ERROR("POSIX threads (libpthread) must be installed"))
AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_LIB([wsock32], [main])
# Checks for header files.
AC_CHECK_HEADERS([netinet/in.h stdlib.h string.h sys/socket.h syslog.h unistd.h guacamole/client.h])

View File

@ -22,14 +22,22 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#ifdef __HAVE_PTHREAD_H__
#include <pthread.h>
#endif
#include <errno.h>
#include <syslog.h>
#include <guacamole/client.h>
#include <guacamole/guaclog.h>
typedef struct client_thread_data {
@ -43,13 +51,13 @@ void* start_client_thread(void* data) {
guac_client* client;
client_thread_data* thread_data = (client_thread_data*) data;
syslog(LOG_INFO, "Spawning client");
GUAC_LOG_INFO("Spawning client");
/* Load and start client */
client = guac_get_client(thread_data->fd);
if (client == NULL) {
syslog(LOG_ERR, "Client retrieval failed");
GUAC_LOG_ERROR("Client retrieval failed");
return NULL;
}
@ -59,12 +67,12 @@ void* start_client_thread(void* data) {
/* Close socket */
if (close(thread_data->fd) < 0) {
syslog(LOG_ERR, "Error closing connection: %s", strerror(errno));
GUAC_LOG_ERROR("Error closing connection: %s", strerror(errno));
free(data);
return NULL;
}
syslog(LOG_INFO, "Client finished");
GUAC_LOG_INFO("Client finished");
free(data);
return NULL;
@ -129,6 +137,7 @@ int main(int argc, char* argv[]) {
}
/* Fork into background */
#ifdef fork
daemon_pid = fork();
/* If error, fail */
@ -141,19 +150,25 @@ int main(int argc, char* argv[]) {
else if (daemon_pid != 0) {
exit(EXIT_SUCCESS);
}
#else
GUAC_LOG_INFO("fork() not defined at compile time.");
GUAC_LOG_INFO("guacd running in foreground only.");
#endif
/* Otherwise, this is the daemon */
syslog(LOG_INFO, "Started, listening on port %i", listen_port);
GUAC_LOG_INFO("Started, listening on port %i", listen_port);
/* Daemon loop */
for (;;) {
#ifdef pthread_t
pthread_t thread;
#endif
client_thread_data* data;
/* Listen for connections */
if (listen(socket_fd, 5) < 0) {
syslog(LOG_ERR, "Could not listen on socket: %s", strerror(errno));
GUAC_LOG_ERROR("Could not listen on socket: %s", strerror(errno));
return 3;
}
@ -161,23 +176,29 @@ int main(int argc, char* argv[]) {
client_addr_len = sizeof(client_addr);
connected_socket_fd = accept(socket_fd, (struct sockaddr*) &client_addr, &client_addr_len);
if (connected_socket_fd < 0) {
syslog(LOG_ERR, "Could not accept client connection: %s", strerror(errno));
GUAC_LOG_ERROR("Could not accept client connection: %s", strerror(errno));
return 3;
}
data = malloc(sizeof(client_thread_data));
data->fd = connected_socket_fd;
#ifdef pthread_t
if (pthread_create(&thread, NULL, start_client_thread, (void*) data)) {
syslog(LOG_ERR, "Could not create client thread: %s", strerror(errno));
GUAC_LOG_ERROR("Could not create client thread: %s", strerror(errno));
return 3;
}
#else
GUAC_LOG_INFO("POSIX threads support not present at compile time.");
GUAC_LOG_INFO("guacd handling one connection at a time.");
start_client_thread(data);
#endif
}
/* Close socket */
if (close(socket_fd) < 0) {
syslog(LOG_ERR, "Could not close socket: %s", strerror(errno));
GUAC_LOG_ERROR("Could not close socket: %s", strerror(errno));
return 3;
}