-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
globals.c
81 lines (68 loc) · 1.59 KB
/
globals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libssh/callbacks.h>
#include "globals.h"
#include "log.h"
void init_globals(struct globals_t* g)
{
memset(g, 0, sizeof(struct globals_t));
pthread_mutex_init(&g->mutex, NULL);
ssh_threads_set_callbacks(ssh_threads_get_pthread());
if (ssh_init() == -1) {
fprintf(stderr, "ssh_init() failed\n");
exit(EXIT_FAILURE);
}
g->sshbind = ssh_bind_new();
#ifndef MINIMALISTIC_BUILD
g->pid_fd = -1;
#endif
}
static void wait_for_threads(struct globals_t* g)
{
size_t num_threads;
do {
pthread_t thread = 0;
pthread_mutex_lock(&g->mutex);
{
num_threads = g->n_threads;
if (num_threads > 0) {
thread = g->head->thread;
}
}
pthread_mutex_unlock(&g->mutex);
if (num_threads > 0) {
pthread_kill(thread, SIGTERM);
pthread_join(thread, NULL);
}
} while (num_threads > 0);
}
void free_globals(struct globals_t* g)
{
#ifndef MINIMALISTIC_BUILD
if (g->pid_fd >= 0) {
if (-1 == unlink(g->pid_file)) {
my_log(LOG_DAEMON | LOG_WARNING, "WARNING: Failed to delete the PID file %s: %s", g->pid_file, strerror(errno));
}
if (-1 == close(g->pid_fd)) {
my_log(LOG_DAEMON | LOG_WARNING, "WARNING: Failed to delete the PID file %s: %s", g->pid_file, strerror(errno));
}
}
free(g->pid_file);
free(g->daemon_name);
if (!g->no_syslog) {
closelog();
}
#endif
free(g->rsa_key);
free(g->dsa_key);
free(g->ecdsa_key);
free(g->ed25519_key);
free(g->bind_address);
free(g->bind_port);
wait_for_threads(g);
pthread_mutex_destroy(&g->mutex);
ssh_bind_free(g->sshbind);
ssh_finalize();
}