| 1 |
/* opensock.c - open a unix domain socket */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2007-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 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. |
| 11 |
* |
| 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>. |
| 15 |
*/ |
| 16 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was initially developed by Brian Candler for inclusion |
| 18 |
* in OpenLDAP Software. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "portable.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
|
| 25 |
#include <ac/errno.h> |
| 26 |
#include <ac/string.h> |
| 27 |
#include <ac/socket.h> |
| 28 |
#include <ac/unistd.h> |
| 29 |
|
| 30 |
#include "slap.h" |
| 31 |
#include "back-sock.h" |
| 32 |
|
| 33 |
/* |
| 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 |
| 37 |
*/ |
| 38 |
|
| 39 |
FILE * |
| 40 |
opensock( |
| 41 |
const char *sockpath |
| 42 |
) |
| 43 |
{ |
| 44 |
int fd; |
| 45 |
FILE *fp; |
| 46 |
struct sockaddr_un sockun; |
| 47 |
|
| 48 |
fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 49 |
if ( fd < 0 ) { |
| 50 |
Debug( LDAP_DEBUG_ANY, "socket create failed\n" ); |
| 51 |
return( NULL ); |
| 52 |
} |
| 53 |
|
| 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>" ); |
| 60 |
close( fd ); |
| 61 |
return( NULL ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( ( fp = fdopen( fd, "r+" ) ) == NULL ) { |
| 65 |
Debug( LDAP_DEBUG_ANY, "fdopen failed\n" ); |
| 66 |
close( fd ); |
| 67 |
return( NULL ); |
| 68 |
} |
| 69 |
|
| 70 |
return( fp ); |
| 71 |
} |