Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 222 lines (174 sloc) 4.82 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#include "config.h"
#include "signal.hpp"
#include "vm.hpp"

#include "builtin/module.hpp"
#include "builtin/array.hpp"

#include "builtin/array.hpp"
#include "builtin/module.hpp"
#include "builtin/class.hpp"

#include <iostream>
#include <fcntl.h>

#include "builtin/thread.hpp"

#ifndef RBX_WINDOWS
#include <sys/select.h>
#endif

#include "windows_compat.h"

namespace rubinius {
  struct CallFrame;

  static SignalHandler* handler_ = 0;
  pthread_t main_thread;

  Object* handle_tramp(STATE) {
    handler_->perform(state);
    return Qnil;
  }

  SignalHandler::SignalHandler(VM* vm)
    : target_(vm)
    , queued_signals_(0)
    , exit_(false)
    , thread_(vm)
  {
    handler_ = this;
    main_thread = pthread_self();

    for(int i = 0; i < NSIG; i++) {
      pending_signals_[i] = 0;
    }

    reopen_pipes();

    self_ = vm->shared.new_vm();

    thread_.set(Thread::create(vm, self_, vm->shared.globals.thread.get(),
                               handle_tramp, false));
  }

  void SignalHandler::reopen_pipes() {
    int f[2];
    if(pipe(f) < 0) {
      perror("SignalHandler::reopen_pipes failed");
    }
    read_fd_ = f[0];
    write_fd_ = f[1];
  }

  void SignalHandler::on_fork(STATE) {
    if(handler_) handler_->on_fork_i(state);
  }

  void SignalHandler::on_fork_i(STATE) {
    exit_ = false;
    reopen_pipes();

    if(!self_) self_ = state->shared.new_vm();
    thread_.set(Thread::create(state, self_, state->shared.globals.thread.get(),
                               handle_tramp, false));

    run(state);
  }

  void SignalHandler::shutdown() {
    if(handler_) handler_->shutdown_i();
  }

  void SignalHandler::shutdown_i() {
    self_ = 0;
    exit_ = true;
    if(write(write_fd_, "!", 1) < 0) {
      perror("SignalHandler::shutdown_i failed to write");
    }
  }

  void SignalHandler::run(STATE) {
    thread_.get()->fork(state);
  }

  void SignalHandler::perform(STATE) {
#ifndef RBX_WINDOWS
    sigset_t set;
    sigfillset(&set);
    pthread_sigmask(SIG_BLOCK, &set, NULL);
#endif

    thread::Thread::set_os_name("rbx.signal-dispatch");

    for(;;) {
      fd_set fds;
      FD_ZERO(&fds);
      FD_SET((int_fd_t)read_fd_, &fds);

      int n;

      {
        GCIndependent indy(state, 0);
        n = select(read_fd_ + 1, &fds, NULL, NULL, NULL);
      }

      if(exit_) {
        close(write_fd_);
        close(read_fd_);
        return;
      }

      if(n == 1) {
        // drain a bunch
        char buf[512];
        if(read(read_fd_, buf, sizeof(buf)) < 0) {
          perror("SignalHandler::perform failed to read");
        }

        {
          target_->check_local_interrupts = true;
          target_->get_attention();
          target_->wakeup(state);

        }
      }
    }
  }

  void SignalHandler::signal_tramp(int sig) {
    handler_->handle_signal(sig);
  }

  void SignalHandler::handle_signal(int sig) {
    queued_signals_ = 1;
    pending_signals_[sig] = 1;

    // If the main thread is running, just tell it that
    // there are local interrupts waiting.
    if(!target_->waiting_p()) {
      target_->check_local_interrupts = true;
      return;
    }

    if(target_->should_interrupt_with_signal()) {
      target_->check_local_interrupts = true;

      if(!pthread_equal(pthread_self(), main_thread)) {
#ifdef RBX_WINDOWS
        // TODO: Windows
#else
        pthread_kill(main_thread, SIGVTALRM);
#endif
      }
      return;
    }

    if(write(write_fd_, "!", 1) < 0) {
      perror("SignalHandler::handle_signal failed to write");
    }
  }

  void SignalHandler::add_signal(STATE, int sig, HandlerType type) {
    SYNC(state);

#ifndef RBX_WINDOWS
    sigset_t sigs;
    sigemptyset(&sigs);
    sigaddset(&sigs, sig);
    sigprocmask(SIG_UNBLOCK, &sigs, NULL);

    struct sigaction action;

    if(type == eDefault) {
      action.sa_handler = SIG_DFL;
    } else if(type == eIgnore) {
      action.sa_handler = SIG_IGN;
    } else {
      action.sa_handler = signal_tramp;
    }

    action.sa_flags = 0;
    sigfillset(&action.sa_mask);

    sigaction(sig, &action, NULL);
#endif
  }

  bool SignalHandler::deliver_signals(STATE, CallFrame* call_frame) {
    queued_signals_ = 0;

    for(int i = 0; i < NSIG; i++) {
      if(pending_signals_[i] > 0) {
        pending_signals_[i] = 0;

        Array* args = Array::create(state, 1);
        args->set(state, 0, Fixnum::from(i));

        // Check whether the send raised an exception and
        // stop running the handlers if that happens
        if(!G(rubinius)->send(state, call_frame,
               state->symbol("received_signal"), args, Qnil)) {
          if(state->thread_state()->raise_reason() == cException ||
             state->thread_state()->raise_reason() == cExit) {
            return false;
          }
        }
      }
    }

    return true;
  }
}
Something went wrong with that request. Please try again.