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
23 #include <libtest/common.h>
30 #include <sys/types.h>
31 #include <sys/types.h>
35 #include <libtest/killpid.h>
36 #include <libtest/stream.h>
38 using namespace libtest
;
40 bool kill_pid(pid_t pid_arg
)
45 Error
<< "Invalid pid:" << pid_arg
;
49 if ((::kill(pid_arg
, SIGTERM
) == -1))
54 Error
<< "Does someone else have a process running locally for " << int(pid_arg
) << "?";
58 Error
<< "Process " << int(pid_arg
) << " not found.";
63 Error
<< "kill() " << strerror(errno
);
69 if (waitpid(pid_arg
, &status
, 0) == -1)
73 // Just means that the server has already gone away
80 Error
<< "Error occured while waitpid(" << strerror(errno
) << ") on pid " << int(pid_arg
);
88 bool check_pid(const std::string
&filename
)
96 if ((fp
= fopen(filename
.c_str(), "r")))
98 char pid_buffer
[1024];
100 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
105 pid_t pid
= (pid_t
)atoi(pid_buffer
);
108 return (::kill(pid
, 0) == 0);
117 bool kill_file(const std::string
&filename
)
119 if (filename
.empty())
125 if ((fp
= fopen(filename
.c_str(), "r")))
127 char pid_buffer
[1024];
129 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
134 pid_t pid
= (pid_t
)atoi(pid_buffer
);
137 bool ret
= kill_pid(pid
);
138 unlink(filename
.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
148 #define STRINGIFY(x) #x
149 #define TOSTRING(x) STRINGIFY(x)
150 #define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__)
152 pid_t
get_pid_from_file(const std::string
&filename
, std::stringstream
& error_message
)
157 if (filename
.empty())
159 error_message
<< LIBTEST_AT
<< " empty pid file";
163 if ((fp
= fopen(filename
.c_str(), "r")))
165 char pid_buffer
[1024];
167 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
172 ret
= (pid_t
)atoi(pid_buffer
);
175 error_message
<< LIBTEST_AT
<< " Invalid pid was read from file " << filename
;
180 error_message
<< LIBTEST_AT
<< " File " << filename
<< " was empty ";
188 char *current_directory
= getcwd(buffer
, sizeof(buffer
));
189 error_message
<< "Error while opening " << current_directory
<< "/" << filename
<< " " << strerror(errno
);