1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include	<errno.h>
27 #include	<stdio.h>
28 #include	<stdlib.h>
29 #include	<string.h>
30 #include	<libgen.h>
31 #include	<sys/param.h>
32 #include	<sys/priocntl.h>
33 #include	<sys/types.h>
34 
35 #include	"priocntl.h"
36 
37 static char usage[] =
38 "usage:	priocntl -l\n\
39 	priocntl -d [-i idtype] [idlist]\n";
40 
41 /*
42  * A whole lot of to-do for a scheduling class that can't actually be
43  * configured or used by user processes.
44  */
45 int
46 main(int argc, char *argv[])
47 {
48 	int	dflag, eflag, iflag, lflag, sflag;
49 	int	c;
50 	char	cmdpath[MAXPATHLEN];
51 	char	basenm[BASENMSZ];
52 
53 	(void) strlcpy(cmdpath, argv[0], MAXPATHLEN);
54 	(void) strlcpy(basenm, basename(argv[0]), BASENMSZ);
55 
56 	dflag = eflag = iflag = lflag = sflag = 0;
57 	while ((c = getopt(argc, argv, "c:dei:ls")) != -1) {
58 		switch (c) {
59 
60 		case 'c':
61 			if (strcmp(optarg, "SDC") != 0)
62 				fatalerr("error: %s executed for %s class, "
63 				    "%s is actually sub-command for %s class\n",
64 				    cmdpath, optarg, cmdpath, "SDC");
65 			break;
66 
67 		case 'd':
68 			dflag++;
69 			break;
70 
71 		case 'e':
72 			eflag++;
73 			break;
74 
75 		case 'i':
76 			iflag++;	/* optarg is parsed, but ignored */
77 			break;
78 
79 		case 'l':
80 			lflag++;
81 			break;
82 
83 		case 's':
84 			sflag++;
85 			break;
86 
87 		case '?':
88 			fatalerr(usage);
89 			/*NOTREACHED*/
90 
91 		default:
92 			break;
93 		}
94 	}
95 
96 	if (sflag && eflag) {
97 		fatalerr(usage);
98 	}
99 	if (sflag || eflag) {
100 		fatalerr(
101 		    "priocntl: \"-%c\" may not be used with the %s class\n",
102 		    (sflag ? 's' : 'e'), "SDC");
103 	}
104 
105 	if ((!dflag && !lflag) || (dflag && lflag)) {
106 		fatalerr(usage);
107 	}
108 
109 	if (dflag) {
110 		pid_t *pidlist;
111 		size_t numread, i;
112 
113 		/*
114 		 * No scheduling-class-specific information to print,
115 		 * but we read the pidlist to avoid generating a SIGPIPE
116 		 * in the main priocntl process.  Once we've read it,
117 		 * we might as well print it.
118 		 */
119 		if ((pidlist = read_pidlist(&numread, stdin)) == NULL) {
120 			fatalerr("%s: Can't read pidlist.\n", basenm);
121 		} else if (numread == 0) {
122 			fatalerr("%s: No pids on input.\n", basenm);
123 		} else {
124 			(void) printf("SYSTEM DUTY-CYCLE PROCESSES:\n");
125 			(void) printf("%7s\n", "PID");
126 			for (i = 0; i < numread; i++) {
127 				(void) printf("%7ld\n", pidlist[i]);
128 			}
129 		}
130 		free_pidlist(pidlist);
131 	} else {
132 		(void) printf("SDC (System Duty-Cycle Class)\n");
133 	}
134 
135 	return (0);
136 }
137