nvttest: Enable specifying device

Also check that the device file exists.
This commit is contained in:
Kim Woelders
2024-11-10 11:27:46 +01:00
parent 611ab3cbf3
commit 3bcbf9c8c1
+14 -2
View File
@@ -7,6 +7,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -20,6 +21,7 @@
static const char help_text[] =
"Usage: nvttest OPTIONS...\n"
"OPTIONS:\n"
" -D name : Specify device name (/dev/name, default /dev/ttyNVT0)\n"
" -d : Enable debug\n"
" -n loops : Test loops per thread\n"
" -q : Quiet\n"
@@ -28,7 +30,7 @@ static const char help_text[] =
;
/**INDENT-ON**/
static const char *dev = "/dev/ttyNVT0";
static char dev[64] = "/dev/ttyNVT0";
typedef struct {
int nthread;
@@ -126,18 +128,22 @@ int main(int argc, char **argv)
topts_t topts = { }, *popts;
pthread_t *ptids;
int debug, quiet;
struct stat st;
debug = quiet = 0;
nthr = 1;
topts.nloop = 1;
while ((opt = getopt(argc, argv, "dn:qrt:")) != -1)
while ((opt = getopt(argc, argv, "D:dn:qrt:")) != -1)
{
switch (opt)
{
default: /* '?' */
_usage(1);
break;
case 'D':
snprintf(dev, sizeof(dev), "/dev/%s", optarg);
break;
case 'd':
debug += 1;
break;
@@ -156,6 +162,12 @@ int main(int argc, char **argv)
}
}
if (stat(dev, &st) || !S_ISCHR(st.st_mode))
{
fprintf(stderr, "Invalid device file: '%s'\n", dev);
exit(EXIT_FAILURE);
}
nvt_log_level(quiet, debug);
nvt_log_dest(NVT_LOG_STDOUT);