=== added file 'branch.conf'
--- a/branch.conf	1970-01-01 00:00:00 +0000
+++ b/branch.conf	2014-10-05 09:00:21 +0000
@@ -0,0 +1,8 @@
+parent_location = bzr+ssh://bazaar.launchpad.net/+branch/maria/5.5/
+gtk_global_commit_message = """MDEV-5679 MariaDB holds stdin open after startup as mysqld
+
+implement --daemonize command-line option"""
+gtk_file_commit_messages = de
+gtk_bugs_fixed_list = mdev:5679
+push_location = bzr+ssh://bazaar.launchpad.net/~maria-captains/maria/5.5-serg/
+submit_branch = file:///usr/home/serg/Abk/mysql/mysql-5.5/

=== modified file 'sql/mysqld.cc'
--- a/sql/mysqld.cc	2014-10-04 19:46:14 +0000
+++ b/sql/mysqld.cc	2014-10-05 09:00:21 +0000
@@ -18,6 +18,9 @@
 #include "sql_priv.h"
 #include "unireg.h"
 #include <signal.h>
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
 #ifndef __WIN__
 #include <netdb.h>        // getservbyname, servent
 #endif
@@ -341,7 +344,7 @@ static bool max_long_data_size_used= fal
 static bool volatile select_thread_in_use, signal_thread_in_use;
 static volatile bool ready_to_exit;
 static my_bool opt_debugging= 0, opt_external_locking= 0, opt_console= 0;
-static my_bool opt_short_log_format= 0;
+static my_bool opt_short_log_format= 0, opt_daemonize= 0;
 static uint kill_cached_threads, wake_thread;
 ulong max_used_connections;
 static volatile ulong cached_thread_count= 0;
@@ -1176,6 +1179,9 @@ struct my_rnd_struct sql_rand; ///< used
 #ifndef EMBEDDED_LIBRARY
 struct passwd *user_info;
 static pthread_t select_thread;
+#ifndef _WIN32
+static pid_t parent_pid= 0;
+#endif
 #endif
 
 /* OS specific variables */
@@ -2892,6 +2898,46 @@ static size_t my_setstacksize(pthread_at
 
 #ifndef EMBEDDED_LIBRARY
 
+/*
+  It's not easy to daemonize MariaDB - we have to fork
+  before starting any thread and before the pid is written to a pid file.
+  But from the user point of view we have to daemonize when we're
+  ready to accept connections, not earlier.
+
+  So, we fork early and the parent waits for a child to signal
+  that it's ready. Only then the parent exits.
+*/
+static void daemonized_exit(int signal)
+{
+  mysqld_exit(0);
+}
+
+static void fork_daemon()
+{
+  fclose(stdin); // *not* my_fclose(), as stdin was not my_fopen()'ed
+
+  parent_pid= getpid(); // to signal it later
+  int pid = fork();
+
+  if (pid == -1)
+  {
+    sql_print_error("Can't fork into background (errno: %M)", errno);
+    unireg_abort(1);
+  }
+
+  if (pid)
+  {
+    int status;
+    struct sigaction sa;
+    sa.sa_flags = 0;
+    sa.sa_handler = daemonized_exit;
+    sigaction(SIGHUP, &sa, NULL);
+    sf_leaking_memory= 1;         // no safemalloc memory leak reports here
+    wait(&status);
+    mysqld_exit(WEXITSTATUS(status)); // if we're here - the child has died
+  }
+}
+
 static void init_signals(void)
 {
   sigset_t set;
@@ -4874,6 +4920,20 @@ int mysqld_main(int argc, char **argv)
   if (init_common_variables())
     unireg_abort(1);				// Will do exit
 
+#ifndef EMBEDDED_LIBRARY
+#ifdef _WIN32
+  /* 
+   The subsequent calls may take a long time : e.g. innodb log read.
+   Thus set the long running service control manager timeout
+  */
+  Service.SetSlowStarting(slow_start_timeout);
+#else
+  /* before starting any new thread we fork, if requested */
+  if (opt_daemonize)
+    fork_daemon();
+#endif
+#endif
+
   init_signals();
 
   my_thread_stack_size= my_setstacksize(&connection_attrib,
@@ -4922,14 +4982,6 @@ int mysqld_main(int argc, char **argv)
 #endif
   }
 
-  /* 
-   The subsequent calls may take a long time : e.g. innodb log read.
-   Thus set the long running service control manager timeout
-  */
-#if defined(_WIN32) && !defined(EMBEDDED_LIBRARY)
-  Service.SetSlowStarting(slow_start_timeout);
-#endif
-
   if (init_server_components())
     unireg_abort(1);
 
@@ -5047,8 +5099,16 @@ int mysqld_main(int argc, char **argv)
                                                        : mysqld_unix_port),
                          mysqld_port,
                          MYSQL_COMPILATION_COMMENT);
-#if defined(_WIN32) && !defined(EMBEDDED_LIBRARY)
+#ifndef EMBEDDED_LIBRARY
+#ifdef _WIN32
   Service.SetRunning();
+#else
+  if (parent_pid)
+  {
+    // we've forked already. Now tell the parent to die. Bwa-ha-ha-ha-ha!
+    kill(parent_pid, SIGHUP);
+  }
+#endif
 #endif
 
 
@@ -6227,9 +6287,13 @@ struct my_option my_long_options[]=
    0, 0, 0},
   {"core-file", OPT_WANT_CORE, "Write core on errors.", 0, 0, 0, GET_NO_ARG,
    NO_ARG, 0, 0, 0, 0, 0, 0},
-  /* default-storage-engine should have "MyISAM" as def_value. Instead
-     of initializing it here it is done in init_common_variables() due
-     to a compiler bug in Sun Studio compiler. */
+#if !defined(_WIN32) && !defined(EMBEDDED_LIBRARY)
+  {"daemonize", 0,
+  "Detach mysqld from the controlling terminal and put in the background "
+  "as a system daemon. This will only happen when a server is ready to "
+  "accept connections.", &opt_daemonize, &opt_daemonize, 0, GET_BOOL,
+   NO_ARG, 0, 0, 0, 0, 0, 0},
+#endif
 #ifdef DBUG_OFF
   {"debug", '#', "Built in DBUG debugger. Disabled in this build.",
    &current_dbug_option, &current_dbug_option, 0, GET_STR, OPT_ARG,

