dropin.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. dropin.c -- a set of drop-in replacements for libc functions
  3. Copyright (C) 2000-2005 Ivo Timmermans,
  4. 2000-2016 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "xalloc.h"
  19. #ifndef HAVE_DAEMON
  20. /*
  21. Replacement for the daemon() function.
  22. The daemon() function is for programs wishing to detach themselves
  23. from the controlling terminal and run in the background as system
  24. daemons.
  25. Unless the argument nochdir is non-zero, daemon() changes the
  26. current working directory to the root (``/'').
  27. Unless the argument noclose is non-zero, daemon() will redirect
  28. standard input, standard output and standard error to /dev/null.
  29. */
  30. int daemon(int nochdir, int noclose) {
  31. #ifdef HAVE_FORK
  32. pid_t pid;
  33. int fd;
  34. pid = fork();
  35. /* Check if forking failed */
  36. if(pid < 0) {
  37. perror("fork");
  38. exit(-1);
  39. }
  40. /* If we are the parent, terminate */
  41. if(pid) {
  42. exit(0);
  43. }
  44. /* Detach by becoming the new process group leader */
  45. if(setsid() < 0) {
  46. perror("setsid");
  47. return -1;
  48. }
  49. /* Change working directory to the root (to avoid keeping mount
  50. points busy) */
  51. if(!nochdir) {
  52. chdir("/");
  53. }
  54. /* Redirect stdin/out/err to /dev/null */
  55. if(!noclose) {
  56. fd = open("/dev/null", O_RDWR);
  57. if(fd < 0) {
  58. perror("opening /dev/null");
  59. return -1;
  60. } else {
  61. dup2(fd, 0);
  62. dup2(fd, 1);
  63. dup2(fd, 2);
  64. }
  65. }
  66. return 0;
  67. #else
  68. return -1;
  69. #endif
  70. }
  71. #endif
  72. #ifndef HAVE_ASPRINTF
  73. int asprintf(char **buf, const char *fmt, ...) {
  74. int result;
  75. va_list ap;
  76. va_start(ap, fmt);
  77. result = vasprintf(buf, fmt, ap);
  78. va_end(ap);
  79. return result;
  80. }
  81. int vasprintf(char **buf, const char *fmt, va_list ap) {
  82. int status;
  83. va_list aq;
  84. int len;
  85. len = 4096;
  86. *buf = xmalloc(len);
  87. va_copy(aq, ap);
  88. status = vsnprintf(*buf, len, fmt, aq);
  89. buf[len - 1] = 0;
  90. va_end(aq);
  91. if(status >= 0) {
  92. *buf = xrealloc(*buf, status + 1);
  93. }
  94. if(status > len - 1) {
  95. len = status;
  96. va_copy(aq, ap);
  97. status = vsnprintf(*buf, len, fmt, aq);
  98. va_end(aq);
  99. }
  100. return status;
  101. }
  102. #endif
  103. #ifndef HAVE_GETTIMEOFDAY
  104. int gettimeofday(struct timeval *tv, void *tz) {
  105. tv->tv_sec = time(NULL);
  106. tv->tv_usec = 0;
  107. return 0;
  108. }
  109. #endif
  110. #ifndef HAVE_USLEEP
  111. int usleep(long long usec) {
  112. struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
  113. select(0, NULL, NULL, NULL, &tv);
  114. return 0;
  115. }
  116. #endif