Merge lp:~stewart/drizzle/bbug565053-daemon into lp:drizzle/7.0

Proposed by Stewart Smith
Status: Merged
Approved by: Brian Aker
Approved revision: 2138
Merged at revision: 2156
Proposed branch: lp:~stewart/drizzle/bbug565053-daemon
Merge into: lp:drizzle/7.0
Diff against target: 328 lines (+182/-8)
8 files modified
drizzled/daemon.c (+138/-0)
drizzled/drizzled.cc (+16/-2)
drizzled/drizzled.h (+2/-1)
drizzled/identifier/schema.cc (+0/-1)
drizzled/include.am (+1/-0)
drizzled/main.cc (+25/-1)
drizzled/sql_table.cc (+0/-2)
drizzled/table.cc (+0/-1)
To merge this branch: bzr merge lp:~stewart/drizzle/bbug565053-daemon
Reviewer Review Type Date Requested Status
Lee Bieber (community) Needs Fixing
Review via email: mp+48267@code.launchpad.net

Description of the change

adds --daemon option.

heavily based on the memcached daemon code, probably will copy it back there too as exiting after successful startup seems to be a good idea instead of having opportunities for silent failure.

To post a comment you must log in.
Revision history for this message
Brian Aker (brianaker) wrote :

1) Code style is off.
2) What happens if someone does a --help and calls the daemon at the same time?

Revision history for this message
Stewart Smith (stewart) wrote :

so it also looks like we create the pid file at a bit of an odd time.

Maybe we could write some tests for this in the new test thing dbqp or whatever it's called that i can't remember :)

Revision history for this message
Patrick Crews (patrick-crews) wrote :

Did some manual testing of the attached branch and things worked as expected.
This was largely cursory (startup / shutdown / basic stuff), but I'm satisfied that this works

Revision history for this message
Patrick Crews (patrick-crews) wrote :

Have a few more testcases to run through - will have them done in the am. Apologies for the delay, but Brian pointed out a few areas to poke at ; )

Revision history for this message
Patrick Crews (patrick-crews) wrote :

Tests fine, parent id is apparently killed off once server is started, killing server is fine, calling --shutdown via drizzle client is fine. Everything is looking a-ok.

Revision history for this message
Lee Bieber (kalebral-deactivatedaccount) wrote :

Fails to build on freebsd 8.0

g++44 -DHAVE_CONFIG_H -I. -I. -g -isystem /usr/local/include -I/usr/local/include -D_THREAD_SAFE -pthread -pipe -std=gnu++98 -O2 -fvisibility=hidden -fvisibility-inlines-hidden -Werror -pedantic -Wall -Wundef -Wshadow -fdiagnostics-show-option -fno-strict-aliasing -Wno-strict-aliasing -Wextra -Wformat -Wno-format-nonliteral -Wno-format-security -Woverloaded-virtual -Wnon-virtual-dtor -Wctor-dtor-privacy -Wno-long-long -Wmissing-declarations -Wframe-larger-than=32768 -Wno-redundant-decls -D_THREAD_SAFE -pthread -MT drizzled/message/cache.o -MD -MP -MF $depbase.Tpo -c -o drizzled/message/cache.o drizzled/message/cache.cc &&\
 mv -f $depbase.Tpo $depbase.Po
drizzled/main.cc: In function 'int main(int, char**)':
drizzled/main.cc:261: error: 'sigignore' was not declared in this scope

http://jenkins.drizzle.org/view/Drizzle-build/job/drizzle-build-freebsd-8.0/1326/console

review: Needs Fixing
Revision history for this message
Stewart Smith (stewart) wrote :

latest commit should fix it.

2139. By Stewart Smith

using signal() instead of sigignore() as it's compatible with FreeBSD < 8.1

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'drizzled/daemon.c'
2--- drizzled/daemon.c 1970-01-01 00:00:00 +0000
3+++ drizzled/daemon.c 2011-02-10 23:44:44 +0000
4@@ -0,0 +1,138 @@
5+/* $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $ */
6+/* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ */
7+/*-
8+ * Copyright (c) 1990, 1993
9+ * The Regents of the University of California. All rights reserved.
10+ * Copyright (c) 2010
11+ * Stewart Smith
12+ *
13+ * Redistribution and use in source and binary forms, with or without
14+ * modification, are permitted provided that the following conditions
15+ * are met:
16+ * 1. Redistributions of source code must retain the above copyright
17+ * notice, this list of conditions and the following disclaimer.
18+ * 2. Redistributions in binary form must reproduce the above copyright
19+ * notice, this list of conditions and the following disclaimer in the
20+ * documentation and/or other materials provided with the distribution.
21+ * 3. Neither the name of the University nor the names of its contributors
22+ * may be used to endorse or promote products derived from this software
23+ * without specific prior written permission.
24+ *
25+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35+ * SUCH DAMAGE.
36+ */
37+
38+#if defined __SUNPRO_C || defined __DECC || defined __HP_cc
39+# pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
40+# pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
41+#endif
42+
43+#include <fcntl.h>
44+#include <stdio.h>
45+#include <stdlib.h>
46+#include <sys/types.h>
47+#include <sys/wait.h>
48+#include <signal.h>
49+#include <unistd.h>
50+#include <sys/select.h>
51+
52+int daemonize(int nochdir, int noclose, int wait_sigusr1);
53+int daemon_is_ready(void);
54+void sigusr1_handler(int sig);
55+
56+pid_t parent_pid;
57+
58+void sigusr1_handler(int sig)
59+{
60+ if (sig == SIGUSR1)
61+ _exit(EXIT_SUCCESS);
62+}
63+
64+int daemon_is_ready()
65+{
66+ kill(parent_pid, SIGUSR1);
67+ return 0;
68+}
69+
70+int daemonize(int nochdir, int noclose, int wait_sigusr1)
71+{
72+ int fd;
73+ pid_t child= -1;
74+
75+ parent_pid= getpid();
76+ signal(SIGUSR1, sigusr1_handler);
77+
78+ child= fork();
79+
80+ switch (child)
81+ {
82+ case -1:
83+ return (-1);
84+ case 0:
85+ break;
86+ default:
87+ if (wait_sigusr1)
88+ {
89+ /* parent */
90+ int exit_code= -1;
91+ int status;
92+ while (waitpid(child, &status, 0) != child);
93+ if (WIFEXITED(status))
94+ {
95+ exit_code= WEXITSTATUS(status);
96+ }
97+ if (WIFSIGNALED(status))
98+ {
99+ exit_code= -1;
100+ }
101+ _exit(exit_code);
102+ }
103+ else
104+ {
105+ _exit(EXIT_SUCCESS);
106+ }
107+ }
108+
109+ /* child */
110+ if (setsid() == -1)
111+ return (-1);
112+
113+ if (nochdir == 0) {
114+ if(chdir("/") != 0) {
115+ perror("chdir");
116+ return (-1);
117+ }
118+ }
119+
120+ if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
121+ if(dup2(fd, STDIN_FILENO) < 0) {
122+ perror("dup2 stdin");
123+ return (-1);
124+ }
125+ if(dup2(fd, STDOUT_FILENO) < 0) {
126+ perror("dup2 stdout");
127+ return (-1);
128+ }
129+ if(dup2(fd, STDERR_FILENO) < 0) {
130+ perror("dup2 stderr");
131+ return (-1);
132+ }
133+
134+ if (fd > STDERR_FILENO) {
135+ if(close(fd) < 0) {
136+ perror("close");
137+ return (-1);
138+ }
139+ }
140+ }
141+ return (0);
142+}
143
144=== modified file 'drizzled/drizzled.cc'
145--- drizzled/drizzled.cc 2011-02-09 15:07:26 +0000
146+++ drizzled/drizzled.cc 2011-02-10 23:44:44 +0000
147@@ -158,6 +158,7 @@
148 namespace po=boost::program_options;
149 namespace dpo=drizzled::program_options;
150
151+bool opt_daemon= false;
152
153 namespace drizzled
154 {
155@@ -1084,7 +1085,7 @@
156 }
157 }
158
159-int init_common_variables(int argc, char **argv, module::Registry &plugins)
160+int init_basic_variables(int argc, char **argv)
161 {
162 time_t curr_time;
163 umask(((~internal::my_umask) & 0666));
164@@ -1134,6 +1135,8 @@
165 config_options.add_options()
166 ("help,?", po::value<bool>(&opt_help)->default_value(false)->zero_tokens(),
167 _("Display this help and exit."))
168+ ("daemon,d", po::value<bool>(&opt_daemon)->default_value(false)->zero_tokens(),
169+ _("Run as a daemon."))
170 ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
171 _("Configuration file defaults are not used if no-defaults is set"))
172 ("defaults-file", po::value<vector<string> >()->composing()->notifier(&compose_defaults_file_list),
173@@ -1409,6 +1412,15 @@
174 unireg_abort(1);
175 }
176
177+ return 0;
178+}
179+
180+int init_remaining_variables(module::Registry &plugins)
181+{
182+ int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
183+
184+ current_pid= getpid(); /* Save for later ref */
185+
186 /* At this point, we've read all the options we need to read from files and
187 collected most of them into unknown options - now let's load everything
188 */
189@@ -1488,7 +1500,6 @@
190
191 fix_paths();
192
193- current_pid= getpid(); /* Save for later ref */
194 init_time(); /* Init time-functions (read zone) */
195
196 if (item_create_init())
197@@ -1723,6 +1734,9 @@
198 {"help", '?', N_("Display this help and exit."),
199 (char**) &opt_help, (char**) &opt_help, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
200 0, 0},
201+ {"daemon", 'd', N_("Run as daemon."),
202+ (char**) &opt_daemon, (char**) &opt_daemon, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
203+ 0, 0},
204 {"auto-increment-increment", OPT_AUTO_INCREMENT,
205 N_("Auto-increment columns are incremented by this"),
206 (char**) &global_system_variables.auto_increment_increment,
207
208=== modified file 'drizzled/drizzled.h'
209--- drizzled/drizzled.h 2011-01-24 05:33:52 +0000
210+++ drizzled/drizzled.h 2011-02-10 23:44:44 +0000
211@@ -60,7 +60,8 @@
212
213 int init_thread_environment();
214 int init_server_components(module::Registry &modules);
215-int init_common_variables(int argc, char **argv, module::Registry &modules);
216+int init_basic_variables(int argc, char **argv);
217+int init_remaining_variables(module::Registry &modules);
218
219 passwd *check_user(const char *user);
220 void set_user(const char *user, passwd *user_info_arg);
221
222=== modified file 'drizzled/identifier/schema.cc'
223--- drizzled/identifier/schema.cc 2011-02-07 01:54:33 +0000
224+++ drizzled/identifier/schema.cc 2011-02-10 23:44:44 +0000
225@@ -44,7 +44,6 @@
226 {
227
228 extern string drizzle_tmpdir;
229-extern pid_t current_pid;
230
231 static size_t build_schema_filename(string &path, const string &db)
232 {
233
234=== modified file 'drizzled/include.am'
235--- drizzled/include.am 2011-02-04 09:29:39 +0000
236+++ drizzled/include.am 2011-02-10 23:44:44 +0000
237@@ -725,6 +725,7 @@
238 drizzled/locking/global.cc \
239 drizzled/lookup_symbol.cc \
240 drizzled/main.cc \
241+ drizzled/daemon.c \
242 drizzled/message.cc \
243 drizzled/message/cache.cc \
244 drizzled/message/catalog.cc \
245
246=== modified file 'drizzled/main.cc'
247--- drizzled/main.cc 2011-02-05 20:36:14 +0000
248+++ drizzled/main.cc 2011-02-10 23:44:44 +0000
249@@ -68,6 +68,9 @@
250
251 #include "drizzled/util/backtrace.h"
252
253+extern "C" int daemonize(int nochdir, int noclose, int wait_sigusr1);
254+extern "C" int daemon_is_ready(void);
255+
256 using namespace drizzled;
257 using namespace std;
258 namespace fs=boost::filesystem;
259@@ -75,6 +78,8 @@
260 static pthread_t select_thread;
261 static uint32_t thr_kill_signal;
262
263+extern bool opt_daemon;
264+
265
266 /**
267 All global error messages are sent here where the first one is stored
268@@ -245,9 +250,26 @@
269
270 /* Function generates error messages before abort */
271 error_handler_hook= my_message_sql;
272+
273 /* init_common_variables must get basic settings such as data_home_dir
274 and plugin_load_list. */
275- if (init_common_variables(argc, argv, modules))
276+ if (init_basic_variables(argc, argv))
277+ unireg_abort(1); // Will do exit
278+
279+ if (opt_daemon)
280+ {
281+ if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
282+ {
283+ perror("Failed to ignore SIGHUP");
284+ }
285+ if (daemonize(1, 1, 1) == -1)
286+ {
287+ fprintf(stderr, "failed to daemon() in order to daemonize\n");
288+ exit(EXIT_FAILURE);
289+ }
290+ }
291+
292+ if (init_remaining_variables(modules))
293 unireg_abort(1); // Will do exit
294
295 /*
296@@ -361,6 +383,8 @@
297 }
298 }
299
300+ if (opt_daemon)
301+ daemon_is_ready();
302
303 /*
304 Listen for new connections and start new session for each connection
305
306=== modified file 'drizzled/sql_table.cc'
307--- drizzled/sql_table.cc 2011-01-24 21:05:10 +0000
308+++ drizzled/sql_table.cc 2011-02-10 23:44:44 +0000
309@@ -58,8 +58,6 @@
310 namespace drizzled
311 {
312
313-extern pid_t current_pid;
314-
315 bool is_primary_key(KeyInfo *key_info)
316 {
317 static const char * primary_key_name="PRIMARY";
318
319=== modified file 'drizzled/table.cc'
320--- drizzled/table.cc 2011-02-04 21:25:07 +0000
321+++ drizzled/table.cc 2011-02-10 23:44:44 +0000
322@@ -63,7 +63,6 @@
323 namespace drizzled
324 {
325
326-extern pid_t current_pid;
327 extern plugin::StorageEngine *heap_engine;
328 extern plugin::StorageEngine *myisam_engine;
329

Subscribers

People subscribed via source and target branches