xref: /xnu-12377.61.12/tests/ledger_entry_info_v2.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <sys/types.h>
4 #include <TargetConditionals.h>
5 
6 #include "../osfmk/kern/ledger.h"
7 extern int ledger(int cmd, caddr_t arg1, caddr_t arg2, caddr_t arg3);
8 
9 T_DECL(ledger_entry_v2,
10     "test the LEDGER_ENTRY_INFO_V2 command of ledger() syscal",
11     T_META_LTEPHASE(LTE_POSTINIT),
12     T_META_OWNER("skwok2"),
13     T_META_TAG_VM_PREFERRED)
14 {
15 	struct ledger_info li;
16 	int64_t ledger_count;
17 	struct ledger_entry_info_v2 *lei_v2 = NULL;
18 	bool retrieved_lifetime_max = false;
19 	size_t malloc_size = 0;
20 
21 	T_QUIET; T_ASSERT_EQ(ledger(LEDGER_INFO,
22 	    (caddr_t)(uintptr_t)getpid(),
23 	    (caddr_t)&li,
24 	    NULL),
25 	    0,
26 	    "ledger(LEDGER_INFO)");
27 
28 	ledger_count = li.li_entries;
29 	T_QUIET; T_ASSERT_GT(ledger_count, 0, "no ledger entry available");
30 
31 	malloc_size = (size_t)ledger_count * sizeof(struct ledger_entry_info_v2);
32 	lei_v2 = (struct ledger_entry_info_v2 *)malloc(malloc_size);
33 	T_QUIET; T_ASSERT_NE(lei_v2, NULL, "malloc(ledger_entry_info_v2) of size %u", malloc_size);
34 
35 
36 	T_ASSERT_GE(ledger(LEDGER_ENTRY_INFO_V2,
37 	    (caddr_t)(uintptr_t)getpid(),
38 	    (caddr_t)lei_v2,
39 	    (caddr_t)&ledger_count),
40 	    0,
41 	    "ledger(LEDGER_ENTRY_INFO_V2)");
42 
43 	for (int i = 0; i < ledger_count; i++) {
44 		if (lei_v2[i].lei_lifetime_max != -1) {
45 			retrieved_lifetime_max = true;
46 			break;
47 		}
48 	}
49 
50 	free(lei_v2);
51 
52 	if (retrieved_lifetime_max) {
53 		T_PASS("successfully retrieved at least one entry which support lifetime max");
54 	} else {
55 		T_FAIL("couldn't read any lifetime max value");
56 	}
57 }
58