1+ use super :: SuInfo ;
2+ use super :: db:: RootSettings ;
13use crate :: consts:: { INTERNAL_DIR , MAGISK_FILE_CON } ;
2- use crate :: daemon:: { MagiskD , to_user_id} ;
3- use crate :: ffi:: { SuAppRequest , SuPolicy , get_magisk_tmp} ;
4+ use crate :: daemon:: to_user_id;
5+ use crate :: ffi:: { SuPolicy , SuRequest , get_magisk_tmp} ;
6+ use crate :: socket:: IpcRead ;
47use ExtraVal :: { Bool , Int , IntList , Str } ;
58use base:: {
6- BytesExt , FileAttr , LibcReturn , LoggedResult , OsError , ResultExt , cstr, fork_dont_care, info,
7- libc,
9+ BytesExt , FileAttr , LibcReturn , LoggedResult , OsError , ResultExt , cstr, fork_dont_care, libc,
810} ;
911use libc:: pollfd as PollFd ;
1012use num_traits:: AsPrimitive ;
@@ -79,19 +81,21 @@ impl Extra {
7981 }
8082}
8183
82- impl MagiskD {
83- fn exec_cmd (
84- & self ,
85- action : & ' static str ,
86- extras : & [ Extra ] ,
87- info : & SuAppRequest ,
88- use_provider : bool ,
89- ) {
90- let user = to_user_id ( info. eval_uid ) ;
84+ pub ( super ) struct SuAppContext < ' a > {
85+ pub ( super ) cred : libc:: ucred ,
86+ pub ( super ) request : & ' a SuRequest ,
87+ pub ( super ) info : & ' a SuInfo ,
88+ pub ( super ) settings : & ' a mut RootSettings ,
89+ pub ( super ) sdk_int : i32 ,
90+ }
91+
92+ impl SuAppContext < ' _ > {
93+ fn exec_cmd ( & self , action : & ' static str , extras : & [ Extra ] , use_provider : bool ) {
94+ let user = to_user_id ( self . info . eval_uid ) ;
9195 let user = user. to_string ( ) ;
9296
9397 if use_provider {
94- let provider = format ! ( "content://{}.provider" , info. mgr_pkg) ;
98+ let provider = format ! ( "content://{}.provider" , self . info. mgr_pkg) ;
9599 let mut cmd = Command :: new ( "/system/bin/app_process" ) ;
96100 cmd. args ( [
97101 "/system/bin" ,
@@ -104,7 +108,7 @@ impl MagiskD {
104108 "--method" ,
105109 action,
106110 ] ) ;
107- if self . sdk_int ( ) >= 30 {
111+ if self . sdk_int >= 30 {
108112 extras. iter ( ) . for_each ( |e| e. add_bind ( & mut cmd) )
109113 } else {
110114 extras. iter ( ) . for_each ( |e| e. add_bind_legacy ( & mut cmd) )
@@ -126,7 +130,7 @@ impl MagiskD {
126130 "com.android.commands.am.Am" ,
127131 "start" ,
128132 "-p" ,
129- info. mgr_pkg ,
133+ & self . info . mgr_pkg ,
130134 "--user" ,
131135 & user,
132136 "-a" ,
@@ -152,21 +156,21 @@ impl MagiskD {
152156 }
153157 }
154158
155- pub fn app_request ( & self , info : & SuAppRequest ) -> LoggedResult < File > {
159+ fn app_request ( & mut self ) {
156160 let mut fifo = cstr:: buf:: new :: < 64 > ( ) ;
157161 fifo. write_fmt ( format_args ! (
158162 "{}/{}/su_request_{}" ,
159163 get_magisk_tmp( ) ,
160164 INTERNAL_DIR ,
161- info . pid
165+ self . cred . pid
162166 ) )
163167 . ok ( ) ;
164168
165169 let fd: LoggedResult < File > = try {
166170 let mut attr = FileAttr :: new ( ) ;
167171 attr. st . st_mode = 0o600 ;
168- attr. st . st_uid = info. mgr_uid . as_ ( ) ;
169- attr. st . st_gid = info. mgr_uid . as_ ( ) ;
172+ attr. st . st_uid = self . info . mgr_uid . as_ ( ) ;
173+ attr. st . st_gid = self . info . mgr_uid . as_ ( ) ;
170174 attr. con . write_str ( MAGISK_FILE_CON ) . ok ( ) ;
171175
172176 fifo. mkfifo ( 0o600 ) ?;
@@ -179,14 +183,14 @@ impl MagiskD {
179183 } ,
180184 Extra {
181185 key : "uid" ,
182- value : Int ( info. eval_uid ) ,
186+ value : Int ( self . info . eval_uid ) ,
183187 } ,
184188 Extra {
185189 key : "pid" ,
186- value : Int ( info . pid ) ,
190+ value : Int ( self . cred . pid ) ,
187191 } ,
188192 ] ;
189- self . exec_cmd ( "request" , & extras, info , false ) ;
193+ self . exec_cmd ( "request" , & extras, false ) ;
190194
191195 // Open with O_RDWR to prevent FIFO open block
192196 let fd = fifo. open ( libc:: O_RDWR | libc:: O_CLOEXEC ) ?;
@@ -206,79 +210,106 @@ impl MagiskD {
206210 } ;
207211
208212 fifo. remove ( ) . log_ok ( ) ;
209- fd
213+
214+ if let Ok ( mut fd) = fd {
215+ self . settings . policy = SuPolicy {
216+ repr : fd
217+ . read_decodable :: < i32 > ( )
218+ . log ( )
219+ . map ( i32:: from_be)
220+ . unwrap_or ( SuPolicy :: Deny . repr ) ,
221+ } ;
222+ } else {
223+ self . settings . policy = SuPolicy :: Deny ;
224+ } ;
210225 }
211226
212- pub fn app_notify ( & self , info : & SuAppRequest , policy : SuPolicy ) {
213- if fork_dont_care ( ) != 0 {
214- return ;
215- }
227+ fn app_notify ( & self ) {
216228 let extras = [
217229 Extra {
218230 key : "from.uid" ,
219- value : Int ( info . uid ) ,
231+ value : Int ( self . cred . uid . as_ ( ) ) ,
220232 } ,
221233 Extra {
222234 key : "pid" ,
223- value : Int ( info . pid ) ,
235+ value : Int ( self . cred . pid . as_ ( ) ) ,
224236 } ,
225237 Extra {
226238 key : "policy" ,
227- value : Int ( policy. repr ) ,
239+ value : Int ( self . settings . policy . repr ) ,
228240 } ,
229241 ] ;
230- self . exec_cmd ( "notify" , & extras, info, true ) ;
231- exit ( 0 ) ;
242+ self . exec_cmd ( "notify" , & extras, true ) ;
232243 }
233244
234- pub fn app_log ( & self , info : & SuAppRequest , policy : SuPolicy , notify : bool ) {
235- if fork_dont_care ( ) != 0 {
236- return ;
237- }
238- let command = if info. request . command . is_empty ( ) {
239- & info. request . shell
245+ fn app_log ( & self ) {
246+ let command = if self . request . command . is_empty ( ) {
247+ & self . request . shell
240248 } else {
241- & info . request . command
249+ & self . request . command
242250 } ;
243251 let extras = [
244252 Extra {
245253 key : "from.uid" ,
246- value : Int ( info . uid ) ,
254+ value : Int ( self . cred . uid . as_ ( ) ) ,
247255 } ,
248256 Extra {
249257 key : "to.uid" ,
250- value : Int ( info . request . target_uid ) ,
258+ value : Int ( self . request . target_uid ) ,
251259 } ,
252260 Extra {
253261 key : "pid" ,
254- value : Int ( info . pid ) ,
262+ value : Int ( self . cred . pid . as_ ( ) ) ,
255263 } ,
256264 Extra {
257265 key : "policy" ,
258- value : Int ( policy. repr ) ,
266+ value : Int ( self . settings . policy . repr ) ,
259267 } ,
260268 Extra {
261269 key : "target" ,
262- value : Int ( info . request . target_pid ) ,
270+ value : Int ( self . request . target_pid ) ,
263271 } ,
264272 Extra {
265273 key : "context" ,
266- value : Str ( info . request . context . clone ( ) ) ,
274+ value : Str ( self . request . context . clone ( ) ) ,
267275 } ,
268276 Extra {
269277 key : "gids" ,
270- value : IntList ( info . request . gids . clone ( ) ) ,
278+ value : IntList ( self . request . gids . clone ( ) ) ,
271279 } ,
272280 Extra {
273281 key : "command" ,
274282 value : Str ( command. clone ( ) ) ,
275283 } ,
276284 Extra {
277285 key : "notify" ,
278- value : Bool ( notify) ,
286+ value : Bool ( self . settings . notify ) ,
279287 } ,
280288 ] ;
281- self . exec_cmd ( "log" , & extras, info, true ) ;
289+ self . exec_cmd ( "log" , & extras, true ) ;
290+ }
291+
292+ pub ( super ) fn connect_app ( & mut self ) {
293+ // If policy is undetermined, show dialog for user consent
294+ if self . settings . policy == SuPolicy :: Query {
295+ self . app_request ( ) ;
296+ }
297+
298+ if !self . settings . log && !self . settings . notify {
299+ return ;
300+ }
301+
302+ if fork_dont_care ( ) != 0 {
303+ return ;
304+ }
305+
306+ // Notify su usage to application
307+ if self . settings . log {
308+ self . app_log ( ) ;
309+ } else if self . settings . notify {
310+ self . app_notify ( ) ;
311+ }
312+
282313 exit ( 0 ) ;
283314 }
284315}
0 commit comments