11#define _GNU_SOURCE
2-
32#include <errno.h>
43#include <stdio.h>
54#include <stdlib.h>
5+ #include <string.h>
66#include <sys/stat.h>
77#include <sys/sysmacros.h>
88#include <sys/types.h>
99#include <sys/wait.h>
1010#include <unistd.h>
11+ #include <fcntl.h>
12+ #include <sys/prctl.h>
1113
1214pid_t child_proc = -1 ;
1315
1416static void intHan (int signum ) {
1517 if (child_proc != -1 ) {
1618 kill (child_proc , SIGKILL );
19+ waitpid (child_proc , NULL , 0 ); // Clean up zombie process
20+ }
21+ exit (130 ); // Standard exit code for SIGINT
22+ }
23+
24+ static int setup_chroot (const char * rootfs_path ) {
25+ if (chdir (rootfs_path ) != 0 ) {
26+ perror ("chdir" );
27+ return -1 ;
28+ }
29+ if (chroot ("./" ) != 0 ) {
30+ perror ("chroot" );
31+ return -1 ;
32+ }
33+ return 0 ;
34+ }
35+
36+ static int setup_devices () {
37+ // Create /dev if it doesn't exist
38+ mkdir ("/dev" , 0755 );
39+
40+ if (mknod ("/dev/urandom" , S_IFCHR | 0666 , makedev (1 , 9 )) != 0 ) {
41+ if (errno != EEXIST ) {
42+ perror ("mknod urandom" );
43+ return -1 ;
44+ }
45+ }
46+ return 0 ;
47+ }
48+
49+ static int setup_permissions () {
50+ const char * files [] = {
51+ "/system/bin/linker64" ,
52+ "/system/bin/main"
53+ };
54+
55+ for (size_t i = 0 ; i < sizeof (files )/sizeof (files [0 ]); i ++ ) {
56+ if (chmod (files [i ], 0755 ) != 0 ) {
57+ fprintf (stderr , "chmod failed for %s: %s\n" , files [i ], strerror (errno ));
58+ return -1 ;
59+ }
60+ }
61+ return 0 ;
62+ }
63+
64+ static int create_directories () {
65+ const char * dirs [] = {
66+ "/data/data/com.apple.android.music/files" ,
67+ "/data/data/com.apple.android.music/files/mpl_db"
68+ };
69+
70+ for (size_t i = 0 ; i < sizeof (dirs )/sizeof (dirs [0 ]); i ++ ) {
71+ if (mkdir (dirs [i ], 0777 ) != 0 && errno != EEXIST ) {
72+ fprintf (stderr , "mkdir failed for %s: %s\n" , dirs [i ], strerror (errno ));
73+ return -1 ;
74+ }
1775 }
76+ return 0 ;
1877}
1978
2079int main (int argc , char * argv [], char * envp []) {
21- if (signal (SIGINT , intHan ) == SIG_ERR ) {
22- perror ("signal" );
80+ // Set up signal handler
81+ struct sigaction sa = {
82+ .sa_handler = intHan ,
83+ .sa_flags = SA_RESETHAND ,
84+ };
85+ sigemptyset (& sa .sa_mask );
86+ if (sigaction (SIGINT , & sa , NULL ) == -1 ) {
87+ perror ("sigaction" );
2388 return 1 ;
2489 }
2590
26- if ( chdir ( "./rootfs" ) != 0 ) {
27- perror ( "chdir" );
91+ // Setup chroot environment
92+ if ( setup_chroot ( "./rootfs" ) != 0 ) {
2893 return 1 ;
2994 }
30- if (chroot ("./" ) != 0 ) {
31- perror ("chroot" );
95+
96+ // Setup devices and permissions
97+ if (setup_devices () != 0 || setup_permissions () != 0 ) {
3298 return 1 ;
3399 }
34- mknod ("/dev/urandom" , S_IFCHR | 0666 , makedev (0x1 , 0x9 ));
35- chmod ("/system/bin/linker64" , 0755 );
36- chmod ("/system/bin/main" , 0755 );
37100
38101 child_proc = fork ();
39102 if (child_proc == -1 ) {
@@ -42,14 +105,26 @@ int main(int argc, char *argv[], char *envp[]) {
42105 }
43106
44107 if (child_proc > 0 ) {
45- close (STDOUT_FILENO );
46- waitpid (child_proc , NULL , 0 );
47- return 0 ;
108+ // Parent process
109+ int status ;
110+ waitpid (child_proc , & status , 0 );
111+
112+ if (WIFEXITED (status )) {
113+ return WEXITSTATUS (status );
114+ }
115+ return 1 ;
116+ }
117+
118+ // Child process
119+ // Set process name for easier debugging
120+ prctl (PR_SET_NAME , "android-main" , 0 , 0 , 0 );
121+
122+ // Create required directories
123+ if (create_directories () != 0 ) {
124+ exit (1 );
48125 }
49126
50- // Child process logic
51- mkdir ("/data/data/com.apple.android.music/files" , 0777 );
52- mkdir ("/data/data/com.apple.android.music/files/mpl_db" , 0777 );
127+ // Execute the main binary
53128 execve ("/system/bin/main" , argv , envp );
54129 perror ("execve" );
55130 return 1 ;
0 commit comments