| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- #ident "ldclt @(#)threadMain.c 1.40 01/05/04"
- /** BEGIN COPYRIGHT BLOCK
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2006 Red Hat, Inc.
- * All rights reserved.
- *
- * License: GPL (version 3 or any later version).
- * See LICENSE for details.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- /*
- FILE : threadMain.c
- AUTHOR : Jean-Luc SCHWING
- VERSION : 1.0
- DATE : 04 December 1998
- DESCRIPTION :
- This file implements the main/core part of the
- threads. The ldap part is in another source file.
- LOCAL : None.
- HISTORY :
- ---------+--------------+------------------------------------------------------
- dd/mm/yy | Author | Comments
- ---------+--------------+------------------------------------------------------
- 04/12/98 | JL Schwing | Creation
- ---------+--------------+------------------------------------------------------
- 10/12/98 | JL Schwing | 1.2 : Add nb of errors statistics.
- ---------+--------------+------------------------------------------------------
- 10/12/98 | JL Schwing | 1.3 : Implement asynchronous mode.
- ---------+--------------+------------------------------------------------------
- 11/12/98 | JL Schwing | 1.4 : Implement max errors threshold.
- | fflush(stdout) after each printf.
- ---------+--------------+------------------------------------------------------
- 14/12/98 | JL Schwing | 1.5 : Implement "-e close".
- | Add "thread is dead" message.
- ---------+--------------+------------------------------------------------------
- 16/12/98 | JL Schwing | 1.6 : Implement "-e add" and "-e delete".
- ---------+--------------+------------------------------------------------------
- 23/12/98 | JL Schwing | 1.7 : bug fix - crash in msgIdDel().
- ---------+--------------+------------------------------------------------------
- 28/12/98 | JL Schwing | 1.8 : Add tag asyncHit.
- ---------+--------------+------------------------------------------------------
- 13/01/99 | JL Schwing | 1.9 : Implement "-e string".
- ---------+--------------+------------------------------------------------------
- 18/01/99 | JL Schwing | 1.10: Implement "-e randombase".
- ---------+--------------+------------------------------------------------------
- 21/01/99 | JL Schwing | 1.11: Implement "-e ascii".
- ---------+--------------+------------------------------------------------------
- 26/02/99 | JL Schwing | 1.12: Improve strict ascii: reject more characters.
- ---------+--------------+------------------------------------------------------
- 26/02/99 | JL Schwing | 1.13: Quote (aka \) characters rather than reject.
- ---------+--------------+------------------------------------------------------
- 04/05/99 | JL Schwing | 1.14: Modify msgId*() to memorize attribs as well.
- ---------+--------------+------------------------------------------------------
- 19/05/99 | JL Schwing | 1.15: Implement "-e rename".
- | Exit the thread if status==DEAD
- ---------+--------------+------------------------------------------------------
- 28/05/99 | JL Schwing | 1.16: Add new option -W (wait).
- ---------+--------------+------------------------------------------------------
- 06/03/00 | JL Schwing | 1.17: Test malloc() return value.
- ---------+--------------+------------------------------------------------------
- 18/08/00 | JL Schwing | 1.18: Print begin and end dates.
- ---------+--------------+------------------------------------------------------
- 25/08/00 | JL Schwing | 1.19: Implement consistent exit status...
- | Fix some old legacy code.
- ---------+--------------+------------------------------------------------------
- 14/11/00 | JL Schwing | 1.20: Will now use utils.c functions.
- ---------+--------------+------------------------------------------------------
- 17/11/00 | JL Schwing | 1.21: Implement "-e smoothshutdown".
- | Add new functions setThreadStatus() getThreadStatus().
- ---------+--------------+------------------------------------------------------
- 21/11/00 | JL Schwing | 1.22: Implement "-e attreplace=name:mask"
- ---------+--------------+------------------------------------------------------
- 29/11/00 | JL Schwing | 1.23: Port on NT 4.
- ---------+--------------+------------------------------------------------------
- 01/12/00 | JL Schwing | 1.24: Port on Linux.
- ---------+--------------+------------------------------------------------------
- 15/12/00 | JL Schwing | 1.25: Add more trace in VERY_VERBOSE mode.
- ---------+--------------+------------------------------------------------------
- 18/12/00 | JL Schwing | 1.26: Add new exit status EXIT_INIT.
- ---------+--------------+------------------------------------------------------
- 05/01/01 | JL Schwing | 1.27: Implement "-e randombinddn" and associated
- | "-e randombinddnlow/high"
- ---------+--------------+------------------------------------------------------
- 08/01/01 | JL Schwing | 1.28: Implement "-e scalab01".
- ---------+--------------+------------------------------------------------------
- 12/01/01 | JL Schwing | 1.29: Include scalab01.h
- ---------+--------------+------------------------------------------------------
- 05/03/01 | JL Schwing | 1.30: Bug fix - crash SIGSEGV if no binDN provided.
- ---------+--------------+------------------------------------------------------
- 14/03/01 | JL Schwing | 1.31: Implement "-e commoncounter"
- | Add new function incrementCommonCounter().
- ---------+--------------+------------------------------------------------------
- 14/03/01 | JL Schwing | 1.32: Implement "-e dontsleeponserverdown".
- ---------+--------------+------------------------------------------------------
- 15/03/01 | JL Schwing | 1.33: Implement "-e randomattrlist=name:name:name"
- | Add new function selectRandomAttrList().
- ---------+--------------+------------------------------------------------------
- 19/03/01 | JL Schwing | 1.34: Implement "-e genldif=filename"
- ---------+--------------+------------------------------------------------------
- 23/03/01 | JL Schwing | 1.35: Implements "-e rdn=value".
- ---------+--------------+------------------------------------------------------
- 28/03/01 | JL Schwing | 1.36: Support -e commoncounter with -e rdn/object
- | Add new function incrementCommonCounterObject().
- ---------+--------------+------------------------------------------------------
- 02/04/01 | JL Schwing | 1.37: Bug fix : large files support for -e genldif.
- ---------+--------------+------------------------------------------------------
- 11/04/01 | JL Schwing | 1.38: Implement [INCRFROMFILE<NOLOOP>(myfile)]
- ---------+--------------+------------------------------------------------------
- 03/05/01 | JL Schwing | 1.39: Implement -e randombinddnfromfile=filename.
- ---------+--------------+------------------------------------------------------
- 04/05/01 | JL Schwing | 1.40: Implement -e bindonly.
- ---------+--------------+------------------------------------------------------
- */
- #include <stdio.h> /* printf(), etc... */
- #include <string.h> /* strerror(), etc... */
- #include <stdlib.h> /* exit(), etc... */
- #include <ctype.h> /* isascii(), etc... */
- #include <errno.h> /* errno, etc... */ /*JLS 06-03-00*/
- #include <lber.h> /* ldap C-API BER declarations */
- #include <ldap.h> /* ldap C-API declarations */
- #include <unistd.h> /* close(), etc... */
- #include <pthread.h> /* pthreads(), etc... */
- #include <signal.h> /* sigfillset(), etc... */
- #include "port.h" /* Portability definitions */ /*JLS 29-11-00*/
- #include "ldclt.h" /* This tool's include file */
- #include "utils.h" /* Utilities functions */ /*JLS 14-11-00*/
- #include "scalab01.h" /* Scalab01 specific */ /*JLS 12-01-01*/
- /* ****************************************************************************
- FUNCTION : selectRandomAttrList
- PURPOSE : Select a random attr list.
- INPUT : tttctx = this thread context
- OUTPUT : None.
- RETURN : The random list.
- DESCRIPTION :
- *****************************************************************************/
- char **
- selectRandomAttrList (
- thread_context *tttctx)
- {
- tttctx->attrlist[0] = mctx.attrlist[rndlim(0,mctx.attrlistNb-1)];
- return (tttctx->attrlist);
- }
- /* ****************************************************************************
- FUNCTION : randomString
- PURPOSE : Return a random string, of length nbDigits.
- The string is returned in tttctx->buf2.
- INPUT : tttctx = thread context.
- nbDigits = number of digits required.
- OUTPUT : None.
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- randomString (
- thread_context *tttctx,
- int nbDigits)
- {
- rndstr (tttctx->buf2, nbDigits); /*JLS 14-11-00*/
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : incrementCommonCounterObject
- PURPOSE : Purpose of the fct
- INPUT : tttctx = thread context
- field = field to process
- OUTPUT : None.
- RETURN : -1 if error or end of loop (no_loop), new value else.
- DESCRIPTION :
- *****************************************************************************/
- int
- incrementCommonCounterObject (
- thread_context *tttctx,
- vers_field *field)
- {
- int ret; /* Return value */
- int val; /* New value */
- /*
- * Get mutex
- */
- if ((ret = ldclt_mutex_lock (&(field->cnt_mutex))) != 0)
- {
- fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(field->cnt_mutex), error=%d (%s)\n",
- mctx.pid, tttctx->thrdId, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Compute next value
- */
- switch (field->how)
- {
- case HOW_INCR_FROM_FILE:
- case HOW_INCR_NB:
- if (field->cnt <= field->high) /* Limit not reached */
- {
- val = field->cnt;
- field->cnt++;
- }
- else
- {
- val = field->low;
- field->cnt = field->low + 1;
- }
- break;
- case HOW_INCR_FROM_FILE_NL:
- case HOW_INCR_NB_NOLOOP:
- if (field->cnt <= field->high) /* Limit not reached */
- {
- val = field->cnt;
- field->cnt++;
- }
- else
- val = -1; /* Exit thread */
- break;
- default:
- printf ("ldclt[%d]: %s: Illegal how=%d in incrementCommonCounterObject()\n",
- mctx.pid, tttctx->thrdId, field->how);
- val = -1;
- break;
- }
- /*
- * Free mutex
- */
- if ((ret = ldclt_mutex_unlock (&(field->cnt_mutex))) != 0)
- {
- fprintf(stderr,"ldclt[%d]: %s: cannot mutex_unlock(field->cnt_mutex), error=%d (%s)\n",
- mctx.pid, tttctx->thrdId, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Maybe a message to print ?
- */
- if (val < 0)
- printf ("ldclt[%d]: %s: Hit top incrementeal value\n", mctx.pid, tttctx->thrdId);
- return (val);
- }
- /* ****************************************************************************
- FUNCTION : incrementCommonCounter
- PURPOSE : Purpose of the fct
- INPUT : tttctx = thread context
- OUTPUT : None.
- RETURN : -1 if error or end of loop (no_loop), new value else.
- DESCRIPTION :
- *****************************************************************************/
- int
- incrementCommonCounter (
- thread_context *tttctx)
- {
- int ret; /* Return value */
- int val; /* New value */
- /*
- * Get mutex
- */
- if ((ret = ldclt_mutex_lock (&(mctx.lastVal_mutex))) != 0)
- {
- fprintf (stderr, "ldclt[%d]: T%03d: cannot mutex_lock(lastVal_mutex), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Compute next value
- */
- if ((mctx.mode & NOLOOP) && ((mctx.lastVal + mctx.incr) > mctx.randomHigh))
- {
- val = -1; /* out of range - cannot continue since not looping */
- }
- else
- {
- val = mctx.lastVal = incr_and_wrap(mctx.lastVal, mctx.randomLow, mctx.randomHigh, mctx.incr);
- }
- /*
- * Free mutex
- */
- if ((ret = ldclt_mutex_unlock (&(mctx.lastVal_mutex))) != 0)
- {
- fprintf(stderr,"ldclt[%d]: T%03d: cannot mutex_unlock(lastVal_mutex), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Maybe a message to print ?
- */
- if (val < 0)
- printf ("ldclt[%d]: T%03d: Hit top incremental value\n", mctx.pid, tttctx->thrdNum);
- return (val);
- }
- /* ****************************************************************************
- FUNCTION : incrementNbOpers
- PURPOSE : Increment the counters tttctx->nbOpers and
- tttctx->totOpers of the given thread.
- INPUT : tttctx = thread context.
- OUTPUT : None.
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- incrementNbOpers (
- thread_context *tttctx)
- {
- int ret; /* Return value */
- /*
- * Get mutex
- */
- if ((ret = ldclt_mutex_lock (&(tttctx->nbOpers_mutex))) != 0)
- {
- fprintf (stderr, "ldclt[%d]: T%03d: cannot mutex_lock(), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Increment counter
- */
- tttctx->nbOpers++;
- /*
- * Free mutex
- */
- if ((ret = ldclt_mutex_unlock (&(tttctx->nbOpers_mutex))) != 0)
- {
- fprintf (stderr, "ldclt[%d]: T%03d: cannot mutex_unlock(), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Increment total and check if max value reached
- */
- tttctx->totOpers++;
- if(tttctx->totalReq > -1) {
- if (tttctx->totOpers >= tttctx->totalReq) {
- if (setThreadStatus(tttctx, MUST_SHUTDOWN) < 0) {
- tttctx->status = DEAD; /* Force thread to die! */
- }
- }
- }
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : ignoreError
- PURPOSE : Returns true or false depending on the given error
- should be ignored or not (option -I).
- We will sleep() if an error about server down is to be
- ignored.
- INPUT : err = error number
- OUTPUT : None.
- RETURN : 1 if should be ignored, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- ignoreError (
- int err)
- {
- int i;
- for (i=0 ; i<mctx.ignErrNb ; i++)
- if (mctx.ignErr[i] == err)
- { /*JLS 14-03-01*/
- if ((!(mctx.mode & DONT_SLEEP_DOWN)) && /*JLS 14-03-01*/
- ((err == LDAP_SERVER_DOWN) || /*JLS 14-03-01*/
- (err == LDAP_CONNECT_ERROR))) /*JLS 14-03-01*/
- ldclt_sleep (1); /*JLS 14-03-01*/
- return (1);
- } /*JLS 14-03-01*/
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : addErrorStat
- PURPOSE : Add the given error number to the statistics.
- INPUT : err = error number
- OUTPUT : None.
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- addErrorStat (
- int err)
- {
- int ret; /* Return value */
- /*
- * Get mutex
- */
- if ((ret = ldclt_mutex_lock (&(mctx.errors_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_lock(errors_mutex), error=%d (%s)\n",
- mctx.pid, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Update the counters
- */
- #if defined(USE_OPENLDAP)
- if ((err <= NEGATIVE_MAX_ERROR_NB) || (err >= MAX_ERROR_NB))
- #else
- if ((err <= 0) || (err >= MAX_ERROR_NB))
- #endif
- {
- fprintf (stderr, "ldclt[%d]: Illegal error number %d\n", mctx.pid, err);
- fflush (stderr);
- mctx.errorsBad++;
- }
- #if defined(USE_OPENLDAP)
- else if (err < 0)
- {
- mctx.negativeErrors[abs(err)]++;
- }
- #endif
- else
- {
- mctx.errors[err]++;
- }
- /*
- * Release the mutex
- */
- if ((ret = ldclt_mutex_unlock (&(mctx.errors_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_unlock(errors_mutex), error=%d (%s)\n",
- mctx.pid, ret, strerror (ret));
- fflush (stderr);
- return (-1);
- }
- /*
- * Maybe we should ignore this error ?
- */
- if (!(ignoreError (err)))
- {
- /*
- * Ok, we should not ignore this error...
- * Maybe the limit is reached ?
- */
- #if defined(USE_OPENLDAP)
- if ((err <= NEGATIVE_MAX_ERROR_NB) || (err >= MAX_ERROR_NB))
- #else
- if ((err <= 0) || (err >= MAX_ERROR_NB))
- #endif
- {
- if (mctx.errorsBad > mctx.maxErrors) {
- printf ("ldclt[%d]: Max error limit reached - exiting.\n", mctx.pid);
- (void) printGlobalStatistics(); /*JLS 25-08-00*/
- fflush (stdout);
- ldclt_sleep (5);
- ldcltExit (EXIT_MAX_ERRORS); /*JLS 25-08-00*/
- }
- } else {
- if (mctx.errors[err] + mctx.negativeErrors[abs(err)] > mctx.maxErrors) {
- printf ("ldclt[%d]: Max error limit reached - exiting.\n", mctx.pid);
- (void) printGlobalStatistics(); /*JLS 25-08-00*/
- fflush (stdout);
- ldclt_sleep (5);
- ldcltExit (EXIT_MAX_ERRORS); /*JLS 25-08-00*/
- }
- }
- }
- /*
- * Normal end
- */
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : msgIdAdd
- PURPOSE : Add a new message id to the pending ones.
- INPUT : tttctx = thread's context.
- msgid = message id.
- str = free string.
- dn = dn of the entry
- attribs = attributes
- OUTPUT : None.
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- msgIdAdd (
- thread_context *tttctx,
- int msgid,
- char *str,
- char *dn,
- LDAPMod **attribs)
- {
- if (mctx.mode & VERY_VERBOSE)
- printf ("ldclt[%d]: T%03d: msgIdAdd (%d, %s)\n", mctx.pid, tttctx->thrdNum, msgid, str);
- /*
- * Add the new cell
- */
- if (tttctx->firstMsgId == NULL)
- {
- tttctx->firstMsgId = (msgid_cell *) malloc (sizeof (msgid_cell));
- if (tttctx->firstMsgId == NULL) /*JLS 06-03-00*/
- { /*JLS 06-03-00*/
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->firstMsgId), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- return (-1); /*JLS 06-03-00*/
- } /*JLS 06-03-00*/
- tttctx->lastMsgId = tttctx->firstMsgId;
- }
- else
- {
- tttctx->lastMsgId->next = (msgid_cell *) malloc (sizeof (msgid_cell));
- if (tttctx->lastMsgId->next == NULL) /*JLS 06-03-00*/
- { /*JLS 06-03-00*/
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->lastMsgId->next), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- return (-1); /*JLS 06-03-00*/
- } /*JLS 06-03-00*/
- tttctx->lastMsgId = tttctx->lastMsgId->next;
- }
- /*
- * Memorize the information
- */
- tttctx->lastMsgId->next = NULL;
- tttctx->lastMsgId->msgid = msgid;
- strncpy (tttctx->lastMsgId->str, str, sizeof(tttctx->lastMsgId->str));
- tttctx->lastMsgId->str[sizeof(tttctx->lastMsgId->str)-1] = '\0';
- strncpy (tttctx->lastMsgId->dn, dn, sizeof(tttctx->lastMsgId->dn));
- tttctx->lastMsgId->dn[sizeof(tttctx->lastMsgId->dn)-1] = '\0';
- tttctx->lastMsgId->attribs = attribs;
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : msgIdAttribs
- PURPOSE : Found the requested message id in the pending list.
- INPUT : tttctx = thread's context
- msgid = message id
- OUTPUT : None
- RETURN : The associated attributes, or NULL.
- DESCRIPTION :
- *****************************************************************************/
- LDAPMod **
- msgIdAttribs (
- thread_context *tttctx,
- int msgid)
- {
- msgid_cell *pt;
- if (mctx.mode & VERY_VERBOSE)
- printf ("ldclt[%d]: T%03d: msgIdAttribs (%d)\n", mctx.pid, tttctx->thrdNum, msgid);
- for (pt = tttctx->firstMsgId ; pt != NULL ; pt = pt->next)
- if (pt->msgid == msgid)
- return (pt->attribs);
- return (NULL);
- }
- /* ****************************************************************************
- FUNCTION : msgIdDN
- PURPOSE : Found the requested message id in the pending list.
- INPUT : tttctx = thread's context
- msgid = message id
- OUTPUT : None
- RETURN : The associated DN, or NULL.
- DESCRIPTION :
- *****************************************************************************/
- char *
- msgIdDN (
- thread_context *tttctx,
- int msgid)
- {
- msgid_cell *pt;
- if (mctx.mode & VERY_VERBOSE)
- printf ("ldclt[%d]: T%03d: msgIdDN (%d)\n", mctx.pid, tttctx->thrdNum, msgid);
- for (pt = tttctx->firstMsgId ; pt != NULL ; pt = pt->next)
- if (pt->msgid == msgid)
- return (pt->dn);
- return (NULL);
- }
- /* ****************************************************************************
- FUNCTION : msgIdStr
- PURPOSE : Found the requested message id in the pending list.
- INPUT : tttctx = thread's context
- msgid = message id
- OUTPUT : None
- RETURN : The associated str, or an error message string.
- DESCRIPTION :
- *****************************************************************************/
- char *
- msgIdStr (
- thread_context *tttctx,
- int msgid)
- {
- msgid_cell *pt;
- if (mctx.mode & VERY_VERBOSE)
- printf ("ldclt[%d]: T%03d: msgIdStr (%d)\n", mctx.pid, tttctx->thrdNum, msgid);
- for (pt = tttctx->firstMsgId ; pt != NULL ; pt = pt->next)
- if (pt->msgid == msgid)
- return (pt->str);
- return ("Error: msgid not found");
- }
- /* ****************************************************************************
- FUNCTION : msgIdDel
- PURPOSE : Delete a message id from the pending ones.
- INPUT : tttctx = thread's context
- msgid = message id.
- freeAttr= true or false depending on freing the attribs
- OUTPUT : None.
- RETURN : -1 if not found, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- msgIdDel (
- thread_context *tttctx,
- int msgid,
- int freeAttr)
- {
- msgid_cell *pt; /* For the loop */
- msgid_cell *ptToFree; /* The cell to free */
- if (mctx.mode & VERY_VERBOSE)
- printf ("ldclt[%d]: T%03d: msgIdDel (%d)\n", mctx.pid, tttctx->thrdNum, msgid);
- /*
- * Make sure there is a list !
- */
- if (tttctx->firstMsgId != NULL)
- {
- /*
- * Maybe it is the first one ?
- */
- if (tttctx->firstMsgId->msgid == msgid)
- {
- ptToFree = tttctx->firstMsgId;
- tttctx->firstMsgId = tttctx->firstMsgId->next;
- if (tttctx->firstMsgId == NULL)
- tttctx->lastMsgId = NULL;
- free (ptToFree);
- return (0);
- }
- /*
- * Let's go through the whole list
- */
- for (pt = tttctx->firstMsgId ; pt->next != NULL ; pt = pt->next)
- if (pt->next->msgid == msgid)
- {
- /*
- * Be carrefull if it is the last element of the list
- */
- if (pt->next->next == NULL)
- tttctx->lastMsgId = pt;
- ptToFree = pt->next;
- pt->next = ptToFree->next;
- if (freeAttr)
- if (freeAttrib (ptToFree->attribs) < 0)
- return (-1);
- /*
- * Free the pointer itself
- */
- free (ptToFree);
- return (0);
- }
- }
- /*
- * Not found
- */
- printf ("ldclt[%d]: T%03d: message %d not found.\n", mctx.pid, tttctx->thrdNum, msgid);
- fflush (stdout);
- return (-1);
- }
- /* ****************************************************************************
- FUNCTION : getThreadStatus
- PURPOSE : Get the value of a given thread's status.
- INPUT : tttctx = thread context
- OUTPUT : status = the thread's status
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- getThreadStatus (
- thread_context *tttctx,
- int *status)
- {
- int ret; /* Return code */
- if ((ret = ldclt_mutex_lock (&(tttctx->status_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_lock(T%03d), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fprintf (stderr, "ldclt[%d]: Problem in getThreadStatus()\n", mctx.pid);
- fflush (stderr);
- return (-1);
- }
- *status = tttctx->status;
- if ((ret = ldclt_mutex_unlock (&(tttctx->status_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_unlock(T%03d), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fprintf (stderr, "ldclt[%d]: Problem in getThreadStatus()\n", mctx.pid);
- fflush (stderr);
- return (-1);
- }
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : setThreadStatus
- PURPOSE : Set the value of a given thread's status.
- INPUT : tttctx = thread context
- status = new status
- OUTPUT : None.
- RETURN : -1 if error, 0 else.
- DESCRIPTION :
- *****************************************************************************/
- int
- setThreadStatus (
- thread_context *tttctx,
- int status)
- {
- int ret; /* Return code */
- if ((ret = ldclt_mutex_lock (&(tttctx->status_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_lock(T%03d), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fprintf (stderr, "ldclt[%d]: Problem in setThreadStatus()\n", mctx.pid);
- fflush (stderr);
- return (-1);
- }
- tttctx->status = status;
- if ((ret = ldclt_mutex_unlock (&(tttctx->status_mutex))) != 0)
- {
- fprintf (stderr,
- "ldclt[%d]: Cannot mutex_unlock(T%03d), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, ret, strerror (ret));
- fprintf (stderr, "ldclt[%d]: Problem in setThreadStatus()\n", mctx.pid);
- fflush (stderr);
- return (-1);
- }
- return (0);
- }
- /* ****************************************************************************
- FUNCTION : threadMain
- PURPOSE : This function is the main function of the client threads
- part of this tool.
- INPUT : arg = this thread's thread_context
- OUTPUT : None.
- RETURN : None.
- DESCRIPTION :
- *****************************************************************************/
- void *
- threadMain (
- void *arg)
- {
- thread_context *tttctx; /* This thread's context */
- int go = 1; /* Thread must continue */
- int status; /* Thread's status */ /*JLS 17-11-00*/
- /*
- * Initialization
- */
- tttctx = (thread_context *) arg;
- if (setThreadStatus (tttctx, CREATED) < 0) /*JLS 17-11-00*/
- { /*JLS 17-11-00*/
- tttctx->status = DEAD; /*JLS 17-11-00*/
- return NULL; /*JLS 17-11-00*/
- } /*JLS 17-11-00*/
- tttctx->asyncHit = 0;
- tttctx->binded = 0;
- tttctx->fd = -1;
- tttctx->lastVal = mctx.randomLow-1;
- tttctx->ldapCtx = NULL;
- tttctx->matcheddnp = NULL; /*JLS 15-12-00*/
- tttctx->nbOpers = 0;
- tttctx->totOpers = 0;
- tttctx->pendingNb = 0;
- tttctx->firstMsgId = NULL;
- tttctx->lastMsgId = NULL;
- /*
- * Don't forget the buffers !!
- * This should save time while redoing random values
- */
- if ((mctx.mode & NEED_FILTER) || (mctx.mod2 & (M2_GENLDIF|M2_NEED_FILTER))) /*JLS 19-03-01*/
- {
- if (mctx.mod2 & M2_RDN_VALUE) /*JLS 23-03-01*/
- tttctx->bufFilter = (char *) malloc (MAX_FILTER); /*JLS 23-03-01*/
- else /*JLS 23-03-01*/
- { /*JLS 23-03-01*/
- /*
- * Variable filter ?
- */
- tttctx->bufFilter = (char *) malloc (strlen (mctx.filter) + 1);
- if (tttctx->bufFilter == NULL) /*JLS 06-03-00*/
- { /*JLS 06-03-00*/
- printf ("ldclt[%d]: %s: cannot malloc(tttctx->bufFilter), error=%d (%s)\n",
- mctx.pid, tttctx->thrdId, errno, strerror (errno));
- ldcltExit (EXIT_INIT); /*JLS 18-12-00*/
- } /*JLS 06-03-00*/
- if (!(mctx.mode & (RANDOM | INCREMENTAL)))
- strcpy (tttctx->bufFilter, mctx.filter);
- else
- {
- tttctx->startRandom = strlen (mctx.randomHead);
- strcpy (tttctx->bufFilter, mctx.randomHead);
- strcpy (&(tttctx->bufFilter[tttctx->startRandom+mctx.randomNbDigit]),
- mctx.randomTail);
- if (mctx.mode & VERY_VERBOSE)
- {
- printf ("ldclt[%d]: %s: startRandom = %d\n",
- mctx.pid, tttctx->thrdId, tttctx->startRandom);
- printf ("ldclt[%d]: %s: randomHead = \"%s\", randomTail = \"%s\"\n",
- mctx.pid, tttctx->thrdId, mctx.randomHead, mctx.randomTail);
- }
- }
- } /*JLS 23-03-01*/
- /*
- * Variable base DN ?
- */
- tttctx->bufBaseDN = (char *) malloc (strlen (mctx.baseDN) + 1);
- if (tttctx->bufBaseDN == NULL) /*JLS 06-03-00*/
- { /*JLS 06-03-00*/
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufBaseDN), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- ldcltExit (EXIT_INIT); /*JLS 18-12-00*/
- } /*JLS 06-03-00*/
- if (!(mctx.mode & RANDOM_BASE))
- strcpy (tttctx->bufBaseDN, mctx.baseDN);
- else
- {
- tttctx->startBaseDN = strlen (mctx.baseDNHead);
- strcpy (tttctx->bufBaseDN, mctx.baseDNHead);
- strcpy (&(tttctx->bufBaseDN[tttctx->startBaseDN+mctx.baseDNNbDigit]),
- mctx.baseDNTail);
- }
- /*
- * Variable bind DN ?
- * Do not forget the random bind password below that is activated
- * at the same time as the random bind DN.
- */
- if (mctx.bindDN != NULL) /*JLS 05-03-01*/
- { /*JLS 05-03-01*/
- tttctx->bufBindDN = (char *) malloc (strlen (mctx.bindDN) + 1);
- if (tttctx->bufBindDN == NULL)
- {
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufBindDN), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- ldcltExit (EXIT_INIT);
- }
- if (!(mctx.mode & RANDOM_BINDDN))
- strcpy (tttctx->bufBindDN, mctx.bindDN);
- else
- {
- tttctx->startBindDN = strlen (mctx.bindDNHead);
- strcpy (tttctx->bufBindDN, mctx.bindDNHead);
- strcpy (&(tttctx->bufBindDN[tttctx->startBindDN+mctx.bindDNNbDigit]),
- mctx.bindDNTail);
- }
- } /*JLS 05-03-01*/
- /*
- * Variable bind password ?
- * Remember that the random bind password feature is activated
- * by the same option as the random bind DN, but has here its own
- * code section for the ease of coding.
- */
- if (mctx.passwd != NULL) /*JLS 05-03-01*/
- { /*JLS 05-03-01*/
- tttctx->bufPasswd = (char *) malloc (strlen (mctx.passwd) + 1);
- if (tttctx->bufPasswd == NULL)
- {
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufPasswd), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- ldcltExit (EXIT_INIT);
- }
- if (!(mctx.mode & RANDOM_BINDDN))
- strcpy (tttctx->bufPasswd, mctx.passwd);
- else
- {
- tttctx->startPasswd = strlen (mctx.passwdHead);
- strcpy (tttctx->bufPasswd, mctx.passwdHead);
- strcpy (&(tttctx->bufPasswd[tttctx->startPasswd+mctx.passwdNbDigit]),
- mctx.passwdTail);
- }
- }
- } /*JLS 05-03-01*/
- /*
- * Bind DN from a file ?
- * The trick (mctx.passwd = "foo bar"; ) is needed to
- * simplify the code, because in many places we check
- * if mctx.passwd exist before sending password.
- */
- if (mctx.mod2 & M2_RNDBINDFILE) /*JLS 03-05-01*/
- { /*JLS 03-05-01*/
- tttctx->bufBindDN = (char *) malloc (MAX_DN_LENGTH); /*JLS 03-05-01*/
- tttctx->bufPasswd = (char *) malloc (MAX_DN_LENGTH); /*JLS 03-05-01*/
- mctx.passwd = "foo bar"; /* trick... */ /*JLS 03-05-01*/
- } /*JLS 03-05-01*/
- /*
- * Variable Authid ?
- */
- if (mctx.sasl_authid != NULL)
- {
- tttctx->bufSaslAuthid = (char *) malloc (strlen (mctx.sasl_authid) + 1);
- if (tttctx->bufSaslAuthid == NULL)
- {
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufSaslAuthid), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- ldcltExit (EXIT_INIT);
- }
- if (!(mctx.mod2 & M2_RANDOM_SASLAUTHID))
- strcpy (tttctx->bufSaslAuthid, mctx.sasl_authid);
- else
- {
- tttctx->startSaslAuthid = strlen (mctx.sasl_authid_head);
- strcpy (tttctx->bufSaslAuthid, mctx.sasl_authid_head);
- strcpy (&(tttctx->bufSaslAuthid[tttctx->startSaslAuthid+mctx.sasl_authid_nbdigit]),
- mctx.sasl_authid_tail);
- }
- }
- /*
- * Initiates the attribute replace buffers
- */
- if (mctx.mode & ATTR_REPLACE) /* New */ /*JLS 21-11-00*/
- {
- tttctx->bufAttrpl = (char *) malloc (strlen (mctx.attrpl) + 1);
- if (tttctx->bufAttrpl == NULL)
- {
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufAttrpl), error=%d (%s)\n",
- mctx.pid, tttctx->thrdNum, errno, strerror (errno));
- ldcltExit (EXIT_INIT); /*JLS 18-12-00*/
- }
- tttctx->startAttrpl = strlen (mctx.attrplHead);
- strcpy (tttctx->bufAttrpl, mctx.attrplHead);
- strcpy (&(tttctx->bufAttrpl[tttctx->startAttrpl+mctx.attrplNbDigit]),
- mctx.attrplTail);
- }
- /*
- * Initiates the attribute replace buffers attrplName
- */
- if ( mctx.mod2 & M2_ATTR_REPLACE_FILE )
- {
- /* bufAttrpl should point to the same memory location that mctx.attrplFileContent points to */
- tttctx->bufAttrpl = mctx.attrplFileContent;
- if (tttctx->bufAttrpl == NULL)
- {
- printf ("ldclt[%d]: T%03d: cannot malloc(tttctx->bufAttrpl), error=%d (%s), can we read file [%s]\n", mctx.pid, tttctx->thrdNum, errno, strerror (errno), mctx.attrplFile);
- ldcltExit (EXIT_INIT);
- }
- }
- /*
- * We are ready to go !
- */
- status = RUNNING; /*JLS 17-11-00*/
- if (setThreadStatus (tttctx, RUNNING) < 0) /*JLS 17-11-00*/
- status = DEAD; /*JLS 17-11-00*/
- /*
- * Let's go !
- */
- while (go && (status != DEAD) && (status != MUST_SHUTDOWN)) /*JLS 17-11-00*/
- {
- if (mctx.waitSec > 0)
- { /*JLS 17-11-00*/
- ldclt_sleep (mctx.waitSec);
- /*
- * Maybe we should shutdown ?
- */
- if (getThreadStatus (tttctx, &status) < 0) /*JLS 17-11-00*/
- break; /*JLS 17-11-00*/
- if (status == MUST_SHUTDOWN) /*JLS 17-11-00*/
- break; /*JLS 17-11-00*/
- } /*JLS 17-11-00*/
- /*
- * Do a LDAP request
- */
- if (tttctx->mode & ADD_ENTRIES)
- if (doAddEntry (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- if (tttctx->mode & ATTR_REPLACE) /*JLS 21-11-00*/
- if (doAttrReplace (tttctx) < 0) /*JLS 21-11-00*/
- { /*JLS 21-11-00*/
- go = 0; /*JLS 21-11-00*/
- continue; /*JLS 21-11-00*/
- } /*JLS 21-11-00*/
- if (mctx.mod2 & M2_ATTR_REPLACE_FILE )
- if (doAttrFileReplace (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- if (tttctx->mode & DELETE_ENTRIES)
- if (doDeleteEntry (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- if (mctx.mod2 & M2_BINDONLY) /*JLS 04-05-01*/
- if (doBindOnly (tttctx) < 0) /*JLS 04-05-01*/
- { /*JLS 04-05-01*/
- go = 0; /*JLS 04-05-01*/
- continue; /*JLS 04-05-01*/
- } /*JLS 04-05-01*/
- if (tttctx->mode & EXACT_SEARCH)
- if (doExactSearch (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- if (tttctx->mode & RENAME_ENTRIES)
- if (doRename (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- /*
- * Maybe a specific scenario ?
- */
- if (tttctx->mode & SCALAB01) /*JLS 08-01-01*/
- if (doScalab01 (tttctx) < 0) /*JLS 08-01-01*/
- { /*JLS 08-01-01*/
- go = 0; /*JLS 08-01-01*/
- continue; /*JLS 08-01-01*/
- } /*JLS 08-01-01*/
- /*
- * Maybe genldif mode ?
- */
- if (mctx.mod2 & M2_GENLDIF) /*JLS 19-03-01*/
- if (doGenldif (tttctx) < 0) /*JLS 19-03-01*/
- { /*JLS 19-03-01*/
- ldclt_flush_genldif(); /*JLS 02-04-01*/
- go = 0; /*JLS 19-03-01*/
- continue; /*JLS 19-03-01*/
- } /*JLS 19-03-01*/
- if (mctx.mod2 & M2_ABANDON)
- {
- if (doAbandon (tttctx) < 0)
- {
- go = 0;
- continue;
- }
- }
- /*
- * Check the thread's status
- */
- if (getThreadStatus (tttctx, &status) < 0) /*JLS 17-11-00*/
- break; /*JLS 17-11-00*/
- }
- /*
- * End of thread
- */
- /* [156984] once setting "DEAD", nothing should be done in the context */
- /* moved the dead message above setThreadStatus(DEAD) */
- printf ("ldclt[%d]: T%03d: thread is dead.\n", mctx.pid, tttctx->thrdNum);
- fflush (stdout);
- if (setThreadStatus (tttctx, DEAD) < 0) /*JLS 17-11-00*/
- { /*JLS 17-11-00*/
- ldclt_sleep (1); /*JLS 17-11-00*/
- tttctx->status = DEAD; /* Force it !!! */ /*JLS 17-11-00*/
- } /*JLS 17-11-00*/
- return (arg);
- }
|