1221807Sstas/*-
2221807Sstas * Copyright (c) 2009 Ulf Lilleengen
3221807Sstas * All rights reserved.
4221807Sstas *
5221807Sstas * Redistribution and use in source and binary forms, with or without
6221807Sstas * modification, are permitted provided that the following conditions
7221807Sstas * are met:
8221807Sstas * 1. Redistributions of source code must retain the above copyright
9221807Sstas *    notice, this list of conditions and the following disclaimer.
10221807Sstas * 2. Redistributions in binary form must reproduce the above copyright
11221807Sstas *    notice, this list of conditions and the following disclaimer in the
12221807Sstas *    documentation and/or other materials provided with the distribution.
13221807Sstas *
14221807Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15221807Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221807Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221807Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18221807Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221807Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221807Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221807Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221807Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221807Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221807Sstas * SUCH DAMAGE.
25221807Sstas *
26221807Sstas * $FreeBSD: stable/11/lib/libutil/kinfo_getproc.c 312031 2017-01-13 08:39:23Z ngie $
27221807Sstas */
28221807Sstas
29221807Sstas#include <sys/cdefs.h>
30221807Sstas__FBSDID("$FreeBSD: stable/11/lib/libutil/kinfo_getproc.c 312031 2017-01-13 08:39:23Z ngie $");
31221807Sstas
32221807Sstas#include <sys/param.h>
33312031Sngie#include <sys/sysctl.h>
34221807Sstas#include <sys/user.h>
35221807Sstas#include <stdlib.h>
36221807Sstas#include <string.h>
37221807Sstas
38221807Sstas#include "libutil.h"
39221807Sstas
40221807Sstasstruct kinfo_proc *
41221807Sstaskinfo_getproc(pid_t pid)
42221807Sstas{
43221807Sstas	struct kinfo_proc *kipp;
44221807Sstas	int mib[4];
45221807Sstas	size_t len;
46221807Sstas
47221807Sstas	len = 0;
48221807Sstas	mib[0] = CTL_KERN;
49221807Sstas	mib[1] = KERN_PROC;
50221807Sstas	mib[2] = KERN_PROC_PID;
51221807Sstas	mib[3] = pid;
52312031Sngie	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
53221807Sstas		return (NULL);
54221807Sstas
55221807Sstas	kipp = malloc(len);
56221807Sstas	if (kipp == NULL)
57221807Sstas		return (NULL);
58221807Sstas
59312031Sngie	if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0)
60221807Sstas		goto bad;
61221807Sstas	if (len != sizeof(*kipp))
62221807Sstas		goto bad;
63221807Sstas	if (kipp->ki_structsize != sizeof(*kipp))
64221807Sstas		goto bad;
65221807Sstas	if (kipp->ki_pid != pid)
66221807Sstas		goto bad;
67221807Sstas	return (kipp);
68221807Sstasbad:
69221807Sstas	free(kipp);
70221807Sstas	return (NULL);
71221807Sstas}
72