daemon.c 3.2 KB

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