ncd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * @file ncd.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/version.h>
  34. #include <misc/loglevel.h>
  35. #include <misc/read_file.h>
  36. #include <misc/open_standard_streams.h>
  37. #include <misc/string_begins_with.h>
  38. #include <base/BLog.h>
  39. #include <system/BReactor.h>
  40. #include <system/BSignal.h>
  41. #include <system/BProcess.h>
  42. #include <udevmonitor/NCDUdevManager.h>
  43. #include <random/BRandom2.h>
  44. #include <ncd/NCDInterpreter.h>
  45. #ifdef BADVPN_USE_SYSLOG
  46. #include <base/BLog_syslog.h>
  47. #endif
  48. #include "ncd.h"
  49. #include <generated/blog_channel_ncd.h>
  50. #define LOGGER_STDOUT 1
  51. #define LOGGER_STDERR 2
  52. #define LOGGER_SYSLOG 3
  53. // command-line options
  54. static struct {
  55. int help;
  56. int version;
  57. int logger;
  58. #ifdef BADVPN_USE_SYSLOG
  59. char *logger_syslog_facility;
  60. char *logger_syslog_ident;
  61. #endif
  62. int loglevel;
  63. int loglevels[BLOG_NUM_CHANNELS];
  64. char *config_file;
  65. int retry_time;
  66. int no_udev;
  67. char **extra_args;
  68. int num_extra_args;
  69. } options;
  70. // reactor
  71. static BReactor reactor;
  72. // process manager
  73. static BProcessManager manager;
  74. // udev manager
  75. static NCDUdevManager umanager;
  76. // random number generator
  77. static BRandom2 random2;
  78. // interpreter
  79. static NCDInterpreter interpreter;
  80. // forward declarations of functions
  81. static void print_help (const char *name);
  82. static void print_version (void);
  83. static int parse_arguments (int argc, char *argv[]);
  84. static void signal_handler (void *unused);
  85. static void interpreter_handler_finished (void *user, int exit_code);
  86. int main (int argc, char **argv)
  87. {
  88. if (argc <= 0) {
  89. return 1;
  90. }
  91. int main_exit_code = 1;
  92. // open standard streams
  93. open_standard_streams();
  94. // parse command-line arguments
  95. if (!parse_arguments(argc, argv)) {
  96. fprintf(stderr, "Failed to parse arguments\n");
  97. print_help(argv[0]);
  98. goto fail0;
  99. }
  100. // handle --help and --version
  101. if (options.help) {
  102. print_version();
  103. print_help(argv[0]);
  104. return 0;
  105. }
  106. if (options.version) {
  107. print_version();
  108. return 0;
  109. }
  110. // initialize logger
  111. switch (options.logger) {
  112. case LOGGER_STDOUT:
  113. BLog_InitStdout();
  114. break;
  115. case LOGGER_STDERR:
  116. BLog_InitStderr();
  117. break;
  118. #ifdef BADVPN_USE_SYSLOG
  119. case LOGGER_SYSLOG:
  120. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  121. fprintf(stderr, "Failed to initialize syslog logger\n");
  122. goto fail0;
  123. }
  124. break;
  125. #endif
  126. default:
  127. ASSERT(0);
  128. }
  129. // configure logger channels
  130. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  131. if (options.loglevels[i] >= 0) {
  132. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  133. }
  134. else if (options.loglevel >= 0) {
  135. BLog_SetChannelLoglevel(i, options.loglevel);
  136. } else {
  137. BLog_SetChannelLoglevel(i, DEFAULT_LOGLEVEL);
  138. }
  139. }
  140. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  141. // initialize network
  142. if (!BNetwork_GlobalInit()) {
  143. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  144. goto fail1;
  145. }
  146. // init time
  147. BTime_Init();
  148. // init reactor
  149. if (!BReactor_Init(&reactor)) {
  150. BLog(BLOG_ERROR, "BReactor_Init failed");
  151. goto fail1;
  152. }
  153. // init process manager
  154. if (!BProcessManager_Init(&manager, &reactor)) {
  155. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  156. goto fail2;
  157. }
  158. // init udev manager
  159. NCDUdevManager_Init(&umanager, options.no_udev, &reactor, &manager);
  160. // init random number generator
  161. if (!BRandom2_Init(&random2, BRANDOM2_INIT_LAZY)) {
  162. BLog(BLOG_ERROR, "BRandom2_Init failed");
  163. goto fail3;
  164. }
  165. // setup signal handler
  166. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  167. BLog(BLOG_ERROR, "BSignal_Init failed");
  168. goto fail4;
  169. }
  170. // read config file
  171. uint8_t *file;
  172. size_t file_len;
  173. if (!read_file(options.config_file, &file, &file_len)) {
  174. BLog(BLOG_ERROR, "failed to read config file");
  175. goto fail5;
  176. }
  177. // setup interpreter parameters
  178. struct NCDInterpreter_params params;
  179. params.handler_finished = interpreter_handler_finished;
  180. params.user = NULL;
  181. params.retry_time = options.retry_time;
  182. params.extra_args = options.extra_args;
  183. params.num_extra_args = options.num_extra_args;
  184. params.reactor = &reactor;
  185. params.manager = &manager;
  186. params.umanager = &umanager;
  187. params.random2 = &random2;
  188. // initialize interpreter
  189. int res = NCDInterpreter_Init(&interpreter, (const char *)file, file_len, params);
  190. free(file);
  191. if (!res) {
  192. goto fail5;
  193. }
  194. BLog(BLOG_NOTICE, "entering event loop");
  195. // enter event loop
  196. main_exit_code = BReactor_Exec(&reactor);
  197. // free interpreter
  198. NCDInterpreter_Free(&interpreter);
  199. fail5:
  200. // remove signal handler
  201. BSignal_Finish();
  202. fail4:
  203. // free random number generator
  204. BRandom2_Free(&random2);
  205. fail3:
  206. // free udev manager
  207. NCDUdevManager_Free(&umanager);
  208. // free process manager
  209. BProcessManager_Free(&manager);
  210. fail2:
  211. // free reactor
  212. BReactor_Free(&reactor);
  213. fail1:
  214. // free logger
  215. BLog(BLOG_NOTICE, "exiting");
  216. BLog_Free();
  217. fail0:
  218. // finish objects
  219. DebugObjectGlobal_Finish();
  220. return main_exit_code;
  221. }
  222. void print_help (const char *name)
  223. {
  224. printf(
  225. "Usage:\n"
  226. " %s\n"
  227. " [--help]\n"
  228. " [--version]\n"
  229. " [--logger <stdout/stderr/syslog>]\n"
  230. " (logger=syslog?\n"
  231. " [--syslog-facility <string>]\n"
  232. " [--syslog-ident <string>]\n"
  233. " )\n"
  234. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  235. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  236. " [--retry-time <ms>]\n"
  237. " [--no-udev]\n"
  238. " [--config-file <ncd_program_file>]\n"
  239. " [-- program_args...]\n"
  240. " [<ncd_program_file> program_args...]\n" ,
  241. name
  242. );
  243. }
  244. void print_version (void)
  245. {
  246. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  247. }
  248. int parse_arguments (int argc, char *argv[])
  249. {
  250. if (argc <= 0) {
  251. return 0;
  252. }
  253. options.help = 0;
  254. options.version = 0;
  255. options.logger = LOGGER_STDERR;
  256. #ifdef BADVPN_USE_SYSLOG
  257. options.logger_syslog_facility = "daemon";
  258. options.logger_syslog_ident = argv[0];
  259. #endif
  260. options.loglevel = -1;
  261. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  262. options.loglevels[i] = -1;
  263. }
  264. options.config_file = NULL;
  265. options.retry_time = DEFAULT_RETRY_TIME;
  266. options.no_udev = 0;
  267. options.extra_args = NULL;
  268. options.num_extra_args = 0;
  269. for (int i = 1; i < argc; i++) {
  270. char *arg = argv[i];
  271. if (!strcmp(arg, "--help")) {
  272. options.help = 1;
  273. }
  274. else if (!strcmp(arg, "--version")) {
  275. options.version = 1;
  276. }
  277. else if (!strcmp(arg, "--logger")) {
  278. if (1 >= argc - i) {
  279. fprintf(stderr, "%s: requires an argument\n", arg);
  280. return 0;
  281. }
  282. char *arg2 = argv[i + 1];
  283. if (!strcmp(arg2, "stdout")) {
  284. options.logger = LOGGER_STDOUT;
  285. }
  286. else if (!strcmp(arg2, "stderr")) {
  287. options.logger = LOGGER_STDERR;
  288. }
  289. #ifdef BADVPN_USE_SYSLOG
  290. else if (!strcmp(arg2, "syslog")) {
  291. options.logger = LOGGER_SYSLOG;
  292. }
  293. #endif
  294. else {
  295. fprintf(stderr, "%s: wrong argument\n", arg);
  296. return 0;
  297. }
  298. i++;
  299. }
  300. #ifdef BADVPN_USE_SYSLOG
  301. else if (!strcmp(arg, "--syslog-facility")) {
  302. if (1 >= argc - i) {
  303. fprintf(stderr, "%s: requires an argument\n", arg);
  304. return 0;
  305. }
  306. options.logger_syslog_facility = argv[i + 1];
  307. i++;
  308. }
  309. else if (!strcmp(arg, "--syslog-ident")) {
  310. if (1 >= argc - i) {
  311. fprintf(stderr, "%s: requires an argument\n", arg);
  312. return 0;
  313. }
  314. options.logger_syslog_ident = argv[i + 1];
  315. i++;
  316. }
  317. #endif
  318. else if (!strcmp(arg, "--loglevel")) {
  319. if (1 >= argc - i) {
  320. fprintf(stderr, "%s: requires an argument\n", arg);
  321. return 0;
  322. }
  323. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  324. fprintf(stderr, "%s: wrong argument\n", arg);
  325. return 0;
  326. }
  327. i++;
  328. }
  329. else if (!strcmp(arg, "--channel-loglevel")) {
  330. if (2 >= argc - i) {
  331. fprintf(stderr, "%s: requires two arguments\n", arg);
  332. return 0;
  333. }
  334. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  335. if (channel < 0) {
  336. fprintf(stderr, "%s: wrong channel argument\n", arg);
  337. return 0;
  338. }
  339. int loglevel = parse_loglevel(argv[i + 2]);
  340. if (loglevel < 0) {
  341. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  342. return 0;
  343. }
  344. options.loglevels[channel] = loglevel;
  345. i += 2;
  346. }
  347. else if (!strcmp(arg, "--config-file")) {
  348. if (1 >= argc - i) {
  349. fprintf(stderr, "%s: requires an argument\n", arg);
  350. return 0;
  351. }
  352. options.config_file = argv[i + 1];
  353. i++;
  354. }
  355. else if (!strcmp(arg, "--retry-time")) {
  356. if (1 >= argc - i) {
  357. fprintf(stderr, "%s: requires an argument\n", arg);
  358. return 0;
  359. }
  360. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  361. fprintf(stderr, "%s: wrong argument\n", arg);
  362. return 0;
  363. }
  364. i++;
  365. }
  366. else if (!strcmp(arg, "--no-udev")) {
  367. options.no_udev = 1;
  368. }
  369. else if (!strcmp(arg, "--")) {
  370. options.extra_args = &argv[i + 1];
  371. options.num_extra_args = argc - i - 1;
  372. i += options.num_extra_args;
  373. }
  374. else if (!string_begins_with(arg, "--")) {
  375. if (options.config_file) {
  376. fprintf(stderr, "%s: program is already specified (did you mean to use -- ?)\n", arg);
  377. return 0;
  378. }
  379. options.config_file = argv[i];
  380. options.extra_args = &argv[i + 1];
  381. options.num_extra_args = argc - i - 1;
  382. i += options.num_extra_args;
  383. }
  384. else {
  385. fprintf(stderr, "unknown option: %s\n", arg);
  386. return 0;
  387. }
  388. }
  389. if (options.help || options.version) {
  390. return 1;
  391. }
  392. if (!options.config_file) {
  393. fprintf(stderr, "No program is specified.\n");
  394. return 0;
  395. }
  396. return 1;
  397. }
  398. void signal_handler (void *unused)
  399. {
  400. BLog(BLOG_NOTICE, "termination requested");
  401. NCDInterpreter_RequestShutdown(&interpreter, 1);
  402. }
  403. void interpreter_handler_finished (void *user, int exit_code)
  404. {
  405. BReactor_Quit(&reactor, exit_code);
  406. }