dropin.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. dropin.c -- a set of drop-in replacements for libc functions
  3. Copyright (C) 2000-2005 Ivo Timmermans,
  4. 2000-2006 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
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. $Id$
  17. */
  18. #include "system.h"
  19. #include "xalloc.h"
  20. #ifndef HAVE_DAEMON
  21. /*
  22. Replacement for the daemon() function.
  23. The daemon() function is for programs wishing to detach themselves
  24. from the controlling terminal and run in the background as system
  25. daemons.
  26. Unless the argument nochdir is non-zero, daemon() changes the
  27. current working directory to the root (``/'').
  28. Unless the argument noclose is non-zero, daemon() will redirect
  29. standard input, standard output and standard error to /dev/null.
  30. */
  31. int daemon(int nochdir, int noclose)
  32. {
  33. #ifdef HAVE_FORK
  34. pid_t pid;
  35. int fd;
  36. pid = fork();
  37. /* Check if forking failed */
  38. if(pid < 0) {
  39. perror("fork");
  40. exit(-1);
  41. }
  42. /* If we are the parent, terminate */
  43. if(pid)
  44. exit(0);
  45. /* Detach by becoming the new process group leader */
  46. if(setsid() < 0) {
  47. perror("setsid");
  48. return -1;
  49. }
  50. /* Change working directory to the root (to avoid keeping mount
  51. points busy) */
  52. if(!nochdir) {
  53. chdir("/");
  54. }
  55. /* Redirect stdin/out/err to /dev/null */
  56. if(!noclose) {
  57. fd = open("/dev/null", O_RDWR);
  58. if(fd < 0) {
  59. perror("opening /dev/null");
  60. return -1;
  61. } else {
  62. dup2(fd, 0);
  63. dup2(fd, 1);
  64. dup2(fd, 2);
  65. }
  66. }
  67. return 0;
  68. #else
  69. return -1;
  70. #endif
  71. }
  72. #endif
  73. #ifndef HAVE_GET_CURRENT_DIR_NAME
  74. /*
  75. Replacement for the GNU get_current_dir_name function:
  76. get_current_dir_name will malloc(3) an array big enough to hold the
  77. current directory name. If the environment variable PWD is set, and
  78. its value is correct, then that value will be returned.
  79. */
  80. char *get_current_dir_name(void)
  81. {
  82. size_t size;
  83. char *buf;
  84. char *r;
  85. /* Start with 100 bytes. If this turns out to be insufficient to
  86. contain the working directory, double the size. */
  87. size = 100;
  88. buf = xmalloc(size);
  89. errno = 0; /* Success */
  90. r = getcwd(buf, size);
  91. /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
  92. is insufficient to contain the entire working directory. */
  93. while(r == NULL && errno == ERANGE) {
  94. free(buf);
  95. size <<= 1; /* double the size */
  96. buf = xmalloc(size);
  97. r = getcwd(buf, size);
  98. }
  99. return buf;
  100. }
  101. #endif
  102. #ifndef HAVE_ASPRINTF
  103. int asprintf(char **buf, const char *fmt, ...)
  104. {
  105. int status;
  106. va_list ap;
  107. int len;
  108. len = 4096;
  109. *buf = xmalloc(len);
  110. va_start(ap, fmt);
  111. status = vsnprintf(*buf, len, fmt, ap);
  112. va_end(ap);
  113. if(status >= 0)
  114. *buf = xrealloc(*buf, status + 1);
  115. if(status > len - 1) {
  116. len = status;
  117. va_start(ap, fmt);
  118. status = vsnprintf(*buf, len, fmt, ap);
  119. va_end(ap);
  120. }
  121. return status;
  122. }
  123. #endif
  124. #ifndef HAVE_GETTIMEOFDAY
  125. int gettimeofday(struct timeval *tv, void *tz) {
  126. tv->tv_sec = time(NULL);
  127. tv->tv_usec = 0;
  128. return 0;
  129. }
  130. #endif
  131. #ifndef HAVE_RANDOM
  132. #include <openssl/rand.h>
  133. long int random(void) {
  134. long int x;
  135. RAND_pseudo_bytes((unsigned char *)&x, sizeof(x));
  136. return x;
  137. }
  138. #endif