tests: Wait for server start in auth/superuser-t
[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 <tests/tap/basic.h>
15
16 #include "common.h"
17
18 /* Start up the VLserver, using the configuration in dirname, and putting our
19  * logs there too.
20  */
21
22 int
23 afstest_StartVLServer(char *dirname, pid_t *serverPid)
24 {
25     pid_t pid;
26     int status;
27
28     pid = fork();
29     if (pid == -1) {
30         exit(1);
31         /* Argggggghhhhh */
32     } else if (pid == 0) {
33         char *binPath, *logPath, *dbPath, *build;
34
35         /* Child */
36         build = getenv("BUILD");
37
38         if (build == NULL)
39             build = "..";
40
41         if (asprintf(&binPath, "%s/../src/tvlserver/vlserver", build) < 0 ||
42             asprintf(&logPath, "%s/VLLog", dirname) < 0 ||
43             asprintf(&dbPath, "%s/vldb", dirname) < 0) {
44             fprintf(stderr, "Out of memory building vlserver arguments\n");
45             exit(1);
46         }
47         execl(binPath, "vlserver",
48               "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL);
49         fprintf(stderr, "Running %s failed\n", binPath);
50         exit(1);
51     }
52
53     if (waitpid(pid, &status, WNOHANG) != 0) {
54         fprintf(stderr, "Error starting vlserver\n");
55         return -1;
56     }
57
58     diag("Sleeping for a few seconds to let the vlserver startup");
59     sleep(5);
60
61     if (waitpid(pid, &status, WNOHANG) != 0) {
62         fprintf(stderr, "vlserver died during startup\n");
63         return -1;
64     }
65
66     *serverPid = pid;
67
68     return 0;
69 }
70
71 int
72 afstest_StopServer(pid_t serverPid)
73 {
74     int status;
75
76     kill(serverPid, SIGTERM);
77
78     waitpid(serverPid, &status, 0);
79
80     if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
81         fprintf(stderr, "Server died exited on signal %d\n", WTERMSIG(status));
82         return -1;
83     }
84     if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
85         fprintf(stderr, "Server exited with code %d\n", WEXITSTATUS(status));
86         return -1;
87     }
88     return 0;
89 }
90
91 int
92 afstest_StartTestRPCService(const char *configPath,
93                             pid_t signal_pid,
94                             u_short port,
95                             u_short serviceId,
96                             afs_int32 (*proc) (struct rx_call *))
97 {
98     struct afsconf_dir *dir;
99     struct rx_securityClass **classes;
100     afs_int32 numClasses;
101     int code;
102     struct rx_service *service;
103
104     dir = afsconf_Open(configPath);
105     if (dir == NULL) {
106         fprintf(stderr, "Server: Unable to open config directory\n");
107         return -1;
108     }
109
110     code = rx_Init(htons(port));
111     if (code != 0) {
112         fprintf(stderr, "Server: Unable to initialise RX\n");
113         return -1;
114     }
115
116     if (signal_pid != 0) {
117         kill(signal_pid, SIGUSR1);
118     }
119
120     afsconf_BuildServerSecurityObjects(dir, &classes, &numClasses);
121     service = rx_NewService(0, serviceId, "test", classes, numClasses,
122                             proc);
123     if (service == NULL) {
124         fprintf(stderr, "Server: Unable to start to test service\n");
125         return -1;
126     }
127
128     rx_StartServer(1);
129
130     return 0; /* Not reached, we donated ourselves to StartServer */
131 }