@@ -66,6 +66,7 @@ struct rezygiskd_status status32 = {
6666
6767int monitor_epoll_fd ;
6868bool monitor_events_running = true;
69+ typedef void (* monitor_event_callback_t )();
6970
7071bool monitor_events_init () {
7172 monitor_epoll_fd = epoll_create (1 );
@@ -78,14 +79,9 @@ bool monitor_events_init() {
7879 return true;
7980}
8081
81- struct monitor_event_cbs {
82- void (* callback )();
83- void (* stop_callback )();
84- };
85-
86- bool monitor_events_register_event (struct monitor_event_cbs * event_cbs , int fd , uint32_t events ) {
82+ bool monitor_events_register_event (monitor_event_callback_t event_cb , int fd , uint32_t events ) {
8783 struct epoll_event ev = {
88- .data .ptr = event_cbs ,
84+ .data .ptr = ( void * ) event_cb ,
8985 .events = events
9086 };
9187
@@ -116,27 +112,23 @@ void monitor_events_loop() {
116112 struct epoll_event events [2 ];
117113 while (monitor_events_running ) {
118114 int nfds = epoll_wait (monitor_epoll_fd , events , 2 , -1 );
119- if (nfds == -1 ) {
120- if ( errno != EINTR ) PLOGE ("epoll_wait" );
115+ if (nfds == -1 && errno != EINTR ) {
116+ PLOGE ("epoll_wait" );
121117
122- continue ;
118+ monitor_events_running = false;
119+
120+ break ;
123121 }
124122
125- for (int i = 0 ; i < nfds ; i ++ ) {
126- struct monitor_event_cbs * event_cbs = (struct monitor_event_cbs * )events [i ].data .ptr ;
127- event_cbs -> callback ();
123+ for (int i = 0 ; i < nfds ; i ++ ) {
124+ ((monitor_event_callback_t )events [i ].data .ptr )();
128125
129126 if (!monitor_events_running ) break ;
130127 }
131128 }
132129
133130 if (monitor_epoll_fd >= 0 ) close (monitor_epoll_fd );
134131 monitor_epoll_fd = -1 ;
135-
136- for (int i = 0 ; i < (int )(sizeof (events ) / sizeof (events [0 ])); i ++ ) {
137- struct monitor_event_cbs * event_cbs = (struct monitor_event_cbs * )events [i ].data .ptr ;
138- event_cbs -> stop_callback ();
139- }
140132}
141133
142134int monitor_sock_fd ;
@@ -272,15 +264,13 @@ void rezygiskd_listener_callback() {
272264 status64 .daemon_info = NULL ;
273265 }
274266
275- status64 .daemon_info = ( char * ) malloc ( msg . length );
267+ status64 .daemon_info = strdup ( msg_data );
276268 if (!status64 .daemon_info ) {
277269 PLOGE ("malloc daemon64 info" );
278270
279271 break ;
280272 }
281273
282- strcpy (status64 .daemon_info , msg_data );
283-
284274 update_status (NULL );
285275
286276 break ;
@@ -293,15 +283,13 @@ void rezygiskd_listener_callback() {
293283 status32 .daemon_info = NULL ;
294284 }
295285
296- status32 .daemon_info = ( char * ) malloc ( msg . length );
286+ status32 .daemon_info = strdup ( msg_data );
297287 if (!status32 .daemon_info ) {
298288 PLOGE ("malloc daemon32 info" );
299289
300290 break ;
301291 }
302292
303- strcpy (status32 .daemon_info , msg_data );
304-
305293 update_status (NULL );
306294
307295 break ;
@@ -316,15 +304,13 @@ void rezygiskd_listener_callback() {
316304 status64 .daemon_error_info = NULL ;
317305 }
318306
319- status64 .daemon_error_info = ( char * ) malloc ( msg . length );
307+ status64 .daemon_error_info = strdup ( msg_data );
320308 if (!status64 .daemon_error_info ) {
321309 PLOGE ("malloc daemon64 error info" );
322310
323311 break ;
324312 }
325313
326- strcpy (status64 .daemon_error_info , msg_data );
327-
328314 update_status (NULL );
329315
330316 break ;
@@ -339,15 +325,13 @@ void rezygiskd_listener_callback() {
339325 status32 .daemon_error_info = NULL ;
340326 }
341327
342- status32 .daemon_error_info = ( char * ) malloc ( msg . length );
328+ status32 .daemon_error_info = strdup ( msg_data );
343329 if (!status32 .daemon_error_info ) {
344330 PLOGE ("malloc daemon32 error info" );
345331
346332 break ;
347333 }
348334
349- strcpy (status32 .daemon_error_info , msg_data );
350-
351335 update_status (NULL );
352336
353337 break ;
@@ -847,10 +831,6 @@ void init_monitor() {
847831
848832 monitor_events_init ();
849833
850- struct monitor_event_cbs listener_cbs = {
851- .callback = rezygiskd_listener_callback ,
852- .stop_callback = rezygiskd_listener_stop
853- };
854834 if (!rezygiskd_listener_init ()) {
855835 LOGE ("failed to create socket" );
856836
@@ -859,12 +839,8 @@ void init_monitor() {
859839 exit (1 );
860840 }
861841
862- monitor_events_register_event (& listener_cbs , monitor_sock_fd , EPOLLIN | EPOLLET );
842+ monitor_events_register_event (rezygiskd_listener_callback , monitor_sock_fd , EPOLLIN | EPOLLET );
863843
864- struct monitor_event_cbs sigchld_cbs = {
865- .callback = sigchld_listener_callback ,
866- .stop_callback = sigchld_listener_stop
867- };
868844 if (sigchld_listener_init () == false) {
869845 LOGE ("failed to create signalfd" );
870846
@@ -874,10 +850,15 @@ void init_monitor() {
874850 exit (1 );
875851 }
876852
877- monitor_events_register_event (& sigchld_cbs , sigchld_signal_fd , EPOLLIN | EPOLLET );
853+ monitor_events_register_event (sigchld_listener_callback , sigchld_signal_fd , EPOLLIN | EPOLLET );
878854
879855 monitor_events_loop ();
880856
857+ /* INFO: Once it stops the loop, we cannot access the epool data, so we
858+ either manually call the stops or save to a structure. */
859+ rezygiskd_listener_stop ();
860+ sigchld_listener_stop ();
861+
881862 if (status64 .daemon_info ) free (status64 .daemon_info );
882863 if (status64 .daemon_error_info ) free (status64 .daemon_error_info );
883864 if (status32 .daemon_info ) free (status32 .daemon_info );
0 commit comments