ncd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/open_standard_streams.h>
  36. #include <misc/string_begins_with.h>
  37. #include <base/BLog.h>
  38. #include <system/BReactor.h>
  39. #include <system/BSignal.h>
  40. #include <system/BProcess.h>
  41. #include <udevmonitor/NCDUdevManager.h>
  42. #include <random/BRandom2.h>
  43. #include <ncd/NCDInterpreter.h>
  44. #include <ncd/NCDBuildProgram.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 syntax_only;
  66. int retry_time;
  67. int signal_exit_code;
  68. int no_udev;
  69. char **extra_args;
  70. int num_extra_args;
  71. } options;
  72. // reactor
  73. static BReactor reactor;
  74. // process manager
  75. static BProcessManager manager;
  76. // udev manager
  77. static NCDUdevManager umanager;
  78. // random number generator
  79. static BRandom2 random2;
  80. // interpreter
  81. static NCDInterpreter interpreter;
  82. // forward declarations of functions
  83. static void print_help (const char *name);
  84. static void print_version (void);
  85. static int parse_arguments (int argc, char *argv[]);
  86. static void signal_handler (void *unused);
  87. static void interpreter_handler_finished (void *user, int exit_code);
  88. int main (int argc, char **argv)
  89. {
  90. if (argc <= 0) {
  91. return 1;
  92. }
  93. int main_exit_code = 1;
  94. // open standard streams
  95. open_standard_streams();
  96. // parse command-line arguments
  97. if (!parse_arguments(argc, argv)) {
  98. fprintf(stderr, "Failed to parse arguments\n");
  99. print_help(argv[0]);
  100. goto fail0;
  101. }
  102. // handle --help and --version
  103. if (options.help) {
  104. print_version();
  105. print_help(argv[0]);
  106. return 0;
  107. }
  108. if (options.version) {
  109. print_version();
  110. return 0;
  111. }
  112. // initialize logger
  113. switch (options.logger) {
  114. case LOGGER_STDOUT:
  115. BLog_InitStdout();
  116. break;
  117. case LOGGER_STDERR:
  118. BLog_InitStderr();
  119. break;
  120. #ifdef BADVPN_USE_SYSLOG
  121. case LOGGER_SYSLOG:
  122. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  123. fprintf(stderr, "Failed to initialize syslog logger\n");
  124. goto fail0;
  125. }
  126. break;
  127. #endif
  128. default:
  129. ASSERT(0);
  130. }
  131. // configure logger channels
  132. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  133. if (options.loglevels[i] >= 0) {
  134. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  135. }
  136. else if (options.loglevel >= 0) {
  137. BLog_SetChannelLoglevel(i, options.loglevel);
  138. } else {
  139. BLog_SetChannelLoglevel(i, DEFAULT_LOGLEVEL);
  140. }
  141. }
  142. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  143. // initialize network
  144. if (!BNetwork_GlobalInit()) {
  145. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  146. goto fail1;
  147. }
  148. // init time
  149. BTime_Init();
  150. // init reactor
  151. if (!BReactor_Init(&reactor)) {
  152. BLog(BLOG_ERROR, "BReactor_Init failed");
  153. goto fail1;
  154. }
  155. // init process manager
  156. if (!BProcessManager_Init(&manager, &reactor)) {
  157. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  158. goto fail2;
  159. }
  160. // init udev manager
  161. NCDUdevManager_Init(&umanager, options.no_udev, &reactor, &manager);
  162. // init random number generator
  163. if (!BRandom2_Init(&random2, BRANDOM2_INIT_LAZY)) {
  164. BLog(BLOG_ERROR, "BRandom2_Init failed");
  165. goto fail3;
  166. }
  167. // setup signal handler
  168. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  169. BLog(BLOG_ERROR, "BSignal_Init failed");
  170. goto fail4;
  171. }
  172. // build program
  173. NCDProgram program;
  174. if (!NCDBuildProgram_Build(options.config_file, &program)) {
  175. BLog(BLOG_ERROR, "failed to build program");
  176. goto fail5;
  177. }
  178. // setup interpreter parameters
  179. struct NCDInterpreter_params params;
  180. params.handler_finished = interpreter_handler_finished;
  181. params.user = NULL;
  182. params.retry_time = options.retry_time;
  183. params.extra_args = options.extra_args;
  184. params.num_extra_args = options.num_extra_args;
  185. params.reactor = &reactor;
  186. params.manager = &manager;
  187. params.umanager = &umanager;
  188. params.random2 = &random2;
  189. // initialize interpreter
  190. if (!NCDInterpreter_Init(&interpreter, program, params)) {
  191. goto fail5;
  192. }
  193. // don't enter event loop if syntax check is requested
  194. if (options.syntax_only) {
  195. main_exit_code = 0;
  196. goto fail6;
  197. }
  198. BLog(BLOG_NOTICE, "entering event loop");
  199. // enter event loop
  200. main_exit_code = BReactor_Exec(&reactor);
  201. fail6:
  202. // free interpreter
  203. NCDInterpreter_Free(&interpreter);
  204. fail5:
  205. // remove signal handler
  206. BSignal_Finish();
  207. fail4:
  208. // free random number generator
  209. BRandom2_Free(&random2);
  210. fail3:
  211. // free udev manager
  212. NCDUdevManager_Free(&umanager);
  213. // free process manager
  214. BProcessManager_Free(&manager);
  215. fail2:
  216. // free reactor
  217. BReactor_Free(&reactor);
  218. fail1:
  219. // free logger
  220. BLog(BLOG_NOTICE, "exiting");
  221. BLog_Free();
  222. fail0:
  223. // finish objects
  224. DebugObjectGlobal_Finish();
  225. return main_exit_code;
  226. }
  227. void print_help (const char *name)
  228. {
  229. printf(
  230. "Usage:\n"
  231. " %s\n"
  232. " [--help]\n"
  233. " [--version]\n"
  234. " [--logger <stdout/stderr/syslog>]\n"
  235. " (logger=syslog?\n"
  236. " [--syslog-facility <string>]\n"
  237. " [--syslog-ident <string>]\n"
  238. " )\n"
  239. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  240. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  241. " [--retry-time <ms>]\n"
  242. " [--no-udev]\n"
  243. " [--config-file <ncd_program_file>]\n"
  244. " [--syntax-only]\n"
  245. " [--signal-exit-code <number>]\n"
  246. " [-- program_args...]\n"
  247. " [<ncd_program_file> program_args...]\n" ,
  248. name
  249. );
  250. }
  251. void print_version (void)
  252. {
  253. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  254. }
  255. int parse_arguments (int argc, char *argv[])
  256. {
  257. if (argc <= 0) {
  258. return 0;
  259. }
  260. options.help = 0;
  261. options.version = 0;
  262. options.logger = LOGGER_STDERR;
  263. #ifdef BADVPN_USE_SYSLOG
  264. options.logger_syslog_facility = "daemon";
  265. options.logger_syslog_ident = argv[0];
  266. #endif
  267. options.loglevel = -1;
  268. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  269. options.loglevels[i] = -1;
  270. }
  271. options.config_file = NULL;
  272. options.syntax_only = 0;
  273. options.retry_time = DEFAULT_RETRY_TIME;
  274. options.signal_exit_code = DEFAULT_SIGNAL_EXIT_CODE;
  275. options.no_udev = 0;
  276. options.extra_args = NULL;
  277. options.num_extra_args = 0;
  278. for (int i = 1; i < argc; i++) {
  279. char *arg = argv[i];
  280. if (!strcmp(arg, "--help")) {
  281. options.help = 1;
  282. }
  283. else if (!strcmp(arg, "--version")) {
  284. options.version = 1;
  285. }
  286. else if (!strcmp(arg, "--logger")) {
  287. if (1 >= argc - i) {
  288. fprintf(stderr, "%s: requires an argument\n", arg);
  289. return 0;
  290. }
  291. char *arg2 = argv[i + 1];
  292. if (!strcmp(arg2, "stdout")) {
  293. options.logger = LOGGER_STDOUT;
  294. }
  295. else if (!strcmp(arg2, "stderr")) {
  296. options.logger = LOGGER_STDERR;
  297. }
  298. #ifdef BADVPN_USE_SYSLOG
  299. else if (!strcmp(arg2, "syslog")) {
  300. options.logger = LOGGER_SYSLOG;
  301. }
  302. #endif
  303. else {
  304. fprintf(stderr, "%s: wrong argument\n", arg);
  305. return 0;
  306. }
  307. i++;
  308. }
  309. #ifdef BADVPN_USE_SYSLOG
  310. else if (!strcmp(arg, "--syslog-facility")) {
  311. if (1 >= argc - i) {
  312. fprintf(stderr, "%s: requires an argument\n", arg);
  313. return 0;
  314. }
  315. options.logger_syslog_facility = argv[i + 1];
  316. i++;
  317. }
  318. else if (!strcmp(arg, "--syslog-ident")) {
  319. if (1 >= argc - i) {
  320. fprintf(stderr, "%s: requires an argument\n", arg);
  321. return 0;
  322. }
  323. options.logger_syslog_ident = argv[i + 1];
  324. i++;
  325. }
  326. #endif
  327. else if (!strcmp(arg, "--loglevel")) {
  328. if (1 >= argc - i) {
  329. fprintf(stderr, "%s: requires an argument\n", arg);
  330. return 0;
  331. }
  332. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  333. fprintf(stderr, "%s: wrong argument\n", arg);
  334. return 0;
  335. }
  336. i++;
  337. }
  338. else if (!strcmp(arg, "--channel-loglevel")) {
  339. if (2 >= argc - i) {
  340. fprintf(stderr, "%s: requires two arguments\n", arg);
  341. return 0;
  342. }
  343. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  344. if (channel < 0) {
  345. fprintf(stderr, "%s: wrong channel argument\n", arg);
  346. return 0;
  347. }
  348. int loglevel = parse_loglevel(argv[i + 2]);
  349. if (loglevel < 0) {
  350. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  351. return 0;
  352. }
  353. options.loglevels[channel] = loglevel;
  354. i += 2;
  355. }
  356. else if (!strcmp(arg, "--config-file")) {
  357. if (1 >= argc - i) {
  358. fprintf(stderr, "%s: requires an argument\n", arg);
  359. return 0;
  360. }
  361. options.config_file = argv[i + 1];
  362. i++;
  363. }
  364. else if (!strcmp(arg, "--syntax-only")) {
  365. options.syntax_only = 1;
  366. }
  367. else if (!strcmp(arg, "--retry-time")) {
  368. if (1 >= argc - i) {
  369. fprintf(stderr, "%s: requires an argument\n", arg);
  370. return 0;
  371. }
  372. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  373. fprintf(stderr, "%s: wrong argument\n", arg);
  374. return 0;
  375. }
  376. i++;
  377. }
  378. else if (!strcmp(arg, "--signal-exit-code")) {
  379. if (1 >= argc - i) {
  380. fprintf(stderr, "%s: requires an argument\n", arg);
  381. return 0;
  382. }
  383. if ((options.signal_exit_code = atoi(argv[i + 1])) < 0) {
  384. fprintf(stderr, "%s: wrong argument\n", arg);
  385. return 0;
  386. }
  387. i++;
  388. }
  389. else if (!strcmp(arg, "--no-udev")) {
  390. options.no_udev = 1;
  391. }
  392. else if (!strcmp(arg, "--")) {
  393. options.extra_args = &argv[i + 1];
  394. options.num_extra_args = argc - i - 1;
  395. i += options.num_extra_args;
  396. }
  397. else if (!string_begins_with(arg, "--")) {
  398. if (options.config_file) {
  399. fprintf(stderr, "%s: program is already specified (did you mean to use -- ?)\n", arg);
  400. return 0;
  401. }
  402. options.config_file = argv[i];
  403. options.extra_args = &argv[i + 1];
  404. options.num_extra_args = argc - i - 1;
  405. i += options.num_extra_args;
  406. }
  407. else {
  408. fprintf(stderr, "unknown option: %s\n", arg);
  409. return 0;
  410. }
  411. }
  412. if (options.help || options.version) {
  413. return 1;
  414. }
  415. if (!options.config_file) {
  416. fprintf(stderr, "No program is specified.\n");
  417. return 0;
  418. }
  419. return 1;
  420. }
  421. void signal_handler (void *unused)
  422. {
  423. BLog(BLOG_NOTICE, "termination requested");
  424. NCDInterpreter_RequestShutdown(&interpreter, options.signal_exit_code);
  425. }
  426. void interpreter_handler_finished (void *user, int exit_code)
  427. {
  428. BReactor_Quit(&reactor, exit_code);
  429. }