1221807Sstas/*- 2221807Sstas * Copyright (c) 2007 Robert N. M. Watson 3221807Sstas * Copyright (c) 2009 Ulf Lilleengen 4221807Sstas * All rights reserved. 5221807Sstas * 6221807Sstas * Redistribution and use in source and binary forms, with or without 7221807Sstas * modification, are permitted provided that the following conditions 8221807Sstas * are met: 9221807Sstas * 1. Redistributions of source code must retain the above copyright 10221807Sstas * notice, this list of conditions and the following disclaimer. 11221807Sstas * 2. Redistributions in binary form must reproduce the above copyright 12221807Sstas * notice, this list of conditions and the following disclaimer in the 13221807Sstas * documentation and/or other materials provided with the distribution. 14221807Sstas * 15221807Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16221807Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17221807Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18221807Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19221807Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20221807Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21221807Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22221807Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23221807Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24221807Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25221807Sstas * SUCH DAMAGE. 26221807Sstas * 27221807Sstas * $FreeBSD: stable/10/lib/libutil/kinfo_getallproc.c 312032 2017-01-13 08:39:40Z ngie $ 28221807Sstas */ 29221807Sstas 30221807Sstas#include <sys/cdefs.h> 31221807Sstas__FBSDID("$FreeBSD: stable/10/lib/libutil/kinfo_getallproc.c 312032 2017-01-13 08:39:40Z ngie $"); 32221807Sstas 33221807Sstas#include <sys/param.h> 34312032Sngie#include <sys/sysctl.h> 35221807Sstas#include <sys/user.h> 36221807Sstas#include <stdlib.h> 37221807Sstas#include <string.h> 38221807Sstas 39221807Sstas#include "libutil.h" 40221807Sstas 41221807Sstas 42221807Sstas/* 43221807Sstas * Sort processes first by pid and then tid. 44221807Sstas */ 45221807Sstasstatic int 46221807Sstaskinfo_proc_compare(const void *a, const void *b) 47221807Sstas{ 48221807Sstas int i; 49221807Sstas 50221807Sstas i = ((const struct kinfo_proc *)a)->ki_pid - 51221807Sstas ((const struct kinfo_proc *)b)->ki_pid; 52221807Sstas if (i != 0) 53221807Sstas return (i); 54221807Sstas i = ((const struct kinfo_proc *)a)->ki_tid - 55221807Sstas ((const struct kinfo_proc *)b)->ki_tid; 56221807Sstas return (i); 57221807Sstas} 58221807Sstas 59221807Sstasstatic void 60221807Sstaskinfo_proc_sort(struct kinfo_proc *kipp, int count) 61221807Sstas{ 62221807Sstas 63221807Sstas qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); 64221807Sstas} 65221807Sstas 66221807Sstasstruct kinfo_proc * 67221807Sstaskinfo_getallproc(int *cntp) 68221807Sstas{ 69221807Sstas struct kinfo_proc *kipp; 70221807Sstas size_t len; 71221807Sstas int mib[3]; 72221807Sstas 73221807Sstas mib[0] = CTL_KERN; 74221807Sstas mib[1] = KERN_PROC; 75221807Sstas mib[2] = KERN_PROC_PROC; 76221807Sstas 77221807Sstas len = 0; 78312032Sngie if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0) 79221807Sstas return (NULL); 80221807Sstas 81221807Sstas kipp = malloc(len); 82221807Sstas if (kipp == NULL) 83221807Sstas return (NULL); 84221807Sstas 85312032Sngie if (sysctl(mib, nitems(mib), kipp, &len, NULL, 0) < 0) 86221807Sstas goto bad; 87221807Sstas if (len % sizeof(*kipp) != 0) 88221807Sstas goto bad; 89221807Sstas if (kipp->ki_structsize != sizeof(*kipp)) 90221807Sstas goto bad; 91221807Sstas *cntp = len / sizeof(*kipp); 92221807Sstas kinfo_proc_sort(kipp, len / sizeof(*kipp)); 93221807Sstas return (kipp); 94221807Sstasbad: 95221807Sstas *cntp = 0; 96221807Sstas free(kipp); 97221807Sstas return (NULL); 98221807Sstas} 99