Guest User

Untitled

a guest
Feb 25th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2017 Yu Jing <yu@argcv.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies 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 included in
  14. *all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, 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 DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. **/
  26. #include "argcv/c/sys/daemon.h"
  27.  
  28. // #include <assert.h>
  29. #include <fcntl.h> // open
  30. #include <signal.h> // signal
  31. #include <sys/stat.h> // open O_RDONL
  32. // #include <syslog.h> // syslog
  33. #include <unistd.h> // setsid
  34.  
  35. int kDaemonStatus = 0; // for err_XXX() functions
  36. char kDaemonWorkDir[1024] = "/";
  37. int kDaemonMaxFD = 64;
  38.  
  39. int daemon_init(const char* ident) {
  40. // openlog(ident, LOG_PID, LOG_USER);
  41. // openlog(ident,LOG_CONS|LOG_PID,LOG_USER);
  42. // syslog(LOG_INFO, "daemon starting");
  43. int i;
  44. pid_t pid;
  45.  
  46. if ((pid = fork()) < 0) {
  47. return -1;
  48. } else if (pid) {
  49. _exit(0); // parent terminates
  50. }
  51.  
  52. // child 1 continues ...
  53. if (setsid() < 0) { // become session leader
  54. return -1;
  55. }
  56.  
  57. // ignore
  58. signal(SIGHUP, SIG_IGN);
  59.  
  60. if ((pid = fork()) < 0) {
  61. return -1;
  62. } else if (pid) {
  63. _exit(0);
  64. }
  65. // child 2 continues ...
  66. kDaemonStatus = 1; // for err_XXX() functions
  67.  
  68. // syslog(LOG_INFO, "chdir: %s\n", kDaemonWorkDir);
  69.  
  70. int status_chdir = chdir(kDaemonWorkDir);
  71. // assert(status_chdir == 0);
  72. if (0 != status_chdir) {
  73. return status_chdir;
  74. }
  75.  
  76. // close off file descriptors
  77. for (i = 0; i < kDaemonMaxFD; i++) {
  78. close(i);
  79. }
  80.  
  81. open("/dev/null", O_RDONLY);
  82. open("/dev/null", O_RDWR);
  83. open("/dev/null", O_RDWR);
  84.  
  85. // syslog(LOG_INFO, "daemon started");
  86. return 0;
  87. }
  88.  
  89. void daemon_destroy() {
  90. // syslog(LOG_INFO, "daemon stopped");
  91. // closelog();
  92. kDaemonStatus = 0;
  93. }
Add Comment
Please, Sign In to add comment