8a1543f4892968a05235bce05690815ba9dc5453
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
);
89 pid_t
kill_file(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
);
110 unlink(filename
.c_str()); // If this happens we may be dealing with a dead server that left its pid file.
118 #define STRINGIFY(x) #x
119 #define TOSTRING(x) STRINGIFY(x)
120 #define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__)
122 pid_t
get_pid_from_file(const std::string
&filename
, std::stringstream
& error_message
)
127 if (filename
.empty())
129 error_message
<< LIBTEST_AT
<< " empty pid file";
133 if ((fp
= fopen(filename
.c_str(), "r")))
135 char pid_buffer
[1024];
137 char *ptr
= fgets(pid_buffer
, sizeof(pid_buffer
), fp
);
142 ret
= (pid_t
)atoi(pid_buffer
);
145 error_message
<< LIBTEST_AT
<< " Invalid pid was read from file " << filename
;
150 error_message
<< LIBTEST_AT
<< " File " << filename
<< " was empty ";
158 char *current_directory
= getcwd(buffer
, sizeof(buffer
));
159 error_message
<< "Error while opening " << current_directory
<< "/" << filename
<< " " << strerror(errno
);