Merge lp:~clint-fewbar/drizzle/no-sigusr1 into lp:~drizzle-trunk/drizzle/development

Proposed by Clint Byrum
Status: Merged
Approved by: Mark Atwood
Approved revision: 2484
Merged at revision: 2488
Proposed branch: lp:~clint-fewbar/drizzle/no-sigusr1
Merge into: lp:~drizzle-trunk/drizzle/development
Diff against target: 130 lines (+62/-21)
1 file modified
drizzled/daemon.cc (+62/-21)
To merge this branch: bzr merge lp:~clint-fewbar/drizzle/no-sigusr1
Reviewer Review Type Date Requested Status
Drizzle Merge Team Pending
Review via email: mp+87387@code.launchpad.net

Description of the change

Uses a pipe rather than SIGUSR1 to delay the parent's exit while the child sets things up.

To post a comment you must log in.
lp:~clint-fewbar/drizzle/no-sigusr1 updated
2484. By Clint Byrum

removing unused parent_pid

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'drizzled/daemon.cc'
--- drizzled/daemon.cc 2011-07-18 17:13:07 +0000
+++ drizzled/daemon.cc 2012-01-05 07:16:24 +0000
@@ -45,6 +45,7 @@
45#include <sys/wait.h>45#include <sys/wait.h>
46#include <signal.h>46#include <signal.h>
47#include <unistd.h>47#include <unistd.h>
48#include <errno.h>
48#include <sys/select.h>49#include <sys/select.h>
4950
50#include <drizzled/daemon.h>51#include <drizzled/daemon.h>
@@ -52,15 +53,19 @@
52namespace drizzled53namespace drizzled
53{54{
5455
55pid_t parent_pid;56int parent_pipe_fds[2];
5657
57extern "C"58extern "C"
58{59{
5960
60static void sigusr1_handler(int sig)61static void sigchld_handler(int sig)
61{62{
62 if (sig == SIGUSR1)63 int status= -1;
63 _exit(EXIT_SUCCESS);64 if (sig == SIGCHLD)
65 {
66 (void)wait(&status);
67 }
68 _exit(status);
64}69}
6570
66}71}
@@ -68,7 +73,20 @@
68void daemon_is_ready()73void daemon_is_ready()
69{74{
70 int fd;75 int fd;
71 kill(parent_pid, SIGUSR1);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 }
7290
73 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) 91 if ((fd = open("/dev/null", O_RDWR, 0)) != -1)
74 {92 {
@@ -105,8 +123,11 @@
105{123{
106 pid_t child= -1;124 pid_t child= -1;
107125
108 parent_pid= getpid();126 if (pipe(parent_pipe_fds))
109 signal(SIGUSR1, sigusr1_handler);127 {
128 perror("pipe");
129 _exit(errno);
130 }
110131
111 child= fork();132 child= fork();
112133
@@ -121,24 +142,44 @@
121 default:142 default:
122 {143 {
123 /* parent */144 /* parent */
124 int exit_code= -1;145 char ready_byte[1];
125 int status;146 size_t rbytes;
126 while (waitpid(child, &status, 0) != child)147 /* Register SIGCHLD handler for case where child exits before
127 { }148 writing to the pipe */
128149 signal(SIGCHLD, sigchld_handler);
129 if (WIFEXITED(status))150
130 {151 if (close(parent_pipe_fds[1]))
131 exit_code= WEXITSTATUS(status);152 {
132 }153 perror("close");
133 if (WIFSIGNALED(status))154 _exit(errno);
134 {155 }
135 exit_code= -1;156 /* If the pipe is closed before a write, we exit -1, otherwise errno is used */
136 }157 if ((rbytes= read(parent_pipe_fds[0],ready_byte,sizeof(ready_byte))) < 1)
137 _exit(exit_code);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);
138 }174 }
139 }175 }
140176
141 /* child */177 /* child */
178 if (close(parent_pipe_fds[0]))
179 {
180 perror("close");
181 _exit(errno);
182 }
142 if (setsid() == -1)183 if (setsid() == -1)
143 {184 {
144 return true;185 return true;