xref: /netbsd-src/external/bsd/openldap/dist/servers/lloadd/module_init.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: module_init.c,v 1.2 2021/08/14 16:14:58 christos Exp $	*/
2 
3 /* module_init.c - module initialization functions */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-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 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that this notice is preserved and that due credit is given
23  * to the University of Michigan at Ann Arbor. The name of the University
24  * may not be used to endorse or promote products derived from this
25  * software without specific prior written permission. This software
26  * is provided ``as is'' without express or implied warranty.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: module_init.c,v 1.2 2021/08/14 16:14:58 christos Exp $");
31 
32 #include "portable.h"
33 
34 #include <stdio.h>
35 
36 #include <ac/socket.h>
37 #include <ac/string.h>
38 #include <ac/time.h>
39 
40 #include "../servers/slapd/slap.h"
41 #include "../servers/slapd/slap-config.h"
42 
43 #include "lload.h"
44 #include "lber_pvt.h"
45 
46 #include "ldap_rq.h"
47 
48 ldap_pvt_thread_t lloadd_main_thread;
49 struct lload_conf_info lload_info;
50 
51 void *
52 lload_start_daemon( void *arg )
53 {
54     int rc = 0;
55 
56     daemon_base = event_base_new();
57     if ( !daemon_base ) {
58         Debug( LDAP_DEBUG_ANY, "lload_start_daemon: "
59                 "main event base allocation failed\n" );
60         rc = 1;
61         goto done;
62     }
63 
64     rc = lloadd_daemon( daemon_base );
65 done:
66     if ( rc != LDAP_SUCCESS ) {
67         assert( lloadd_inited == 0 );
68         checked_lock( &lload_wait_mutex );
69         ldap_pvt_thread_cond_signal( &lload_wait_cond );
70         checked_unlock( &lload_wait_mutex );
71     }
72     return (void *)(uintptr_t)rc;
73 }
74 
75 static int
76 lload_pause_cb( BackendInfo *bi )
77 {
78     if ( daemon_base ) {
79         lload_pause_server();
80     }
81     return 0;
82 }
83 
84 static int
85 lload_unpause_cb( BackendInfo *bi )
86 {
87     if ( daemon_base ) {
88         lload_unpause_server();
89     }
90     return 0;
91 }
92 
93 int
94 lload_back_open( BackendInfo *bi )
95 {
96     int rc = 0;
97 
98     if ( slapMode & SLAP_TOOL_MODE ) {
99         return 0;
100     }
101 
102     /* This will fail if we ever try to instantiate more than one lloadd within
103      * the process */
104     epoch_init();
105 
106     if ( lload_tls_init() != 0 ) {
107         return -1;
108     }
109 
110     if ( lload_monitor_open() != 0 ) {
111         return -1;
112     }
113 
114     assert( lloadd_get_listeners() );
115 
116     checked_lock( &lload_wait_mutex );
117     rc = ldap_pvt_thread_create( &lloadd_main_thread,
118             0, lload_start_daemon, NULL );
119     if ( !rc ) {
120         ldap_pvt_thread_cond_wait( &lload_wait_cond, &lload_wait_mutex );
121         if ( lloadd_inited != 1 ) {
122             ldap_pvt_thread_join( lloadd_main_thread, (void *)NULL );
123             rc = -1;
124         }
125     }
126     checked_unlock( &lload_wait_mutex );
127     return rc;
128 }
129 
130 int
131 lload_back_close( BackendInfo *bi )
132 {
133     if ( slapMode & SLAP_TOOL_MODE ) {
134         return 0;
135     }
136 
137     assert( lloadd_inited == 1 );
138 
139     checked_lock( &lload_wait_mutex );
140     event_base_loopexit( daemon_base, NULL );
141     ldap_pvt_thread_cond_wait( &lload_wait_cond, &lload_wait_mutex );
142     checked_unlock( &lload_wait_mutex );
143     ldap_pvt_thread_join( lloadd_main_thread, (void *)NULL );
144 
145     return 0;
146 }
147 
148 int
149 lload_back_initialize( BackendInfo *bi )
150 {
151     bi->bi_flags = SLAP_BFLAG_STANDALONE;
152     bi->bi_open = lload_back_open;
153     bi->bi_config = config_generic_wrapper;
154     bi->bi_pause = lload_pause_cb;
155     bi->bi_unpause = lload_unpause_cb;
156     bi->bi_close = lload_back_close;
157     bi->bi_destroy = 0;
158 
159     bi->bi_db_init = 0;
160     bi->bi_db_config = 0;
161     bi->bi_db_open = 0;
162     bi->bi_db_close = 0;
163     bi->bi_db_destroy = 0;
164 
165     bi->bi_op_bind = 0;
166     bi->bi_op_unbind = 0;
167     bi->bi_op_search = 0;
168     bi->bi_op_compare = 0;
169     bi->bi_op_modify = 0;
170     bi->bi_op_modrdn = 0;
171     bi->bi_op_add = 0;
172     bi->bi_op_delete = 0;
173     bi->bi_op_abandon = 0;
174 
175     bi->bi_extended = 0;
176 
177     bi->bi_chk_referrals = 0;
178 
179     bi->bi_connection_init = 0;
180     bi->bi_connection_destroy = 0;
181 
182     if ( lload_global_init() ) {
183         return -1;
184     }
185 
186     bi->bi_private = &lload_info;
187     return lload_back_init_cf( bi );
188 }
189 
190 SLAP_BACKEND_INIT_MODULE( lload )
191