Fix race in case of device destruction.

During device destruction it is possible that open() succeed, but
fdevname() return NULL, that can't be assigned to string variable.
Fix that by adding explicit NULL check.

Also while there switch from fdevname() to fdevname_r().

Sponsored by:	iXsystems, Inc.
MFC after:	2 weeks
This commit is contained in:
Alexander Motin 2021-04-13 11:19:10 -04:00
parent 8cca7b7f28
commit e49d3eb403

View File

@ -277,6 +277,7 @@ Event::GetTimestamp() const
bool bool
Event::DevPath(std::string &path) const Event::DevPath(std::string &path) const
{ {
char buf[SPECNAMELEN + 1];
string devName; string devName;
if (!DevName(devName)) if (!DevName(devName))
@ -288,7 +289,11 @@ Event::DevPath(std::string &path) const
return (false); return (false);
/* Normalize the device name in case the DEVFS event is for a link. */ /* Normalize the device name in case the DEVFS event is for a link. */
devName = fdevname(devFd); if (fdevname_r(devFd, buf, sizeof(buf)) == NULL) {
close(devFd);
return (false);
}
devName = buf;
path = _PATH_DEV + devName; path = _PATH_DEV + devName;
close(devFd); close(devFd);