dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 10/14] grantpt
Date: Tue, 26 Jun 2018 09:38:13 +0000	[thread overview]
Message-ID: <20180626093817.1533-11-e@80x24.org> (raw)
In-Reply-To: <20180626093817.1533-1-e@80x24.org>

---
 configure.ac  |  1 +
 ext/pty/pty.c | 22 ++++------------------
 internal.h    |  3 +++
 signal.c      | 40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/configure.ac b/configure.ac
index d9283d9e17..f48db0733c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1782,6 +1782,7 @@ AC_CHECK_FUNCS(getsid)
 AC_CHECK_FUNCS(gettimeofday)		# for making ac_cv_func_gettimeofday
 AC_CHECK_FUNCS(getuidx)
 AC_CHECK_FUNCS(gmtime_r)
+AC_CHECK_FUNCS(grantpt)
 AC_CHECK_FUNCS(initgroups)
 AC_CHECK_FUNCS(ioctl)
 AC_CHECK_FUNCS(isfinite)
diff --git a/ext/pty/pty.c b/ext/pty/pty.c
index b3c7535321..1ac89061c1 100644
--- a/ext/pty/pty.c
+++ b/ext/pty/pty.c
@@ -246,19 +246,13 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
     /* Unix98 PTY */
     int masterfd = -1, slavefd = -1;
     char *slavedevice;
-    struct sigaction dfl, old;
-
-    dfl.sa_handler = SIG_DFL;
-    dfl.sa_flags = 0;
-    sigemptyset(&dfl.sa_mask);
 
 #if defined(__sun) || (defined(__FreeBSD__) && __FreeBSD_version < 902000)
     /* workaround for Solaris 10: grantpt() doesn't work if FD_CLOEXEC is set.  [ruby-dev:44688] */
     /* FreeBSD 9.2 or later supports O_CLOEXEC
      * http://www.freebsd.org/cgi/query-pr.cgi?pr=162374 */
     if ((masterfd = posix_openpt(O_RDWR|O_NOCTTY)) == -1) goto error;
-    if (sigaction(SIGCHLD, &dfl, &old) == -1) goto error;
-    if (grantpt(masterfd) == -1) goto grantpt_error;
+    if (rb_grantpt(masterfd) == -1) goto error;
     rb_fd_fix_cloexec(masterfd);
 #else
     {
@@ -272,10 +266,8 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
 	if ((masterfd = posix_openpt(flags)) == -1) goto error;
     }
     rb_fd_fix_cloexec(masterfd);
-    if (sigaction(SIGCHLD, &dfl, &old) == -1) goto error;
-    if (grantpt(masterfd) == -1) goto grantpt_error;
+    if (rb_grantpt(masterfd) == -1) goto error;
 #endif
-    if (sigaction(SIGCHLD, &old, NULL) == -1) goto error;
     if (unlockpt(masterfd) == -1) goto error;
     if ((slavedevice = ptsname(masterfd)) == NULL) goto error;
     if (no_mesg(slavedevice, nomesg) == -1) goto error;
@@ -293,8 +285,6 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
     strlcpy(SlaveName, slavedevice, DEVICELEN);
     return 0;
 
-  grantpt_error:
-    sigaction(SIGCHLD, &old, NULL);
   error:
     if (slavefd != -1) close(slavefd);
     if (masterfd != -1) close(masterfd);
@@ -346,21 +336,17 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
 
     extern char *ptsname(int);
     extern int unlockpt(int);
-    extern int grantpt(int);
 
 #if defined(__sun)
     /* workaround for Solaris 10: grantpt() doesn't work if FD_CLOEXEC is set.  [ruby-dev:44688] */
     if((masterfd = open("/dev/ptmx", O_RDWR, 0)) == -1) goto error;
-    s = signal(SIGCHLD, SIG_DFL);
-    if(grantpt(masterfd) == -1) goto error;
+    if(rb_grantpt(masterfd) == -1) goto error;
     rb_fd_fix_cloexec(masterfd);
 #else
     if((masterfd = rb_cloexec_open("/dev/ptmx", O_RDWR, 0)) == -1) goto error;
     rb_update_max_fd(masterfd);
-    s = signal(SIGCHLD, SIG_DFL);
-    if(grantpt(masterfd) == -1) goto error;
+    if(rb_grantpt(masterfd) == -1) goto error;
 #endif
-    signal(SIGCHLD, s);
     if(unlockpt(masterfd) == -1) goto error;
     if((slavedevice = ptsname(masterfd)) == NULL) goto error;
     if (no_mesg(slavedevice, nomesg) == -1) goto error;
diff --git a/internal.h b/internal.h
index c486a46d99..4cd1e7b181 100644
--- a/internal.h
+++ b/internal.h
@@ -2039,6 +2039,9 @@ VALUE rb_gcd_normal(VALUE self, VALUE other);
 VALUE rb_gcd_gmp(VALUE x, VALUE y);
 #endif
 
+/* signal.c (export) */
+int rb_grantpt(int fd);
+
 /* string.c (export) */
 #ifdef RUBY_ENCODING_H
 /* internal use */
diff --git a/signal.c b/signal.c
index 9f74c43c5d..d09480fb6f 100644
--- a/signal.c
+++ b/signal.c
@@ -1573,3 +1573,43 @@ Init_signal(void)
 
     rb_enable_interrupt();
 }
+
+#if defined(HAVE_GRANTPT)
+extern int grantpt(int);
+#endif
+
+int
+rb_grantpt(int masterfd)
+{
+    int ret = -1;
+    int e = ENOSYS;
+#if defined(HAVE_GRANTPT)
+    if (RUBY_SIGCHLD) {
+        rb_vm_t *vm = GET_VM();
+
+        rb_nativethread_lock_lock(&vm->waitpid_lock);
+        if (list_empty(&vm->waiting_pids) && list_empty(&vm->waiting_grps)) {
+            sighandler_t old = ruby_signal(RUBY_SIGCHLD, SIG_DFL);
+
+            ret = grantpt(masterfd);
+            if (ret < 0) e = errno;
+
+            ruby_signal(RUBY_SIGCHLD, old);
+        }
+        else {
+            /* grantpt cannot function with multiple threads in waitpid */
+            e = EAGAIN;
+            ret = -1;
+        }
+        rb_nativethread_lock_unlock(&vm->waitpid_lock);
+    }
+    else {
+        /* maybe some platforms have grantpt w/o SIGCHLD */
+        return grantpt(masterfd);
+    }
+#endif  /* !HAVE_GRANTPT */
+
+    if (ret < 0)
+        errno = e;
+    return ret;
+}
-- 
EW


  parent reply	other threads:[~2018-06-26  9:38 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-26  9:38 [PATCH 00/14] SIGCHLD hijack for waitpid Eric Wong
2018-06-26  9:38 ` [PATCH 01/14] hijack SIGCHLD handler for internal use Eric Wong
2018-06-26  9:38 ` [PATCH 02/14] fix SIGCHLD hijacking race conditions Eric Wong
2018-06-26  9:38 ` [PATCH 03/14] mjit.c: allow working on platforms without SIGCHLD Eric Wong
2018-06-26  9:38 ` [PATCH 04/14] cleanups Eric Wong
2018-06-26  9:38 ` [PATCH 05/14] handle SIGCHLD in both the timer-thread and main thread Eric Wong
2018-06-26  9:38 ` [PATCH 06/14] Revert "test_process.rb: skip tests for Bug 14867" Eric Wong
2018-06-26  9:38 ` [PATCH 07/14] Revert "spec: skip Process wait specs on MJIT" Eric Wong
2018-06-26  9:38 ` [PATCH 08/14] wip testing Eric Wong
2018-06-26  9:38 ` [PATCH 09/14] pgrp list Eric Wong
2018-06-26  9:38 ` Eric Wong [this message]
2018-06-26  9:38 ` [PATCH 11/14] blocking mjit_finish Eric Wong
2018-06-26  9:38 ` [PATCH 12/14] reinitialize waitpid_lock Eric Wong
2018-06-26  9:38 ` [PATCH 13/14] signal.c: prevent spurious wakeup of main thread for SIGCHLD Eric Wong
2018-06-26  9:38 ` [PATCH 14/14] process.c (rb_waitpid): reimplement non-SIGCHLD code path Eric Wong
2018-06-26 10:51 ` [PATCH 15/14] force SIGCHLD to the timer-thread Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180626093817.1533-11-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).