summaryrefslogtreecommitdiff
path: root/other/bootstrap_daemon/src/tox-bootstrapd.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/bootstrap_daemon/src/tox-bootstrapd.c')
-rw-r--r--other/bootstrap_daemon/src/tox-bootstrapd.c114
1 files changed, 1 insertions, 113 deletions
diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c
index d4191e80..8ffef009 100644
--- a/other/bootstrap_daemon/src/tox-bootstrapd.c
+++ b/other/bootstrap_daemon/src/tox-bootstrapd.c
@@ -49,6 +49,7 @@
49#include "../bootstrap_node_packets.h" 49#include "../bootstrap_node_packets.h"
50#include "../../testing/misc_tools.c" 50#include "../../testing/misc_tools.c"
51 51
52#include "command_line_arguments.h"
52#include "config.h" 53#include "config.h"
53#include "global.h" 54#include "global.h"
54#include "log.h" 55#include "log.h"
@@ -122,119 +123,6 @@ void print_public_key(const uint8_t *public_key)
122 return; 123 return;
123} 124}
124 125
125// Prints --help message
126
127bool print_help()
128{
129 // 2 space ident
130 // make sure all lines fit into 80 columns
131 // make sure options are listed in alphabetical order
132 write_log(LOG_LEVEL_INFO,
133 "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
134 "\n"
135 "Options:\n"
136 " --config=FILE_PATH Specify path to the config file.\n"
137 " This is a required option.\n"
138 " Set FILE_PATH to a path to an empty file in order to\n"
139 " use default settings.\n"
140 " --foreground Run the daemon in foreground. The daemon won't fork\n"
141 " (detach from the terminal) and won't use the PID file.\n"
142 " --help Print this help message.\n"
143 " --log-backend=BACKEND Specify which logging backend to use.\n"
144 " Valid BACKEND values (case sensetive):\n"
145 " syslog Writes log messages to syslog.\n"
146 " Default option when no --log-backend is\n"
147 " specified.\n"
148 " stdout Writes log messages to stdout/stderr.\n"
149 " --version Print version information.\n");
150}
151
152// Handels command line arguments, setting cfg_file_path and log_backend.
153// Terminates the application if incorrect arguments are specified.
154
155void handle_command_line_arguments(int argc, char *argv[], char **cfg_file_path, LOG_BACKEND *log_backend, bool *run_in_foreground)
156{
157 if (argc < 2) {
158 write_log(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n");
159 print_help();
160 exit(1);
161 }
162
163 opterr = 0;
164
165 static struct option long_options[] = {
166 {"help", no_argument, 0, 'h'},
167 {"config", required_argument, 0, 'c'}, // required option
168 {"foreground", no_argument, 0, 'f'},
169 {"log-backend", required_argument, 0, 'l'}, // optional, defaults to syslog
170 {"version", no_argument, 0, 'v'},
171 {0, 0, 0, 0 }
172 };
173
174 bool cfg_file_path_set = false;
175 bool log_backend_set = false;
176
177 *run_in_foreground = false;
178
179 int opt;
180
181 while ((opt = getopt_long(argc, argv, ":", long_options, NULL)) != -1) {
182
183 switch (opt) {
184 case 'h':
185 print_help();
186 exit(0);
187
188 case 'c':
189 *cfg_file_path = optarg;
190 cfg_file_path_set = true;
191 break;
192
193 case 'f':
194 *run_in_foreground = true;
195 break;
196
197 case 'l':
198 if (strcmp(optarg, "syslog") == 0) {
199 *log_backend = LOG_BACKEND_SYSLOG;
200 log_backend_set = true;
201 } else if (strcmp(optarg, "stdout") == 0) {
202 *log_backend = LOG_BACKEND_STDOUT;
203 log_backend_set = true;
204 } else {
205 write_log(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg);
206 print_help();
207 exit(1);
208 }
209 break;
210
211 case 'v':
212 write_log(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER);
213 exit(0);
214
215 case '?':
216 write_log(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind-1]);
217 print_help();
218 exit(1);
219
220 case ':':
221 write_log(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind-1]);
222 print_help();
223 exit(1);
224 }
225 }
226
227 if (!log_backend_set) {
228 *log_backend = LOG_BACKEND_SYSLOG;
229 }
230
231 if (!cfg_file_path_set) {
232 write_log(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n");
233 print_help();
234 exit(1);
235 }
236}
237
238// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend 126// Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend
239// Terminates the application if the daemonization fails. 127// Terminates the application if the daemonization fails.
240 128