]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2015 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | #include <sys/param.h> | |
29 | #include <sys/kernel.h> | |
30 | #include <sys/kernel_types.h> | |
31 | #include <sys/sysproto.h> | |
32 | #include <sys/priv.h> | |
33 | #include <sys/work_interval.h> | |
34 | #include <kern/sched_prim.h> | |
35 | #include <kern/thread.h> | |
36 | #include <kern/work_interval.h> | |
37 | ||
38 | #include <libkern/libkern.h> | |
39 | ||
40 | int | |
41 | work_interval_ctl(__unused proc_t p, struct work_interval_ctl_args *uap, | |
42 | __unused int32_t *retval) | |
43 | { | |
44 | uint32_t operation = uap->operation; | |
45 | int error = 0; | |
46 | kern_return_t kret = KERN_SUCCESS; | |
47 | struct work_interval_notification notification; | |
48 | ||
49 | /* Two different structs, because headers are complicated */ | |
50 | struct work_interval_create_params create_params; | |
51 | struct kern_work_interval_create_args create_args; | |
52 | ||
53 | switch (operation) { | |
54 | case WORK_INTERVAL_OPERATION_CREATE: | |
55 | return ENOTSUP; | |
56 | case WORK_INTERVAL_OPERATION_CREATE2: | |
57 | if (uap->arg == USER_ADDR_NULL || uap->work_interval_id != 0) | |
58 | return EINVAL; | |
59 | if (uap->len < sizeof(create_params)) | |
60 | return EINVAL; | |
61 | ||
62 | /* | |
63 | * Privilege check performed up-front, and then the work | |
64 | * ID is allocated for use by the thread | |
65 | */ | |
66 | if ((error = priv_check_cred(kauth_cred_get(), PRIV_WORK_INTERVAL, 0))) | |
67 | return error; | |
68 | ||
69 | if ((error = copyin(uap->arg, &create_params, sizeof(create_params)))) | |
70 | return error; | |
71 | ||
72 | create_args = (struct kern_work_interval_create_args) { | |
73 | .wica_id = create_params.wicp_id, | |
74 | .wica_port = create_params.wicp_port, | |
75 | .wica_create_flags = create_params.wicp_create_flags, | |
76 | }; | |
77 | ||
78 | kret = kern_work_interval_create(current_thread(), &create_args); | |
79 | ||
80 | /* thread already has a work interval */ | |
81 | if (kret == KERN_FAILURE) | |
82 | return EALREADY; | |
83 | ||
84 | /* port copyout failed */ | |
85 | if (kret == KERN_RESOURCE_SHORTAGE) | |
86 | return ENOMEM; | |
87 | ||
88 | /* some other failure */ | |
89 | if (kret != KERN_SUCCESS) | |
90 | return EINVAL; | |
91 | ||
92 | create_params = (struct work_interval_create_params) { | |
93 | .wicp_id = create_args.wica_id, | |
94 | .wicp_port = create_args.wica_port, | |
95 | .wicp_create_flags = create_args.wica_create_flags, | |
96 | }; | |
97 | ||
98 | if ((error = copyout(&create_params, uap->arg, sizeof(create_params)))) | |
99 | return error; | |
100 | ||
101 | break; | |
102 | case WORK_INTERVAL_OPERATION_DESTROY: | |
103 | if (uap->arg != USER_ADDR_NULL || uap->work_interval_id == 0) { | |
104 | return EINVAL; | |
105 | } | |
106 | ||
107 | /* | |
108 | * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE | |
109 | * operation would have allocated a work interval ID for the current | |
110 | * thread, which the scheduler will validate. | |
111 | */ | |
112 | kret = kern_work_interval_destroy(current_thread(), uap->work_interval_id); | |
113 | if (kret != KERN_SUCCESS) | |
114 | return EINVAL; | |
115 | ||
116 | break; | |
117 | case WORK_INTERVAL_OPERATION_NOTIFY: | |
118 | if (uap->arg == USER_ADDR_NULL || uap->work_interval_id == 0) | |
119 | return EINVAL; | |
120 | ||
121 | if (uap->len < sizeof(notification)) | |
122 | return EINVAL; | |
123 | ||
124 | /* | |
125 | * No privilege check, we assume a previous WORK_INTERVAL_OPERATION_CREATE | |
126 | * operation would have allocated a work interval ID for the current | |
127 | * thread, which the scheduler will validate. | |
128 | */ | |
129 | if ((error = copyin(uap->arg, ¬ification, sizeof(notification)))) | |
130 | return error; | |
131 | ||
132 | struct kern_work_interval_args kwi_args = { | |
133 | .work_interval_id = uap->work_interval_id, | |
134 | .start = notification.start, | |
135 | .finish = notification.finish, | |
136 | .deadline = notification.deadline, | |
137 | .next_start = notification.next_start, | |
138 | .notify_flags = notification.notify_flags, | |
139 | .create_flags = notification.create_flags, | |
140 | }; | |
141 | ||
142 | kret = kern_work_interval_notify(current_thread(), &kwi_args); | |
143 | if (kret != KERN_SUCCESS) | |
144 | return EINVAL; | |
145 | ||
146 | break; | |
147 | case WORK_INTERVAL_OPERATION_JOIN: | |
148 | if (uap->arg != USER_ADDR_NULL) { | |
149 | return EINVAL; | |
150 | } | |
151 | ||
152 | /* | |
153 | * No privilege check, because the work interval port | |
154 | * is a capability. | |
155 | */ | |
156 | kret = kern_work_interval_join(current_thread(), | |
157 | (mach_port_name_t)uap->work_interval_id); | |
158 | if (kret != KERN_SUCCESS) | |
159 | return EINVAL; | |
160 | ||
161 | break; | |
162 | ||
163 | default: | |
164 | return ENOTSUP; | |
165 | } | |
166 | ||
167 | return (error); | |
168 | } | |
169 |