]> git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/taskpolicy.tproj/taskpolicy.c
file_cmds: Fix BINDIRs again
[apple_cmds.git] / system_cmds / taskpolicy.tproj / taskpolicy.c
1 /*
2 * Copyright (c) 2013-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/resource.h>
30 #include <err.h>
31 #include <sys/errno.h>
32 #include <stdbool.h>
33 #include <sysexits.h>
34 #include <mach/mach.h>
35 #include <mach/task_policy.h>
36
37 #include <spawn.h>
38 #include <spawn_private.h>
39 #include <sys/spawn_internal.h>
40
41 #define QOS_PARAMETER_LATENCY 0
42 #define QOS_PARAMETER_THROUGHPUT 1
43
44 extern char **environ;
45
46 static void usage(void);
47 static int parse_disk_policy(const char *strpolicy);
48 static int parse_qos_tier(const char *strpolicy, int parameter);
49 static uint64_t parse_qos_clamp(const char *qos_string);
50
51 int main(int argc, char * argv[])
52 {
53 int ch, ret;
54 pid_t pid = 0;
55 posix_spawnattr_t attr;
56 extern char **environ;
57 bool flagx = false, flagX = false, flagb = false, flagB = false, flaga = false;
58 int flagd = -1, flagg = -1;
59 struct task_qos_policy qosinfo = { LATENCY_QOS_TIER_UNSPECIFIED, THROUGHPUT_QOS_TIER_UNSPECIFIED };
60 uint64_t qos_clamp = POSIX_SPAWN_PROC_CLAMP_NONE;
61
62 while ((ch = getopt(argc, argv, "xXbBd:g:c:t:l:p:a")) != -1) {
63 switch (ch) {
64 case 'x':
65 flagx = true;
66 break;
67 case 'X':
68 flagX = true;
69 break;
70 case 'b':
71 flagb = true;
72 break;
73 case 'B':
74 flagB = true;
75 break;
76 case 'd':
77 flagd = parse_disk_policy(optarg);
78 if (flagd == -1) {
79 warnx("Could not parse '%s' as a disk policy", optarg);
80 usage();
81 }
82 break;
83 case 'g':
84 flagg = parse_disk_policy(optarg);
85 if (flagg == -1) {
86 warnx("Could not parse '%s' as a disk policy", optarg);
87 usage();
88 }
89 break;
90 case 'c':
91 qos_clamp = parse_qos_clamp(optarg);
92 if (qos_clamp == POSIX_SPAWN_PROC_CLAMP_NONE) {
93 warnx("Could not parse '%s' as a QoS clamp", optarg);
94 usage();
95 }
96 break;
97 case 't':
98 qosinfo.task_throughput_qos_tier = parse_qos_tier(optarg, QOS_PARAMETER_THROUGHPUT);
99 if (qosinfo.task_throughput_qos_tier == -1) {
100 warnx("Could not parse '%s' as a qos tier", optarg);
101 usage();
102 }
103 break;
104 case 'l':
105 qosinfo.task_latency_qos_tier = parse_qos_tier(optarg, QOS_PARAMETER_LATENCY);
106 if (qosinfo.task_latency_qos_tier == -1) {
107 warnx("Could not parse '%s' as a qos tier", optarg);
108 usage();
109 }
110 break;
111 case 'p':
112 pid = atoi(optarg);
113 if (pid == 0) {
114 warnx("Invalid pid '%s' specified", optarg);
115 usage();
116 }
117 break;
118 case 'a':
119 flaga = true;
120 break;
121 case '?':
122 default:
123 usage();
124 }
125 }
126 argc -= optind;
127 argv += optind;
128
129 if (pid == 0 && argc == 0) {
130 usage();
131 }
132
133 if (pid != 0 && (flagx || flagX || flagg != -1 || flagd != -1)) {
134 warnx("Incompatible option(s) used with -p");
135 usage();
136 }
137
138 if (flagx && flagX){
139 warnx("Incompatible options -x, -X");
140 usage();
141 }
142
143 if (flagb && flagB) {
144 warnx("Incompatible options -b, -B");
145 usage();
146 }
147
148 if (flagB && pid == 0) {
149 warnx("The -B option can only be used with the -p option");
150 usage();
151 }
152
153 if (flagx) {
154 ret = setiopolicy_np(IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY, IOPOL_SCOPE_PROCESS, IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE);
155 if (ret == -1) {
156 err(EX_SOFTWARE, "setiopolicy_np(IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY...)");
157 }
158 }
159
160 if (flagX) {
161 ret = setiopolicy_np(IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY, IOPOL_SCOPE_PROCESS, IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT);
162 if (ret == -1) {
163 err(EX_SOFTWARE, "setiopolicy_np(IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY...)");
164 }
165 }
166
167 if (flagb) {
168 ret = setpriority(PRIO_DARWIN_PROCESS, pid, PRIO_DARWIN_BG);
169 if (ret == -1) {
170 err(EX_SOFTWARE, "setpriority()");
171 }
172 }
173
174 if (flagB) {
175 ret = setpriority(PRIO_DARWIN_PROCESS, pid, 0);
176 if (ret == -1) {
177 err(EX_SOFTWARE, "setpriority()");
178 }
179 }
180
181 if (flagd >= 0) {
182 ret = setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, flagd);
183 if (ret == -1) {
184 err(EX_SOFTWARE, "setiopolicy_np(...IOPOL_SCOPE_PROCESS...)");
185 }
186 }
187
188 if (flagg >= 0){
189 ret = setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_DARWIN_BG, flagg);
190 if (ret == -1) {
191 err(EX_SOFTWARE, "setiopolicy_np(...IOPOL_SCOPE_DARWIN_BG...)");
192 }
193 }
194
195 if (qosinfo.task_latency_qos_tier != LATENCY_QOS_TIER_UNSPECIFIED ||
196 qosinfo.task_throughput_qos_tier != THROUGHPUT_QOS_TIER_UNSPECIFIED){
197 mach_port_t task;
198 if (pid) {
199 ret = task_for_pid(mach_task_self(), pid, &task);
200 if (ret != KERN_SUCCESS) {
201 err(EX_SOFTWARE, "task_for_pid(%d) failed", pid);
202 return EX_OSERR;
203 }
204 } else {
205 task = mach_task_self();
206 }
207 ret = task_policy_set((task_t)task, TASK_OVERRIDE_QOS_POLICY, (task_policy_t)&qosinfo, TASK_QOS_POLICY_COUNT);
208 if (ret != KERN_SUCCESS){
209 err(EX_SOFTWARE, "task_policy_set(...TASK_OVERRIDE_QOS_POLICY...)");
210 }
211 }
212
213 if (pid != 0)
214 return 0;
215
216 ret = posix_spawnattr_init(&attr);
217 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawnattr_init");
218
219 ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC);
220 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawnattr_setflags");
221
222 if (qos_clamp != POSIX_SPAWN_PROC_CLAMP_NONE) {
223 ret = posix_spawnattr_set_qos_clamp_np(&attr, qos_clamp);
224 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawnattr_set_qos_clamp_np");
225 }
226
227 if (flaga) {
228 ret = posix_spawnattr_setprocesstype_np(&attr, POSIX_SPAWN_PROC_TYPE_APP_DEFAULT);
229 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawnattr_setprocesstype_np");
230
231 ret = posix_spawnattr_set_darwin_role_np(&attr, PRIO_DARWIN_ROLE_UI);
232 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawnattr_set_darwin_role_np");
233 }
234
235 ret = posix_spawnp(&pid, argv[0], NULL, &attr, argv, environ);
236 if (ret != 0) errc(EX_NOINPUT, ret, "posix_spawn");
237
238 return EX_OSERR;
239 }
240
241 static void usage(void)
242 {
243 fprintf(stderr, "Usage: %s [-x|-X] [-d <policy>] [-g policy] [-c clamp] [-b] [-t <tier>]\n"
244 " [-l <tier>] [-a] <program> [<pargs> [...]]\n", getprogname());
245 fprintf(stderr, " %s [-b|-B] [-t <tier>] [-l <tier>] -p pid\n", getprogname());
246 exit(EX_USAGE);
247 }
248
249 static int parse_disk_policy(const char *strpolicy)
250 {
251 long policy;
252 char *endptr = NULL;
253
254 /* first try as an integer */
255 policy = strtol(strpolicy, &endptr, 0);
256 if (endptr && (endptr[0] == '\0') && (strpolicy[0] != '\0')) {
257 /* parsed complete string as a number */
258 return (int)policy;
259 }
260
261 if (0 == strcasecmp(strpolicy, "DEFAULT") ) {
262 return IOPOL_DEFAULT;
263 } else if (0 == strcasecmp(strpolicy, "IMPORTANT")) {
264 return IOPOL_IMPORTANT;
265 } else if (0 == strcasecmp(strpolicy, "PASSIVE")) {
266 return IOPOL_PASSIVE;
267 } else if (0 == strcasecmp(strpolicy, "THROTTLE")) {
268 return IOPOL_THROTTLE;
269 } else if (0 == strcasecmp(strpolicy, "UTILITY")) {
270 return IOPOL_UTILITY;
271 } else if (0 == strcasecmp(strpolicy, "STANDARD")) {
272 return IOPOL_STANDARD;
273 } else {
274 return -1;
275 }
276 }
277
278 static int parse_qos_tier(const char *strtier, int parameter){
279 long policy;
280 char *endptr = NULL;
281
282 /* first try as an integer */
283 policy = strtol(strtier, &endptr, 0);
284 if (endptr && (endptr[0] == '\0') && (strtier[0] != '\0')) {
285 switch (policy) {
286 case 0:
287 return parameter ? THROUGHPUT_QOS_TIER_0 : LATENCY_QOS_TIER_0;
288 break;
289 case 1:
290 return parameter ? THROUGHPUT_QOS_TIER_1 : LATENCY_QOS_TIER_1;
291 break;
292 case 2:
293 return parameter ? THROUGHPUT_QOS_TIER_2 : LATENCY_QOS_TIER_2;
294 break;
295 case 3:
296 return parameter ? THROUGHPUT_QOS_TIER_3 : LATENCY_QOS_TIER_3;
297 break;
298 case 4:
299 return parameter ? THROUGHPUT_QOS_TIER_4 : LATENCY_QOS_TIER_4;
300 break;
301 case 5:
302 return parameter ? THROUGHPUT_QOS_TIER_5 : LATENCY_QOS_TIER_5;
303 break;
304 default:
305 return -1;
306 break;
307 }
308 }
309
310 return -1;
311 }
312
313 static uint64_t parse_qos_clamp(const char *qos_string) {
314
315 if (0 == strcasecmp(qos_string, "utility") ) {
316 return POSIX_SPAWN_PROC_CLAMP_UTILITY;
317 } else if (0 == strcasecmp(qos_string, "background")) {
318 return POSIX_SPAWN_PROC_CLAMP_BACKGROUND;
319 } else if (0 == strcasecmp(qos_string, "maintenance")) {
320 return POSIX_SPAWN_PROC_CLAMP_MAINTENANCE;
321 } else {
322 return POSIX_SPAWN_PROC_CLAMP_NONE;
323 }
324 }