Class: God::NetlinkHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/god/event_handlers/netlink_handler.rb,
ext/god/netlink_handler.c

Constant Summary collapse

EVENT_SYSTEM =
"netlink"

Class Method Summary collapse

Class Method Details

.handle_eventsObject

socket for netlink connection


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
# File 'ext/god/netlink_handler.c', line 31

VALUE
nlh_handle_events()
{
  char buff[CONNECTOR_MAX_MSG_SIZE];
  struct nlmsghdr *hdr;
  struct proc_event *event;

  VALUE extra_data;

#ifdef HAVE_RB_WAIT_FOR_SINGLE_FD
  int ret;
  if((ret = rb_wait_for_single_fd(nl_sock, RB_WAITFD_IN, NULL)) < 0){
    rb_raise(rb_eStandardError, "%s", strerror(errno));
  }
  /* If there were no events detected, return */
  if(!(ret & RB_WAITFD_IN)){
    return INT2FIX(0);
  }
#else
  fd_set fds;

  FD_ZERO(&fds);
  FD_SET(nl_sock, &fds);

  if (0 > rb_thread_select(nl_sock + 1, &fds, NULL, NULL, NULL)) {
    rb_raise(rb_eStandardError, "%s", strerror(errno));
  }

  /* If there were no events detected, return */
  if (! FD_ISSET(nl_sock, &fds)) {
    return INT2FIX(0);
  }
#endif

  /* if there are events, make calls */
  if (-1 == recv(nl_sock, buff, sizeof(buff), 0)) {
    rb_raise(rb_eStandardError, "%s", strerror(errno));
  }

  hdr = (struct nlmsghdr *)buff;

  if (NLMSG_ERROR == hdr->nlmsg_type) {
    rb_raise(rb_eStandardError, "%s", strerror(errno));
  } else if (NLMSG_DONE == hdr->nlmsg_type) {

    event = (struct proc_event *)((struct cn_msg *)NLMSG_DATA(hdr))->data;

    switch(event->what) {
      case PROC_EVENT_EXIT:
        if (Qnil == rb_funcall(cEventHandler, m_watching_pid, 1, INT2FIX(event->event_data.exit.process_pid))) {
          return INT2FIX(0);
        }

        extra_data = rb_hash_new();
        rb_hash_aset(extra_data, ID2SYM(rb_intern("pid")), INT2FIX(event->event_data.exit.process_pid));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("exit_code")), INT2FIX(event->event_data.exit.exit_code));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("exit_signal")), INT2FIX(event->event_data.exit.exit_signal));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("thread_group_id")), INT2FIX(event->event_data.exit.process_tgid));

        rb_funcall(cEventHandler, m_call, 3, INT2FIX(event->event_data.exit.process_pid), ID2SYM(proc_exit), extra_data);
        return INT2FIX(1);

      case PROC_EVENT_FORK:
        if (Qnil == rb_funcall(cEventHandler, m_watching_pid, 1, INT2FIX(event->event_data.fork.parent_pid))) {
          return INT2FIX(0);
        }

        extra_data = rb_hash_new();
        rb_hash_aset(extra_data, ID2SYM(rb_intern("parent_pid")), INT2FIX(event->event_data.fork.parent_pid));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("parent_thread_group_id")), INT2FIX(event->event_data.fork.parent_tgid));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("child_pid")), INT2FIX(event->event_data.fork.child_pid));
        rb_hash_aset(extra_data, ID2SYM(rb_intern("child_thread_group_id")), INT2FIX(event->event_data.fork.child_tgid));

        rb_funcall(cEventHandler, m_call, 3, INT2FIX(event->event_data.fork.parent_pid), ID2SYM(proc_fork), extra_data);
        return INT2FIX(1);

      default:
        break;
    }
  }

  return Qnil;
}

.register_process(pid, events) ⇒ Object


7
8
9
10
11
# File 'lib/god/event_handlers/netlink_handler.rb', line 7

def self.register_process(pid, events)
  # netlink doesn't need to do this
  # it just reads from the eventhandler actions to see if the pid
  # matches the list we're looking for -- Kev
end