xref: /freebsd/lib/libutil/kinfo_getallproc.c (revision 42249ef2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007 Robert N. M. Watson
5  * Copyright (c) 2009 Ulf Lilleengen
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "libutil.h"
42 
43 
44 /*
45  * Sort processes first by pid and then tid.
46  */
47 static int
48 kinfo_proc_compare(const void *a, const void *b)
49 {
50 	int i;
51 
52 	i = ((const struct kinfo_proc *)a)->ki_pid -
53 	    ((const struct kinfo_proc *)b)->ki_pid;
54 	if (i != 0)
55 		return (i);
56 	i = ((const struct kinfo_proc *)a)->ki_tid -
57 	    ((const struct kinfo_proc *)b)->ki_tid;
58 	return (i);
59 }
60 
61 static void
62 kinfo_proc_sort(struct kinfo_proc *kipp, int count)
63 {
64 
65 	qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
66 }
67 
68 struct kinfo_proc *
69 kinfo_getallproc(int *cntp)
70 {
71 	struct kinfo_proc *kipp;
72 	size_t len;
73 	int mib[3];
74 
75 	mib[0] = CTL_KERN;
76 	mib[1] = KERN_PROC;
77 	mib[2] = KERN_PROC_PROC;
78 
79 	len = 0;
80 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
81 		return (NULL);
82 
83 	kipp = malloc(len);
84 	if (kipp == NULL)
85 		return (NULL);
86 
87 	if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0)
88 		goto bad;
89 	if (len % sizeof(*kipp) != 0)
90 		goto bad;
91 	if (kipp->ki_structsize != sizeof(*kipp))
92 		goto bad;
93 	*cntp = len / sizeof(*kipp);
94 	kinfo_proc_sort(kipp, len / sizeof(*kipp));
95 	return (kipp);
96 bad:
97 	*cntp = 0;
98 	free(kipp);
99 	return (NULL);
100 }
101