Stop leaking fds in pthread_chdir

This was causing EMFILE in applications that frequently use pthread_chdir.
This commit is contained in:
Ariel Abreu 2020-11-27 09:10:58 -05:00
parent d8702a85de
commit 617d552eb8
No known key found for this signature in database
GPG Key ID: ECF8C2B9E8AD3E6B

View File

@ -10,12 +10,16 @@
long sys_pthread_chdir(const char* path)
{
int rv, newfd;
int rv, newfd, oldfd;
newfd = sys_open(path, BSD_O_RDONLY | BSD_O_DIRECTORY | BSD_O_CLOEXEC, 0);
if (newfd < 0)
return newfd;
oldfd = get_perthread_wd();
if (oldfd != LINUX_AT_FDCWD)
close_internal(oldfd);
set_perthread_wd(newfd);
return 0;
}