agent: add signal handlers

This commit is contained in:
2019-01-13 12:26:15 +02:00
parent 77947d17e5
commit fe354d9f74
2 changed files with 72 additions and 14 deletions

View File

@@ -23,14 +23,20 @@
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "job.h" #include "job.h"
#include "../src/protocol.h" #include "../src/protocol.h"
#include "agent_ssl.h" #include "agent_ssl.h"
#define FAIL -1 #define FAIL -1
static SSL_CTX *ctx;
static SSL *ssl;
static int server;
static struct job_args *args; static struct job_args *args;
static pthread_t *job_thread; static pthread_t *job_thread;
static void agent_shutdown(void);
static void signal_handler(int sig);
static short get_job_slot(); static short get_job_slot();
@@ -52,11 +58,61 @@ static short find_job(unsigned id)
return -1; return -1;
} }
static void signal_handler(int sig)
{
switch (sig) {
case SIGHUP:
printf("Received SIGHUP signal. Ignoring...\n");
break;
case SIGINT:
case SIGTERM:
printf("Received SIGTERM signal!\n");
agent_shutdown();
printf("Agent has been stopped properly.\n");
_exit(EXIT_SUCCESS);
break;
default:
printf("Unhandled signal %s", strsignal(sig));
break;
}
}
static void agent_shutdown(void)
{
printf("Shutting down agent...\n");
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx); /* release context */
close(server); /* close socket */
}
static void set_env(void)
{
struct sigaction new_sigaction;
sigset_t new_sigset;
/* Set signal mask - signals we want to block */
sigemptyset(&new_sigset);
sigaddset(&new_sigset, SIGCHLD); /* ignore child */
sigaddset(&new_sigset, SIGTSTP); /* ignore Tty stop signals */
sigaddset(&new_sigset, SIGTTOU); /* ignore Tty background writes */
sigaddset(&new_sigset, SIGTTIN); /* ignore Tty background reads */
sigprocmask(SIG_BLOCK, &new_sigset, NULL); /* Block above signals */
/* Set up a signal handler */
new_sigaction.sa_handler = signal_handler;
sigemptyset(&new_sigaction.sa_mask);
new_sigaction.sa_flags = 0;
/* Signals to handle */
sigaction(SIGHUP, &new_sigaction, NULL); /* catch hangup signal */
sigaction(SIGTERM, &new_sigaction, NULL); /* catch term signal */
sigaction(SIGINT, &new_sigaction, NULL); /* catch interrupt signal */
signal(SIGPIPE, SIG_IGN); /* prevent crashing from bad writes */
}
int main(int count, char *strings[]) int main(int count, char *strings[])
{ {
SSL_CTX *ctx;
int server;
SSL *ssl;
int bytes, i; int bytes, i;
char *hostname, *portnum; char *hostname, *portnum;
@@ -85,6 +141,8 @@ int main(int count, char *strings[])
} }
printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
show_certs(ssl); show_certs(ssl);
set_env();
atexit(agent_shutdown);
if (!(args = calloc(1, sizeof(*args) * MAX_AGENT_JOBS))) { if (!(args = calloc(1, sizeof(*args) * MAX_AGENT_JOBS))) {
fprintf( stderr, fprintf( stderr,
"Failed to calloc() %d bytes for job_args! Exiting...\n", "Failed to calloc() %d bytes for job_args! Exiting...\n",
@@ -220,7 +278,4 @@ int main(int count, char *strings[])
SSL_shutdown(ssl); SSL_shutdown(ssl);
SSL_free(ssl); /* release connection state */ SSL_free(ssl); /* release connection state */
} while (bytes); } while (bytes);
close(server); /* close socket */
SSL_CTX_free(ctx); /* release context */
} }

View File

@@ -104,17 +104,20 @@ void* get_os(void *args)
system(cmd); /* TODO: set proper path */ system(cmd); /* TODO: set proper path */
if ((fd = open(dump_path, O_RDONLY)) == -1) { if ((fd = open(dump_path, O_RDONLY)) == -1) {
fprintf(stderr, "Cannot open file: %s\n", dump_path); fprintf(stderr, "Cannot open file: %s\n", dump_path);
job->buf.meta.len = sprintf((char*)job->buf.chunk.data, "Unknown"); job->buf.meta.len = sprintf((char*)job->buf.chunk.data,
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t)); "I/O error");
//SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
} else { } else {
job->buf.meta.len = read(fd, job->buf.chunk.data, sizeof(job->buf.chunk.data)); job->buf.meta.len = read(fd, job->buf.chunk.data, sizeof(job->buf.chunk.data));
if ((signed)job->buf.meta.len == -1) { if ((signed)job->buf.meta.len == -1) {
perror("Failed to read from file: "); perror("Failed to read from file: ");
} }
close(fd); close(fd);
//SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
//job->slot = FREE;
}
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t)); SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
job->slot = FREE; job->slot = FREE;
}
unlink(dump_path); unlink(dump_path);
return 0; return 0;
} }