diff -Npru sphinx-0.9.8/api/sphinxapi.php sphinx-0.9.8-unix_socket/api/sphinxapi.php --- sphinx-0.9.8/api/sphinxapi.php 2008-07-15 01:33:22.000000000 +0100 +++ sphinx-0.9.8-unix_socket/api/sphinxapi.php 2008-07-25 15:30:55.000000000 +0100 @@ -285,10 +285,16 @@ class SphinxClient { $errno = 0; $errstr = ""; - if ( $this->_timeout<=0 ) - $fp = @fsockopen ( $this->_host, $this->_port, $errno, $errstr ); - else - $fp = @fsockopen ( $this->_host, $this->_port, $errno, $errstr, $this->_timeout ); + if ( $this->_timeout<=0 ) + if ( strtolower(substr($this->_host, 0, 7)) == "unix://" ) + $fp = @fsockopen ( $this->_host, null, $errno, $errstr ); + else + $fp = @fsockopen ( $this->_host, $this->_port, $errno, $errstr ); + else + if ( strtolower(substr($this->_host, 0, 7)) == "unix://" ) + $fp = @fsockopen ( $this->_host, null, $errno, $errstr, $this->_timeout ); + else + $fp = @fsockopen ( $this->_host, $this->_port, $errno, $errstr, $this->_timeout ); if ( !$fp ) { diff -Npru sphinx-0.9.8/src/searchd.cpp sphinx-0.9.8-unix_socket/src/searchd.cpp --- sphinx-0.9.8/src/searchd.cpp 2008-07-13 23:14:41.000000000 +0100 +++ sphinx-0.9.8-unix_socket/src/searchd.cpp 2008-07-15 18:32:37.000000000 +0100 @@ -25,6 +25,7 @@ #include #include #include +#include ///////////////////////////////////////////////////////////////////////////// @@ -63,6 +64,9 @@ // MISC GLOBALS ///////////////////////////////////////////////////////////////////////////// +int UNIX_SOCKET_CONN = 0; +const char *address_field; + struct ServedIndex_t { CSphIndex * m_pIndex; @@ -803,41 +807,88 @@ void sphSockSetErrno ( int iErr ) int sphCreateServerSocket ( DWORD uAddr, int iPort ) { static struct sockaddr_in iaddr; + struct sockaddr_un local; + char *socketname; + int len; + + + if ( UNIX_SOCKET_CONN == 1 ) { - iaddr.sin_family = AF_INET; - iaddr.sin_addr.s_addr = uAddr; - iaddr.sin_port = htons ( (short)iPort ); - - char sAddr [ 256 ]; - DWORD uHost = ntohl(uAddr); - snprintf ( sAddr, sizeof(sAddr), "%d.%d.%d.%d:%d", - (uHost>>24) & 0xff, (uHost>>16) & 0xff, (uHost>>8) & 0xff, uHost & 0xff, - iPort ); - - sphInfo ( "creating server socket on %s", sAddr ); - int iSock = socket ( AF_INET, SOCK_STREAM, 0 ); - if ( iSock<0 ) - sphFatal ( "failed to create server socket on %s: %s", sAddr, sphSockError() ); - - int iOn = 1; - if ( setsockopt ( iSock, SOL_SOCKET, SO_REUSEADDR, (char*)&iOn, sizeof(iOn) ) ) - sphFatal ( "setsockopt() failed: %s", sphSockError() ); - - int iTries = 12; - int iRes; - do - { - iRes = bind ( iSock, (struct sockaddr *)&iaddr, sizeof(iaddr) ); - if ( iRes==0 ) - break; + /* Open unix socket */ + + socketname = (char *) (address_field + strlen("unix://")); + sphInfo ( "creating unix socket on %s", socketname); + + int iSock = socket ( PF_UNIX, SOCK_STREAM, 0 ); + if ( iSock<0 ) + sphFatal ( "failed to create unix socket on %s", socketname); + + int iTries = 12; + int iRes; + do + { + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, socketname); + unlink(local.sun_path); + + len = strlen(local.sun_path) + sizeof(local.sun_family); + iRes = bind(iSock, (struct sockaddr *)&local, len); + + if ( iRes==0 ) + break; + + sphInfo ( "failed to bind on %s, retrying...", socketname); + sphUsleep ( 3000 ); + } while ( --iTries>0 ); + if ( iRes ) + sphFatal ( "failed to bind on %s", socketname); + + return iSock; + + } else { + + /* Open TCP listening port */ + + iaddr.sin_family = AF_INET; + iaddr.sin_addr.s_addr = uAddr; + iaddr.sin_port = htons ( (short)iPort ); + + char sAddr [ 256 ]; + DWORD uHost = ntohl(uAddr); + snprintf ( sAddr, sizeof(sAddr), "%d.%d.%d.%d:%d", + (uHost>>24) & 0xff, (uHost>>16) & 0xff, (uHost>>8) & 0xff, uHost & 0xff, + iPort ); + + sphInfo ( "creating server socket on %s", sAddr ); + + int iSock = socket ( AF_INET, SOCK_STREAM, 0 ); + if ( iSock<0 ) + sphFatal ( "failed to create server socket on %s: %s", sAddr, sphSockError() ); + + int iOn = 1; + if ( setsockopt ( iSock, SOL_SOCKET, SO_REUSEADDR, (char*)&iOn, sizeof(iOn) ) ) + sphFatal ( "setsockopt() failed: %s", sphSockError() ); + + int iTries = 12; + int iRes; + do + { + iRes = bind ( iSock, (struct sockaddr *)&iaddr, sizeof(iaddr) ); + if ( iRes==0 ) + break; + + sphInfo ( "failed to bind on %s, retrying...", sAddr ); + sphUsleep ( 3000 ); + } while ( --iTries>0 ); + if ( iRes ) + sphFatal ( "failed to bind on %s", sAddr ); + + return iSock; + + } - sphInfo ( "failed to bind on %s, retrying...", sAddr ); - sphUsleep ( 3000 ); - } while ( --iTries>0 ); - if ( iRes ) - sphFatal ( "failed to bind on %s", sAddr ); - return iSock; } @@ -5050,6 +5101,8 @@ int WINAPI ServiceMain ( int argc, char int rsock; struct sockaddr_in remote_iaddr; + struct sockaddr_in remote_usocket; + socklen_t len; CSphConfig conf; @@ -5725,14 +5778,19 @@ int WINAPI ServiceMain ( int argc, char DWORD uAddr = htonl(INADDR_ANY); if ( hSearchd("address") ) { - struct hostent * pHost = gethostbyname ( hSearchd["address"].cstr() ); - if ( !pHost || pHost->h_addrtype!=AF_INET ) - { - sphWarning ( "no AF_INET address associated with %s, listening on INADDR_ANY", hSearchd["address"].cstr() ); - } else - { - assert ( sizeof(DWORD)==pHost->h_length ); - memcpy ( &uAddr, pHost->h_addr, sizeof(uAddr) ); + address_field = hSearchd["address"].cstr(); + if ( strncmp(address_field, "unix://", strlen("unix://")) == 0 ) UNIX_SOCKET_CONN = 1; + + if ( UNIX_SOCKET_CONN == 0 ) { + struct hostent * pHost = gethostbyname ( hSearchd["address"].cstr() ); + if ( !pHost || pHost->h_addrtype!=AF_INET ) + { + sphWarning ( "no AF_INET address associated with %s, listening on INADDR_ANY", hSearchd["address"].cstr() ); + } else + { + assert ( sizeof(DWORD)==pHost->h_length ); + memcpy ( &uAddr, pHost->h_addr, sizeof(uAddr) ); + } } } g_iSocket = sphCreateServerSocket ( uAddr, iPort ); @@ -5840,8 +5898,17 @@ int WINAPI ServiceMain ( int argc, char continue; // select says something interesting happened, so let's accept - len = sizeof ( remote_iaddr ); - rsock = accept ( g_iSocket, (struct sockaddr*)&remote_iaddr, &len ); + if ( UNIX_SOCKET_CONN == 1 ) { + + len = sizeof ( remote_usocket ); + rsock = accept ( g_iSocket, (struct sockaddr*)&remote_usocket, &len ); + + } else { + + len = sizeof ( remote_iaddr ); + rsock = accept ( g_iSocket, (struct sockaddr*)&remote_iaddr, &len ); + + } int iErr = sphSockGetErrno(); if ( rsock<0 && ( iErr==EINTR || iErr==EAGAIN || iErr==EWOULDBLOCK ) )