|
| 1 | +#include <sys/socket.h> |
| 2 | +#include <linux/netlink.h> |
| 3 | +#include <linux/connector.h> |
| 4 | +#include <linux/cn_proc.h> |
| 5 | +#include <signal.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <stdbool.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <string.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <stdio.h> |
| 12 | + |
| 13 | +#include "and.h" |
| 14 | + |
| 15 | +bool need_exit = false; |
| 16 | +/* |
| 17 | + * connect to netlink |
| 18 | + * returns netlink socket, or -1 on error |
| 19 | + */ |
| 20 | +int nl_connect() |
| 21 | +{ |
| 22 | + int rc; |
| 23 | + int nl_sock; |
| 24 | + struct sockaddr_nl sa_nl; |
| 25 | + |
| 26 | + nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); |
| 27 | + if (nl_sock == -1) { |
| 28 | + perror("socket"); |
| 29 | + return -1; |
| 30 | + } |
| 31 | + |
| 32 | + sa_nl.nl_family = AF_NETLINK; |
| 33 | + sa_nl.nl_groups = CN_IDX_PROC; |
| 34 | + sa_nl.nl_pid = getpid(); |
| 35 | + |
| 36 | + rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl)); |
| 37 | + if (rc == -1) { |
| 38 | + perror("bind"); |
| 39 | + close(nl_sock); |
| 40 | + return -1; |
| 41 | + } |
| 42 | + |
| 43 | + return nl_sock; |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | + * subscribe on proc events (process notifications) |
| 48 | + */ |
| 49 | +int proc_set_ev_listen(int nl_sock, bool enable) |
| 50 | +{ |
| 51 | + int rc; |
| 52 | + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { |
| 53 | + struct nlmsghdr nl_hdr; |
| 54 | + struct __attribute__ ((__packed__)) { |
| 55 | + struct cn_msg cn_msg; |
| 56 | + enum proc_cn_mcast_op cn_mcast; |
| 57 | + }; |
| 58 | + } nlcn_msg; |
| 59 | + |
| 60 | + memset(&nlcn_msg, 0, sizeof(nlcn_msg)); |
| 61 | + nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg); |
| 62 | + nlcn_msg.nl_hdr.nlmsg_pid = getpid(); |
| 63 | + nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE; |
| 64 | + |
| 65 | + nlcn_msg.cn_msg.id.idx = CN_IDX_PROC; |
| 66 | + nlcn_msg.cn_msg.id.val = CN_VAL_PROC; |
| 67 | + nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op); |
| 68 | + |
| 69 | + nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE; |
| 70 | + |
| 71 | + rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0); |
| 72 | + if (rc == -1) { |
| 73 | + perror("netlink send"); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +/* |
| 81 | + * handle a single process event |
| 82 | + */ |
| 83 | +int proc_handle_ev(int nl_sock) |
| 84 | +{ |
| 85 | + int rc; |
| 86 | + |
| 87 | + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { |
| 88 | + struct nlmsghdr nl_hdr; |
| 89 | + struct __attribute__ ((__packed__)) { |
| 90 | + struct cn_msg cn_msg; |
| 91 | + struct proc_event proc_ev; |
| 92 | + }; |
| 93 | + } nlcn_msg; |
| 94 | + struct sockaddr_nl addr; |
| 95 | + socklen_t addrlen; |
| 96 | + |
| 97 | + while (!need_exit) { |
| 98 | + rc = recvfrom(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0, (struct sockaddr *)&addr, &addrlen); |
| 99 | + if (addr.nl_pid) /* check that message is from kernel */ |
| 100 | + continue; |
| 101 | + if (rc == 0) { |
| 102 | + /* shutdown? */ |
| 103 | + return -1; |
| 104 | + } else if (rc == -1) { |
| 105 | + and_printf(0, "netlink receive error\n"); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + switch (nlcn_msg.proc_ev.what) { |
| 109 | + case PROC_EVENT_NONE: |
| 110 | + and_printf(1, "start listening to netlink...\n"); |
| 111 | + break; |
| 112 | + case PROC_EVENT_FORK: |
| 113 | + //if (fork() == 0) /* handle event in child */ |
| 114 | + // return nlcn_msg.proc_ev.event_data.fork.parent_pid; |
| 115 | + //and_printf(1, "FORK pid=%d ppid=%d\n", nlcn_msg.proc_ev.event_data.fork.child_pid,nlcn_msg.proc_ev.event_data.fork.parent_pid); |
| 116 | + break; |
| 117 | + case PROC_EVENT_EXEC: |
| 118 | + if (fork() == 0) /* handle event in child */ |
| 119 | + return nlcn_msg.proc_ev.event_data.exec.process_pid; |
| 120 | + and_printf(2, "EXEC pid=%d\n", nlcn_msg.proc_ev.event_data.exec.process_pid); |
| 121 | + break; |
| 122 | + case PROC_EVENT_UID: |
| 123 | + case PROC_EVENT_GID: |
| 124 | + case PROC_EVENT_SID: |
| 125 | + case PROC_EVENT_PTRACE: |
| 126 | + case PROC_EVENT_COMM: |
| 127 | + case PROC_EVENT_EXIT: |
| 128 | + break; |
| 129 | + default: |
| 130 | + and_printf(1, "unhandled proc_event %d\n", nlcn_msg.proc_ev.what); |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | + |
0 commit comments