Skip to content

Commit 23b228a

Browse files
committed
Native implementation of ctermid()
1 parent 0258c25 commit 23b228a

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ int64_t call_getsid(int64_t pid) {
479479
return getsid(pid);
480480
}
481481

482+
int32_t call_ctermid(char *buf) {
483+
return ctermid(buf) == NULL ? -1 : 0;
484+
}
485+
482486
int32_t get_errno() {
483487
return errno;
484488
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
439439

440440
@Specialization
441441
long getSid(VirtualFrame frame, long pid,
442-
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
442+
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
443443
try {
444444
return posixLib.getsid(getPosixSupport(), pid);
445445
} catch (PosixException e) {
@@ -1989,8 +1989,13 @@ int umask(VirtualFrame frame, int mask,
19891989
@GenerateNodeFactory
19901990
abstract static class CtermId extends PythonBuiltinNode {
19911991
@Specialization
1992-
static String ctermid() {
1993-
return "/dev/tty";
1992+
String ctermid(VirtualFrame frame,
1993+
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib) {
1994+
try {
1995+
return posixLib.ctermid(getPosixSupport());
1996+
} catch (PosixException e) {
1997+
throw raiseOSErrorFromPosixException(frame, e);
1998+
}
19941999
}
19952000
}
19962001

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,12 @@ public long getsid(long pid) {
15841584
throw new UnsupportedPosixFeatureException("Emulated getsid not supported");
15851585
}
15861586

1587+
@ExportMessage
1588+
@SuppressWarnings("static-method")
1589+
public String ctermid() {
1590+
return "/dev/tty";
1591+
}
1592+
15871593
@ExportMessage
15881594
@TruffleBoundary
15891595
public int forkExec(Object[] executables, Object[] args, Object cwd, Object[] env, int stdinReadFd, int stdinWriteFd, int stdoutReadFd, int stdoutWriteFd, int stderrReadFd, int stderrWriteFd,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/LoggingPosixSupport.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,16 @@ final long getsid(long pid,
686686
}
687687
}
688688

689+
@ExportMessage
690+
final String ctermid(@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
691+
logEnter("ctermid", "");
692+
try {
693+
return logExit("ctermid", "%s", lib.ctermid(delegate));
694+
} catch (PosixException e) {
695+
throw logException("ctermid", e);
696+
}
697+
}
698+
689699
@ExportMessage
690700
final int forkExec(Object[] executables, Object[] args, Object cwd, Object[] env, int stdinReadFd, int stdinWriteFd, int stdoutReadFd, int stdoutWriteFd, int stderrReadFd, int stderrWriteFd,
691701
int errPipeReadFd, int errPipeWriteFd, boolean closeFds, boolean restoreSignals, boolean callSetsid, int[] fdsToKeep,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIPosixSupport.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public final class NFIPosixSupport extends PosixSupport {
8989
private static final int UNAME_BUF_LENGTH = 256;
9090
private static final int DIRENT_NAME_BUF_LENGTH = 256;
9191
private static final int PATH_MAX = 4096;
92+
private static final int L_ctermid = 9;
9293

9394
private static final int MAX_READ = Integer.MAX_VALUE / 2;
9495

@@ -151,6 +152,7 @@ private enum PosixNativeFunction {
151152
call_getuid("():sint64"),
152153
call_getppid("():sint64"),
153154
call_getsid("(sint64):sint64"),
155+
call_ctermid("([sint8]):sint32"),
154156
fork_exec("([sint8], [sint64], sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, sint32, [sint32], sint64):sint32");
155157

156158
private final String signature;
@@ -913,6 +915,17 @@ public long getsid(long pid,
913915
return res;
914916
}
915917

918+
@ExportMessage
919+
public String ctermid(@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
920+
byte[] buf = new byte[L_ctermid];
921+
int res = invokeNode.callInt(this, PosixNativeFunction.call_ctermid, wrap(buf));
922+
if (res == -1) {
923+
throw getErrnoAndThrowPosixException(invokeNode);
924+
}
925+
// TODO PyUnicode_DecodeFSDefault
926+
return cStringToJavaString(buf);
927+
}
928+
916929
@ExportMessage
917930
public int forkExec(Object[] executables, Object[] args, Object cwd, Object[] env, int stdinReadFd, int stdinWriteFd, int stdoutReadFd, int stdoutWriteFd, int stderrReadFd, int stderrWriteFd,
918931
int errPipeReadFd, int errPipeWriteFd, boolean closeFds, boolean restoreSignals, boolean callSetsid, int[] fdsToKeep,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PosixSupportLibrary.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ public abstract class PosixSupportLibrary extends Library {
271271

272272
public abstract long getsid(Object receiver, long pid) throws PosixException;
273273

274+
public abstract String ctermid(Object receiver) throws PosixException;
275+
274276
public abstract int forkExec(Object receiver, Object[] executables, Object[] args, Object cwd, Object[] env, int stdinReadFd, int stdinWriteFd, int stdoutReadFd, int stdoutWriteFd,
275277
int stderrReadFd, int stderrWriteFd, int errPipeReadFd, int errPipeWriteFd, boolean closeFds, boolean restoreSignals, boolean callSetsid, int[] fdsToKeep) throws PosixException;
276278

0 commit comments

Comments
 (0)