Make more robust (patch by Larry Doolittle).
This commit is contained in:
		| @@ -39,16 +39,18 @@ extern char *find_real_root_device_name(const char* name) | |||||||
| 	struct dirent *entry; | 	struct dirent *entry; | ||||||
| 	struct stat statBuf, rootStat; | 	struct stat statBuf, rootStat; | ||||||
| 	char *fileName; | 	char *fileName; | ||||||
|  | 	dev_t dev; | ||||||
|  |  | ||||||
| 	if (stat("/", &rootStat) != 0) { | 	if (stat("/", &rootStat) != 0) { | ||||||
| 		error_msg("could not stat '/'"); | 		perror_msg("could not stat '/'"); | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	} | 	} | ||||||
|  | 	if ((dev = rootStat.st_rdev)==0) dev=rootStat.st_dev; | ||||||
|  |  | ||||||
| 	dir = opendir("/dev"); | 	dir = opendir("/dev"); | ||||||
| 	if (!dir) { | 	if (!dir) { | ||||||
| 		error_msg("could not open '/dev'"); | 		perror_msg("could not open '/dev'"); | ||||||
| 		return NULL; | 		goto fallback; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	while((entry = readdir(dir)) != NULL) { | 	while((entry = readdir(dir)) != NULL) { | ||||||
| @@ -64,14 +66,18 @@ extern char *find_real_root_device_name(const char* name) | |||||||
| 		 * devices, so make sure this is a block device */ | 		 * devices, so make sure this is a block device */ | ||||||
| 		if (stat(fileName, &statBuf) == 0 &&  | 		if (stat(fileName, &statBuf) == 0 &&  | ||||||
| 				S_ISBLK(statBuf.st_mode)!=0 && | 				S_ISBLK(statBuf.st_mode)!=0 && | ||||||
| 				statBuf.st_rdev == rootStat.st_rdev) { | 				statBuf.st_rdev == dev) { | ||||||
| 			return fileName;  | 			return fileName;  | ||||||
| 		} | 		} | ||||||
| 		free(fileName); | 		free(fileName); | ||||||
| 	} | 	} | ||||||
| 	closedir(dir); | 	closedir(dir); | ||||||
|  |  | ||||||
| 	return NULL; | fallback: | ||||||
|  | 	/* don't use stack space, caller expects to free() result */ | ||||||
|  | 	fileName=xmalloc(20); | ||||||
|  | 	sprintf(fileName,"(rdev %u)",(unsigned int) rootStat.st_rdev); | ||||||
|  | 	return fileName; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user