4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
30 /* All Rights Reserved */
37 #include <sys/types.h>
39 #include <sys/procset.h>
40 #include <sys/priocntl.h>
41 #include <sys/rtpriocntl.h>
42 #include <sys/param.h>
51 * This file contains the class specific code implementing
52 * the real-time priocntl sub-command.
55 #define ADDKEYVAL(p, k, v) { (p[0]) = (k); (p[1]) = (v); p += 2; }
57 #define RT_KEYCNT 4 /* maximal number of (key, value) pairs */
62 #define RT_DOPRI 0x01 /* change priority */
63 #define RT_DOTQ 0x02 /* change RT time quantum */
64 #define RT_DOSIG 0x10 /* change RT time quantum signal */
67 static void print_rtinfo(void);
68 static int print_rtprocs(void);
69 static int rt_priocntl(idtype_t
, id_t
, int, char *, uintptr_t *);
70 static int set_rtprocs(idtype_t
, int, char **, uint_t
, pri_t
, long,
72 static void exec_rtcmd(char **, uint_t
, pri_t
, long, long, int);
76 "usage: priocntl -l\n"
77 " priocntl -d [-i idtype] [idlist]\n"
78 " priocntl -s [-c RT] [-p rtpri] [-t tqntm [-r res]] [-q tqsig]\n"
79 " [-i idtype] [idlist]\n"
80 " priocntl -e [-c RT] [-p rtpri] [-t tqntm [-r res]] [-q tqsig]\n"
81 " command [argument(s)]\n";
83 static char cmdpath
[MAXPATHLEN
];
84 static char basenm
[BASENMSZ
];
88 main(int argc
, char *argv
[])
91 int lflag
, dflag
, sflag
, pflag
;
92 int tflag
, rflag
, eflag
, iflag
, qflag
;
102 (void) strlcpy(cmdpath
, argv
[0], MAXPATHLEN
);
103 (void) strlcpy(basenm
, basename(argv
[0]), BASENMSZ
);
104 lflag
= dflag
= sflag
= pflag
= 0;
105 tflag
= rflag
= eflag
= iflag
= qflag
= 0;
106 while ((c
= getopt(argc
, argv
, "ldsp:t:r:q:ec:i:")) != -1) {
123 rtpri
= (pri_t
)str2num(optarg
, SHRT_MIN
, SHRT_MAX
);
125 fatalerr("%s: Specified real time priority %s"
126 " out of configured range\n",
132 tqntm
= str2num(optarg
, 1, INT_MAX
);
134 fatalerr("%s: Invalid time quantum specified;"
135 " time quantum must be positive\n", basenm
);
140 res
= str2num(optarg
, 1, 1000000000);
142 fatalerr("%s: Invalid resolution specified;"
143 " resolution must be between"
144 " 1 and 1,000,000,000\n", basenm
);
150 if (str2sig(optarg
, &tqsig
) != 0)
151 fatalerr("%s: Invalid real time quantum signal"
152 " specified\n", basenm
);
160 if (strcmp(optarg
, "RT") != 0)
161 fatalerr("error: %s executed for %s class, %s"
162 " is actually sub-command for RT class\n",
163 cmdpath
, optarg
, cmdpath
);
180 if (dflag
|| sflag
|| pflag
|| tflag
|| rflag
|| eflag
||
187 if (lflag
|| sflag
|| pflag
|| tflag
|| rflag
|| eflag
|| qflag
)
190 return (print_rtprocs());
193 if (lflag
|| dflag
|| eflag
)
197 if (str2idtyp(idtypnm
, &idtype
) == -1)
198 fatalerr("%s: Bad idtype %s\n", basenm
,
204 cflags
= (pflag
? RT_DOPRI
: 0);
213 idargc
= argc
- optind
;
220 return (set_rtprocs(idtype
, idargc
, &argv
[optind
], cflags
,
221 rtpri
, tqntm
, res
, tqsig
));
224 if (lflag
|| dflag
|| sflag
|| iflag
)
227 cflags
= (pflag
? RT_DOPRI
: 0);
238 exec_rtcmd(&argv
[optind
], cflags
, rtpri
, tqntm
, res
, tqsig
);
249 * Print our class name and the configured user priority range.
256 (void) strcpy(pcinfo
.pc_clname
, "RT");
258 (void) printf("RT (Real Time)\n");
260 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
261 fatalerr("\tCan't get maximum configured RT priority\n");
263 (void) printf("\tConfigured RT User Priority Range: 0 through %d\n",
264 ((rtinfo_t
*)pcinfo
.pc_clinfo
)->rt_maxpri
);
269 * Read a list of pids from stdin and print the real-time priority and time
270 * quantum (in millisecond resolution) for each of the corresponding processes.
278 char clname
[PC_CLNMSZ
];
286 * Read a list of pids from stdin.
288 if ((pidlist
= read_pidlist(&numread
, stdin
)) == NULL
)
289 fatalerr("%s: Can't read pidlist.\n", basenm
);
291 (void) printf("REAL TIME PROCESSES:\n"
292 " PID RTPRI TQNTM TQSIG\n");
295 fatalerr("%s: No pids on input\n", basenm
);
297 for (i
= 0; i
< numread
; i
++) {
298 (void) printf("%7ld", pidlist
[i
]);
299 if (priocntl(P_PID
, pidlist
[i
], PC_GETXPARMS
, "RT",
300 RT_KY_TQSECS
, &rt_tqsecs
, RT_KY_TQNSECS
, &rt_tqnsecs
,
301 RT_KY_PRI
, &rt_pri
, RT_KY_TQSIG
, &rt_tqsig
, 0) != -1) {
302 (void) printf(" %5d", rt_pri
);
303 if (rt_tqnsecs
== RT_TQINF
)
304 (void) printf(" RT_TQINF");
306 (void) printf(" %11lld",
307 (longlong_t
)rt_tqsecs
* 1000 +
308 rt_tqnsecs
/ 1000000);
310 (void) printf(" %3d\n", rt_tqsig
);
314 if (priocntl(P_PID
, pidlist
[i
], PC_GETXPARMS
, NULL
,
315 PC_KY_CLNAME
, clname
, 0) != -1 &&
316 strcmp(clname
, "RT"))
318 * Process from some class other than real time.
319 * It has probably changed class while priocntl
320 * command was executing (otherwise we wouldn't
321 * have been passed its pid). Print the little
324 (void) printf("\tChanged to class %s while"
325 " priocntl command executing\n", clname
);
327 (void) printf("\tCan't get real time"
332 free_pidlist(pidlist
);
338 * Call priocntl() with command codes PC_SETXPARMS or PC_GETXPARMS.
339 * The first parameter behind the command code is always the class name.
340 * Each parameter is headed by a key, which determines the meaning of the
341 * following value. There are maximal RT_KEYCNT = 4 (key, value) pairs.
344 rt_priocntl(idtype_t idtype
, id_t id
, int cmd
, char *clname
, uintptr_t *argsp
)
346 return (priocntl(idtype
, id
, cmd
, clname
, argsp
[0], argsp
[1],
347 argsp
[2], argsp
[3], argsp
[4], argsp
[5], argsp
[6], argsp
[7], 0));
352 * Set all processes in the set specified by idtype/idargv to real time
353 * (if they aren't already real time) and set their real-time priority,
354 * real-time quantum and real-time quantum signal to those specified by
355 * rtpri, tqntm/res and rtqsig.
358 set_rtprocs(idtype_t idtype
, int idargc
, char **idargv
, uint_t cflags
,
359 pri_t rtpri
, long tqntm
, long res
, int rtqsig
)
362 uintptr_t args
[2*RT_KEYCNT
+1];
363 uintptr_t *argsp
= &args
[0];
366 char idtypnm
[PC_IDTYPNMSZ
];
373 * Get the real time class ID and max configured RT priority.
375 (void) strcpy(pcinfo
.pc_clname
, "RT");
376 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
377 fatalerr("%s: Can't get RT class ID, priocntl system call"
378 " failed with errno %d\n", basenm
, errno
);
379 maxrtpri
= ((rtinfo_t
*)pcinfo
.pc_clinfo
)->rt_maxpri
;
382 * Validate the rtpri and res arguments.
384 if ((cflags
& RT_DOPRI
) != 0) {
385 if (rtpri
> maxrtpri
|| rtpri
< 0)
386 fatalerr("%s: Specified real time priority %d out of"
387 " configured range\n", basenm
, rtpri
);
388 ADDKEYVAL(argsp
, RT_KY_PRI
, rtpri
);
391 if ((cflags
& RT_DOTQ
) != 0) {
393 hrtime
.hrt_rem
= tqntm
;
394 hrtime
.hrt_res
= res
;
395 if (_hrtnewres(&hrtime
, NANOSEC
, HRT_RNDUP
) == -1)
396 fatalerr("%s: Can't convert resolution.\n", basenm
);
397 ADDKEYVAL(argsp
, RT_KY_TQSECS
, hrtime
.hrt_secs
);
398 ADDKEYVAL(argsp
, RT_KY_TQNSECS
, hrtime
.hrt_rem
);
401 if ((cflags
& RT_DOSIG
) != 0)
402 ADDKEYVAL(argsp
, RT_KY_TQSIG
, rtqsig
);
405 if (idtype
== P_ALL
) {
406 if (rt_priocntl(P_ALL
, 0, PC_SETXPARMS
, "RT", args
) == -1) {
407 if (errno
== EPERM
) {
408 (void) fprintf(stderr
,
409 "Permissions error encountered"
410 " on one or more processes.\n");
413 fatalerr("%s: Can't reset real time parameters"
414 "\npriocntl system call failed with"
415 " errno %d\n", basenm
, errno
);
418 } else if (idargc
== 0) {
419 if (rt_priocntl(idtype
, P_MYID
, PC_SETXPARMS
, "RT",
421 if (errno
== EPERM
) {
422 (void) idtyp2str(idtype
, idtypnm
);
423 (void) fprintf(stderr
, "Permissions error"
424 " encountered on current %s.\n", idtypnm
);
427 fatalerr("%s: Can't reset real time parameters"
428 "\npriocntl system call failed with"
429 " errno %d\n", basenm
, errno
);
433 (void) idtyp2str(idtype
, idtypnm
);
434 for (i
= 0; i
< idargc
; i
++) {
435 if (idtype
== P_CID
) {
436 (void) strcpy(pcinfo
.pc_clname
, idargv
[i
]);
437 if (priocntl(0, 0, PC_GETCID
,
438 (caddr_t
)&pcinfo
) == -1)
439 fatalerr("%s: Invalid or unconfigured"
440 " class %s, priocntl system call"
441 " failed with errno %d\n",
442 basenm
, pcinfo
.pc_clname
, errno
);
445 id
= (id_t
)str2num(idargv
[i
], INT_MIN
, INT_MAX
);
447 fatalerr("%s: Invalid id \"%s\"\n",
451 if (rt_priocntl(idtype
, id
, PC_SETXPARMS
, "RT",
453 if (errno
== EPERM
) {
454 (void) fprintf(stderr
,
455 "Permissions error encountered on"
456 " %s %s.\n", idtypnm
, idargv
[i
]);
459 fatalerr("%s: Can't reset real time"
460 " parameters\npriocntl system call"
461 " failed with errno %d\n",
473 * Execute the command pointed to by cmdargv as a real-time process
474 * with real time priority rtpri, quantum tqntm/res and quantum signal rtqsig.
477 exec_rtcmd(char **cmdargv
, uint_t cflags
, pri_t rtpri
, long tqntm
, long res
,
481 uintptr_t args
[2*RT_KEYCNT
+1];
482 uintptr_t *argsp
= &args
[0];
487 * Get the real time class ID and max configured RT priority.
489 (void) strcpy(pcinfo
.pc_clname
, "RT");
490 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
491 fatalerr("%s: Can't get RT class ID, priocntl system call"
492 " failed with errno %d\n", basenm
, errno
);
493 maxrtpri
= ((rtinfo_t
*)pcinfo
.pc_clinfo
)->rt_maxpri
;
495 if ((cflags
& RT_DOPRI
) != 0) {
496 if (rtpri
> maxrtpri
|| rtpri
< 0)
497 fatalerr("%s: Specified real time priority %d out of"
498 " configured range\n", basenm
, rtpri
);
499 ADDKEYVAL(argsp
, RT_KY_PRI
, rtpri
);
502 if ((cflags
& RT_DOTQ
) != 0) {
504 hrtime
.hrt_rem
= tqntm
;
505 hrtime
.hrt_res
= res
;
506 if (_hrtnewres(&hrtime
, NANOSEC
, HRT_RNDUP
) == -1)
507 fatalerr("%s: Can't convert resolution.\n", basenm
);
508 ADDKEYVAL(argsp
, RT_KY_TQSECS
, hrtime
.hrt_secs
);
509 ADDKEYVAL(argsp
, RT_KY_TQNSECS
, hrtime
.hrt_rem
);
512 if ((cflags
& RT_DOSIG
) != 0)
513 ADDKEYVAL(argsp
, RT_KY_TQSIG
, rtqsig
);
516 if (rt_priocntl(P_PID
, P_MYID
, PC_SETXPARMS
, "RT", args
) == -1)
517 fatalerr("%s: Can't reset real time parameters\n"
518 "priocntl system call failed with errno %d\n",
521 (void) execvp(cmdargv
[0], cmdargv
);
522 fatalerr("%s: Can't execute %s, exec failed with errno %d\n",
523 basenm
, cmdargv
[0], errno
);