Move all vars into global. Handle signals to perform a cleanup.
This commit is contained in:
parent
8d2f97a87d
commit
85feeebd8c
92
src/main.c
92
src/main.c
@ -4,6 +4,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "addrbook.h"
|
||||
#include "audio.h"
|
||||
@ -57,15 +58,58 @@ void version(void) {
|
||||
"\nCopyright (c) Alexander \"Arav\" Andreev <me@arav.su>\n");
|
||||
}
|
||||
|
||||
addrbook_t ab = {0};
|
||||
bool is_ab_changed = false;
|
||||
audio_t aud = {0};
|
||||
crypt_key_t ok = {0}, rk = {0};
|
||||
crypt_session_t s = {0};
|
||||
net_t n = {0};
|
||||
struct options opts = {0};
|
||||
|
||||
bool is_cleanup_performed = false;
|
||||
|
||||
void cleanup(void) {
|
||||
if (!is_cleanup_performed)
|
||||
is_cleanup_performed = true;
|
||||
else
|
||||
return;
|
||||
|
||||
if (is_ab_changed) {
|
||||
FILE *addrbook_file = fopen(opts.addressbook_path, "w+");
|
||||
if (addrbook_file != NULL) {
|
||||
if (addrbook_store(&ab, addrbook_file) == -1)
|
||||
fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno));
|
||||
fclose(addrbook_file);
|
||||
}
|
||||
}
|
||||
|
||||
options_destroy(&opts);
|
||||
addrbook_destroy(&ab);
|
||||
audio_destroy(&aud);
|
||||
net_destroy(&n);
|
||||
crypt_session_destroy(&s);
|
||||
crypt_key_destroy(&ok);
|
||||
crypt_key_destroy(&rk);
|
||||
|
||||
// if (audio_terminate_soundsystem() == -1)
|
||||
// return 1;
|
||||
}
|
||||
|
||||
void signal_handler(int signo) {
|
||||
switch (signo) {
|
||||
case SIGINT:
|
||||
case SIGABRT:
|
||||
case SIGTERM:
|
||||
case SIGQUIT:
|
||||
case SIGSEGV:
|
||||
cleanup();
|
||||
exit(0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
int result = 0;
|
||||
struct options opts = {0};
|
||||
|
||||
addrbook_t ab = {0};
|
||||
bool is_ab_changed = false;
|
||||
net_t n = {0};
|
||||
crypt_session_t s = {0};
|
||||
crypt_key_t ok = {0}, rk = {0};
|
||||
|
||||
switch (parse_argv(argc, argv, &opts)) {
|
||||
case -1: usage(); return 1;
|
||||
@ -143,6 +187,17 @@ int main(int argc, const char **argv) {
|
||||
fclose(skey_file);
|
||||
}
|
||||
|
||||
if (signal(SIGINT, signal_handler) == SIG_ERR)
|
||||
fprintf(stderr, "Cannot catch SIGINT.\n");
|
||||
if (signal(SIGABRT, signal_handler) == SIG_ERR)
|
||||
fprintf(stderr, "Cannot catch SIGABRT.\n");
|
||||
if (signal(SIGTERM, signal_handler) == SIG_ERR)
|
||||
fprintf(stderr, "Cannot catch SIGTERM.\n");
|
||||
if (signal(SIGQUIT, signal_handler) == SIG_ERR)
|
||||
fprintf(stderr, "Cannot catch SIGQUIT.\n");
|
||||
if (signal(SIGSEGV, signal_handler) == SIG_ERR)
|
||||
fprintf(stderr, "Cannot catch SIGSEGV.\n");
|
||||
|
||||
(opts.mode == 'c') ?
|
||||
(result = net_client_init(&n, opts.addr, opts.port))
|
||||
: (result = net_server_init(&n, opts.addr , opts.port));
|
||||
@ -161,7 +216,6 @@ int main(int argc, const char **argv) {
|
||||
|
||||
fprintf(stdout, "A connection with %s established!\n", rkph);
|
||||
|
||||
audio_t aud = {0};
|
||||
unsigned char aud_buf[AUDIO_BUF_SIZE];
|
||||
memset(aud_buf, 0, AUDIO_BUF_SIZE);
|
||||
|
||||
@ -185,25 +239,7 @@ int main(int argc, const char **argv) {
|
||||
|
||||
cleanup:
|
||||
|
||||
if (is_ab_changed) {
|
||||
addrbook_file = fopen(opts.addressbook_path, "w+");
|
||||
if (addrbook_file != NULL) {
|
||||
if (addrbook_store(&ab, addrbook_file) == -1)
|
||||
fprintf(stderr, "Failed to store an addressbook: %s\n", strerror(errno));
|
||||
fclose(addrbook_file);
|
||||
}
|
||||
}
|
||||
|
||||
options_destroy(&opts);
|
||||
addrbook_destroy(&ab);
|
||||
audio_destroy(&aud);
|
||||
net_destroy(&n);
|
||||
crypt_session_destroy(&s);
|
||||
crypt_key_destroy(&ok);
|
||||
crypt_key_destroy(&rk);
|
||||
|
||||
// if (audio_terminate_soundsystem() == -1)
|
||||
// return 1;
|
||||
cleanup();
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -360,8 +396,6 @@ int parse_argv(int argc, const char **argv, struct options *opts) {
|
||||
strncpy(opts->key_sec_path, config_dir, config_dir_len);
|
||||
strcat(opts->key_sec_path, "/own_sec_key");
|
||||
|
||||
printf("%s:%s:%s\n", opts->addressbook_path, opts->key_pub_path, opts->key_sec_path);
|
||||
|
||||
free(config_dir);
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user