Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-sock / opensock.c
blobfbcde2d0cc3a85833242f7d4f679c158ea8a0eb5
1 /* opensock.c - open a unix domain socket */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-sock/opensock.c,v 1.3.2.1 2008/02/09 00:46:09 quanah Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2007-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* ACKNOWLEDGEMENTS:
17 * This work was initially developed by Brian Candler for inclusion
18 * in OpenLDAP Software.
21 #include "portable.h"
23 #include <stdio.h>
25 #include <ac/errno.h>
26 #include <ac/string.h>
27 #include <ac/socket.h>
28 #include <ac/unistd.h>
30 #include "slap.h"
31 #include "back-sock.h"
34 * FIXME: count the number of concurrent open sockets (since each thread
35 * may open one). Perhaps block here if a soft limit is reached, and fail
36 * if a hard limit reached
39 FILE *
40 opensock(
41 const char *sockpath
44 int fd;
45 FILE *fp;
46 struct sockaddr_un sockun;
48 fd = socket(PF_UNIX, SOCK_STREAM, 0);
49 if ( fd < 0 ) {
50 Debug( LDAP_DEBUG_ANY, "socket create failed\n", 0, 0, 0 );
51 return( NULL );
54 sockun.sun_family = AF_UNIX;
55 sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1),
56 sockpath);
57 if ( connect( fd, (struct sockaddr *)&sockun, sizeof(sockun) ) < 0 ) {
58 Debug( LDAP_DEBUG_ANY, "socket connect(%s) failed\n",
59 sockpath ? sockpath : "<null>", 0, 0 );
60 return( NULL );
63 if ( ( fp = fdopen( fd, "r+" ) ) == NULL ) {
64 Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 );
65 close( fd );
66 return( NULL );
69 return( fp );