Skip to content

Commit

Permalink
add unittest_pidfile
Browse files Browse the repository at this point in the history
Fixes: #13422
Signed-off-by: shun song <song.shun3@zte.com.cn>
  • Loading branch information
shun-s committed Jan 8, 2016
1 parent a4eae40 commit af7b0b0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/global/pidfile.cc
Expand Up @@ -51,21 +51,21 @@ struct pidfh {
static struct pidfh pfh;

static int pidfile_verify() {
struct stat sb;
struct stat st;

if (pfh.pf_fd == -1)
return -EINVAL;
/*
* Check remembered descriptor
*/
if (fstat(pfh.pf_fd, &sb) == -1)
if (fstat(pfh.pf_fd, &st) == -1)
return -errno;
if (sb.st_dev != pfh.pf_dev || sb.st_ino != pfh.pf_ino)
if (st.st_dev != pfh.pf_dev || st.st_ino != pfh.pf_ino)
return -ESTALE;
return 0;
}

int pidfile_write()
int pidfile_write(void)
{
int ret;
if (!pfh.pf_path[0])
Expand Down Expand Up @@ -94,7 +94,9 @@ int pidfile_remove(void)
if (!pfh.pf_path[0])
return 0;
if ( (ret=pidfile_verify()) < 0) {
VOID_TEMP_FAILURE_RETRY(::close(pfh.pf_fd));
if (phf.pf_fd != -1) {
::close(pfh.pf_fd);
}
return ret;
}

Expand All @@ -119,7 +121,7 @@ int pidfile_remove(void)
int pidfile_open(const md_config_t *conf)
{
int fd;
struct stat sb;
struct stat st;
if (conf->pid_file.empty()) {
return 0;
}
Expand All @@ -139,19 +141,19 @@ int pidfile_open(const md_config_t *conf)
return -errno;
}

if (fstat(fd, &sb) == -1) {
if (fstat(fd, &st) == -1) {
close(fd);
pfh.close();
return -errno;
}
pfh.pf_fd = fd;
pfh.pf_dev = sb.st_dev;
pfh.pf_ino = sb.st_ino;
pfh.pf_dev = st.st_dev;
pfh.pf_ino = st.st_ino;

struct flock l = { F_WRLCK, SEEK_SET, 0, 0, 0 };
int r = ::fcntl(pfh.pf_fd, F_SETLK, &l);
if (r < 0) {
derr << "failed to lock" << pfh.pf_path << ".is there another process in using?" << dendl;
derr << "failed to lock" << pfh.pf_path << ".is there another process in us?" << dendl;
close(pfh.pf_fd);
pfh.close();
return -errno;
Expand Down
4 changes: 2 additions & 2 deletions src/global/pidfile.h
Expand Up @@ -19,13 +19,13 @@ struct md_config_t;

// Write a pidfile with the current pid, using the configuration in the
// provided conf structure.
int pidfile_write();
int pidfile_write(void);

// Remove the pid file that was previously written by pidfile_write.
// This is safe to call in a signal handler context.
int pidfile_remove(void);

//test whether the pid_file is being used by another process
// test whether the pid_file is being used by another process
int pidfile_open(const md_config_t *conf);

#endif
5 changes: 5 additions & 0 deletions src/test/Makefile.am
Expand Up @@ -148,6 +148,11 @@ unittest_addrs_CXXFLAGS = $(UNITTEST_CXXFLAGS)
unittest_addrs_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
check_TESTPROGRAMS += unittest_addrs

unittest_pidfile_SOURCES = test/test_pidfile.cc
unittest_pidfile_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
unittest_pidfile_CXXFLAGS = $(UNITTEST_CXXFLAGS)
check_PROGRAMS += unittest_pidfile

unittest_blkdev_SOURCES = test/common/test_blkdev.cc
unittest_blkdev_CXXFLAGS = $(UNITTEST_CXXFLAGS)
unittest_blkdev_LDADD = $(UNITTEST_LDADD) $(CEPH_GLOBAL)
Expand Down
52 changes: 52 additions & 0 deletions src/test/test_pidfile.cc
@@ -0,0 +1,52 @@
#include "global/pidfile.h"
#include "common/ceph_argparse.h"
#include "common/config.h"
#include "include/cephfs/libcephfs.h"
#include "include/rados/librados.h"
#include "test/unit.h"

#include <errno.h>
#include <sstream>
#include <string>
#include <string.h>

#include <boost/lexical_cast.hpp>
using std::string
pidfile_path[] = "test.pid";

TEST(Pidfile, NoPidfile) {
md_config_t conf;
conf.pid_file[0] = '\0';
int ret;
ret = pidfile_open(&conf);
ASSERT_EQ(ret, 0);
ret = pidfile_write();
ASSERT_EQ(ret, 0);
ret = pidfile_remove();
ASSERT_EQ(ret, 0);
}

TEST(Pidfile, Normal) {
md_config_t conf;
conf.pid_file = pidfile_path;
int ret;
ret = pidfile_open(&conf);
ASSERT_EQ(ret, 0);
ret = pidfile_write();
ASSERT_EQ(ret, 0);
ret = pidfile_remove();
ASSERT_EQ(ret, 0);
}

TEST(Pidfile, CommendPidfile) {
md_config_t conf;
conf.pid_file = pidfile_path;
int ret;
ret = pidfile_open(&conf);
ASSERT_EQ(ret, 0);
// simulate another daemon want to run with the pidfile
ret = pidfile_open(&conf);
ASSERT_LT(ret, 0);
ret = pidfile_remove();
ASSERT_EQ(ret, 0);
}

0 comments on commit af7b0b0

Please sign in to comment.