extrace.c 13.6 KB
Newer Older
1 2 3 4 5
/* extrace - trace exec() calls system-wide
 *
 * Requires CONFIG_CONNECTOR=y and CONFIG_PROC_EVENTS=y.
 * Requires root or "setcap cap_net_admin+ep extrace".
 *
6
 * Usage: extrace [-deflqu] [-o FILE] [-p PID|CMD...]
7 8 9
 * default: show all exec(), globally
 * -p PID   only show exec() descendant of PID
 * CMD...   run CMD... and only show exec() descendant of it
10
 * -o FILE  log to FILE instead of standard output
11 12
 * -d       print cwd of process
 * -e       print environment of process
13
 * -f       flat output: no indentation
Christian Neukirchen's avatar
Christian Neukirchen committed
14 15
 * -l       print full path of argv[0]
 * -q       don't print exec() arguments
16
 * -u       print user of process
17
 *
Leah Neukirchen's avatar
Leah Neukirchen committed
18
 * Copyright (C) 2014-2019 Leah Neukirchen <leah@vuxu.org>
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
 *
 * hacked from sources of:
 */
/* exec-notify, so you can watch your acrobat reader or vim executing "bash -c"
 * commands ;-)
 * Requires some 2.6.x Linux kernel with proc connector enabled.
 *
 * $  cc -Wall -ansi -pedantic -std=c99 exec-notify.c
 *
 * (C) 2007-2010 Sebastian Krahmer <krahmer@suse.de> original netlink handling
 * stolen from an proc-connector example, copyright folows:
 */
/* Copyright (C) Matt Helsley, IBM Corp. 2005
 * Derived from fcctl.c by Guillaume Thouvenin
 * Original copyright notice follows:
 *
 * Copyright (C) 2005 BULL SA.
 * Written by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define _XOPEN_SOURCE 700

Leah Neukirchen's avatar
Leah Neukirchen committed
55
#include <linux/cn_proc.h>
Leah Neukirchen's avatar
Leah Neukirchen committed
56 57 58 59
#include <linux/connector.h>
#include <linux/netlink.h>

#include <sys/socket.h>
60
#include <sys/stat.h>
Leah Neukirchen's avatar
Leah Neukirchen committed
61 62 63
#include <sys/types.h>
#include <sys/wait.h>

64
#include <errno.h>
65 66
#include <fcntl.h>
#include <limits.h>
67
#include <pwd.h>
68
#include <signal.h>
69
#include <stdint.h>
Leah Neukirchen's avatar
Leah Neukirchen committed
70
#include <stdio.h>
71 72
#include <stdlib.h>
#include <string.h>
73
#include <time.h>
74 75
#include <unistd.h>

Leah Neukirchen's avatar
Leah Neukirchen committed
76 77
#define max(x, y) ((y) < (x) ? (x) : (y))
#define min(x, y) ((y) > (x) ? (x) : (y))
78 79

#define SEND_MESSAGE_LEN (NLMSG_LENGTH(sizeof (struct cn_msg) + \
Christian Neukirchen's avatar
Christian Neukirchen committed
80
                                       sizeof (enum proc_cn_mcast_op)))
81
#define RECV_MESSAGE_LEN (NLMSG_LENGTH(sizeof (struct cn_msg) + \
Christian Neukirchen's avatar
Christian Neukirchen committed
82
                                       sizeof (struct proc_event)))
83

Leah Neukirchen's avatar
Leah Neukirchen committed
84 85
#define SEND_MESSAGE_SIZE (NLMSG_SPACE(SEND_MESSAGE_LEN))
#define RECV_MESSAGE_SIZE (NLMSG_SPACE(RECV_MESSAGE_LEN))
86 87 88 89

#define BUFF_SIZE (max(max(SEND_MESSAGE_SIZE, RECV_MESSAGE_SIZE), 1024))
#define MIN_RECV_SIZE (min(SEND_MESSAGE_SIZE, RECV_MESSAGE_SIZE))

90
#define CMDLINE_MAX 32768
91
#define CMDLINE_DB_MAX 32
92 93
pid_t parent = 1;
int flat = 0;
94
int run = 0;
Christian Neukirchen's avatar
Christian Neukirchen committed
95 96
int full_path = 0;
int show_args = 1;
97
int show_cwd = 0;
98
int show_env = 0;
99
int show_exit = 0;
100
int show_user = 0;
101 102
FILE *output;
sig_atomic_t quit = 0;
103 104
#define CPU_MAX 4096
uint32_t last_seq[CPU_MAX];
105

106 107
#define PID_DB_SIZE 1024
struct {
Leah Neukirchen's avatar
Leah Neukirchen committed
108 109
	pid_t pid;
	int depth;
110
	uint64_t start;
Leah Neukirchen's avatar
Leah Neukirchen committed
111
	char cmdline[CMDLINE_DB_MAX];
112 113
} pid_db[PID_DB_SIZE];

114 115 116 117 118 119 120
static int
open_proc_dir(pid_t pid) {
	char name[48];
	snprintf(name, sizeof name, "/proc/%d", pid);
	return open(name, O_DIRECTORY);
}

121
static int
122 123
pid_depth(pid_t pid)
{
Leah Neukirchen's avatar
Leah Neukirchen committed
124 125
	pid_t ppid = 0;
	char name[PATH_MAX];
126 127 128
	char buf[2048];
	char *s;
	int fd, d, i;
129

Leah Neukirchen's avatar
Leah Neukirchen committed
130
	snprintf(name, sizeof name, "/proc/%d/stat", pid);
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	if ((fd = open(name, O_RDONLY)) < 0)
		return 0;
	if (read(fd, buf, sizeof buf) <= 0)
		return 0;
	close(fd);

	s = strrchr(buf, ')');  /* find end of COMM (recommended way) */
	if (!s)
		return 0;
	while (*++s == ' ')
		;
	/* skip over STATE */
	while (*++s == ' ')
		;
	errno = 0;
	ppid = strtol(s, 0, 10);  /* parse PPID */
	if (errno != 0)
		return 0;
150

Leah Neukirchen's avatar
Leah Neukirchen committed
151 152
	if (ppid == parent)
		return 0;
153

Leah Neukirchen's avatar
Leah Neukirchen committed
154 155
	if (ppid == 0)
		return -1;  /* a parent we are not interested in */
156

157 158 159 160 161 162
	for (i = 0; i < PID_DB_SIZE - 1; i++)
		if (pid_db[i].pid == ppid)
			d = pid_db[i].depth;
	if (i == PID_DB_SIZE - 1)
		d = pid_depth(ppid);  /* we need to recurse */

Leah Neukirchen's avatar
Leah Neukirchen committed
163 164
	if (d == -1)
		return -1;
165

166
	return d + 1;
167 168
}

Leah Neukirchen's avatar
Leah Neukirchen committed
169
static const char *
170 171 172 173
sig2name(int sig)
{
	switch (sig) {
#define X(s) case s: return #s;
174
		/* signals defined in POSIX.1-1990 */
175 176 177 178 179 180 181 182
		X(SIGHUP)
		X(SIGINT)
		X(SIGQUIT)
		X(SIGILL)
		X(SIGABRT)
		X(SIGFPE)
		X(SIGKILL)
		X(SIGSEGV)
183
		X(SIGUSR1)
184 185 186 187 188 189 190 191 192 193
		X(SIGUSR2)
		X(SIGPIPE)
		X(SIGALRM)
		X(SIGTERM)
		X(SIGCHLD)
		X(SIGCONT)
		X(SIGSTOP)
		X(SIGTSTP)
		X(SIGTTIN)
		X(SIGTTOU)
194 195 196 197 198 199 200

		/* signals defined in POSIX.1-2001 */
		X(SIGBUS)
		X(SIGPOLL)
		X(SIGPROF)
		X(SIGSYS)
		X(SIGTRAP)
201
		X(SIGURG)
202
		X(SIGVTALRM)
203 204
		X(SIGXCPU)
		X(SIGXFSZ)
205 206 207 208 209 210 211 212 213 214 215 216

		/* other signals */
#ifdef SIGSTKFLT
		X(SIGSTKFLT)  /* Stack fault on coprocessor (unused) */
#endif
#ifdef SIGWINCH
		X(SIGWINCH)   /* Window resize signal (4.3BSD) */
#endif
#ifdef SIGPWR
		X(SIGPWR)     /* Power failure (System V) */
#endif

217 218 219 220 221 222 223 224 225
#undef X
	default: {
		static char buf[8];
		snprintf(buf, sizeof buf, "SIG%d", sig);
		return buf;
	}
	}
}

226 227 228
static void
sigchld(int sig)
{
Leah Neukirchen's avatar
Leah Neukirchen committed
229 230 231 232
	(void)sig;
	while (waitpid(-1, NULL, WNOHANG) > 0)
		;
	quit = 1;
233 234
}

235 236 237
static void
print_shquoted(const char *s)
{
Leah Neukirchen's avatar
Leah Neukirchen committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	if (*s && !strpbrk(s,
	    "\001\002\003\004\005\006\007\010"
	    "\011\012\013\014\015\016\017\020"
	    "\021\022\023\024\025\026\027\030"
	    "\031\032\033\034\035\036\037\040"
	    "`^#*[]=|\\?${}()'\"<>&;\177")) {
		fprintf(output, "%s", s);
		return;
	}

	putc('\'', output);
	for (; *s; s++)
		if (*s == '\'')
			fprintf(output, "'\\''");
		else if (*s == '\n')
			fprintf(output, "'$'\\n''");
		else
			putc(*s, output);
	putc('\'', output);
}
258

Leah Neukirchen's avatar
Leah Neukirchen committed
259
static void
260
print_env(int proc_dir_fd)
Leah Neukirchen's avatar
Leah Neukirchen committed
261
{
262
	int fd;
Leah Neukirchen's avatar
Leah Neukirchen committed
263 264 265
	FILE *env;

	fprintf(output, "  ");
266 267
	fd = openat(proc_dir_fd, "environ", O_RDONLY);
	if (fd >= 0 && (env = fdopen(fd, "r"))) {
Leah Neukirchen's avatar
Leah Neukirchen committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
		char *line = 0, *eq = 0;
		size_t linelen = 0;
		while (getdelim(&line, &linelen, '\0', env) >= 0) {
			putc(' ', output);
			if ((eq = strchr(line, '='))) {
				/* print split so = doesn't trigger escaping.  */
				*eq = 0;
				print_shquoted(line);
				putc('=', output);
				print_shquoted(eq+1);
			} else {
				/* weird env entry without equal sign.  */
				print_shquoted(line);
			}
		}
		free(line);
		fclose(env);
	} else {
		fprintf(output, " -");
	}
288 289
}

290
static void
291 292
handle_msg(struct cn_msg *cn_hdr)
{
293
	char cmdline[CMDLINE_MAX];
Leah Neukirchen's avatar
Leah Neukirchen committed
294 295 296 297 298 299 300 301 302 303
	char exe[PATH_MAX];
	char cwd[PATH_MAX];
	char *argvrest;

	int r = 0, r2 = 0, r3 = 0, fd, d;
	struct proc_event *ev = (struct proc_event *)cn_hdr->data;

	if (ev->what == PROC_EVENT_EXEC) {
		pid_t pid = ev->event_data.exec.process_pid;
		int i = 0;
304 305
		int proc_dir_fd = open_proc_dir(pid);
		if (proc_dir_fd < 0) {
306 307 308
			fprintf(stderr,
			    "extrace: process vanished before notification: pid %d\n",
			    pid);
309 310
			return;
		}
Leah Neukirchen's avatar
Leah Neukirchen committed
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		memset(&cmdline, 0, sizeof cmdline);
		fd = openat(proc_dir_fd, "cmdline", O_RDONLY);
		if (fd >= 0) {
			r = read(fd, cmdline, sizeof cmdline);
			close(fd);

			if (r > 0)
				cmdline[r] = 0;

			if (full_path) {
				r2 = readlinkat(proc_dir_fd, "exe", exe, sizeof exe);
				if (r2 > 0)
					exe[r2] = 0;
			}

			argvrest = strchr(cmdline, 0) + 1;
		}

Leah Neukirchen's avatar
Leah Neukirchen committed
330
		d = pid_depth(pid);
331
		if (d < 0) {
332 333 334 335 336 337 338 339 340
			if (*cmdline) {
				fprintf(stderr,
				    "extrace: process vanished before we found its parent: pid %d: %s\n",
				    pid, cmdline);
			} else {
				fprintf(stderr,
				    "extrace: process vanished without a name: pid %d\n",
				    pid);
			}
341
			close(proc_dir_fd);
Leah Neukirchen's avatar
Leah Neukirchen committed
342
			return;
343
		}
Leah Neukirchen's avatar
Leah Neukirchen committed
344

345
		if (show_exit || !flat) {
Leah Neukirchen's avatar
Leah Neukirchen committed
346 347 348 349
			for (i = 0; i < PID_DB_SIZE - 1; i++)
				if (pid_db[i].pid == 0)
					break;
			if (i == PID_DB_SIZE - 1)
350
				fprintf(stderr, "extrace: warning: pid_db of "
Leah Neukirchen's avatar
Leah Neukirchen committed
351
				    "size %d overflowed\n", PID_DB_SIZE);
Leah Neukirchen's avatar
Leah Neukirchen committed
352

Leah Neukirchen's avatar
Leah Neukirchen committed
353 354
			pid_db[i].pid = pid;
			pid_db[i].depth = d;
355
			pid_db[i].start = ev->timestamp_ns;
Leah Neukirchen's avatar
Leah Neukirchen committed
356 357 358
		}

		if (show_cwd) {
359
			r3 = readlinkat(proc_dir_fd, "cwd", cwd, sizeof cwd);
Leah Neukirchen's avatar
Leah Neukirchen committed
360 361 362 363 364 365 366 367 368
			if (r3 > 0)
				cwd[r3] = 0;
		}

		if (!flat)
			fprintf(output, "%*s", 2*d, "");
		fprintf(output, "%d", pid);
		if (show_exit) {
			putc('+', output);
369 370
			snprintf(pid_db[i].cmdline, CMDLINE_DB_MAX,
			    "%s", cmdline);
Leah Neukirchen's avatar
Leah Neukirchen committed
371
		}
372 373 374 375 376 377 378 379 380 381 382
		if (show_user) {
			struct stat st;
			struct passwd *p;

			if (fstat(proc_dir_fd, &st) < 0)
				st.st_uid = -1;
			if ((p = getpwuid(st.st_uid)))
				fprintf(output," <%s>", p->pw_name);
			else
				fprintf(output," <%d>", st.st_uid);
		}
Leah Neukirchen's avatar
Leah Neukirchen committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		putc(' ', output);
		if (show_cwd) {
			print_shquoted(cwd);
			fprintf(output, " %% ");
		}

		if (full_path)
			print_shquoted(exe);
		else
			print_shquoted(cmdline);

		if (show_args && r > 0) {
			while (argvrest - cmdline < r) {
				putc(' ', output);
				print_shquoted(argvrest);
				argvrest = strchr(argvrest, 0)+1;
			}
		}

		if (r == sizeof cmdline)
			fprintf(output, "... <truncated>");

405 406 407 408
		if (show_env)
			print_env(proc_dir_fd);

		close(proc_dir_fd);
Leah Neukirchen's avatar
Leah Neukirchen committed
409 410 411

		fprintf(output, "\n");
		fflush(output);
412
	} else if ((show_exit || !flat) && ev->what == PROC_EVENT_EXIT) {
Leah Neukirchen's avatar
Leah Neukirchen committed
413 414 415 416 417 418 419 420
		pid_t pid = ev->event_data.exit.process_pid;
		int i;

		for (i = 0; i < PID_DB_SIZE; i++)
			if (pid_db[i].pid == pid)
				break;
		if (i == PID_DB_SIZE)
			return;
Leah Neukirchen's avatar
Leah Neukirchen committed
421

Leah Neukirchen's avatar
Leah Neukirchen committed
422
		pid_db[i].pid = 0;
423 424 425 426

		if (!show_exit)
			return;

Leah Neukirchen's avatar
Leah Neukirchen committed
427 428 429 430 431 432
		if (!flat)
			fprintf(output, "%*s",
			    2*pid_db[i].depth, "");

		fprintf(output, "%d- ", pid);
		print_shquoted(pid_db[i].cmdline);
433

Leah Neukirchen's avatar
Leah Neukirchen committed
434
		if (!WIFEXITED(ev->event_data.exit.exit_code))
435
			fprintf(output, " exited signal=%s",
Leah Neukirchen's avatar
Leah Neukirchen committed
436
			    sig2name(WTERMSIG(ev->event_data.exit.exit_code)));
437 438
		else
			fprintf(output, " exited status=%d",
Leah Neukirchen's avatar
Leah Neukirchen committed
439
			    WEXITSTATUS(ev->event_data.exit.exit_code));
Leah Neukirchen's avatar
Leah Neukirchen committed
440 441
		fprintf(output, " time=%.3fs\n",
		    (ev->timestamp_ns - pid_db[i].start) / 1e9);
Leah Neukirchen's avatar
Leah Neukirchen committed
442 443
		fflush(output);
	}
444 445
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static pid_t
parse_pid(char *s)
{
	pid_t pid;
	char *end;

	errno = 0;
	pid = strtol(s, &end, 10);
	if (pid <= 0 || *end || errno != 0) {
		fprintf(stderr, "extrace: %s: invalid process id\n", s);
		exit(1);
	}

	errno = 0;
	kill(pid, 0);
	if (errno == ESRCH) {
		fprintf(stderr, "extrace: %s: no such process\n", s);
		exit(1);
	}

	return pid;
}

469 470 471
int
main(int argc, char *argv[])
{
Leah Neukirchen's avatar
Leah Neukirchen committed
472 473 474 475 476
	int sk_nl;
	struct sockaddr_nl my_nla, kern_nla, from_nla;
	socklen_t from_nla_len;
	char buff[BUFF_SIZE];
	struct nlmsghdr *nl_hdr, *nlh;
477 478
	struct cn_msg *cn_hdr, *cmsg;
	struct proc_event *cproc;
Leah Neukirchen's avatar
Leah Neukirchen committed
479 480 481 482 483 484
	enum proc_cn_mcast_op *mcop_msg;
	size_t recv_len = 0;
	int rc = -1, opt;

	output = stdout;

485
	while ((opt = getopt(argc, argv, "+deflo:p:qtwu")) != -1)
Leah Neukirchen's avatar
Leah Neukirchen committed
486 487 488 489 490
		switch (opt) {
		case 'd': show_cwd = 1; break;
		case 'e': show_env = 1; break;
		case 'f': flat = 1; break;
		case 'l': full_path = 1; break;
491
		case 'p': parent = parse_pid(optarg); break;
Leah Neukirchen's avatar
Leah Neukirchen committed
492 493 494 495 496 497 498 499 500 501
		case 'q': show_args = 0; break;
		case 't': show_exit = 1; break;
		case 'o':
			output = fopen(optarg, "w");
			if (!output) {
				perror("fopen");
				exit(1);
			}
			break;
		case 'w': /* obsoleted, ignore */; break;
502
		case 'u': show_user = 1; break;
Leah Neukirchen's avatar
Leah Neukirchen committed
503 504 505 506
		default: goto usage;
		}

	if (parent != 1 && optind != argc) {
Leah Neukirchen's avatar
Leah Neukirchen committed
507
usage:
Leah Neukirchen's avatar
Leah Neukirchen committed
508
		fprintf(stderr, "Usage: extrace [-deflqt] [-o FILE] [-p PID|CMD...]\n");
Leah Neukirchen's avatar
Leah Neukirchen committed
509
		exit(1);
Leah Neukirchen's avatar
Leah Neukirchen committed
510
	}
511

Leah Neukirchen's avatar
Leah Neukirchen committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	sk_nl = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
	if (sk_nl == -1) {
		perror("socket sk_nl error");
		exit(1);
	}

	my_nla.nl_family = AF_NETLINK;
	my_nla.nl_groups = CN_IDX_PROC;
	my_nla.nl_pid = getpid();

	kern_nla.nl_family = AF_NETLINK;
	kern_nla.nl_groups = CN_IDX_PROC;
	kern_nla.nl_pid = 1;

	if (bind(sk_nl, (struct sockaddr *)&my_nla, sizeof my_nla) == -1) {
		perror("binding sk_nl error");
		goto close_and_exit;
	}
	nl_hdr = (struct nlmsghdr *)buff;
	cn_hdr = (struct cn_msg *)NLMSG_DATA(nl_hdr);
Leah Neukirchen's avatar
Leah Neukirchen committed
532
	mcop_msg = (enum proc_cn_mcast_op *)&cn_hdr->data[0];
Leah Neukirchen's avatar
Leah Neukirchen committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

	memset(buff, 0, sizeof buff);
	*mcop_msg = PROC_CN_MCAST_LISTEN;

	nl_hdr->nlmsg_len = SEND_MESSAGE_LEN;
	nl_hdr->nlmsg_type = NLMSG_DONE;
	nl_hdr->nlmsg_flags = 0;
	nl_hdr->nlmsg_seq = 0;
	nl_hdr->nlmsg_pid = getpid();

	cn_hdr->id.idx = CN_IDX_PROC;
	cn_hdr->id.val = CN_VAL_PROC;
	cn_hdr->seq = 0;
	cn_hdr->ack = 0;
	cn_hdr->len = sizeof (enum proc_cn_mcast_op);

	if (send(sk_nl, nl_hdr, nl_hdr->nlmsg_len, 0) != nl_hdr->nlmsg_len) {
		printf("failed to send proc connector mcast ctl op!\n");
		goto close_and_exit;
	}

	if (*mcop_msg == PROC_CN_MCAST_IGNORE) {
		rc = 0;
		goto close_and_exit;
	}

	if (optind != argc) {
		pid_t child;

		parent = getpid();
		signal(SIGCHLD, sigchld);

		child = fork();
		if (child == -1) {
			perror("fork");
			goto close_and_exit;
		}
		if (child == 0) {
			execvp(argv[optind], argv+optind);
			perror("execvp");
			goto close_and_exit;
		}
	}

	rc = 0;
	while (!quit) {
579 580 581
		cmsg = (struct cn_msg *)(buff + sizeof (struct nlmsghdr));
		cproc = (struct proc_event *)(buff + sizeof (struct nlmsghdr) + sizeof (struct cn_msg));

Leah Neukirchen's avatar
Leah Neukirchen committed
582 583 584 585 586 587 588 589
		memset(buff, 0, sizeof buff);
		from_nla_len = sizeof from_nla;
		nlh = (struct nlmsghdr *)buff;
		memcpy(&from_nla, &kern_nla, sizeof from_nla);
		recv_len = recvfrom(sk_nl, buff, BUFF_SIZE, 0,
		    (struct sockaddr *)&from_nla, &from_nla_len);
		if (from_nla.nl_pid != 0 || recv_len < 1)
			continue;
590 591 592

		if (cproc->what == PROC_EVENT_NONE)
			continue;
593 594 595 596 597 598 599
	
		if (last_seq[cproc->cpu] &&
		    cmsg->seq != last_seq[cproc->cpu] + 1)
			fprintf(stderr,
			    "extrace: out of order message on cpu %d\n",
			    cproc->cpu);
		last_seq[cproc->cpu] = cmsg->seq;
Leah Neukirchen's avatar
Leah Neukirchen committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

		while (NLMSG_OK(nlh, recv_len)) {
			if (nlh->nlmsg_type == NLMSG_NOOP)
				continue;
			if (nlh->nlmsg_type == NLMSG_ERROR ||
			    nlh->nlmsg_type == NLMSG_OVERRUN)
				break;

			handle_msg(NLMSG_DATA(nlh));

			if (nlh->nlmsg_type == NLMSG_DONE)
				break;
			nlh = NLMSG_NEXT(nlh, recv_len);
		}
	}
615 616

close_and_exit:
Leah Neukirchen's avatar
Leah Neukirchen committed
617 618
	close(sk_nl);
	return rc;
619
}