xref: /freebsd/lib/libutil/kinfo_getallproc.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
10daf62d9SStanislav Sedov /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
40daf62d9SStanislav Sedov  * Copyright (c) 2007 Robert N. M. Watson
50daf62d9SStanislav Sedov  * Copyright (c) 2009 Ulf Lilleengen
60daf62d9SStanislav Sedov  * All rights reserved.
70daf62d9SStanislav Sedov  *
80daf62d9SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
90daf62d9SStanislav Sedov  * modification, are permitted provided that the following conditions
100daf62d9SStanislav Sedov  * are met:
110daf62d9SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
120daf62d9SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
130daf62d9SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
140daf62d9SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
150daf62d9SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
160daf62d9SStanislav Sedov  *
170daf62d9SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
180daf62d9SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190daf62d9SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200daf62d9SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
210daf62d9SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220daf62d9SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230daf62d9SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240daf62d9SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250daf62d9SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260daf62d9SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270daf62d9SStanislav Sedov  * SUCH DAMAGE.
280daf62d9SStanislav Sedov  *
290daf62d9SStanislav Sedov  * $FreeBSD$
300daf62d9SStanislav Sedov  */
310daf62d9SStanislav Sedov 
320daf62d9SStanislav Sedov #include <sys/cdefs.h>
330daf62d9SStanislav Sedov __FBSDID("$FreeBSD$");
340daf62d9SStanislav Sedov 
350daf62d9SStanislav Sedov #include <sys/param.h>
360daf62d9SStanislav Sedov #include <sys/sysctl.h>
377adf46f0SEnji Cooper #include <sys/user.h>
380daf62d9SStanislav Sedov #include <stdlib.h>
390daf62d9SStanislav Sedov #include <string.h>
400daf62d9SStanislav Sedov 
410daf62d9SStanislav Sedov #include "libutil.h"
420daf62d9SStanislav Sedov 
430daf62d9SStanislav Sedov 
440daf62d9SStanislav Sedov /*
450daf62d9SStanislav Sedov  * Sort processes first by pid and then tid.
460daf62d9SStanislav Sedov  */
470daf62d9SStanislav Sedov static int
480daf62d9SStanislav Sedov kinfo_proc_compare(const void *a, const void *b)
490daf62d9SStanislav Sedov {
500daf62d9SStanislav Sedov 	int i;
510daf62d9SStanislav Sedov 
520daf62d9SStanislav Sedov 	i = ((const struct kinfo_proc *)a)->ki_pid -
530daf62d9SStanislav Sedov 	    ((const struct kinfo_proc *)b)->ki_pid;
540daf62d9SStanislav Sedov 	if (i != 0)
550daf62d9SStanislav Sedov 		return (i);
560daf62d9SStanislav Sedov 	i = ((const struct kinfo_proc *)a)->ki_tid -
570daf62d9SStanislav Sedov 	    ((const struct kinfo_proc *)b)->ki_tid;
580daf62d9SStanislav Sedov 	return (i);
590daf62d9SStanislav Sedov }
600daf62d9SStanislav Sedov 
610daf62d9SStanislav Sedov static void
620daf62d9SStanislav Sedov kinfo_proc_sort(struct kinfo_proc *kipp, int count)
630daf62d9SStanislav Sedov {
640daf62d9SStanislav Sedov 
650daf62d9SStanislav Sedov 	qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
660daf62d9SStanislav Sedov }
670daf62d9SStanislav Sedov 
680daf62d9SStanislav Sedov struct kinfo_proc *
690daf62d9SStanislav Sedov kinfo_getallproc(int *cntp)
700daf62d9SStanislav Sedov {
710daf62d9SStanislav Sedov 	struct kinfo_proc *kipp;
720daf62d9SStanislav Sedov 	size_t len;
730daf62d9SStanislav Sedov 	int mib[3];
740daf62d9SStanislav Sedov 
750daf62d9SStanislav Sedov 	mib[0] = CTL_KERN;
760daf62d9SStanislav Sedov 	mib[1] = KERN_PROC;
770daf62d9SStanislav Sedov 	mib[2] = KERN_PROC_PROC;
780daf62d9SStanislav Sedov 
790daf62d9SStanislav Sedov 	len = 0;
807adf46f0SEnji Cooper 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
810daf62d9SStanislav Sedov 		return (NULL);
820daf62d9SStanislav Sedov 
830daf62d9SStanislav Sedov 	kipp = malloc(len);
840daf62d9SStanislav Sedov 	if (kipp == NULL)
850daf62d9SStanislav Sedov 		return (NULL);
860daf62d9SStanislav Sedov 
877adf46f0SEnji Cooper 	if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0)
880daf62d9SStanislav Sedov 		goto bad;
890daf62d9SStanislav Sedov 	if (len % sizeof(*kipp) != 0)
900daf62d9SStanislav Sedov 		goto bad;
910daf62d9SStanislav Sedov 	if (kipp->ki_structsize != sizeof(*kipp))
920daf62d9SStanislav Sedov 		goto bad;
930daf62d9SStanislav Sedov 	*cntp = len / sizeof(*kipp);
940daf62d9SStanislav Sedov 	kinfo_proc_sort(kipp, len / sizeof(*kipp));
950daf62d9SStanislav Sedov 	return (kipp);
960daf62d9SStanislav Sedov bad:
970daf62d9SStanislav Sedov 	*cntp = 0;
980daf62d9SStanislav Sedov 	free(kipp);
990daf62d9SStanislav Sedov 	return (NULL);
1000daf62d9SStanislav Sedov }
101