tests: Add tests for the vlserver
[openafs.git] / tests / common / servers.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 #include <roken.h>
5
6 #ifdef HAVE_SYS_WAIT_H
7 #include <sys/wait.h>
8 #endif
9
10 #include "common.h"
11
12 /* Start up the VLserver, using the configuration in dirname, and putting our
13  * logs there too.
14  */
15
16 int
17 afstest_StartVLServer(char *dirname, pid_t *serverPid)
18 {
19     pid_t pid;
20
21     pid = fork();
22     if (pid == -1) {
23         exit(1);
24         /* Argggggghhhhh */
25     } else if (pid == 0) {
26         char *binPath, *logPath, *dbPath, *build;
27
28         /* Child */
29         build = getenv("BUILD");
30
31         if (build == NULL)
32             build = "..";
33
34         asprintf(&binPath, "%s/../src/tvlserver/vlserver", build);
35         asprintf(&logPath, "%s/VLLog", dirname);
36         asprintf(&dbPath, "%s/vldb", dirname);
37         execl(binPath, "vlserver",
38               "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL);
39         fprintf(stderr, "Running %s failed\n", binPath);
40         exit(1);
41     }
42     *serverPid = pid;
43
44     return 0;
45 }
46
47 int
48 afstest_StopVLServer(pid_t serverPid)
49 {
50     int status;
51
52     kill(serverPid, SIGTERM);
53
54     waitpid(serverPid, &status, 0);
55
56     if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
57         fprintf(stderr, "Server died exited on signal %d\n", WTERMSIG(status));
58         return -1;
59     }
60     if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
61         fprintf(stderr, "Server exited with code %d\n", WEXITSTATUS(status));
62         return -1;
63     }
64     return 0;
65 }