File: opal_pty.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (256 lines) | stat: -rw-r--r-- 7,227 bytes parent folder | download | duplicates (5)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2018      Cisco Systems, Inc.  All rights reserved
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */
/*-
 * Copyright (c) 1990, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "opal_config.h"

#ifdef HAVE_SYS_CDEFS_H
#    include <sys/cdefs.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif
#include <sys/stat.h>
#ifdef HAVE_SYS_IOCTL_H
#    include <sys/ioctl.h>
#endif
#ifdef HAVE_FCNTL_H
#    include <fcntl.h>
#endif
#ifdef HAVE_TERMIOS_H
#    include <termios.h>
#else
#    ifdef HAVE_TERMIO_H
#        include <termio.h>
#    endif
#endif
#include <errno.h>
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef HAVE_GRP_H
#    include <grp.h>
#endif
#ifdef HAVE_PTY_H
#    include <pty.h>
#endif
#ifdef HAVE_UTMP_H
#    include <utmp.h>
#endif

#ifdef HAVE_PTSNAME
#    include <stdlib.h>
#    ifdef HAVE_STROPTS_H
#        include <stropts.h>
#    endif
#endif

#ifdef HAVE_UTIL_H
#    include <util.h>
#endif

#include "opal/util/opal_pty.h"

/* The only public interface is openpty - all others are to support
   openpty() */

#if OPAL_ENABLE_PTY_SUPPORT == 0

int opal_openpty(int *amaster, int *aslave, char *name, void *termp, void *winpp)
{
    return -1;
}

#elif defined(HAVE_OPENPTY)

int opal_openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp)
{
    return openpty(amaster, aslave, name, termp, winp);
}

#else

/* implement openpty in terms of ptym_open and ptys_open */

static int ptym_open(char *pts_name);
static int ptys_open(int fdm, char *pts_name);

int opal_openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp)
{
    char line[20];
    *amaster = ptym_open(line);
    if (*amaster < 0) {
        return -1;
    }
    *aslave = ptys_open(*amaster, line);
    if (*aslave < 0) {
        close(*amaster);
        return -1;
    }
    if (name) {
        // We don't know the max length of name, but we do know the
        // max length of the source, so at least use that.
        opal_string_copy(name, line, sizeof(line));
    }
#    ifndef TCSAFLUSH
#        define TCSAFLUSH TCSETAF
#    endif
    if (termp) {
        (void) tcsetattr(*aslave, TCSAFLUSH, termp);
    }
#    ifdef TIOCSWINSZ
    if (winp) {
        (void) ioctl(*aslave, TIOCSWINSZ, (char *) winp);
    }
#    endif
    return 0;
}

static int ptym_open(char *pts_name)
{
    int fdm;
#    ifdef HAVE_PTSNAME
    char *ptr;

#        ifdef _AIX
    strcpy(pts_name, "/dev/ptc");
#        else
    strcpy(pts_name, "/dev/ptmx");
#        endif
    fdm = open(pts_name, O_RDWR);
    if (fdm < 0) {
        return -1;
    }
    if (grantpt(fdm) < 0) { /* grant access to slave */
        close(fdm);
        return -2;
    }
    if (unlockpt(fdm) < 0) { /* clear slave's lock flag */
        close(fdm);
        return -3;
    }
    ptr = ptsname(fdm);
    if (ptr == NULL) { /* get slave's name */
        close(fdm);
        return -4;
    }
    strcpy(pts_name, ptr); /* return name of slave */
    return fdm;            /* return fd of master */
#    else
    char *ptr1, *ptr2;

    strcpy(pts_name, "/dev/ptyXY");
    /* array index: 012345689 (for references in following code) */
    for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
        pts_name[8] = *ptr1;
        for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
            pts_name[9] = *ptr2;
            /* try to open master */
            fdm = open(pts_name, O_RDWR);
            if (fdm < 0) {
                if (errno == ENOENT) { /* different from EIO */
                    return -1;         /* out of pty devices */
                } else {
                    continue; /* try next pty device */
                }
            }
            pts_name[5] = 't'; /* change "pty" to "tty" */
            return fdm;        /* got it, return fd of master */
        }
    }
    return -1; /* out of pty devices */
#    endif
}

static int ptys_open(int fdm, char *pts_name)
{
    int fds;
#    ifdef HAVE_PTSNAME
    /* following should allocate controlling terminal */
    fds = open(pts_name, O_RDWR);
    if (fds < 0) {
        close(fdm);
        return -5;
    }
#        if defined(__SVR4) && defined(__sun)
    if (ioctl(fds, I_PUSH, "ptem") < 0) {
        close(fdm);
        close(fds);
        return -6;
    }
    if (ioctl(fds, I_PUSH, "ldterm") < 0) {
        close(fdm);
        close(fds);
        return -7;
    }
#        endif

    return fds;
#    else
    int gid;
    struct group *grptr;

    grptr = getgrnam("tty");
    if (grptr != NULL) {
        gid = grptr->gr_gid;
    } else {
        gid = -1; /* group tty is not in the group file */
    }
    /* following two functions don't work unless we're root */
    chown(pts_name, getuid(), gid);
    chmod(pts_name, S_IRUSR | S_IWUSR | S_IWGRP);
    fds = open(pts_name, O_RDWR);
    if (fds < 0) {
        close(fdm);
        return -1;
    }
    return fds;
#    endif
}

#endif /* #ifdef HAVE_OPENPTY */