tests: Reformat loopback tests
[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 <rx/rx.h>
11
12 #include <afs/cellconfig.h>
13
14 #include "common.h"
15
16 /* Start up the VLserver, using the configuration in dirname, and putting our
17  * logs there too.
18  */
19
20 int
21 afstest_StartVLServer(char *dirname, pid_t *serverPid)
22 {
23     pid_t pid;
24
25     pid = fork();
26     if (pid == -1) {
27         exit(1);
28         /* Argggggghhhhh */
29     } else if (pid == 0) {
30         char *binPath, *logPath, *dbPath, *build;
31
32         /* Child */
33         build = getenv("BUILD");
34
35         if (build == NULL)
36             build = "..";
37
38         asprintf(&binPath, "%s/../src/tvlserver/vlserver", build);
39         asprintf(&logPath, "%s/VLLog", dirname);
40         asprintf(&dbPath, "%s/vldb", dirname);
41         execl(binPath, "vlserver",
42               "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL);
43         fprintf(stderr, "Running %s failed\n", binPath);
44         exit(1);
45     }
46     *serverPid = pid;
47
48     return 0;
49 }
50
51 int
52 afstest_StopServer(pid_t serverPid)
53 {
54     int status;
55
56     kill(serverPid, SIGTERM);
57
58     waitpid(serverPid, &status, 0);
59
60     if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
61         fprintf(stderr, "Server died exited on signal %d\n", WTERMSIG(status));
62         return -1;
63     }
64     if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
65         fprintf(stderr, "Server exited with code %d\n", WEXITSTATUS(status));
66         return -1;
67     }
68     return 0;
69 }
70
71 int
72 afstest_StartTestRPCService(const char *configPath,
73                             u_short port,
74                             u_short serviceId,
75                             afs_int32 (*proc) (struct rx_call *))
76 {
77     struct afsconf_dir *dir;
78     struct rx_securityClass **classes;
79     afs_int32 numClasses;
80     int code;
81     struct rx_service *service;
82
83     dir = afsconf_Open(configPath);
84     if (dir == NULL) {
85         fprintf(stderr, "Server: Unable to open config directory\n");
86         return -1;
87     }
88
89     code = rx_Init(htons(port));
90     if (code != 0) {
91         fprintf(stderr, "Server: Unable to initialise RX\n");
92         return -1;
93     }
94
95     afsconf_BuildServerSecurityObjects(dir, &classes, &numClasses);
96     service = rx_NewService(0, serviceId, "test", classes, numClasses,
97                             proc);
98     if (service == NULL) {
99         fprintf(stderr, "Server: Unable to start to test service\n");
100         return -1;
101     }
102
103     rx_StartServer(1);
104
105     return 0; /* Not reached, we donated ourselves to StartServer */
106 }