procd.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <sys/wait.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/reboot.h>
  18. #include <unistd.h>
  19. #include <getopt.h>
  20. #include <libgen.h>
  21. #include "procd.h"
  22. #include "watchdog.h"
  23. #include "plug/hotplug.h"
  24. unsigned int debug;
  25. static struct udebug ud;
  26. static struct udebug_buf udb;
  27. static bool udebug_enabled;
  28. static void procd_udebug_vprintf(const char *format, va_list ap)
  29. {
  30. if (!udebug_enabled)
  31. return;
  32. udebug_entry_init(&udb);
  33. udebug_entry_vprintf(&udb, format, ap);
  34. udebug_entry_add(&udb);
  35. }
  36. void procd_udebug_printf(const char *format, ...)
  37. {
  38. va_list ap;
  39. va_start(ap, format);
  40. procd_udebug_vprintf(format, ap);
  41. va_end(ap);
  42. }
  43. void procd_udebug_set_enabled(bool val)
  44. {
  45. static const struct udebug_buf_meta meta = {
  46. .name = "procd_log",
  47. .format = UDEBUG_FORMAT_STRING,
  48. };
  49. if (udebug_enabled == val)
  50. return;
  51. udebug_enabled = val;
  52. if (!val) {
  53. ulog_udebug(NULL);
  54. udebug_buf_free(&udb);
  55. udebug_free(&ud);
  56. return;
  57. }
  58. udebug_init(&ud);
  59. udebug_auto_connect(&ud, NULL);
  60. udebug_buf_init(&udb, 1024, 64 * 1024);
  61. udebug_buf_add(&ud, &udb, &meta);
  62. ulog_udebug(&udb);
  63. }
  64. static int usage(const char *prog)
  65. {
  66. fprintf(stderr, "Usage: %s [options]\n"
  67. "Options:\n"
  68. " -s <path> Path to ubus socket\n"
  69. " -h <path> run as hotplug daemon\n"
  70. " -d <level> Enable debug messages\n"
  71. " -S Print messages to stdout\n"
  72. "\n", prog);
  73. return 1;
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. int ch;
  78. char *dbglvl = getenv("DBGLVL");
  79. int ulog_channels = ULOG_KMSG;
  80. if (dbglvl) {
  81. debug = atoi(dbglvl);
  82. unsetenv("DBGLVL");
  83. }
  84. while ((ch = getopt(argc, argv, "d:s:h:S")) != -1) {
  85. switch (ch) {
  86. case 'h':
  87. return hotplug_run(optarg);
  88. case 's':
  89. ubus_socket = optarg;
  90. break;
  91. case 'd':
  92. debug = atoi(optarg);
  93. break;
  94. case 'S':
  95. ulog_channels = ULOG_STDIO;
  96. break;
  97. default:
  98. return usage(argv[0]);
  99. }
  100. }
  101. ulog_open(ulog_channels, LOG_DAEMON, "procd");
  102. ulog_threshold(LOG_DEBUG + 1);
  103. setsid();
  104. uloop_init();
  105. procd_signal();
  106. procd_udebug_set_enabled(true);
  107. if (getpid() != 1)
  108. procd_connect_ubus();
  109. else
  110. procd_state_next();
  111. uloop_run();
  112. uloop_done();
  113. return 0;
  114. }