Skip to content

Commit b0b1ca3

Browse files
authored
Merge pull request #292 from tbzatek/errno-fixes-1
tree: nvme_configure_ctrl(): Set errno to zero on success
2 parents 61caac8 + 7985eb0 commit b0b1ca3

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

src/nvme/linux.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,22 @@ static char *__nvme_get_attr(const char *path)
433433
{
434434
char value[4096] = { 0 };
435435
int ret, fd;
436+
int saved_errno;
436437

437438
fd = open(path, O_RDONLY);
438-
if (fd < 0) {
439-
#if 0
440-
nvme_msg(LOG_DEBUG, "Failed to open %s: %s\n", path,
441-
strerror(errno));
442-
#endif
439+
if (fd < 0)
443440
return NULL;
444-
}
445441

446442
ret = read(fd, value, sizeof(value) - 1);
443+
saved_errno = errno;
447444
close(fd);
448-
if (ret < 0 || !strlen(value)) {
445+
if (ret < 0) {
446+
errno = saved_errno;
449447
return NULL;
450448
}
449+
errno = 0;
450+
if (!strlen(value))
451+
return NULL;
451452

452453
if (value[strlen(value) - 1] == '\n')
453454
value[strlen(value) - 1] = '\0';
@@ -463,8 +464,10 @@ char *nvme_get_attr(const char *dir, const char *attr)
463464
int ret;
464465

465466
ret = asprintf(&path, "%s/%s", dir, attr);
466-
if (ret < 0)
467+
if (ret < 0) {
468+
errno = ENOMEM;
467469
return NULL;
470+
}
468471

469472
value = __nvme_get_attr(path);
470473
free(path);

src/nvme/tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
11441144
c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype");
11451145
c->dctype = nvme_get_ctrl_attr(c, "dctype");
11461146

1147+
errno = 0; /* cleanup after nvme_get_ctrl_attr() */
11471148
return 0;
11481149
}
11491150

src/nvme/tree.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,8 @@ int nvme_dump_tree(nvme_root_t r);
11181118
* @d: sysfs directory
11191119
* @attr: sysfs attribute name
11201120
*
1121-
* Return: String with the contents of @attr
1121+
* Return: String with the contents of @attr or %NULL in case of an empty value
1122+
* or in case of an error (indicated by non-zero errno code).
11221123
*/
11231124
char *nvme_get_attr(const char *d, const char *attr);
11241125

@@ -1127,7 +1128,8 @@ char *nvme_get_attr(const char *d, const char *attr);
11271128
* @s: nvme_subsystem_t object
11281129
* @attr: sysfs attribute name
11291130
*
1130-
* Return: String with the contents of @attr
1131+
* Return: String with the contents of @attr or %NULL in case of an empty value
1132+
* or in case of an error (indicated by non-zero errno code).
11311133
*/
11321134
char *nvme_get_subsys_attr(nvme_subsystem_t s, const char *attr);
11331135

@@ -1136,7 +1138,8 @@ char *nvme_get_subsys_attr(nvme_subsystem_t s, const char *attr);
11361138
* @c: Controller instance
11371139
* @attr: sysfs attribute name
11381140
*
1139-
* Return: String with the contents of @attr
1141+
* Return: String with the contents of @attr or %NULL in case of an empty value
1142+
* or in case of an error (indicated by non-zero errno code).
11401143
*/
11411144
char *nvme_get_ctrl_attr(nvme_ctrl_t c, const char *attr);
11421145

@@ -1145,7 +1148,8 @@ char *nvme_get_ctrl_attr(nvme_ctrl_t c, const char *attr);
11451148
* @n: nvme_ns_t object
11461149
* @attr: sysfs attribute name
11471150
*
1148-
* Return: String with the contents of @attr
1151+
* Return: String with the contents of @attr or %NULL in case of an empty value
1152+
* or in case of an error (indicated by non-zero errno code).
11491153
*/
11501154
char *nvme_get_ns_attr(nvme_ns_t n, const char *attr);
11511155

@@ -1164,7 +1168,8 @@ nvme_ns_t nvme_subsystem_lookup_namespace(struct nvme_subsystem *s,
11641168
* @p: nvme_path_t object
11651169
* @attr: sysfs attribute name
11661170
*
1167-
* Return: String with the contents of @attr
1171+
* Return: String with the contents of @attr or %NULL in case of an empty value
1172+
* or in case of an error (indicated by non-zero errno code).
11681173
*/
11691174
char *nvme_get_path_attr(nvme_path_t p, const char *attr);
11701175

0 commit comments

Comments
 (0)