1 /* $NetBSD: opensock.c,v 1.1.1.4 2014/05/28 09:58:51 tron Exp $ */ 2 3 /* opensock.c - open a unix domain socket */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 2007-2014 The OpenLDAP Foundation. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted only as authorized by the OpenLDAP 12 * Public License. 13 * 14 * A copy of this license is available in the file LICENSE in the 15 * top-level directory of the distribution or, alternatively, at 16 * <http://www.OpenLDAP.org/license.html>. 17 */ 18 /* ACKNOWLEDGEMENTS: 19 * This work was initially developed by Brian Candler for inclusion 20 * in OpenLDAP Software. 21 */ 22 23 #include "portable.h" 24 25 #include <stdio.h> 26 27 #include <ac/errno.h> 28 #include <ac/string.h> 29 #include <ac/socket.h> 30 #include <ac/unistd.h> 31 32 #include "slap.h" 33 #include "back-sock.h" 34 35 /* 36 * FIXME: count the number of concurrent open sockets (since each thread 37 * may open one). Perhaps block here if a soft limit is reached, and fail 38 * if a hard limit reached 39 */ 40 41 FILE * 42 opensock( 43 const char *sockpath 44 ) 45 { 46 int fd; 47 FILE *fp; 48 struct sockaddr_un sockun; 49 50 fd = socket(PF_UNIX, SOCK_STREAM, 0); 51 if ( fd < 0 ) { 52 Debug( LDAP_DEBUG_ANY, "socket create failed\n", 0, 0, 0 ); 53 return( NULL ); 54 } 55 56 sockun.sun_family = AF_UNIX; 57 sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), 58 sockpath); 59 if ( connect( fd, (struct sockaddr *)&sockun, sizeof(sockun) ) < 0 ) { 60 Debug( LDAP_DEBUG_ANY, "socket connect(%s) failed\n", 61 sockpath ? sockpath : "<null>", 0, 0 ); 62 close( fd ); 63 return( NULL ); 64 } 65 66 if ( ( fp = fdopen( fd, "r+" ) ) == NULL ) { 67 Debug( LDAP_DEBUG_ANY, "fdopen failed\n", 0, 0, 0 ); 68 close( fd ); 69 return( NULL ); 70 } 71 72 return( fp ); 73 } 74