summaryrefslogtreecommitdiffstats
path: root/servers/lloadd/module_init.c
blob: a122a529625bf15d766a60df0c1bf6c00dfb542f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* module_init.c - module initialization functions */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2022 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

#include "../servers/slapd/slap.h"
#include "../servers/slapd/slap-config.h"

#include "lload.h"
#include "lber_pvt.h"

#include "ldap_rq.h"

ldap_pvt_thread_t lloadd_main_thread;
struct lload_conf_info lload_info;

void *
lload_start_daemon( void *arg )
{
    int rc = 0;

    daemon_base = event_base_new();
    if ( !daemon_base ) {
        Debug( LDAP_DEBUG_ANY, "lload_start_daemon: "
                "main event base allocation failed\n" );
        rc = 1;
        goto done;
    }

    rc = lloadd_daemon( daemon_base );
done:
    if ( rc != LDAP_SUCCESS ) {
        assert( lloadd_inited == 0 );
        checked_lock( &lload_wait_mutex );
        ldap_pvt_thread_cond_signal( &lload_wait_cond );
        checked_unlock( &lload_wait_mutex );
    }
    return (void *)(uintptr_t)rc;
}

static int
lload_pause_cb( BackendInfo *bi )
{
    if ( daemon_base ) {
        lload_pause_server();
    }
    return 0;
}

static int
lload_unpause_cb( BackendInfo *bi )
{
    if ( daemon_base ) {
        lload_unpause_server();
    }
    return 0;
}

int
lload_back_open( BackendInfo *bi )
{
    int rc = 0;

    if ( slapMode & SLAP_TOOL_MODE ) {
        return 0;
    }

    /* This will fail if we ever try to instantiate more than one lloadd within
     * the process */
    epoch_init();

    if ( lload_tls_init() != 0 ) {
        return -1;
    }

    if ( lload_monitor_open() != 0 ) {
        return -1;
    }

    assert( lloadd_get_listeners() );

    checked_lock( &lload_wait_mutex );
    rc = ldap_pvt_thread_create( &lloadd_main_thread,
            0, lload_start_daemon, NULL );
    if ( !rc ) {
        ldap_pvt_thread_cond_wait( &lload_wait_cond, &lload_wait_mutex );
        if ( lloadd_inited != 1 ) {
            ldap_pvt_thread_join( lloadd_main_thread, (void *)NULL );
            rc = -1;
        }
    }
    checked_unlock( &lload_wait_mutex );
    return rc;
}

int
lload_back_close( BackendInfo *bi )
{
    if ( slapMode & SLAP_TOOL_MODE ) {
        return 0;
    }

    assert( lloadd_inited == 1 );

    checked_lock( &lload_wait_mutex );
    event_base_loopexit( daemon_base, NULL );
    ldap_pvt_thread_cond_wait( &lload_wait_cond, &lload_wait_mutex );
    checked_unlock( &lload_wait_mutex );
    ldap_pvt_thread_join( lloadd_main_thread, (void *)NULL );

    return 0;
}

int
lload_back_destroy( BackendInfo *bi )
{
    return lload_global_destroy();
}

int
lload_back_initialize( BackendInfo *bi )
{
    bi->bi_flags = SLAP_BFLAG_STANDALONE;
    bi->bi_open = lload_back_open;
    bi->bi_pause = lload_pause_cb;
    bi->bi_unpause = lload_unpause_cb;
    bi->bi_close = lload_back_close;
    bi->bi_destroy = lload_back_destroy;

    bi->bi_db_init = 0;
    bi->bi_db_config = 0;
    bi->bi_db_open = 0;
    bi->bi_db_close = 0;
    bi->bi_db_destroy = 0;

    bi->bi_op_bind = 0;
    bi->bi_op_unbind = 0;
    bi->bi_op_search = 0;
    bi->bi_op_compare = 0;
    bi->bi_op_modify = 0;
    bi->bi_op_modrdn = 0;
    bi->bi_op_add = 0;
    bi->bi_op_delete = 0;
    bi->bi_op_abandon = 0;

    bi->bi_extended = 0;

    bi->bi_chk_referrals = 0;

    bi->bi_connection_init = 0;
    bi->bi_connection_destroy = 0;

    if ( lload_global_init() ) {
        return -1;
    }

    bi->bi_private = &lload_info;
    return lload_back_init_cf( bi );
}

SLAP_BACKEND_INIT_MODULE( lload )