xref: /freebsd/lib/libutil/kinfo_getvmobject.c (revision ff87ae350ea75191bb1b11f5b5ef7b4db55ee799)
1*ff87ae35SJohn Baldwin /*
2*ff87ae35SJohn Baldwin  * Copyright (c) 2013 Hudson River Trading LLC
3*ff87ae35SJohn Baldwin  * Written by: John H. Baldwin <jhb@FreeBSD.org>
4*ff87ae35SJohn Baldwin  * All rights reserved.
5*ff87ae35SJohn Baldwin  *
6*ff87ae35SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
7*ff87ae35SJohn Baldwin  * modification, are permitted provided that the following conditions
8*ff87ae35SJohn Baldwin  * are met:
9*ff87ae35SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
10*ff87ae35SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
11*ff87ae35SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
12*ff87ae35SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
13*ff87ae35SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
14*ff87ae35SJohn Baldwin  *
15*ff87ae35SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*ff87ae35SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*ff87ae35SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*ff87ae35SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*ff87ae35SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*ff87ae35SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*ff87ae35SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*ff87ae35SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*ff87ae35SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*ff87ae35SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*ff87ae35SJohn Baldwin  * SUCH DAMAGE.
26*ff87ae35SJohn Baldwin  */
27*ff87ae35SJohn Baldwin 
28*ff87ae35SJohn Baldwin #include <sys/cdefs.h>
29*ff87ae35SJohn Baldwin __FBSDID("$FreeBSD$");
30*ff87ae35SJohn Baldwin 
31*ff87ae35SJohn Baldwin #include <sys/types.h>
32*ff87ae35SJohn Baldwin #include <sys/sysctl.h>
33*ff87ae35SJohn Baldwin #include <sys/user.h>
34*ff87ae35SJohn Baldwin #include <stdlib.h>
35*ff87ae35SJohn Baldwin #include <string.h>
36*ff87ae35SJohn Baldwin 
37*ff87ae35SJohn Baldwin #include "libutil.h"
38*ff87ae35SJohn Baldwin 
39*ff87ae35SJohn Baldwin struct kinfo_vmobject *
40*ff87ae35SJohn Baldwin kinfo_getvmobject(int *cntp)
41*ff87ae35SJohn Baldwin {
42*ff87ae35SJohn Baldwin 	char *buf, *bp, *ep;
43*ff87ae35SJohn Baldwin 	struct kinfo_vmobject *kvo, *list, *kp;
44*ff87ae35SJohn Baldwin 	size_t len;
45*ff87ae35SJohn Baldwin 	int cnt, i;
46*ff87ae35SJohn Baldwin 
47*ff87ae35SJohn Baldwin 	buf = NULL;
48*ff87ae35SJohn Baldwin 	for (i = 0; i < 3; i++) {
49*ff87ae35SJohn Baldwin 		if (sysctlbyname("vm.objects", NULL, &len, NULL, 0) < 0)
50*ff87ae35SJohn Baldwin 			return (NULL);
51*ff87ae35SJohn Baldwin 		buf = reallocf(buf, len);
52*ff87ae35SJohn Baldwin 		if (buf == NULL)
53*ff87ae35SJohn Baldwin 			return (NULL);
54*ff87ae35SJohn Baldwin 		if (sysctlbyname("vm.objects", buf, &len, NULL, 0) == 0)
55*ff87ae35SJohn Baldwin 			goto unpack;
56*ff87ae35SJohn Baldwin 		if (errno != ENOMEM) {
57*ff87ae35SJohn Baldwin 			free(buf);
58*ff87ae35SJohn Baldwin 			return (NULL);
59*ff87ae35SJohn Baldwin 		}
60*ff87ae35SJohn Baldwin 	}
61*ff87ae35SJohn Baldwin 	free(buf);
62*ff87ae35SJohn Baldwin 	return (NULL);
63*ff87ae35SJohn Baldwin 
64*ff87ae35SJohn Baldwin unpack:
65*ff87ae35SJohn Baldwin 	/* Count items */
66*ff87ae35SJohn Baldwin 	cnt = 0;
67*ff87ae35SJohn Baldwin 	bp = buf;
68*ff87ae35SJohn Baldwin 	ep = buf + len;
69*ff87ae35SJohn Baldwin 	while (bp < ep) {
70*ff87ae35SJohn Baldwin 		kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
71*ff87ae35SJohn Baldwin 		bp += kvo->kvo_structsize;
72*ff87ae35SJohn Baldwin 		cnt++;
73*ff87ae35SJohn Baldwin 	}
74*ff87ae35SJohn Baldwin 
75*ff87ae35SJohn Baldwin 	list = calloc(cnt, sizeof(*list));
76*ff87ae35SJohn Baldwin 	if (list == NULL) {
77*ff87ae35SJohn Baldwin 		free(buf);
78*ff87ae35SJohn Baldwin 		return (NULL);
79*ff87ae35SJohn Baldwin 	}
80*ff87ae35SJohn Baldwin 
81*ff87ae35SJohn Baldwin 	/* Unpack */
82*ff87ae35SJohn Baldwin 	bp = buf;
83*ff87ae35SJohn Baldwin 	kp = list;
84*ff87ae35SJohn Baldwin 	while (bp < ep) {
85*ff87ae35SJohn Baldwin 		kvo = (struct kinfo_vmobject *)(uintptr_t)bp;
86*ff87ae35SJohn Baldwin 		memcpy(kp, kvo, kvo->kvo_structsize);
87*ff87ae35SJohn Baldwin 		bp += kvo->kvo_structsize;
88*ff87ae35SJohn Baldwin 		kp->kvo_structsize = sizeof(*kp);
89*ff87ae35SJohn Baldwin 		kp++;
90*ff87ae35SJohn Baldwin 	}
91*ff87ae35SJohn Baldwin 	free(buf);
92*ff87ae35SJohn Baldwin 	*cntp = cnt;
93*ff87ae35SJohn Baldwin 	return (list);
94*ff87ae35SJohn Baldwin }
95