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