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



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

VALUE
nlh_handle_events()
{
  char buff[CONNECTOR_MAX_MSG_SIZE];
  struct nlmsghdr *hdr;
  struct proc_event *event;
  
  VALUE extra_data;
  
  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, strerror(errno));
  }
  
  /* If there were no events detected, return */
  if (! FD_ISSET(nl_sock, &fds)) {
    return INT2FIX(0);
  }
  
  /* if there are events, make calls */  
  if (-1 == recv(nl_sock, buff, sizeof(buff), 0)) {
    rb_raise(rb_eStandardError, strerror(errno));
  }
  
  hdr = (struct nlmsghdr *)buff;
  
  if (NLMSG_ERROR == hdr->nlmsg_type) {
    rb_raise(rb_eStandardError, 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, rb_intern("parent_pid"), INT2FIX(event->event_data.fork.parent_pid));
        rb_hash_aset(extra_data, rb_intern("parent_thread_group_id"), INT2FIX(event->event_data.fork.parent_tgid));
        rb_hash_aset(extra_data, rb_intern("child_pid"), INT2FIX(event->event_data.fork.child_pid));
        rb_hash_aset(extra_data, 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);

      case PROC_EVENT_NONE:
      case PROC_EVENT_EXEC:
      case PROC_EVENT_UID:
      case PROC_EVENT_GID:
        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