1 /* Common database routines for nss_db.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
30 /* This file contains the functions used to open and close the databases
31 read by the rest of libnss_db. Not all of them are thread safe;
32 make sure the caller does the appropriate locking.
34 We dynamically load the database library, so that it does not have
35 to be present when glibc is compiled. Once loaded, the database
36 library is never never unloaded again until the libnss_db module is
37 unloaded (from the free_mem routine in nsswitch.c) -- we catch the
38 unload by providing a shlib destructor. (XXX Does that actually
41 /* Handle for the shared Berkeley DB library. If non-null, the
42 database library is completely loaded and ready to be used by
43 multithreaded code. */
44 static void *libdb_handle
;
46 /* The version of the Berkeley DB library we are using. */
54 /* Pointer to the db_open function. For use with DB 2.x. */
55 static int (*libdb_db_open
) (const char *, int,
56 uint32_t, int, void *, void *, void **);
58 /* Pointer to the db_create function. For use with DB 3.x. */
59 static int (*libdb_db_create
) (void *, void *, uint32_t);
61 /* Constants which vary from version to version are actually variables
68 /* Variables which keep track of the error values. */
72 /* Locks the static variables in this file. */
73 __libc_lock_define_initialized (static, lock
)
75 /* Dynamically load the database library. Return zero if successful,
76 non-zero if no suitable version of the library could be loaded.
77 Must be called with the above lock held if it might run in a
78 multithreaded context.
81 - libdb.so.3: the name used by glibc 2.1
82 - libdb-3.0.so: the name used by db-3.0.x
83 and maybe others in the future. */
88 static const char *libnames
[] = { "libdb.so.3", "libdb-3.0.so" };
91 for (x
= 0; x
< sizeof (libnames
) / sizeof (libnames
[0]); ++x
)
93 libdb_handle
= dlopen (libnames
[x
], RTLD_LAZY
);
94 if (libdb_handle
== NULL
)
97 /* DB 3.0 has db_create instead of db_open. */
98 libdb_db_create
= dlsym (libdb_handle
, "db_create");
100 if (libdb_db_create
== NULL
)
101 /* DB 2.x uses db_open. */
102 libdb_db_open
= dlsym (libdb_handle
, "db_open");
104 if (libdb_db_open
!= NULL
|| libdb_db_create
!= NULL
)
106 /* Alright, we got a library. Now find out which version it is. */
107 const char *(*db_version
) (int *, int *, int *);
109 db_version
= dlsym (libdb_handle
, "db_version");
110 if (db_version
!= NULL
)
112 /* Call the function and get the information. */
113 int major
, minor
, subminor
;
115 DL_CALL_FCT (db_version
, (&major
, &minor
, &subminor
));
119 /* Sanity check: Do we have db_open? */
120 if (libdb_db_open
!= NULL
)
122 if (minor
< 6 || (minor
== 6 && subminor
< 4))
124 libdb_version
= db24
;
125 db_first
= DB24_FIRST
;
127 db_nooverwrite
= DB24_NOOVERWRITE
;
128 db_truncate
= DB24_TRUNCATE
;
132 libdb_version
= db27
;
133 db_first
= DB27_FIRST
;
135 db_nooverwrite
= DB27_NOOVERWRITE
;
136 db_truncate
= DB27_TRUNCATE
;
138 db_keyexist
= DB2x_KEYEXIST
;
139 db_notfound
= DB2x_NOTFOUND
;
140 db_rdonly
= DB2x_RDONLY
;
145 /* Sanity check: Do we have db_create? */
146 if (libdb_db_create
!= NULL
)
148 libdb_version
= db30
;
149 db_first
= DB30_FIRST
;
151 db_keyexist
= DB30_KEYEXIST
;
152 db_notfound
= DB30_NOTFOUND
;
153 db_rdonly
= DB30_RDONLY
;
162 if (libdb_version
!= nodb
)
163 return NSS_STATUS_SUCCESS
;
165 /* Clear variables. */
166 libdb_db_open
= NULL
;
167 libdb_db_create
= NULL
;
170 dlclose (libdb_handle
);
174 return NSS_STATUS_UNAVAIL
;
177 /* Set the `FD_CLOEXEC' flag of FD. Return 0 on success, or -1 on
178 error with `errno' set. */
180 set_cloexec_flag (int fd
)
182 int oldflags
= fcntl (fd
, F_GETFD
, 0);
187 oldflags
|= FD_CLOEXEC
;
189 return fcntl (fd
, F_SETFD
, oldflags
);
192 /* Make sure we don't use the library anymore once we are shutting down. */
193 static void __attribute__ ((destructor
))
196 if (libdb_handle
!= NULL
)
198 libdb_db_open
= NULL
;
199 libdb_db_create
= NULL
;
200 libdb_version
= nodb
;
201 dlclose (libdb_handle
);
205 /* Open the database stored in FILE. If succesful, store the database
206 handle in *DBP and return NSS_STATUS_SUCCESS. On failure, return
207 the appropriate lookup status. */
209 internal_setent (const char *file
, NSS_DB
**dbp
)
211 enum nss_status status
= NSS_STATUS_SUCCESS
;
215 if (libdb_db_open
== NULL
&& libdb_db_create
== NULL
)
217 __libc_lock_lock (lock
);
219 if (libdb_db_open
== NULL
&& libdb_db_create
== NULL
)
222 __libc_lock_unlock (lock
);
225 if (status
== NSS_STATUS_SUCCESS
)
226 status
= dbopen (file
, db_rdonly
, 0, dbp
);
233 /* Close the database *DBP. */
235 internal_endent (NSS_DB
**dbp
)
241 DL_CALL_FCT (db
->close
, (db
->db
, 0));
246 /* Allocate a cursor for database DB and transaction TXN. On success,
247 store the cursor in *DBCP and return zero. Otherwise return an
250 db_cursor (void *db
, void *txn
, NSS_DBC
**dbcp
)
255 dbc
= (NSS_DBC
*) malloc (sizeof (NSS_DBC
));
259 switch (libdb_version
)
262 ret
= ((struct db24
*) db
)->cursor (db
, txn
, &dbc
->cursor
);
265 dbc
->c_get
= ((struct dbc24
*) dbc
->cursor
)->c_get
;
269 ret
= ((struct db27
*) db
)->cursor (db
, txn
, &dbc
->cursor
, 0);
272 dbc
->c_get
= ((struct dbc27
*) dbc
->cursor
)->c_get
;
276 ret
= ((struct db30
*) db
)->cursor (db
, txn
, &dbc
->cursor
, 0);
279 dbc
->c_get
= ((struct dbc30
*) dbc
->cursor
)->c_get
;
298 /* Open the database in FNAME, for access specified by FLAGS. If
299 opening the database causes the file FNAME to be created, it is
300 created with MODE. If succesful, store the database handle in *DBP
301 and return NSS_STATUS_SUCCESS. On failure, return the appropriate
304 dbopen (const char *fname
, int oper
, int mode
, NSS_DB
**dbp
)
310 /* Construct the object we pass up. */
311 db
= (NSS_DB
*) calloc (1, sizeof (NSS_DB
));
313 return NSS_STATUS_UNAVAIL
;
315 /* Initialize the object. */
316 db
->cursor
= db_cursor
;
318 /* Actually open the database. */
319 switch (libdb_version
)
323 err
= DL_CALL_FCT (libdb_db_open
,
324 (fname
, DB_BTREE
, oper
, mode
, NULL
, NULL
, &db
->db
));
330 db
->close
= ((struct db24
*) db
->db
)->close
;
331 db
->fd
= ((struct db24
*) db
->db
)->fd
;
332 db
->get
= ((struct db24
*) db
->db
)->get
;
333 db
->put
= ((struct db24
*) db
->db
)->put
;
337 db
->close
= ((struct db27
*) db
->db
)->close
;
338 db
->fd
= ((struct db27
*) db
->db
)->fd
;
339 db
->get
= ((struct db27
*) db
->db
)->get
;
340 db
->put
= ((struct db27
*) db
->db
)->put
;
345 err
= DL_CALL_FCT (libdb_db_create
, (db
->db
, NULL
, 0));
349 db
->close
= ((struct db30
*) db
->db
)->close
;
350 db
->fd
= ((struct db30
*) db
->db
)->fd
;
351 db
->get
= ((struct db30
*) db
->db
)->get
;
352 db
->put
= ((struct db30
*) db
->db
)->put
;
354 err
= ((struct db30
*) db
->db
)->open (db
->db
, fname
, NULL
, DB_BTREE
,
364 /* We have to make sure the file is `closed on exec'. */
365 err
= DL_CALL_FCT (db
->fd
, (db
->db
, &fd
));
368 if (set_cloexec_flag (fd
) < 0)
373 return NSS_STATUS_UNAVAIL
;
376 /* Something went wrong. Close the database if necessary. */
379 if (db
->db
&& db
->close
)
380 DL_CALL_FCT (db
->close
, (db
->db
, 0));
384 /* Make sure `errno' is set. */
388 return err
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;