Drizzled Public API Documentation

daemon.cc
1 /* $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $ */
2 /* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ */
3 /*-
4  * Copyright (c) 1990, 1993
5  * The Regents of the University of California. All rights reserved.
6  * Copyright (c) 2010
7  * Stewart Smith
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <config.h>
35 
36 #if defined __SUNPRO_C || defined __DECC || defined __HP_cc
37 # pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
38 # pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
39 #endif
40 
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <signal.h>
47 #include <unistd.h>
48 #include <errno.h>
49 #include <sys/select.h>
50 
51 #include <drizzled/daemon.h>
52 
53 namespace drizzled
54 {
55 
56 int parent_pipe_fds[2];
57 
58 extern "C"
59 {
60 
61 static void sigchld_handler(int sig)
62 {
63  int status= -1;
64  if (sig == SIGCHLD)
65  {
66  (void)wait(&status);
67  }
68  _exit(status);
69 }
70 
71 }
72 
73 void daemon_is_ready()
74 {
75  int fd;
76  ssize_t wbytes;
77  while ((wbytes= write(parent_pipe_fds[1], "\0", sizeof("\0"))) == 0)
78  {
79  if (wbytes < 0)
80  {
81  perror("write");
82  _exit(errno);
83  }
84  }
85  if (close(parent_pipe_fds[1]))
86  {
87  perror("close");
88  _exit(errno);
89  }
90 
91  if ((fd = open("/dev/null", O_RDWR, 0)) != -1)
92  {
93  if(dup2(fd, STDIN_FILENO) < 0)
94  {
95  perror("dup2 stdin");
96  return;
97  }
98 
99  if(dup2(fd, STDOUT_FILENO) < 0)
100  {
101  perror("dup2 stdout");
102  return;
103  }
104 
105  if(dup2(fd, STDERR_FILENO) < 0)
106  {
107  perror("dup2 stderr");
108  return;
109  }
110 
111  if (fd > STDERR_FILENO)
112  {
113  if (close(fd) < 0)
114  {
115  perror("close");
116  return;
117  }
118  }
119  }
120 }
121 
122 bool daemonize()
123 {
124  pid_t child= -1;
125 
126  if (pipe(parent_pipe_fds))
127  {
128  perror("pipe");
129  _exit(errno);
130  }
131 
132  child= fork();
133 
134  switch (child)
135  {
136  case -1:
137  return true;
138 
139  case 0:
140  break;
141 
142  default:
143  {
144  /* parent */
145  char ready_byte[1];
146  size_t rbytes;
147  /* Register SIGCHLD handler for case where child exits before
148  writing to the pipe */
149  signal(SIGCHLD, sigchld_handler);
150 
151  if (close(parent_pipe_fds[1]))
152  {
153  perror("close");
154  _exit(errno);
155  }
156  /* If the pipe is closed before a write, we exit -1, otherwise errno is used */
157  if ((rbytes= read(parent_pipe_fds[0],ready_byte,sizeof(ready_byte))) < 1)
158  {
159  int estatus= -1;
160  if (rbytes != 0)
161  {
162  estatus= errno;
163  perror("read");
164  }
165  _exit(estatus);
166  }
167  if (close(parent_pipe_fds[0]))
168  {
169  perror("close");
170  _exit(errno);
171  }
172 
173  _exit(EXIT_SUCCESS);
174  }
175  }
176 
177  /* child */
178  if (close(parent_pipe_fds[0]))
179  {
180  perror("close");
181  _exit(errno);
182  }
183  if (setsid() == -1)
184  {
185  return true;
186  }
187 
188  return false;
189 }
190 
191 } /* namespace drizzled */