[launchd-dev] Setting up an AF_UNIX based launchd server

mailist at ericgorr.net mailist at ericgorr.net
Sun Jul 24 15:15:48 PDT 2011


I have been able to setup a AF_INET launchd based server, but, as I 
understand it, an AF_UNIX based server offers better performance and 
there is no need to even think about allowing anything outside of the 
local machine to access the server.

I have made an attempt to get something working, but it is not working 
yet. I am sure there are several problems with the test code, which can 
be found at:

http://ericgorr.net/pq/AF_UNIX.zip
(Developed on Mac OS X 10.6.8 using Xcode 4)

On the client side, I try to connect to the server with the following 
code:

#define kServerSocketPath "/var/tmp/net.ericgorr.testserver/Socket"

int client_connect( void )
{
     int socketFD = socket( AF_UNIX, SOCK_STREAM, 0 );
     assert( socketFD != -1 );

     struct sockaddr_un addr;

     addr.sun_len    = sizeof( addr );
     addr.sun_family = AF_UNIX;

     strcpy( addr.sun_path, kServerSocketPath );

     int result;
     int err;

     result  = connect( socketFD, (struct sockaddr *)&socketFD, sizeof( 
socketFD ) );
     err     = MoreUNIXErrno( result );
     CFShow( CFStringCreateWithFormat( NULL, NULL, CFSTR( 
"client_connect: %d" ), err ) );

     assert( err == 0 );

     return socketFD;
}

However, the connect call sets errno to 2, which I believes means that 
it cannot find kServerSocketPath.

On the server side, I have the following plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>EnableTransactions</key>
     <true/>
     <key>Label</key>
     <string>net.ericgorr.testserver</string>
     <key>Program</key>
     <string>/usr/local/libexec/testserver</string>
     <key>ProgramArguments</key>
     <array>
         <string>testserver</string>
         <string>launchd</string>
     </array>
     <key>Sockets</key>
     <dict>
         <key>net.ericgorr.testserver.sock</key>
         <dict>
             <key>SockFamily</key>
             <string>Unix</string>
             <key>SockPathName</key>
             <string>/var/tmp/net.ericgorr.testserver/Socket</string>
             <key>SockPathMode</key>
             <integer>438</integer>
             <key>SockType</key>
             <string>Stream</string>
         </dict>
     </dict>
</dict>
</plist>

One question is how should /var/tmp/net.ericgorr.testserver/Socket be 
created?

Another question is, after I can launch the server, the server will 
need to check in with launchd and I need to know what index should be 
passed to launch_data_array_get_index...? And, where is this documented?

These questions seem like they should be straightforward...

Thank you.







More information about the launchd-dev mailing list