Example #1
0
int main(int argc, char *argv[])
{
	int ret;
	int pid;
	char core_file[15];

	if (argc < 2 || argc > 3) {
		fprintf(stderr, "Invalid number of arguments.\n\n");
		fprintf(stderr, "Usage: %s pid [output-file-name]\n", argv[0]);
		return -1;
	}

	if (strcmp(argv[1], "--daemon") == 0)
		ret = daemon_dump();
#if HAVE_SYSTEMD_SOCKET_SUPPORT
	else if (strcmp(argv[1], "--socket") == 0) {
		fp_log = stderr;
		ret = socket_dump();
	}
#endif
	else if (strcmp(argv[1], "--help") == 0) {
		printf("Usage: %s pid [output-file-name]\n", argv[0]);
		return -1;
	} else {
		fp_log = stderr;
		pid = atoi(argv[1]);
		if (pid == 0 && argv[1][0] != '0') {
			fprintf(stderr, "Enter a valid PID.\n");
			fprintf(stderr, "Usage: %s pid [output-file-name]\n", argv[0]);
			return -1;
		}
		if (argc == 2) {
			snprintf(core_file, 15, "core.%d", pid);
			ret = do_coredump(pid, core_file);
		} else
			ret = do_coredump(pid, argv[2]);

		if (ret == -1)
			gencore_log("Failed to create core file.\n");
		else
			fprintf(stdout, "Created core file.\n");
	}

	return ret;
}
Example #2
0
static void
unixdomainpr(struct socket *so, caddr_t soaddr, u_long pcbaddr)
{
	struct unpcb unpcb, *unp = &unpcb;
	struct mbuf mbuf, *m;
	struct sockaddr_un *sa = NULL;
	static int first = 1;

	if (Pflag) {
		if (pcbaddr == (u_long)soaddr)
			socket_dump(pcbaddr);
		return;
	}

	if (kread((u_long)so->so_pcb, unp, sizeof (*unp)))
		return;
	if (unp->unp_addr) {
		m = &mbuf;
		if (kread((u_long)unp->unp_addr, m, sizeof (*m)))
			m = NULL;
		sa = (struct sockaddr_un *)(m->m_dat);
	} else
		m = NULL;
	if (first) {
		printf("Active UNIX domain sockets\n");
		printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
		    PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
		    PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
		    PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
		first = 0;
	}
	printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p",
	    PLEN, soaddr, socktype[so->so_type], so->so_rcv.sb_cc,
	    so->so_snd.sb_cc, PLEN, unp->unp_vnode, PLEN, unp->unp_conn,
	    PLEN, unp->unp_refs, PLEN, unp->unp_nextref);
	if (m)
		printf(" %.*s",
		    (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))),
		    sa->sun_path);
	putchar('\n');
}
Example #3
0
/*
 * Print a summary of connections related to an Internet
 * protocol.  For TCP, also give state of connection.
 * Listening processes (aflag) are suppressed unless the
 * -a (all) flag is specified.
 */
void
protopr(u_long off, char *name, int af, u_int tableid, u_long pcbaddr)
{
	struct inpcbtable table;
	struct inpcb *prev, *next;
	struct inpcb inpcb, prevpcb;
	int istcp, israw, isany;
	int addrlen = 22;
	int first = 1;
	char *name0;
	char namebuf[20];

	name0 = name;
	if (off == 0)
		return;
	istcp = strcmp(name, "tcp") == 0;
	israw = strncmp(name, "ip", 2) == 0;
	kread(off, &table, sizeof table);
	prev = NULL;
	next = TAILQ_FIRST(&table.inpt_queue);

	while (next != NULL) {
		kread((u_long)next, &inpcb, sizeof inpcb);
		if (prev != NULL) {
			kread((u_long)prev, &prevpcb, sizeof prevpcb);
			if (TAILQ_NEXT(&prevpcb, inp_queue) != next) {
				printf("PCB list changed\n");
				break;
			}
		}
		prev = next;
		next = TAILQ_NEXT(&inpcb, inp_queue);

		switch (af) {
		case AF_INET:
			if ((inpcb.inp_flags & INP_IPV6) != 0)
				continue;
			isany = inet_lnaof(inpcb.inp_faddr) == INADDR_ANY;
			break;
		case AF_INET6:
			if ((inpcb.inp_flags & INP_IPV6) == 0)
				continue;
			isany = IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6);
			break;
		default:
			isany = 0;
			break;
		}

		if (Pflag) {
			if (istcp && pcbaddr == (u_long)inpcb.inp_ppcb) {
				if (vflag)
					socket_dump((u_long)inpcb.inp_socket);
				else
					tcpcb_dump(pcbaddr);
			} else if (pcbaddr == (u_long)prev) {
				if (vflag)
					socket_dump((u_long)inpcb.inp_socket);
				else
					inpcb_dump(pcbaddr, 0, af);
			}
			continue;
		}

		if (inpcb.inp_rtableid != tableid)
			continue;

		kread((u_long)inpcb.inp_socket, &sockb, sizeof (sockb));
		if (istcp) {
			kread((u_long)inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb));
			if (!aflag && tcpcb.t_state <= TCPS_LISTEN)
				continue;
		} else if (!aflag && isany)
			continue;
		if (first) {
			printf("Active Internet connections");
			if (aflag)
				printf(" (including servers)");
			putchar('\n');
			if (Aflag) {
				addrlen = 18;
				printf("%-*.*s ", PLEN, PLEN, "PCB");
			}
			printf("%-7.7s %-6.6s %-6.6s ",
			    "Proto", "Recv-Q", "Send-Q");
			if (Bflag && istcp)
				printf("%-6.6s %-6.6s %-6.6s ",
				    "Recv-W", "Send-W", "Cgst-W");
			printf(" %-*.*s %-*.*s %s\n",
			    addrlen, addrlen, "Local Address",
			    addrlen, addrlen, "Foreign Address", "(state)");
			first = 0;
		}
		if (Aflag) {
			if (istcp)
				printf("%*p ", PLEN, hideroot ? 0 : inpcb.inp_ppcb);
			else
				printf("%*p ", PLEN, hideroot ? 0 : prev);
		}
		if (inpcb.inp_flags & INP_IPV6 && !israw) {
			strlcpy(namebuf, name0, sizeof namebuf);
			strlcat(namebuf, "6", sizeof namebuf);
			name = namebuf;
		} else
			name = name0;
		printf("%-7.7s %6lu %6lu ",
		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
		if (Bflag && istcp)
			printf("%6lu %6lu %6lu ", tcpcb.rcv_wnd, tcpcb.snd_wnd,
			    (tcpcb.t_state == TCPS_ESTABLISHED) ?
			    tcpcb.snd_cwnd : 0);

		if (inpcb.inp_flags & INP_IPV6) {
			inet6print(&inpcb.inp_laddr6, (int)inpcb.inp_lport,
			    name);
			inet6print(&inpcb.inp_faddr6, (int)inpcb.inp_fport,
			    name);
		} else {
			inetprint(&inpcb.inp_laddr, (int)inpcb.inp_lport,
			    name, 1);
			inetprint(&inpcb.inp_faddr, (int)inpcb.inp_fport,
			    name, 0);
		}
		if (istcp) {
			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
				printf(" %d", tcpcb.t_state);
			else
				printf(" %s", tcpstates[tcpcb.t_state]);
		} else if (israw) {
			u_int8_t proto;

			if (inpcb.inp_flags & INP_IPV6)
				proto = inpcb.inp_ipv6.ip6_nxt;
			else
				proto = inpcb.inp_ip.ip_p;
			printf(" %u", proto);
		}
		putchar('\n');
	}
}