1 # include <kernel/kernel.h>
2 # include <kernel/rsrc.h>
3 # include <type.h>
4 
5 private object rsrcd;		/* resource manager */
6 
7 /*
8  * NAME:	create()
9  * DESCRIPTION:	initialize API
10  */
create()11 static void create()
12 {
13     rsrcd = find_object(RSRCD);
14 }
15 
16 /*
17  * NAME:	add_owner()
18  * DESCRIPTION:	add a new resource owner
19  */
add_owner(string owner)20 static void add_owner(string owner)
21 {
22     rsrcd->add_owner(owner);
23 }
24 
25 /*
26  * NAME:	remove_owner()
27  * DESCRIPTION:	remove a resource owner
28  */
remove_owner(string owner)29 static void remove_owner(string owner)
30 {
31     rsrcd->remove_owner(owner);
32 }
33 
34 /*
35  * NAME:	query_owners()
36  * DESCRIPTION:	return a list of resource owners
37  */
query_owners()38 static string *query_owners()
39 {
40     return rsrcd->query_owners();
41 }
42 
43 
44 /*
45  * NAME:	set_rsrc()
46  * DESCRIPTION:	set the maximum, decay percentage and decay period of a
47  *		resource
48  */
set_rsrc(string name,int max,int decay,int period)49 static void set_rsrc(string name, int max, int decay, int period)
50 {
51     if (!name || max < -1 || decay < 0 || decay > 100 || period < 0 ||
52 	!decay != !period) {
53 	error("Bad arguments for set_rsrc");
54     }
55     rsrcd->set_rsrc(name, max, decay, period);
56 }
57 
58 /*
59  * NAME:	remove_rsrc()
60  * DESCRIPTION:	remove a resource
61  */
remove_rsrc(string name)62 static void remove_rsrc(string name)
63 {
64     if (!name) {
65 	error("Bad argument for remove_rsrc");
66     }
67     rsrcd->remove_rsrc(name);
68 }
69 
70 /*
71  * NAME:	query_rsrc()
72  * DESCRIPTION:	get usage and limits of a resource
73  */
query_rsrc(string name)74 static mixed *query_rsrc(string name)
75 {
76     if (!name) {
77 	error("Bad argument for query_rsrc");
78     }
79     return rsrcd->query_rsrc(name);
80 }
81 
82 /*
83  * NAME:	query_resources()
84  * DESCRIPTION:	return a list of resources
85  */
query_resources()86 static string *query_resources()
87 {
88     return rsrcd->query_resources();
89 }
90 
91 
92 /*
93  * NAME:	rsrc_set_limit()
94  * DESCRIPTION:	set individual resource limit
95  */
rsrc_set_limit(string owner,string name,int max)96 static void rsrc_set_limit(string owner, string name, int max)
97 {
98     if (!name || max < -1) {
99 	error("Bad arguments for rsrc_set_limit");
100     }
101     rsrcd->rsrc_set_limit(owner, name, max);
102 }
103 
104 /*
105  * NAME:	rsrc_get()
106  * DESCRIPTION:	get individual resource usage
107  */
rsrc_get(string owner,string name)108 static mixed *rsrc_get(string owner, string name)
109 {
110     if (!name) {
111 	error("Bad arguments for rsrc_get");
112     }
113     return rsrcd->rsrc_get(owner, name);
114 }
115 
116 /*
117  * NAME:	rsrc_incr()
118  * DESCRIPTION:	increment or decrement a resource, returning 1 if succeeded,
119  *		0 if failed
120  */
rsrc_incr(string owner,string name,mixed index,int incr,varargs int force)121 static int rsrc_incr(string owner, string name, mixed index, int incr,
122 		     varargs int force)
123 {
124     if (!name || (typeof(index) == T_OBJECT &&
125 		  sscanf(object_name(index), "%*s#-1") != 0)) {
126 	error("Bad arguments for rsrc_incr");
127     }
128     return rsrcd->rsrc_incr(owner, name, index, incr, force);
129 }
130