/
kern_sysctl.c
5020 lines (4235 loc) · 156 KB
/
kern_sysctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2019 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*-
* Copyright (c) 1982, 1986, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Karels at Berkeley Software Design, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
/*
* DEPRECATED sysctl system call code
*
* Everything in this file is deprecated. Sysctls should be handled
* by the code in kern_newsysctl.c.
* The remaining "case" sections are supposed to be converted into
* SYSCTL_*-style definitions, and as soon as all of them are gone,
* this source file is supposed to die.
*
* DO NOT ADD ANY MORE "case" SECTIONS TO THIS FILE, instead define
* your sysctl with SYSCTL_INT, SYSCTL_PROC etc. in your source file.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/file_internal.h>
#include <sys/vnode_internal.h>
#include <sys/unistd.h>
#include <sys/buf.h>
#include <sys/ioctl.h>
#include <sys/namei.h>
#include <sys/tty.h>
#include <sys/disklabel.h>
#include <sys/vm.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/aio_kern.h>
#include <sys/reboot.h>
#include <sys/memory_maintenance.h>
#include <sys/priv.h>
#include <stdatomic.h>
#include <security/audit/audit.h>
#include <kern/kalloc.h>
#include <machine/smp.h>
#include <machine/atomic.h>
#include <mach/machine.h>
#include <mach/mach_host.h>
#include <mach/mach_types.h>
#include <mach/processor_info.h>
#include <mach/vm_param.h>
#include <kern/debug.h>
#include <kern/mach_param.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/thread_group.h>
#include <kern/processor.h>
#include <kern/cpu_number.h>
#include <kern/cpu_quiesce.h>
#include <kern/sched_prim.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <mach/host_info.h>
#include <sys/mount_internal.h>
#include <sys/kdebug.h>
#include <sys/kern_sysctl.h>
#include <IOKit/IOPlatformExpert.h>
#include <pexpert/pexpert.h>
#include <machine/machine_routines.h>
#include <machine/exec.h>
#include <nfs/nfs_conf.h>
#include <vm/vm_protos.h>
#include <vm/vm_pageout.h>
#include <vm/vm_compressor_algorithms.h>
#include <sys/imgsrc.h>
#include <kern/timer_call.h>
#if defined(__i386__) || defined(__x86_64__)
#include <i386/cpuid.h>
#endif
#if CONFIG_FREEZE
#include <sys/kern_memorystatus.h>
#endif
#if KPERF
#include <kperf/kperf.h>
#endif
#if HYPERVISOR
#include <kern/hv_support.h>
#endif
/*
* deliberately setting max requests to really high number
* so that runaway settings do not cause MALLOC overflows
*/
#define AIO_MAX_REQUESTS (128 * CONFIG_AIO_MAX)
extern int aio_max_requests;
extern int aio_max_requests_per_process;
extern int aio_worker_threads;
extern int lowpri_IO_window_msecs;
extern int lowpri_IO_delay_msecs;
#if DEVELOPMENT || DEBUG
extern int nx_enabled;
#endif
extern int speculative_reads_disabled;
extern unsigned int speculative_prefetch_max;
extern unsigned int speculative_prefetch_max_iosize;
extern unsigned int preheat_max_bytes;
extern unsigned int preheat_min_bytes;
extern long numvnodes;
extern long num_recycledvnodes;
extern uuid_string_t bootsessionuuid_string;
extern unsigned int vm_max_delayed_work_limit;
extern unsigned int vm_max_batch;
extern unsigned int vm_page_free_min;
extern unsigned int vm_page_free_target;
extern unsigned int vm_page_free_reserved;
#if (DEVELOPMENT || DEBUG)
extern uint32_t vm_page_creation_throttled_hard;
extern uint32_t vm_page_creation_throttled_soft;
#endif /* DEVELOPMENT || DEBUG */
#if CONFIG_LOCKERBOOT
extern const char kernel_protoboot_mount[];
#endif
/*
* Conditionally allow dtrace to see these functions for debugging purposes.
*/
#ifdef STATIC
#undef STATIC
#endif
#if 0
#define STATIC
#else
#define STATIC static
#endif
extern boolean_t mach_timer_coalescing_enabled;
extern uint64_t timer_deadline_tracking_bin_1, timer_deadline_tracking_bin_2;
STATIC void
fill_user32_eproc(proc_t, struct user32_eproc *__restrict);
STATIC void
fill_user32_externproc(proc_t, struct user32_extern_proc *__restrict);
STATIC void
fill_user64_eproc(proc_t, struct user64_eproc *__restrict);
STATIC void
fill_user64_proc(proc_t, struct user64_kinfo_proc *__restrict);
STATIC void
fill_user64_externproc(proc_t, struct user64_extern_proc *__restrict);
STATIC void
fill_user32_proc(proc_t, struct user32_kinfo_proc *__restrict);
extern int
kdbg_control(int *name, u_int namelen, user_addr_t where, size_t * sizep);
#if CONFIG_NFS_CLIENT
extern int
netboot_root(void);
#endif
int
sysctl_procargs(int *name, u_int namelen, user_addr_t where,
size_t *sizep, proc_t cur_proc);
STATIC int
sysctl_procargsx(int *name, u_int namelen, user_addr_t where, size_t *sizep,
proc_t cur_proc, int argc_yes);
int
sysctl_struct(user_addr_t oldp, size_t *oldlenp, user_addr_t newp,
size_t newlen, void *sp, int len);
STATIC int sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg);
STATIC int sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg);
STATIC int sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg);
STATIC int sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg);
STATIC int sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg);
int sysdoproc_callback(proc_t p, void *arg);
/* forward declarations for non-static STATIC */
STATIC void fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64);
STATIC void fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32);
STATIC int sysctl_handle_kern_threadname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_sched_stats(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_sched_stats_enable(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_kdebug_ops SYSCTL_HANDLER_ARGS;
#if COUNT_SYSCALLS
STATIC int sysctl_docountsyscalls SYSCTL_HANDLER_ARGS;
#endif /* COUNT_SYSCALLS */
#if !CONFIG_EMBEDDED
STATIC int sysctl_doprocargs SYSCTL_HANDLER_ARGS;
#endif /* !CONFIG_EMBEDDED */
STATIC int sysctl_doprocargs2 SYSCTL_HANDLER_ARGS;
STATIC int sysctl_prochandle SYSCTL_HANDLER_ARGS;
STATIC int sysctl_aiomax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_aioprocmax(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_aiothreads(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_maxproc(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_osversion(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_sysctl_bootargs(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_maxvnodes(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_securelvl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_domainname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_hostname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_procname(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_boottime(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_symfile(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#if CONFIG_NFS_CLIENT
STATIC int sysctl_netboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#endif
#ifdef CONFIG_IMGSRC_ACCESS
STATIC int sysctl_imgsrcdev(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#endif
STATIC int sysctl_usrstack(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_usrstack64(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#if CONFIG_COREDUMP
STATIC int sysctl_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_suid_coredump(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#endif
STATIC int sysctl_delayterm(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_rage_vnode(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_kern_check_openevt(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#if DEVELOPMENT || DEBUG
STATIC int sysctl_nx(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#endif
STATIC int sysctl_loadavg(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_vm_toggle_address_reuse(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_swapusage(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int fetch_process_cputype( proc_t cur_proc, int *name, u_int namelen, cpu_type_t *cputype);
STATIC int sysctl_sysctl_native(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_sysctl_cputype(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_safeboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_singleuser(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_minimalboot(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_slide(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#ifdef CONFIG_XNUPOST
#include <tests/xnupost.h>
STATIC int sysctl_debug_test_oslog_ctl(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_debug_test_stackshot_mutex_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
STATIC int sysctl_debug_test_stackshot_rwlck_owner(struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req);
#endif
extern void IORegistrySetOSBuildVersion(char * build_version);
STATIC void
fill_loadavg64(struct loadavg *la, struct user64_loadavg *la64)
{
la64->ldavg[0] = la->ldavg[0];
la64->ldavg[1] = la->ldavg[1];
la64->ldavg[2] = la->ldavg[2];
la64->fscale = (user64_long_t)la->fscale;
}
STATIC void
fill_loadavg32(struct loadavg *la, struct user32_loadavg *la32)
{
la32->ldavg[0] = la->ldavg[0];
la32->ldavg[1] = la->ldavg[1];
la32->ldavg[2] = la->ldavg[2];
la32->fscale = (user32_long_t)la->fscale;
}
#if CONFIG_COREDUMP
/*
* Attributes stored in the kernel.
*/
extern char corefilename[MAXPATHLEN + 1];
extern int do_coredump;
extern int sugid_coredump;
#endif
#if COUNT_SYSCALLS
extern int do_count_syscalls;
#endif
#ifdef INSECURE
int securelevel = -1;
#else
int securelevel;
#endif
STATIC int
sysctl_handle_kern_threadname( __unused struct sysctl_oid *oidp, __unused void *arg1,
__unused int arg2, struct sysctl_req *req)
{
int error;
struct uthread *ut = get_bsdthread_info(current_thread());
user_addr_t oldp = 0, newp = 0;
size_t *oldlenp = NULL;
size_t newlen = 0;
oldp = req->oldptr;
oldlenp = &(req->oldlen);
newp = req->newptr;
newlen = req->newlen;
/* We want the current length, and maybe the string itself */
if (oldlenp) {
/* if we have no thread name yet tell'em we want MAXTHREADNAMESIZE - 1 */
size_t currlen = MAXTHREADNAMESIZE - 1;
if (ut->pth_name) {
/* use length of current thread name */
currlen = strlen(ut->pth_name);
}
if (oldp) {
if (*oldlenp < currlen) {
return ENOMEM;
}
/* NOTE - we do not copy the NULL terminator */
if (ut->pth_name) {
error = copyout(ut->pth_name, oldp, currlen);
if (error) {
return error;
}
}
}
/* return length of thread name minus NULL terminator (just like strlen) */
req->oldidx = currlen;
}
/* We want to set the name to something */
if (newp) {
if (newlen > (MAXTHREADNAMESIZE - 1)) {
return ENAMETOOLONG;
}
if (!ut->pth_name) {
char *tmp_pth_name = (char *)kalloc(MAXTHREADNAMESIZE);
if (!tmp_pth_name) {
return ENOMEM;
}
bzero(tmp_pth_name, MAXTHREADNAMESIZE);
if (!OSCompareAndSwapPtr(NULL, tmp_pth_name, &ut->pth_name)) {
kfree(tmp_pth_name, MAXTHREADNAMESIZE);
return EBUSY;
}
} else {
kernel_debug_string_simple(TRACE_STRING_THREADNAME_PREV, ut->pth_name);
bzero(ut->pth_name, MAXTHREADNAMESIZE);
}
error = copyin(newp, ut->pth_name, newlen);
if (error) {
return error;
}
kernel_debug_string_simple(TRACE_STRING_THREADNAME, ut->pth_name);
}
return 0;
}
SYSCTL_PROC(_kern, KERN_THREADNAME, threadname, CTLFLAG_ANYBODY | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_handle_kern_threadname, "A", "");
#define BSD_HOST 1
STATIC int
sysctl_sched_stats(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
{
host_basic_info_data_t hinfo;
kern_return_t kret;
uint32_t size;
int changed;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
struct _processor_statistics_np *buf;
int error;
kret = host_info((host_t)BSD_HOST, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
if (kret != KERN_SUCCESS) {
return EINVAL;
}
size = sizeof(struct _processor_statistics_np) * (hinfo.logical_cpu_max + 2); /* One for RT Queue, One for Fair Share Queue */
if (req->oldlen < size) {
return EINVAL;
}
MALLOC(buf, struct _processor_statistics_np*, size, M_TEMP, M_ZERO | M_WAITOK);
kret = get_sched_statistics(buf, &size);
if (kret != KERN_SUCCESS) {
error = EINVAL;
goto out;
}
error = sysctl_io_opaque(req, buf, size, &changed);
if (error) {
goto out;
}
if (changed) {
panic("Sched info changed?!");
}
out:
FREE(buf, M_TEMP);
return error;
}
SYSCTL_PROC(_kern, OID_AUTO, sched_stats, CTLFLAG_LOCKED, 0, 0, sysctl_sched_stats, "-", "");
STATIC int
sysctl_sched_stats_enable(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, __unused struct sysctl_req *req)
{
boolean_t active;
int res;
if (req->newlen != sizeof(active)) {
return EINVAL;
}
res = copyin(req->newptr, &active, sizeof(active));
if (res != 0) {
return res;
}
return set_sched_stats_active(active);
}
SYSCTL_PROC(_kern, OID_AUTO, sched_stats_enable, CTLFLAG_LOCKED | CTLFLAG_WR, 0, 0, sysctl_sched_stats_enable, "-", "");
extern uint32_t sched_debug_flags;
SYSCTL_INT(_debug, OID_AUTO, sched, CTLFLAG_RW | CTLFLAG_LOCKED, &sched_debug_flags, 0, "scheduler debug");
#if (DEBUG || DEVELOPMENT)
extern boolean_t doprnt_hide_pointers;
SYSCTL_INT(_debug, OID_AUTO, hide_kernel_pointers, CTLFLAG_RW | CTLFLAG_LOCKED, &doprnt_hide_pointers, 0, "hide kernel pointers from log");
#endif
extern int get_kernel_symfile(proc_t, char **);
#if COUNT_SYSCALLS
#define KERN_COUNT_SYSCALLS (KERN_OSTYPE + 1000)
extern const unsigned int nsysent;
extern int syscalls_log[];
extern const char *syscallnames[];
STATIC int
sysctl_docountsyscalls SYSCTL_HANDLER_ARGS
{
__unused int cmd = oidp->oid_arg2; /* subcommand*/
__unused int *name = arg1; /* oid element argument vector */
__unused int namelen = arg2; /* number of oid element arguments */
user_addr_t oldp = req->oldptr; /* user buffer copy out address */
size_t *oldlenp = &req->oldlen; /* user buffer copy out size */
user_addr_t newp = req->newptr; /* user buffer copy in address */
size_t newlen = req->newlen; /* user buffer copy in size */
int error;
int tmp;
/* valid values passed in:
* = 0 means don't keep called counts for each bsd syscall
* > 0 means keep called counts for each bsd syscall
* = 2 means dump current counts to the system log
* = 3 means reset all counts
* for example, to dump current counts:
* sysctl -w kern.count_calls=2
*/
error = sysctl_int(oldp, oldlenp, newp, newlen, &tmp);
if (error != 0) {
return error;
}
if (tmp == 1) {
do_count_syscalls = 1;
} else if (tmp == 0 || tmp == 2 || tmp == 3) {
int i;
for (i = 0; i < nsysent; i++) {
if (syscalls_log[i] != 0) {
if (tmp == 2) {
printf("%d calls - name %s \n", syscalls_log[i], syscallnames[i]);
} else {
syscalls_log[i] = 0;
}
}
}
if (tmp != 0) {
do_count_syscalls = 1;
}
}
/* adjust index so we return the right required/consumed amount */
if (!error) {
req->oldidx += req->oldlen;
}
return error;
}
SYSCTL_PROC(_kern, KERN_COUNT_SYSCALLS, count_syscalls, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
0, /* Integer argument (arg2) */
sysctl_docountsyscalls, /* Handler function */
NULL, /* Data pointer */
"");
#endif /* COUNT_SYSCALLS */
/*
* The following sysctl_* functions should not be used
* any more, as they can only cope with callers in
* user mode: Use new-style
* sysctl_io_number()
* sysctl_io_string()
* sysctl_io_opaque()
* instead.
*/
/*
* Validate parameters and get old / set new parameters
* for an integer-valued sysctl function.
*/
int
sysctl_int(user_addr_t oldp, size_t *oldlenp,
user_addr_t newp, size_t newlen, int *valp)
{
int error = 0;
if (oldp != USER_ADDR_NULL && oldlenp == NULL) {
return EFAULT;
}
if (oldp && *oldlenp < sizeof(int)) {
return ENOMEM;
}
if (newp && newlen != sizeof(int)) {
return EINVAL;
}
*oldlenp = sizeof(int);
if (oldp) {
error = copyout(valp, oldp, sizeof(int));
}
if (error == 0 && newp) {
error = copyin(newp, valp, sizeof(int));
AUDIT_ARG(value32, *valp);
}
return error;
}
/*
* Validate parameters and get old / set new parameters
* for an quad(64bit)-valued sysctl function.
*/
int
sysctl_quad(user_addr_t oldp, size_t *oldlenp,
user_addr_t newp, size_t newlen, quad_t *valp)
{
int error = 0;
if (oldp != USER_ADDR_NULL && oldlenp == NULL) {
return EFAULT;
}
if (oldp && *oldlenp < sizeof(quad_t)) {
return ENOMEM;
}
if (newp && newlen != sizeof(quad_t)) {
return EINVAL;
}
*oldlenp = sizeof(quad_t);
if (oldp) {
error = copyout(valp, oldp, sizeof(quad_t));
}
if (error == 0 && newp) {
error = copyin(newp, valp, sizeof(quad_t));
}
return error;
}
STATIC int
sysdoproc_filt_KERN_PROC_PID(proc_t p, void * arg)
{
if (p->p_pid != (pid_t)*(int*)arg) {
return 0;
} else {
return 1;
}
}
STATIC int
sysdoproc_filt_KERN_PROC_PGRP(proc_t p, void * arg)
{
if (p->p_pgrpid != (pid_t)*(int*)arg) {
return 0;
} else {
return 1;
}
}
STATIC int
sysdoproc_filt_KERN_PROC_TTY(proc_t p, void * arg)
{
int retval;
struct tty *tp;
/* This is very racy but list lock is held.. Hmmm. */
if ((p->p_flag & P_CONTROLT) == 0 ||
(p->p_pgrp == NULL) || (p->p_pgrp->pg_session == NULL) ||
(tp = SESSION_TP(p->p_pgrp->pg_session)) == TTY_NULL ||
tp->t_dev != (dev_t)*(int*)arg) {
retval = 0;
} else {
retval = 1;
}
return retval;
}
STATIC int
sysdoproc_filt_KERN_PROC_UID(proc_t p, void * arg)
{
kauth_cred_t my_cred;
uid_t uid;
if (p->p_ucred == NULL) {
return 0;
}
my_cred = kauth_cred_proc_ref(p);
uid = kauth_cred_getuid(my_cred);
kauth_cred_unref(&my_cred);
if (uid != (uid_t)*(int*)arg) {
return 0;
} else {
return 1;
}
}
STATIC int
sysdoproc_filt_KERN_PROC_RUID(proc_t p, void * arg)
{
kauth_cred_t my_cred;
uid_t ruid;
if (p->p_ucred == NULL) {
return 0;
}
my_cred = kauth_cred_proc_ref(p);
ruid = kauth_cred_getruid(my_cred);
kauth_cred_unref(&my_cred);
if (ruid != (uid_t)*(int*)arg) {
return 0;
} else {
return 1;
}
}
/*
* try over estimating by 5 procs
*/
#define KERN_PROCSLOP (5 * sizeof (struct kinfo_proc))
struct sysdoproc_args {
int buflen;
void *kprocp;
boolean_t is_64_bit;
user_addr_t dp;
size_t needed;
int sizeof_kproc;
int *errorp;
int uidcheck;
int ruidcheck;
int ttycheck;
int uidval;
};
int
sysdoproc_callback(proc_t p, void *arg)
{
struct sysdoproc_args *args = arg;
if (args->buflen >= args->sizeof_kproc) {
if ((args->ruidcheck != 0) && (sysdoproc_filt_KERN_PROC_RUID(p, &args->uidval) == 0)) {
return PROC_RETURNED;
}
if ((args->uidcheck != 0) && (sysdoproc_filt_KERN_PROC_UID(p, &args->uidval) == 0)) {
return PROC_RETURNED;
}
if ((args->ttycheck != 0) && (sysdoproc_filt_KERN_PROC_TTY(p, &args->uidval) == 0)) {
return PROC_RETURNED;
}
bzero(args->kprocp, args->sizeof_kproc);
if (args->is_64_bit) {
fill_user64_proc(p, args->kprocp);
} else {
fill_user32_proc(p, args->kprocp);
}
int error = copyout(args->kprocp, args->dp, args->sizeof_kproc);
if (error) {
*args->errorp = error;
return PROC_RETURNED_DONE;
}
args->dp += args->sizeof_kproc;
args->buflen -= args->sizeof_kproc;
}
args->needed += args->sizeof_kproc;
return PROC_RETURNED;
}
SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_LOCKED, 0, "");
STATIC int
sysctl_prochandle SYSCTL_HANDLER_ARGS
{
int cmd = oidp->oid_arg2; /* subcommand for multiple nodes */
int *name = arg1; /* oid element argument vector */
int namelen = arg2; /* number of oid element arguments */
user_addr_t where = req->oldptr;/* user buffer copy out address */
user_addr_t dp = where;
size_t needed = 0;
int buflen = where != USER_ADDR_NULL ? req->oldlen : 0;
int error = 0;
boolean_t is_64_bit = proc_is64bit(current_proc());
struct user32_kinfo_proc user32_kproc;
struct user64_kinfo_proc user_kproc;
int sizeof_kproc;
void *kprocp;
int (*filterfn)(proc_t, void *) = 0;
struct sysdoproc_args args;
int uidcheck = 0;
int ruidcheck = 0;
int ttycheck = 0;
if (namelen != 1 && !(namelen == 0 && cmd == KERN_PROC_ALL)) {
return EINVAL;
}
if (is_64_bit) {
sizeof_kproc = sizeof(user_kproc);
kprocp = &user_kproc;
} else {
sizeof_kproc = sizeof(user32_kproc);
kprocp = &user32_kproc;
}
switch (cmd) {
case KERN_PROC_PID:
filterfn = sysdoproc_filt_KERN_PROC_PID;
break;
case KERN_PROC_PGRP:
filterfn = sysdoproc_filt_KERN_PROC_PGRP;
break;
case KERN_PROC_TTY:
ttycheck = 1;
break;
case KERN_PROC_UID:
uidcheck = 1;
break;
case KERN_PROC_RUID:
ruidcheck = 1;
break;
case KERN_PROC_ALL:
break;
default:
/* must be kern.proc.<unknown> */
return ENOTSUP;
}
error = 0;
args.buflen = buflen;
args.kprocp = kprocp;
args.is_64_bit = is_64_bit;
args.dp = dp;
args.needed = needed;
args.errorp = &error;
args.uidcheck = uidcheck;
args.ruidcheck = ruidcheck;
args.ttycheck = ttycheck;
args.sizeof_kproc = sizeof_kproc;
if (namelen) {
args.uidval = name[0];
}
proc_iterate((PROC_ALLPROCLIST | PROC_ZOMBPROCLIST),
sysdoproc_callback, &args, filterfn, name);
if (error) {
return error;
}
dp = args.dp;
needed = args.needed;
if (where != USER_ADDR_NULL) {
req->oldlen = dp - where;
if (needed > req->oldlen) {
return ENOMEM;
}
} else {
needed += KERN_PROCSLOP;
req->oldlen = needed;
}
/* adjust index so we return the right required/consumed amount */
req->oldidx += req->oldlen;
return 0;
}
/*
* We specify the subcommand code for multiple nodes as the 'req->arg2' value
* in the sysctl declaration itself, which comes into the handler function
* as 'oidp->oid_arg2'.
*
* For these particular sysctls, since they have well known OIDs, we could
* have just obtained it from the '((int *)arg1)[0]' parameter, but that would
* not demonstrate how to handle multiple sysctls that used OID_AUTO instead
* of a well known value with a common handler function. This is desirable,
* because we want well known values to "go away" at some future date.
*
* It should be noted that the value of '((int *)arg1)[1]' is used for many
* an integer parameter to the subcommand for many of these sysctls; we'd
* rather have used '((int *)arg1)[0]' for that, or even better, an element
* in a structure passed in as the the 'newp' argument to sysctlbyname(3),
* and then use leaf-node permissions enforcement, but that would have
* necessitated modifying user space code to correspond to the interface
* change, and we are striving for binary backward compatibility here; even
* though these are SPI, and not intended for use by user space applications
* which are not themselves system tools or libraries, some applications
* have erroneously used them.
*/
SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_ALL, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_PID, pid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_PID, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_TTY, tty, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_TTY, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_PGRP, pgrp, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_PGRP, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_UID, uid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_UID, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_RUID, ruid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_RUID, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
SYSCTL_PROC(_kern_proc, KERN_PROC_LCID, lcid, CTLTYPE_NODE | CTLFLAG_RD | CTLFLAG_LOCKED,
0, /* Pointer argument (arg1) */
KERN_PROC_LCID, /* Integer argument (arg2) */
sysctl_prochandle, /* Handler function */
NULL, /* Data is size variant on ILP32/LP64 */
"");
/*
* Fill in non-zero fields of an eproc structure for the specified process.
*/
STATIC void
fill_user32_eproc(proc_t p, struct user32_eproc *__restrict ep)
{
struct tty *tp;
struct pgrp *pg;
struct session *sessp;
kauth_cred_t my_cred;
pg = proc_pgrp(p);
sessp = proc_session(p);
if (pg != PGRP_NULL) {
ep->e_pgid = p->p_pgrpid;
ep->e_jobc = pg->pg_jobc;
if (sessp != SESSION_NULL && sessp->s_ttyvp) {
ep->e_flag = EPROC_CTTY;
}
}
ep->e_ppid = p->p_ppid;
if (p->p_ucred) {
my_cred = kauth_cred_proc_ref(p);
/* A fake historical pcred */
ep->e_pcred.p_ruid = kauth_cred_getruid(my_cred);
ep->e_pcred.p_svuid = kauth_cred_getsvuid(my_cred);
ep->e_pcred.p_rgid = kauth_cred_getrgid(my_cred);
ep->e_pcred.p_svgid = kauth_cred_getsvgid(my_cred);
/* A fake historical *kauth_cred_t */
ep->e_ucred.cr_ref = os_atomic_load(&my_cred->cr_ref, relaxed);
ep->e_ucred.cr_uid = kauth_cred_getuid(my_cred);
ep->e_ucred.cr_ngroups = posix_cred_get(my_cred)->cr_ngroups;
bcopy(posix_cred_get(my_cred)->cr_groups,
ep->e_ucred.cr_groups, NGROUPS * sizeof(gid_t));
kauth_cred_unref(&my_cred);
}
if ((p->p_flag & P_CONTROLT) && (sessp != SESSION_NULL) &&
(tp = SESSION_TP(sessp))) {
ep->e_tdev = tp->t_dev;
ep->e_tpgid = sessp->s_ttypgrpid;
} else {
ep->e_tdev = NODEV;
}
if (sessp != SESSION_NULL) {
if (SESS_LEADER(p, sessp)) {
ep->e_flag |= EPROC_SLEADER;