* Libmemcached library
*
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
- * Copyright (C) 2006-2009 Brian Aker All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
#include <config.h>
#include <iostream>
+#include <cstdlib>
+#include <cassert>
#include <libtest/server.h>
#include <libtest/killpid.h>
return output; // for multiple << operators
}
+static void global_sleep(void)
+{
+ static struct timespec global_sleep_value= { 0, 50000 };
+
+#ifdef WIN32
+ sleep(1);
+#else
+ nanosleep(&global_sleep_value, NULL);
+#endif
+}
+
server_st::~server_st()
{
- if (not _used)
+ if (has_pid())
{
kill();
}
}
+bool server_st::start()
+{
+ assert(not _command.empty());
+ assert(not has_pid());
+
+ if (has_pid())
+ return false;
+
+ if (system(_command.c_str()) == -1)
+ return false;
+
+ int count= 30;
+ while (not ping() and --count)
+ {
+ global_sleep();
+ }
+
+ if (count == 0)
+ {
+ return false;
+ }
+
+ _pid= get_pid();
+
+ return has_pid();
+}
+
void server_st::reset_pid()
{
pid_file[0]= 0;
_pid= -1;
}
-bool server_st::kill()
+pid_t server_st::pid()
{
- if (not has_pid() and pid_file[0] == 0)
+ if (not has_pid())
{
- return true;
+ _pid= get_pid();
}
-
- if (has_pid())
+
+ return _pid;
+}
+
+
+bool server_st::kill()
+{
+ if (is_used())
+ return false;
+
+ if ((_pid= get_pid()))
{
- kill_pid(pid());
+ kill_pid(_pid);
if (pid_file[0])
{
unlink(pid_file); // If this happens we may be dealing with a dead server that left its pid file.
return true;
}
+#if 0
else if (pid_file[0])
{
kill_file(pid_file);
return true;
}
+#endif
return false;
}
+
+server_startup_st::~server_startup_st()
+{ }