… | |
… | |
64 | /* |
64 | /* |
65 | * Returns pty file descriptor, or -1 on failure |
65 | * Returns pty file descriptor, or -1 on failure |
66 | * If successful, ttydev is set to the name of the slave device. |
66 | * If successful, ttydev is set to the name of the slave device. |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
68 | */ |
68 | */ |
69 | static int |
69 | static inline int |
70 | get_pty (int *fd_tty, char **ttydev) |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
71 | { |
71 | { |
|
|
72 | #ifdef NO_SETOWNER_TTYDEV |
72 | int pfd; |
73 | int pfd; |
73 | |
74 | |
|
|
75 | # ifdef PTYS_ARE_GETPT |
|
|
76 | pfd = getpt(); |
|
|
77 | # else |
|
|
78 | # ifdef PTYS_ARE_POSIX |
|
|
79 | pfd = posix_openpt (O_RDWR); |
|
|
80 | # else |
|
|
81 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
|
|
82 | # endif |
|
|
83 | # endif |
|
|
84 | if (pfd >= 0) |
|
|
85 | { |
|
|
86 | if (grantpt (pfd) == 0 /* change slave permissions */ |
|
|
87 | && unlockpt (pfd) == 0) |
|
|
88 | { /* slave now unlocked */ |
|
|
89 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
|
|
90 | return pfd; |
|
|
91 | } |
|
|
92 | close (pfd); |
|
|
93 | } |
|
|
94 | #endif |
|
|
95 | return -1; |
|
|
96 | } |
|
|
97 | |
|
|
98 | static inline int |
|
|
99 | get_pty_openpty (int *fd_tty, char **ttydev) |
|
|
100 | { |
74 | #ifdef PTYS_ARE_OPENPTY |
101 | #ifdef PTYS_ARE_OPENPTY |
|
|
102 | int pfd; |
|
|
103 | int res; |
75 | char tty_name[sizeof "/dev/pts/????\0"]; |
104 | char tty_name[sizeof "/dev/pts/????\0"]; |
76 | |
105 | |
77 | int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
106 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
78 | |
|
|
79 | if (res != -1) |
107 | if (res != -1) |
80 | { |
108 | { |
81 | *ttydev = strdup (tty_name); |
109 | *ttydev = strdup (tty_name); |
82 | return pfd; |
110 | return pfd; |
83 | } |
111 | } |
84 | #endif |
112 | #endif |
|
|
113 | return -1; |
|
|
114 | } |
85 | |
115 | |
|
|
116 | static inline int |
|
|
117 | get_pty__getpty (int *fd_tty, char **ttydev) |
|
|
118 | { |
86 | #ifdef PTYS_ARE__GETPTY |
119 | #ifdef PTYS_ARE__GETPTY |
|
|
120 | int pfd; |
|
|
121 | |
87 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
122 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
88 | if (*ttydev != NULL) |
123 | if (*ttydev != NULL) |
89 | return pfd; |
124 | return pfd; |
90 | #endif |
125 | #endif |
|
|
126 | return -1; |
|
|
127 | } |
91 | |
128 | |
92 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
129 | static inline int |
93 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) |
130 | get_pty_ptc (int *fd_tty, char **ttydev) |
94 | |
131 | { |
95 | { |
|
|
96 | # ifdef PTYS_ARE_GETPT |
|
|
97 | pfd = getpt(); |
|
|
98 | # else |
|
|
99 | # ifdef PTYS_ARE_POSIX |
|
|
100 | pfd = posix_openpt (O_RDWR); |
|
|
101 | # else |
|
|
102 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
|
|
103 | # endif |
|
|
104 | # endif |
|
|
105 | |
|
|
106 | if (pfd >= 0) |
|
|
107 | { |
|
|
108 | if (grantpt (pfd) == 0 /* change slave permissions */ |
|
|
109 | && unlockpt (pfd) == 0) |
|
|
110 | { /* slave now unlocked */ |
|
|
111 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
|
|
112 | return pfd; |
|
|
113 | } |
|
|
114 | close (pfd); |
|
|
115 | } |
|
|
116 | } |
|
|
117 | # endif |
|
|
118 | #endif |
|
|
119 | |
|
|
120 | #ifdef PTYS_ARE_PTC |
132 | #ifdef PTYS_ARE_PTC |
|
|
133 | int pfd; |
|
|
134 | |
121 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
135 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
122 | { |
136 | { |
123 | *ttydev = strdup (ttyname (pfd)); |
137 | *ttydev = strdup (ttyname (pfd)); |
124 | return pfd; |
138 | return pfd; |
125 | } |
139 | } |
126 | #endif |
140 | #endif |
|
|
141 | return -1; |
|
|
142 | } |
127 | |
143 | |
|
|
144 | static inline int |
|
|
145 | get_pty_clone (int *fd_tty, char **ttydev) |
|
|
146 | { |
128 | #ifdef PTYS_ARE_CLONE |
147 | #ifdef PTYS_ARE_CLONE |
|
|
148 | int pfd; |
|
|
149 | |
129 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
150 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
130 | { |
151 | { |
131 | *ttydev = strdup (ptsname (pfd)); |
152 | *ttydev = strdup (ptsname (pfd)); |
132 | return pfd; |
153 | return pfd; |
133 | } |
154 | } |
134 | #endif |
155 | #endif |
|
|
156 | return -1; |
|
|
157 | } |
135 | |
158 | |
|
|
159 | static inline int |
|
|
160 | get_pty_numeric (int *fd_tty, char **ttydev) |
|
|
161 | { |
136 | #ifdef PTYS_ARE_NUMERIC |
162 | #ifdef PTYS_ARE_NUMERIC |
137 | { |
163 | int pfd; |
138 | int idx; |
164 | int idx; |
139 | char *c1, *c2; |
165 | char *c1, *c2; |
140 | char pty_name[] = "/dev/ptyp???"; |
166 | char pty_name[] = "/dev/ptyp???"; |
141 | char tty_name[] = "/dev/ttyp???"; |
167 | char tty_name[] = "/dev/ttyp???"; |
142 | |
168 | |
… | |
… | |
161 | } |
187 | } |
162 | |
188 | |
163 | close (pfd); |
189 | close (pfd); |
164 | } |
190 | } |
165 | } |
191 | } |
166 | } |
|
|
167 | #endif |
192 | #endif |
|
|
193 | return -1; |
|
|
194 | } |
168 | |
195 | |
|
|
196 | static inline int |
|
|
197 | get_pty_searched (int *fd_tty, char **ttydev) |
|
|
198 | { |
169 | #ifdef PTYS_ARE_SEARCHED |
199 | #ifdef PTYS_ARE_SEARCHED |
170 | { |
200 | # ifndef PTYCHAR1 |
|
|
201 | # define PTYCHAR1 "pqrstuvwxyz" |
|
|
202 | # endif |
|
|
203 | # ifndef PTYCHAR2 |
|
|
204 | # define PTYCHAR2 "0123456789abcdef" |
|
|
205 | # endif |
|
|
206 | int pfd; |
171 | const char *c1, *c2; |
207 | const char *c1, *c2; |
172 | char pty_name[] = "/dev/pty??"; |
208 | char pty_name[] = "/dev/pty??"; |
173 | char tty_name[] = "/dev/tty??"; |
209 | char tty_name[] = "/dev/tty??"; |
174 | |
|
|
175 | # ifndef PTYCHAR1 |
|
|
176 | # define PTYCHAR1 "pqrstuvwxyz" |
|
|
177 | # endif |
|
|
178 | # ifndef PTYCHAR2 |
|
|
179 | # define PTYCHAR2 "0123456789abcdef" |
|
|
180 | # endif |
|
|
181 | |
210 | |
182 | for (c1 = PTYCHAR1; *c1; c1++) |
211 | for (c1 = PTYCHAR1; *c1; c1++) |
183 | { |
212 | { |
184 | pty_name[ (sizeof (pty_name) - 3)] = |
213 | pty_name[ (sizeof (pty_name) - 3)] = |
185 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
214 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
… | |
… | |
197 | |
226 | |
198 | close (pfd); |
227 | close (pfd); |
199 | } |
228 | } |
200 | } |
229 | } |
201 | } |
230 | } |
202 | } |
|
|
203 | #endif |
231 | #endif |
|
|
232 | return -1; |
|
|
233 | } |
204 | |
234 | |
|
|
235 | static int |
|
|
236 | get_pty (int *fd_tty, char **ttydev) |
|
|
237 | { |
|
|
238 | int pfd; |
|
|
239 | |
|
|
240 | if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1 |
|
|
241 | || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1 |
|
|
242 | || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1 |
|
|
243 | || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1 |
|
|
244 | || (pfd = get_pty_clone (fd_tty, ttydev)) != -1 |
|
|
245 | || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 |
|
|
246 | || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) |
|
|
247 | return pfd; |
205 | return -1; |
248 | return -1; |
206 | } |
249 | } |
207 | |
250 | |
208 | /*----------------------------------------------------------------------*/ |
251 | /*----------------------------------------------------------------------*/ |
209 | /* |
252 | /* |