BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
state.c File Reference
#include "state.h"
#include <stdbool.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include "utils.h"

Go to the source code of this file.

Functions

void init_program_state ()
 Initialize the program state.
 
void print_program_state ()
 Print the program state.
 
void set_exit_flag (volatile sig_atomic_t flag)
 Set the exit flag.
 
sig_atomic_t get_exit_flag ()
 Get the exit flag.
 
void mark_started_with_parameters ()
 Mark the program as started with parameters.
 
void destroy_program_state ()
 Destroy the program state.
 
int init_program_operation ()
 Initialize the program operation.
 
void start_peer_discovery_progress ()
 Start the peer discovery progress.
 
void finish_peer_discovery_progress (bool succeeded)
 Finish the peer discovery progress.
 
bool set_peer_discovery (bool value)
 Set the peer discovery operation.
 
bool force_stop_peer_discovery ()
 Force stop the peer discovery operation.
 
bool get_peer_discovery ()
 Get the peer discovery operation.
 
bool get_peer_discovery_in_progress ()
 Get the peer discovery in progress state.
 
bool get_peer_discovery_succeeded ()
 Get the peer discovery succeeded state.
 
bool set_peer_discovery_daemon (bool value)
 Set the peer discovery daemon state.
 
bool set_peer_discovery_hardcoded_seeds (bool value)
 Set the peer discovery hardcoded seeds state.
 
bool set_peer_discovery_dns_lookup (bool value)
 Set the peer discovery DNS lookup state.
 
bool get_peer_discovery_daemon ()
 Get the peer discovery daemon state.
 
bool get_peer_discovery_hardcoded_seeds ()
 Get the peer discovery hardcoded seeds state.
 
bool get_peer_discovery_dns_lookup ()
 Get the peer discovery DNS lookup state.
 
bool set_peer_discovery_dns_domain (const char *domain)
 Set the peer discovery DNS domain.
 
const char * get_peer_discovery_dns_domain ()
 Get the peer discovery DNS domain.
 
void destroy_program_operation ()
 Clear all program operations.
 
int get_pid ()
 Get the program PID.
 
time_t get_start_time ()
 Get the program start time.
 
int get_elapsed_time ()
 Get the elapsed time since the program started.
 

Variables

program_state state
 The program state structure used to store the state of the program.
 
program_operation operation
 The program operation structure used to store the operation of the program.
 

Function Documentation

◆ destroy_program_operation()

void destroy_program_operation ( )

Clear all program operations.

Definition at line 247 of file state.c.

248{
249 pthread_mutex_destroy(&operation.operation_mutex);
250}
program_operation operation
The program operation structure used to store the operation of the program.
Definition state.c:40
pthread_mutex_t operation_mutex
Definition state.h:56

References operation, and program_operation::operation_mutex.

Referenced by run_bitlab().

◆ destroy_program_state()

void destroy_program_state ( )

Destroy the program state.

Definition at line 95 of file state.c.

96{
97 pthread_mutex_destroy(&state.exit_flag_mutex);
98}
program_state state
The program state structure used to store the state of the program.
Definition state.c:19
pthread_mutex_t exit_flag_mutex
Definition state.h:32

References program_state::exit_flag_mutex, and state.

Referenced by run_bitlab().

◆ finish_peer_discovery_progress()

void finish_peer_discovery_progress ( bool  succeeded)

Finish the peer discovery progress.

Parameters
succeededThe flag to indicate if the peer discovery operation succeeded.

Definition at line 117 of file state.c.

118{
119 pthread_mutex_lock(&operation.operation_mutex);
122 pthread_mutex_unlock(&operation.operation_mutex);
123}
bool peer_discovery_succeeded
Definition state.h:51
bool peer_discovery_in_progress
Definition state.h:50

References operation, program_operation::operation_mutex, program_operation::peer_discovery_in_progress, and program_operation::peer_discovery_succeeded.

Referenced by handle_peer_discovery().

◆ force_stop_peer_discovery()

bool force_stop_peer_discovery ( )

Force stop the peer discovery operation.

Returns
True if successful, otherwise false.

Definition at line 140 of file state.c.

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}
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_WARN
Definition log.h:31
bool peer_discovery
Definition state.h:49

References BITLAB_LOG, log_message(), LOG_WARN, operation, program_operation::operation_mutex, program_operation::peer_discovery, and program_operation::peer_discovery_in_progress.

◆ get_elapsed_time()

int get_elapsed_time ( )

Get the elapsed time since the program started.

Returns
The elapsed time.

Definition at line 262 of file state.c.

263{
264 return time(NULL) - state.start_time;
265}
time_t start_time
Definition state.h:29

References program_state::start_time, and state.

Referenced by print_program_state().

◆ get_exit_flag()

sig_atomic_t get_exit_flag ( )

Get the exit flag.

Returns
The exit flag.

Definition at line 80 of file state.c.

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}
volatile sig_atomic_t exit_flag
Definition state.h:31

References program_state::exit_flag, program_state::exit_flag_mutex, and state.

Referenced by handle_cli(), handle_peer_discovery(), and run_bitlab().

◆ get_peer_discovery()

bool get_peer_discovery ( )

Get the peer discovery operation.

Returns
The peer discovery operation state.

Definition at line 150 of file state.c.

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}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery.

Referenced by cli_peer_discovery(), and handle_peer_discovery().

◆ get_peer_discovery_daemon()

bool get_peer_discovery_daemon ( )

Get the peer discovery daemon state.

Returns
The peer discovery daemon state.

Definition at line 198 of file state.c.

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}
bool peer_discovery_daemon
Definition state.h:52

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_daemon.

◆ get_peer_discovery_dns_domain()

const char * get_peer_discovery_dns_domain ( )

Get the peer discovery DNS domain.

Returns
The peer discovery DNS domain.

Definition at line 239 of file state.c.

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}
char * peer_discovery_dns_domain
Definition state.h:55

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_dns_domain.

Referenced by handle_peer_discovery().

◆ get_peer_discovery_dns_lookup()

bool get_peer_discovery_dns_lookup ( )

Get the peer discovery DNS lookup state.

Returns
The peer discovery DNS lookup state.

Definition at line 214 of file state.c.

215{
216 pthread_mutex_lock(&operation.operation_mutex);
218 pthread_mutex_unlock(&operation.operation_mutex);
219 return value;
220}
bool peer_discovery_dns_lookup
Definition state.h:54

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_dns_lookup.

Referenced by handle_peer_discovery().

◆ get_peer_discovery_hardcoded_seeds()

bool get_peer_discovery_hardcoded_seeds ( )

Get the peer discovery hardcoded seeds state.

Returns
The peer discovery hardcoded seeds state.

Definition at line 206 of file state.c.

207{
208 pthread_mutex_lock(&operation.operation_mutex);
210 pthread_mutex_unlock(&operation.operation_mutex);
211 return value;
212}
bool peer_discovery_hardcoded_seeds
Definition state.h:53

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_hardcoded_seeds.

Referenced by handle_peer_discovery().

◆ get_peer_discovery_in_progress()

bool get_peer_discovery_in_progress ( )

Get the peer discovery in progress state.

Returns
The peer discovery in progress state.

Definition at line 158 of file state.c.

159{
160 pthread_mutex_lock(&operation.operation_mutex);
162 pthread_mutex_unlock(&operation.operation_mutex);
163 return value;
164}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_in_progress.

Referenced by cli_peer_discovery(), and handle_peer_discovery().

◆ get_peer_discovery_succeeded()

bool get_peer_discovery_succeeded ( )

Get the peer discovery succeeded state.

Returns
The peer discovery succeeded state.

Definition at line 166 of file state.c.

167{
168 pthread_mutex_lock(&operation.operation_mutex);
170 pthread_mutex_unlock(&operation.operation_mutex);
171 return value;
172}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_succeeded.

Referenced by cli_peer_discovery(), and handle_peer_discovery().

◆ get_pid()

int get_pid ( )

Get the program PID.

Returns
The program PID.

Definition at line 252 of file state.c.

253{
254 return state.pid;
255}
pid_t pid
Definition state.h:28

References program_state::pid, and state.

◆ get_start_time()

time_t get_start_time ( )

Get the program start time.

Returns
The program start time.

Definition at line 257 of file state.c.

258{
259 return state.start_time;
260}

References program_state::start_time, and state.

◆ init_program_operation()

int init_program_operation ( )

Initialize the program operation.

Returns
0 if successful, otherwise 1.

Definition at line 100 of file state.c.

101{
102 pthread_mutex_init(&operation.operation_mutex, NULL);
104 return 0;
105}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery.

Referenced by run_bitlab().

◆ init_program_state()

void init_program_state ( )

Initialize the program state.

Definition at line 52 of file state.c.

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}

References BITLAB_LOG, program_state::exit_flag, program_state::exit_flag_mutex, log_message(), LOG_WARN, program_state::pid, program_state::start_time, and state.

Referenced by run_bitlab().

◆ mark_started_with_parameters()

void mark_started_with_parameters ( )

Mark the program as started with parameters.

Definition at line 88 of file state.c.

89{
90 pthread_mutex_lock(&state.exit_flag_mutex);
92 pthread_mutex_unlock(&state.exit_flag_mutex);
93}
bool started_with_parameters
Definition state.h:30

References program_state::exit_flag_mutex, program_state::started_with_parameters, and state.

Referenced by run_bitlab().

◆ print_program_state()

void print_program_state ( )

Print the program state.

Definition at line 63 of file state.c.

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}
int get_elapsed_time()
Get the elapsed time since the program started.
Definition state.c:262
void guarded_print_line(const char *format,...)
Guarded print line function.
Definition utils.c:65

References get_elapsed_time(), guarded_print_line(), program_state::pid, program_state::started_with_parameters, and state.

Referenced by cli_info().

◆ set_exit_flag()

void set_exit_flag ( volatile sig_atomic_t  flag)

Set the exit flag.

Parameters
flagThe flag to set.

Definition at line 73 of file state.c.

74{
75 pthread_mutex_lock(&state.exit_flag_mutex);
76 state.exit_flag = flag;
77 pthread_mutex_unlock(&state.exit_flag_mutex);
78}

References program_state::exit_flag, program_state::exit_flag_mutex, and state.

Referenced by cli_exit().

◆ set_peer_discovery()

bool set_peer_discovery ( bool  value)

Set the peer discovery operation.

This function cannot stop the peer discovery operation if it is in progress. Use force_stop_peer_discovery instead.

Parameters
valueThe value to set.
Returns
True if successful, otherwise false.

Definition at line 125 of file state.c.

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}

References BITLAB_LOG, log_message(), LOG_WARN, operation, program_operation::operation_mutex, program_operation::peer_discovery, and program_operation::peer_discovery_in_progress.

Referenced by cli_peer_discovery().

◆ set_peer_discovery_daemon()

bool set_peer_discovery_daemon ( bool  value)

Set the peer discovery daemon state.

Parameters
valueThe value to set.
Returns
True if successful, otherwise false.

Definition at line 174 of file state.c.

175{
176 pthread_mutex_lock(&operation.operation_mutex);
178 pthread_mutex_unlock(&operation.operation_mutex);
179 return true;
180}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_daemon.

Referenced by cli_peer_discovery().

◆ set_peer_discovery_dns_domain()

bool set_peer_discovery_dns_domain ( const char *  domain)

Set the peer discovery DNS domain.

Parameters
domainThe domain to set.
Returns
True if successful, otherwise false.

Definition at line 222 of file state.c.

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}
@ LOG_ERROR
Definition log.h:32

References BITLAB_LOG, LOG_ERROR, log_message(), operation, program_operation::operation_mutex, and program_operation::peer_discovery_dns_domain.

Referenced by cli_peer_discovery().

◆ set_peer_discovery_dns_lookup()

bool set_peer_discovery_dns_lookup ( bool  value)

Set the peer discovery DNS lookup state.

Parameters
valueThe value to set.
Returns
True if successful, otherwise false.

Definition at line 190 of file state.c.

191{
192 pthread_mutex_lock(&operation.operation_mutex);
194 pthread_mutex_unlock(&operation.operation_mutex);
195 return true;
196}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_dns_lookup.

Referenced by cli_peer_discovery().

◆ set_peer_discovery_hardcoded_seeds()

bool set_peer_discovery_hardcoded_seeds ( bool  value)

Set the peer discovery hardcoded seeds state.

Parameters
valueThe value to set.
Returns
True if successful, otherwise false.

Definition at line 182 of file state.c.

183{
184 pthread_mutex_lock(&operation.operation_mutex);
186 pthread_mutex_unlock(&operation.operation_mutex);
187 return true;
188}

References operation, program_operation::operation_mutex, and program_operation::peer_discovery_hardcoded_seeds.

Referenced by cli_peer_discovery().

◆ start_peer_discovery_progress()

void start_peer_discovery_progress ( )

Start the peer discovery progress.

This function only starts the progress if the peer discovery operation is set.

Definition at line 107 of file state.c.

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}

References BITLAB_LOG, log_message(), LOG_WARN, operation, program_operation::operation_mutex, program_operation::peer_discovery, and program_operation::peer_discovery_in_progress.

Referenced by handle_peer_discovery().

Variable Documentation

◆ operation

program_operation operation
Initial value:
=
{
false,
false,
false,
NULL,
PTHREAD_MUTEX_INITIALIZER
}
#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.

The program operation used to store all current operations of the program.

Only accessible in state.c directly, otherwise functions should be used.

Parameters
peer_discoveryThe peer discovery operation of the program.
peer_discovery_in_progressThe peer discovery operation in progress.
peer_discovery_succeededThe peer discovery operation succeeded.
peer_discovery_daemonThe peer discovery operation as a daemon.
peer_discovery_hardcoded_seedsThe peer discovery operation with hardcoded seeds.
peer_discovery_dns_lookupThe peer discovery operation with DNS lookup.
peer_discovery_dns_domainThe peer discovery DNS domain field for custom DNS lookup.
mutexThe mutex to protect the operation.

Definition at line 40 of file state.c.

41{
42 false,
43 false,
44 false,
48 NULL,
49 PTHREAD_MUTEX_INITIALIZER
50};

Referenced by cli_info(), destroy_program_operation(), finish_peer_discovery_progress(), force_stop_peer_discovery(), get_peer_discovery(), get_peer_discovery_daemon(), get_peer_discovery_dns_domain(), get_peer_discovery_dns_lookup(), get_peer_discovery_hardcoded_seeds(), get_peer_discovery_in_progress(), get_peer_discovery_succeeded(), init_program_operation(), run_bitlab(), set_peer_discovery(), set_peer_discovery_daemon(), set_peer_discovery_dns_domain(), set_peer_discovery_dns_lookup(), set_peer_discovery_hardcoded_seeds(), and start_peer_discovery_progress().

◆ state

Initial value:
=
{
0,
0,
0,
0,
PTHREAD_MUTEX_INITIALIZER
}

The program state structure used to store the state of the program.

The program state used to store the state of the program.

Only accessible in state.c directly, otherwise functions should be used.

Parameters
pidThe process ID of the program.
start_timeThe start time of the program.
started_with_parametersThe flag to indicate if the program started with CLI parameters.
exit_flagThe exit flag of the program.
exit_flag_mutexThe mutex to protect the exit flag.

Definition at line 19 of file state.c.

20{
21 0,
22 0,
23 0,
24 0,
25 PTHREAD_MUTEX_INITIALIZER
26};

Referenced by cli_command_generator(), destroy_program_state(), get_elapsed_time(), get_exit_flag(), get_pid(), get_start_time(), init_program_state(), mark_started_with_parameters(), print_program_state(), run_bitlab(), and set_exit_flag().