Set atexit once. Next time, RTFM.

This commit is contained in:
2018-02-16 14:23:41 +02:00
parent 7ebfa213ac
commit 3a0ccd3b31

30
rmps.c
View File

@@ -141,6 +141,7 @@ static void spawn_pidfile(const char *pidfile)
log(ERROR, "Could not create PID file %s - Exiting!", pidfile); log(ERROR, "Could not create PID file %s - Exiting!", pidfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
atexit(cleanup);
/* Try to lock file */ /* Try to lock file */
if (lockf(pid_file_handle, F_TLOCK, 0) == -1) { if (lockf(pid_file_handle, F_TLOCK, 0) == -1) {
@@ -174,27 +175,23 @@ static int open_listener(int port)
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
sd = socket(addr.sin_family, SOCK_STREAM, 0); sd = socket(addr.sin_family, SOCK_STREAM, 0);
if (sd < 0) { if (sd < 0) {
log(ERROR, "Failed to create socket"); log(ERROR, "Failed to create socket - Aborting RMPS...");
goto exit; exit(EXIT_FAILURE);
} }
if (set_reuse_addr(sd) < 0) { if (set_reuse_addr(sd) < 0) {
log(ERROR, log(ERROR,
"Failed to set reuse on address - Aborting...", port); "Failed to set reuse on address - Aborting RMPS...", port);
goto exit; exit(EXIT_FAILURE);
} }
if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
log(ERROR, "Failed to bind on port: %d - Aborting...", port); log(ERROR, "Failed to bind on port: %d - Aborting RMPS...", port);
goto exit; exit(EXIT_FAILURE);
} }
if (listen(sd, 10) != 0) { if (listen(sd, 10) != 0) {
log(ERROR, "Failed to start listener on port %d - Aborting...", log(ERROR, "Failed to start listener on port %d - Aborting RMPS...", port);
port); exit(EXIT_FAILURE);
goto exit;
} }
return sd; return sd;
exit:
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
} }
/* Init server and create context */ /* Init server and create context */
@@ -238,21 +235,21 @@ void load_certificates(SSL_CTX *ctx, const char *certfile,
log(ERROR, "Failed to load certfile! SSL error below:"); log(ERROR, "Failed to load certfile! SSL error below:");
log_ssl(); log_ssl();
log(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); exit(EXIT_FAILURE);
} }
/* set the private key from KeyFile (may be the same as CertFile) */ /* set the private key from KeyFile (may be the same as CertFile) */
if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0) { if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0) {
log(ERROR, "Failed to load keyfile! SSL error below:"); log(ERROR, "Failed to load keyfile! SSL error below:");
log_ssl(); log_ssl();
log(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); exit(EXIT_FAILURE);
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ctx)) { if (!SSL_CTX_check_private_key(ctx)) {
log(ERROR, log(ERROR,
"Private key does not match the public certificate."); "Private key does not match the public certificate.");
log(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); exit(EXIT_FAILURE);
} }
if (cafile != NULL) { if (cafile != NULL) {
SSL_CTX_set_client_CA_list(ctx, SSL_CTX_set_client_CA_list(ctx,
@@ -308,8 +305,7 @@ void launch_rmps(struct conf_table *conf, int fork_flag)
if (start_job_queue(conf->rmps.agent_poolsize) == FAIL) { if (start_job_queue(conf->rmps.agent_poolsize) == FAIL) {
log(ERROR, log(ERROR,
"On start_job_queue(), RMPS failed to start, shutting down..."); "On start_job_queue(), RMPS failed to start, shutting down...");
atexit(cleanup); exit(EXIT_FAILURE);
return;
} }
pthread_join(pool[0], NULL); pthread_join(pool[0], NULL);
pthread_join(pool[1], NULL); pthread_join(pool[1], NULL);