Detection of framework usage on Mac system?

2019-07-20 14:00发布

问题:

I wanted to develop sample framework on OSX with having requirement that at any point in time the framework should be used by only single client, i am not getting how to achieve this ? is their any API's to detect weather the framework is in use? can we use some file related API for this?..i have seen a windows sample where they where detecting the dylib's usage using Following API's ?? CreateFileMappingW MapViewOfFile OpenFileMappingW

Does anyone has come across such scenarios??

回答1:

You can use lsof command. it will return list of open files.

In the absence of any options, lsof lists all open files belonging to all active processes.

NSTask* task = [[NSTask alloc] init];
NSPipe* pipe = [[NSPipe alloc] init];

NSArray* args = [NSArray arrayWithObjects: @"-c", @"lsof | grep -i some.framework | wc -l",nil];
[task setLaunchPath: @"/bin/sh"];
[task setArguments: args];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
[task setStandardInput: [NSPipe pipe]]; 
[task launch];    
[task waitUntilExit];

NSFileHandle* file = [pipe fileHandleForReading];
NSString* result = [[NSString alloc] initWithData: [file readDataToEndOfFile] encoding: NSASCIIStringEncoding];
NSLog(@"%@",result);
[result release];
[task release];
[pipe release];