xref: /freebsd/lib/libutil/kinfo_getallproc.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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 
300daf62d9SStanislav Sedov #include <sys/param.h>
310daf62d9SStanislav Sedov #include <sys/sysctl.h>
327adf46f0SEnji Cooper #include <sys/user.h>
330daf62d9SStanislav Sedov #include <stdlib.h>
340daf62d9SStanislav Sedov #include <string.h>
350daf62d9SStanislav Sedov 
360daf62d9SStanislav Sedov #include "libutil.h"
370daf62d9SStanislav Sedov 
380daf62d9SStanislav Sedov 
390daf62d9SStanislav Sedov /*
400daf62d9SStanislav Sedov  * Sort processes first by pid and then tid.
410daf62d9SStanislav Sedov  */
420daf62d9SStanislav Sedov static int
kinfo_proc_compare(const void * a,const void * b)430daf62d9SStanislav Sedov kinfo_proc_compare(const void *a, const void *b)
440daf62d9SStanislav Sedov {
450daf62d9SStanislav Sedov 	int i;
460daf62d9SStanislav Sedov 
470daf62d9SStanislav Sedov 	i = ((const struct kinfo_proc *)a)->ki_pid -
480daf62d9SStanislav Sedov 	    ((const struct kinfo_proc *)b)->ki_pid;
490daf62d9SStanislav Sedov 	if (i != 0)
500daf62d9SStanislav Sedov 		return (i);
510daf62d9SStanislav Sedov 	i = ((const struct kinfo_proc *)a)->ki_tid -
520daf62d9SStanislav Sedov 	    ((const struct kinfo_proc *)b)->ki_tid;
530daf62d9SStanislav Sedov 	return (i);
540daf62d9SStanislav Sedov }
550daf62d9SStanislav Sedov 
560daf62d9SStanislav Sedov static void
kinfo_proc_sort(struct kinfo_proc * kipp,int count)570daf62d9SStanislav Sedov kinfo_proc_sort(struct kinfo_proc *kipp, int count)
580daf62d9SStanislav Sedov {
590daf62d9SStanislav Sedov 
600daf62d9SStanislav Sedov 	qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare);
610daf62d9SStanislav Sedov }
620daf62d9SStanislav Sedov 
630daf62d9SStanislav Sedov struct kinfo_proc *
kinfo_getallproc(int * cntp)640daf62d9SStanislav Sedov kinfo_getallproc(int *cntp)
650daf62d9SStanislav Sedov {
660daf62d9SStanislav Sedov 	struct kinfo_proc *kipp;
670daf62d9SStanislav Sedov 	size_t len;
680daf62d9SStanislav Sedov 	int mib[3];
690daf62d9SStanislav Sedov 
700daf62d9SStanislav Sedov 	mib[0] = CTL_KERN;
710daf62d9SStanislav Sedov 	mib[1] = KERN_PROC;
720daf62d9SStanislav Sedov 	mib[2] = KERN_PROC_PROC;
730daf62d9SStanislav Sedov 
740daf62d9SStanislav Sedov 	len = 0;
757adf46f0SEnji Cooper 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
760daf62d9SStanislav Sedov 		return (NULL);
770daf62d9SStanislav Sedov 
780daf62d9SStanislav Sedov 	kipp = malloc(len);
790daf62d9SStanislav Sedov 	if (kipp == NULL)
800daf62d9SStanislav Sedov 		return (NULL);
810daf62d9SStanislav Sedov 
827adf46f0SEnji Cooper 	if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0)
830daf62d9SStanislav Sedov 		goto bad;
840daf62d9SStanislav Sedov 	if (len % sizeof(*kipp) != 0)
850daf62d9SStanislav Sedov 		goto bad;
860daf62d9SStanislav Sedov 	if (kipp->ki_structsize != sizeof(*kipp))
870daf62d9SStanislav Sedov 		goto bad;
880daf62d9SStanislav Sedov 	*cntp = len / sizeof(*kipp);
890daf62d9SStanislav Sedov 	kinfo_proc_sort(kipp, len / sizeof(*kipp));
900daf62d9SStanislav Sedov 	return (kipp);
910daf62d9SStanislav Sedov bad:
920daf62d9SStanislav Sedov 	*cntp = 0;
930daf62d9SStanislav Sedov 	free(kipp);
940daf62d9SStanislav Sedov 	return (NULL);
950daf62d9SStanislav Sedov }
96