Back to index

openldap  2.4.31
opensock.c
Go to the documentation of this file.
00001 /* opensock.c - open a unix domain socket */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2007-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Brian Candler for inclusion
00018  * in OpenLDAP Software.
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <stdio.h>
00024 
00025 #include <ac/errno.h>
00026 #include <ac/string.h>
00027 #include <ac/socket.h>
00028 #include <ac/unistd.h>
00029 
00030 #include "slap.h"
00031 #include "back-sock.h"
00032 
00033 /*
00034  * FIXME: count the number of concurrent open sockets (since each thread
00035  * may open one). Perhaps block here if a soft limit is reached, and fail
00036  * if a hard limit reached
00037  */
00038 
00039 FILE *
00040 opensock(
00041     const char       *sockpath
00042 )
00043 {
00044        int    fd;
00045        FILE   *fp;
00046        struct sockaddr_un sockun;
00047 
00048        fd = socket(PF_UNIX, SOCK_STREAM, 0);
00049        if ( fd < 0 ) {
00050               Debug( LDAP_DEBUG_ANY, "socket create failed\n", 0, 0, 0 );
00051               return( NULL );
00052        }
00053 
00054        sockun.sun_family = AF_UNIX;
00055        sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1),
00056               sockpath);
00057        if ( connect( fd, (struct sockaddr *)&sockun, sizeof(sockun) ) < 0 ) {
00058               Debug( LDAP_DEBUG_ANY, "socket connect(%s) failed\n",
00059                      sockpath ? sockpath : "<null>", 0, 0 );
00060               close( fd );
00061               return( NULL );
00062        }
00063 
00064        if ( ( fp = fdopen( fd, "r+" ) ) == NULL ) {
00065               Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );
00066               close( fd );
00067               return( NULL );
00068        }
00069 
00070        return( fp );
00071 }