tests: fix C servers to run with a dynamic log directory

Ref: #10818
This commit is contained in:
Dan Fandrich 2023-03-29 10:23:28 -07:00
parent 1fe081ab42
commit c102502eae
5 changed files with 104 additions and 42 deletions

View File

@ -112,13 +112,15 @@ struct configurable {
int testnum;
};
#define REQUEST_DUMP "log/server.input"
#define REQUEST_DUMP "server.input"
#define CONFIG_VERSION 5
static struct configurable config;
const char *serverlogfile = DEFAULT_LOGFILE;
static const char *configfile = DEFAULT_CONFIG;
const char *logdir = "log";
char loglockfile[256];
#ifdef ENABLE_IPV6
static bool use_ipv6 = FALSE;
@ -504,14 +506,16 @@ static curl_socket_t mqttit(curl_socket_t fd)
char client_id[MAX_CLIENT_ID_LENGTH];
long testno;
FILE *stream = NULL;
FILE *dump;
char dumpfile[256];
static const char protocol[7] = {
0x00, 0x04, /* protocol length */
'M','Q','T','T', /* protocol name */
0x04 /* protocol level */
};
FILE *dump = fopen(REQUEST_DUMP, "ab");
msnprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
dump = fopen(dumpfile, "ab");
if(!dump)
goto end;
@ -772,9 +776,9 @@ static bool incoming(curl_socket_t listenfd)
else {
logmsg("====> Client connect, fd %d. Read config from %s",
newfd, configfile);
set_advisor_read_lock(SERVERLOGS_LOCK);
set_advisor_read_lock(loglockfile);
(void)mqttit(newfd); /* until done */
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
logmsg("====> Client disconnect");
sclose(newfd);
@ -962,6 +966,11 @@ int main(int argc, char *argv[])
if(argc>arg)
serverlogfile = argv[arg++];
}
else if(!strcmp("--logdir", argv[arg])) {
arg++;
if(argc>arg)
logdir = argv[arg++];
}
else if(!strcmp("--ipv6", argv[arg])) {
#ifdef ENABLE_IPV6
ipv_inuse = "IPv6";
@ -997,6 +1006,7 @@ int main(int argc, char *argv[])
" --config [file]\n"
" --version\n"
" --logfile [file]\n"
" --logdir [directory]\n"
" --pidfile [file]\n"
" --portfile [file]\n"
" --ipv4\n"
@ -1006,6 +1016,9 @@ int main(int argc, char *argv[])
}
}
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s",
logdir, SERVERLOGS_LOCK);
#ifdef WIN32
win32_init();
atexit(win32_cleanup);

View File

@ -133,11 +133,13 @@ static void storerequest(char *reqbuf, size_t totalsize);
#endif
const char *serverlogfile = DEFAULT_LOGFILE;
const char *logdir = "log";
char loglockfile[256];
#define RTSPDVERSION "curl test suite RTSP server/0.1"
#define REQUEST_DUMP "log/server.input"
#define RESPONSE_DUMP "log/server.response"
#define REQUEST_DUMP "server.input"
#define RESPONSE_DUMP "server.response"
/* very-big-path support */
#define MAXDOCNAMELEN 140000
@ -604,6 +606,9 @@ static void storerequest(char *reqbuf, size_t totalsize)
size_t written;
size_t writeleft;
FILE *dump;
char dumpfile[256];
msnprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
if(!reqbuf)
return;
@ -611,12 +616,12 @@ static void storerequest(char *reqbuf, size_t totalsize)
return;
do {
dump = fopen(REQUEST_DUMP, "ab");
dump = fopen(dumpfile, "ab");
} while(!dump && ((error = errno) == EINTR));
if(!dump) {
logmsg("Error opening file %s error: %d %s",
REQUEST_DUMP, error, strerror(error));
logmsg("Failed to write request input to " REQUEST_DUMP);
dumpfile, error, strerror(error));
logmsg("Failed to write request input to %s", dumpfile);
return;
}
@ -631,12 +636,12 @@ static void storerequest(char *reqbuf, size_t totalsize)
} while((writeleft > 0) && ((error = errno) == EINTR));
if(writeleft == 0)
logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile);
else if(writeleft > 0) {
logmsg("Error writing file %s error: %d %s",
REQUEST_DUMP, error, strerror(error));
dumpfile, error, strerror(error));
logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
totalsize-writeleft, totalsize, REQUEST_DUMP);
totalsize-writeleft, totalsize, dumpfile);
}
storerequest_cleanup:
@ -646,7 +651,7 @@ storerequest_cleanup:
} while(res && ((error = errno) == EINTR));
if(res)
logmsg("Error closing file %s error: %d %s",
REQUEST_DUMP, error, strerror(error));
dumpfile, error, strerror(error));
}
/* return 0 on success, non-zero on failure */
@ -775,8 +780,11 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
size_t responsesize;
int error = 0;
int res;
static char weare[256];
char responsedump[256];
msnprintf(responsedump, sizeof(responsedump), "%s/%s",
logdir, RESPONSE_DUMP);
logmsg("Send response number %ld part %ld", req->testno, req->partno);
@ -916,12 +924,12 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
else
prevbounce = FALSE;
dump = fopen(RESPONSE_DUMP, "ab");
dump = fopen(responsedump, "ab");
if(!dump) {
error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", RESPONSE_DUMP);
logmsg("couldn't create logfile: " RESPONSE_DUMP);
logmsg("Error opening file: %s", responsedump);
logmsg("couldn't create logfile: %s", responsedump);
free(ptr);
free(cmd);
return -1;
@ -978,7 +986,7 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
} while(res && ((error = errno) == EINTR));
if(res)
logmsg("Error closing file %s error: %d %s",
RESPONSE_DUMP, error, strerror(error));
responsedump, error, strerror(error));
if(got_exit_signal) {
free(ptr);
@ -995,8 +1003,8 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
return -1;
}
logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
responsesize);
logmsg("Response sent (%zu bytes) and written to %s",
responsesize, responsedump);
free(ptr);
if(cmdsize > 0) {
@ -1091,6 +1099,11 @@ int main(int argc, char *argv[])
if(argc>arg)
serverlogfile = argv[arg++];
}
else if(!strcmp("--logdir", argv[arg])) {
arg++;
if(argc>arg)
logdir = argv[arg++];
}
else if(!strcmp("--ipv4", argv[arg])) {
#ifdef ENABLE_IPV6
ipv_inuse = "IPv4";
@ -1125,6 +1138,7 @@ int main(int argc, char *argv[])
puts("Usage: rtspd [option]\n"
" --version\n"
" --logfile [file]\n"
" --logdir [directory]\n"
" --pidfile [file]\n"
" --portfile [file]\n"
" --ipv4\n"
@ -1135,6 +1149,9 @@ int main(int argc, char *argv[])
}
}
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s",
logdir, SERVERLOGS_LOCK);
#ifdef WIN32
win32_init();
atexit(win32_cleanup);
@ -1279,7 +1296,7 @@ int main(int argc, char *argv[])
** logs should not be read until this lock is removed by this server.
*/
set_advisor_read_lock(SERVERLOGS_LOCK);
set_advisor_read_lock(loglockfile);
serverlogslocked = 1;
logmsg("====> Client connect");
@ -1351,7 +1368,7 @@ int main(int argc, char *argv[])
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
if(req.testno == DOCNUMBER_QUIT)
@ -1376,7 +1393,7 @@ server_cleanup:
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
restore_signal_handlers(false);

View File

@ -146,16 +146,18 @@ static void storerequest(const char *reqbuf, size_t totalsize);
#endif
const char *serverlogfile = DEFAULT_LOGFILE;
const char *logdir = "log";
char loglockfile[256];
#define SWSVERSION "curl test suite HTTP server/0.1"
#define REQUEST_DUMP "log/server.input"
#define RESPONSE_DUMP "log/server.response"
#define REQUEST_DUMP "server.input"
#define RESPONSE_DUMP "server.response"
/* when told to run as proxy, we store the logs in different files so that
they can co-exist with the same program running as a "server" */
#define REQUEST_PROXY_DUMP "log/proxy.input"
#define RESPONSE_PROXY_DUMP "log/proxy.response"
#define REQUEST_PROXY_DUMP "proxy.input"
#define RESPONSE_PROXY_DUMP "proxy.response"
/* file in which additional instructions may be found */
#define DEFAULT_CMDFILE "log/ftpserver.cmd"
@ -787,7 +789,10 @@ static void storerequest(const char *reqbuf, size_t totalsize)
size_t written;
size_t writeleft;
FILE *dump;
const char *dumpfile = is_proxy?REQUEST_PROXY_DUMP:REQUEST_DUMP;
char dumpfile[256];
msnprintf(dumpfile, sizeof(dumpfile), "%s/%s",
logdir, is_proxy?REQUEST_PROXY_DUMP:REQUEST_DUMP);
if(!reqbuf)
return;
@ -1011,8 +1016,11 @@ static int send_doc(curl_socket_t sock, struct httprequest *req)
size_t responsesize;
int error = 0;
int res;
const char *responsedump = is_proxy?RESPONSE_PROXY_DUMP:RESPONSE_DUMP;
static char weare[256];
char responsedump[256];
msnprintf(responsedump, sizeof(responsedump), "%s/%s",
logdir, is_proxy?RESPONSE_PROXY_DUMP:RESPONSE_DUMP);
switch(req->rcmd) {
default:
@ -1832,7 +1840,7 @@ static curl_socket_t accept_connection(curl_socket_t sock)
*/
if(!serverlogslocked)
set_advisor_read_lock(SERVERLOGS_LOCK);
set_advisor_read_lock(loglockfile);
serverlogslocked += 1;
logmsg("====> Client connect");
@ -1983,6 +1991,11 @@ int main(int argc, char *argv[])
if(argc>arg)
serverlogfile = argv[arg++];
}
else if(!strcmp("--logdir", argv[arg])) {
arg++;
if(argc>arg)
logdir = argv[arg++];
}
else if(!strcmp("--cmdfile", argv[arg])) {
arg++;
if(argc>arg)
@ -2062,6 +2075,7 @@ int main(int argc, char *argv[])
puts("Usage: sws [option]\n"
" --version\n"
" --logfile [file]\n"
" --logdir [directory]\n"
" --pidfile [file]\n"
" --portfile [file]\n"
" --ipv4\n"
@ -2075,6 +2089,9 @@ int main(int argc, char *argv[])
}
}
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s",
logdir, SERVERLOGS_LOCK);
#ifdef WIN32
win32_init();
atexit(win32_cleanup);
@ -2329,7 +2346,7 @@ int main(int argc, char *argv[])
serverlogslocked -= 1;
if(!serverlogslocked)
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
if(req->testno == DOCNUMBER_QUIT)
goto sws_cleanup;
@ -2375,7 +2392,7 @@ sws_cleanup:
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
restore_signal_handlers(false);

View File

@ -163,7 +163,7 @@ struct bf {
#define DEFAULT_LOGFILE "log/tftpd.log"
#endif
#define REQUEST_DUMP "log/server.input"
#define REQUEST_DUMP "server.input"
#define DEFAULT_PORT 8999 /* UDP */
@ -215,6 +215,8 @@ static bool use_ipv6 = FALSE;
static const char *ipv_inuse = "IPv4";
const char *serverlogfile = DEFAULT_LOGFILE;
const char *logdir = "log";
char loglockfile[256];
static const char *pidname = ".tftpd.pid";
static const char *portname = NULL; /* none by default */
static int serverlogslocked = 0;
@ -300,7 +302,7 @@ static void timer(int signum)
}
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
exit(1);
}
@ -458,7 +460,7 @@ static ssize_t write_behind(struct testcase *test, int convert)
if(!test->ofile) {
char outfile[256];
msnprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
msnprintf(outfile, sizeof(outfile), "%s/upload.%ld", logdir, test->testno);
#ifdef WIN32
test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777);
#else
@ -595,6 +597,11 @@ int main(int argc, char **argv)
if(argc>arg)
serverlogfile = argv[arg++];
}
else if(!strcmp("--logdir", argv[arg])) {
arg++;
if(argc>arg)
logdir = argv[arg++];
}
else if(!strcmp("--ipv4", argv[arg])) {
#ifdef ENABLE_IPV6
ipv_inuse = "IPv4";
@ -629,6 +636,7 @@ int main(int argc, char **argv)
puts("Usage: tftpd [option]\n"
" --version\n"
" --logfile [file]\n"
" --logdir [directory]\n"
" --pidfile [file]\n"
" --portfile [file]\n"
" --ipv4\n"
@ -639,6 +647,9 @@ int main(int argc, char **argv)
}
}
msnprintf(loglockfile, sizeof(loglockfile), "%s/%s",
logdir, SERVERLOGS_LOCK);
#ifdef WIN32
win32_init();
atexit(win32_cleanup);
@ -779,7 +790,7 @@ int main(int argc, char **argv)
break;
}
set_advisor_read_lock(SERVERLOGS_LOCK);
set_advisor_read_lock(loglockfile);
serverlogslocked = 1;
#ifdef ENABLE_IPV6
@ -833,7 +844,7 @@ int main(int argc, char **argv)
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
logmsg("end of one transfer");
@ -861,7 +872,7 @@ tftpd_cleanup:
if(serverlogslocked) {
serverlogslocked = 0;
clear_advisor_read_lock(SERVERLOGS_LOCK);
clear_advisor_read_lock(loglockfile);
}
restore_signal_handlers(true);
@ -895,13 +906,17 @@ static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
#endif
const char *option = "mode"; /* mode is implicit */
int toggle = 1;
FILE *server;
char dumpfile[256];
msnprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
/* Open request dump file. */
FILE *server = fopen(REQUEST_DUMP, "ab");
server = fopen(dumpfile, "ab");
if(!server) {
int error = errno;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", REQUEST_DUMP);
logmsg("Error opening file: %s", dumpfile);
return -1;
}

View File

@ -32,7 +32,7 @@ long timediff(struct timeval newer, struct timeval older);
#define TEST_DATA_PATH "%s/data/test%ld"
#define ALTTEST_DATA_PATH "%s/log/test%ld"
#define SERVERLOGS_LOCK "log/serverlogs.lock"
#define SERVERLOGS_LOCK "serverlogs.lock"
/* global variable, where to find the 'data' dir */
extern const char *path;