I implemented syscall macro to invoke software interrupt calls. It was working fine for many syscalls. However, it was not for fork()
. The return pid is the same for both parent and child process. The snippet is as below:
#define SYSCALL0(N) ({ \
register int ip asm("ip") = N; \
register int r0 asm("r0"); \
asm volatile("swi 0x80" : "=r"(r0) : "r"(ip) : "memory"); \
r0; \
})
int main(int argc, char * argv[]) {
NSLog(@"--beginning of program\n");
int counter = 0;
pid_t pid = SYSCALL0(2);
if (pid == 0) {
NSLog(@"pid = %d", getpid());
// child process
for (int i = 0; i < 3; ++i)
NSLog(@"child process: counter=%d\n", ++counter);
}
else if (pid > 0) {
NSLog(@"pid = %d", getpid());
// parent process
for (int i = 0; i < 3; ++i)
NSLog(@"parent process: counter=%d\n", ++counter);
}
else {
// fork failed
NSLog(@"fork() failed!\n");
return 1;
}
NSLog(@"--end of program--\n");
}
Output:
2015-10-11 21:29:43.666 Training[2564:907] --beginning of program
2015-10-11 21:29:43.669 Training[2564:907] pid = 2650
2015-10-11 21:29:43.670 Training[2564:907] parent process: counter=1
2015-10-11 21:29:43.670 Training[2564:907] parent process: counter=2
2015-10-11 21:29:43.669 Training[2564:907] pid = 2650
2015-10-11 21:29:43.671 Training[2564:907] parent process: counter=3
2015-10-11 21:29:43.671 Training[2564:907] --end of program--
2015-10-11 21:29:43.671 Training[2564:907] parent process: counter=1
2015-10-11 21:29:43.672 Training[2564:907] parent process: counter=2
2015-10-11 21:29:43.673 Training[2564:907] parent process: counter=3
2015-10-11 21:29:43.674 Training[2564:907] --end of program--
The tested environment is a jail broken iOS (it will not run on non-jailbroken) running on armv7. I think I might not have done enough with the return of the swi call, so it could not return 0 to indicate child process. What did I miss? How do I get it work correctly?