File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change 6
6
#include <sys/socket.h>
7
7
#endif
8
8
#include <unistd.h>
9
+ #include <fcntl.h>
9
10
#include "wait_for_single_fd.h"
10
11
11
12
#include "mysql_enc_name_to_ruby.h"
@@ -170,12 +171,23 @@ static void *nogvl_connect(void *ptr) {
170
171
* We do this hack because we want to call mysql_close to release
171
172
* memory, but do not want mysql_close to drop connections in the
172
173
* parent if the socket got shared in fork.
173
- * Returns Qtrue or false (success or failure)
174
+ * Returns Qtrue or Qfalse (success or failure)
174
175
*/
175
176
static VALUE invalidate_fd (int clientfd )
176
177
{
177
- /* TODO: set cloexec flags, atomically if possible */
178
+ #ifdef SOCK_CLOEXEC
179
+ /* Atomically set CLOEXEC on the new FD in case another thread forks */
180
+ int sockfd = socket (AF_UNIX , SOCK_STREAM | SOCK_CLOEXEC , 0 );
181
+ if (sockfd < 0 ) {
182
+ /* Maybe SOCK_CLOEXEC is defined but not available on this kernel */
183
+ int sockfd = socket (AF_UNIX , SOCK_STREAM , 0 );
184
+ fcntl (sockfd , F_SETFD , FD_CLOEXEC );
185
+ }
186
+ #else
187
+ /* Well we don't have SOCK_CLOEXEC, so just set FD_CLOEXEC quickly */
178
188
int sockfd = socket (AF_UNIX , SOCK_STREAM , 0 );
189
+ fcntl (sockfd , F_SETFD , FD_CLOEXEC );
190
+ #endif
179
191
180
192
if (sockfd < 0 ) {
181
193
/*
You can’t perform that action at this time.
0 commit comments