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