12 #include <afs/authcon.h>
13 #include <afs/cellconfig.h>
14 #define UBIK_INTERNALS
17 #include <tests/tap/basic.h>
22 check_startup(pid_t pid, char *log, int *a_started, int *a_stopped)
25 struct rx_connection *conn;
26 struct ubik_debug udebug;
31 memset(&udebug, 0, sizeof(udebug));
35 exited = waitpid(pid, &status, WNOHANG);
37 opr_Assert(errno == ECHILD);
41 /* pid is no longer running; vlserver must have died during startup */
46 if (!afstest_file_contains(log, "Starting AFS vlserver")) {
47 /* vlserver hasn't logged the "Starting AFS vlserver" line yet, so it's
48 * presumably still starting up. */
53 * If we're going to write to the db, we also need to wait until recovery
54 * has the UBIK_RECHAVEDB state bit (without that, we won't be able to
55 * start write transactions). If we're just reading from the db, we
56 * wouldn't need to wait for this, but we don't know whether our caller
57 * will be writing to the db or not. It shouldn't take long for
58 * UBIK_RECHAVEDB to get set anyway, so just always check if it's been set
62 conn = rx_NewConnection(afstest_MyHostAddr(), htons(7003), VOTE_SERVICE_ID,
63 rxnull_NewClientSecurityObject(), 0);
64 code = VOTE_XDebug(conn, &udebug, &isclone);
65 rx_DestroyConnection(conn);
67 diag("VOTE_XDebug returned %d while waiting for vlserver startup",
72 if (udebug.amSyncSite && (udebug.recoveryState & UBIK_RECHAVEDB) != 0) {
73 /* Okay, it's set! We have finished startup. */
78 /* Start up the VLserver, using the configuration in dirname, and putting our
83 afstest_StartVLServer(char *dirname, pid_t *serverPid)
93 logPath = afstest_asprintf("%s/VLLog", dirname);
95 /* Create/truncate the log in advance (since we look at it to detect when
96 * the vlserver has started). */
97 fh = fopen(logPath, "w");
98 opr_Assert(fh != NULL);
105 } else if (pid == 0) {
106 char *binPath, *dbPath;
109 binPath = afstest_obj_path("src/tvlserver/vlserver");
110 dbPath = afstest_asprintf("%s/vldb", dirname);
112 execl(binPath, "vlserver",
113 "-logfile", logPath, "-database", dbPath, "-config", dirname, NULL);
114 fprintf(stderr, "Running %s failed\n", binPath);
119 * Wait for the vlserver to startup. Try to check if the vlserver is ready
120 * by checking the log file and the urecovery_state (check_startup()), but
121 * if it's taking too long, just return success anyway. If the vlserver
122 * isn't ready yet, then the caller's tests may fail, but we can't wait
126 diag("waiting for vlserver to startup");
128 usleep(5000); /* 5ms */
129 check_startup(pid, logPath, &started, &stopped);
130 for (try = 0; !started && !stopped; try++) {
132 diag("waited too long for vlserver to finish starting up; "
133 "proceeding anyway");
137 usleep(1000 * 10); /* 10ms */
138 check_startup(pid, logPath, &started, &stopped);
142 fprintf(stderr, "vlserver died during startup\n");
147 diag("vlserver started after try %d", try);
158 afstest_StopServer(pid_t serverPid)
162 kill(serverPid, SIGTERM);
164 if (waitpid(serverPid, &status, 0) < 0) {
168 if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM) {
169 fprintf(stderr, "Server died exited on signal %d\n", WTERMSIG(status));
172 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
173 fprintf(stderr, "Server exited with code %d\n", WEXITSTATUS(status));
180 afstest_StartTestRPCService(const char *configPath,
184 afs_int32 (*proc) (struct rx_call *))
186 struct afsconf_dir *dir;
187 struct rx_securityClass **classes;
188 afs_int32 numClasses;
190 struct rx_service *service;
191 struct afsconf_bsso_info bsso;
193 memset(&bsso, 0, sizeof(bsso));
195 dir = afsconf_Open(configPath);
197 fprintf(stderr, "Server: Unable to open config directory\n");
201 code = rx_Init(htons(port));
203 fprintf(stderr, "Server: Unable to initialise RX\n");
207 if (signal_pid != 0) {
208 kill(signal_pid, SIGUSR1);
212 afsconf_BuildServerSecurityObjects_int(&bsso, &classes, &numClasses);
213 service = rx_NewService(0, serviceId, "test", classes, numClasses,
215 if (service == NULL) {
216 fprintf(stderr, "Server: Unable to start to test service\n");
222 return 0; /* Not reached, we donated ourselves to StartServer */