1 /* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include "mysqld_daemon.h"
24 #include "mysqld.h"
25 #include "log.h"
26 
27 #include <unistd.h>
28 #ifdef HAVE_SYS_WAIT_H
29 #include <sys/wait.h>
30 #endif
31 
32 /**
33   Daemonize mysqld.
34 
35   This function does sysv style of daemonization of mysqld.
36 
37   @return - returns write end of the pipe file descriptor
38             which is used to notify the parent to exit.
39 */
mysqld_daemonize()40 int mysqld::runtime::mysqld_daemonize()
41 {
42   int pipe_fd[2];
43   if (pipe(pipe_fd) < 0)
44     return -1;
45 
46   pid_t pid= fork();
47   if (pid == -1)
48   {
49     // Error
50     close(pipe_fd[0]);
51     close(pipe_fd[1]);
52     return -1;
53   }
54 
55   if (pid != 0)
56   {
57     // Parent, close write end of pipe.
58     close(pipe_fd[1]);
59 
60     // Wait for first child to fork successfully.
61     int rc,status;
62     char waitstatus;
63     while ((rc= waitpid(pid, &status, 0)) == -1 &&
64            errno == EINTR)
65     {
66       // Retry if errno is EINTR.
67     }
68     if (rc == -1)
69     {
70       fprintf(stderr, "Unable to wait for process %lld\n",
71                       static_cast<long long>(pid));
72       close(pipe_fd[0]);
73       close(pipe_fd[1]);
74       return -1;
75     }
76 
77     // Exit parent on signal from grand child
78     rc= read(pipe_fd[0], &waitstatus, 1);
79     close(pipe_fd[0]);
80 
81     if (rc != 1)
82     {
83       fprintf(stderr, "Unable to determine if daemon is running: %s\n",
84                       strerror(errno));
85       exit(MYSQLD_ABORT_EXIT);
86     }
87     else if (waitstatus != 1)
88     {
89       fprintf(stderr, "Initialization of mysqld failed: %d\n", waitstatus);
90       exit(MYSQLD_ABORT_EXIT);
91     }
92     _exit(MYSQLD_SUCCESS_EXIT);
93   }
94   else
95   {
96     // Child, close read end of pipe file descriptor.
97     close(pipe_fd[0]);
98 
99     int stdinfd;
100     if ((stdinfd= open("/dev/null", O_RDONLY)) <= STDERR_FILENO)
101     {
102       close(pipe_fd[1]);
103       exit(MYSQLD_ABORT_EXIT);
104     }
105 
106     if (! (dup2(stdinfd, STDIN_FILENO) != STDIN_FILENO)
107         && (setsid() > -1))
108     {
109       close(stdinfd);
110       pid_t grand_child_pid= fork();
111       switch (grand_child_pid)
112       {
113         case 0: // Grand child
114           return pipe_fd[1];
115         case -1:
116           close(pipe_fd[1]);
117           _exit(MYSQLD_FAILURE_EXIT);
118         default:
119           _exit(MYSQLD_SUCCESS_EXIT);
120       }
121     }
122     else
123     {
124       close(stdinfd);
125       close(pipe_fd[1]);
126       _exit(MYSQLD_SUCCESS_EXIT);
127     }
128   }
129 }
130 
131 /**
132   Signal parent to exit.
133 
134   @param pipe_write_fd File Descriptor of write end of pipe.
135 
136   @param status status of the initialization done by grand child.
137                 1 means initialization complete and the server
138                   is ready to accept client connections.
139                 0 means intialization aborted due to some failure.
140 
141   @note This function writes the status to write end of pipe.
142   This notifies the parent which is block on read end of pipe.
143 */
signal_parent(int pipe_write_fd,char status)144 void mysqld::runtime::signal_parent(int pipe_write_fd, char status)
145 {
146   if (pipe_write_fd != -1)
147   {
148     while (write(pipe_write_fd, &status, 1) == -1 && errno == EINTR)
149     {
150     // Retry write syscall if errno is EINTR.
151     }
152 
153     close(pipe_write_fd);
154   }
155 }
156