135a5a35Jonathan Adams/*
235a5a35Jonathan Adams * CDDL HEADER START
335a5a35Jonathan Adams *
435a5a35Jonathan Adams * The contents of this file are subject to the terms of the
535a5a35Jonathan Adams * Common Development and Distribution License (the "License").
635a5a35Jonathan Adams * You may not use this file except in compliance with the License.
735a5a35Jonathan Adams *
835a5a35Jonathan Adams * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
935a5a35Jonathan Adams * or http://www.opensolaris.org/os/licensing.
1035a5a35Jonathan Adams * See the License for the specific language governing permissions
1135a5a35Jonathan Adams * and limitations under the License.
1235a5a35Jonathan Adams *
1335a5a35Jonathan Adams * When distributing Covered Code, include this CDDL HEADER in each
1435a5a35Jonathan Adams * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1535a5a35Jonathan Adams * If applicable, add the following below this CDDL HEADER, with the
1635a5a35Jonathan Adams * fields enclosed by brackets "[]" replaced with your own identifying
1735a5a35Jonathan Adams * information: Portions Copyright [yyyy] [name of copyright owner]
1835a5a35Jonathan Adams *
1935a5a35Jonathan Adams * CDDL HEADER END
2035a5a35Jonathan Adams */
2135a5a35Jonathan Adams/*
2235a5a35Jonathan Adams * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2335a5a35Jonathan Adams * Use is subject to license terms.
2435a5a35Jonathan Adams */
2535a5a35Jonathan Adams
2635a5a35Jonathan Adams#include	<errno.h>
2735a5a35Jonathan Adams#include	<stdio.h>
2835a5a35Jonathan Adams#include	<stdlib.h>
2935a5a35Jonathan Adams#include	<string.h>
30ac23bf8Blake Jones#include	<libgen.h>
3135a5a35Jonathan Adams#include	<sys/param.h>
3235a5a35Jonathan Adams#include	<sys/priocntl.h>
3335a5a35Jonathan Adams#include	<sys/types.h>
3435a5a35Jonathan Adams
3535a5a35Jonathan Adams#include	"priocntl.h"
3635a5a35Jonathan Adams
37ac23bf8Blake Jonesstatic char usage[] =
38ac23bf8Blake Jones"usage:	priocntl -l\n\
39ac23bf8Blake Jones	priocntl -d [-i idtype] [idlist]\n";
4035a5a35Jonathan Adams
41ac23bf8Blake Jones/*
42ac23bf8Blake Jones * A whole lot of to-do for a scheduling class that can't actually be
43ac23bf8Blake Jones * configured or used by user processes.
44ac23bf8Blake Jones */
4535a5a35Jonathan Adamsint
4635a5a35Jonathan Adamsmain(int argc, char *argv[])
4735a5a35Jonathan Adams{
48ac23bf8Blake Jones	int	dflag, eflag, iflag, lflag, sflag;
49ac23bf8Blake Jones	int	c;
50ac23bf8Blake Jones	char	cmdpath[MAXPATHLEN];
51ac23bf8Blake Jones	char	basenm[BASENMSZ];
5235a5a35Jonathan Adams
53ac23bf8Blake Jones	(void) strlcpy(cmdpath, argv[0], MAXPATHLEN);
54ac23bf8Blake Jones	(void) strlcpy(basenm, basename(argv[0]), BASENMSZ);
5535a5a35Jonathan Adams
56ac23bf8Blake Jones	dflag = eflag = iflag = lflag = sflag = 0;
57ac23bf8Blake Jones	while ((c = getopt(argc, argv, "c:dei:ls")) != -1) {
58ac23bf8Blake Jones		switch (c) {
5935a5a35Jonathan Adams
6035a5a35Jonathan Adams		case 'c':
6135a5a35Jonathan Adams			if (strcmp(optarg, "SDC") != 0)
6235a5a35Jonathan Adams				fatalerr("error: %s executed for %s class, "
6335a5a35Jonathan Adams				    "%s is actually sub-command for %s class\n",
64ac23bf8Blake Jones				    cmdpath, optarg, cmdpath, "SDC");
65ac23bf8Blake Jones			break;
66ac23bf8Blake Jones
67ac23bf8Blake Jones		case 'd':
68ac23bf8Blake Jones			dflag++;
69ac23bf8Blake Jones			break;
70ac23bf8Blake Jones
71ac23bf8Blake Jones		case 'e':
72ac23bf8Blake Jones			eflag++;
73ac23bf8Blake Jones			break;
74ac23bf8Blake Jones
75ac23bf8Blake Jones		case 'i':
76ac23bf8Blake Jones			iflag++;	/* optarg is parsed, but ignored */
77ac23bf8Blake Jones			break;
7835a5a35Jonathan Adams
79ac23bf8Blake Jones		case 'l':
80ac23bf8Blake Jones			lflag++;
81ac23bf8Blake Jones			break;
82ac23bf8Blake Jones
83ac23bf8Blake Jones		case 's':
84ac23bf8Blake Jones			sflag++;
8535a5a35Jonathan Adams			break;
8635a5a35Jonathan Adams
8735a5a35Jonathan Adams		case '?':
8835a5a35Jonathan Adams			fatalerr(usage);
89ac23bf8Blake Jones			/*NOTREACHED*/
90ac23bf8Blake Jones
9135a5a35Jonathan Adams		default:
9235a5a35Jonathan Adams			break;
9335a5a35Jonathan Adams		}
9435a5a35Jonathan Adams	}
9535a5a35Jonathan Adams
96ac23bf8Blake Jones	if (sflag && eflag) {
97ac23bf8Blake Jones		fatalerr(usage);
98ac23bf8Blake Jones	}
99ac23bf8Blake Jones	if (sflag || eflag) {
100ac23bf8Blake Jones		fatalerr(
101ac23bf8Blake Jones		    "priocntl: \"-%c\" may not be used with the %s class\n",
102ac23bf8Blake Jones		    (sflag ? 's' : 'e'), "SDC");
103ac23bf8Blake Jones	}
104ac23bf8Blake Jones
105ac23bf8Blake Jones	if ((!dflag && !lflag) || (dflag && lflag)) {
10635a5a35Jonathan Adams		fatalerr(usage);
107ac23bf8Blake Jones	}
108ac23bf8Blake Jones
109ac23bf8Blake Jones	if (dflag) {
110ac23bf8Blake Jones		pid_t *pidlist;
111ac23bf8Blake Jones		size_t numread, i;
112ac23bf8Blake Jones
113ac23bf8Blake Jones		/*
114ac23bf8Blake Jones		 * No scheduling-class-specific information to print,
115ac23bf8Blake Jones		 * but we read the pidlist to avoid generating a SIGPIPE
116ac23bf8Blake Jones		 * in the main priocntl process.  Once we've read it,
117ac23bf8Blake Jones		 * we might as well print it.
118ac23bf8Blake Jones		 */
119ac23bf8Blake Jones		if ((pidlist = read_pidlist(&numread, stdin)) == NULL) {
120ac23bf8Blake Jones			fatalerr("%s: Can't read pidlist.\n", basenm);
121ac23bf8Blake Jones		} else if (numread == 0) {
122ac23bf8Blake Jones			fatalerr("%s: No pids on input.\n", basenm);
123ac23bf8Blake Jones		} else {
124ac23bf8Blake Jones			(void) printf("SYSTEM DUTY-CYCLE PROCESSES:\n");
125ac23bf8Blake Jones			(void) printf("%7s\n", "PID");
126ac23bf8Blake Jones			for (i = 0; i < numread; i++) {
127ac23bf8Blake Jones				(void) printf("%7ld\n", pidlist[i]);
128ac23bf8Blake Jones			}
129ac23bf8Blake Jones		}
130ac23bf8Blake Jones		free_pidlist(pidlist);
131ac23bf8Blake Jones	} else {
132ac23bf8Blake Jones		(void) printf("SDC (System Duty-Cycle Class)\n");
133ac23bf8Blake Jones	}
13435a5a35Jonathan Adams
13535a5a35Jonathan Adams	return (0);
13635a5a35Jonathan Adams}
137