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