procd.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 int usage(const char *prog)
  26. {
  27. ERROR("Usage: %s [options]\n"
  28. "Options:\n"
  29. "\t-s <path>\tPath to ubus socket\n"
  30. "\t-h <path>\trun as hotplug daemon\n"
  31. "\t-d <level>\tEnable debug messages\n"
  32. "\n", prog);
  33. return 1;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. int ch;
  38. char *dbglvl = getenv("DBGLVL");
  39. ulog_open(ULOG_KMSG, LOG_DAEMON, "procd");
  40. if (dbglvl) {
  41. debug = atoi(dbglvl);
  42. unsetenv("DBGLVL");
  43. }
  44. while ((ch = getopt(argc, argv, "d:s:h:")) != -1) {
  45. switch (ch) {
  46. case 'h':
  47. return hotplug_run(optarg);
  48. case 's':
  49. ubus_socket = optarg;
  50. break;
  51. case 'd':
  52. debug = atoi(optarg);
  53. break;
  54. default:
  55. return usage(argv[0]);
  56. }
  57. }
  58. setsid();
  59. uloop_init();
  60. procd_signal();
  61. trigger_init();
  62. if (getpid() != 1)
  63. procd_connect_ubus();
  64. else
  65. procd_state_next();
  66. uloop_run();
  67. uloop_done();
  68. return 0;
  69. }