1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
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.
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.
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
24 #include <libtest/common.h>
31 #include <sys/types.h>
32 #include <sys/types.h>
36 #include <libtest/killpid.h>
37 #include <libtest/stream.h>
39 using namespace libtest
;
41 bool kill_pid(pid_t pid_arg
)
46 Error
<< "Invalid pid:" << pid_arg
;
50 if ((::kill(pid_arg
, SIGTERM
) == -1))
55 Error
<< "Does someone else have a process running locally for " << int(pid_arg
) << "?";
59 Error
<< "Process " << int(pid_arg
) << " not found.";
64 Error
<< "kill() " << strerror(errno
);
70 if (waitpid(pid_arg
, &status
, 0) == -1)
74 // Just means that the server has already gone away
81 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(pid_arg
);
89 bool check_pid(const std::string
&filename
)
97 if ((fp
= fopen(filename
.c_str(), "r")))
99 char pid_buffer
[1024];
101 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
106 pid_t pid
= (pid_t
)atoi(pid_buffer
);
109 return (::kill(pid
, 0) == 0);
118 bool kill_file(const std::string
&filename
)
120 if (filename
.empty())
126 if ((fp
= fopen(filename
.c_str(), "r")))
128 char pid_buffer
[1024];
130 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
135 pid_t pid
= (pid_t
)atoi(pid_buffer
);
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.
149 #define STRINGIFY(x) #x
150 #define TOSTRING(x) STRINGIFY(x)
151 #define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__)
153 pid_t
get_pid_from_file(const std::string
&filename
, std::stringstream
& error_message
)
158 if (filename
.empty())
160 error_message
<< LIBTEST_AT
<< " empty pid file";
164 if ((fp
= fopen(filename
.c_str(), "r")))
166 char pid_buffer
[1024];
168 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
173 ret
= (pid_t
)atoi(pid_buffer
);
176 error_message
<< LIBTEST_AT
<< " Invalid pid was read from file " << filename
;
181 error_message
<< LIBTEST_AT
<< " File " << filename
<< " was empty ";
189 char *current_directory
= getcwd(buffer
, sizeof(buffer
));
190 error_message
<< "Error while opening " << current_directory
<< "/" << filename
<< " " << strerror(errno
);