PERFORCE change 89756 for review

Robert Watson rwatson at FreeBSD.org
Sun Jan 15 15:05:57 PST 2006


http://perforce.freebsd.org/chv.cgi?CH=89756

Change 89756 by rwatson at rwatson_peppercorn on 2006/01/15 23:05:33

	Substantially modify the memory management semantics of libbsm:
	rather than allocating au_*_ent structures using malloc, instead
	rely on the caller to allocate them for _r() functions, or
	return statically allocated memory for non-_r() versions.  This
	brings libbsm in line with other base system database APIs, as
	well as the Open Solaris BSM API.  Not all documentation is
	completely updated yet, as references to array lengths and
	memory allocation need to be completed.  audump is updated to
	offer options to use both _r and non-_r variants to dump various
	audit databases.
	
	While here, fix a moderate number of bugs, improve consistency
	between databases, etc.

Affected files ...

.. //depot/projects/trustedbsd/openbsm/bsm/libbsm.h#11 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/au_class.3#2 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/au_event.3#2 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/au_user.3#2 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_class.c#7 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_control.c#9 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_event.c#6 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_flags.c#8 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_io.c#19 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_mask.c#8 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/bsm_user.c#9 edit
.. //depot/projects/trustedbsd/openbsm/libbsm/libbsm.3#2 edit
.. //depot/projects/trustedbsd/openbsm/tools/audump.c#2 edit

Differences ...

==== //depot/projects/trustedbsd/openbsm/bsm/libbsm.h#11 (text+ko) ====

@@ -697,9 +697,13 @@
 void			 setauclass(void);
 void			 endauclass(void);
 struct au_class_ent	*getauclassent(void);
+struct au_class_ent	*getauclassent_r(au_class_ent_t *class_int);
 struct au_class_ent	*getauclassnam(const char *name);
+struct au_class_ent	*getauclassnam_r(au_class_ent_t *class_int,
+			    const char *name);
 struct au_class_ent	*getauclassnum(au_class_t class_number);
-void			 free_au_class_ent(struct au_class_ent *c);
+struct au_class_ent	*getauclassnum_r(au_class_ent_t *class_int,
+			    au_class_t class_number);
 
 /*
  * Functions relating to querying audit control information.
@@ -713,33 +717,41 @@
 int			 getauditflagsbin(char *auditstr, au_mask_t *masks);
 int			 getauditflagschar(char *auditstr, au_mask_t *masks,
 			    int verbose);
-int			au_preselect(au_event_t event, au_mask_t *mask_p,
+int			 au_preselect(au_event_t event, au_mask_t *mask_p,
 			    int sorf, int flag);
 
 /*
  * Functions relating to querying audit event information.
+ *
+ * XXXRW: getauevnonam() has no _r version?
  */
 void			 setauevent(void);
 void			 endauevent(void);
 struct au_event_ent	*getauevent(void);
-struct au_event_ent	*getauevnam(char *name);
+struct au_event_ent	*getauevent_r(struct au_event_ent *e);
+struct au_event_ent	*getauevnam(const char *name);
+struct au_event_ent	*getauevnam_r(struct au_event_ent *e,
+			    const char *name);
 struct au_event_ent	*getauevnum(au_event_t event_number);
-void			 free_au_event_ent(struct au_event_ent *e);
-au_event_t		*getauevnonam(char *event_name);
-void			 free_au_event(au_event_t *e);
+struct au_event_ent	*getauevnum_r(struct au_event_ent *e,
+			    au_event_t event_number);
+au_event_t		*getauevnonam(const char *event_name);
+au_event_t		*getauevnonam_r(au_event_t *ev,
+			    const char *event_name);
 
-
 /*
  * Functions relating to querying audit user information.
  */
 void			 setauuser(void);
 void			 endauuser(void);
 struct au_user_ent	*getauuserent(void);
+struct au_user_ent	*getauuserent_r(struct au_user_ent *u);
 struct au_user_ent	*getauusernam(const char *name);
+struct au_user_ent	*getauusernam_r(struct au_user_ent *u,
+			    const char *name);
 int			 au_user_mask(char *username, au_mask_t *mask_p);
 int			 getfauditflags(au_mask_t *usremask,
 			    au_mask_t *usrdmask, au_mask_t *lastmask);
-void			 free_au_user_ent(struct au_user_ent *u);
 
 /*
  * Functions for reading and printing records and tokens from audit trails.

==== //depot/projects/trustedbsd/openbsm/libbsm/au_class.3#2 (text+ko) ====

@@ -1,5 +1,5 @@
 .\"-
-.\" Copyright (c) 2005 Robert N. M. Watson
+.\" Copyright (c) 2005-2006 Robert N. M. Watson
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -29,9 +29,10 @@
 .Dt AU_CLASS 3
 .Os
 .Sh NAME
-.Nm free_au_class_ent ,
 .Nm getauclassent ,
+.Nm getauclassent_r ,
 .Nm getauclassnam ,
+.Nm getauclassnam_r ,
 .Nm setauclass ,
 .Nm endauclass
 .Nd "Look up information from the audit_class database"
@@ -39,12 +40,14 @@
 .Lb libbsm
 .Sh SYNOPSIS
 .In libbsm.h
-.Ft void
-.Fn free_au_class_ent "struct au_class_ent *c"
 .Ft struct au_class_ent *
 .Fn getauclassent "void"
 .Ft struct au_class_ent *
+.Fn getauclassent_r "struct au_class_ent *e"
+.Ft struct au_class_ent *
 .Fn getauclassnam "const char *name"
+.Ft struct au_class_ent *
+.Fn getauclassnam_r "struct au_class_ent *e" "const char *name"
 .Ft void
 .Fn setauclass "void"
 .Ft void
@@ -56,31 +59,16 @@
 Audit event classes are described by
 .Vt struct au_class_ent .
 .Pp
-.Fn free_au_class_ent
-frees a previously allocated
-.Vt struct au_class_ent
-returned by
-.Fn getauclassent
-or
-.Fn getauclassnam .
 .Pp
 .Fn getauclassent
 will return the next class found in the
 .Xr audit_class 5
 database, or the first if the function has not yet been called.
-The returned
-.Vt struct au_class_ent
-must be freed by calling
-.Fn free_au_class_ent .
 .Dv NULL
 will be returned if no further records are available.
 .Pp
 .Fn getauclassnam
 looks up a class by name.
-The returned
-.Vt struct au_class_ent
-must be freed by calling
-.Fn free_au_class_ent .
 .Dv NULL
 will be returned if no matching class can be found.
 .Pp

==== //depot/projects/trustedbsd/openbsm/libbsm/au_event.3#2 (text+ko) ====

@@ -1,5 +1,5 @@
 .\"-
-.\" Copyright (c) 2005 Robert N. M. Watson
+.\" Copyright (c) 2005-2006 Robert N. M. Watson
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -30,34 +30,41 @@
 .Os
 .Sh NAME
 .Nm free_au_event_ent ,
-.Nm setauevent   ,
-.Nm endauevent   ,
+.Nm setauevent ,
+.Nm endauevent ,
 .Nm getauevent ,
+.Nm getauevent_r ,
 .Nm getauevnam ,
+.Nm getauevnam_r ,
 .Nm getauevnum ,
+.Nm getauevnum_r ,
 .Nm getauevnonam ,
-.Nm free_au_event
+.Nm getauevnonam_r ,
 .Nd "Look up information from the audit_event database"
 .Sh LIBRARY
 .Lb libbsm
 .Sh SYNOPSIS
 .In libbsm.h
 .Ft void
-.Fn free_au_event_ent "struct au_event_ent *e"
-.Ft void
 .Fn setauevent "void"
 .Ft void
 .Fn endauevent "void"
 .Ft "struct au_event_ent *"
 .Fn getauevent "void"
 .Ft "struct au_event_ent *"
+.Fn getauevent_r "struct au_event_ent *e"
+.Ft "struct au_event_ent *"
 .Fn getauevnam "char *name"
 .Ft "struct au_event_ent *"
+.Fn getauevnam_r "struct au_event_ent *e" "char *name"
+.Ft "struct au_event_ent *"
 .Fn getauevnum "au_event_t event_number"
+.Ft "struct au_event_ent *"
+.Fn getauevnum_r "struct au_event_ent *e" "au_event_t event_number"
 .Ft "au_event_t *"
 .Fn getauevnonam "char *event_name"
-.Ft void
-.Fn free_au_event "au_event_t *e"
+.Ft "au_event_t *"
+.Fn getauevnonam_r "au_event_t *ev" "char *event_name"
 .Sh DESCRIPTION
 These interfaces may be used to look up information from the
 .Xr audit_event 5
@@ -69,21 +76,9 @@
 .Fn getauevnam ,
 or
 .Fn getauevnum .
-They must be freed with a call to
-.Fn free_au_event_ent .
 It is also possible look up an event number via a call to
-.Nm getauevnonam ,
-which must be freed with a call to
-.Fn free_au_event .
+.Nm getauevnonam .
 .Pp
-.Fn free_au_event_ent
-frees an audit event description structure that has been allocated by an
-earlier call to
-.Fn getauevent ,
-.Fn getauevnam ,
-or
-.Fn getauevnum .
-.Pp
 .Fn setauevent
 resets the database access session for
 .Xr audit_event 5 ,
@@ -99,39 +94,32 @@
 .Fn getauevent
 returns a reference to the next entry in the
 .Xr audit_event 5
-database, which much be freed with a call to
-.Fn free_au_event_ent .
+database.
 .Pp
 .Fn getauevnam
 returns a reference to the entry in the
 .Xr audit_event 5
 database with a name of
-.Va name ,
-which must be freed with a call to
-.Fn free_au_event_ent .
+.Va name .
 .Pp
 .Fn getauevnum
 returns a reference to the entry in the
 .Xr audit_event 5
 database with an event number of
-.Va event_number ,
-which must be freed with a call to
-.Fn free_au_event_ent .
+.Va event_number .
 .Pp
 .Fn getauevnonam
 returns a reference to an audit event number using the
 .Xr audit_event 5
-database, which must be freed with a call to
-.Fn free_au_event .
-.Pp
-.Fn free_au_event
-frees a reference to an audit event number that was allocated by
-.Fn getauevnonam .
+database.
 .Sh RETURN VALUES
 Functions
 .Fn getauevent ,
+.Fn getauevent_r ,
 .Fn getauevnam ,
+.Fn getauevnam_r ,
 .Fn getauevnum ,
+.Fn getauevnum_r ,
 and
 .Fn getauevnuam
 will return a reference to a

==== //depot/projects/trustedbsd/openbsm/libbsm/au_user.3#2 (text+ko) ====

@@ -1,5 +1,5 @@
 .\"-
-.\" Copyright (c) 2005 Robert N. M. Watson
+.\" Copyright (c) 2005-2006 Robert N. M. Watson
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -29,11 +29,12 @@
 .Dt AU_USER 3
 .Os
 .Sh NAME
-.Nm free_au_user_ent ,
 .Nm setauuser ,
 .Nm endauuser ,
 .Nm getauuserent ,
+.Nm getauuserent_r ,
 .Nm getauusernam ,
+.Nm getauusernam_r ,
 .Nm au_user_mask ,
 .Nm getfauditflags
 .Nd "Look up information from the audit_user database"
@@ -42,15 +43,17 @@
 .Sh SYNOPSIS
 .In libbsm.h
 .Ft void
-.Fn free_au_user_ent "au_user_ent_t *u"
-.Ft void
 .Fn setauuser "void"
 .Ft void
 .Fn endauuser "void"
-.Ft au_user_ent_t *
+.Ft struct au_user_ent *
 .Fn getauuserent "void"
-.Ft au_user_ent_t *
+.Ft struct au_user_ent *
+.Fn getauuserent_r "struct au_user_ent *u" "void"
+.Ft struct au_user_ent *
 .Fn getauusernam "const char *name"
+.Ft struct au_user_ent *
+.Fn getauusernam_r "struct au_user_ent *u" "const char *name"
 .Ft int
 .Fn au_user_mask "char *username" "au_mask_t *mask_p"
 .Ft int
@@ -67,31 +70,16 @@
 .Dv au_always ,
 and events never to audit
 .Dv au_never .
-.Vt au_user_ent
-structures allocated by calls to
-.Fn getauuserent
-and
-.Fn getauusernam
-may be freed by calling
-.Fn free_au_user_ent .
 .Pp
 .Fn getauuserent
 return the next user found in the
 .Xr audit_user 5
 database, or the first if the function has not yet been called.
-The returned
-.Vt au_user_ent_t
-may be freed by calling
-.Fn free_au_user_ent .
 .Dv NULL
 will be returned if no further records are available.
 .Pp
 .Fn getauusernam
 looks up a user by name.
-The returned
-.Vt au_user_ent_t
-must be freed by calling
-.Fn free_au_user_ent .
 .Dv NULL
 will be returned if no matching class can be found.
 .Pp

==== //depot/projects/trustedbsd/openbsm/libbsm/bsm_class.c#7 (text+ko) ====

@@ -1,5 +1,7 @@
 /*
- * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2004, Apple Computer, Inc.
+ * Copyright (c) 2006 Robert N. M. Watson
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -44,56 +46,6 @@
 static pthread_mutex_t	mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /*
- * XXX The reentrant versions of the following functions is TBD
- * XXX struct au_class_ent *getclassent_r(au_class_ent_t *class_int);
- * XXX struct au_class_ent *getclassnam_r(au_class_ent_t *class_int, const
- *         char *name);
- */
-
-/*
- * Allocate a au_class_ent structure.
- */
-static struct au_class_ent *
-get_class_area(void)
-{
-	struct au_class_ent *c;
-
-	c = malloc(sizeof(struct au_class_ent));
-	if (c == NULL)
-		return (NULL);
-	c->ac_name = malloc(AU_CLASS_NAME_MAX * sizeof(char));
-	if (c->ac_name == NULL) {
-		free(c);
-		return (NULL);
-	}
-	c->ac_desc = malloc(AU_CLASS_DESC_MAX * sizeof(char));
-	if (c->ac_desc == NULL) {
-		free(c->ac_name);
-		free(c);
-		return (NULL);
-	}
-
-	return (c);
-}
-
-
-/*
- * Free the au_class_ent structure.
- */
-void
-free_au_class_ent(struct au_class_ent *c)
-{
-
-	if (c) {
-		if (c->ac_name)
-			free(c->ac_name);
-		if (c->ac_desc)
-			free(c->ac_desc);
-		free(c);
-	}
-}
-
-/*
  * Parse a single line from the audit_class file passed in str to the struct
  * au_class_ent elements; store the result in c.
  */
@@ -137,9 +89,8 @@
  * Must be called with mutex held.
  */
 static struct au_class_ent *
-getauclassent_locked(void)
+getauclassent_r_locked(struct au_class_ent *c)
 {
-	struct au_class_ent *c;
 	char *tokptr, *nl;
 
 	if ((fp == NULL) && ((fp = fopen(AUDIT_CLASS_FILE, "r")) == NULL))
@@ -161,28 +112,41 @@
 
 	tokptr = linestr;
 
-	c = get_class_area(); /* allocate */
-	if (c == NULL)
-		return (NULL);
-
 	/* Parse tokptr to au_class_ent components. */
-	if (classfromstr(tokptr, delim, c) == NULL) {
-		free_au_class_ent(c);
+	if (classfromstr(tokptr, delim, c) == NULL)
 		return (NULL);
-	}
 
 	return (c);
 }
 
 struct au_class_ent *
+getauclassent_r(struct au_class_ent *c)
+{
+	struct au_class_ent *cp;
+
+	pthread_mutex_lock(&mutex);
+	cp = getauclassent_r_locked(c);
+	pthread_mutex_unlock(&mutex);
+	return (cp);
+}
+
+struct au_class_ent *
 getauclassent(void)
 {
-	struct au_class_ent *c;
+	static char class_ent_name[AU_CLASS_NAME_MAX];
+	static char class_ent_desc[AU_CLASS_DESC_MAX];
+	static struct au_class_ent c, *cp;
+
+	bzero(&c, sizeof(c));
+	bzero(class_ent_name, sizeof(class_ent_name));
+	bzero(class_ent_desc, sizeof(class_ent_desc));
+	c.ac_name = class_ent_name;
+	c.ac_desc = class_ent_desc;
 
 	pthread_mutex_lock(&mutex);
-	c = getauclassent_locked();
+	cp = getauclassent_r_locked(&c);
 	pthread_mutex_unlock(&mutex);
-	return (c);
+	return (cp);
 }
 
 /*
@@ -210,52 +174,84 @@
 /*
  * Return the next au_class_entry having the given class name.
  */
-au_class_ent_t *
-getauclassnam(const char *name)
+struct au_class_ent *
+getauclassnam_r(struct au_class_ent *c, const char *name)
 {
-	struct au_class_ent *c;
+	struct au_class_ent *cp;
 
 	if (name == NULL)
 		return (NULL);
 
 	pthread_mutex_lock(&mutex);
 	setauclass_locked();
-	while ((c = getauclassent()) != NULL) {
-		if (strcmp(name, c->ac_name) == 0) {
+	while ((cp = getauclassent_r_locked(c)) != NULL) {
+		if (strcmp(name, cp->ac_name) == 0) {
 			pthread_mutex_unlock(&mutex);
-			return (c);
+			return (cp);
 		}
-		free_au_class_ent(c);
 	}
 	pthread_mutex_unlock(&mutex);
 	return (NULL);
 }
 
+struct au_class_ent *
+getauclassnam(const char *name)
+{
+	static char class_ent_name[AU_CLASS_NAME_MAX];
+	static char class_ent_desc[AU_CLASS_DESC_MAX];
+	static struct au_class_ent c;
+
+	bzero(&c, sizeof(c));
+	bzero(class_ent_name, sizeof(class_ent_name));
+	bzero(class_ent_desc, sizeof(class_ent_desc));
+	c.ac_name = class_ent_name;
+	c.ac_desc = class_ent_desc;
+
+	return (getauclassnam_r(&c, name));
+}
+
+
 /*
  * Return the next au_class_entry having the given class number.
  *
  * OpenBSM extension.
  */
-au_class_ent_t *
-getauclassnum(au_class_t class_number)
+struct au_class_ent *
+getauclassnum_r(struct au_class_ent *c, au_class_t class_number)
 {
-	au_class_ent_t *c;
+	struct au_class_ent *cp;
 
 	pthread_mutex_lock(&mutex);
 	setauclass_locked();
-	while ((c = getauclassent()) != NULL) {
-		if (class_number == c->ac_class)
-			return (c);
-		free_au_class_ent(c);
+	while ((cp = getauclassent_r_locked(c)) != NULL) {
+		if (class_number == cp->ac_class)
+			return (cp);
 	}
 	pthread_mutex_unlock(&mutex);
 	return (NULL);
 }
 
+struct au_class_ent *
+getauclassnum(au_class_t class_number)
+{
+	static char class_ent_name[AU_CLASS_NAME_MAX];
+	static char class_ent_desc[AU_CLASS_DESC_MAX];
+	static struct au_class_ent c;
+
+	bzero(&c, sizeof(c));
+	bzero(class_ent_name, sizeof(class_ent_name));
+	bzero(class_ent_desc, sizeof(class_ent_desc));
+	c.ac_name = class_ent_name;
+	c.ac_desc = class_ent_desc;
+
+	return (getauclassnum_r(&c, class_number));
+}
+
 /*
  * audit_class processing is complete; close any open files.
  */
-void endauclass(void)
+void
+endauclass(void)
 {
 
 	pthread_mutex_lock(&mutex);

==== //depot/projects/trustedbsd/openbsm/libbsm/bsm_control.c#9 (text+ko) ====

@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2004, Apple Computer, Inc.
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions

==== //depot/projects/trustedbsd/openbsm/libbsm/bsm_event.c#6 (text+ko) ====

@@ -1,5 +1,7 @@
 /*
- * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2004, Apple Computer, Inc.
+ * Copyright (c) 2006 Robert N. M. Watson
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -44,55 +46,6 @@
 static pthread_mutex_t	mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /*
- * XXX The reentrant versions of the following functions is TBD
- * XXX struct au_event_ent *getauevent_r(au_event_ent_t *e);
- * XXX struct au_event_ent *getauevnam_r(au_event_ent_t *e, char *name);
- * XXX struct au_event_ent *getauevnum_r(au_event_ent_t *e, au_event_t event_number);
- */
-
-/*
- * Allocate an au_event_ent structure.
- */
-static struct au_event_ent *
-get_event_area(void)
-{
-	struct au_event_ent *e;
-
-	e = (struct au_event_ent *) malloc (sizeof(struct au_event_ent));
-	if (e == NULL)
-		return (NULL);
-	e->ae_name = (char *)malloc(AU_EVENT_NAME_MAX * sizeof(char));
-	if (e->ae_name == NULL) {
-		free(e);
-		return (NULL);
-	}
-	e->ae_desc = (char *)malloc(AU_EVENT_DESC_MAX * sizeof(char));
-	if (e->ae_desc == NULL) {
-		free(e->ae_name);
-		free(e);
-		return (NULL);
-	}
-
-	return (e);
-}
-
-/*
- * Free the au_event_ent structure.
- */
-void
-free_au_event_ent(struct au_event_ent *e)
-{
-
-	if (e) {
-		if (e->ae_name)
-			free(e->ae_name);
-		if (e->ae_desc)
-			free(e->ae_desc);
-		free(e);
-	}
-}
-
-/*
  * Parse one line from the audit_event file into the au_event_ent structure.
  */
 static struct au_event_ent *
@@ -170,71 +123,77 @@
 /*
  * Enumerate the au_event_ent entries.
  */
-struct au_event_ent *
-getauevent(void)
+static struct au_event_ent *
+getauevent_r_locked(struct au_event_ent *e)
 {
-	struct au_event_ent *e;
 	char *nl;
 
-	pthread_mutex_lock(&mutex);
+	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL))
+		return (NULL);
 
-	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL)) {
-		pthread_mutex_unlock(&mutex);
+	if (fgets(linestr, AU_LINE_MAX, fp) == NULL)
 		return (NULL);
-	}
 
-	if (fgets(linestr, AU_LINE_MAX, fp) == NULL) {
-		pthread_mutex_unlock(&mutex);
-		return (NULL);
-	}
 	/* Remove new lines. */
 	if ((nl = strrchr(linestr, '\n')) != NULL)
 		*nl = '\0';
 
-	e = get_event_area();
-	if (e == NULL) {
-		pthread_mutex_unlock(&mutex);
+	/*
+	 * Get the next event structure.
+	 *
+	 * XXXRW: Perhaps we should keep reading lines until we find a valid
+	 * one, rather than stopping when we hit an invalid one?
+	 */
+	if (eventfromstr(linestr, delim, e) == NULL)
 		return (NULL);
-	}
+
+	return (e);
+}
 
-	/* Get the next event structure. */
-	if (eventfromstr(linestr, delim, e) == NULL) {
-		free_au_event_ent(e);
-		pthread_mutex_unlock(&mutex);
-		return (NULL);
-	}
+struct au_event_ent *
+getauevent_r(struct au_event_ent *e)
+{
+	struct au_event_ent *ep;
 
+	pthread_mutex_lock(&mutex);
+	ep = getauevent_r_locked(e);
 	pthread_mutex_unlock(&mutex);
-	return (e);
+	return (ep);
+}
+
+struct au_event_ent *
+getauevent(void)
+{
+	static char event_ent_name[AU_EVENT_NAME_MAX];
+	static char event_ent_desc[AU_EVENT_DESC_MAX];
+	static struct au_event_ent e;
+
+	bzero(&e, sizeof(e));
+	bzero(event_ent_name, sizeof(event_ent_name));
+	bzero(event_ent_desc, sizeof(event_ent_desc));
+	e.ae_name = event_ent_name;
+	e.ae_desc = event_ent_desc;
+	return (getauevent_r(&e));
 }
 
 /*
- * Search for an audit event structure having the given event name
+ * Search for an audit event structure having the given event name.
+ *
+ * XXXRW: Why accept NULL name?
  */
-struct au_event_ent *
-getauevnam(char *name)
+static struct au_event_ent *
+getauevnam_r_locked(struct au_event_ent *e, const char *name)
 {
-	struct au_event_ent *e;
 	char *nl;
 
 	if (name == NULL)
 		return (NULL);
 
-	pthread_mutex_lock(&mutex);
-
 	/* Rewind to beginning of the file. */
 	setauevent_locked();
 
-	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL)) {
-		pthread_mutex_unlock(&mutex);
-		return (NULL);
-	}
-
-	e = get_event_area();
-	if (e == NULL) {
-		pthread_mutex_unlock(&mutex);
+	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL))
 		return (NULL);
-	}
 
 	while (fgets(linestr, AU_LINE_MAX, fp) != NULL) {
 		/* Remove new lines. */
@@ -242,61 +201,92 @@
 			*nl = '\0';
 
 		if (eventfromstr(linestr, delim, e) != NULL) {
-			if (!strcmp(name, e->ae_name)) {
-				pthread_mutex_unlock(&mutex);
+			if (strcmp(name, e->ae_name) == 0)
 				return (e);
-			}
 		}
 	}
 
+	return (NULL);
+}
+
+struct au_event_ent *
+getauevnam_r(struct au_event_ent *e, const char *name)
+{
+	struct au_event_ent *ep;
+
+	pthread_mutex_lock(&mutex);
+	ep = getauevnam_r_locked(e, name);
 	pthread_mutex_unlock(&mutex);
+	return (ep);
+}
 
-	free_au_event_ent(e);
+struct au_event_ent *
+getauevnam(const char *name)
+{
+	static char event_ent_name[AU_EVENT_NAME_MAX];
+	static char event_ent_desc[AU_EVENT_DESC_MAX];
+	static struct au_event_ent e;
 
-	return (NULL);
+	bzero(&e, sizeof(e));
+	bzero(event_ent_name, sizeof(event_ent_name));
+	bzero(event_ent_desc, sizeof(event_ent_desc));
+	e.ae_name = event_ent_name;
+	e.ae_desc = event_ent_desc;
+	return (getauevnam_r(&e, name));
 }
 
 /*
  * Search for an audit event structure having the given event number.
  */
-struct au_event_ent *getauevnum(au_event_t event_number)
+static struct au_event_ent *
+getauevnum_r_locked(struct au_event_ent *e, au_event_t event_number)
 {
-	struct au_event_ent *e;
 	char *nl;
 
-	pthread_mutex_lock(&mutex);
-
 	/* Rewind to beginning of the file. */
 	setauevent_locked();
 
-	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL)) {
-		pthread_mutex_unlock(&mutex);
+	if ((fp == NULL) && ((fp = fopen(AUDIT_EVENT_FILE, "r")) == NULL))
 		return (NULL);
-	}
 
-	e = get_event_area();
-	if (e == NULL) {
-		pthread_mutex_unlock(&mutex);
-		return (NULL);
-	}
-
 	while (fgets(linestr, AU_LINE_MAX, fp) != NULL) {
 		/* Remove new lines. */
 		if ((nl = strrchr(linestr, '\n')) != NULL)
 			*nl = '\0';
 
 		if (eventfromstr(linestr, delim, e) != NULL) {
-			if (event_number == e->ae_number) {
-				pthread_mutex_unlock(&mutex);
+			if (event_number == e->ae_number)
 				return (e);
-			}
 		}
 	}
 
+	return (NULL);
+}
+
+struct au_event_ent *
+getauevnum_r(struct au_event_ent *e, au_event_t event_number)
+{
+	struct au_event_ent *ep;
+
+	pthread_mutex_lock(&mutex);
+	ep = getauevnum_r_locked(e, event_number);
 	pthread_mutex_unlock(&mutex);
-	free_au_event_ent(e);
-	return (NULL);
+	return (ep);
+}
+
+struct au_event_ent *
+getauevnum(au_event_t event_number)
+{
+	static char event_ent_name[AU_EVENT_NAME_MAX];
+	static char event_ent_desc[AU_EVENT_DESC_MAX];
+	static struct au_event_ent e;
 
+	bzero(&e, sizeof(e));
+	bzero(event_ent_name, sizeof(event_ent_name));
+	bzero(event_ent_desc, sizeof(event_ent_desc));
+	e.ae_name = event_ent_name;
+	e.ae_desc = event_ent_desc;
+	return (getauevnum_r(&e, event_number));
 }
 
 /*
@@ -304,25 +294,30 @@
  * corresponding event number.
  */
 au_event_t *
-getauevnonam(char *event_name)
+getauevnonam_r(au_event_t *ev, const char *event_name)
 {
-	struct au_event_ent *e;
-	au_event_t *n = NULL;
+	static char event_ent_name[AU_EVENT_NAME_MAX];
+	static char event_ent_desc[AU_EVENT_DESC_MAX];
+	static struct au_event_ent e, *ep;
+
+	bzero(event_ent_name, sizeof(event_ent_name));
+	bzero(event_ent_desc, sizeof(event_ent_desc));
+	bzero(&e, sizeof(e));
+	e.ae_name = event_ent_name;
+	e.ae_desc = event_ent_desc;
 
-	e = getauevnam(event_name);
-	if (e != NULL) {
-		n = malloc (sizeof(au_event_t));
-		if (n != NULL)
-			*n = e->ae_number;
-		free_au_event_ent(e);
-	}
+	ep = getauevnam_r(&e, event_name);
+	if (ep == NULL)
+		return (NULL);
 
-	return (n);
+	*ev = e.ae_number;
+	return (ev);
 }
 
-void
-free_au_event(au_event_t *e)
+au_event_t *
+getauevnonam(const char *event_name)
 {
-	if (e)
-		free(e);
+	static au_event_t event;
+
+	return (getauevnonam_r(&event, event_name));
 }

==== //depot/projects/trustedbsd/openbsm/libbsm/bsm_flags.c#8 (text+ko) ====

@@ -1,5 +1,7 @@
 /*
- * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2004, Apple Computer, Inc.
+ * Copyright (c) 2006 Robert N. M. Watson
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -41,16 +43,25 @@
 int
 getauditflagsbin(char *auditstr, au_mask_t *masks)
 {
+	char class_ent_name[AU_CLASS_NAME_MAX];
+	char class_ent_desc[AU_CLASS_DESC_MAX];
+	struct au_class_ent c, *cp;
 	char *tok;
 	char sel, sub;
-	struct au_class_ent *c;
 	char *last;
 
+
 	if ((auditstr == NULL) || (masks == NULL)) {
 		errno = EINVAL;
 		return (-1);
 	}
 
+	bzero(&c, sizeof(c));
+	bzero(class_ent_name, sizeof(class_ent_name));
+	bzero(class_ent_desc, sizeof(class_ent_desc));
+	c.ac_name = class_ent_name;

>>> TRUNCATED FOR MAIL (1000 lines) <<<


More information about the p4-projects mailing list