Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
35eb5cdc
Unverified
Commit
35eb5cdc
authored
Jul 06, 2020
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
openpty: improve implementation and handling of platforms without it
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
6d3b6851
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
136 additions
and
88 deletions
+136
-88
configure.ac
configure.ac
+5
-1
openpty.c
src/include/openpty.c
+116
-56
openpty.h
src/include/openpty.h
+3
-25
Makefile.am
src/lxc/Makefile.am
+10
-4
conf.c
src/lxc/conf.c
+1
-1
terminal.c
src/lxc/terminal.c
+1
-1
No files found.
configure.ac
View file @
35eb5cdc
...
@@ -663,7 +663,7 @@ fi
...
@@ -663,7 +663,7 @@ fi
AC_CHECK_LIB(pthread, main)
AC_CHECK_LIB(pthread, main)
AC_CHECK_FUNCS(statvfs)
AC_CHECK_FUNCS(statvfs)
AC_CHECK_LIB(util, openpty)
AC_CHECK_LIB(util, openpty)
AC_CHECK_FUNCS([
openpty
hasmntopt setmntent endmntent utmpxname])
AC_CHECK_FUNCS([hasmntopt setmntent endmntent utmpxname])
AC_CHECK_FUNCS([getgrgid_r],
AC_CHECK_FUNCS([getgrgid_r],
AM_CONDITIONAL(HAVE_GETGRGID_R, true)
AM_CONDITIONAL(HAVE_GETGRGID_R, true)
AC_DEFINE(HAVE_GETGRGID_R,1,[Have getgrgid_r]),
AC_DEFINE(HAVE_GETGRGID_R,1,[Have getgrgid_r]),
...
@@ -684,6 +684,10 @@ AC_CHECK_FUNCS([keyctl],
...
@@ -684,6 +684,10 @@ AC_CHECK_FUNCS([keyctl],
AM_CONDITIONAL(HAVE_KEYCTL, true)
AM_CONDITIONAL(HAVE_KEYCTL, true)
AC_DEFINE(HAVE_KEYCTL,1,[Have keyctl]),
AC_DEFINE(HAVE_KEYCTL,1,[Have keyctl]),
AM_CONDITIONAL(HAVE_KEYCTL, false))
AM_CONDITIONAL(HAVE_KEYCTL, false))
AC_CHECK_FUNCS([openpty],
AM_CONDITIONAL(HAVE_OPENPTY, true)
AC_DEFINE(HAVE_OPENPTY,1,[Have openpty]),
AM_CONDITIONAL(HAVE_OPENPTY, false))
AC_CHECK_FUNCS([prlimit],
AC_CHECK_FUNCS([prlimit],
AM_CONDITIONAL(HAVE_PRLIMIT, true)
AM_CONDITIONAL(HAVE_PRLIMIT, true)
AC_DEFINE(HAVE_PRLIMIT,1,[Have prlimit]),
AC_DEFINE(HAVE_PRLIMIT,1,[Have prlimit]),
...
...
src/include/openpty.c
View file @
35eb5cdc
/*
/* SPDX-License-Identifier: LGPL-2.1+ */
* openpty: glibc implementation
*
#define _GNU_SOURCE
* Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
*
* Authors:
* Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _XOPEN_SOURCE
/* See feature_test_macros(7) */
#include <errno.h>
#include <errno.h>
#include <fcntl.h>
#include <fcntl.h>
#include <limits.h>
#include <limits.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <termios.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#define _PATH_DEVPTMX "/dev/ptmx"
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
int
openpty
(
int
*
aptx
,
int
*
apty
,
char
*
name
,
struct
termios
*
termp
,
static
int
pts_name
(
int
fd
,
char
**
pts
,
size_t
buf_len
)
struct
winsize
*
winp
)
{
{
char
buf
[
PATH_MAX
];
int
rv
;
int
ptx
,
pty
;
char
*
buf
=
*
pts
;
for
(;;)
{
char
*
new_buf
;
if
(
buf_len
)
{
rv
=
ptsname_r
(
fd
,
buf
,
buf_len
);
if
(
rv
!=
0
||
memchr
(
buf
,
'\0'
,
buf_len
))
/* We either got an error, or we succeeded and the
returned name fit in the buffer. */
break
;
/* Try again with a longer buffer. */
buf_len
+=
buf_len
;
/* Double it */
}
else
/* No initial buffer; start out by mallocing one. */
buf_len
=
128
;
/* First time guess. */
if
(
buf
!=
*
pts
)
/* We've already malloced another buffer at least once. */
new_buf
=
realloc
(
buf
,
buf_len
);
else
new_buf
=
malloc
(
buf_len
);
if
(
!
new_buf
)
{
rv
=
-
1
;
break
;
}
buf
=
new_buf
;
}
if
(
rv
==
0
)
*
pts
=
buf
;
/* Return buffer to the user. */
else
if
(
buf
!=
*
pts
)
free
(
buf
);
/* Free what we malloced when returning an error. */
return
rv
;
}
int
__unlockpt
(
int
fd
)
{
#ifdef TIOCSPTLCK
int
unlock
=
0
;
if
(
ioctl
(
fd
,
TIOCSPTLCK
,
&
unlock
))
{
if
(
errno
!=
EINVAL
)
return
-
1
;
}
#endif
return
0
;
}
int
openpty
(
int
*
ptx
,
int
*
pty
,
char
*
name
,
const
struct
termios
*
termp
,
const
struct
winsize
*
winp
)
{
char
_buf
[
PATH_MAX
];
char
*
buf
=
_buf
;
int
ptx_fd
,
ret
=
-
1
,
pty_fd
=
-
1
;
*
buf
=
'\0'
;
ptx_fd
=
open
(
"/dev/ptmx"
,
O_RDWR
|
O_NOCTTY
);
if
(
ptx_fd
==
-
1
)
return
-
1
;
if
(
__unlockpt
(
ptx_fd
))
goto
on_error
;
#ifdef TIOCGPTPEER
/* Try to allocate pty_fd solely based on ptx_fd first. */
pty_fd
=
ioctl
(
ptx_fd
,
TIOCGPTPEER
,
O_RDWR
|
O_NOCTTY
);
#endif
if
(
pty_fd
==
-
1
)
{
/* Fallback to path-based pty_fd allocation in case kernel doesn't
* support TIOCGPTPEER.
*/
if
(
pts_name
(
ptx_fd
,
&
buf
,
sizeof
(
_buf
)))
goto
on_error
;
ptx
=
open
(
_PATH_DEVPTMX
,
O_RDWR
);
pty_fd
=
open
(
buf
,
O_RDWR
|
O_NOCTTY
);
if
(
ptx
==
-
1
)
if
(
pty_fd
==
-
1
)
return
-
1
;
goto
on_error
;
}
if
(
grantpt
(
ptx
))
if
(
termp
)
goto
fail
;
tcsetattr
(
pty_fd
,
TCSAFLUSH
,
termp
);
#ifdef TIOCSWINSZ
if
(
winp
)
ioctl
(
pty_fd
,
TIOCSWINSZ
,
winp
);
#endif
if
(
unlockpt
(
ptx
))
*
ptx
=
ptx_fd
;
goto
fail
;
*
pty
=
pty_fd
;
if
(
name
!=
NULL
)
{
if
(
*
buf
==
'\0'
)
if
(
pts_name
(
ptx_fd
,
&
buf
,
sizeof
(
_buf
)))
goto
on_error
;
if
(
ptyname_r
(
ptx
,
buf
,
sizeof
buf
))
strcpy
(
name
,
buf
);
goto
fail
;
}
pty
=
open
(
buf
,
O_RDWR
|
O_NOCTTY
);
ret
=
0
;
if
(
pty
==
-
1
)
goto
fail
;
/* XXX Should we ignore errors here? */
on_error:
if
(
termp
)
if
(
ret
==
-
1
)
{
tcsetattr
(
pty
,
TCSAFLUSH
,
termp
);
close
(
ptx_fd
);
if
(
winp
)
ioctl
(
pty
,
TIOCSWINSZ
,
winp
);
*
aptx
=
ptx
;
if
(
pty_fd
!=
-
1
)
*
apty
=
pty
;
close
(
pty_fd
);
if
(
name
!=
NULL
)
}
strcpy
(
name
,
buf
);
return
0
;
if
(
buf
!=
_buf
)
free
(
buf
);
fail:
return
ret
;
close
(
ptx
);
return
-
1
;
}
}
src/include/openpty.h
View file @
35eb5cdc
/*
/* SPDX-License-Identifier: LGPL-2.1+ */
* openpty: glibc implementation
*
* Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
*
* Authors:
* Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _OPENPTY_H
#ifndef _OPENPTY_H
#define _OPENPTY_H
#define _OPENPTY_H
...
@@ -32,8 +11,7 @@
...
@@ -32,8 +11,7 @@
* attributes according to @__termp and @__winp and return handles for both
* attributes according to @__termp and @__winp and return handles for both
* ends in @__aptx and @__apts.
* ends in @__aptx and @__apts.
*/
*/
extern
int
openpty
(
int
*
__aptx
,
int
*
__apts
,
char
*
__name
,
extern
int
openpty
(
int
*
ptx
,
int
*
pty
,
char
*
name
,
const
struct
termios
*
termp
,
const
struct
termios
*
__termp
,
const
struct
winsize
*
winp
);
const
struct
winsize
*
__winp
);
#endif
#endif
src/lxc/Makefile.am
View file @
35eb5cdc
...
@@ -54,8 +54,11 @@ noinst_HEADERS = api_extensions.h \
...
@@ -54,8 +54,11 @@ noinst_HEADERS = api_extensions.h \
if
IS_BIONIC
if
IS_BIONIC
noinst_HEADERS
+=
../include/fexecve.h
\
noinst_HEADERS
+=
../include/fexecve.h
\
../include/lxcmntent.h
\
../include/lxcmntent.h
../include/openpty.h
endif
if
!HAVE_OPENPTY
noinst_HEADERS
+=
../include/openpty.h
endif
endif
if
!HAVE_PRLIMIT
if
!HAVE_PRLIMIT
...
@@ -156,8 +159,11 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
...
@@ -156,8 +159,11 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
if
IS_BIONIC
if
IS_BIONIC
liblxc_la_SOURCES
+=
../include/fexecve.c ../include/fexecve.h
\
liblxc_la_SOURCES
+=
../include/fexecve.c ../include/fexecve.h
\
../include/lxcmntent.c ../include/lxcmntent.h
\
../include/lxcmntent.c ../include/lxcmntent.h
../include/openpty.c ../include/openpty.h
endif
if
!HAVE_OPENPTY
liblxc_la_SOURCES
+=
../include/openpty.c ../include/openpty.h
endif
endif
if
!HAVE_GETGRGID_R
if
!HAVE_GETGRGID_R
...
...
src/lxc/conf.c
View file @
35eb5cdc
...
@@ -69,7 +69,7 @@
...
@@ -69,7 +69,7 @@
#include <sys/statvfs.h>
#include <sys/statvfs.h>
#endif
#endif
#if HAVE_
PTY_H
#if HAVE_
OPENPTY
#include <pty.h>
#include <pty.h>
#else
#else
#include <../include/openpty.h>
#include <../include/openpty.h>
...
...
src/lxc/terminal.c
View file @
35eb5cdc
...
@@ -29,7 +29,7 @@
...
@@ -29,7 +29,7 @@
#include "terminal.h"
#include "terminal.h"
#include "utils.h"
#include "utils.h"
#if HAVE_
PTY_H
#if HAVE_
OPENPTY
#include <pty.h>
#include <pty.h>
#else
#else
#include <../include/openpty.h>
#include <../include/openpty.h>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment