Skip to content

Commit

Permalink
[pty] Support ptsname_r of glibc
Browse files Browse the repository at this point in the history
Although glibc `ptsname_r` man page mentions Tru64 and HP-UX, this
function appears to be declared obsolete on both.
  • Loading branch information
nobu committed Apr 11, 2024
1 parent 1b83074 commit e7f8db9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions ext/pty/extconf.rb
Expand Up @@ -16,6 +16,7 @@
if have_func("posix_openpt") or
(util or have_func("openpty")) or
have_func("_getpty") or
have_func("ptsname_r") or
have_func("ptsname") or
have_func("ioctl")
create_makefile('pty')
Expand Down
25 changes: 19 additions & 6 deletions ext/pty/pty.c
Expand Up @@ -256,7 +256,21 @@ establishShell(int argc, VALUE *argv, struct pty_info *info,
RB_GC_GUARD(carg.execarg_obj);
}

#if defined(HAVE_POSIX_OPENPT) || defined(HAVE_OPENPTY) || defined(HAVE_PTSNAME)
#if defined(HAVE_PTSNAME) && !defined(HAVE_PTSNAME_R)
/* glibc only, not obsolete interface on Tru64 or HP-UX */
static int
ptsname_r(int fd, char *buf, size_t buflen)
{
extern char *ptsname(int);
char *name = ptsname(fd);
if (!name) return -1;
strlcpy(buf, name, buflen);
return 0;
}
# define HAVE_PTSNAME_R 1
#endif

#if defined(HAVE_POSIX_OPENPT) || defined(HAVE_OPENPTY) || defined(HAVE_PTSNAME_R)
static int
no_mesg(char *slavedevice, int nomesg)
{
Expand Down Expand Up @@ -320,7 +334,8 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
#endif
if (sigaction(SIGCHLD, &old, NULL) == -1) goto error;
if (unlockpt(masterfd) == -1) goto error;
if ((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (ptsname_r(masterfd, SlaveName, DEVICELEN) != 0) goto error;
slavedevice = SlaveName;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if ((slavefd = rb_cloexec_open(slavedevice, O_RDWR|O_NOCTTY, 0)) == -1) goto error;
rb_update_max_fd(slavefd);
Expand All @@ -333,7 +348,6 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,

*master = masterfd;
*slave = slavefd;
strlcpy(SlaveName, slavedevice, DEVICELEN);
return 0;

grantpt_error:
Expand Down Expand Up @@ -387,7 +401,6 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
char *slavedevice;
void (*s)();

extern char *ptsname(int);
extern int unlockpt(int);
extern int grantpt(int);

Expand All @@ -405,7 +418,8 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
#endif
signal(SIGCHLD, s);
if(unlockpt(masterfd) == -1) goto error;
if((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (ptsname_r(masterfd, SlaveName, DEVICELEN) != 0) goto error;
slavedevice = SlaveName;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if((slavefd = rb_cloexec_open(slavedevice, O_RDWR, 0)) == -1) goto error;
rb_update_max_fd(slavefd);
Expand All @@ -416,7 +430,6 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
#endif
*master = masterfd;
*slave = slavefd;
strlcpy(SlaveName, slavedevice, DEVICELEN);
return 0;

error:
Expand Down

0 comments on commit e7f8db9

Please sign in to comment.