41
41
package com .oracle .graal .python .builtins .modules ;
42
42
43
43
import java .io .BufferedReader ;
44
- import java .io .File ;
45
- import java .io .FileReader ;
46
44
import java .io .IOException ;
47
45
import java .net .Inet4Address ;
48
46
import java .net .InetAddress ;
72
70
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
73
71
import com .oracle .graal .python .nodes .util .CastToJavaIntNode ;
74
72
import com .oracle .graal .python .nodes .util .CastToStringNode ;
73
+ import com .oracle .graal .python .runtime .PythonCore ;
75
74
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
76
75
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
77
76
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
77
+ import com .oracle .truffle .api .TruffleFile ;
78
+ import com .oracle .truffle .api .TruffleLanguage ;
78
79
import com .oracle .truffle .api .dsl .Cached ;
79
80
import com .oracle .truffle .api .dsl .Fallback ;
80
81
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
@@ -106,10 +107,10 @@ public Service(int port, String protocol) {
106
107
protected static Map <String , Integer > protocols ;
107
108
108
109
@ TruffleBoundary
109
- private static Map <String , List <Service >> parseServices () {
110
- File services_file = new File ("/etc/services" );
110
+ private static Map <String , List <Service >> parseServices (TruffleLanguage . Env env ) {
111
+ TruffleFile services_file = env . getPublicTruffleFile ("/etc/services" );
111
112
try {
112
- BufferedReader br = new BufferedReader ( new FileReader ( services_file ) );
113
+ BufferedReader br = services_file . newBufferedReader ( );
113
114
String line ;
114
115
Map <String , List <Service >> parsedServices = new HashMap <>();
115
116
while ((line = br .readLine ()) != null ) {
@@ -135,10 +136,10 @@ private static Map<String, List<Service>> parseServices() {
135
136
}
136
137
137
138
@ TruffleBoundary
138
- private static Map <String , Integer > parseProtocols () {
139
- File protocols_file = new File ("/etc/protocols" );
139
+ private static Map <String , Integer > parseProtocols (TruffleLanguage . Env env ) {
140
+ TruffleFile protocols_file = env . getPublicTruffleFile ("/etc/protocols" );
140
141
try {
141
- BufferedReader br = new BufferedReader ( new FileReader ( protocols_file ) );
142
+ BufferedReader br = protocols_file . newBufferedReader ( );
142
143
String line ;
143
144
Map <String , Integer > parsedProtocols = new HashMap <>();
144
145
while ((line = br .readLine ()) != null ) {
@@ -157,9 +158,9 @@ private static Map<String, Integer> parseProtocols() {
157
158
}
158
159
159
160
@ TruffleBoundary
160
- private static String searchServicesForPort (int port , String protocol ) {
161
+ private static String searchServicesForPort (TruffleLanguage . Env env , int port , String protocol ) {
161
162
if (services == null ) {
162
- services = parseServices ();
163
+ services = parseServices (env );
163
164
}
164
165
165
166
Set <String > servicesNames = services .keySet ();
@@ -192,10 +193,13 @@ private static String[] cleanLine(String input) {
192
193
return words ;
193
194
}
194
195
195
- static {
196
+ @ Override
197
+ public void initialize (PythonCore core ) {
198
+ super .initialize (core );
196
199
if (ImageInfo .inImageBuildtimeCode ()) {
197
- services = parseServices ();
198
- protocols = parseProtocols ();
200
+ // we do this eagerly for SVM images
201
+ services = parseServices (core .getContext ().getEnv ());
202
+ protocols = parseProtocols (core .getContext ().getEnv ());
199
203
}
200
204
}
201
205
@@ -356,7 +360,7 @@ public abstract static class GetServByNameNode extends PythonBuiltinNode {
356
360
@ Specialization (guards = {"isNoValue(protocolName)" })
357
361
Object getServByName (String serviceName , @ SuppressWarnings ("unused" ) PNone protocolName ) {
358
362
if (services == null ) {
359
- services = parseServices ();
363
+ services = parseServices (getContext (). getEnv () );
360
364
}
361
365
362
366
List <Service > portsForService = services .get (serviceName );
@@ -371,7 +375,7 @@ Object getServByName(String serviceName, @SuppressWarnings("unused") PNone proto
371
375
@ Specialization
372
376
Object getServByName (String serviceName , String protocolName ) {
373
377
if (services == null ) {
374
- services = parseServices ();
378
+ services = parseServices (getContext (). getEnv () );
375
379
}
376
380
int port = op (serviceName , protocolName );
377
381
if (port >= 0 ) {
@@ -426,7 +430,7 @@ Object getServByPort(int port, String protocolName) {
426
430
if (port < 0 || port > 65535 ) {
427
431
throw raise (PythonBuiltinClassType .OverflowError );
428
432
}
429
- String service = searchServicesForPort (port , protocolName );
433
+ String service = searchServicesForPort (getContext (). getEnv (), port , protocolName );
430
434
if (service != null ) {
431
435
return service ;
432
436
}
@@ -463,7 +467,7 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, Object flagArg,
463
467
464
468
String portServ = String .valueOf (port );
465
469
if ((flags & PSocket .NI_NUMERICSERV ) != PSocket .NI_NUMERICSERV ) {
466
- portServ = searchServicesForPort (port , null );
470
+ portServ = searchServicesForPort (getContext (). getEnv (), port , null );
467
471
if (portServ == null ) {
468
472
throw raise (PythonBuiltinClassType .OSError , "port/proto not found" );
469
473
}
@@ -545,7 +549,7 @@ private Object getAddrInfo(InetAddress[] addresses, String port, int family, int
545
549
throw raise (PythonBuiltinClassType .UnicodeEncodeError );
546
550
}
547
551
if (services == null ) {
548
- services = parseServices ();
552
+ services = parseServices (getContext (). getEnv () );
549
553
}
550
554
List <Service > serviceList = services .get (port );
551
555
return mergeAdressesAndServices (addresses , serviceList , family , type , proto , flags );
@@ -554,7 +558,7 @@ private Object getAddrInfo(InetAddress[] addresses, String port, int family, int
554
558
@ TruffleBoundary
555
559
private Object mergeAdressesAndServices (InetAddress [] adresses , List <Service > serviceList , int family , int type , int proto , int flags ) {
556
560
if (protocols == null ) {
557
- protocols = parseProtocols ();
561
+ protocols = parseProtocols (getContext (). getEnv () );
558
562
}
559
563
List <Object > addressTuples = new ArrayList <>();
560
564
for (InetAddress addr : adresses ) {
0 commit comments