]> git.sur5r.net Git - openldap/blob - servers/slapd/back-perl/init.c
Consolidated static/dynamic backend switches
[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 <EXTERN.h>
19 #include <perl.h>
20 #undef _ /* #defined by both Perl and ac/localize.h */
21
22 #ifdef HAVE_WIN32_ASPERL
23 #include "asperl_undefs.h"
24 #endif
25
26 #include "portable.h"
27         
28 #include <stdio.h>
29
30 #include "slap.h"
31
32 #include "perl_back.h"
33
34
35 static void perl_back_xs_init LDAP_P((PERL_BACK_XS_INIT_PARAMS));
36 EXT void boot_DynaLoader LDAP_P((PERL_BACK_BOOT_DYNALOADER_PARAMS));
37
38 PerlInterpreter *PERL_INTERPRETER = NULL;
39 ldap_pvt_thread_mutex_t perl_interpreter_mutex;
40
41 #if SLAPD_PERL == SLAPD_MOD_DYNAMIC
42
43 int init_module(int argc, char *argv[])
44 {
45         BackendInfo bi;
46
47         memset( &bi, '\0', sizeof(bi) );
48         bi.bi_type = "perl";
49         bi.bi_init = perl_back_initialize;
50
51         backend_add(&bi);
52         return 0;
53 }
54
55 #endif /* SLAPD_PERL */
56
57
58 /**********************************************************
59  *
60  * Init
61  *
62  **********************************************************/
63
64 int
65 perl_back_initialize(
66         BackendInfo     *bi
67 )
68 {
69         char *embedding[] = { "", "-e", "0" };
70
71         Debug( LDAP_DEBUG_TRACE, "perl backend open\n", 0, 0, 0 );
72
73         if( PERL_INTERPRETER != NULL ) {
74                 Debug( LDAP_DEBUG_ANY, "perl backend open: already opened\n",
75                         0, 0, 0 );
76                 return 1;
77         }
78         
79         PERL_INTERPRETER = perl_alloc();
80         perl_construct(PERL_INTERPRETER);
81         perl_parse(PERL_INTERPRETER, perl_back_xs_init, 3, embedding, (char **)NULL);
82         perl_run(PERL_INTERPRETER);
83
84         bi->bi_open = perl_back_open;
85         bi->bi_config = 0;
86         bi->bi_close = perl_back_close;
87         bi->bi_destroy = perl_back_destroy;
88
89         bi->bi_db_init = perl_back_db_init;
90         bi->bi_db_config = perl_back_db_config;
91         bi->bi_db_open = perl_back_db_open;
92         bi->bi_db_close = 0;
93         bi->bi_db_destroy = perl_back_db_destroy;
94
95         bi->bi_op_bind = perl_back_bind;
96         bi->bi_op_unbind = 0;
97         bi->bi_op_search = perl_back_search;
98         bi->bi_op_compare = perl_back_compare;
99         bi->bi_op_modify = perl_back_modify;
100         bi->bi_op_modrdn = perl_back_modrdn;
101         bi->bi_op_add = perl_back_add;
102         bi->bi_op_delete = perl_back_delete;
103         bi->bi_op_abandon = 0;
104
105         bi->bi_extended = 0;
106
107         bi->bi_chk_referrals = 0;
108
109         bi->bi_connection_init = 0;
110         bi->bi_connection_destroy = 0;
111
112         return 0;
113 }
114                 
115 int
116 perl_back_open(
117         BackendInfo     *bi
118 )
119 {
120         ldap_pvt_thread_mutex_init( &perl_interpreter_mutex );
121         return 0;
122 }
123
124 int
125 perl_back_db_init(
126         BackendDB       *be
127 )
128 {
129         be->be_private = (PerlBackend *) ch_malloc( sizeof(PerlBackend) );
130         memset( be->be_private, '\0', sizeof(PerlBackend));
131
132         ((PerlBackend *)be->be_private)->pb_filter_search_results = 0;
133
134         Debug( LDAP_DEBUG_TRACE, "perl backend db init\n", 0, 0, 0 );
135
136         return 0;
137 }
138
139 int
140 perl_back_db_open(
141         BackendDB       *be
142 )
143 {
144         int count;
145         int return_code;
146
147         PerlBackend *perl_back = (PerlBackend *) be->be_private;
148
149         ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );
150
151         {
152                 dSP; ENTER; SAVETMPS;
153
154                 PUSHMARK(sp);
155                 XPUSHs( perl_back->pb_obj_ref );
156
157                 PUTBACK;
158
159 #ifdef PERL_IS_5_6
160                 count = call_method("init", G_SCALAR);
161 #else
162                 count = perl_call_method("init", G_SCALAR);
163 #endif
164
165                 SPAGAIN;
166
167                 if (count != 1) {
168                         croak("Big trouble in perl_back_db_open\n");
169                 }
170
171                 return_code = POPi;
172
173                 PUTBACK; FREETMPS; LEAVE;
174         }
175
176         ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
177
178         return return_code;
179 }
180
181
182 static void
183 perl_back_xs_init(PERL_BACK_XS_INIT_PARAMS)
184 {
185         char *file = __FILE__;
186         dXSUB_SYS;
187         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
188 }