BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
state.c
Go to the documentation of this file.
1#include "state.h"
2
3#include <stdbool.h>
4#include <signal.h>
5#include <pthread.h>
6#include <string.h>
7
8#include "utils.h"
9
20{
21 0,
22 0,
23 0,
24 0,
25 PTHREAD_MUTEX_INITIALIZER
26};
27
41{
42 false,
43 false,
44 false,
48 NULL,
49 PTHREAD_MUTEX_INITIALIZER
50};
51
53{
54 state.pid = getpid();
55 state.start_time = time(NULL);
56 state.exit_flag = 0;
57 pthread_mutex_init(&state.exit_flag_mutex, NULL);
58
59 if (strcmp(getenv("USER"), "root") == 0)
60 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Running as root is not recommended");
61}
62
64{
65 guarded_print_line("Program uptime: %d", get_elapsed_time());
66 guarded_print_line("Program PID: %d", state.pid);
68 guarded_print_line("Program started with CLI parameters");
69 else
70 guarded_print_line("Program started without CLI parameters");
71}
72
73void set_exit_flag(volatile sig_atomic_t flag)
74{
75 pthread_mutex_lock(&state.exit_flag_mutex);
76 state.exit_flag = flag;
77 pthread_mutex_unlock(&state.exit_flag_mutex);
78}
79
80sig_atomic_t get_exit_flag()
81{
82 pthread_mutex_lock(&state.exit_flag_mutex);
83 sig_atomic_t flag = state.exit_flag;
84 pthread_mutex_unlock(&state.exit_flag_mutex);
85 return flag;
86}
87
89{
90 pthread_mutex_lock(&state.exit_flag_mutex);
92 pthread_mutex_unlock(&state.exit_flag_mutex);
93}
94
96{
97 pthread_mutex_destroy(&state.exit_flag_mutex);
98}
99
101{
102 pthread_mutex_init(&operation.operation_mutex, NULL);
104 return 0;
105}
106
108{
109 pthread_mutex_lock(&operation.operation_mutex);
112 else
113 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Peer discovery operation not set, cannot start progress");
114 pthread_mutex_unlock(&operation.operation_mutex);
115}
116
118{
119 pthread_mutex_lock(&operation.operation_mutex);
122 pthread_mutex_unlock(&operation.operation_mutex);
123}
124
125bool set_peer_discovery(bool value)
126{
127 pthread_mutex_lock(&operation.operation_mutex);
129 {
130 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Peer discovery operation in progress, cannot stop. Did you mean force_stop_peer_discovery?");
131 pthread_mutex_unlock(&operation.operation_mutex);
132 return false;
133 }
134 else
136 pthread_mutex_unlock(&operation.operation_mutex);
137 return true;
138}
139
141{
142 pthread_mutex_lock(&operation.operation_mutex);
145 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Peer discovery operation force-stopped");
146 pthread_mutex_unlock(&operation.operation_mutex);
147 return true;
148}
149
151{
152 pthread_mutex_lock(&operation.operation_mutex);
153 bool value = operation.peer_discovery;
154 pthread_mutex_unlock(&operation.operation_mutex);
155 return value;
156}
157
159{
160 pthread_mutex_lock(&operation.operation_mutex);
162 pthread_mutex_unlock(&operation.operation_mutex);
163 return value;
164}
165
167{
168 pthread_mutex_lock(&operation.operation_mutex);
170 pthread_mutex_unlock(&operation.operation_mutex);
171 return value;
172}
173
175{
176 pthread_mutex_lock(&operation.operation_mutex);
178 pthread_mutex_unlock(&operation.operation_mutex);
179 return true;
180}
181
183{
184 pthread_mutex_lock(&operation.operation_mutex);
186 pthread_mutex_unlock(&operation.operation_mutex);
187 return true;
188}
189
191{
192 pthread_mutex_lock(&operation.operation_mutex);
194 pthread_mutex_unlock(&operation.operation_mutex);
195 return true;
196}
197
199{
200 pthread_mutex_lock(&operation.operation_mutex);
201 bool value = operation.peer_discovery_daemon;
202 pthread_mutex_unlock(&operation.operation_mutex);
203 return value;
204}
205
207{
208 pthread_mutex_lock(&operation.operation_mutex);
210 pthread_mutex_unlock(&operation.operation_mutex);
211 return value;
212}
213
215{
216 pthread_mutex_lock(&operation.operation_mutex);
218 pthread_mutex_unlock(&operation.operation_mutex);
219 return value;
220}
221
222bool set_peer_discovery_dns_domain(const char* domain)
223{
224 pthread_mutex_lock(&operation.operation_mutex);
227 operation.peer_discovery_dns_domain = (char*)malloc(strlen(domain) + 1);
229 {
230 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Failed to allocate memory for peer discovery DNS domain");
231 pthread_mutex_unlock(&operation.operation_mutex);
232 return false;
233 }
234 strcpy(operation.peer_discovery_dns_domain, domain);
235 pthread_mutex_unlock(&operation.operation_mutex);
236 return true;
237}
238
240{
241 pthread_mutex_lock(&operation.operation_mutex);
242 const char* value = operation.peer_discovery_dns_domain;
243 pthread_mutex_unlock(&operation.operation_mutex);
244 return value;
245}
246
248{
249 pthread_mutex_destroy(&operation.operation_mutex);
250}
251
253{
254 return state.pid;
255}
256
258{
259 return state.start_time;
260}
261
263{
264 return time(NULL) - state.start_time;
265}
void log_message(log_level level, const char *filename, const char *source_file, const char *format,...)
Log a message used to log a message to the console or a file.
Definition log.c:89
#define BITLAB_LOG
Definition log.h:11
@ LOG_ERROR
Definition log.h:32
@ LOG_WARN
Definition log.h:31
bool set_peer_discovery_daemon(bool value)
Set the peer discovery daemon state.
Definition state.c:174
bool get_peer_discovery()
Get the peer discovery operation.
Definition state.c:150
bool set_peer_discovery_hardcoded_seeds(bool value)
Set the peer discovery hardcoded seeds state.
Definition state.c:182
bool get_peer_discovery_in_progress()
Get the peer discovery in progress state.
Definition state.c:158
bool set_peer_discovery_dns_lookup(bool value)
Set the peer discovery DNS lookup state.
Definition state.c:190
bool set_peer_discovery_dns_domain(const char *domain)
Set the peer discovery DNS domain.
Definition state.c:222
void destroy_program_state()
Destroy the program state.
Definition state.c:95
int get_elapsed_time()
Get the elapsed time since the program started.
Definition state.c:262
bool get_peer_discovery_hardcoded_seeds()
Get the peer discovery hardcoded seeds state.
Definition state.c:206
void print_program_state()
Print the program state.
Definition state.c:63
void destroy_program_operation()
Clear all program operations.
Definition state.c:247
program_state state
The program state structure used to store the state of the program.
Definition state.c:19
void set_exit_flag(volatile sig_atomic_t flag)
Set the exit flag.
Definition state.c:73
bool get_peer_discovery_succeeded()
Get the peer discovery succeeded state.
Definition state.c:166
void start_peer_discovery_progress()
Start the peer discovery progress.
Definition state.c:107
int get_pid()
Get the program PID.
Definition state.c:252
void mark_started_with_parameters()
Mark the program as started with parameters.
Definition state.c:88
int init_program_operation()
Initialize the program operation.
Definition state.c:100
program_operation operation
The program operation structure used to store the operation of the program.
Definition state.c:40
bool set_peer_discovery(bool value)
Set the peer discovery operation.
Definition state.c:125
bool get_peer_discovery_daemon()
Get the peer discovery daemon state.
Definition state.c:198
const char * get_peer_discovery_dns_domain()
Get the peer discovery DNS domain.
Definition state.c:239
sig_atomic_t get_exit_flag()
Get the exit flag.
Definition state.c:80
void init_program_state()
Initialize the program state.
Definition state.c:52
void finish_peer_discovery_progress(bool succeeded)
Finish the peer discovery progress.
Definition state.c:117
bool force_stop_peer_discovery()
Force stop the peer discovery operation.
Definition state.c:140
time_t get_start_time()
Get the program start time.
Definition state.c:257
bool get_peer_discovery_dns_lookup()
Get the peer discovery DNS lookup state.
Definition state.c:214
#define PEER_DISCOVERY_DEFAULT_DAEMON
Definition state.h:6
#define PEER_DISCOVERY_DEFAULT_HARDCODED_SEEDS
Definition state.h:7
The program operation structure used to store the operation of the program.
Definition state.h:48
bool peer_discovery_succeeded
Definition state.h:51
bool peer_discovery_in_progress
Definition state.h:50
char * peer_discovery_dns_domain
Definition state.h:55
bool peer_discovery_daemon
Definition state.h:52
bool peer_discovery_dns_lookup
Definition state.h:54
bool peer_discovery_hardcoded_seeds
Definition state.h:53
bool peer_discovery
Definition state.h:49
pthread_mutex_t operation_mutex
Definition state.h:56
The program state structure used to store the state of the program.
Definition state.h:27
volatile sig_atomic_t exit_flag
Definition state.h:31
pthread_mutex_t exit_flag_mutex
Definition state.h:32
pid_t pid
Definition state.h:28
bool started_with_parameters
Definition state.h:30
time_t start_time
Definition state.h:29
void guarded_print_line(const char *format,...)
Guarded print line function.
Definition utils.c:65