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
1=== modified file 'drizzled/daemon.cc'
2--- drizzled/daemon.cc 2011-07-18 17:13:07 +0000
3+++ drizzled/daemon.cc 2012-01-05 07:16:24 +0000
4@@ -45,6 +45,7 @@
5 #include <sys/wait.h>
6 #include <signal.h>
7 #include <unistd.h>
8+#include <errno.h>
9 #include <sys/select.h>
10
11 #include <drizzled/daemon.h>
12@@ -52,15 +53,19 @@
13 namespace drizzled
14 {
15
16-pid_t parent_pid;
17+int parent_pipe_fds[2];
18
19 extern "C"
20 {
21
22-static void sigusr1_handler(int sig)
23+static void sigchld_handler(int sig)
24 {
25- if (sig == SIGUSR1)
26- _exit(EXIT_SUCCESS);
27+ int status= -1;
28+ if (sig == SIGCHLD)
29+ {
30+ (void)wait(&status);
31+ }
32+ _exit(status);
33 }
34
35 }
36@@ -68,7 +73,20 @@
37 void daemon_is_ready()
38 {
39 int fd;
40- kill(parent_pid, SIGUSR1);
41+ ssize_t wbytes;
42+ while ((wbytes= write(parent_pipe_fds[1], "\0", sizeof("\0"))) == 0);
43+ {
44+ if (wbytes < 0)
45+ {
46+ perror("write");
47+ _exit(errno);
48+ }
49+ }
50+ if (close(parent_pipe_fds[1]))
51+ {
52+ perror("close");
53+ _exit(errno);
54+ }
55
56 if ((fd = open("/dev/null", O_RDWR, 0)) != -1)
57 {
58@@ -105,8 +123,11 @@
59 {
60 pid_t child= -1;
61
62- parent_pid= getpid();
63- signal(SIGUSR1, sigusr1_handler);
64+ if (pipe(parent_pipe_fds))
65+ {
66+ perror("pipe");
67+ _exit(errno);
68+ }
69
70 child= fork();
71
72@@ -121,24 +142,44 @@
73 default:
74 {
75 /* parent */
76- int exit_code= -1;
77- int status;
78- while (waitpid(child, &status, 0) != child)
79- { }
80-
81- if (WIFEXITED(status))
82- {
83- exit_code= WEXITSTATUS(status);
84- }
85- if (WIFSIGNALED(status))
86- {
87- exit_code= -1;
88- }
89- _exit(exit_code);
90+ char ready_byte[1];
91+ size_t rbytes;
92+ /* Register SIGCHLD handler for case where child exits before
93+ writing to the pipe */
94+ signal(SIGCHLD, sigchld_handler);
95+
96+ if (close(parent_pipe_fds[1]))
97+ {
98+ perror("close");
99+ _exit(errno);
100+ }
101+ /* If the pipe is closed before a write, we exit -1, otherwise errno is used */
102+ if ((rbytes= read(parent_pipe_fds[0],ready_byte,sizeof(ready_byte))) < 1)
103+ {
104+ int estatus= -1;
105+ if (rbytes != 0)
106+ {
107+ estatus= errno;
108+ perror("read");
109+ }
110+ _exit(estatus);
111+ }
112+ if (close(parent_pipe_fds[0]))
113+ {
114+ perror("close");
115+ _exit(errno);
116+ }
117+
118+ _exit(EXIT_SUCCESS);
119 }
120 }
121
122 /* child */
123+ if (close(parent_pipe_fds[0]))
124+ {
125+ perror("close");
126+ _exit(errno);
127+ }
128 if (setsid() == -1)
129 {
130 return true;