예제 #1
0
static void nlmsg_print(uint16_t family, struct nlmsghdr *hdr)
{
	u16 nlmsg_flags = hdr->nlmsg_flags;
	char type[32];
	char flags[128];
	char procname[PATH_MAX];

	/* Look up the process name if message is not coming from the kernel.
	 *
	 * Note that the port id is not necessarily equal to the PID of the
	 * receiving process (e.g. if the application is multithreaded or using
	 * multiple sockets). In these cases we're not able to find a matching
	 * PID and the information will not be printed.
	 */
	if (hdr->nlmsg_pid != 0) {
		if (proc_get_cmdline(hdr->nlmsg_pid, procname, sizeof(procname)) < 0)
			snprintf(procname, sizeof(procname), "unknown process");
	} else
		snprintf(procname, sizeof(procname), "kernel");

	tprintf(" [ NLMSG ");
	tprintf("Family %d (%s%s%s), ", family,
		colorize_start(bold),
		nlmsg_family2str(family),
		colorize_end());
	tprintf("Len %u, ", hdr->nlmsg_len);
	tprintf("Type 0x%.4x (%s%s%s), ", hdr->nlmsg_type,
		colorize_start(bold),
		nlmsg_type2str(family, hdr->nlmsg_type, type, sizeof(type)),
		colorize_end());
	tprintf("Flags 0x%.4x (%s%s%s), ", nlmsg_flags,
		colorize_start(bold),
		nlmsg_flags ? nl_nlmsg_flags2str(nlmsg_flags, flags, sizeof(flags)) : "none",
		colorize_end());
	tprintf("Seq-Nr %u, ", hdr->nlmsg_seq);
	tprintf("PID %u", hdr->nlmsg_pid);
	if (procname[0])
		tprintf(" (%s%s%s)", colorize_start(bold), basename(procname),
			colorize_end());
	tprintf(" ]\n");

	switch (family) {
	case NETLINK_ROUTE:
		rtnl_msg_print(hdr);
		break;
	case NETLINK_GENERIC:
		genl_msg_print(hdr);
		break;
	default:
		nlmsg_print_raw(hdr);
	}
}
예제 #2
0
int proc_find_by_inode(ino_t ino, char *cmdline, size_t len, pid_t *pid)
{
	struct dirent *ent;
	DIR *dir;

	if (ino <= 0) {
		cmdline[0] = '\0';
		return 0;
	}

	dir = opendir("/proc");
	if (!dir)
		panic("Cannot open /proc: %s\n", strerror(errno));

	while ((ent = readdir(dir))) {
		int ret;
		char *end;
		const char *name = ent->d_name;
		pid_t cur_pid = strtoul(name, &end, 10);

		/* not a PID */
		if (cur_pid == 0 && end == name)
			continue;

		ret = match_pid_by_inode(cur_pid, ino);
		if (!ret) {
			ret = proc_get_cmdline(cur_pid, cmdline, len);
			if (ret < 0)
				panic("Failed to get process cmdline: %s\n", strerror(errno));

			closedir(dir);
			*pid = cur_pid;
			return ret;
		}
	}

	closedir(dir);
	return -1;
}