system 函数被废除的替代方法
做越狱应用和插件开发,经常会调用 system 去执行系统命令,早在 Xcode 7,使用 system 函数提示警告:
'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs installd
只是警告,还是可以正常编译和使用,但是升级到 Xcode 9,system 函数就从 SDK 中移除了,不能再使用了,提示:
'system' is unavailable: not available on iOS
替代的方法一般有三种,第一种是使用 posix_spawn,代码如下:
pid_t pid;
char *argv[] = {"/bin/ls", //path"-al", //parameter1"/", //parameter2NULL
};posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL);printf("pid=%d,child pid = %d\n",getpid(),pid);int stat;
waitpid(pid,&stat,0);
printf("stat is %d\n",stat);
第二种是使用 NSTask,代码如下:
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/bin/ls";
task.arguments = [NSArray arrayWithObjects:@"-al",@"/",nil];
[task launch];
[task waitUntilExit];
NSTask.h 头文件信息如下:
#import <Foundation/NSObject.h>@class NSString, NSArray, NSDictionary;@interface NSTask : NSObject// Create an NSTask which can be run at a later time
// An NSTask can only be run once. Subsequent attempts to
// run an NSTask will raise.
// Upon task death a notification will be sent
// { Name = NSTaskDidTerminateNotification; object = task; }
//- (instancetype)init;// set parameters
// these methods can only be done before a launch
// if not set, use current
// if not set, use current// set standard I/O channels; may be either an NSFileHandle or an NSPipe
- (void)setStandardInput:(id)input;
- (void)setStandardOutput:(id)output;
- (void)setStandardError:(id)error;// get parameters
@property (NS_NONATOMIC_IOSONLY, copy) NSString *launchPath;
@property (NS_NONATOMIC_IOSONLY, copy) NSArray *arguments;
@property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *environment;
@property (NS_NONATOMIC_IOSONLY, copy) NSString *currentDirectoryPath;// get standard I/O channels; could be either an NSFileHandle or an NSPipe
- (id)standardInput;
- (id)standardOutput;
- (id)standardError;// actions
- (void)launch;- (void)interrupt; // Not always possible. Sends SIGINT.
- (void)terminate; // Not always possible. Sends SIGTERM.@property (NS_NONATOMIC_IOSONLY, readonly) BOOL suspend;
@property (NS_NONATOMIC_IOSONLY, readonly) BOOL resume;// status
@property (NS_NONATOMIC_IOSONLY, readonly) int processIdentifier;
@property (NS_NONATOMIC_IOSONLY, getter=isRunning, readonly) BOOL running;@property (NS_NONATOMIC_IOSONLY, readonly) int terminationStatus;@end@interface NSTask (NSTaskConveniences)+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments;
// convenience; create and launch- (void)waitUntilExit;
// poll the runLoop in defaultMode until task completes@endFOUNDATION_EXPORT NSString * const NSTaskDidTerminateNotification;
如果非要调用 system 函数不可,那就使用第三种方法,找到 system 函数地址直接调用,方法参见: 动态调用函数,具体代码如下:
typedef int (*my_system) (const char *str);
int call_system(const char *str){//动态库路径char *dylib_path = "/usr/lib/libSystem.dylib";//打开动态库void *handle = dlopen(dylib_path, RTLD_GLOBAL | RTLD_NOW);if (handle == NULL) {//打开动态库出错fprintf(stderr, "%s\n", dlerror());} else {//获取 system 地址my_system system = dlsym(handle, "system");//地址获取成功则调用if (system) {int ret = system(str);return ret;}dlclose(handle); //关闭句柄}return -1;
}
这样 call_system 函数就相当于 system 的功能了,替换即可。
原文地址:https://www.exchen.net/ios-hacker-system-%E5%87%BD%E6%95%B0%E8%A2%AB%E5%BA%9F%E9%99%A4%E7%9A%84%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%B3%95.html
system 函数被废除的替代方法相关推荐
- LinuxC++开发面试系列(三):代码运行、gcc编译、system函数与VS2013 C4996解决方法
文章目录 同步GitHub在此
- php是否直接支持函数的重载,php函数重载的替代方法--伪重载详解
对于弱类型语言来说,PHP函数重载,并不像一般的OOP那样.而弱类型本来就不区分,所以无法通过这些来实现.但是,可以通过下面的方法来实现简单的伪重载吧. 函数重载的替代方法-伪重载,下面看一个具体的实 ...
- (04)System Verilog 利用函数通用总线激励驱动方法
(04)System Verilog 利用函数通用总线激励驱动方法 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog 利用函数通用总线激 ...
- php 中c函数重载,php函数重载的替代方法--伪重载详解
php //函数重载的替代方法-伪重载 // //确实,在PHP中没有函数重载这个概念,让很多时候我们无法进行一些处理,甚至有时候不得不在函数后面定义好N个参数 //在看到了func_get_arg, ...
- popen和system函数的区别 以及 popen打开的FILE指针能否用close替代fclose关闭
popen和system函数的区别 在c/cpp程序中执行shell命令,通常有两种方式,一种是使用popen函数,一种是使用system函数:两者会调用fork函数从父进程中fork出一个子进程,然 ...
- Linux下使用system()函数一定要谨慎
转载自:http://my.oschina.net/renhc/blog/53580 linux尽量避免使用system. 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system( ...
- linux下system函数的深入理解
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...
- 【C/C++】Linux下使用system()函数一定要谨慎
曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原 ...
- 头条白板面试_让我们谈谈白板面试和可能的替代方法
头条白板面试 by Sun-Li Beatteay 通过孙丽·贝蒂 让我们谈谈白板面试和可能的替代方法 (Let's talk about whiteboarding interviews and t ...
最新文章
- 2020年世界机器人报告
- Hadoop运维记录系列(十七)
- linux文件编程(2)——系统文件描述符、动静态文件、块设备介绍
- csp-s模拟测试41「夜莺与玫瑰·玫瑰花精·影子」
- 为什么有的人喜欢夜间工作_为什么开发人员喜欢在夜间编码
- 绕过COM,一个巧妙的思路
- NASA 遭攻击,安全 Bug 仍未解决!
- php中的索引数组和关联数组
- java hssfsheet_Java中,使用HSSFSheet创建excel模板如何创建一列两行的数据?
- 安卓GLSurfaceView使用简单范例
- Chromium OS?本土化气息的的Flint OS
- 即席查询—Presto
- ADPRL - 近似动态规划和强化学习 - Note 1 - Introduction
- ArcGIS Engine10.0轻松入门级教程(1)——必备基础知识
- pycharm使用问题:鼠标光标变成黑(白)色粗方块
- 信息系统安全等级保护、安全策略设计原则及安全方案笔记
- 网络安全红队常用的攻击方法及路径
- 小远qq1611532647淘宝主图设计
- 低代码和无代码开发的4个安全问题
- 户籍管理系统测试用例
热门文章
- Windows下用Mingw编译Boost.Regex库
- mongoose 验证
- Spring mvc中自定义拦截器
- 1020. Tree Traversals (25) PAT甲级真题
- Error -26612: HTTP Status-Code=500 (Internal Server Error) ...
- Jquery Mobile 百度地图 Demo
- 【solr基础教程之二】索引
- hdu 4666 Hyperspace
- 如何使用DNN中的Calendar控件
- [基础题] 6.(*)按如下要求编写Java程序: (1)编写一个接口:OneToN,只含有一个方法int dispose(int n)