Simplify open_dev() by returning errno values rather than just 0 or 1.
Also remove stat() call and just rely on errno from open() call to discern whether dev node exists or not. Sponsored by: Intel Reviewed by: kib, carl MFC after: 3 days
This commit is contained in:
parent
de89bd6bc2
commit
b378da2781
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=253458
@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -80,7 +81,7 @@ devlist(int argc, char *argv[])
|
|||||||
ret = open_dev(name, &fd, 0, 0);
|
ret = open_dev(name, &fd, 0, 0);
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
if (fd < 0) {
|
if (ret == EACCES) {
|
||||||
warnx("could not open /dev/%s\n", name);
|
warnx("could not open /dev/%s\n", name);
|
||||||
continue;
|
continue;
|
||||||
} else
|
} else
|
||||||
|
@ -163,11 +163,8 @@ read_namespace_data(int fd, int nsid, struct nvme_namespace_data *nsdata)
|
|||||||
int
|
int
|
||||||
open_dev(const char *str, int *fd, int show_error, int exit_on_error)
|
open_dev(const char *str, int *fd, int show_error, int exit_on_error)
|
||||||
{
|
{
|
||||||
struct stat devstat;
|
|
||||||
char full_path[64];
|
char full_path[64];
|
||||||
|
|
||||||
*fd = 0;
|
|
||||||
|
|
||||||
if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
|
if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
|
||||||
if (show_error)
|
if (show_error)
|
||||||
warnx("controller/namespace ids must begin with '%s'",
|
warnx("controller/namespace ids must begin with '%s'",
|
||||||
@ -175,19 +172,10 @@ open_dev(const char *str, int *fd, int show_error, int exit_on_error)
|
|||||||
if (exit_on_error)
|
if (exit_on_error)
|
||||||
exit(1);
|
exit(1);
|
||||||
else
|
else
|
||||||
return (1);
|
return (EINVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(full_path, sizeof(full_path), "/dev/%s", str);
|
snprintf(full_path, sizeof(full_path), "/dev/%s", str);
|
||||||
if (stat(full_path, &devstat) != 0) {
|
|
||||||
if (show_error)
|
|
||||||
warn("could not stat %s", full_path);
|
|
||||||
if (exit_on_error)
|
|
||||||
exit(1);
|
|
||||||
else
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
*fd = open(full_path, O_RDWR);
|
*fd = open(full_path, O_RDWR);
|
||||||
if (*fd < 0) {
|
if (*fd < 0) {
|
||||||
if (show_error)
|
if (show_error)
|
||||||
@ -195,7 +183,7 @@ open_dev(const char *str, int *fd, int show_error, int exit_on_error)
|
|||||||
if (exit_on_error)
|
if (exit_on_error)
|
||||||
exit(1);
|
exit(1);
|
||||||
else
|
else
|
||||||
return (1);
|
return (errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
|
Loading…
Reference in New Issue
Block a user