~dannf/ubuntu/saucy/screen/lp1213278-from-debian

« back to all changes in this revision

Viewing changes to pty.c

  • Committer: Bazaar Package Importer
  • Author(s): Nathaniel McCallum
  • Date: 2004-09-03 15:15:33 UTC
  • Revision ID: james.westby@ubuntu.com-20040903151533-px02yqlrchs4fv2t
Tags: upstream-4.0.2
ImportĀ upstreamĀ versionĀ 4.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1993-2002
 
2
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 
3
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 
4
 * Copyright (c) 1987 Oliver Laumann
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2, or (at your option)
 
9
 * any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program (see the file COPYING); if not, write to the
 
18
 * Free Software Foundation, Inc.,
 
19
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 
20
 *
 
21
 ****************************************************************
 
22
 */
 
23
 
 
24
#include <sys/types.h>
 
25
#include <sys/stat.h>
 
26
#include <fcntl.h>
 
27
#include <signal.h>
 
28
 
 
29
#include "config.h"
 
30
#include "screen.h"
 
31
 
 
32
#ifndef sun
 
33
# include <sys/ioctl.h>
 
34
#endif
 
35
 
 
36
/* for solaris 2.1, Unixware (SVR4.2) and possibly others */
 
37
#ifdef HAVE_SVR4_PTYS
 
38
# include <sys/stropts.h>
 
39
#endif
 
40
 
 
41
#if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
 
42
# include <sys/ttold.h>
 
43
#endif
 
44
 
 
45
#ifdef ISC
 
46
# include <sys/tty.h>
 
47
# include <sys/sioctl.h>
 
48
# include <sys/pty.h>
 
49
#endif
 
50
 
 
51
#ifdef sgi
 
52
# include <sys/sysmacros.h>
 
53
#endif /* sgi */
 
54
 
 
55
#include "extern.h"
 
56
 
 
57
/*
 
58
 * if no PTYRANGE[01] is in the config file, we pick a default
 
59
 */
 
60
#ifndef PTYRANGE0
 
61
# define PTYRANGE0 "qpr"
 
62
#endif
 
63
#ifndef PTYRANGE1
 
64
# define PTYRANGE1 "0123456789abcdef"
 
65
#endif
 
66
 
 
67
/* SVR4 pseudo ttys don't seem to work with SCO-5 */
 
68
#ifdef M_UNIX
 
69
# undef HAVE_SVR4_PTYS
 
70
#endif
 
71
 
 
72
extern int eff_uid;
 
73
 
 
74
/* used for opening a new pty-pair: */
 
75
static char PtyName[32], TtyName[32];
 
76
 
 
77
#if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
 
78
# ifdef hpux
 
79
static char PtyProto[] = "/dev/ptym/ptyXY";
 
80
static char TtyProto[] = "/dev/pty/ttyXY";
 
81
# else
 
82
#  ifdef M_UNIX
 
83
static char PtyProto[] = "/dev/ptypXY";
 
84
static char TtyProto[] = "/dev/ttypXY";
 
85
#  else
 
86
static char PtyProto[] = "/dev/ptyXY";
 
87
static char TtyProto[] = "/dev/ttyXY";
 
88
#  endif
 
89
# endif /* hpux */
 
90
#endif
 
91
 
 
92
static void initmaster __P((int));
 
93
 
 
94
#if defined(sun)
 
95
/* sun's utmp_update program opens the salve side, thus corrupting
 
96
 */
 
97
int pty_preopen = 1;
 
98
#else
 
99
int pty_preopen = 0;
 
100
#endif
 
101
 
 
102
/*
 
103
 *  Open all ptys with O_NOCTTY, just to be on the safe side
 
104
 *  (RISCos mips breaks otherwise)
 
105
 */
 
106
#ifndef O_NOCTTY
 
107
# define O_NOCTTY 0
 
108
#endif
 
109
 
 
110
/***************************************************************/
 
111
 
 
112
static void
 
113
initmaster(f)
 
114
int f;
 
115
{
 
116
#ifdef POSIX
 
117
  tcflush(f, TCIOFLUSH);
 
118
#else
 
119
# ifdef TIOCFLUSH
 
120
  (void) ioctl(f, TIOCFLUSH, (char *) 0);
 
121
# endif
 
122
#endif
 
123
#ifdef LOCKPTY
 
124
  (void) ioctl(f, TIOCEXCL, (char *) 0);
 
125
#endif
 
126
}
 
127
 
 
128
void
 
129
InitPTY(f)
 
130
int f;
 
131
{
 
132
  if (f < 0)
 
133
    return;
 
134
#if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
 
135
  if (ioctl(f, I_PUSH, "ptem"))
 
136
    Panic(errno, "InitPTY: cannot I_PUSH ptem");
 
137
  if (ioctl(f, I_PUSH, "ldterm"))
 
138
    Panic(errno, "InitPTY: cannot I_PUSH ldterm");
 
139
# ifdef sun
 
140
  if (ioctl(f, I_PUSH, "ttcompat"))
 
141
    Panic(errno, "InitPTY: cannot I_PUSH ttcompat");
 
142
# endif
 
143
#endif
 
144
}
 
145
 
 
146
/***************************************************************/
 
147
 
 
148
#if defined(OSX) && !defined(PTY_DONE)
 
149
#define PTY_DONE
 
150
int
 
151
OpenPTY(ttyn)
 
152
char **ttyn;
 
153
{
 
154
  register int f;
 
155
  if ((f = open_controlling_pty(TtyName)) < 0)
 
156
    return -1;
 
157
  initmaster(f);
 
158
  *ttyn = TtyName;
 
159
  return f;
 
160
}
 
161
#endif
 
162
 
 
163
/***************************************************************/
 
164
 
 
165
#if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
 
166
#define PTY_DONE
 
167
int
 
168
OpenPTY(ttyn)
 
169
char **ttyn;
 
170
{
 
171
  char *m, *s;
 
172
  register int f;
 
173
 
 
174
  if ((f = getpseudotty(&s, &m)) < 0)
 
175
    return -1;
 
176
#ifdef _SEQUENT_
 
177
  fvhangup(s);
 
178
#endif
 
179
  strncpy(PtyName, m, sizeof(PtyName));
 
180
  strncpy(TtyName, s, sizeof(TtyName));
 
181
  initmaster(f);
 
182
  *ttyn = TtyName;
 
183
  return f;
 
184
}
 
185
#endif
 
186
 
 
187
/***************************************************************/
 
188
 
 
189
#if defined(__sgi) && !defined(PTY_DONE)
 
190
#define PTY_DONE
 
191
int
 
192
OpenPTY(ttyn)
 
193
char **ttyn;
 
194
{
 
195
  int f;
 
196
  char *name, *_getpty(); 
 
197
  sigret_t (*sigcld)__P(SIGPROTOARG);
 
198
 
 
199
  /*
 
200
   * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
 
201
   * exec() /usr/adm/mkpts
 
202
   */
 
203
  sigcld = signal(SIGCHLD, SIG_DFL);
 
204
  name = _getpty(&f, O_RDWR | O_NONBLOCK, 0600, 0);
 
205
  signal(SIGCHLD, sigcld);
 
206
 
 
207
  if (name == 0)
 
208
    return -1;
 
209
  initmaster(f);
 
210
  *ttyn = name;
 
211
  return f;
 
212
}
 
213
#endif
 
214
 
 
215
/***************************************************************/
 
216
 
 
217
#if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
 
218
#define PTY_DONE
 
219
int
 
220
OpenPTY(ttyn)
 
221
char **ttyn;
 
222
{
 
223
  register int f;
 
224
  struct stat buf;
 
225
   
 
226
  strcpy(PtyName, "/dev/ptc");
 
227
  if ((f = open(PtyName, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
 
228
    return -1;
 
229
  if (fstat(f, &buf) < 0)
 
230
    {
 
231
      close(f);
 
232
      return -1;
 
233
    }
 
234
  sprintf(TtyName, "/dev/ttyq%d", minor(buf.st_rdev));
 
235
  initmaster(f);
 
236
  *ttyn = TtyName;
 
237
  return f;
 
238
}
 
239
#endif
 
240
 
 
241
/***************************************************************/
 
242
 
 
243
#if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
 
244
#define PTY_DONE
 
245
int
 
246
OpenPTY(ttyn)
 
247
char **ttyn;
 
248
{
 
249
  register int f;
 
250
  char *m, *ptsname();
 
251
  int unlockpt __P((int)), grantpt __P((int));
 
252
#if defined(HAVE_GETPT) && defined(linux)
 
253
  int getpt __P((void));
 
254
#endif
 
255
  sigret_t (*sigcld)__P(SIGPROTOARG);
 
256
 
 
257
  strcpy(PtyName, "/dev/ptmx");
 
258
#if defined(HAVE_GETPT) && defined(linux)
 
259
  if ((f = getpt()) == -1)
 
260
#else
 
261
  if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
 
262
#endif
 
263
    return -1;
 
264
 
 
265
  /*
 
266
   * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
 
267
   * exec()s pt_chmod
 
268
   */
 
269
  sigcld = signal(SIGCHLD, SIG_DFL);
 
270
  if ((m = ptsname(f)) == NULL || grantpt(f) || unlockpt(f))
 
271
    {
 
272
      signal(SIGCHLD, sigcld);
 
273
      close(f);
 
274
      return -1;
 
275
    } 
 
276
  signal(SIGCHLD, sigcld);
 
277
  strncpy(TtyName, m, sizeof(TtyName));
 
278
  initmaster(f);
 
279
  *ttyn = TtyName;
 
280
  return f;
 
281
}
 
282
#endif
 
283
 
 
284
/***************************************************************/
 
285
 
 
286
#if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
 
287
#define PTY_DONE
 
288
 
 
289
int
 
290
OpenPTY(ttyn)
 
291
char **ttyn;
 
292
{
 
293
  register int f;
 
294
 
 
295
  /* a dumb looking loop replaced by mycrofts code: */
 
296
  strcpy (PtyName, "/dev/ptc");
 
297
  if ((f = open (PtyName, O_RDWR | O_NOCTTY)) < 0)
 
298
    return -1;
 
299
  strncpy(TtyName, ttyname(f), sizeof(TtyName));
 
300
  if (eff_uid && access(TtyName, R_OK | W_OK))
 
301
    {
 
302
      close(f);
 
303
      return -1;
 
304
    }
 
305
  initmaster(f);
 
306
# ifdef _IBMR2
 
307
  pty_preopen = 1;
 
308
# endif
 
309
  *ttyn = TtyName;
 
310
  return f;
 
311
}
 
312
#endif
 
313
 
 
314
/***************************************************************/
 
315
 
 
316
#if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
 
317
#define PTY_DONE
 
318
int
 
319
OpenPTY(ttyn)
 
320
char **ttyn;
 
321
{
 
322
  int f, s;
 
323
  if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
 
324
    return -1;
 
325
  close(s);
 
326
  initmaster(f);
 
327
  pty_preopen = 1;
 
328
  *ttyn = TtyName;
 
329
  return f;    
 
330
}
 
331
#endif
 
332
 
 
333
/***************************************************************/
 
334
 
 
335
#ifndef PTY_DONE
 
336
int
 
337
OpenPTY(ttyn)
 
338
char **ttyn;
 
339
{
 
340
  register char *p, *q, *l, *d;
 
341
  register int f;
 
342
 
 
343
  debug("OpenPTY: Using BSD style ptys.\n");
 
344
  strcpy(PtyName, PtyProto);
 
345
  strcpy(TtyName, TtyProto);
 
346
  for (p = PtyName; *p != 'X'; p++)
 
347
    ;
 
348
  for (q = TtyName; *q != 'X'; q++)
 
349
    ;
 
350
  for (l = PTYRANGE0; (*p = *l) != '\0'; l++)
 
351
    {
 
352
      for (d = PTYRANGE1; (p[1] = *d) != '\0'; d++)
 
353
        {
 
354
          debug1("OpenPTY tries '%s'\n", PtyName);
 
355
          if ((f = open(PtyName, O_RDWR | O_NOCTTY)) == -1)
 
356
            continue;
 
357
          q[0] = *l;
 
358
          q[1] = *d;
 
359
          if (eff_uid && access(TtyName, R_OK | W_OK))
 
360
            {
 
361
              close(f);
 
362
              continue;
 
363
            }
 
364
#if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
 
365
          /* Hack to ensure that the slave side of the pty is
 
366
           * unused. May not work in anything other than SunOS4.1
 
367
           */
 
368
            {
 
369
              int pgrp;
 
370
 
 
371
              /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
 
372
              if (ioctl(f, TIOCGPGRP, (char *)&pgrp) != -1 || errno != EIO)
 
373
                {
 
374
                  close(f);
 
375
                  continue;
 
376
                }
 
377
            }
 
378
#endif
 
379
          initmaster(f);
 
380
          *ttyn = TtyName;
 
381
          return f;
 
382
        }
 
383
    }
 
384
  return -1;
 
385
}
 
386
#endif
 
387