FreeBSD/sys/kern/kern_loginclass.c
/*
 * Return loginclass structure with a corresponding name.  Not
 * performance critical, as it's used mainly by setloginclass(2),
 * which happens once per login session.  Caller has to use
 * loginclass_free() on the returned value when it's no longer
 * needed.
 */
Return loginclass structure with a corresponding name.  Not
performance critical, as it's used mainly by setloginclass(2),
which happens once per login session.  Caller has to use
loginclass_free() on the returned value when it's no longer
needed.
Return loginclass structure with a corresponding name . Not performance critical as it ' s used mainly by setloginclass ( 2 ) which happens once per login session . Caller has to use loginclass_free () on the returned value when it ' s no longer needed .
struct loginclass *
loginclass_find(const char *name)
{
	struct loginclass *lc, *new_lc;
	if (name[0] == '\0' || strlen(name) >= MAXLOGNAME)
		return (NULL);
	rw_rlock(&loginclasses_lock);
	lc = loginclass_lookup(name);
	rw_runlock(&loginclasses_lock);
	if (lc != NULL)
		return (lc);
	new_lc = malloc(sizeof(*new_lc), M_LOGINCLASS, M_ZERO | M_WAITOK);
	racct_create(&new_lc->lc_racct);
	refcount_init(&new_lc->lc_refcount, 1);
	strcpy(new_lc->lc_name, name);
	rw_wlock(&loginclasses_lock);
	if ((lc = loginclass_lookup(name)) == NULL) {
		LIST_INSERT_HEAD(&loginclasses, new_lc, lc_next);
		rw_wunlock(&loginclasses_lock);
		lc = new_lc;
	} else {
		rw_wunlock(&loginclasses_lock);
		racct_destroy(&new_lc->lc_racct);
		free(new_lc, M_LOGINCLASS);
	}
	return (lc);
}
struct loginclass * loginclass_find ( const char * name ) { struct loginclass * lc , * new_lc ; if ( name [ 0 ] == ' \0 ' || strlen ( name ) >= MAXLOGNAME ) return ( NULL ) ; rw_rlock ( & loginclasses_lock ) ; lc = loginclass_lookup ( name ) ; rw_runlock ( & loginclasses_lock ) ; if ( lc != NULL ) return ( lc ) ; new_lc = malloc ( sizeof ( * new_lc ) , M_LOGINCLASS , M_ZERO | M_WAITOK ) ; racct_create ( & new_lc -> lc_racct ) ; refcount_init ( & new_lc -> lc_refcount , 1 ) ; strcpy ( new_lc -> lc_name , name ) ; rw_wlock ( & loginclasses_lock ) ; if ( ( lc = loginclass_lookup ( name ) ) == NULL ) { LIST_INSERT_HEAD ( & loginclasses , new_lc , lc_next ) ; rw_wunlock ( & loginclasses_lock ) ; lc = new_lc ; } else { rw_wunlock ( & loginclasses_lock ) ; racct_destroy ( & new_lc -> lc_racct ) ; free ( new_lc , M_LOGINCLASS ) ; } return ( lc ) ; }
Return loginclass structure with a corresponding name.
Return loginclass structure with a corresponding name .
/*
	 * There's a chance someone created our loginclass while we
	 * were in malloc and not holding the lock, so we have to
	 * make sure we don't insert a duplicate loginclass.
	 */