-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
232 lines (215 loc) · 5.96 KB
/
main.c
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
223
224
225
226
227
228
229
230
231
232
/*
* Process/Thread Start Monitor
* Copyright (C) 2011 Philip J. Turmel <[email protected]>
*
* Inspired by a blog entry by Scott James Remnant:
* http://netsplit.com/2011/02/09/the-proc-connector-and-socket-filters/
*
* Maintained at http://github.com/pturmel/startmon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <linux/netlink.h>
#include <linux/connector.h>
#include <linux/cn_proc.h>
void usage(char *arg0) {
fprintf(stderr, "Usage:\n %s [-eft] [--exec] [--fork] [--thread]\n\n"
"Specify at least '--exec' or '--fork'\n",
arg0);
exit(1);
}
const char my_short_opts[] = "efht";
struct option my_long_opts[] = {
{"exec", 0, NULL, 'e'},
{"fork", 0, NULL, 'f'},
{"help", 0, NULL, 'h'},
{"thread", 0, NULL, 't'},
{}
};
int execflag = 0;
int forkflag = 0;
int threadflag = 0;
char procname[50];
char cmdlinebuf[4096];
ssize_t bytes;
char *get_cmdline(pid_t id) {
int fd;
ssize_t remain;
char *c;
sprintf(procname, "/proc/%d/cmdline", id);
fd = open(procname, O_RDONLY);
if (fd != -1) {
bytes = read(fd, cmdlinebuf, 4096);
close(fd);
for (c=cmdlinebuf, remain=bytes; remain>0; c++, remain--)
if (*c < 32)
*c = ' ';
*c = 0;
} else
bytes = sprintf(cmdlinebuf, "<N/A>");
return cmdlinebuf;
}
/* Given a single connection message, evaluate and process
* the enclosed proc message.
*/
void dispatch_nl_cn(struct cn_msg *hdr) {
if (hdr->id.idx != CN_IDX_PROC || hdr->id.val != CN_VAL_PROC)
return;
struct proc_event *pe = (struct proc_event *)hdr->data;
switch (pe->what) {
case PROC_EVENT_FORK:
if (!forkflag)
break;
if (pe->event_data.fork.child_pid == pe->event_data.fork.child_tgid) {
/* Regular fork, parent process is the originator */
printf("Fork %d %d %s\n",
pe->event_data.fork.parent_pid,
pe->event_data.fork.child_pid,
get_cmdline(pe->event_data.fork.child_tgid));
} else {
/* Thread fork, thread group leader is the originator */
if (threadflag)
printf("Thread %d %d %s\n",
pe->event_data.fork.child_tgid,
pe->event_data.fork.child_pid,
get_cmdline(pe->event_data.fork.child_tgid));
}
break;
case PROC_EVENT_EXEC:
if (!execflag)
break;
if (pe->event_data.exec.process_pid == pe->event_data.exec.process_tgid) {
/* Thread group leader did an exec */
printf("Exec - %d %s\n",
pe->event_data.exec.process_pid,
get_cmdline(pe->event_data.exec.process_tgid));
} else {
/* Subordinate thread did an exec */
if (threadflag)
printf("Exec %d %d %s\n",
pe->event_data.exec.process_tgid,
pe->event_data.exec.process_pid,
get_cmdline(pe->event_data.exec.process_tgid));
}
break;
}
}
/* Given a single netlink message, evaluate and process the
* enclosed container message.
*/
void dispatch_nl(struct nlmsghdr *nlhdr) {
switch (nlhdr->nlmsg_type) {
case NLMSG_ERROR:
case NLMSG_NOOP:
return;
case NLMSG_OVERRUN:
printf("overrun\n");
return;
default:
dispatch_nl_cn(NLMSG_DATA(nlhdr));
}
}
int main(int argc, char **argv) {
int opt, longidx, nlsock, rc;
struct sockaddr_nl nladdr = {AF_NETLINK};
socklen_t nlalen;
pid_t mypid;
ssize_t bcount;
char *called;
void *rcvbuf;
/* Isolate the base name of the program as invoked. */
called = strrchr(argv[0], '/');
if (!called)
called = argv[0];
/* Parse the given options, looking for the filtering mode. */
while ((opt = getopt_long(argc, argv, my_short_opts, my_long_opts, &longidx)) != -1) {
switch (opt) {
case 'e':
execflag = 1;
break;
case 'f':
forkflag = 1;
break;
case 't':
threadflag = 1;
break;
default:
if (opt != 'h')
fprintf(stderr, "%s: Invalid option '%c' !\n", called, opt);
usage(called);
}
}
/* If no filtering mode, bail out. */
if (!(execflag || forkflag)) {
fprintf(stderr, "%s: Missing required mode option!\n", called);
usage(called);
}
/* Create the netlink socket */
nlsock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_CONNECTOR);
if (nlsock == -1) {
perror("Unable to open a netlink socket!");
exit(1);
}
/* Attach to the process connector group */
{
nladdr.nl_pid = mypid = getpid();
nladdr.nl_groups = CN_IDX_PROC;
if (bind(nlsock, (struct sockaddr *)&nladdr, sizeof(nladdr))) {
perror("Unable to bind to the process connector!");
exit(1);
}
}
/* Request the process messages */
{
enum proc_cn_mcast_op cnop = PROC_CN_MCAST_LISTEN;
struct cn_msg cnmsg = {{CN_IDX_PROC, CN_VAL_PROC}, 0, 0, sizeof(cnop), 0};
struct nlmsghdr nlmsg = {NLMSG_LENGTH(sizeof cnmsg + sizeof cnop), NLMSG_DONE};
char padding[16];
struct iovec iov[4] = {
{&nlmsg, sizeof(nlmsg)},
{padding, NLMSG_LENGTH(0) - sizeof(nlmsg)},
{&cnmsg, sizeof(cnmsg)},
{&cnop, sizeof(cnop)}
};
nlmsg.nlmsg_pid = mypid;
if ((bcount = writev(nlsock, iov, 4)) == -1) {
perror("Unable to listen to the process connector!");
exit(1);
}
}
/* Receive messages forever ... */
rcvbuf = malloc(4096+CONNECTOR_MAX_MSG_SIZE);
if (!rcvbuf) {
perror("Unable to allocate a receive buffer!");
exit(1);
}
setbuf(stdout, NULL);
while (1) {
nlalen = sizeof(nladdr);
bcount = recvfrom(nlsock, rcvbuf, 4096+CONNECTOR_MAX_MSG_SIZE,
0, (struct sockaddr *)&nladdr, &nlalen);
if (nladdr.nl_pid == 0) {
struct nlmsghdr *hdr = rcvbuf;
for (hdr=rcvbuf; NLMSG_OK(hdr, bcount); hdr=NLMSG_NEXT(hdr, bcount))
dispatch_nl(hdr);
}
}
}