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;

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

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

	return status;
}
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


#5

狗神 这样定义环境变量 extern char **environment; 编译的时候报这样的错误
“_environment”, referenced from:
-[xxx xxx] in xxx.m.274b3df1.o

折腾一段时间后, 发现这样定义
extern char **environ , 就能编译通过 , 不知道为什么, 希望前辈指点


#6

就是 environ,原文可能是笔误


#7

好吧 =-=