_info 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "config.h"
  4. /**
  5. * daemon-with-notify - turn a process into a daemon with parent exiting when
  6. * child has decided that it has started correctly.
  7. *
  8. * Daemons should detach themselves thoroughly from the process which launched
  9. * them, and not prevent any filesystems from being unmounted. daemonize()
  10. * helps with the process.
  11. *
  12. * Daemon-with-notify is different in that the child can send a SIGUSR1 to
  13. * the parent to indicate it has started (e.g. after memory allocation and
  14. * other things that may fail) so that the parent can return a success or
  15. * failing exit code and init scripts can pick this up easily.
  16. *
  17. * Example:
  18. * #include <ccan/daemon-with-notify/daemon-with-notify.h>
  19. * #include <ccan/str/str.h>
  20. * #include <err.h>
  21. * #include <unistd.h>
  22. * #include <stdlib.h>
  23. *
  24. * static void usage(const char *name)
  25. * {
  26. * errx(1, "Usage: %s [--daemonize]\n", name);
  27. * }
  28. *
  29. * // Wait for a minute, possibly as a daemon.
  30. * int main(int argc, char *argv[])
  31. * {
  32. * if (argc != 1) {
  33. * if (argc == 2 && streq(argv[1], "--daemonize")) {
  34. * if (!daemonize(1, 1, 1))
  35. * err(1, "Failed to become daemon");
  36. * } else
  37. * usage(argv[1]);
  38. * }
  39. * sleep(10); /* do some init here */
  40. * daemon_is_ready();
  41. * sleep(20); /* will be done in child, detached from parent */
  42. * exit(0);
  43. * }
  44. *
  45. * License: BSD-MIT
  46. */
  47. int main(int argc, char *argv[])
  48. {
  49. if (argc != 2)
  50. return 1;
  51. if (strcmp(argv[1], "depends") == 0) {
  52. return 0;
  53. }
  54. if (strcmp(argv[1], "libs") == 0) {
  55. return 0;
  56. }
  57. return 1;
  58. }