InfoDirectory使用手引書
|
目次
索引
|
5.5.3.5 非同期型のエントリ削除
非同期型でエントリを削除する場合のサンプルプログラムを以下に示します。
[del.c]
/*
* Copyright (c) 1998. Fujitsu Limited. All rights reserved.
*
* Search the directory for all entry.
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(unix)
#include <time.h>
#include <unistd.h>
#elif defined(_WIN32)
#include <winsock.h>
#endif
#include "examples.h"
unsigned long global_counter = 0;
char *host = "localhost";
int port = LDAP_PORT;
char *binddn = "cn=admin,o=Fujitsu,c=JP";
char *passwd = "admin123";
char *del_dn = "uid=User1,o=Fujitsu,c=JP";
/*-------------------------------------------------------------*/
/*
* Perform other work while polling for results. This doesn't do anything
* useful, but it could.
*/
static void do_other_work()
{
extern unsigned long global_counter;
global_counter++;
}
/*-------------------------------------------------------------*/
int main(
int argc,
char *argv[]
)
{
LDAP *ld;
LDAPMessage *result;
int rtn;
int msgid;
int finished;
struct timeval zerotime;
int optdata;
int errcode;
char *errmsg;
char *mached;
extern char *host;
extern int port;
extern char *binddn;
extern char *passwd;
extern char *del_dn;
/*----------------------------------------------------------*/
printf( "%s is start\n", argv[0] );
/* get a handle to an LDAP connection */
printf( "%s: ldap_init( \"%s\", %d )\n", argv[0], host, port );
ld = ldap_init( host, port );
if ( ld == NULL ) {
perror( "ldap_init" );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
}
/* set option ( LDAP_DEREF_ALWAYS ) */
optdata = LDAP_DEREF_ALWAYS;
printf( "%s: ldap_set_option( LDAP_OPT_DEREF, %d )\n", argv[0], optdata);
rtn = ldap_set_option(ld, LDAP_OPT_DEREF, (void *)(&optdata));
if (rtn != 0 ) {
do_error_msg( "ldap_set_option", rtn, NULL, NULL );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
}
/* set option ( LDAP_VERSION3 ) */
optdata = LDAP_VERSION3;
printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n",argv[0], optdata);
rtn = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)(&optdata));
if ( rtn != 0 ) {
do_error_msg( "ldap_set_option", rtn, NULL, NULL );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
}
/* authenticate to the directory as the Directory Manager */
printf( "%s: ldap_simple_bind_s( \"%s\", \"******\" )\n", argv[0], binddn);
rtn = ldap_simple_bind_s( ld, binddn, passwd );
if ( rtn != LDAP_SUCCESS ) {
do_error_msg( "ldap_simple_bind_s", rtn, NULL, NULL );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
}
/* Initiate the delete operation */
printf( "%s: ldap_delete( \"%s\" )\n", argv[0], del_dn );
msgid = ldap_delete( ld, del_dn );
if ( msgid < 0 ) {
do_get_ldaperror( ld, &errcode, &errmsg );
do_error_msg( "ldap_delete", errcode, NULL, errmsg );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
}
/* Poll for the result */
finished = 0;
while ( finished == 0 ) {
printf( "%s: ldap_result( )\n", argv[0] );
result = NULL;
zerotime.tv_sec = zerotime.tv_usec = 0L;
rtn = ldap_result( ld, msgid, LDAP_MSG_ALL, &zerotime, &result);
switch ( rtn ) {
case -1:
/* some error occurred */
if ( result != NULL ) {
errcode = NULL;
mached = NULL;
errmsg = NULL;
ldap_parse_result( ld, result, &errcode, &mached, &errmsg,0, 0, 0 );
do_error_msg( "ldap_result", errcode, mached, errmsg );
}
printf( "%s: Entry delete error.\n", argv[0] );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
exit( 1 );
case 0:
/* Timeout was exceeded. No entries are ready for retrieval */
#if defined(unix)
sleep(1);
#elif defined(_WIN32)
Sleep(1*1000);
#endif
break;
default:
/* Should be finished here */
finished = 1;
errcode = NULL;
mached = NULL;
errmsg = NULL;
rtn = ldap_parse_result( ld, result, &errcode, &mached, &errmsg,0 , 0, 0 );
if ( rtn == LDAP_SUCCESS ) {
printf( "%s: Entry deleted successfully." " I counted to %ld whilewaiting.\n", argv[0], global_counter);
}
else {
printf( "%s: Error while deleting entry.\n", argv[0]);
do_error_msg( "ldap_result", errcode, mached, errmsg );
}
ldap_msgfree( result );
}
do_other_work();
}
do_unbind( ld, argv );
printf( "%s is normal end\n", argv[0] );
exit( 0 );
}
All Rights Reserved, Copyright(C) 富士通株式会社 2005