@@ -124,11 +124,20 @@ impl Task {
124
124
}
125
125
}
126
126
127
+ /// Returns a raw pointer to the underlying C task struct.
128
+ ///
129
+ /// # Safety
130
+ ///
131
+ /// Callers must ensure that the returned pointer doesn't outlive the current task/thread.
132
+ pub unsafe fn as_raw ( & self ) -> * mut bindings:: task_struct {
133
+ self . 0 . get ( )
134
+ }
135
+
127
136
/// Returns the group leader of the given task.
128
137
pub fn group_leader ( & self ) -> & Task {
129
- // SAFETY: By the type invariant, we know that `self.0 ` is a valid task. Valid tasks always
138
+ // SAFETY: By the type invariant, we know that `self.as_raw() ` is a valid task. Valid tasks always
130
139
// have a valid group_leader.
131
- let ptr = unsafe { * ptr:: addr_of!( ( * self . 0 . get ( ) ) . group_leader) } ;
140
+ let ptr = unsafe { * ptr:: addr_of!( ( * self . as_raw ( ) ) . group_leader) } ;
132
141
133
142
// SAFETY: The lifetime of the returned task reference is tied to the lifetime of `self`,
134
143
// and given that a task has a reference to its group leader, we know it must be valid for
@@ -138,43 +147,43 @@ impl Task {
138
147
139
148
/// Returns the PID of the given task.
140
149
pub fn pid ( & self ) -> Pid {
141
- // SAFETY: By the type invariant, we know that `self.0 ` is a valid task. Valid tasks always
150
+ // SAFETY: By the type invariant, we know that `self.as_raw() ` is a valid task. Valid tasks always
142
151
// have a valid pid.
143
- unsafe { * ptr:: addr_of!( ( * self . 0 . get ( ) ) . pid) }
152
+ unsafe { * ptr:: addr_of!( ( * self . as_raw ( ) ) . pid) }
144
153
}
145
154
146
155
/// Returns the UID of the given task.
147
156
pub fn uid ( & self ) -> Kuid {
148
- // SAFETY: By the type invariant, we know that `self.0 ` is valid.
149
- Kuid :: from_raw ( unsafe { bindings:: task_uid ( self . 0 . get ( ) ) } )
157
+ // SAFETY: By the type invariant, we know that `self.as_raw() ` is valid.
158
+ Kuid :: from_raw ( unsafe { bindings:: task_uid ( self . as_raw ( ) ) } )
150
159
}
151
160
152
161
/// Returns the effective UID of the given task.
153
162
pub fn euid ( & self ) -> Kuid {
154
- // SAFETY: By the type invariant, we know that `self.0 ` is valid.
155
- Kuid :: from_raw ( unsafe { bindings:: task_euid ( self . 0 . get ( ) ) } )
163
+ // SAFETY: By the type invariant, we know that `self.as_raw() ` is valid.
164
+ Kuid :: from_raw ( unsafe { bindings:: task_euid ( self . as_raw ( ) ) } )
156
165
}
157
166
158
167
/// Determines whether the given task has pending signals.
159
168
pub fn signal_pending ( & self ) -> bool {
160
- // SAFETY: By the type invariant, we know that `self.0 ` is valid.
161
- unsafe { bindings:: signal_pending ( self . 0 . get ( ) ) != 0 }
169
+ // SAFETY: By the type invariant, we know that `self.as_raw() ` is valid.
170
+ unsafe { bindings:: signal_pending ( self . as_raw ( ) ) != 0 }
162
171
}
163
172
164
173
/// Returns the given task's pid in the current pid namespace.
165
174
pub fn pid_in_current_ns ( & self ) -> Pid {
166
175
// SAFETY: Calling `task_active_pid_ns` with the current task is always safe.
167
176
let namespace = unsafe { bindings:: task_active_pid_ns ( bindings:: get_current ( ) ) } ;
168
- // SAFETY: We know that `self.0.get ()` is valid by the type invariant.
169
- unsafe { bindings:: task_tgid_nr_ns ( self . 0 . get ( ) , namespace) }
177
+ // SAFETY: We know that `self.raw ()` is valid by the type invariant.
178
+ unsafe { bindings:: task_tgid_nr_ns ( self . as_raw ( ) , namespace) }
170
179
}
171
180
172
181
/// Wakes up the task.
173
182
pub fn wake_up ( & self ) {
174
- // SAFETY: By the type invariant, we know that `self.0.get ()` is non-null and valid.
183
+ // SAFETY: By the type invariant, we know that `self.raw ()` is non-null and valid.
175
184
// And `wake_up_process` is safe to be called for any valid task, even if the task is
176
185
// running.
177
- unsafe { bindings:: wake_up_process ( self . 0 . get ( ) ) } ;
186
+ unsafe { bindings:: wake_up_process ( self . as_raw ( ) ) } ;
178
187
}
179
188
}
180
189
0 commit comments