Unix Socket domain binding from iOS extension fails with errno 48 (address already in use)

I have some logic that works by creating a Unix Domain Socket and does not have problems when running in a regular application. However, when I run it to extend the application, I get -1 with errno = 48 ("Address is already in use") from bind ().

NSArray *applicationSupportDirectoryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); int fd = socket(AF_UNIX, SOCK_STREAM, 0); // returns a non-zero value NSString *loc = [applicationSupportDirectoryPaths[0] stringByAppendingPathComponent:@"usd"]; struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX ; strncpy(addr.sun_path, [loc UTF8String], sizeof(addr.sun_path)-1); int bindres = bind(fd, (struct sockaddr*)&addr, sizeof(addr)); // returns -1, error is 48 

Return location for library directory:

 /var/mobile/Containers/Data/PluginKitPlugin/A8110BA2-5AE7-42C1-84DA-2A9B303C7277/Library/ 

I think the reason is that this is a mistake, because it is some kind of special place compared to the library catalog for the application.

If anyone has an idea why this is happening or some workarounds, I appreciate that.

UPDATE: I tried with the cache directory (NSCachesDirectory), but now I get errno 3 (no such process).

+6
source share
3 answers

First guess that the address is actually being used.

iOS can instantiate / kill extensions on demand, so the socket associated in the previous call cannot be released on the next call.

+3
source

I think you should first release the first object, and then call the next.

0
source

If you want the new socket to be bound forcibly, you can use the SO_REUSEADDR option.

You can use setsockopt () as shown below to set SO_REUSEADDR.

 int reuse = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) { perror("setsockopt(SO_REUSEADDR) failed"); } 
0
source

All Articles