o ioctl DIOCGDINFO error wasn't checked

o memory wasn't reclaimed in certain cases
o add more msgs under #ifdef DEBUG
o rewrite tangle of for loops for clarity

NB: Open_Disk should redo how it malloc's memory so the caller can free
    everything.  Documentation says the caller can free the disk list to
    reclaim everything but this leaks the indirect strings.  Fixing this
    is simple for the sysctl case but adds complexity to the fallback,
    non-sysctl, case.
This commit is contained in:
Sam Leffler 2002-10-17 18:34:32 +00:00
parent dda5b5a58a
commit c1623066ca
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=105349

View File

@ -92,7 +92,13 @@ Int_Open_Disk(const char *name, u_long size)
} }
memset(&dl, 0, sizeof dl); memset(&dl, 0, sizeof dl);
ioctl(fd, DIOCGDINFO, &dl); if (ioctl(fd, DIOCGDINFO, &dl) < 0) {
#ifdef DEBUG
warn("DIOCGDINFO(%s) failed", device);
#endif
close(fd);
return 0;
}
i = ioctl(fd, DIOCGSLICEINFO, &ds); i = ioctl(fd, DIOCGSLICEINFO, &ds);
if (i < 0) { if (i < 0) {
#ifdef DEBUG #ifdef DEBUG
@ -129,8 +135,13 @@ Int_Open_Disk(const char *name, u_long size)
} }
} }
free (buf); free (buf);
if (sector_size > MAX_SEC_SIZE) if (sector_size > MAX_SEC_SIZE) {
#ifdef DEBUG
warn("Int_Open_Disk: could not determine sector size, "
"calculated %u, max %u\n", sector_size, MAX_SEC_SIZE);
#endif
return NULL; /* could not determine sector size */ return NULL; /* could not determine sector size */
}
#ifdef PC98 #ifdef PC98
p = (unsigned char*)read_block(fd, 1, sector_size); p = (unsigned char*)read_block(fd, 1, sector_size);
@ -508,20 +519,31 @@ Disk_Names()
static char **disks; static char **disks;
int error; int error;
size_t listsize; size_t listsize;
char *disklist, **dp; char *disklist;
disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS)); disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
if (disks == NULL)
return NULL;
memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS)); memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0); error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
if (!error) { if (!error) {
disklist = (char *)malloc(listsize); disklist = (char *)malloc(listsize);
if (disklist == NULL) {
free(disks);
return NULL;
}
memset(disklist, 0, listsize); memset(disklist, 0, listsize);
error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0); error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
if (error) if (error) {
free(disklist);
free(disks);
return NULL; return NULL;
disk_cnt = 0; }
for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) && for (disk_cnt = 0; disk_cnt < MAX_NO_DISKS; disk_cnt++) {
disk_cnt < MAX_NO_DISKS; disk_cnt++, dp++); disks[disk_cnt] = strsep(&disklist, " ");
if (disks[disk_cnt] == NULL)
break;
}
} else { } else {
warn("kern.disks sysctl not available"); warn("kern.disks sysctl not available");
disk_cnt = 0; disk_cnt = 0;