diff options
author | Noah Watkins <noahwatkins@gmail.com> | 2012-11-30 11:10:27 -0800 |
---|---|---|
committer | Noah Watkins <noahwatkins@gmail.com> | 2013-01-05 11:42:44 -0800 |
commit | e4427f23da30526565aa0524f2e4b014aabecb53 (patch) | |
tree | b8fe49d975ebf3ab878f0d073a20bf9eb3b9305e | |
parent | 73bdd25e59e562b67e1bdc41e878c92830754289 (diff) | |
download | ceph-e4427f23da30526565aa0524f2e4b014aabecb53.tar.gz |
tools: create noinst libtools_common
Creates a non-installed static libtools_common library from
tools/common.cc and moves do_admin_socket(..) into the library from
tools/ceph.cc. This routine is useful in the unit tests for connecting
to and querying an admin socket.
Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/tools/ceph.cc | 61 | ||||
-rw-r--r-- | src/tools/tools.cc | 72 | ||||
-rw-r--r-- | src/tools/tools.h | 8 |
4 files changed, 89 insertions, 60 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index babec35581f..97385af8666 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -101,7 +101,7 @@ ceph_SOURCES = \ tools/ceph.cc \ tools/common.cc \ mon/PGMap.cc -ceph_LDADD = $(LIBGLOBAL_LDA) $(LIBEDIT_LIBS) +ceph_LDADD = libcephtools.la $(LIBGLOBAL_LDA) $(LIBEDIT_LIBS) ceph_CXXFLAGS = ${AM_CXXFLAGS} ceph_conf_SOURCES = ceph_conf.cc @@ -290,6 +290,11 @@ lib_LTLIBRARIES = noinst_LTLIBRARIES = noinst_LIBRARIES = +# tools library +libcephtools_la_SOURCES = tools/tools.cc +libcephtools_la_CXXFLAGS = ${AM_CXXFLAGS} +noinst_LTLIBRARIES += libcephtools.la + # libcephfs libcephfs_la_SOURCES = \ libcephfs.cc @@ -1881,6 +1886,7 @@ noinst_HEADERS = \ rgw/rgw_user.h\ sample.ceph.conf\ tools/common.h\ + tools/tools.h\ test/osd/RadosModel.h\ test/osd/Object.h\ test/osd/TestOpStat.h\ diff --git a/src/tools/ceph.cc b/src/tools/ceph.cc index d5300e69bdd..ec9d4decb58 100644 --- a/src/tools/ceph.cc +++ b/src/tools/ceph.cc @@ -33,6 +33,7 @@ #include "common/safe_io.h" #include "common/config.h" #include "tools/common.h" +#include "tools/tools.h" #include "include/compat.h" #include "include/assert.h" @@ -199,64 +200,6 @@ static int get_indata(const char *in_file, bufferlist &indata) return 0; } -int do_admin_socket(string path, string cmd) -{ - struct sockaddr_un address; - int fd; - int r; - - fd = socket(PF_UNIX, SOCK_STREAM, 0); - if(fd < 0) { - cerr << "socket failed with " << cpp_strerror(errno) << std::endl; - return -1; - } - - memset(&address, 0, sizeof(struct sockaddr_un)); - address.sun_family = AF_UNIX; - snprintf(address.sun_path, UNIX_PATH_MAX, "%s", path.c_str()); - - if (connect(fd, (struct sockaddr *) &address, - sizeof(struct sockaddr_un)) != 0) { - cerr << "connect to " << path << " failed with " << cpp_strerror(errno) << std::endl; - return -1; - } - - char *buf; - uint32_t len; - r = safe_write(fd, cmd.c_str(), cmd.length() + 1); - if (r < 0) { - cerr << "write to " << path << " failed with " << cpp_strerror(errno) << std::endl; - goto out; - } - - r = safe_read(fd, &len, sizeof(len)); - if (r < 0) { - cerr << "read " << len << " length from " << path << " failed with " << cpp_strerror(errno) << std::endl; - goto out; - } - if (r < 4) { - cerr << "read only got " << r << " bytes of 4 expected for response length; invalid command?" << std::endl; - goto out; - } - len = ntohl(len); - - buf = new char[len+1]; - r = safe_read(fd, buf, len); - if (r < 0) { - cerr << "read " << len << " bytes from " << path << " failed with " << cpp_strerror(errno) << std::endl; - goto out; - } - buf[len] = '\0'; - - cout << buf << std::endl; - r = 0; - - out: - ::close(fd); - return r; -} - - int main(int argc, const char **argv) { std::string in_file, out_file; @@ -279,7 +222,7 @@ int main(int argc, const char **argv) // daemon admin socket? if (admin_socket.length()) { - return do_admin_socket(admin_socket, admin_socket_cmd); + return ceph_tool_do_admin_socket(admin_socket, admin_socket_cmd, cout); } // input diff --git a/src/tools/tools.cc b/src/tools/tools.cc new file mode 100644 index 00000000000..45570431da9 --- /dev/null +++ b/src/tools/tools.cc @@ -0,0 +1,72 @@ +#include <sys/stat.h> +#include <sys/socket.h> +#include <linux/un.h> +#include <arpa/inet.h> +#include <string> +#include <iostream> +#include <string.h> +#include <unistd.h> +#include "errno.h" +#include "stdio.h" +#include "common/errno.h" +#include "include/inttypes.h" +#include "common/safe_io.h" +#include "tools/tools.h" +using namespace std; + +int ceph_tool_do_admin_socket(std::string path, std::string cmd, std::ostream& output) +{ + struct sockaddr_un address; + int fd; + int r; + + fd = socket(PF_UNIX, SOCK_STREAM, 0); + if(fd < 0) { + cerr << "socket failed with " << cpp_strerror(errno) << std::endl; + return -1; + } + + memset(&address, 0, sizeof(struct sockaddr_un)); + address.sun_family = AF_UNIX; + snprintf(address.sun_path, UNIX_PATH_MAX, "%s", path.c_str()); + + if (connect(fd, (struct sockaddr *) &address, + sizeof(struct sockaddr_un)) != 0) { + cerr << "connect to " << path << " failed with " << cpp_strerror(errno) << std::endl; + return -1; + } + + char *buf; + uint32_t len; + r = safe_write(fd, cmd.c_str(), cmd.length() + 1); + if (r < 0) { + cerr << "write to " << path << " failed with " << cpp_strerror(errno) << std::endl; + goto out; + } + + r = safe_read(fd, &len, sizeof(len)); + if (r < 0) { + cerr << "read " << len << " length from " << path << " failed with " << cpp_strerror(errno) << std::endl; + goto out; + } + if (r < 4) { + cerr << "read only got " << r << " bytes of 4 expected for response length; invalid command?" << std::endl; + goto out; + } + len = ntohl(len); + + buf = new char[len+1]; + r = safe_read(fd, buf, len); + if (r < 0) { + cerr << "read " << len << " bytes from " << path << " failed with " << cpp_strerror(errno) << std::endl; + goto out; + } + buf[len] = '\0'; + + output << buf << std::endl; + r = 0; + + out: + ::close(fd); + return r; +} diff --git a/src/tools/tools.h b/src/tools/tools.h new file mode 100644 index 00000000000..2cc697da4e5 --- /dev/null +++ b/src/tools/tools.h @@ -0,0 +1,8 @@ +#ifndef CEPH_TOOLS_TOOLS_DOT_H +#define CEPH_TOOLS_TOOLS_DOT_H +#include <string> +#include <ostream> + +int ceph_tool_do_admin_socket(std::string path, std::string cmd, std::ostream& output); + +#endif |