From: Matt Birkholz Date: Sat, 20 Dec 2014 20:06:03 +0000 (-0700) Subject: smp: share: uxterm.o X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=22769f972b11368f5b871ba49cef8d353e9928a2;p=mit-scheme.git smp: share: uxterm.o --- diff --git a/README.txt b/README.txt index 998d170b3..184d86a0b 100644 --- a/README.txt +++ b/README.txt @@ -799,9 +799,13 @@ command line. The remaining 12 belong to the 7 microcode modules and OK. This variable is now auto (thread-local). uxterm.o: - 00000008 b slave_name.9024 - 00000004 b terminal_table - 00000000 b terminal_table_size + 00000008 b slave_name.9024 eliminated + 00000004 b terminal_table locked + 00000000 b terminal_table_size locked + + OK. terminal_table works like channel_table, and is locked up + in the same way. Used ptsname_r instead of ptsname. Avoided + unscrupulous use of strcpy and static buffers. uxtop.o: 00000000 b interactive diff --git a/src/microcode/ospty.h b/src/microcode/ospty.h index eb24d7f7b..4ee42d5bb 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 14a5e501f..fb28944d2 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 def14c934..716c23d12 100644 --- a/src/microcode/uxterm.c +++ b/src/microcode/uxterm.c @@ -24,11 +24,11 @@ USA. */ +#include "prims.h" #include "ux.h" #include "uxterm.h" #include "uxio.h" #include "ospty.h" -#include "prims.h" extern int UX_terminal_control_ok (int fd); @@ -57,6 +57,13 @@ extern int UX_terminal_control_ok (int fd); #else # define TRY_OPEN_PTY_MASTER_BSD 1 #endif + +#ifdef ENABLE_SMP +static pthread_mutex_t mutex = MUTEX_INITIALIZER; +# ifdef ENABLE_DEBUGGING_TOOLS +static bool locked_p = false; +# endif +#endif struct terminal_state { @@ -95,15 +102,17 @@ UX_reset_terminals (void) void terminal_open (Tchannel channel) { + LOCK(); if (terminal_table_size != OS_channel_table_size) { - terminal_table_size = OS_channel_table_size; terminal_table = (OS_realloc (terminal_table, (terminal_table_size * (sizeof (struct terminal_state))))); + terminal_table_size = OS_channel_table_size; } (TERMINAL_BUFFER (channel)) = (-1); + UNLOCK(); get_terminal_state (channel, (& (TERMINAL_ORIGINAL_STATE (channel)))); } @@ -740,15 +749,21 @@ 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_name, + 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) + { + fprintf (stderr, "\nopen_pty_master_bsd: buffers too small\n"); + fflush (stderr); + return (1); + } + for (p1 = "pqrstuvwxyzPQRST"; ((*p1) != 0); p1 += 1) for (p2 = "0123456789abcdef"; ((*p2) != 0); p2 += 1) { @@ -772,10 +787,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 +803,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 +817,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 +841,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 +852,44 @@ 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) + { + fprintf (stderr, "\nptsname_r failed: %d (%s)\n", + err, strerror(errno)); + fflush (stderr); + return (1); + } + } +#else + { + char *name = ptsname (fd); + int len = strlen (name); + if (name_length < len) + { + fprintf (stderr, "\nptsname too long for buffer: %s\n", name); + fflush (stderr); + return (1); + } + 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 */ }