From b00b6f685ad622c91e4b2c0adbcba42647f56b76 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Mon, 22 Aug 2011 10:48:15 -0700 Subject: [PATCH] common/pipe: add function to do pipe2(O_CLOEXEC) Support old systems that don't have O_CLOEXEC. Signed-off-by: Colin McCabe --- src/Makefile.am | 2 ++ src/common/admin_socket.cc | 10 ++++----- src/common/pipe.c | 44 ++++++++++++++++++++++++++++++++++++++ src/common/pipe.h | 33 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/common/pipe.c create mode 100644 src/common/pipe.h diff --git a/src/Makefile.am b/src/Makefile.am index dabc29f714d88..9d051b98c2bfd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -813,6 +813,7 @@ libcommon_files = \ osd/osd_types.cc \ mds/MDSMap.cc \ common/common_init.cc \ + common/pipe.c \ common/ceph_argparse.cc \ common/ceph_context.cc \ common/buffer.cc \ @@ -1020,6 +1021,7 @@ noinst_HEADERS = \ global/global_init.h \ global/global_context.h \ common/common_init.h\ + common/pipe.h\ common/code_environment.h \ common/signal.h\ global/signal_handler.h\ diff --git a/src/common/admin_socket.cc b/src/common/admin_socket.cc index 0e5e8d492653c..9ca231454f773 100644 --- a/src/common/admin_socket.cc +++ b/src/common/admin_socket.cc @@ -12,13 +12,14 @@ * */ -#include "common/admin_socket.h" -#include "common/perf_counters.h" #include "common/Thread.h" +#include "common/admin_socket.h" #include "common/config.h" #include "common/config_obs.h" #include "common/dout.h" #include "common/errno.h" +#include "common/perf_counters.h" +#include "common/pipe.h" #include "common/safe_io.h" #include @@ -110,11 +111,10 @@ public: static std::string create_shutdown_pipe(int *pipe_rd, int *pipe_wr) { int pipefd[2]; - int ret = pipe2(pipefd, O_CLOEXEC); + int ret = pipe_cloexec(pipefd); if (ret < 0) { - int err = errno; ostringstream oss; - oss << "AdminSocket::create_shutdown_pipe error: " << cpp_strerror(err); + oss << "AdminSocket::create_shutdown_pipe error: " << cpp_strerror(ret); return oss.str(); } diff --git a/src/common/pipe.c b/src/common/pipe.c new file mode 100644 index 0000000000000..312f486f7a0c9 --- /dev/null +++ b/src/common/pipe.c @@ -0,0 +1,44 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "common/pipe.h" + +#include +#include +#include +#include + +int pipe_cloexec(int pipefd[2]) +{ +#ifdef O_CLOEXEC + int ret; + ret = pipe2(pipefd, O_CLOEXEC); + if (ret) { + ret = -errno; + return ret; + } + return 0; +#else + /* The old-fashioned, race-condition prone way that we have to fall back on if + * O_CLOEXEC does not exist. */ + int ret = pipe(pipefd); + if (ret) { + ret = -errno; + return ret; + } + fcntl(pipefd[0], F_SETFD, FD_CLOEXEC); + fcntl(pipefd[1], F_SETFD, FD_CLOEXEC); + return 0; +#endif +} diff --git a/src/common/pipe.h b/src/common/pipe.h new file mode 100644 index 0000000000000..b57ec0cafd84c --- /dev/null +++ b/src/common/pipe.h @@ -0,0 +1,33 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_COMMON_PIPE_H +#define CEPH_COMMON_PIPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** Create a pipe and set both ends to have F_CLOEXEC + * + * @param pipefd pipe array, just as in pipe(2) + * @return 0 on success, errno otherwise + */ +int pipe_cloexec(int pipefd[2]); + +#ifdef __cplusplus +} +#endif + +#endif -- 2.39.5