From f4b545b3ff3aacf50f6c1e6d2e1a0d42ed0a5e8e Mon Sep 17 00:00:00 2001 From: Matt Birkholz Date: Sat, 25 Jul 2015 14:48:52 -0700 Subject: [PATCH] Make uxterm.o state thread-local. Make the static slave_name buffer auto. Use ptsname_r instead of ptsname. (terminal_table et al are already used serially because of the runtime system's use of the open-channels gc-finalizer.) --- src/microcode/ospty.h | 4 +-- src/microcode/prospty.c | 8 +++--- src/microcode/uxterm.c | 55 +++++++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/microcode/ospty.h b/src/microcode/ospty.h index 38c43b0c1..62345c51d 100644 --- a/src/microcode/ospty.h +++ b/src/microcode/ospty.h @@ -29,8 +29,8 @@ USA. #include "os.h" -extern const char * OS_open_pty_master - (Tchannel * master_fd, const char ** master_fname); +extern int OS_open_pty_master (Tchannel * master_fd, char * master_fname, + char * slave_name, size_t slave_name_length); extern void OS_pty_master_send_signal (Tchannel channel, int sig); extern void OS_pty_master_kill (Tchannel channel); extern void OS_pty_master_stop (Tchannel channel); diff --git a/src/microcode/prospty.c b/src/microcode/prospty.c index f31b0a234..5d16b9235 100644 --- a/src/microcode/prospty.c +++ b/src/microcode/prospty.c @@ -49,9 +49,11 @@ Returns a vector #(CHANNEL MASTER-NAME SLAVE-NAME).") PRIMITIVE_HEADER (0); { Tchannel channel; - const char * master_name; - const char * slave_name = - (OS_open_pty_master ((&channel), (&master_name))); + char master_name[24]; + char slave_name[24]; + int err = OS_open_pty_master (&channel, master_name, slave_name, 24); + if (err) + error_external_return (); transaction_begin (); OS_channel_close_on_abort (channel); { diff --git a/src/microcode/uxterm.c b/src/microcode/uxterm.c index c3431be2b..52e261e73 100644 --- a/src/microcode/uxterm.c +++ b/src/microcode/uxterm.c @@ -740,15 +740,17 @@ OS_have_ptys_p (void) #ifdef TRY_OPEN_PTY_MASTER_BSD -static const char * -open_pty_master_bsd (Tchannel * master_fd, const char ** master_fname) +static int +open_pty_master_bsd (Tchannel * master_fd, const char ** master_fname, + const char * slave_name, size_t namelen) { - static char master_name [24]; - static char slave_name [24]; const char * p1; const char * p2; int fd; + if (namelen < 11) + return (1); + for (p1 = "pqrstuvwxyzPQRST"; ((*p1) != 0); p1 += 1) for (p2 = "0123456789abcdef"; ((*p2) != 0); p2 += 1) { @@ -772,10 +774,9 @@ open_pty_master_bsd (Tchannel * master_fd, const char ** master_fname) continue; } MAKE_CHANNEL (fd, channel_type_unix_pty_master, (*master_fd) =); - (*master_fname) = master_name; - return (slave_name); + return (0); } - return (0); + return (1); } #endif /* not TRY_OPEN_PTY_MASTER_BSD */ @@ -789,17 +790,13 @@ open_pty_master_bsd (Tchannel * master_fd, const char ** master_fname) # undef HAVE_POSIX_OPENPT #endif -/* Open an available pty, putting channel in (*master_fd), - and return the file name of the pty. - Signal error if none available. */ - -const char * -OS_open_pty_master (Tchannel * master_fd, const char ** master_fname) +int +OS_open_pty_master (Tchannel * master_fd, char * master_fname, + char * slave_name, size_t name_length) { #ifdef HAVE_GRANTPT while (1) { - static char slave_name [24]; #ifdef HAVE_POSIX_OPENPT int fd = (posix_openpt (O_RDWR | O_NOCTTY)); #else @@ -807,7 +804,7 @@ OS_open_pty_master (Tchannel * master_fd, const char ** master_fname) int fd; { int slave_fd; - int rc = (openpty ((&fd), (&slave_fd), slave_name, 0, 0)); + int rc = (openpty ((&fd), (&slave_fd), 0, 0, 0)); if (rc < 0) fd = -1; else @@ -831,9 +828,10 @@ OS_open_pty_master (Tchannel * master_fd, const char ** master_fname) #ifdef TRY_OPEN_PTY_MASTER_BSD /* Try BSD open. This is needed for Linux which might have Unix98 support in the library but not the kernel. */ - return (open_pty_master_bsd (master_fd, master_fname)); + return (open_pty_master_bsd (master_fd, master_fname, + slave_name, name_length)); #else - return (0); + return (1); #endif } #ifdef sonyrisc @@ -841,20 +839,35 @@ OS_open_pty_master (Tchannel * master_fd, const char ** master_fname) #endif grantpt (fd); unlockpt (fd); - strcpy (slave_name, (ptsname (fd))); +#ifdef ENABLE_SMP + { + int err = ptsname_r (fd, slave_name, name_length); + if (err) + return (2); + } +#else + { + char *name = ptsname (fd); + int len = strlen (name); + if (name_length < len) + return (3); + strcpy (slave_name, name); + } +#endif #ifdef sonyrisc sony_unblock_sigchld (); #endif MAKE_CHANNEL (fd, channel_type_unix_pty_master, (*master_fd) =); - (*master_fname) = "/dev/ptmx"; - return (slave_name); + strcpy (master_fname, "/dev/ptmx"); + return (0); } #else /* not HAVE_GRANTPT */ if (!OS_have_ptys_p ()) error_unimplemented_primitive (); - return (open_pty_master_bsd (master_fd, master_fname)); + return (open_pty_master_bsd (master_fd, master_fname, + slave_name, name_length)); #endif /* not HAVE_GRANTPT */ } -- 2.25.1