1998-08-09 08:43:13 +08:00
|
|
|
/* id.c - keep track of the next id to be given out */
|
1999-08-07 07:07:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/param.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "slap.h"
|
|
|
|
#include "back-ldbm.h"
|
|
|
|
|
1999-08-16 15:09:28 +08:00
|
|
|
/* All functions except put_nextid() obey slapMode == SLAP_TOOL_MODE. */
|
1999-08-08 15:28:44 +08:00
|
|
|
|
1999-04-30 06:26:58 +08:00
|
|
|
static ID next_id_read( Backend *be );
|
|
|
|
static ID next_id_get_save( Backend *be, int do_save );
|
|
|
|
|
1999-08-08 15:28:44 +08:00
|
|
|
#define next_id_write( be, id ) \
|
1999-08-16 15:09:28 +08:00
|
|
|
(slapMode == SLAP_TOOL_MODE ? (be, id, 0) : put_nextid( be, id ))
|
1999-04-30 06:26:58 +08:00
|
|
|
|
1999-01-22 09:40:39 +08:00
|
|
|
static ID
|
|
|
|
next_id_read( Backend *be )
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
ID id;
|
|
|
|
char buf[20];
|
|
|
|
char* file = li->li_nextid_file;
|
|
|
|
FILE* fp;
|
|
|
|
|
1999-08-16 15:09:28 +08:00
|
|
|
if ( slapMode == SLAP_TOOL_MODE )
|
1999-04-30 13:24:16 +08:00
|
|
|
return NOID;
|
|
|
|
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( (fp = fopen( file, "r" )) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"next_id_read: could not open \"%s\"\n",
|
|
|
|
file, 0, 0 );
|
|
|
|
return NOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( fgets( buf, sizeof(buf), fp ) == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"next_id_read: could not fgets nextid from \"%s\"\n",
|
|
|
|
file, 0, 0 );
|
|
|
|
fclose( fp );
|
|
|
|
return NOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = atol( buf );
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
if(id < 1) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
1999-02-12 02:19:52 +08:00
|
|
|
"next_id_read %ld: atol(%s) return non-positive integer\n",
|
1999-01-22 09:40:39 +08:00
|
|
|
id, buf, 0 );
|
|
|
|
return NOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
1999-08-08 15:28:44 +08:00
|
|
|
int
|
|
|
|
put_nextid( Backend *be, ID id )
|
1999-01-22 09:40:39 +08:00
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
char* file = li->li_nextid_file;
|
|
|
|
FILE* fp;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ( (fp = fopen( file, "w" )) == NULL ) {
|
1999-08-08 15:28:44 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "put_nextid(%ld): could not open \"%s\"\n",
|
1999-01-22 09:40:39 +08:00
|
|
|
id, file, 0 );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
if ( fprintf( fp, "%ld\n", id ) == EOF ) {
|
1999-08-08 15:28:44 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "put_nextid(%ld): cannot fprintf\n",
|
1999-01-22 09:40:39 +08:00
|
|
|
id, 0, 0 );
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fclose( fp ) != 0 ) {
|
1999-08-08 15:28:44 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "put_nextid %ld: cannot fclose\n",
|
1999-01-22 09:40:39 +08:00
|
|
|
id, 0, 0 );
|
|
|
|
rc = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-01-22 10:26:19 +08:00
|
|
|
int
|
|
|
|
next_id_save( Backend *be )
|
|
|
|
{
|
1999-04-30 06:26:58 +08:00
|
|
|
return( next_id_get_save( be, 1 ) == NOID ? -1 : 0 );
|
1999-01-22 10:26:19 +08:00
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
ID
|
|
|
|
next_id( Backend *be )
|
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
ID id;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* first time in here since startup - try to read the nexid */
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
|
|
|
li->li_nextid = next_id_read( be );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
|
|
|
li->li_nextid = 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-22 10:26:19 +08:00
|
|
|
|
|
|
|
#if SLAPD_NEXTID_CHUNK > 1
|
|
|
|
li->li_nextid_wrote = li->li_nextid;
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1998-11-26 07:45:57 +08:00
|
|
|
id = li->li_nextid++;
|
1999-01-22 10:26:19 +08:00
|
|
|
|
|
|
|
#if SLAPD_NEXTID_CHUNK > 1
|
|
|
|
if ( li->li_nextid > li->li_nextid_wrote ) {
|
|
|
|
li->li_nextid_wrote += SLAPD_NEXTID_CHUNK;
|
|
|
|
(void) next_id_write( be, li->li_nextid_wrote );
|
|
|
|
}
|
|
|
|
#else
|
1999-01-22 09:40:39 +08:00
|
|
|
(void) next_id_write( be, li->li_nextid );
|
1999-01-22 10:26:19 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( id );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
next_id_return( Backend *be, ID id )
|
|
|
|
{
|
1999-01-22 10:26:19 +08:00
|
|
|
#ifdef SLAPD_NEXTID_RETURN
|
1998-08-09 08:43:13 +08:00
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( id != li->li_nextid - 1 ) {
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
li->li_nextid--;
|
1999-01-22 10:26:19 +08:00
|
|
|
|
1999-04-30 02:26:25 +08:00
|
|
|
#if !( SLAPD_NEXTID_CHUNK > 1 )
|
1999-01-22 09:40:39 +08:00
|
|
|
(void) next_id_write( be, li->li_nextid );
|
1999-01-22 10:26:19 +08:00
|
|
|
#endif
|
1999-01-22 09:40:39 +08:00
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
1999-01-22 09:51:53 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ID
|
|
|
|
next_id_get( Backend *be )
|
1999-04-30 06:26:58 +08:00
|
|
|
{
|
|
|
|
return next_id_get_save( be, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static ID
|
|
|
|
next_id_get_save( Backend *be, int do_save )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
|
|
|
|
ID id;
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &li->li_nextid_mutex );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* first time in here since startup - try to read the nexid */
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
|
|
|
li->li_nextid = next_id_read( be );
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-01-22 09:40:39 +08:00
|
|
|
if ( li->li_nextid == NOID ) {
|
|
|
|
li->li_nextid = 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-01-22 10:26:19 +08:00
|
|
|
|
|
|
|
#if SLAPD_NEXTID_CHUNK > 1
|
|
|
|
li->li_nextid_wrote = li->li_nextid;
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
id = li->li_nextid;
|
1998-11-26 07:45:57 +08:00
|
|
|
|
1999-04-30 06:26:58 +08:00
|
|
|
if ( do_save ) {
|
|
|
|
if ( next_id_write( be, id ) == 0 ) {
|
|
|
|
li->li_nextid_wrote = id;
|
|
|
|
} else {
|
|
|
|
id = NOID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &li->li_nextid_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( id );
|
|
|
|
}
|