Commit a6a5b493 authored by jerome.poulin's avatar jerome.poulin Committed by Jérôme Poulin

Use systemd notify system to start daemon and give status.

parent dca4e2e9
......@@ -42,6 +42,9 @@
#include <sys/socket.h>
#include <unistd.h>
#include <wait.h>
#ifdef USE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
#define BUF_SIZE 16384
......@@ -72,6 +75,7 @@ int create_connection();
int parse_options(int argc, char *argv[]);
int server_sock, client_sock, remote_sock, remote_port = 0;
int connections_processed = 0;
char *remote_host, *cmd_in, *cmd_out;
bool foreground = FALSE;
......@@ -175,9 +179,17 @@ int create_socket(int port) {
return server_sock;
}
void update_connection_count()
{
#ifdef USE_SYSTEMD
sd_notifyf(0, "STATUS=Ready. %d connections processed.\n", connections_processed);
#endif
}
/* Handle finished child process */
void sigchld_handler(int signal) {
while (waitpid(-1, NULL, WNOHANG) > 0);
update_connection_count();
}
/* Handle term signal */
......@@ -192,13 +204,19 @@ void server_loop() {
struct sockaddr_in client_addr;
socklen_t addrlen = sizeof(client_addr);
#ifdef USE_SYSTEMD
sd_notify(0, "READY=1\n");
#endif
while (TRUE) {
update_connection_count();
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
if (fork() == 0) { // handle client connection in a separate process
close(server_sock);
handle_client(client_sock, client_addr);
exit(0);
}
} else
connections_processed++;
close(client_sock);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment