ncd.c 13 KB

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