@@ -108,31 +108,95 @@ impl Controllers {
108
108
}
109
109
}
110
110
111
+ mod sealed {
112
+ use super :: * ;
113
+
114
+ pub trait ControllerInternal {
115
+ fn apply ( & self , res : & Resources ) -> Result < ( ) > ;
116
+
117
+ // meta stuff
118
+ fn control_type ( & self ) -> Controllers ;
119
+ fn get_path ( & self ) -> & PathBuf ;
120
+ fn get_path_mut ( & mut self ) -> & mut PathBuf ;
121
+ fn get_base ( & self ) -> & PathBuf ;
122
+
123
+ fn verify_path ( & self ) -> Result < ( ) > {
124
+ if self . get_path ( ) . starts_with ( self . get_base ( ) ) {
125
+ Ok ( ( ) )
126
+ } else {
127
+ Err ( Error :: new ( ErrorKind :: InvalidPath ) )
128
+ }
129
+ }
130
+
131
+ fn open_path ( & self , p : & str , w : bool ) -> Result < File > {
132
+ let mut path = self . get_path ( ) . clone ( ) ;
133
+ path. push ( p) ;
134
+
135
+ self . verify_path ( ) ?;
136
+
137
+ if w {
138
+ match File :: create ( & path) {
139
+ Err ( e) => return Err ( Error :: with_cause ( ErrorKind :: WriteFailed , e) ) ,
140
+ Ok ( file) => return Ok ( file) ,
141
+ }
142
+ } else {
143
+ match File :: open ( & path) {
144
+ Err ( e) => return Err ( Error :: with_cause ( ErrorKind :: ReadFailed , e) ) ,
145
+ Ok ( file) => return Ok ( file) ,
146
+ }
147
+ }
148
+ }
149
+
150
+ #[ doc( hidden) ]
151
+ fn path_exists ( & self , p : & str ) -> bool {
152
+ if let Err ( _) = self . verify_path ( ) {
153
+ return false ;
154
+ }
155
+
156
+ std:: path:: Path :: new ( p) . exists ( )
157
+ }
158
+
159
+ }
160
+ }
161
+
162
+ pub ( crate ) use sealed:: ControllerInternal ;
163
+
111
164
/// A Controller is a subsystem attached to the control group.
112
165
///
113
166
/// Implementors are able to control certain aspects of a control group.
114
167
pub trait Controller {
168
+ #[ doc( hidden) ]
169
+ fn control_type ( & self ) -> Controllers ;
170
+
115
171
/// Apply a set of resources to the Controller, invoking its internal functions to pass the
116
172
/// kernel the information.
117
173
fn apply ( & self , res : & Resources ) -> Result < ( ) > ;
118
174
119
- // meta stuff
120
- #[ doc( hidden) ]
121
- fn control_type ( & self ) -> Controllers ;
122
- #[ doc( hidden) ]
123
- fn get_path ( & self ) -> & PathBuf ;
124
- #[ doc( hidden) ]
125
- fn get_path_mut ( & mut self ) -> & mut PathBuf ;
126
- #[ doc( hidden) ]
127
- fn get_base ( & self ) -> & PathBuf ;
175
+ /// Create this controller
176
+ fn create ( & self ) ;
128
177
129
- #[ doc( hidden) ]
130
- fn verify_path ( & self ) -> Result < ( ) > {
131
- if self . get_path ( ) . starts_with ( self . get_base ( ) ) {
132
- Ok ( ( ) )
133
- } else {
134
- Err ( Error :: new ( ErrorKind :: InvalidPath ) )
135
- }
178
+ /// Does this controller already exist?
179
+ fn exists ( & self ) -> bool ;
180
+
181
+ /// Delete the controller.
182
+ fn delete ( & self ) ;
183
+
184
+ /// Attach a task to this controller.
185
+ fn add_task ( & self , pid : & CgroupPid ) -> Result < ( ) > ;
186
+
187
+ /// Get the list of tasks that this controller has.
188
+ fn tasks ( & self ) -> Vec < CgroupPid > ;
189
+ }
190
+
191
+ impl < T > Controller for T where T : ControllerInternal {
192
+ fn control_type ( & self ) -> Controllers {
193
+ ControllerInternal :: control_type ( self )
194
+ }
195
+
196
+ /// Apply a set of resources to the Controller, invoking its internal functions to pass the
197
+ /// kernel the information.
198
+ fn apply ( & self , res : & Resources ) -> Result < ( ) > {
199
+ ControllerInternal :: apply ( self , res)
136
200
}
137
201
138
202
/// Create this controller
@@ -157,35 +221,6 @@ pub trait Controller {
157
221
}
158
222
}
159
223
160
- #[ doc( hidden) ]
161
- fn open_path ( & self , p : & str , w : bool ) -> Result < File > {
162
- let mut path = self . get_path ( ) . clone ( ) ;
163
- path. push ( p) ;
164
-
165
- self . verify_path ( ) ?;
166
-
167
- if w {
168
- match File :: create ( & path) {
169
- Err ( e) => return Err ( Error :: with_cause ( ErrorKind :: WriteFailed , e) ) ,
170
- Ok ( file) => return Ok ( file) ,
171
- }
172
- } else {
173
- match File :: open ( & path) {
174
- Err ( e) => return Err ( Error :: with_cause ( ErrorKind :: ReadFailed , e) ) ,
175
- Ok ( file) => return Ok ( file) ,
176
- }
177
- }
178
- }
179
-
180
- #[ doc( hidden) ]
181
- fn path_exists ( & self , p : & str ) -> bool {
182
- if let Err ( _) = self . verify_path ( ) {
183
- return false ;
184
- }
185
-
186
- std:: path:: Path :: new ( p) . exists ( )
187
- }
188
-
189
224
/// Attach a task to this controller.
190
225
fn add_task ( & self , pid : & CgroupPid ) -> Result < ( ) > {
191
226
self . open_path ( "tasks" , true ) . and_then ( |mut file| {
0 commit comments