1 /* $NetBSD: opensock.c,v 1.3 2021/08/14 16:15:01 christos 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-2021 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 <sys/cdefs.h>
24 __RCSID("$NetBSD: opensock.c,v 1.3 2021/08/14 16:15:01 christos Exp $");
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/errno.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
33 #include <ac/unistd.h>
34
35 #include "slap.h"
36 #include "back-sock.h"
37
38 /*
39 * FIXME: count the number of concurrent open sockets (since each thread
40 * may open one). Perhaps block here if a soft limit is reached, and fail
41 * if a hard limit reached
42 */
43
44 FILE *
opensock(const char * sockpath)45 opensock(
46 const char *sockpath
47 )
48 {
49 int fd;
50 FILE *fp;
51 struct sockaddr_un sockun;
52
53 fd = socket(PF_UNIX, SOCK_STREAM, 0);
54 if ( fd < 0 ) {
55 Debug( LDAP_DEBUG_ANY, "socket create failed\n" );
56 return( NULL );
57 }
58
59 sockun.sun_family = AF_UNIX;
60 sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1),
61 sockpath);
62 if ( connect( fd, (struct sockaddr *)&sockun, sizeof(sockun) ) < 0 ) {
63 Debug( LDAP_DEBUG_ANY, "socket connect(%s) failed\n",
64 sockpath ? sockpath : "<null>" );
65 close( fd );
66 return( NULL );
67 }
68
69 if ( ( fp = fdopen( fd, "r+" ) ) == NULL ) {
70 Debug( LDAP_DEBUG_ANY, "fdopen failed\n" );
71 close( fd );
72 return( NULL );
73 }
74
75 return( fp );
76 }
77