| 1 | /* |
|---|
| 2 | * Utility functions for tests that use remctl. |
|---|
| 3 | * |
|---|
| 4 | * Provides functions to start and stop a remctl daemon that uses the test |
|---|
| 5 | * Kerberos environment and runs on port 14373 instead of the default 4373. |
|---|
| 6 | * |
|---|
| 7 | * Copyright 2006, 2007, 2009 |
|---|
| 8 | * Board of Trustees, Leland Stanford Jr. University |
|---|
| 9 | * |
|---|
| 10 | * See LICENSE for licensing terms. |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | #include <config.h> |
|---|
| 14 | #include <portable/system.h> |
|---|
| 15 | |
|---|
| 16 | #include <signal.h> |
|---|
| 17 | #include <sys/time.h> |
|---|
| 18 | #include <sys/wait.h> |
|---|
| 19 | |
|---|
| 20 | #include <tests/tap/basic.h> |
|---|
| 21 | #include <tests/tap/remctl.h> |
|---|
| 22 | #include <util/util.h> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /* |
|---|
| 26 | * Start remctld. Takes the path to remctld, the principal to use as the |
|---|
| 27 | * server principal and the path to the configuration file to use. Writes the |
|---|
| 28 | * PID file to tests/data/remctl.pid in the BUILD directory and returns the |
|---|
| 29 | * PID file. If anything fails, calls bail(). |
|---|
| 30 | */ |
|---|
| 31 | pid_t |
|---|
| 32 | remctld_start(const char *remctld, const char *principal, const char *config) |
|---|
| 33 | { |
|---|
| 34 | char *pidfile; |
|---|
| 35 | pid_t child; |
|---|
| 36 | struct timeval tv; |
|---|
| 37 | size_t n; |
|---|
| 38 | |
|---|
| 39 | pidfile = concatpath(getenv("BUILD"), "data/remctld.pid"); |
|---|
| 40 | if (access(pidfile, F_OK) == 0) |
|---|
| 41 | if (unlink(pidfile) != 0) |
|---|
| 42 | sysbail("cannot delete %s", pidfile); |
|---|
| 43 | child = fork(); |
|---|
| 44 | if (child < 0) |
|---|
| 45 | sysbail("fork failed"); |
|---|
| 46 | else if (child == 0) { |
|---|
| 47 | if (getenv("VALGRIND") != NULL) |
|---|
| 48 | execl(getenv("VALGRIND"), "valgrind", "--log-file=valgrind.%p", |
|---|
| 49 | "--leak-check=full", remctld, "-m", "-p", "14373", "-s", |
|---|
| 50 | principal, "-P", pidfile, "-f", config, "-d", "-S", "-F", |
|---|
| 51 | (char *) 0); |
|---|
| 52 | else |
|---|
| 53 | execl(remctld, "remctld", "-m", "-p", "14373", "-s", principal, |
|---|
| 54 | "-P", pidfile, "-f", config, "-d", "-S", "-F", (char *) 0); |
|---|
| 55 | _exit(1); |
|---|
| 56 | } else { |
|---|
| 57 | for (n = 0; n < 100 && access(pidfile, F_OK) != 0; n++) { |
|---|
| 58 | tv.tv_sec = (getenv("VALGRIND") != NULL) ? 1 : 0; |
|---|
| 59 | tv.tv_usec = 10000; |
|---|
| 60 | select(0, NULL, NULL, NULL, &tv); |
|---|
| 61 | } |
|---|
| 62 | if (access(pidfile, F_OK) != 0) { |
|---|
| 63 | kill(child, SIGTERM); |
|---|
| 64 | waitpid(child, NULL, 0); |
|---|
| 65 | bail("cannot start remctld"); |
|---|
| 66 | } |
|---|
| 67 | free(pidfile); |
|---|
| 68 | return child; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /* |
|---|
| 74 | * Stop remctld. Takes the PID file of the remctld process. |
|---|
| 75 | */ |
|---|
| 76 | void |
|---|
| 77 | remctld_stop(pid_t child) |
|---|
| 78 | { |
|---|
| 79 | char *pidfile; |
|---|
| 80 | struct timeval tv; |
|---|
| 81 | |
|---|
| 82 | tv.tv_sec = 0; |
|---|
| 83 | tv.tv_usec = 10000; |
|---|
| 84 | select(0, NULL, NULL, NULL, &tv); |
|---|
| 85 | if (waitpid(child, NULL, WNOHANG) == 0) { |
|---|
| 86 | kill(child, SIGTERM); |
|---|
| 87 | waitpid(child, NULL, 0); |
|---|
| 88 | } |
|---|
| 89 | pidfile = concatpath(getenv("BUILD"), "data/remctld.pid"); |
|---|
| 90 | unlink(pidfile); |
|---|
| 91 | free(pidfile); |
|---|
| 92 | } |
|---|