/**
 * @brief	Append one string to another and return the result.
 * @param	s		a pointer to a leading null-terminated string to to which the other string will be appended.
 * @param	append	a pointer to a null-terminated string to be appended to the leading string.
 * @return	NULL if a memory allocation failure occurred, or a pointer to the resulting string on success.
 */
chr_t *ns_append(chr_t *s, chr_t *append) {

	chr_t *output = NULL;
	size_t alen, slen = 0;

	if (!append || !(alen = ns_length_get(append))) {
		log_pedantic("The append string appears to be empty.");
		return s;
	}

	// Allocate a new string if the existing string pointer is NULL.
	if (!s) {
		s = ns_dupe(append);
	} else if (!(slen = ns_length_get(s))) {
		ns_free(s);
		s = ns_dupe(append);
	}
	// Otherwise check the amount of available space in the buffer and if necessary allocate more.
	else if ((output = ns_alloc(slen + alen + 1))) {
		mm_copy(output, s, slen);
		mm_copy(output + slen, append, alen);
		ns_free(s);
		s = output;
	}

	return s;
}
Exemple #2
0
bool_t check_inx_append_helper(inx_t *inx) {

	void *val;
	chr_t snum[64];
	bool_t outcome = true;
	multi_t last = mt_get_null(), key = mt_get_null();
	uint64_t offset = (uint64_t)thread_get_thread_id() * INX_CHECK_OBJECTS;

	key = mt_set_type(key, M_TYPE_UINT64);
	last = mt_set_type(last, M_TYPE_UINT64);

	// Add to the index, alternating insert and then append, while occasionally truncating and/or deleting.
	for (uint64_t i = 0; status() && outcome && i < INX_CHECK_OBJECTS; i++) {

		key.val.u64 = offset + i;
		snprintf(snum, 64, "%lu", offset + i);
		inx_lock_write(inx);

		if (!(val = ns_dupe(snum))) {
			outcome = false;
		}
		else if (i % 4 == 0) {
			if (!inx_insert(inx, key, val)) {
				outcome = false;
				ns_free(val);
			}
			else {
				last = mt_dupe(key);
			}
		}
		else if (i % 2 == 0) {
			if (!inx_append(inx, key, val)) {
				outcome = false;
				ns_free(val);
			}
			else {
				last = mt_dupe(key);
			}
		}
		else {
			if (!inx_append(inx, key, val)) {
				outcome = false;
				ns_free(val);
			}
			inx_delete(inx, key);
			inx_delete(inx, last);
			last.val.u64 = 0;
		}

		if (i == 73) inx_truncate(inx);

		inx_unlock(inx);
	}

	inx_lock_write(inx);
	inx_truncate(inx);
	inx_unlock(inx);

	return outcome;
}
Exemple #3
0
NS_PRIVATE NsError _ns_pixel_proc_flip_optical_axis_all
	(
	NsPixelType   pixel_type,
	nspointer     pixels,
	nssize        width,
	nssize        height,
	nssize        length,
	nssize        row_align,
	NsProgress   *progress
	)
	{
	nspointer  start_slice, end_slice, swap_slice;
	nssize     bytes_per_slice;
	nsfloat    iter, num_iters;


	bytes_per_slice = ns_pixel_buffer_slice_size( pixel_type, width, height, row_align );

	if( NULL == ( swap_slice = ns_malloc( bytes_per_slice ) ) )
		return ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE );

	start_slice = pixels;
	end_slice   = NS_INCREMENT_POINTER( void, pixels, bytes_per_slice * ( length - 1 ) );

	iter      = 0.0f;
	num_iters = ( nsfloat )length / 2.0f;

	if( NULL != progress )
		ns_progress_update( progress, NS_PROGRESS_BEGIN );

	while( start_slice < end_slice )
		{
		if( NULL != progress )
			{
			if( ns_progress_cancelled( progress ) )
				{
				ns_free( swap_slice );
				return ns_no_error();
				}

			ns_progress_update( progress, 100.0f * ( iter / num_iters ) );
			iter += 1.0f;
			}

		ns_memcpy( swap_slice, start_slice, bytes_per_slice ); /* swap  = start */
		ns_memcpy( start_slice, end_slice, bytes_per_slice );  /* start = end   */
		ns_memcpy( end_slice, swap_slice, bytes_per_slice );   /* end   = swap  */

		start_slice = NS_INCREMENT_POINTER( void, start_slice, bytes_per_slice );
		end_slice   = NS_DECREMENT_POINTER( void, end_slice, bytes_per_slice );
		}

	if( NULL != progress )
		ns_progress_update( progress, NS_PROGRESS_END );

	ns_free( swap_slice );
	return ns_no_error();
	}
static void
FreeUrl(Ns_Request * request)
{
    if (request->url != NULL) {
        ns_free(request->url);
        request->url = NULL;
    }
    if (request->urlv != NULL) {
        ns_free(request->urlv[0]);
        ns_free(request->urlv);
        request->urlv = NULL;
    }
}
Exemple #5
0
void FreeEventList( Module *mod_ptr )
{
	if( mod_ptr->event_list )
	{
		ns_free( mod_ptr->event_list );
		mod_ptr->event_list = NULL;
	}
#ifdef USE_PERL
	if( mod_ptr->pm && mod_ptr->pm->event_list )
	{
		ns_free( mod_ptr->pm->event_list );
		mod_ptr->pm->event_list = NULL;
	}
#endif /* USE_PERL */
}
Exemple #6
0
void ResetTLDStatistics( void ) 
{
	lnode_t *tn, *tn2;
	TLD *t;
	
	tn = list_first( tldstatlist );
	while( tn != NULL )
	{
		t = lnode_get( tn );
		ResetStatistic( &t->users );
		if( t->users.current == 0 )
		{
			/* don't delete the tld entry ??? as its our "unknown" entry */
			if( ircstrcasecmp( t->tld, UNKNOWN_COUNTRY_CODE ) )
			{
				tn2 = list_next( tldstatlist, tn );
				ns_free( t );
				list_delete_destroy_node( tldstatlist, tn );
				tn = tn2;
				continue;
			}
		}
		tn = list_next( tldstatlist, tn );
	}
}
Exemple #7
0
int
Dci_ListenCallback(char *name, char *addr, int port,
	Dci_ListenAcceptProc *acceptProc, Dci_ListenExitProc *exitProc, void *arg)
{
    ListenData *lPtr;
    int sock;
    int ok;

    lPtr = ns_malloc(sizeof(ListenData) + strlen(name));
    lPtr->acceptProc = acceptProc;
    lPtr->exitProc = exitProc;
    lPtr->arg = arg;
    strcpy(lPtr->name, name);
    ok = 0;
    sock = Ns_SockListen(addr, port);
    if (sock != -1) {
	Ns_SockSetNonBlocking(sock);
	if (Ns_SockCallback(sock, ListenProc, lPtr, NS_SOCK_READ|NS_SOCK_EXIT) != NS_OK) {
	    Ns_Log(Error, "Dci_ListenCallback: %s: %s(%d): %s", name, addr, port, strerror(errno));
	    close(sock);
	} else {
	    ok = 1;
	}
    }
    Ns_Log(ok ? Notice : Error, "%s: %s on %s:%d", name,
	   ok ? "listening" : "could not listen", addr ? addr : "*", port);
    if (!ok) {
	ns_free(lPtr);
    }
    return (ok ? NS_OK : NS_ERROR);
}
Exemple #8
0
static int
ListenProc(int sock, void *arg, int why)
{
    ListenData *lPtr = arg;

    if (why == NS_SOCK_EXIT) {
	Ns_Log(Notice, "%s: shutdown pending", lPtr->name);
	if (lPtr->exitProc != NULL) {
	    (*lPtr->exitProc)(lPtr->arg);
	}
	close(sock);
	Ns_Log(Notice, "%s: shutdown complete", lPtr->name);
	ns_free(lPtr);
	return NS_FALSE;
    }

    sock = Ns_SockAccept(sock, NULL, NULL);
    if (sock == -1) {
	Ns_Log(Error, "%s: accept() failed: %s", lPtr->name, strerror(errno));
    } else {
	DciLogPeer2(sock, lPtr->name, "connected to");
	(lPtr->acceptProc)(sock, lPtr->arg);
    }
    return NS_TRUE;
}
Exemple #9
0
void
MemThread(void *arg)
{
    int             i;
    void           *ptr;

    Ns_ThreadSetName("memthread");
    Ns_MutexLock(&lock);
    ++nrunning;
    Ns_CondBroadcast(&cond);
    while (!memstart) {
	Ns_CondWait(&cond, &lock);
    }
    Ns_MutexUnlock(&lock);

    ptr = NULL;
    for (i = 0; i < NA; ++i) {
	if (arg) {
	    if (ptr)
		ns_free(ptr);
	    ptr = ns_malloc(10);
	} else {
	    if (ptr)
		free(ptr);
	    ptr = malloc(10);
	}
    }
}
Exemple #10
0
void clean_string( char *text, size_t len )
{
	char *dd, *start, *orig; 
	size_t i = 0;

	dd = ns_malloc( len );	
	start = dd;
	orig = text;
	while( *text != '\0' ) {
		i++;
		switch( *text ) {
			case '%':
				/* if our final length is bigger than the buffer, then we just 
				 * drop the char */
				if ( ( i + 1 ) <= len ) {
					*dd++ = '%';
				} else {
					text++;
				}
				break;
			default:
				break;
		}
		*dd++ = *text++;		/* Move on to the next char */
	}
	*dd = 0;
	strncpy( orig, start, len );
	ns_free( start );	
}
Exemple #11
0
static void
FreeCallback(void *arg)
{
    TclCallback *cbPtr = arg;

    ckfree(cbPtr->script);
    ns_free(cbPtr);
}
Exemple #12
0
void
TlsLogArg(void *arg)
{
    int            *ip = arg;

    Msg("tls cleanup %d", *ip);
    ns_free(ip);
}
Exemple #13
0
void xchat_modeproc( ChannelLog *chandata, const CmdParams *cmdparams )
{
	char *modebuf;
	
	modebuf = joinbuf( cmdparams->av, cmdparams->ac, 0 );
	ls_write_log( chandata, XMODEPROC, xchat_time(), cmdparams->source->name, chandata->channame, modebuf );
	ns_free( modebuf );
}
Exemple #14
0
void nsperl2_delete_assoc_perl( ClientData clientData, Tcl_Interp *interp)
{
    perl_context *context = (perl_context *) clientData;
    Ns_Log (Notice, "in nsperl2_delete_assoc_perl - about to free and destruct interp perl context");
    PERL_SET_CONTEXT (context->perl_interp);
    /* perl_destruct (context->perl_interp); - I don't think we want to run END blocks for each interp shutdown do we?? */
    perl_free (context->perl_interp);
    ns_free ((void*)context);
}
Exemple #15
0
int
Ns_AbsoluteUrl(Ns_DString *dsPtr, char *url, char *base)
{
    char           *protocol, *host, *port, *path, *tail, *baseprotocol,
                   *basehost, *baseport, *basepath, *basetail;
    int             status = NS_OK;

    /*
     * Copy the URL's to allow Ns_ParseUrl to destory them.
     */

    url = ns_strdup(url);
    base = ns_strdup(base);
    Ns_ParseUrl(url, &protocol, &host, &port, &path, &tail);
    Ns_ParseUrl(base, &baseprotocol, &basehost, &baseport, &basepath, &basetail);
    if (baseprotocol == NULL || basehost == NULL || basepath == NULL) {
        status = NS_ERROR;
        goto done;
    }
    if (protocol == NULL) {
        protocol = baseprotocol;
    }
    if (host == NULL) {
        host = basehost;
        port = baseport;
    }
    if (path == NULL) {
        path = basepath;
    }
    Ns_DStringVarAppend(dsPtr, protocol, "://", host, NULL);
    if (port != NULL) {
        Ns_DStringVarAppend(dsPtr, ":", port, NULL);
    }
    if (*path == '\0') {
        Ns_DStringVarAppend(dsPtr, "/", tail, NULL);
    } else {
        Ns_DStringVarAppend(dsPtr, "/", path, "/", tail, NULL);
    }
done:
    ns_free(url);
    ns_free(base);
    return status;
}
Exemple #16
0
void
NsThreadMain(void *arg)
{
    ThreadArg   *argPtr = arg;
    Thread      *thrPtr;

    thrPtr = NewThread(argPtr);
    ns_free(argPtr);
    (*thrPtr->proc) (thrPtr->arg);
}
void
NsLockFree(void *lock)
{
    int err;

    err = pthread_mutex_destroy((pthread_mutex_t *) lock);
    if (err != 0) {
    	NsThreadFatal("NsLockFree", "pthread_mutex_destroy", err);
    }
    ns_free(lock);
}
Exemple #18
0
void
Ns_SemaDestroy(Ns_Sema *semaPtr)
{
    if (*semaPtr != NULL) {
    	Sema *sPtr = (Sema *) *semaPtr;

    	Ns_MutexDestroy(&sPtr->lock);
    	Ns_CondDestroy(&sPtr->cond);
    	ns_free(sPtr);
    	*semaPtr = NULL;
    }
}
Exemple #19
0
void CheckTimers_cb( int notused, short event, void *arg )
{
	Timer *timer = NULL;
	lnode_t *tn;

	SET_SEGV_LOCATION();

	update_time_now();
	/* We only run Timers one at a time */
	tn = list_first(timerlist);
	timer = lnode_get( tn );
	/* If a module is not yet synched, reset it's lastrun */
	if( !IsModuleSynched( timer->moduleptr ) ) {
		timer->lastrun = ( int ) me.now;
	} else if (timer->nextrun <= me.now) {
		if( setjmp( sigvbuf ) == 0 ) {
			dlog( DEBUG10, "run_mod_timers: Running timer %s for module %s", timer->name, timer->moduleptr->info->name );
			SET_RUN_LEVEL( timer->moduleptr );
			if( timer->handler( timer->userptr ) < 0 ) {
				dlog( DEBUG2, "run_mod_timers: Deleting Timer %s for Module %s as requested", timer->name, timer->moduleptr->info->name );
				list_delete_destroy_node(timerlist, tn);
				ns_free( timer );
				NextSchedule();
			} else {
				timer->lastrun = ( int ) me.now;
				TimerCalcNextRun(timer, tn);
			}
			RESET_RUN_LEVEL();
#if 0
			if( timer->type == TIMER_TYPE_COUNTDOWN ) {
					hash_scan_delete_destroy_node( timerlist, tn );
					ns_free( timer );
			}
#endif
		} else {
			nlog( LOG_CRITICAL, "run_mod_timers: setjmp() failed, can't call module %s", timer->moduleptr->info->name );
			NextSchedule();
		}
	}
}
Exemple #20
0
/*
 * Destroy all nodes in list and free associated data pointer.
 */
void
list_destroy_auto (list_t * list)
{
	lnode_t *ln;

	ln = list_first (list);
	while (ln) {
		void *ptr =	( void * )lnode_get (ln);
		ns_free (ptr);
		ln = list_next (list, ln);
	}
	list_destroy_nodes (list);
	list_destroy (list);
}
Exemple #21
0
int del_timers( const Module *mod_ptr )
{
	Timer *timer;
	lnode_t *tn, *tn2;

	tn = list_first(timerlist);
	while( tn != NULL )
	{
		tn2 = list_next(timerlist, tn);
		timer = lnode_get( tn );
		if( timer->moduleptr == mod_ptr )
		{
			dlog( DEBUG1, "del_timers: deleting timer %s from module %s.", timer->name, mod_ptr->info->name );
			list_delete_destroy_node(timerlist, tn);
#ifdef USE_PERL
			if( IS_PERL_MOD( timer->moduleptr ) )
				ns_free( timer->userptr );
#endif /* USE_PERL */
			ns_free( timer );
		}
		tn = tn2;
	}
	return NS_SUCCESS;
}
Exemple #22
0
static void
JsCleanup(void *arg) 
{
	jsEnv *jsEnvPtr = arg;
	
	if (jsEnvPtr != NULL) {
		JS_DestroyContext(jsEnvPtr->context); 
		JS_DestroyRuntime(jsEnvPtr->runtime);
		JS_ShutDown();

		Ns_Log(Debug, "JsCleanup: %p", jsEnvPtr);

		ns_free(jsEnvPtr);
	}
}
void
Ns_CondDestroy(Ns_Cond *cond)
{
    pthread_cond_t *condPtr = (pthread_cond_t *) *cond;
    int             err;

    if (condPtr != NULL) {
    	err = pthread_cond_destroy(condPtr);
    	if (err != 0) {
    	    NsThreadFatal("Ns_CondDestroy", "pthread_cond_destroy", err);
    	}
    	ns_free(condPtr);
    	*cond = NULL;
    }
}
Exemple #24
0
int DelTimer( const char *name )
{
	Timer *timer;
	lnode_t *tn;

	SET_SEGV_LOCATION();
	tn = list_first(timerlist);
	while (tn != NULL) {
		timer = lnode_get(tn);
		if (!ircstrcasecmp(timer->name, name)) {
			dlog( DEBUG2, "DelTimer: removed timer %s for module %s", name, timer->moduleptr->info->name );
			list_delete_destroy_node(timerlist, tn);
#ifdef USE_PERL
			if( IS_PERL_MOD( timer->moduleptr ) )
				ns_free( timer->userptr );
#endif /* USE_PERL */
			ns_free( timer );
			return NS_SUCCESS;
		}
		tn = list_next(timerlist, tn);
	}
	dlog(DEBUG2, "DelTimer: Timer %s not found", name);
	return NS_FAILURE;
}
Exemple #25
0
static void
CleanupThread(void *arg)
{
    Thread **thrPtrPtr;
    Thread *thrPtr = arg;

    Ns_MutexLock(&threadlock);
    thrPtrPtr = &firstThreadPtr;
    while (*thrPtrPtr != thrPtr) {
	thrPtrPtr = &(*thrPtrPtr)->nextPtr;
    }
    *thrPtrPtr = thrPtr->nextPtr;
    thrPtr->nextPtr = NULL;
    Ns_MutexUnlock(&threadlock);
    ns_free(thrPtr);
}
Exemple #26
0
void
NsJoinConnThreads(void)
{
    ConnData *firstPtr;
    void *arg;

    Ns_MutexLock(&joinlock);
    firstPtr = joinPtr;
    joinPtr = NULL;
    Ns_MutexUnlock(&joinlock);
    while (firstPtr != NULL) {
	Ns_ThreadJoin(&firstPtr->thread, &arg);
	firstPtr = firstPtr->nextPtr;
	ns_free(arg);
    }
}
Exemple #27
0
/**
 * @brief	Free all loaded magma configuration options.
 * @note	First all magma config keys will be freed, then the cache servers, relay servers, and magma servers.
 * @return	This function returns no value.
 */
void config_free(void) {

	for (uint64_t i = 0; i < sizeof(magma_keys) / sizeof(magma_keys_t); i++) {
		switch (magma_keys[i].norm.type) {

		case (M_TYPE_BLOCK):
			if (*((void **)(magma_keys[i].store))) {
				mm_free(*((void **)(magma_keys[i].store)));
			}
			break;
		case (M_TYPE_NULLER):
			if (*((char **)(magma_keys[i].store))) {
				ns_free(*((char **)(magma_keys[i].store)));
			}
			break;
		case (M_TYPE_STRINGER):

			// Intercept the blacklist config key.
			if (!st_cmp_cs_eq(NULLER(magma_keys[i].name), PLACER("magma.smtp.blacklist", 20))) {
				for (uint32_t j = 0; j < magma.smtp.blacklists.count; j++) {
					st_free(magma.smtp.blacklists.domain[j]);
				}
			}
			else if (*((stringer_t **)(magma_keys[i].store))) {
				st_free(*((stringer_t **)(magma_keys[i].store)));
			}
			break;
		default:
#ifdef MAGMA_PEDANTIC
			if (magma_keys[i].norm.type != M_TYPE_BOOLEAN && magma_keys[i].norm.type != M_TYPE_DOUBLE && magma_keys[i].norm.type != M_TYPE_FLOAT &&
					magma_keys[i].norm.type != M_TYPE_INT16 && magma_keys[i].norm.type != M_TYPE_INT32 && magma_keys[i].norm.type != M_TYPE_INT64 &&
					magma_keys[i].norm.type != M_TYPE_INT8 && magma_keys[i].norm.type != M_TYPE_UINT8 && magma_keys[i].norm.type != M_TYPE_UINT16 &&
					magma_keys[i].norm.type != M_TYPE_UINT32 && magma_keys[i].norm.type != M_TYPE_UINT64 && magma_keys[i].norm.type != M_TYPE_ENUM) {
				log_pedantic("Unexpected type. {type = %s = %u}", type(magma_keys[i].norm.type), magma_keys[i].norm.type);
			}
#endif
			break;
		}
	}

	cache_free();
	relay_free();
	servers_free();
	return;
}
static void
FreeThread(void *arg)
{
    Thread *thrPtr = arg;

    /*
     * Restore the current slots during cleanup so handlers can access
     * TLS in other slots.
     */

    SetKey("FreeThread", arg);
    NsCleanupTls(thrPtr->slots);
    SetKey("FreeThread", NULL);
    if (thrPtr->marked) {
	StackPages(thrPtr, 0);
    }
    ns_free(thrPtr);
}
void
Ns_FreeRequest(Ns_Request * request)
{
    if (request != NULL) {
        ns_free(request->line);
        ns_free(request->method);
        ns_free(request->protocol);
        ns_free(request->host);
        ns_free(request->query);
        FreeUrl(request);
        ns_free(request);
    }
}
Exemple #30
0
int
NsTclConfigSectionsCmd(ClientData dummy, Tcl_Interp *interp, int argc,
		       char **argv)
{
    Ns_Set **sets;
    int      i;

    if (argc != 1) {
        Tcl_AppendResult(interp, "wrong # args: should be \"",
            argv[0], " key\"", NULL);
        return TCL_ERROR;
    }
    sets = Ns_ConfigGetSections();
    for (i = 0; sets[i] != NULL; i++) {
        Ns_TclEnterSet(interp, sets[i], NS_TCL_SET_STATIC);
    }
    ns_free(sets);
    return TCL_OK;
}