Solution to "call to unavailable function 'system': not available on iOS"


#1

From iOS 11 if we call system() we get something like:

error: call to unavailable function 'system': not
      available on iOS
        system("reboot");
        ^~~~~~
/opt/theos/sdks/iPhoneOS11.2.sdk/usr/include/stdlib.h:195:6: note: 
      candidate function has been explicitly made unavailable
int      system(const char *) __DARWIN_ALIAS_C(system);
         ^
1 error generated.

An alternative is to use posix_spawn instead like this:

#import <spawn.h>

extern char **environment;

void run_cmd(const char *cmd)
{
	pid_t pid;
	const char *argv[] = {"sh", "-c", cmd, NULL};
	int status;

	status = posix_spawn(&pid, "/bin/sh", NULL, NULL, (char* const*)argv, environment);
	if (status == 0) {
		if (waitpid(pid, &status, 0) == -1) {
			perror("waitpid");
		}
	}
}
void your_function(void)
{
	run_cmd("reboot");
}

#2

顶顶:smile:


#3

Looks like this symbol still exists in 11.4

    void *handle = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
    void (*system)(char *) = dlsym(handle, "system");

Didn’t check if it works since my iPhone is jailed.


#4

It does exist but is marked as __IOS_PROHIBITED