4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 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>
38 #include <sys/procset.h>
39 #include <sys/priocntl.h>
40 #include <sys/tspriocntl.h>
48 * This file contains the class specific code implementing
49 * the time-sharing priocntl sub-command.
52 #define ADDKEYVAL(p, k, v) { (p[0]) = (k); (p[1]) = (v); p += 2; }
54 #define TS_KEYCNT 2 /* maximal number of (key, value) pairs */
59 #define TS_DOUPRILIM 0x01 /* user priority limit */
60 #define TS_DOUPRI 0x02 /* user priority */
63 static void print_tsinfo(void);
64 static int print_tsprocs(void);
65 static int ts_priocntl(idtype_t
, id_t
, int, char *, uintptr_t *);
66 static int set_tsprocs(idtype_t
, int, char **, uint_t
, pri_t
, pri_t
);
67 static void exec_tscmd(char **, uint_t
, pri_t
, pri_t
);
71 "usage: priocntl -l\n\
72 priocntl -d [-d idtype] [idlist]\n\
73 priocntl -s [-c TS] [-m tsuprilim] [-p tsupri] [-i idtype] [idlist]\n\
74 priocntl -e [-c TS] [-m tsuprilim] [-p tsupri] command [argument(s)]\n";
76 static char cmdpath
[MAXPATHLEN
];
77 static char basenm
[BASENMSZ
];
81 main(int argc
, char *argv
[])
84 int lflag
, dflag
, sflag
, mflag
, pflag
, eflag
, iflag
;
92 (void) strlcpy(cmdpath
, argv
[0], MAXPATHLEN
);
93 (void) strlcpy(basenm
, basename(argv
[0]), BASENMSZ
);
94 lflag
= dflag
= sflag
= mflag
= pflag
= eflag
= iflag
= 0;
95 while ((c
= getopt(argc
, argv
, "ldsm:p:ec:i:")) != -1) {
112 tsuprilim
= (pri_t
)str2num(optarg
, SHRT_MIN
, SHRT_MAX
);
114 fatalerr("%s: Specified user priority limit %s"
115 " out of configured range\n",
121 tsupri
= (pri_t
)str2num(optarg
, SHRT_MIN
, SHRT_MAX
);
123 fatalerr("%s: Specified user priority %s out of"
124 " configured range\n", basenm
, optarg
);
132 if (strcmp(optarg
, "TS") != 0)
133 fatalerr("error: %s executed for %s class, %s"
134 " is actually sub-command for TS class\n",
135 cmdpath
, optarg
, cmdpath
);
152 if (dflag
|| sflag
|| mflag
|| pflag
|| eflag
|| iflag
)
158 if (lflag
|| sflag
|| mflag
|| pflag
|| eflag
)
161 return (print_tsprocs());
164 if (lflag
|| dflag
|| eflag
)
168 if (str2idtyp(idtypnm
, &idtype
) == -1)
169 fatalerr("%s: Bad idtype %s\n", basenm
,
175 cflags
= (pflag
? TS_DOUPRI
: 0);
178 cflags
|= TS_DOUPRILIM
;
181 idargc
= argc
- optind
;
185 return (set_tsprocs(idtype
, idargc
, &argv
[optind
], cflags
,
189 if (lflag
|| dflag
|| sflag
|| iflag
)
192 cflags
= (pflag
? TS_DOUPRI
: 0);
195 cflags
|= TS_DOUPRILIM
;
197 exec_tscmd(&argv
[optind
], cflags
, tsuprilim
, tsupri
);
208 * Print our class name and the configured user priority range.
215 (void) strcpy(pcinfo
.pc_clname
, "TS");
217 (void) printf("TS (Time Sharing)\n");
219 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
220 fatalerr("\tCan't get configured TS user priority range\n");
222 (void) printf("\tConfigured TS User Priority Range: -%d through %d\n",
223 ((tsinfo_t
*)pcinfo
.pc_clinfo
)->ts_maxupri
,
224 ((tsinfo_t
*)pcinfo
.pc_clinfo
)->ts_maxupri
);
229 * Read a list of pids from stdin and print the user priority and user
230 * priority limit for each of the corresponding processes.
238 char clname
[PC_CLNMSZ
];
244 * Read a list of pids from stdin.
246 if ((pidlist
= read_pidlist(&numread
, stdin
)) == NULL
)
247 fatalerr("%s: Can't read pidlist.\n", basenm
);
250 "TIME SHARING PROCESSES:\n PID TSUPRILIM TSUPRI\n");
253 fatalerr("%s: No pids on input\n", basenm
);
255 for (i
= 0; i
< numread
; i
++) {
256 (void) printf("%7ld", pidlist
[i
]);
257 if (priocntl(P_PID
, pidlist
[i
], PC_GETXPARMS
, "TS", TS_KY_UPRI
,
258 &ts_upri
, TS_KY_UPRILIM
, &ts_uprilim
, 0) != -1) {
259 (void) printf(" %5d %5d\n",
260 ts_uprilim
, ts_upri
);
264 if (priocntl(P_PID
, pidlist
[i
], PC_GETXPARMS
, NULL
,
265 PC_KY_CLNAME
, clname
, 0) != -1 &&
266 strcmp(clname
, "TS"))
268 * Process from some class other than time
269 * sharing. It has probably changed class while
270 * priocntl command was executing (otherwise
271 * we wouldn't have been passed its pid).
272 * Print the little we know about it.
274 (void) printf("\tChanged to class %s while"
275 " priocntl command executing\n", clname
);
277 (void) printf("\tCan't get TS user priority\n");
281 free_pidlist(pidlist
);
287 * Call priocntl() with command codes PC_SETXPARMS or PC_GETXPARMS.
288 * The first parameter behind the command code is always the class name.
289 * Each parameter is headed by a key, which determines the meaning of the
290 * following value. There are maximal TS_KEYCNT = 2 (key, value) pairs.
293 ts_priocntl(idtype_t idtype
, id_t id
, int cmd
, char *clname
, uintptr_t *argsp
)
295 return (priocntl(idtype
, id
, cmd
, clname
, argsp
[0], argsp
[1],
296 argsp
[2], argsp
[3], 0));
301 * Set all processes in the set specified by idtype/idargv to time-sharing
302 * (if they aren't already time-sharing) and set their user priority limit
303 * and user priority to those specified by tsuprilim and tsupri.
306 set_tsprocs(idtype_t idtype
, int idargc
, char **idargv
, uint_t cflags
,
307 pri_t tsuprilim
, pri_t tsupri
)
310 uintptr_t args
[2*TS_KEYCNT
+1];
311 uintptr_t *argsp
= &args
[0];
313 char idtypnm
[PC_IDTYPNMSZ
];
319 * Get the time sharing class ID and max configured user priority.
321 (void) strcpy(pcinfo
.pc_clname
, "TS");
322 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
323 fatalerr("%s: Can't get TS class ID, priocntl system call"
324 " failed with errno %d\n", basenm
, errno
);
325 maxupri
= ((tsinfo_t
*)pcinfo
.pc_clinfo
)->ts_maxupri
;
328 * Validate the tsuprilim and tsupri arguments.
330 if ((cflags
& TS_DOUPRILIM
) != 0) {
331 if (tsuprilim
> maxupri
|| tsuprilim
< -maxupri
)
332 fatalerr("%s: Specified user priority limit %d out of"
333 " configured range\n", basenm
, tsuprilim
);
334 ADDKEYVAL(argsp
, TS_KY_UPRILIM
, tsuprilim
);
337 if ((cflags
& TS_DOUPRI
) != 0) {
338 if (tsupri
> maxupri
|| tsupri
< -maxupri
)
339 fatalerr("%s: Specified user priority %d out of"
340 " configured range\n", basenm
, tsupri
);
341 ADDKEYVAL(argsp
, TS_KY_UPRI
, tsupri
);
345 if (idtype
== P_ALL
) {
346 if (ts_priocntl(P_ALL
, 0, PC_SETXPARMS
, "TS", args
) == -1) {
347 if (errno
== EPERM
) {
348 (void) fprintf(stderr
,
349 "Permissions error encountered"
350 " on one or more processes.\n");
353 fatalerr("%s: Can't reset time sharing"
354 " parameters\npriocntl system call failed"
355 " with errno %d\n", basenm
, errno
);
357 } else if ((cflags
& (TS_DOUPRILIM
|TS_DOUPRI
)) == TS_DOUPRI
) {
358 (void) verifyupri(idtype
, 0, "TS", TS_KY_UPRILIM
,
361 } else if (idargc
== 0) {
362 if (ts_priocntl(idtype
, P_MYID
, PC_SETXPARMS
, "TS",
364 if (errno
== EPERM
) {
365 (void) idtyp2str(idtype
, idtypnm
);
366 (void) fprintf(stderr
, "Permissions error"
367 " encountered on current %s.\n", idtypnm
);
370 fatalerr("%s: Can't reset time sharing"
371 " parameters\npriocntl system call failed"
372 " with errno %d\n", basenm
, errno
);
374 } else if ((cflags
& (TS_DOUPRILIM
|TS_DOUPRI
)) == TS_DOUPRI
&&
375 getmyid(idtype
, &id
) != -1) {
376 (void) verifyupri(idtype
, id
, "TS", TS_KY_UPRILIM
,
380 (void) idtyp2str(idtype
, idtypnm
);
381 for (i
= 0; i
< idargc
; i
++) {
382 if (idtype
== P_CID
) {
383 (void) strcpy(pcinfo
.pc_clname
, idargv
[i
]);
384 if (priocntl(0, 0, PC_GETCID
,
385 (caddr_t
)&pcinfo
) == -1)
386 fatalerr("%s: Invalid or unconfigured"
387 " class %s, priocntl system call"
388 " failed with errno %d\n",
389 basenm
, pcinfo
.pc_clname
, errno
);
392 id
= (id_t
)str2num(idargv
[i
], INT_MIN
, INT_MAX
);
394 fatalerr("%s: Invalid id \"%s\"\n",
398 if (ts_priocntl(idtype
, id
, PC_SETXPARMS
, "TS",
400 if (errno
== EPERM
) {
401 (void) fprintf(stderr
,
402 "Permissions error encountered on"
403 " %s %s.\n", idtypnm
, idargv
[i
]);
406 fatalerr("%s: Can't reset time sharing"
407 " parameters\npriocntl system call"
408 " failed with errno %d\n",
411 } else if ((cflags
& (TS_DOUPRILIM
|TS_DOUPRI
)) ==
413 (void) verifyupri(idtype
, id
, "TS",
414 TS_KY_UPRILIM
, tsupri
, basenm
);
424 * Execute the command pointed to by cmdargv as a time-sharing process
425 * with the user priority limit given by tsuprilim and user priority tsupri.
428 exec_tscmd(char **cmdargv
, uint_t cflags
, pri_t tsuprilim
, pri_t tsupri
)
431 uintptr_t args
[2*TS_KEYCNT
+1];
432 uintptr_t *argsp
= &args
[0];
437 * Get the time sharing class ID and max configured user priority.
439 (void) strcpy(pcinfo
.pc_clname
, "TS");
440 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1)
441 fatalerr("%s: Can't get TS class ID, priocntl system call"
442 " failed with errno %d\n", basenm
, errno
);
443 maxupri
= ((tsinfo_t
*)pcinfo
.pc_clinfo
)->ts_maxupri
;
445 if ((cflags
& TS_DOUPRILIM
) != 0) {
446 if (tsuprilim
> maxupri
|| tsuprilim
< -maxupri
)
447 fatalerr("%s: Specified user priority limit %d out of"
448 " configured range\n", basenm
, tsuprilim
);
449 ADDKEYVAL(argsp
, TS_KY_UPRILIM
, tsuprilim
);
452 if ((cflags
& TS_DOUPRI
) != 0) {
453 if (tsupri
> maxupri
|| tsupri
< -maxupri
)
454 fatalerr("%s: Specified user priority %d out of"
455 " configured range\n", basenm
, tsupri
);
456 ADDKEYVAL(argsp
, TS_KY_UPRI
, tsupri
);
460 if (ts_priocntl(P_PID
, P_MYID
, PC_SETXPARMS
, "TS", args
) == -1)
461 fatalerr("%s: Can't reset time sharing parameters\n"
462 "priocntl system call failed with errno %d\n",
465 if ((cflags
& (TS_DOUPRILIM
|TS_DOUPRI
)) == TS_DOUPRI
) {
466 if (priocntl(P_PID
, P_MYID
, PC_GETXPARMS
, "TS",
467 TS_KY_UPRILIM
, &uprilim
, 0) != -1 && tsupri
> uprilim
)
468 (void) fprintf(stderr
,
469 "%s: Specified user priority %d exceeds"
470 " limit %d; set to %d (pid %d)\n",
471 basenm
, tsupri
, uprilim
, uprilim
, (int)getpid());
474 (void) execvp(cmdargv
[0], cmdargv
);
475 fatalerr("%s: Can't execute %s, exec failed with errno %d\n",
476 basenm
, cmdargv
[0], errno
);