cfc9316f34e2b02976060ffb09035077aa07fb5a
[m6w6/libmemcached] / libtest / killpid.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24 #include <libtest/common.h>
25
26 #include <cstdlib>
27 #include <cstring>
28 #include <iostream>
29 #include <sstream>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34
35
36 #include <libtest/killpid.h>
37 #include <libtest/stream.h>
38
39 using namespace libtest;
40
41 bool kill_pid(pid_t pid_arg)
42 {
43 assert(pid_arg > 0);
44 if (pid_arg < 1)
45 {
46 Error << "Invalid pid:" << pid_arg;
47 return false;
48 }
49
50 if ((::kill(pid_arg, SIGTERM) == -1))
51 {
52 switch (errno)
53 {
54 case EPERM:
55 Error << "Does someone else have a process running locally for " << int(pid_arg) << "?";
56 return false;
57
58 case ESRCH:
59 Error << "Process " << int(pid_arg) << " not found.";
60 return false;
61
62 default:
63 case EINVAL:
64 Error << "kill() " << strerror(errno);
65 return false;
66 }
67 }
68
69 int status= 0;
70 if (waitpid(pid_arg, &status, 0) == -1)
71 {
72 switch (errno)
73 {
74 // Just means that the server has already gone away
75 case ECHILD:
76 {
77 return true;
78 }
79 }
80
81 Error << "Error occured while waitpid(" << strerror(errno) << ") on pid " << int(pid_arg);
82
83 return false;
84 }
85
86 return true;
87 }
88
89 bool check_pid(const std::string &filename)
90 {
91 if (filename.empty())
92 {
93 return false;
94 }
95
96 FILE *fp;
97 if ((fp= fopen(filename.c_str(), "r")))
98 {
99 char pid_buffer[1024];
100
101 char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
102 fclose(fp);
103
104 if (ptr)
105 {
106 pid_t pid= (pid_t)atoi(pid_buffer);
107 if (pid > 0)
108 {
109 return (::kill(pid, 0) == 0);
110 }
111 }
112 }
113
114 return false;
115 }
116
117
118 bool kill_file(const std::string &filename)
119 {
120 if (filename.empty())
121 {
122 return true;
123 }
124
125 FILE *fp;
126 if ((fp= fopen(filename.c_str(), "r")))
127 {
128 char pid_buffer[1024];
129
130 char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
131 fclose(fp);
132
133 if (ptr)
134 {
135 pid_t pid= (pid_t)atoi(pid_buffer);
136 if (pid != 0)
137 {
138 bool ret= kill_pid(pid);
139 unlink(filename.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
140
141 return ret;
142 }
143 }
144 }
145
146 return false;
147 }
148
149 #define STRINGIFY(x) #x
150 #define TOSTRING(x) STRINGIFY(x)
151 #define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__)
152
153 pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message)
154 {
155 pid_t ret= -1;
156 FILE *fp;
157
158 if (filename.empty())
159 {
160 error_message << LIBTEST_AT << " empty pid file";
161 return ret;
162 }
163
164 if ((fp= fopen(filename.c_str(), "r")))
165 {
166 char pid_buffer[1024];
167
168 char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
169 fclose(fp);
170
171 if (ptr)
172 {
173 ret= (pid_t)atoi(pid_buffer);
174 if (ret < 1)
175 {
176 error_message << LIBTEST_AT << " Invalid pid was read from file " << filename;
177 }
178 }
179 else
180 {
181 error_message << LIBTEST_AT << " File " << filename << " was empty ";
182 }
183
184 return ret;
185 }
186 else
187 {
188 char buffer[1024];
189 char *current_directory= getcwd(buffer, sizeof(buffer));
190 error_message << "Error while opening " << current_directory << "/" << filename << " " << strerror(errno);
191 }
192
193 return ret;
194 }