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