summaryrefslogtreecommitdiffstats
path: root/src/util/pty/open_slave.c
blob: d7c94dcba2dda48b6d5e8119474ffb8fc7112535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * pty_open_slave: open slave side of terminal, clearing for use.
 *
 * Copyright 1995 by the Massachusetts Institute of Technology.
 *
 * 
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of
 * M.I.T. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability
 * of this software for any purpose.  It is provided "as is" without
 * express or implied warranty.
 * 
 */

#include <com_err.h>
#include "libpty.h"
#include "pty-int.h"


long pty_open_slave ( slave, fd)
    const char *slave;
    int *fd;
{
    int vfd;
long retval;
#ifdef POSIX_SIGNALS
    struct sigaction sa;
    /* Initialize "sa" structure. */
    (void) sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    
#endif


    /* First, chmod and chown the slave*/
    /*
       * If we have vhangup then we really need pty_open_ctty to make sure
       * Our controlling terminal is the pty we're opening.  However, if we
       * are using revoke or nothing then we just need  a file descriiptor
       * for the pty.  Considering some OSes in this category break on
       * the second call to open_ctty (currently OSF but others may),
       * we simply use a descriptor if we can.
       */
#ifdef VHANG_FIRST
    if (( retval = pty_open_ctty ( slave, &vfd )) != 0 )
      return retval;
#else /*VHANG_FIRST*/
    if ( (vfd = open(slave, O_RDWR)) < 0 )
      return errno;
#endif
    
        if (vfd < 0)
	return PTY_OPEN_SLAVE_OPENFAIL;

#ifndef HAVE_FCHMOD
        if (chmod(line, 0))
	    return PTY_OPEN_SLAVE_CHMODFAIL;
#else
        if (fchmod(vfd, 0))
return PTY_OPEN_SLAVE_CHMODFAIL;
#endif /*HAVE_FCHMOD*/
#ifdef HAVE_FCHOWN
    if ( fchown(vfd, 0, 0 ) == -1 ) 
#else
	if ( chown(slave, 0, 0 ) == -1 ) 
#endif /* HAVE_FCHOWN*/
	    return PTY_OPEN_SLAVE_CHOWNFAIL;

#ifdef VHANG_FIRST
    ptyint_vhangup();
#endif

    (void) close(vfd);
    if ( (retval = ptyint_void_association()) != 0)
      return retval;
    
#ifdef HAVE_REVOKE
    if (revoke (slave) < 0 ) {
	return PTY_OPEN_SLAVE_REVOKEFAIL;
    }
#endif /*HAVE_REVOKE*/

/* Open the pty for real. */
    if  (( retval = pty_open_ctty ( slave, fd))  != 0 ) {
	return PTY_OPEN_SLAVE_OPENFAIL;
    }
    return pty_initialize_slave (*fd);
}