Beispiel #1
0
int
_ftrylockfile(FILE *fp)
{
	pthread_t curthread = _pthread_self();
	int	ret = 0;

	if (fp->_lock->fl_owner == curthread)
		fp->_lock->fl_count++;
	/*
	 * Make sure this mutex is treated as a private
	 * internal mutex:
	 */
	else if (_pthread_mutex_trylock(&fp->_lock->fl_mutex) == 0) {
		fp->_lock->fl_owner = curthread;
		fp->_lock->fl_count = 1;
	}
	else
		ret = -1;
	return (ret);
}
Beispiel #2
0
/*
 * The first time nsdispatch is called (during a process's lifetime,
 * or after nsswitch.conf has been updated), nss_configure will
 * prepare global data needed by NSS.
 */
static int
nss_configure(void)
{
	static pthread_mutex_t conf_lock = PTHREAD_MUTEX_INITIALIZER;
	static time_t	 confmod;
	struct stat	 statbuf;
	int		 result, isthreaded;
	const char	*path;
#ifdef NS_CACHING
	void		*handle;
#endif

	result = 0;
	isthreaded = __isthreaded;
#if defined(_NSS_DEBUG) && defined(_NSS_SHOOT_FOOT)
	/* NOTE WELL:  THIS IS A SECURITY HOLE. This must only be built
	 * for debugging purposes and MUST NEVER be used in production.
	 */
	path = getenv("NSSWITCH_CONF");
	if (path == NULL)
#endif
	path = _PATH_NS_CONF;
	if (stat(path, &statbuf) != 0)
		return (0);
	if (statbuf.st_mtime <= confmod)
		return (0);
	if (isthreaded) {
	    result = _pthread_mutex_trylock(&conf_lock);
	    if (result != 0)
		    return (0);
	    _pthread_rwlock_unlock(&nss_lock);
	    result = _pthread_rwlock_wrlock(&nss_lock);
	    if (result != 0)
		    goto fin2;
	}
	_nsyyin = fopen(path, "r");
	if (_nsyyin == NULL)
		goto fin;
	VECTOR_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap),
	    (vector_free_elem)ns_dbt_free);
	VECTOR_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod),
	    (vector_free_elem)ns_mod_free);
	nss_load_builtin_modules();
	_nsyyparse();
	fclose(_nsyyin);
	vector_sort(_nsmap, _nsmapsize, sizeof(*_nsmap), string_compare);
	if (confmod == 0)
		atexit(nss_atexit);
	confmod = statbuf.st_mtime;

#ifdef NS_CACHING
	handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
	if (handle != NULL) {
		nss_cache_cycle_prevention_func = dlsym(handle,
			"_nss_cache_cycle_prevention_function");
		dlclose(handle);
	}
#endif
fin:
	if (isthreaded) {
	    _pthread_rwlock_unlock(&nss_lock);
	    if (result == 0)
		    result = _pthread_rwlock_rdlock(&nss_lock);
	}
fin2:
	if (isthreaded)
		_pthread_mutex_unlock(&conf_lock);
	return (result);
}