objective c - Exception `-[NSConcreteFileHandle readDataOfLength:]: Bad file descriptor` when running a NSUserUnixTask -
i'm using nsuserunixtask
run unsandboxed nstask
on sandboxed app. however, code hangs on calls [nsfilehandle readdatatoendoffile]
. if remove calls works perfectly. if replace [nsfilehandle readdatatoendoffile]
[nsfilehandle availabledata]
hangs well.
here code:
nsuserunixtask* unixtask = [[nsuserunixtask alloc] initwithurl: [nsurl fileurlwithpath: path] error: nil]; // create error file handle nsfilehandle* errorfh = [nsfilehandle filehandlewithstandarderror]; [unixtask setstandarderror: errorfh]; // create output file handle nsfilehandle* outputfh = [nsfilehandle filehandlewithstandardoutput]; [unixtask setstandardoutput: outputfh]; // run task termination handler [unixtask executewitharguments: nil completionhandler: ^(nserror* error2) { // save output nsstring* output = [[nsstring alloc] initwithdata: [outputfh readdatatoendoffile] encoding: nsutf8stringencoding]; if ([output length]) { // <-- execution never reaches line when block called nslog(@"%@", output); } // read error 1 nsstring* error1 = [[nsstring alloc] initwithdata: [errorfh readdatatoendoffile] encoding: nsutf8stringencoding]; if ([error1 length]) { nslog(@"%@", error1); } // read error 2 if (error2) { nslog(@"%@", error2); } }];
nothing logged on xcode's console, if open console.app see line:
warning: exception caught during decoding of received reply message 'executescript:interpreter:arguments:standardinput:standardoutput:standarderror::', dropping incoming message , calling failure block. exception: *** -[nsconcretefilehandle readdataoflength:]: bad file descriptor
hmmm, how can solve this? before had app sandboxed used nspipe
in conjunction nstask
, worked, nsuserunixtask
requires use of nsfilehandle
instead , reckon problem because i'm not using correctly. still have no idea i'm doing wrong.
it seems me have use nspipe
read stderr , stdout nsuserunixtask
in same way done nstask
.
i have tested "/bin/ls" task , produced expected output:
nspipe *outpipe = [nspipe pipe]; nspipe *errpipe = [nspipe pipe]; nsstring *path = @"/bin/ls"; nsuserunixtask *unixtask = [[nsuserunixtask alloc] initwithurl: [nsurl fileurlwithpath: path] error: nil]; [unixtask setstandardoutput:[outpipe filehandleforwriting]]; [unixtask setstandarderror:[errpipe filehandleforwriting]]; [unixtask executewitharguments:nil completionhandler:^(nserror *error) { nsstring *output = [[nsstring alloc] initwithdata: [[outpipe filehandleforreading] readdatatoendoffile] encoding: nsutf8stringencoding]; nslog(@"stdout: %@", output); nsstring *error1 = [[nsstring alloc] initwithdata: [[errpipe filehandleforreading] readdatatoendoffile] encoding: nsutf8stringencoding]; nslog(@"stderr: %@", error1); }];
Comments
Post a Comment