1*0f4c859eSApple OSS Distributions /*
2*0f4c859eSApple OSS Distributions * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
3*0f4c859eSApple OSS Distributions *
4*0f4c859eSApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*0f4c859eSApple OSS Distributions *
6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*0f4c859eSApple OSS Distributions * compliance with the License. Please obtain a copy of the License at
10*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this
11*0f4c859eSApple OSS Distributions * file.
12*0f4c859eSApple OSS Distributions *
13*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are
14*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and
19*0f4c859eSApple OSS Distributions * limitations under the License.
20*0f4c859eSApple OSS Distributions *
21*0f4c859eSApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
22*0f4c859eSApple OSS Distributions */
23*0f4c859eSApple OSS Distributions #include <errno.h>
24*0f4c859eSApple OSS Distributions #include <sys/resource.h>
25*0f4c859eSApple OSS Distributions #include <mach/port.h>
26*0f4c859eSApple OSS Distributions
27*0f4c859eSApple OSS Distributions extern int __iopolicysys(int, struct _iopol_param_t *);
28*0f4c859eSApple OSS Distributions extern void _pthread_clear_qos_tsd(mach_port_t);
29*0f4c859eSApple OSS Distributions
30*0f4c859eSApple OSS Distributions int
getiopolicy_np(int iotype,int scope)31*0f4c859eSApple OSS Distributions getiopolicy_np(int iotype, int scope)
32*0f4c859eSApple OSS Distributions {
33*0f4c859eSApple OSS Distributions int policy, error;
34*0f4c859eSApple OSS Distributions struct _iopol_param_t iop_param;
35*0f4c859eSApple OSS Distributions
36*0f4c859eSApple OSS Distributions /* Do not sanity check iotype and scope, leave it to kernel. */
37*0f4c859eSApple OSS Distributions
38*0f4c859eSApple OSS Distributions iop_param.iop_scope = scope;
39*0f4c859eSApple OSS Distributions iop_param.iop_iotype = iotype;
40*0f4c859eSApple OSS Distributions error = __iopolicysys(IOPOL_CMD_GET, &iop_param);
41*0f4c859eSApple OSS Distributions if (error != 0) {
42*0f4c859eSApple OSS Distributions errno = error;
43*0f4c859eSApple OSS Distributions policy = -1;
44*0f4c859eSApple OSS Distributions goto exit;
45*0f4c859eSApple OSS Distributions }
46*0f4c859eSApple OSS Distributions
47*0f4c859eSApple OSS Distributions policy = iop_param.iop_policy;
48*0f4c859eSApple OSS Distributions
49*0f4c859eSApple OSS Distributions exit:
50*0f4c859eSApple OSS Distributions return policy;
51*0f4c859eSApple OSS Distributions }
52*0f4c859eSApple OSS Distributions
53*0f4c859eSApple OSS Distributions int
setiopolicy_np(int iotype,int scope,int policy)54*0f4c859eSApple OSS Distributions setiopolicy_np(int iotype, int scope, int policy)
55*0f4c859eSApple OSS Distributions {
56*0f4c859eSApple OSS Distributions /* kernel validates the indiv values, no need to repeat it */
57*0f4c859eSApple OSS Distributions struct _iopol_param_t iop_param;
58*0f4c859eSApple OSS Distributions
59*0f4c859eSApple OSS Distributions iop_param.iop_scope = scope;
60*0f4c859eSApple OSS Distributions iop_param.iop_iotype = iotype;
61*0f4c859eSApple OSS Distributions iop_param.iop_policy = policy;
62*0f4c859eSApple OSS Distributions
63*0f4c859eSApple OSS Distributions int rv = __iopolicysys(IOPOL_CMD_SET, &iop_param);
64*0f4c859eSApple OSS Distributions if (rv == -2) {
65*0f4c859eSApple OSS Distributions /* not an actual error but indication that __iopolicysys removed the thread QoS */
66*0f4c859eSApple OSS Distributions _pthread_clear_qos_tsd(MACH_PORT_NULL);
67*0f4c859eSApple OSS Distributions rv = 0;
68*0f4c859eSApple OSS Distributions }
69*0f4c859eSApple OSS Distributions
70*0f4c859eSApple OSS Distributions return rv;
71*0f4c859eSApple OSS Distributions }
72