17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
29*7257d1b4Sraf #include "lint.h"
307c478bd9Sstevel@tonic-gate #include <sys/rctl_impl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <rctl.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * Resource control routines
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * rctl_walk(3C)
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * Resource control block manipulation routines
417c478bd9Sstevel@tonic-gate * The setrctl(2) and getrctl(2) interfaces are accessed via opaque resource
427c478bd9Sstevel@tonic-gate * control blocks, the characteristics of which are in turn set and fetched
437c478bd9Sstevel@tonic-gate * using the following functions. Applications using the following interfaces
447c478bd9Sstevel@tonic-gate * will be binary compatible across enhancements to the resource control
457c478bd9Sstevel@tonic-gate * subsystem that involve modification of the control block.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate int
rctl_walk(int (* callback)(const char * rctlname,void * walk_data),void * init_data)487c478bd9Sstevel@tonic-gate rctl_walk(int (*callback)(const char *rctlname, void *walk_data),
497c478bd9Sstevel@tonic-gate void *init_data)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate int ret = 0;
527c478bd9Sstevel@tonic-gate char *ctl_names, *curr_name;
537c478bd9Sstevel@tonic-gate size_t sz = rctllist(NULL, 0);
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate if ((ctl_names = malloc(sz)) == NULL)
567c478bd9Sstevel@tonic-gate return (-1);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate (void) rctllist(ctl_names, sz);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate for (curr_name = ctl_names;
617c478bd9Sstevel@tonic-gate curr_name < ctl_names + sz;
627c478bd9Sstevel@tonic-gate curr_name += strlen(curr_name) + 1) {
637c478bd9Sstevel@tonic-gate if (ret = callback(curr_name, init_data)) {
647c478bd9Sstevel@tonic-gate free(ctl_names);
657c478bd9Sstevel@tonic-gate return (ret);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate free(ctl_names);
707c478bd9Sstevel@tonic-gate return (ret);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate uint_t
rctlblk_get_global_action(rctlblk_t * rblk)747c478bd9Sstevel@tonic-gate rctlblk_get_global_action(rctlblk_t *rblk)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate return (ropaque->rcq_global_flagaction & (~RCTL_GLOBAL_ACTION_MASK));
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate uint_t
rctlblk_get_local_action(rctlblk_t * rblk,int * signal)827c478bd9Sstevel@tonic-gate rctlblk_get_local_action(rctlblk_t *rblk, int *signal)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if (signal != NULL)
877c478bd9Sstevel@tonic-gate *signal = ropaque->rcq_local_signal;
887c478bd9Sstevel@tonic-gate return (ropaque->rcq_local_flagaction & (~RCTL_LOCAL_ACTION_MASK));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate uint_t
rctlblk_get_global_flags(rctlblk_t * rblk)927c478bd9Sstevel@tonic-gate rctlblk_get_global_flags(rctlblk_t *rblk)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate return (ropaque->rcq_global_flagaction & RCTL_GLOBAL_ACTION_MASK);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate uint_t
rctlblk_get_local_flags(rctlblk_t * rblk)1007c478bd9Sstevel@tonic-gate rctlblk_get_local_flags(rctlblk_t *rblk)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate return (ropaque->rcq_local_flagaction & RCTL_LOCAL_ACTION_MASK);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate hrtime_t
rctlblk_get_firing_time(rctlblk_t * rblk)1087c478bd9Sstevel@tonic-gate rctlblk_get_firing_time(rctlblk_t *rblk)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate return (ropaque->rcq_firing_time);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate id_t
rctlblk_get_recipient_pid(rctlblk_t * rblk)1167c478bd9Sstevel@tonic-gate rctlblk_get_recipient_pid(rctlblk_t *rblk)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate return (ropaque->rcq_local_recipient_pid);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate rctl_priv_t
rctlblk_get_privilege(rctlblk_t * rblk)1247c478bd9Sstevel@tonic-gate rctlblk_get_privilege(rctlblk_t *rblk)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1277c478bd9Sstevel@tonic-gate return (ropaque->rcq_privilege);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate rctl_qty_t
rctlblk_get_value(rctlblk_t * rblk)1317c478bd9Sstevel@tonic-gate rctlblk_get_value(rctlblk_t *rblk)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1347c478bd9Sstevel@tonic-gate return (ropaque->rcq_value);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate rctl_qty_t
rctlblk_get_enforced_value(rctlblk_t * rblk)1387c478bd9Sstevel@tonic-gate rctlblk_get_enforced_value(rctlblk_t *rblk)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1417c478bd9Sstevel@tonic-gate return (ropaque->rcq_enforced_value);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate void
rctlblk_set_local_action(rctlblk_t * rblk,uint_t action,int signal)1457c478bd9Sstevel@tonic-gate rctlblk_set_local_action(rctlblk_t *rblk, uint_t action, int signal)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1487c478bd9Sstevel@tonic-gate ropaque->rcq_local_signal = signal;
1497c478bd9Sstevel@tonic-gate ropaque->rcq_local_flagaction = (ropaque->rcq_local_flagaction &
1507c478bd9Sstevel@tonic-gate RCTL_LOCAL_ACTION_MASK) | (action & ~RCTL_LOCAL_ACTION_MASK);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate void
rctlblk_set_local_flags(rctlblk_t * rblk,uint_t flags)1547c478bd9Sstevel@tonic-gate rctlblk_set_local_flags(rctlblk_t *rblk, uint_t flags)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1577c478bd9Sstevel@tonic-gate ropaque->rcq_local_flagaction = (ropaque->rcq_local_flagaction &
1587c478bd9Sstevel@tonic-gate ~RCTL_LOCAL_ACTION_MASK) | (flags & RCTL_LOCAL_ACTION_MASK);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate void
rctlblk_set_recipient_pid(rctlblk_t * rblk,id_t pid)1627c478bd9Sstevel@tonic-gate rctlblk_set_recipient_pid(rctlblk_t *rblk, id_t pid)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1657c478bd9Sstevel@tonic-gate ropaque->rcq_local_recipient_pid = pid;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate void
rctlblk_set_privilege(rctlblk_t * rblk,rctl_priv_t privilege)1697c478bd9Sstevel@tonic-gate rctlblk_set_privilege(rctlblk_t *rblk, rctl_priv_t privilege)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1727c478bd9Sstevel@tonic-gate ropaque->rcq_privilege = privilege;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate void
rctlblk_set_value(rctlblk_t * rblk,rctl_qty_t value)1767c478bd9Sstevel@tonic-gate rctlblk_set_value(rctlblk_t *rblk, rctl_qty_t value)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate rctl_opaque_t *ropaque = (rctl_opaque_t *)rblk;
1797c478bd9Sstevel@tonic-gate ropaque->rcq_value = value;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate size_t
rctlblk_size(void)1837c478bd9Sstevel@tonic-gate rctlblk_size(void)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate return (sizeof (rctl_opaque_t));
1867c478bd9Sstevel@tonic-gate }
187