daemon.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * daemon implementation for uClibc
  4. *
  5. * Copyright (c) 1991, 1993
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * Modified for uClibc by Erik Andersen
  9. * <andersee@debian.org>, <andersen@lineo.com>
  10. *
  11. * The uClibc Library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Library General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * The GNU C Library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Library General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public
  22. * License along with the GNU C Library; see the file COPYING.LIB. If not,
  23. * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 02111-1307, USA.
  25. *
  26. * Original copyright notice is retained at the end of this file.
  27. */
  28. #include <features.h>
  29. #include <fcntl.h>
  30. #include <paths.h>
  31. #include <unistd.h>
  32. int daemon( int nochdir, int noclose )
  33. {
  34. #if __UCLIBC_HAS_MMU__
  35. int fd;
  36. switch (fork()) {
  37. case -1:
  38. return(-1);
  39. case 0:
  40. break;
  41. default:
  42. _exit(0);
  43. }
  44. if (setsid() == -1)
  45. return(-1);
  46. if (!nochdir)
  47. chdir("/");
  48. if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
  49. dup2(fd, STDIN_FILENO);
  50. dup2(fd, STDOUT_FILENO);
  51. dup2(fd, STDERR_FILENO);
  52. if (fd > 2)
  53. close(fd);
  54. }
  55. return(0);
  56. #else
  57. fprintf(stderr, "Sorry, daemon() requires an MMU\n");
  58. return(-1);
  59. #endif
  60. }
  61. /*-
  62. * Copyright (c) 1990, 1993
  63. * The Regents of the University of California. All rights reserved.
  64. *
  65. * Redistribution and use in source and binary forms, with or without
  66. * modification, are permitted provided that the following conditions
  67. * are met:
  68. * 1. Redistributions of source code must retain the above copyright
  69. * notice, this list of conditions and the following disclaimer.
  70. * 2. Redistributions in binary form must reproduce the above copyright
  71. * notice, this list of conditions and the following disclaimer in the
  72. * documentation and/or other materials provided with the distribution.
  73. *
  74. * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
  75. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
  76. *
  77. * 4. Neither the name of the University nor the names of its contributors
  78. * may be used to endorse or promote products derived from this software
  79. * without specific prior written permission.
  80. *
  81. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  82. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  84. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  85. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  86. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  87. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  88. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  89. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  90. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  91. * SUCH DAMAGE.
  92. */