Skip to content

Commit 15b2cc7

Browse files
devnexennikic
authored andcommitted
Implements an openpty wrapper for solaris based systems
This is only used by proc_open pty support, and as such declared directly there.
1 parent 727ae51 commit 15b2cc7

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

ext/standard/proc_open.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,63 @@ extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
5656
/* Mac OS X (and some BSDs) define `openpty` in <util.h> */
5757
# include <util.h>
5858
# endif
59+
#elif defined(__sun)
60+
# include <fcntl.h>
61+
# include <stropts.h>
62+
# include <termios.h>
63+
# define HAVE_OPENPTY 1
64+
65+
/* Solaris/Illumos does not have any openpty implementation */
66+
int openpty(int *master, int *slave, char *name, struct termios *termp, struct winsize *winp)
67+
{
68+
int fd, sd;
69+
const char *slaveid;
70+
71+
assert(master);
72+
assert(slave);
73+
74+
sd = *master = *slave = -1;
75+
fd = open("/dev/ptmx", O_NOCTTY|O_RDWR);
76+
if (fd == -1) {
77+
return -1;
78+
}
79+
/* Checking if we can have to the pseudo terminal */
80+
if (grantpt(fd) != 0 || unlockpt(fd) != 0) {
81+
goto fail;
82+
}
83+
slaveid = ptsname(fd);
84+
if (!slaveid) {
85+
goto fail;
86+
}
87+
88+
/* Getting the slave path and pushing pseudo terminal */
89+
sd = open(slaveid, O_NOCTTY|O_RDONLY);
90+
if (sd == -1 || ioctl(sd, I_PUSH, "ptem") == -1) {
91+
goto fail;
92+
}
93+
if (termp) {
94+
if (tcgetattr(sd, termp) < 0) {
95+
goto fail;
96+
}
97+
}
98+
if (winp) {
99+
if (ioctl(sd, TIOCSWINSZ, winp) == -1) {
100+
goto fail;
101+
}
102+
}
103+
104+
*slave = sd;
105+
*master = fd;
106+
return 0;
107+
fail:
108+
if (sd != -1) {
109+
close(sd);
110+
}
111+
if (fd != -1) {
112+
close(fd);
113+
}
114+
return -1;
115+
}
59116
#endif
60117

61118
#include "proc_open.h"

0 commit comments

Comments
 (0)