ncd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * @file ncd.c
  3. * @author Ambroz Bizjak <[email protected]>
  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/balloc.h>
  37. #include <misc/open_standard_streams.h>
  38. #include <misc/string_begins_with.h>
  39. #include <base/BLog.h>
  40. #include <system/BReactor.h>
  41. #include <system/BSignal.h>
  42. #include <system/BProcess.h>
  43. #include <udevmonitor/NCDUdevManager.h>
  44. #include <random/BRandom2.h>
  45. #include <ncd/NCDInterpreter.h>
  46. #ifdef BADVPN_USE_SYSLOG
  47. #include <base/BLog_syslog.h>
  48. #endif
  49. #include "ncd.h"
  50. #include <generated/blog_channel_ncd.h>
  51. #define LOGGER_STDOUT 1
  52. #define LOGGER_STDERR 2
  53. #define LOGGER_SYSLOG 3
  54. // command-line options
  55. static struct {
  56. int help;
  57. int version;
  58. int logger;
  59. #ifdef BADVPN_USE_SYSLOG
  60. char *logger_syslog_facility;
  61. char *logger_syslog_ident;
  62. #endif
  63. int loglevel;
  64. int loglevels[BLOG_NUM_CHANNELS];
  65. char *config_file;
  66. int retry_time;
  67. int no_udev;
  68. char **extra_args;
  69. int num_extra_args;
  70. } options;
  71. // exit code of main
  72. int main_exit_code;
  73. // reactor
  74. static BReactor reactor;
  75. // process manager
  76. static BProcessManager manager;
  77. // udev manager
  78. static NCDUdevManager umanager;
  79. // random number generator
  80. static BRandom2 random2;
  81. // interpreter
  82. static NCDInterpreter interpreter;
  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. 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 fail1a;
  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 fail1aa;
  166. }
  167. // setup signal handler
  168. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  169. BLog(BLOG_ERROR, "BSignal_Init failed");
  170. goto fail2;
  171. }
  172. // read config file
  173. uint8_t *file;
  174. size_t file_len;
  175. if (!read_file(options.config_file, &file, &file_len)) {
  176. BLog(BLOG_ERROR, "failed to read config file");
  177. goto fail3;
  178. }
  179. // setup interpreter parameters
  180. struct NCDInterpreter_params params;
  181. params.handler_finished = interpreter_handler_finished;
  182. params.user = NULL;
  183. params.retry_time = options.retry_time;
  184. params.extra_args = options.extra_args;
  185. params.num_extra_args = options.num_extra_args;
  186. params.reactor = &reactor;
  187. params.manager = &manager;
  188. params.umanager = &umanager;
  189. params.random2 = &random2;
  190. // initialize interpreter
  191. if (!NCDInterpreter_Init(&interpreter, (const char *)file, file_len, params)) {
  192. free(file);
  193. goto fail3;
  194. }
  195. // fee config file memory
  196. free(file);
  197. // enter event loop
  198. BLog(BLOG_NOTICE, "entering event loop");
  199. BReactor_Exec(&reactor);
  200. // free interpreter
  201. NCDInterpreter_Free(&interpreter);
  202. fail3:
  203. // remove signal handler
  204. BSignal_Finish();
  205. fail2:
  206. // free random number generator
  207. BRandom2_Free(&random2);
  208. fail1aa:
  209. // free udev manager
  210. NCDUdevManager_Free(&umanager);
  211. // free process manager
  212. BProcessManager_Free(&manager);
  213. fail1a:
  214. // free reactor
  215. BReactor_Free(&reactor);
  216. fail1:
  217. // free logger
  218. BLog(BLOG_NOTICE, "exiting");
  219. BLog_Free();
  220. fail0:
  221. // finish objects
  222. DebugObjectGlobal_Finish();
  223. return main_exit_code;
  224. }
  225. void print_help (const char *name)
  226. {
  227. printf(
  228. "Usage:\n"
  229. " %s\n"
  230. " [--help]\n"
  231. " [--version]\n"
  232. " [--logger <stdout/stderr/syslog>]\n"
  233. " (logger=syslog?\n"
  234. " [--syslog-facility <string>]\n"
  235. " [--syslog-ident <string>]\n"
  236. " )\n"
  237. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  238. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  239. " [--retry-time <ms>]\n"
  240. " [--no-udev]\n"
  241. " [--config-file <ncd_program_file>]\n"
  242. " [-- program_args...]\n"
  243. " [<ncd_program_file> program_args...]\n" ,
  244. name
  245. );
  246. }
  247. void print_version (void)
  248. {
  249. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  250. }
  251. int parse_arguments (int argc, char *argv[])
  252. {
  253. if (argc <= 0) {
  254. return 0;
  255. }
  256. options.help = 0;
  257. options.version = 0;
  258. options.logger = LOGGER_STDERR;
  259. #ifdef BADVPN_USE_SYSLOG
  260. options.logger_syslog_facility = "daemon";
  261. options.logger_syslog_ident = argv[0];
  262. #endif
  263. options.loglevel = -1;
  264. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  265. options.loglevels[i] = -1;
  266. }
  267. options.config_file = NULL;
  268. options.retry_time = DEFAULT_RETRY_TIME;
  269. options.no_udev = 0;
  270. options.extra_args = NULL;
  271. options.num_extra_args = 0;
  272. for (int i = 1; i < argc; i++) {
  273. char *arg = argv[i];
  274. if (!strcmp(arg, "--help")) {
  275. options.help = 1;
  276. }
  277. else if (!strcmp(arg, "--version")) {
  278. options.version = 1;
  279. }
  280. else if (!strcmp(arg, "--logger")) {
  281. if (1 >= argc - i) {
  282. fprintf(stderr, "%s: requires an argument\n", arg);
  283. return 0;
  284. }
  285. char *arg2 = argv[i + 1];
  286. if (!strcmp(arg2, "stdout")) {
  287. options.logger = LOGGER_STDOUT;
  288. }
  289. else if (!strcmp(arg2, "stderr")) {
  290. options.logger = LOGGER_STDERR;
  291. }
  292. #ifdef BADVPN_USE_SYSLOG
  293. else if (!strcmp(arg2, "syslog")) {
  294. options.logger = LOGGER_SYSLOG;
  295. }
  296. #endif
  297. else {
  298. fprintf(stderr, "%s: wrong argument\n", arg);
  299. return 0;
  300. }
  301. i++;
  302. }
  303. #ifdef BADVPN_USE_SYSLOG
  304. else if (!strcmp(arg, "--syslog-facility")) {
  305. if (1 >= argc - i) {
  306. fprintf(stderr, "%s: requires an argument\n", arg);
  307. return 0;
  308. }
  309. options.logger_syslog_facility = argv[i + 1];
  310. i++;
  311. }
  312. else if (!strcmp(arg, "--syslog-ident")) {
  313. if (1 >= argc - i) {
  314. fprintf(stderr, "%s: requires an argument\n", arg);
  315. return 0;
  316. }
  317. options.logger_syslog_ident = argv[i + 1];
  318. i++;
  319. }
  320. #endif
  321. else if (!strcmp(arg, "--loglevel")) {
  322. if (1 >= argc - i) {
  323. fprintf(stderr, "%s: requires an argument\n", arg);
  324. return 0;
  325. }
  326. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  327. fprintf(stderr, "%s: wrong argument\n", arg);
  328. return 0;
  329. }
  330. i++;
  331. }
  332. else if (!strcmp(arg, "--channel-loglevel")) {
  333. if (2 >= argc - i) {
  334. fprintf(stderr, "%s: requires two arguments\n", arg);
  335. return 0;
  336. }
  337. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  338. if (channel < 0) {
  339. fprintf(stderr, "%s: wrong channel argument\n", arg);
  340. return 0;
  341. }
  342. int loglevel = parse_loglevel(argv[i + 2]);
  343. if (loglevel < 0) {
  344. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  345. return 0;
  346. }
  347. options.loglevels[channel] = loglevel;
  348. i += 2;
  349. }
  350. else if (!strcmp(arg, "--config-file")) {
  351. if (1 >= argc - i) {
  352. fprintf(stderr, "%s: requires an argument\n", arg);
  353. return 0;
  354. }
  355. options.config_file = argv[i + 1];
  356. i++;
  357. }
  358. else if (!strcmp(arg, "--retry-time")) {
  359. if (1 >= argc - i) {
  360. fprintf(stderr, "%s: requires an argument\n", arg);
  361. return 0;
  362. }
  363. if ((options.retry_time = atoi(argv[i + 1])) < 0) {
  364. fprintf(stderr, "%s: wrong argument\n", arg);
  365. return 0;
  366. }
  367. i++;
  368. }
  369. else if (!strcmp(arg, "--no-udev")) {
  370. options.no_udev = 1;
  371. }
  372. else if (!strcmp(arg, "--")) {
  373. options.extra_args = &argv[i + 1];
  374. options.num_extra_args = argc - i - 1;
  375. i += options.num_extra_args;
  376. }
  377. else if (!string_begins_with(arg, "--")) {
  378. if (options.config_file) {
  379. fprintf(stderr, "%s: program is already specified (did you mean to use -- ?)\n", arg);
  380. return 0;
  381. }
  382. options.config_file = argv[i];
  383. options.extra_args = &argv[i + 1];
  384. options.num_extra_args = argc - i - 1;
  385. i += options.num_extra_args;
  386. }
  387. else {
  388. fprintf(stderr, "unknown option: %s\n", arg);
  389. return 0;
  390. }
  391. }
  392. if (options.help || options.version) {
  393. return 1;
  394. }
  395. if (!options.config_file) {
  396. fprintf(stderr, "No program is specified.\n");
  397. return 0;
  398. }
  399. return 1;
  400. }
  401. void signal_handler (void *unused)
  402. {
  403. BLog(BLOG_NOTICE, "termination requested");
  404. NCDInterpreter_RequestShutdown(&interpreter, 1);
  405. }
  406. void interpreter_handler_finished (void *user, int exit_code)
  407. {
  408. main_exit_code = exit_code;
  409. BReactor_Quit(&reactor, 0);
  410. }