Browse Source

Avoid hardcoded offsets

Use Netlink macros and offsetof to get the offset of
interesting data inside a Netlink packet.
develop
Damien Goutte-Gattat 9 years ago
parent
commit
7d5189fa94
  1. 21
      src/wait4pid.c

21
src/wait4pid.c

@ -127,10 +127,16 @@ wait4pid_init(void)
return 0;
}
struct __attribute__ ((__packed__)) cn_proc_msg {
struct cn_msg msg;
struct proc_event evt;
};
#ifdef HAVE_LINUX_FILTER_H
#include <linux/filter.h>
#include <arpa/inet.h>
#include <stddef.h>
static int
set_filter(int sock, pid_t *pids, size_t len)
@ -144,7 +150,8 @@ set_filter(int sock, pid_t *pids, size_t len)
/* Block the packet if proc_event.what != PROC_EVENT_EXIT */
flt.filter[j].code = BPF_LD+BPF_W+BPF_ABS;
flt.filter[j++].k = 36; /* proc_event.what */
flt.filter[j++].k = NLMSG_LENGTH(0)
+ offsetof(struct cn_proc_msg, evt.what);
flt.filter[j].code = BPF_JMP+BPF_JEQ+BPF_K;
flt.filter[j].k = htonl(PROC_EVENT_EXIT);
flt.filter[j].jt = 0; /* continue */
@ -152,7 +159,8 @@ set_filter(int sock, pid_t *pids, size_t len)
/* Get the process identifier */
flt.filter[j].code = BPF_LD+BPF_W+BPF_ABS;
flt.filter[j++].k = 52; /* proc_event.event_data.exit.process_pid */
flt.filter[j++].k = NLMSG_LENGTH(0)
+ offsetof(struct cn_proc_msg, evt.event_data.exit.process_pid);
for ( i = 0; i < len; i++ ) {
/* Accept the packet if its pid matches pids[i]. */
@ -182,11 +190,8 @@ int
wait4all(pid_t *pids, size_t len)
{
int ec, nproc, i;
struct __attribute__ ((__packed__ )) payload_t {
struct cn_msg msg;
struct proc_event evt;
} *payload;
char buf[NLMSG_SPACE(sizeof(struct payload_t))];
struct cn_proc_msg *payload;
char buf[NLMSG_SPACE(sizeof(struct cn_proc_msg))];
if ( ! listener_set && wait4pid_init() == -1 )
return -1;
@ -206,7 +211,7 @@ wait4all(pid_t *pids, size_t len)
if ( do_recv(sock, buf, sizeof(buf), 0) <= 0 )
return -1;
payload = (struct payload_t *) NLMSG_DATA(buf);
payload = (struct cn_proc_msg *) NLMSG_DATA(buf);
if ( payload->evt.what != PROC_EVENT_EXIT )
continue;

Loading…
Cancel
Save