Commit b509dd4d authored by Kurt Zeilenga's avatar Kurt Zeilenga
Browse files

sockpair cleanup including:

provide prototype in lutil.h
use LBER_SOCKET_T instead of int in prototype.  (didn't use
	ber_socket_t so all of lber.h wouldn't have be dragged in)
rename signal handlers, use RETSIGTYPE, and make SIGHUP wake
rename fd to sd to reflect descriptor must be usable as a socket.
renamed sel_exit_fds to wake_sds to reflect current use
improve readability of sockpeer.c including removing obtuse goto.
parent 0319dcad
......@@ -13,6 +13,8 @@
#define _LUTIL_H 1
#include <ldap_cdefs.h>
#include <lber_types.h>
/*
* Include file for LDAP utility routine
*/
......@@ -55,6 +57,9 @@ lutil_progname LDAP_P((
int argc,
char *argv[] ));
/* sockpair.c */
LDAP_F( int )
lutil_pair( LBER_SOCKET_T sd[2] );
LDAP_END_DECL
......
......@@ -4,42 +4,50 @@
*/
#include "portable.h"
#include <ac/socket.h>
/* Return a pair of descriptors that are connected to each other. The
* returned descriptors are suitable for use with select(). The two
#include <lutil.h>
/* Return a pair of socket descriptors that are connected to each other.
* The returned descriptors are suitable for use with select(). The two
* descriptors may or may not be identical; the function may return
* the same descriptor number in both slots. It is guaranteed that
* data written on fds[1] will be readable on fds[0]. The returned
* data written on sds[1] will be readable on sds[0]. The returned
* descriptors may be datagram oriented, so data should be written
* in reasonably small pieces and read all at once. On Unix systems
* this function is best implemented using a single pipe() call.
*/
int lutil_pair( int fds[2] )
int lutil_pair( LBER_SOCKET_T sds[2] )
{
struct sockaddr_in si;
int rc, len = sizeof(si);
int fd;
LBER_SOCKET_T sd;
fd = socket( AF_INET, SOCK_DGRAM, 0 );
if (fd < 0)
return fd;
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if (sd < 0)
return sd;
(void) memset( (void*) &si, 0, len );
si.sin_family = AF_INET;
si.sin_port = 0;
si.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
if ( rc = bind( fd, (struct sockaddr *)&si, len ) )
{
fail: tcp_close(fd);
if ( rc = bind( sd, (struct sockaddr *)&si, len ) ) {
tcp_close(sd);
return rc;
}
if ( rc = getsockname( sd, (struct sockaddr *)&si, &len ) ) {
tcp_close(sd);
return rc;
}
if ( rc = getsockname( fd, (struct sockaddr *)&si, &len ) )
goto fail;
if ( rc = connect( fd, (struct sockaddr *)&si, len ) )
goto fail;
fds[0] = fds[1] = fd;
if ( rc = connect( sd, (struct sockaddr *)&si, len ) ) {
tcp_close(sd);
return rc;
}
sds[0] = sds[1] = sd;
return 0;
}
......@@ -42,10 +42,10 @@ typedef struct slap_listener {
Listener **slap_listeners = NULL;
static int sel_exit_fds[2];
static ber_socket_t wake_sds[2];
#define WAKE_LISTENER(w) \
do { if (w) tcp_write( sel_exit_fds[1], "0", 1 ); } while(0)
do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
#ifdef HAVE_WINSOCK2
/* in nt_main.c */
......@@ -359,7 +359,7 @@ int slapd_daemon_init(char *urls, int port, int tls_port )
* loop will be select'ing on this socket, and will wake up when
* this byte arrives.
*/
if( (rc = lutil_pair( sel_exit_fds )) < 0 )
if( (rc = lutil_pair( wake_sds )) < 0 )
{
Debug( LDAP_DEBUG_ANY,
"daemon: lutil_pair() failed rc=%d\n", rc, 0, 0 );
......@@ -422,8 +422,8 @@ int
slapd_daemon_destroy(void)
{
connections_destroy();
tcp_close( sel_exit_fds[1] );
tcp_close( sel_exit_fds[0] );
tcp_close( wake_sds[1] );
tcp_close( wake_sds[0] );
sockdestroy();
return 0;
}
......@@ -512,7 +512,7 @@ slapd_daemon_task(
memcpy( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );
memcpy( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) );
#endif
FD_SET( sel_exit_fds[0], &readfds );
FD_SET( wake_sds[0], &readfds );
for ( l = 0; slap_listeners[l] != NULL; l++ ) {
if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
......@@ -588,12 +588,12 @@ slapd_daemon_task(
/* FALL THRU */
}
if( FD_ISSET( sel_exit_fds[0], &readfds ) )
{
if( FD_ISSET( wake_sds[0], &readfds ) ) {
char c;
tcp_read( sel_exit_fds[0], &c, 1 );
tcp_read( wake_sds[0], &c, 1 );
continue;
}
for ( l = 0; slap_listeners[l] != NULL; l++ ) {
ber_int_t s;
socklen_t len = sizeof(from);
......@@ -1015,19 +1015,21 @@ static int sockdestroy(void)
}
#endif
void
slap_set_shutdown( int sig )
RETSIGTYPE
slap_sig_shutdown( int sig )
{
slapd_shutdown = sig;
WAKE_LISTENER(1);
/* reinstall self */
(void) SIGNAL( sig, slap_set_shutdown );
(void) SIGNAL( sig, slap_sig_shutdown );
}
void
slap_do_nothing( int sig )
RETSIGTYPE
slap_sig_wake( int sig )
{
WAKE_LISTENER(1);
/* reinstall self */
(void) SIGNAL( sig, slap_do_nothing );
(void) SIGNAL( sig, slap_sig_wake );
}
......@@ -173,7 +173,11 @@ int main( int argc, char **argv )
{
int *i;
char *newConfigFile;
if ( is_NT_Service ) CommenceStartupProcessing( NTservice, slap_set_shutdown );
if ( is_NT_Service ) {
CommenceStartupProcessing( NTservice, slap_sig_shutdown );
}
i = (int*)getRegParam( NULL, "Port" );
if ( i != NULL )
{
......@@ -366,22 +370,23 @@ int main( int argc, char **argv )
ldap_pvt_tls_init_def_ctx();
#endif
(void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing );
(void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown );
(void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
(void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
#ifdef SIGPIPE
(void) SIGNAL( SIGPIPE, SIG_IGN );
#endif
#ifdef SIGHUP
(void) SIGNAL( SIGHUP, slap_set_shutdown );
(void) SIGNAL( SIGHUP, slap_sig_shutdown );
#endif
(void) SIGNAL( SIGINT, slap_set_shutdown );
(void) SIGNAL( SIGTERM, slap_set_shutdown );
(void) SIGNAL( SIGINT, slap_sig_shutdown );
(void) SIGNAL( SIGTERM, slap_sig_shutdown );
#ifdef LDAP_SIGCHLD
(void) SIGNAL( LDAP_SIGCHLD, wait4child );
#endif
#ifdef SIGBREAK
/* SIGBREAK is generated when Ctrl-Break is pressed. */
(void) SIGNAL( SIGBREAK, slap_set_shutdown );
(void) SIGNAL( SIGBREAK, slap_sig_shutdown );
#endif
#ifndef HAVE_WINSOCK
......
......@@ -484,8 +484,8 @@ extern void slapd_clr_read LDAP_P((ber_socket_t s, int wake));
extern void slapd_remove LDAP_P((ber_socket_t s, int wake));
extern void slap_set_shutdown LDAP_P((int sig));
extern void slap_do_nothing LDAP_P((int sig));
extern RETSIGTYPE slap_sig_shutdown LDAP_P((int sig));
extern RETSIGTYPE slap_sig_wake LDAP_P((int sig));
extern void config_info LDAP_P((
Connection *conn,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment