From: Colin Patrick McCabe Date: Mon, 22 Aug 2011 17:48:15 +0000 (-0700) Subject: common/pipe: add function to do pipe2(O_CLOEXEC) X-Git-Tag: v0.35~286 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=abe5cd28d1e1e1e1684cd227cf9f9272202bea8d;p=ceph.git common/pipe: add function to do pipe2(O_CLOEXEC) Support old systems that don't have O_CLOEXEC. Signed-off-by: Colin McCabe --- diff --git a/src/Makefile.am b/src/Makefile.am index 95ebf17d1ec2..3869f546b437 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -817,6 +817,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 \ @@ -1024,6 +1025,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 0e5e8d492653..9ca231454f77 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 000000000000..312f486f7a0c --- /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 000000000000..b57ec0cafd84 --- /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