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]
22 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
26 * poolbind - bind processes, tasks, and projects to pools, and query process
41 #include <sys/procset.h>
42 #include <sys/types.h>
51 #define TEXT_DOMAIN "SYS_TEST"
60 static const char OPTS
[] = "Qei:p:q";
66 { P_TASKID
, "taskid" },
67 { P_PROJID
, "projid" },
68 { P_PROJID
, "project" },
69 { P_ZONEID
, "zoneid" },
73 int error
= E_PO_SUCCESS
;
75 void exec_cmd(char *, char *[]);
76 void process_ids(char *, uint_t
, idtype_t
, char *, int, char *[]);
81 (void) fprintf(stderr
,
83 " poolbind -p pool_name -e command [arguments...]\n"
84 " poolbind -p pool_name "
85 "[-i pid | -i taskid | -i projid | -i zoneid] id ...\n"
86 " poolbind -q pid ...\n"
87 " poolbind -Q pid ... \n"));
92 print_resource_binding(const char *type
, pid_t pid
)
96 if ((resource_name
= pool_get_resource_binding(type
, pid
)) == NULL
)
97 warn(gettext("getting '%s' binding for %d: %s\n"), type
,
98 (int)pid
, get_errstr());
100 (void) printf("%d\t%s\t%s\n", (int)pid
, type
, resource_name
);
106 main(int argc
, char *argv
[])
110 idtype_t idtype
= P_PID
;
112 char *pool_name
= NULL
;
116 (void) getpname(argv
[0]);
117 (void) setlocale(LC_ALL
, "");
118 (void) textdomain(TEXT_DOMAIN
);
120 while ((c
= getopt(argc
, argv
, OPTS
)) != EOF
) {
123 if (flags
& (qFLAG
| iFLAG
| pFLAG
))
128 if (flags
& (iFLAG
| qFLAG
| QFLAG
))
133 for (i
= 0; idtypes
[i
].str
!= NULL
; i
++) {
134 if (strcmp(optarg
, idtypes
[i
].str
) == 0) {
135 idtype
= idtypes
[i
].idtype
;
136 idstr
= idtypes
[i
].str
;
140 if ((flags
& (iFLAG
| qFLAG
| QFLAG
)) ||
141 idtypes
[i
].str
== NULL
)
146 if (flags
& (pFLAG
| qFLAG
| QFLAG
))
152 if (flags
& (pFLAG
| iFLAG
| QFLAG
))
165 if (flags
& eFLAG
&& pool_name
== NULL
)
167 if (argc
< 1 || (flags
& (pFLAG
| qFLAG
| QFLAG
)) == 0)
171 * Check to see that the pools facility is enabled
173 if (pool_get_status(&status
) != PO_SUCCESS
)
174 die((ERR_OPEN_DYNAMIC
), get_errstr());
175 if (status
== POOL_DISABLED
)
176 die((ERR_OPEN_DYNAMIC
), strerror(ENOTACTIVE
));
179 exec_cmd(pool_name
, argv
);
182 process_ids(pool_name
, flags
, idtype
, idstr
, argc
, argv
);
188 exec_cmd(char *pool_name
, char *argv
[])
190 if (pool_set_binding(pool_name
, P_PID
, getpid()) != PO_SUCCESS
) {
191 warn(gettext("binding to pool '%s': %s\n"), pool_name
,
197 if (execvp(argv
[0], argv
) == -1)
198 die(gettext("exec of %s failed"), argv
[0]);
203 process_ids(char *pool_name
, uint_t flags
, idtype_t idtype
, char *idstr
,
204 int argc
, char *argv
[])
209 for (i
= 0; i
< argc
; i
++) {
214 id
= (id_t
)strtol(argv
[i
], &endp
, 10);
216 (endp
&& endp
!= argv
[i
] + strlen(argv
[i
])) ||
217 (idtype
== P_ZONEID
&&
218 getzonenamebyid(id
, NULL
, 0) == -1)) {
220 * The string does not completely parse to
221 * an integer, or it represents an invalid
226 * It must be a project or zone name.
228 if (idtype
== P_ZONEID
) {
229 if (zone_get_id(argv
[i
], &id
) != 0) {
230 warn(gettext("invalid zone '%s'\n"),
235 /* make sure the zone is booted */
237 warn(gettext("zone '%s' is not "
238 "active\n"), argv
[i
]);
242 } else if (idtype
== P_PROJID
) {
243 if ((id
= getprojidbyname(argv
[i
])) < 0) {
244 warn(gettext("failed to get project "
245 "id for project: '%s'"), argv
[i
]);
250 warn(gettext("invalid %s '%s'\n"),
258 if (pool_set_binding(pool_name
, idtype
, id
) !=
260 warn(gettext("binding %s %ld to pool '%s': "
261 "%s\n"), idstr
, id
, pool_name
,
269 if ((poolname
= pool_get_binding(id
)) == NULL
) {
270 warn(gettext("couldn't determine binding for "
271 "pid %ld: %s\n"), id
, get_errstr());
274 (void) printf("%ld\t%s\n", id
, poolname
);
280 const char **resource_types
;
281 (void) pool_resource_type_list(NULL
, &count
);
283 if ((resource_types
= malloc(count
*
284 sizeof (const char *))) == NULL
) {
285 warn(gettext("couldn't allocate query memory "
286 "for pid %ld: %s\n"), id
, get_errstr());
289 (void) pool_resource_type_list(resource_types
, &count
);
291 for (j
= 0; j
< count
; j
++)
292 (void) print_resource_binding(resource_types
[j
],
294 free(resource_types
);