procd.c 1.8 KB

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