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