Import Upstream version 1.8.5
[hcoop/debian/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 if (asprintf(&binPath, "%s/../src/tvlserver/vlserver", build) < 0 ||
39 asprintf(&logPath, "%s/VLLog", dirname) < 0 ||
40 asprintf(&dbPath, "%s/vldb", dirname) < 0) {
41 fprintf(stderr, "Out of memory building vlserver arguments\n");
42 exit(1);
43 }
44 execl(binPath, "vlserver",
45 "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL);
46 fprintf(stderr, "Running %s failed\n", binPath);
47 exit(1);
48 }
49 *serverPid = pid;
50
51 return 0;
52 }
53
54 int
55 afstest_StopServer(pid_t serverPid)
56 {
57 int status;
58
59 kill(serverPid, SIGTERM);
60
61 waitpid(serverPid, &status, 0);
62
63 if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
64 fprintf(stderr, "Server died exited on signal %d\n", WTERMSIG(status));
65 return -1;
66 }
67 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
68 fprintf(stderr, "Server exited with code %d\n", WEXITSTATUS(status));
69 return -1;
70 }
71 return 0;
72 }
73
74 int
75 afstest_StartTestRPCService(const char *configPath,
76 u_short port,
77 u_short serviceId,
78 afs_int32 (*proc) (struct rx_call *))
79 {
80 struct afsconf_dir *dir;
81 struct rx_securityClass **classes;
82 afs_int32 numClasses;
83 int code;
84 struct rx_service *service;
85
86 dir = afsconf_Open(configPath);
87 if (dir == NULL) {
88 fprintf(stderr, "Server: Unable to open config directory\n");
89 return -1;
90 }
91
92 code = rx_Init(htons(port));
93 if (code != 0) {
94 fprintf(stderr, "Server: Unable to initialise RX\n");
95 return -1;
96 }
97
98 afsconf_BuildServerSecurityObjects(dir, &classes, &numClasses);
99 service = rx_NewService(0, serviceId, "test", classes, numClasses,
100 proc);
101 if (service == NULL) {
102 fprintf(stderr, "Server: Unable to start to test service\n");
103 return -1;
104 }
105
106 rx_StartServer(1);
107
108 return 0; /* Not reached, we donated ourselves to StartServer */
109 }