daemon.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. *
  3. * Copyright (c) 2019, Ali Rıza Keskin
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <syslog.h>
  33. static void skeleton_daemon()
  34. {
  35. pid_t pid;
  36. /* Fork off the parent process */
  37. pid = fork();
  38. /* An error occurred */
  39. if (pid < 0)
  40. exit(EXIT_FAILURE);
  41. /* Success: Let the parent terminate */
  42. if (pid > 0)
  43. exit(EXIT_SUCCESS);
  44. /* On success: The child process becomes session leader */
  45. if (setsid() < 0)
  46. exit(EXIT_FAILURE);
  47. /* Catch, ignore and handle signals */
  48. //TODO: Implement a working signal handler */
  49. signal(SIGCHLD, SIG_IGN);
  50. signal(SIGHUP, SIG_IGN);
  51. /* Fork off for the second time*/
  52. pid = fork();
  53. /* An error occurred */
  54. if (pid < 0)
  55. exit(EXIT_FAILURE);
  56. /* Success: Let the parent terminate */
  57. if (pid > 0)
  58. exit(EXIT_SUCCESS);
  59. /* Set new file permissions */
  60. umask(0);
  61. /* Open the log file */
  62. openlog ("scom-daemon", LOG_PID, LOG_DAEMON);
  63. }