killpid.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <cstdlib>
  39. #include <cstring>
  40. #include <iostream>
  41. #include <sstream>
  42. #include <signal.h>
  43. #include <sys/types.h>
  44. #include <sys/types.h>
  45. #include <sys/wait.h>
  46. #include <libtest/killpid.h>
  47. #include <libtest/stream.h>
  48. using namespace libtest;
  49. bool kill_pid(pid_t pid_arg)
  50. {
  51. assert(pid_arg > 0);
  52. if (pid_arg < 1)
  53. {
  54. Error << "Invalid pid:" << pid_arg;
  55. return false;
  56. }
  57. if ((::kill(pid_arg, SIGTERM) == -1))
  58. {
  59. switch (errno)
  60. {
  61. case EPERM:
  62. Error << "Does someone else have a process running locally for " << int(pid_arg) << "?";
  63. return false;
  64. case ESRCH:
  65. Error << "Process " << int(pid_arg) << " not found.";
  66. return false;
  67. default:
  68. case EINVAL:
  69. Error << "kill() " << strerror(errno);
  70. return false;
  71. }
  72. }
  73. {
  74. uint32_t this_wait= 0;
  75. uint32_t timeout= 20; // This number should be high enough for valgrind startup (which is slow)
  76. uint32_t waited;
  77. uint32_t retry;
  78. for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
  79. {
  80. int status= 0;
  81. if (waitpid(pid_arg, &status, WNOHANG) == 0)
  82. {
  83. break;
  84. }
  85. else if (errno == ECHILD)
  86. {
  87. // Server has already gone away
  88. break;
  89. }
  90. else if (waited >= timeout)
  91. {
  92. // Timeout failed
  93. kill(pid_arg, SIGKILL);
  94. break;
  95. }
  96. this_wait= retry * retry / 3 + 1;
  97. libtest::dream(this_wait, 0);
  98. }
  99. }
  100. return true;
  101. }
  102. bool check_pid(const std::string &filename)
  103. {
  104. if (filename.empty())
  105. {
  106. return false;
  107. }
  108. FILE *fp;
  109. if ((fp= fopen(filename.c_str(), "r")))
  110. {
  111. libtest::vchar_t pid_buffer;
  112. pid_buffer.resize(1024);
  113. char *ptr= fgets(&pid_buffer[0], int(pid_buffer.size()), fp);
  114. fclose(fp);
  115. if (ptr)
  116. {
  117. pid_t pid= (pid_t)atoi(&pid_buffer[0]);
  118. if (pid > 0)
  119. {
  120. return (::kill(pid, 0) == 0);
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. bool kill_file(const std::string &filename)
  127. {
  128. if (filename.empty())
  129. {
  130. return true;
  131. }
  132. FILE *fp;
  133. if ((fp= fopen(filename.c_str(), "r")))
  134. {
  135. libtest::vchar_t pid_buffer;
  136. pid_buffer.resize(1024);
  137. char *ptr= fgets(&pid_buffer[0], int(pid_buffer.size()), fp);
  138. fclose(fp);
  139. if (ptr)
  140. {
  141. pid_t pid= (pid_t)atoi(&pid_buffer[0]);
  142. if (pid != 0)
  143. {
  144. bool ret= kill_pid(pid);
  145. unlink(filename.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
  146. return ret;
  147. }
  148. }
  149. }
  150. return false;
  151. }
  152. #define STRINGIFY(x) #x
  153. #define TOSTRING(x) STRINGIFY(x)
  154. #define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__)
  155. pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message)
  156. {
  157. pid_t ret= -1;
  158. if (filename.empty())
  159. {
  160. error_message << LIBTEST_AT << " empty pid file";
  161. return ret;
  162. }
  163. FILE *fp;
  164. if ((fp= fopen(filename.c_str(), "r")))
  165. {
  166. libtest::vchar_t pid_buffer;
  167. pid_buffer.resize(1024);
  168. char *ptr= fgets(&pid_buffer[0], int(pid_buffer.size()), fp);
  169. if (ptr)
  170. {
  171. ret= (pid_t)atoi(&pid_buffer[0]);
  172. if (ret < 1)
  173. {
  174. error_message << LIBTEST_AT << " Invalid pid was read from file " << filename;
  175. }
  176. }
  177. else
  178. {
  179. error_message << LIBTEST_AT << " File " << filename << " was empty ";
  180. }
  181. fclose(fp);
  182. return ret;
  183. }
  184. else
  185. {
  186. libtest::vchar_t buffer;
  187. buffer.resize(1024);
  188. char *current_directory= getcwd(&buffer[0], buffer.size());
  189. error_message << "Error while opening " << current_directory << "/" << filename << " " << strerror(errno);
  190. }
  191. return ret;
  192. }