PERFORCE change 178149 for review
Jonathan Anderson
jona at FreeBSD.org
Wed May 12 16:18:48 UTC 2010
http://p4web.freebsd.org/@@178149?ac=10
Change 178149 by jona at jona-belle-freebsd8 on 2010/05/12 16:18:17
Added lc_fdlist_find(), changed whitespace in lc_fdlist_lookup() for clarity
Affected files ...
.. //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 edit
.. //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 edit
Differences ...
==== //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 (text+ko) ====
@@ -30,7 +30,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#14 $
+ * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum.h#15 $
*/
#ifndef _LIBCAPSICUM_H_
@@ -90,6 +90,19 @@
cap_rights_t rights);
/*
+ * Open a stored file descriptor.
+ *
+ * Given a filename '/foo/bar/fubar', this function will attempt to find the file
+ * in the FD list. If that fails, it will attempt to find a parent directory in the
+ * FD list and supply a filename relative to that FD (which will be a pointer to a
+ * location within the supplied filename - do NOT free it!).
+ */
+int
+lc_fdlist_find(struct lc_fdlist *lfp, const char *subsystem,
+ const char *classname, const char *filename,
+ const char **relative_name);
+
+/*
* Look up a file descriptor.
*
* Multiple entries with the same classname are allowed, so iterating through
==== //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 (text+ko) ====
@@ -31,7 +31,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#12 $
+ * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapsicum/libcapsicum_fdlist.c#13 $
*/
#include <sys/mman.h>
@@ -393,6 +393,60 @@
}
int
+lc_fdlist_find(struct lc_fdlist *lfp, const char *subsystem,
+ const char *classname, const char *filename,
+ const char **relative_name)
+{
+ int pos = 0;
+ int fd = -1;
+
+ /* try to find the file itself in the FD list */
+ size_t len = strlen(filename);
+ *relative_name = filename + len;
+
+ while (fd == -1)
+ {
+ char *dirname;
+
+ if (lc_fdlist_lookup(lfp, subsystem, classname,
+ &dirname, &fd, &pos) == -1)
+ break;
+
+ if (strncmp(dirname, filename, len + 1)) fd = -1;
+ free(dirname);
+ }
+
+ if (fd >= 0) return fd;
+
+
+ /* now try to find a parent directory and a relative filename */
+ *relative_name = NULL;
+ pos = 0;
+
+ while (fd == -1)
+ {
+ char *dirname;
+
+ if (lc_fdlist_lookup(lfp, subsystem, classname,
+ &dirname, &fd, &pos) == -1)
+ return (-1);
+
+ len = strlen(dirname);
+ if (strncmp(dirname, filename, len)) fd = -1;
+ else
+ {
+ *relative_name = filename + len;
+ if (**relative_name == '/') (*relative_name)++;
+ }
+
+ free(dirname);
+ }
+
+ return fd;
+}
+
+
+int
lc_fdlist_lookup(struct lc_fdlist *lfp, const char *subsystem,
const char *classname, char **name, int *fdp, int *pos)
{
@@ -412,28 +466,30 @@
for (u_int i = (pos ? *pos : 0); i < lfsp->count; i++) {
struct lc_fdlist_entry *entry = lfsp->entries + i;
- if ((!subsystem ||
- !strncmp(subsystem, names + entry->sysoff,
- entry->syslen + 1))
- && (!classname || !strncmp(classname, names +
- entry->classoff, entry->classnamelen + 1))) {
+ if ((!subsystem
+ || !strncmp(subsystem, names + entry->sysoff,
+ entry->syslen + 1))
+ && (!classname
+ || !strncmp(classname, names + entry->classoff,
+ entry->classnamelen + 1)))
+ {
+ /* found a matching entry! */
+ successful = 1;
+ *fdp = entry->fd;
- /* found a matching entry! */
if (name) {
*name = malloc(entry->namelen + 1);
strncpy(*name, names + entry->nameoff,
entry->namelen + 1);
}
-
- *fdp = entry->fd;
- if (pos != NULL) *pos = i + 1;
- successful = 1;
+ if (pos) *pos = i + 1;
break;
}
}
UNLOCK(lfp);
if (successful)
return (0);
+
errno = ENOENT;
return (-1);
}
More information about the p4-projects
mailing list