]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/init.c
rework static backend initialization
[openldap] / servers / slapd / back-perl / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 John C. Quillan.
6  * Portions Copyright 2002 myinternet Limited.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "perl_back.h"
19 #include "external.h"
20
21 static void perl_back_xs_init LDAP_P((PERL_BACK_XS_INIT_PARAMS));
22 EXT void boot_DynaLoader LDAP_P((PERL_BACK_BOOT_DYNALOADER_PARAMS));
23
24 PerlInterpreter *PERL_INTERPRETER = NULL;
25 ldap_pvt_thread_mutex_t perl_interpreter_mutex;
26
27 #if SLAPD_PERL == SLAPD_MOD_DYNAMIC
28
29 int init_module(int argc, char *argv[])
30 {
31         BackendInfo bi;
32
33         memset( &bi, '\0', sizeof(bi) );
34         bi.bi_type = "perl";
35         bi.bi_init = perl_back_initialize;
36
37         backend_add(&bi);
38         return 0;
39 }
40
41 #endif /* SLAPD_PERL */
42
43
44 /**********************************************************
45  *
46  * Init
47  *
48  **********************************************************/
49
50 int
51 perl_back_initialize(
52         BackendInfo     *bi
53 )
54 {
55         char *embedding[] = { "", "-e", "0" };
56
57         Debug( LDAP_DEBUG_TRACE, "perl backend open\n", 0, 0, 0 );
58
59         if( PERL_INTERPRETER != NULL ) {
60                 Debug( LDAP_DEBUG_ANY, "perl backend open: already opened\n",
61                         0, 0, 0 );
62                 return 1;
63         }
64         
65         PERL_INTERPRETER = perl_alloc();
66         perl_construct(PERL_INTERPRETER);
67         perl_parse(PERL_INTERPRETER, perl_back_xs_init, 3, embedding, (char **)NULL);
68         perl_run(PERL_INTERPRETER);
69
70         bi->bi_open = perl_back_open;
71         bi->bi_config = 0;
72         bi->bi_close = perl_back_close;
73         bi->bi_destroy = perl_back_destroy;
74
75         bi->bi_db_init = perl_back_db_init;
76         bi->bi_db_config = perl_back_db_config;
77         bi->bi_db_open = perl_back_db_open;
78         bi->bi_db_close = 0;
79         bi->bi_db_destroy = perl_back_db_destroy;
80
81         bi->bi_op_bind = perl_back_bind;
82         bi->bi_op_unbind = 0;
83         bi->bi_op_search = perl_back_search;
84         bi->bi_op_compare = perl_back_compare;
85         bi->bi_op_modify = perl_back_modify;
86         bi->bi_op_modrdn = perl_back_modrdn;
87         bi->bi_op_add = perl_back_add;
88         bi->bi_op_delete = perl_back_delete;
89         bi->bi_op_abandon = 0;
90
91         bi->bi_extended = 0;
92
93         bi->bi_chk_referrals = 0;
94
95         bi->bi_connection_init = 0;
96         bi->bi_connection_destroy = 0;
97
98         return 0;
99 }
100                 
101 int
102 perl_back_open(
103         BackendInfo     *bi
104 )
105 {
106         ldap_pvt_thread_mutex_init( &perl_interpreter_mutex );
107         return 0;
108 }
109
110 int
111 perl_back_db_init(
112         BackendDB       *be
113 )
114 {
115         be->be_private = (PerlBackend *) ch_malloc( sizeof(PerlBackend) );
116         memset( be->be_private, '\0', sizeof(PerlBackend));
117
118         ((PerlBackend *)be->be_private)->pb_filter_search_results = 0;
119
120         Debug( LDAP_DEBUG_TRACE, "perl backend db init\n", 0, 0, 0 );
121
122         return 0;
123 }
124
125 int
126 perl_back_db_open(
127         BackendDB       *be
128 )
129 {
130         int count;
131         int return_code;
132
133         PerlBackend *perl_back = (PerlBackend *) be->be_private;
134
135         ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );
136
137         {
138                 dSP; ENTER; SAVETMPS;
139
140                 PUSHMARK(sp);
141                 XPUSHs( perl_back->pb_obj_ref );
142
143                 PUTBACK;
144
145 #ifdef PERL_IS_5_6
146                 count = call_method("init", G_SCALAR);
147 #else
148                 count = perl_call_method("init", G_SCALAR);
149 #endif
150
151                 SPAGAIN;
152
153                 if (count != 1) {
154                         croak("Big trouble in perl_back_db_open\n");
155                 }
156
157                 return_code = POPi;
158
159                 PUTBACK; FREETMPS; LEAVE;
160         }
161
162         ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
163
164         return return_code;
165 }
166
167
168 static void
169 perl_back_xs_init(PERL_BACK_XS_INIT_PARAMS)
170 {
171         char *file = __FILE__;
172         dXSUB_SYS;
173         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
174 }