ncd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 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. // build program
  171. NCDProgram program;
  172. if (!NCDBuildProgram_Build(options.config_file, &program)) {
  173. BLog(BLOG_ERROR, "failed to build program");
  174. goto fail5;
  175. }
  176. // setup interpreter parameters
  177. struct NCDInterpreter_params params;
  178. params.handler_finished = interpreter_handler_finished;
  179. params.user = NULL;
  180. params.retry_time = options.retry_time;
  181. params.extra_args = options.extra_args;
  182. params.num_extra_args = options.num_extra_args;
  183. params.reactor = &reactor;
  184. params.manager = &manager;
  185. params.umanager = &umanager;
  186. params.random2 = &random2;
  187. // initialize interpreter
  188. if (!NCDInterpreter_Init(&interpreter, program, params)) {
  189. goto fail5;
  190. }
  191. BLog(BLOG_NOTICE, "entering event loop");
  192. // enter event loop
  193. main_exit_code = BReactor_Exec(&reactor);
  194. // free interpreter
  195. NCDInterpreter_Free(&interpreter);
  196. fail5:
  197. // remove signal handler
  198. BSignal_Finish();
  199. fail4:
  200. // free random number generator
  201. BRandom2_Free(&random2);
  202. fail3:
  203. // free udev manager
  204. NCDUdevManager_Free(&umanager);
  205. // free process manager
  206. BProcessManager_Free(&manager);
  207. fail2:
  208. // free reactor
  209. BReactor_Free(&reactor);
  210. fail1:
  211. // free logger
  212. BLog(BLOG_NOTICE, "exiting");
  213. BLog_Free();
  214. fail0:
  215. // finish objects
  216. DebugObjectGlobal_Finish();
  217. return main_exit_code;
  218. }
  219. void print_help (const char *name)
  220. {
  221. printf(
  222. "Usage:\n"
  223. " %s\n"
  224. " [--help]\n"
  225. " [--version]\n"
  226. " [--logger <stdout/stderr/syslog>]\n"
  227. " (logger=syslog?\n"
  228. " [--syslog-facility <string>]\n"
  229. " [--syslog-ident <string>]\n"
  230. " )\n"
  231. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  232. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  233. " [--retry-time <ms>]\n"
  234. " [--no-udev]\n"
  235. " [--config-file <ncd_program_file>]\n"
  236. " [-- program_args...]\n"
  237. " [<ncd_program_file> program_args...]\n" ,
  238. name
  239. );
  240. }
  241. void print_version (void)
  242. {
  243. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  244. }
  245. int parse_arguments (int argc, char *argv[])
  246. {
  247. if (argc <= 0) {
  248. return 0;
  249. }
  250. options.help = 0;
  251. options.version = 0;
  252. options.logger = LOGGER_STDERR;
  253. #ifdef BADVPN_USE_SYSLOG
  254. options.logger_syslog_facility = "daemon";
  255. options.logger_syslog_ident = argv[0];
  256. #endif
  257. options.loglevel = -1;
  258. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  259. options.loglevels[i] = -1;
  260. }
  261. options.config_file = NULL;
  262. options.retry_time = DEFAULT_RETRY_TIME;
  263. options.no_udev = 0;
  264. options.extra_args = NULL;
  265. options.num_extra_args = 0;
  266. for (int i = 1; i < argc; i++) {
  267. char *arg = argv[i];
  268. if (!strcmp(arg, "--help")) {
  269. options.help = 1;
  270. }
  271. else if (!strcmp(arg, "--version")) {
  272. options.version = 1;
  273. }
  274. else if (!strcmp(arg, "--logger")) {
  275. if (1 >= argc - i) {
  276. fprintf(stderr, "%s: requires an argument\n", arg);
  277. return 0;
  278. }
  279. char *arg2 = argv[i + 1];
  280. if (!strcmp(arg2, "stdout")) {
  281. options.logger = LOGGER_STDOUT;
  282. }
  283. else if (!strcmp(arg2, "stderr")) {
  284. options.logger = LOGGER_STDERR;
  285. }
  286. #ifdef BADVPN_USE_SYSLOG
  287. else if (!strcmp(arg2, "syslog")) {
  288. options.logger = LOGGER_SYSLOG;
  289. }
  290. #endif
  291. else {
  292. fprintf(stderr, "%s: wrong argument\n", arg);
  293. return 0;
  294. }
  295. i++;
  296. }
  297. #ifdef BADVPN_USE_SYSLOG
  298. else if (!strcmp(arg, "--syslog-facility")) {
  299. if (1 >= argc - i) {
  300. fprintf(stderr, "%s: requires an argument\n", arg);
  301. return 0;
  302. }
  303. options.logger_syslog_facility = argv[i + 1];
  304. i++;
  305. }
  306. else if (!strcmp(arg, "--syslog-ident")) {
  307. if (1 >= argc - i) {
  308. fprintf(stderr, "%s: requires an argument\n", arg);
  309. return 0;
  310. }
  311. options.logger_syslog_ident = argv[i + 1];
  312. i++;
  313. }
  314. #endif
  315. else if (!strcmp(arg, "--loglevel")) {
  316. if (1 >= argc - i) {
  317. fprintf(stderr, "%s: requires an argument\n", arg);
  318. return 0;
  319. }
  320. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  321. fprintf(stderr, "%s: wrong argument\n", arg);
  322. return 0;
  323. }
  324. i++;
  325. }
  326. else if (!strcmp(arg, "--channel-loglevel")) {
  327. if (2 >= argc - i) {
  328. fprintf(stderr, "%s: requires two arguments\n", arg);
  329. return 0;
  330. }
  331. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  332. if (channel < 0) {
  333. fprintf(stderr, "%s: wrong channel argument\n", arg);
  334. return 0;
  335. }
  336. int loglevel = parse_loglevel(argv[i + 2]);
  337. if (loglevel < 0) {
  338. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  339. return 0;
  340. }
  341. options.loglevels[channel] = loglevel;
  342. i += 2;
  343. }
  344. else if (!strcmp(arg, "--config-file")) {
  345. if (1 >= argc - i) {
  346. fprintf(stderr, "%s: requires an argument\n", arg);
  347. return 0;
  348. }
  349. options.config_file = argv[i + 1];
  350. i++;
  351. }
  352. else if (!strcmp(arg, "--retry-time")) {
  353. if (1 >= argc - i) {
  354. fprintf(stderr, "%s: requires an argument\n", arg);
  355. return 0;
  356. }
  357. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  358. fprintf(stderr, "%s: wrong argument\n", arg);
  359. return 0;
  360. }
  361. i++;
  362. }
  363. else if (!strcmp(arg, "--no-udev")) {
  364. options.no_udev = 1;
  365. }
  366. else if (!strcmp(arg, "--")) {
  367. options.extra_args = &argv[i + 1];
  368. options.num_extra_args = argc - i - 1;
  369. i += options.num_extra_args;
  370. }
  371. else if (!string_begins_with(arg, "--")) {
  372. if (options.config_file) {
  373. fprintf(stderr, "%s: program is already specified (did you mean to use -- ?)\n", arg);
  374. return 0;
  375. }
  376. options.config_file = argv[i];
  377. options.extra_args = &argv[i + 1];
  378. options.num_extra_args = argc - i - 1;
  379. i += options.num_extra_args;
  380. }
  381. else {
  382. fprintf(stderr, "unknown option: %s\n", arg);
  383. return 0;
  384. }
  385. }
  386. if (options.help || options.version) {
  387. return 1;
  388. }
  389. if (!options.config_file) {
  390. fprintf(stderr, "No program is specified.\n");
  391. return 0;
  392. }
  393. return 1;
  394. }
  395. void signal_handler (void *unused)
  396. {
  397. BLog(BLOG_NOTICE, "termination requested");
  398. NCDInterpreter_RequestShutdown(&interpreter, 1);
  399. }
  400. void interpreter_handler_finished (void *user, int exit_code)
  401. {
  402. BReactor_Quit(&reactor, exit_code);
  403. }