static int
-lookup_binding(int fd, char *map_wwid, char **map_alias)
+lookup_binding(FILE *f, char *map_wwid, char **map_alias)
{
char buf[LINE_MAX];
- FILE *f;
unsigned int line_nr = 0;
- int scan_fd;
int id = 0;
*map_alias = NULL;
- scan_fd = dup(fd);
- if (scan_fd < 0) {
- condlog(0, "Cannot dup bindings file descriptor : %s",
- strerror(errno));
- return -1;
- }
- f = fdopen(scan_fd, "r");
- if (!f) {
- condlog(0, "cannot fdopen on bindings file descriptor : %s",
- strerror(errno));
- close(scan_fd);
- return -1;
- }
+
while (fgets(buf, LINE_MAX, f)) {
char *c, *alias, *wwid;
int curr_id;
if (*map_alias == NULL)
condlog(0, "Cannot copy alias from bindings "
"file : %s", strerror(errno));
- fclose(f);
return id;
}
}
condlog(3, "No matching wwid [%s] in bindings file.", map_wwid);
- fclose(f);
return id;
}
static int
-rlookup_binding(int fd, char **map_wwid, char *map_alias)
+rlookup_binding(FILE *f, char **map_wwid, char *map_alias)
{
char buf[LINE_MAX];
- FILE *f;
unsigned int line_nr = 0;
- int scan_fd;
int id = 0;
*map_wwid = NULL;
- scan_fd = dup(fd);
- if (scan_fd < 0) {
- condlog(0, "Cannot dup bindings file descriptor : %s",
- strerror(errno));
- return -1;
- }
- f = fdopen(scan_fd, "r");
- if (!f) {
- condlog(0, "cannot fdopen on bindings file descriptor : %s",
- strerror(errno));
- close(scan_fd);
- return -1;
- }
+
while (fgets(buf, LINE_MAX, f)) {
char *c, *alias, *wwid;
int curr_id;
if (*map_wwid == NULL)
condlog(0, "Cannot copy alias from bindings "
"file : %s", strerror(errno));
- fclose(f);
return id;
}
}
condlog(3, "No matching alias [%s] in bindings file.", map_alias);
- fclose(f);
return id;
}
get_user_friendly_alias(char *wwid, char *file)
{
char *alias;
- int fd, id;
+ int fd, scan_fd, id;
+ FILE *f;
if (!wwid || *wwid == '\0') {
condlog(3, "Cannot find binding for empty WWID");
fd = open_bindings_file(file);
if (fd < 0)
return NULL;
- id = lookup_binding(fd, wwid, &alias);
+
+ scan_fd = dup(fd);
+ if (scan_fd < 0) {
+ condlog(0, "Cannot dup bindings file descriptor : %s",
+ strerror(errno));
+ close(fd);
+ return NULL;
+ }
+
+ f = fdopen(scan_fd, "r");
+ if (!f) {
+ condlog(0, "cannot fdopen on bindings file descriptor : %s",
+ strerror(errno));
+ close(scan_fd);
+ close(fd);
+ return NULL;
+ }
+
+ id = lookup_binding(f, wwid, &alias);
if (id < 0) {
+ fclose(f);
+ close(scan_fd);
close(fd);
return NULL;
}
+
if (!alias)
alias = allocate_binding(fd, wwid, id);
+ fclose(f);
+ close(scan_fd);
close(fd);
return alias;
}
get_user_friendly_wwid(char *alias, char *file)
{
char *wwid;
- int fd, id;
+ int fd, scan_fd, id;
+ FILE *f;
if (!alias || *alias == '\0') {
condlog(3, "Cannot find binding for empty alias");
fd = open_bindings_file(file);
if (fd < 0)
return NULL;
- id = rlookup_binding(fd, &wwid, alias);
+
+ scan_fd = dup(fd);
+ if (scan_fd < 0) {
+ condlog(0, "Cannot dup bindings file descriptor : %s",
+ strerror(errno));
+ close(fd);
+ return NULL;
+ }
+
+ f = fdopen(scan_fd, "r");
+ if (!f) {
+ condlog(0, "cannot fdopen on bindings file descriptor : %s",
+ strerror(errno));
+ close(scan_fd);
+ close(fd);
+ return NULL;
+ }
+
+ id = rlookup_binding(f, &wwid, alias);
if (id < 0) {
+ fclose(f);
+ close(scan_fd);
close(fd);
return NULL;
}
+ fclose(f);
+ close(scan_fd);
close(fd);
return wwid;
}