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