http://bbs.iosre.com/forum.php?m … =204&highlight=root
写的daemon可以root权限启动和运行了,这个启动方式是重启后启动的,有没有什么方法可以再root权限的app上,随时启动和关闭这个root权限的daemon?
用
launchctl
命令
@implementation LaunchctlHelper
+ (int)loadLaunchdJobWithPlistAt:(NSString*)pathToLaunchdPlist {
NSTask* task = [NSTask alloc] init] autorelease];
[task setLaunchPath: @"/bin/launchctl"];
[task setArguments: [NSArray arrayWithObjects:
@"load",
@"-w",
pathToLaunchdPlist,
nil]];
[task launch];
[task waitUntilExit];
return [task terminationStatus];
}
+ (int)unloadLaunchdJobWithPlistAt:(NSString*)pathToLaunchdPlist {
NSTask* task = [NSTask alloc] init] autorelease];
[task setLaunchPath: @"/bin/launchctl"];
[task setArguments: [NSArray arrayWithObjects:
@"unload",
@"-w",
pathToLaunchdPlist,
nil]];
[task launch];
[task waitUntilExit];
return [task terminationStatus];
}
@end
int result = [xxxx loadLaunchdJobWithPlistAt: @"/Library/LaunchDaemons/xxxx.plist"];
@implementation DaemonHandler
-
(BOOL)daemonIsLaunchedAtLogin {
NSFileManager * fileManager = [NSFileManager defaultManager];
return ([fileManager fileExistsAtPath:[NSString stringWithCString:LAUNCHCTL_PLIST_PATH
encoding:NSASCIIStringEncoding]]);
} -
(BOOL)daemonIsLaunched {
ProcessHelper * procInfo = [ProcessHelper alloc] init];
[procInfo obtainFreshProcessList]; // Get a list of process
BOOL daemonLaunched = [procInfo findProcessWithName:@“SLNTFS Daemon”];
return daemonLaunched;
} -
(BOOL)launchDaemon:(SFAuthorization *)auth {
char * loadArgs[3] = {“load”, LAUNCHCTL_PLIST_PATH, NULL}; // Launch daemon
OSStatus ret = AuthorizationExecuteWithPrivileges([auth authorizationRef],
[BIN_LAUNCHCTL_PATH cStringUsingEncoding:NSASCIIStringEncoding],
kAuthorizationFlagDefaults,
loadArgs,
NULL);
return (ret == errAuthorizationSuccess);
} -
(BOOL)terminateDaemon:(SFAuthorization *)auth {
char * unloadArgs[3] = {“unload”, LAUNCHCTL_PLIST_PATH, NULL}; // Stop daemon
OSStatus ret = AuthorizationExecuteWithPrivileges([auth authorizationRef],
[BIN_LAUNCHCTL_PATH cStringUsingEncoding:NSASCIIStringEncoding],
kAuthorizationFlagDefaults,
unloadArgs,
NULL);
return (ret == errAuthorizationSuccess);
} -
(void)enableDaemonAtLoginWithAuthorization:(SFAuthorization *)auth {
char * cpArgs[4] = {
“-f”,
“/Library/PreferencePanes/SLNTFS.prefPane/Contents/Resources/com.juicybinary.slntfsDaemon.plist”,
“/Library/LaunchDaemons/com.juicybinary.slntfsDaemon.plist”,
NULL
};
OSStatus ret = AuthorizationExecuteWithPrivileges([auth authorizationRef],
[BIN_CP_PATH cStringUsingEncoding:NSASCIIStringEncoding],
kAuthorizationFlagDefaults,
cpArgs,
NULL);
ALog(@"%d", ret);
} -
(void)disableDaemonAtLoginWithAuthorization:(SFAuthorization *)auth {
char * rmArgs[3] = {"-rf", LAUNCHCTL_PLIST_PATH, NULL};
AuthorizationExecuteWithPrivileges([auth authorizationRef],
[BIN_RM_PATH cStringUsingEncoding:NSASCIIStringEncoding],
kAuthorizationFlagDefaults,
rmArgs,
NULL);
} -
(void)launchDaemonsAtLogin:(BOOL)state
withAuthorization:(SFAuthorization *)auth {
if (state) [DaemonHandler enableDaemonAtLoginWithAuthorization:auth];
else [DaemonHandler disableDaemonAtLoginWithAuthorization:auth];
}
调用后直接Launch就crash了,需要root权限来调用吗?
{“name”:“launchctl”,“bug_type”:“109”,“os_version”:“iPhone OS 7.1.2 (11D257)”,“version”:"???",“app_name”:“launchctl”}
Incident Identifier: C1F3E4E6-E826-4433-AA8A-0D1702EEFB4E
CrashReporter Key: acaf78bd7a2032e758d8d50e059221f91167f56f
Hardware Model: iPhone3,3
Process: launchctl [9779]
Path: /bin/launchctl
Identifier: launchctl
Version: ???
Code Type: ARM (Native)
Parent Process: SpringBoard [9735]
Date/Time: 2015-02-12 15:00:51.496 +0800
OS Version: iOS 7.1.2 (11D257)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000
Triggered by Thread: 0
Thread 0 Crashed:
0 liblaunch.dylib 0x38141442 0x3813e000 + 13378
1 launchctl 0x0003d572 0x3c000 + 5490
2 launchctl 0x0003cc76 0x3c000 + 3190
3 libdyld.dylib 0x3813cab4 0x3813b000 + 6836
Thread 0 crashed with ARM Thread State (32-bit):
r0: 0x00000000 r1: 0x00000000 r2: 0x17d53000 r3: 0x00000049
r4: 0x17d521d0 r5: 0x00000000 r6: 0x17d452c0 r7: 0x27dc3f54
r8: 0x00000000 r9: 0x00000fff r10: 0x000467b0 r11: 0x17d45c60
ip: 0x39f59200 sp: 0x27dc3f44 lr: 0x0003d577 pc: 0x38141442
cpsr: 0x60000030
Binary Images:
0x3c000 - 0x45fff launchctl armv7 <744747b5ba4036adb8869ebe8b9dc6bb> /bin/launchctl
0x50000 - 0x50fff MobileSubstrate.dylib armv6 <4ba32397003435858ce17742601c11dd> /Library/MobileSubstrate/MobileSubstrate.dylib
0x2be7b000 - 0x2be9bfff dyld armv7 <651a31c39f71311f965f8ac44de02c88> /usr/lib/dyld
0x2d3ab000 - 0x2d4edff0 CoreFoundation armv7 <4f5fe18d15f2330bb4759690b5d2e575> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
0x2e167000 - 0x2e1bdfff IOKit armv7 /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
0x37281000 - 0x37282fff libSystem.B.dylib armv7 <89c8b9badf4e3d5594391736841b2f25> /usr/lib/libSystem.B.dylib
0x3746e000 - 0x3747afff libbsm.0.dylib armv7 /usr/lib/libbsm.0.dylib
0x37485000 - 0x374d0fff libc++.1.dylib armv7 /usr/lib/libc++.1.dylib
0x374d1000 - 0x374ebfff libc++abi.dylib armv7 /usr/lib/libc++abi.dylib
0x37504000 - 0x3751bfff libedit.3.dylib armv7 /usr/lib/libedit.3.dylib
0x3752e000 - 0x3752efff libgcc_s.1.dylib armv7 <5adc2932566937829dac6190137ff948> /usr/lib/libgcc_s.1.dylib
0x37749000 - 0x3789afff libicucore.A.dylib armv7 <2878f2bd470530c88fdb8911abea73e8> /usr/lib/libicucore.A.dylib
0x37c04000 - 0x37c2bfff libncurses.5.4.dylib armv7 <16b8ead9aea43c8cb543c5d56f3d8c61> /usr/lib/libncurses.5.4.dylib
0x37c2c000 - 0x37dcbfff libobjc.A.dylib armv7 /usr/lib/libobjc.A.dylib
0x37f68000 - 0x37fb5fff libstdc++.6.dylib armv7 <60d2a9f34296398bb7c057e01fde6afc> /usr/lib/libstdc++.6.dylib
0x380b6000 - 0x380c2fff libz.1.dylib armv7 <76b41c58034a30dabe6ad9be1e637644> /usr/lib/libz.1.dylib
0x380c3000 - 0x380c7fff libcache.dylib armv7 /usr/lib/system/libcache.dylib
0x380c8000 - 0x380d0fff libcommonCrypto.dylib armv7 /usr/lib/system/libcommonCrypto.dylib
0x380d1000 - 0x380d5fff libcompiler_rt.dylib armv7 <29f589388cbd3290b650304eadc980ce> /usr/lib/system/libcompiler_rt.dylib
0x380d6000 - 0x380dcfff libcopyfile.dylib armv7 <236a4387651534e9a9dd670ca9db02bb> /usr/lib/system/libcopyfile.dylib
0x380dd000 - 0x38115fff libcorecrypto.dylib armv7 <912e56f196943500877e1aba46efcb78> /usr/lib/system/libcorecrypto.dylib
0x38116000 - 0x3813afff libdispatch.dylib armv7 /usr/lib/system/libdispatch.dylib
0x3813b000 - 0x3813cfff libdyld.dylib armv7 /usr/lib/system/libdyld.dylib
0x3813d000 - 0x3813dfff libkeymgr.dylib armv7 /usr/lib/system/libkeymgr.dylib
0x3813e000 - 0x38144fff liblaunch.dylib armv7 <1efc4b3c146d347cb586fce80e8acdcb> /usr/lib/system/liblaunch.dylib
0x38145000 - 0x38148fff libmacho.dylib armv7 <0ffeb9459e8d3ba99467a159b8c23d78> /usr/lib/system/libmacho.dylib
0x38149000 - 0x3814afff libremovefile.dylib armv7 /usr/lib/system/libremovefile.dylib
0x3814b000 - 0x38158fff libsystem_asl.dylib armv7 <6569ced4c68b3a119b89f3a8f48564be> /usr/lib/system/libsystem_asl.dylib
0x38159000 - 0x38159fff libsystem_blocks.dylib armv7 <0178b6f1a9bf362d9672641076a42ee2> /usr/lib/system/libsystem_blocks.dylib
0x3815a000 - 0x381bbfff libsystem_c.dylib armv7 <371f0e1854f03b05ba143619e4eac99f> /usr/lib/system/libsystem_c.dylib
0x381bc000 - 0x381befff libsystem_configuration.dylib armv7 <3e82bcb352f53951b5bce186bf9c30f7> /usr/lib/system/libsystem_configuration.dylib
0x381bf000 - 0x381c5fff libsystem_dnssd.dylib armv7 /usr/lib/system/libsystem_dnssd.dylib
0x381c6000 - 0x381defff libsystem_info.dylib armv7 <2996bfe2d4833c20b428a1b7b4db3c52> /usr/lib/system/libsystem_info.dylib
0x381df000 - 0x381f7fff libsystem_kernel.dylib armv7 /usr/lib/system/libsystem_kernel.dylib
0x381f8000 - 0x38217fff libsystem_m.dylib armv7 /usr/lib/system/libsystem_m.dylib
0x38218000 - 0x38229fff libsystem_malloc.dylib armv7 /usr/lib/system/libsystem_malloc.dylib
0x3822a000 - 0x3824afff libsystem_network.dylib armv7 /usr/lib/system/libsystem_network.dylib
0x3824b000 - 0x38252fff libsystem_notify.dylib armv7 <932b71a166a3302f92405c0e77031b11> /usr/lib/system/libsystem_notify.dylib
0x38253000 - 0x38258fff libsystem_platform.dylib armv7 <12ec95161bda391c8db84f7255b85501> /usr/lib/system/libsystem_platform.dylib
0x38259000 - 0x3825efff libsystem_pthread.dylib armv7 /usr/lib/system/libsystem_pthread.dylib
0x3825f000 - 0x38260fff libsystem_sandbox.dylib armv7 /usr/lib/system/libsystem_sandbox.dylib
0x38261000 - 0x38263fff libsystem_stats.dylib armv7 <0f72cd8b79ad3600b2eb01c4391b2f53> /usr/lib/system/libsystem_stats.dylib
0x38264000 - 0x38264fff libunwind.dylib armv7 <45c29718368e3d1c8821911ab5953080> /usr/lib/system/libunwind.dylib
0x38265000 - 0x3827ffff libxpc.dylib armv7 <399deef84c9e3570a39520230faa3216> /usr/lib/system/libxpc.dylib
应该需要root权限;launchctl是一个命令行工具,可以直接
system("launchctl -load xxx");
搞定,具体命令如下
system(“launchctl load -w /Library/LaunchDaemons/xxxx.plist”);
感谢狗神
IOS 8 里面显示 system 命令已经被遗弃了,提示改用posix_spawn。如果这样需要怎么写呢,我试了几次都无法成功
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
system("reboot");
#pragma clang diagnostic pop
非常感谢。