@@ -34,6 +34,7 @@ fd idx - show the given file descriptor
3434 private static readonly F_GETFD : number = 1 ;
3535 private static readonly F_GETPATH : number = 50 ;
3636
37+ private pErrnoLocation : NativePointer | null = null ;
3738 private pOpenDir : NativePointer | null = null ;
3839 private pCloseDir : NativePointer | null = null ;
3940 private pReadDir : NativePointer | null = null ;
@@ -103,6 +104,12 @@ fd idx - show the given file descriptor
103104 )
104105 throw new Error ( 'failed to find necessary native functions' ) ;
105106
107+ // int * __errno_location(void);
108+ const fnErrnoLocation = new SystemFunction (
109+ this . pErrnoLocation as NativePointer ,
110+ 'pointer' ,
111+ [ ] ,
112+ ) ;
106113 // DIR *opendir(const char *name);
107114 const fnOpenDir = new SystemFunction ( this . pOpenDir , 'pointer' , [ 'pointer' ] ) ;
108115 // int closedir(DIR *dirp);
@@ -117,7 +124,15 @@ fd idx - show the given file descriptor
117124 'int' ,
118125 ] ) ;
119126
120- const path = Memory . allocUtf8String ( '/proc/self/fd/' ) ;
127+ const { value : errnoLocation , errno : errnoLocationErr } = fnErrnoLocation ( ) as UnixSystemFunctionResult < NativePointer > ;
128+ if ( errnoLocation . equals ( ptr ( 0 ) ) )
129+ throw new Error (
130+ `Failed to __errno_location, errno: ${ errnoLocationErr } ` ,
131+ ) ;
132+
133+ errnoLocation . writeInt ( 0 ) ;
134+
135+ const path = Memory . allocUtf8String ( '/proc/self/fd' ) ;
121136
122137 const { value : dir , errno : openErrno } = fnOpenDir (
123138 path ,
@@ -237,12 +252,14 @@ fd idx - show the given file descriptor
237252 public override isSupported ( ) : boolean {
238253 switch ( Process . platform ) {
239254 case 'linux' :
255+ this . pErrnoLocation = Module . findGlobalExportByName ( '__errno_location' ) ;
240256 this . pOpenDir = Module . findGlobalExportByName ( 'opendir' ) ;
241257 this . pCloseDir = Module . findGlobalExportByName ( 'closedir' ) ;
242258 this . pReadDir = Module . findGlobalExportByName ( 'readdir' ) ;
243259 this . pReadLink = Module . findGlobalExportByName ( 'readlink' ) ;
244260
245261 if (
262+ this . pErrnoLocation === null ||
246263 this . pOpenDir === null ||
247264 this . pCloseDir === null ||
248265 this . pReadDir === null ||
0 commit comments