aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2015-09-10 11:22:49 +0200
committerJakub Jelen <jjelen@redhat.com>2018-10-02 16:34:02 +0200
commit96e04d4691263649031a4300c9d41803fc4bc0bf (patch)
tree2495cc0519b820a8f6d41b4bb9d0a501335e9783
parent7113074ae4cfbd13c7a1b9511739cb880daf2424 (diff)
downloadlibssh-96e04d4691263649031a4300c9d41803fc4bc0bf.tar.gz
libssh-96e04d4691263649031a4300c9d41803fc4bc0bf.tar.xz
libssh-96e04d4691263649031a4300c9d41803fc4bc0bf.zip
torture: Create a torture_terminate_process() function
Signed-off-by: Andreas Schneider <asn@cryptomilk.org> (cherry-picked from commit ae89b6c0)
-rw-r--r--tests/torture.c79
-rw-r--r--tests/torture.h2
2 files changed, 47 insertions, 34 deletions
diff --git a/tests/torture.c b/tests/torture.c
index 42e1d74b..b4d73c6b 100644
--- a/tests/torture.c
+++ b/tests/torture.c
@@ -344,6 +344,47 @@ int torture_isdir(const char *path) {
return 0;
}
+int torture_terminate_process(const char *pidfile)
+{
+ char buf[8] = {0};
+ long int tmp;
+ ssize_t rc;
+ pid_t pid;
+ int fd;
+
+ /* read the pidfile */
+ fd = open(pidfile, O_RDONLY);
+ if (fd < 0) {
+ return -1;
+ }
+
+ rc = read(fd, buf, sizeof(buf));
+ close(fd);
+ if (rc <= 0) {
+ return -1;
+ }
+
+ buf[sizeof(buf) - 1] = '\0';
+
+ tmp = strtol(buf, NULL, 10);
+ if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) {
+ return -1;
+ }
+
+ pid = (pid_t)(tmp & 0xFFFF);
+
+ /* Make sure the process goes away! */
+ kill(pid, SIGTERM);
+
+ kill(pid, 0);
+ if (rc == 0) {
+ fprintf(stderr,
+ "WARNING: The process server is still running!\n");
+ }
+
+ return 0;
+}
+
ssh_session torture_ssh_session(const char *host,
const unsigned int *port,
const char *user,
@@ -824,43 +865,13 @@ void torture_teardown_socket_dir(void **state)
void torture_teardown_sshd_server(void **state)
{
struct torture_state *s = *state;
- char buf[8] = {0};
- long int tmp;
- ssize_t rc;
- pid_t pid;
- int fd;
-
- /* read the pidfile */
- fd = open(s->srv_pidfile, O_RDONLY);
- if (fd < 0) {
- goto done;
- }
-
- rc = read(fd, buf, sizeof(buf));
- close(fd);
- if (rc <= 0) {
- goto done;
- }
-
- buf[sizeof(buf) - 1] = '\0';
-
- tmp = strtol(buf, NULL, 10);
- if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) {
- goto done;
- }
-
- pid = (pid_t)(tmp & 0xFFFF);
-
- /* Make sure the daemon goes away! */
- kill(pid, SIGTERM);
+ int rc;
- kill(pid, 0);
- if (rc == 0) {
- fprintf(stderr,
- "WARNING the sshd server is still running!\n");
+ rc = torture_terminate_process(s->srv_pidfile);
+ if (rc != 0) {
+ fprintf(stderr, "Failed to terminate sshd");
}
-done:
torture_teardown_socket_dir(state);
}
diff --git a/tests/torture.h b/tests/torture.h
index 1177bc3c..643e4539 100644
--- a/tests/torture.h
+++ b/tests/torture.h
@@ -76,6 +76,8 @@ void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments);
int torture_rmdirs(const char *path);
int torture_isdir(const char *path);
+int torture_terminate_process(const char *pidfile);
+
/*
* Returns the verbosity level asked by user
*/