From: Michael Jumper Date: Wed, 9 Feb 2011 03:58:26 +0000 (-0800) Subject: Allow compile even if fork() and pthreads not present. X-Git-Url: http://git.alex.org.uk Allow compile even if fork() and pthreads not present. --- diff --git a/.gitignore b/.gitignore index 22896dd..013e632 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Compiled proxy guacd +guacd.exe # Object code *.o diff --git a/configure.in b/configure.in index 014a731..18c25cf 100644 --- a/configure.in +++ b/configure.in @@ -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]) diff --git a/src/daemon.c b/src/daemon.c index 20ef136..cd4257b 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -22,14 +22,22 @@ #include #include #include + +#ifdef __MINGW32__ +#include +#else #include #include +#endif + +#ifdef __HAVE_PTHREAD_H__ #include +#endif #include -#include #include +#include 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; }