Date: Thu, 23 Jul 2009 13:56:46 GMT From: Jonathan Anderson <jona@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 166441 for review Message-ID: <200907231356.n6NDukpX028367@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=166441 Change 166441 by jona@jona-trustedbsd-belle-vmware on 2009/07/23 13:56:30 Added ua_stat(), ua_access(), ua_opendir() and ua_unmarshall_bytes() Affected files ... .. //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.c#9 edit .. //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.h#11 edit Differences ... ==== //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.c#9 (text+ko) ==== @@ -37,6 +37,7 @@ #include <libcapability.h> +#include <dirent.h> #include <errno.h> #include <fcntl.h> #include <stdarg.h> @@ -139,6 +140,64 @@ +int ua_access(const char *path, int mode) +{ + if(angel < 0) angel = ua_find(); + if(angel < 0) return -1; + + struct ua_datum *data[3]; + data[0] = ua_marshall_int(UA_CHECK_ACCESS); + data[1] = ua_marshall_string(path, strlen(path)); + data[2] = ua_marshall_int(mode); + + for(int i = 0; i < 3; i++) if(ua_send(angel, data[i], NULL, 0) < 0) return -1; + + free(data[0]); + free(data[1]); + free(data[2]); + + + + // retrieve the file descriptor(s) + struct ua_datum *d = ua_recv(angel, NULL, NULL); + if(!d) return -1; + + int response; + if(ua_unmarshall_int(d, &response) < 0) return -1; + + return response; +} + + + +int ua_stat(const char *path, struct stat *s) +{ + if(angel < 0) angel = ua_find(); + if(angel < 0) return -1; + + struct ua_datum *data[2]; + data[0] = ua_marshall_int(UA_STAT); + data[1] = ua_marshall_string(path, strlen(path)); + + for(int i = 0; i < 2; i++) + if(ua_send(angel, data[i], NULL, 0) < 0) + return -1; + + free(data[0]); + free(data[1]); + + + + struct ua_datum *d = ua_recv(angel, NULL, NULL); + if(!d) return -1; + + unsigned int len = sizeof(struct stat); + if(ua_unmarshall_bytes(d, (char*) s, &len) < 0) return -1; + + return 0; +} + + int ua_open(const char *path, int flags) { cap_rights_t rights = CAP_SEEK | CAP_FSYNC; @@ -158,8 +217,6 @@ if(angel < 0) angel = ua_find(); if(angel < 0) return -1; - printf("ua_ropen('%s', %i, %016llx)\n", path, flags, rights); - struct ua_datum *data[4]; data[0] = ua_marshall_int(UA_OPEN_PATH); data[1] = ua_marshall_string(path, strlen(path)); @@ -256,7 +313,16 @@ } +DIR* ua_opendir(const char *filename) +{ + int fd = ua_open(filename, O_RDONLY | O_DIRECTORY); + if(fd < 0) return NULL; + + return fdopendir(fd); +} + + int ua_send(int sock, datum *d, int32_t fds[], int32_t fdlen) { // the datum is the I/O vector @@ -434,6 +500,16 @@ int ua_unmarshall_string(const datum *d, char *value, unsigned int *len) { + (*len)--; + ua_unmarshall_bytes(d, value, len); + value[*len] = '\0'; + + return d->length; +} + + +int ua_unmarshall_bytes(const datum *d, char *value, unsigned int *len) +{ if(d == NULL) { errno = EINVAL; @@ -448,7 +524,7 @@ return -1; } } - else if(d->length >= *len) + else if(d->length > *len) { errno = EOVERFLOW; return -1; @@ -456,7 +532,6 @@ *len = d->length; memcpy(value, ((const char*) d) + sizeof(datum), d->length); - value[*len] = '\0'; return d->length; } ==== //depot/projects/trustedbsd/capabilities/src/lib/libuserangel/libuserangel.h#11 (text+ko) ==== @@ -35,6 +35,9 @@ #define _LIBUSERANGEL_H_ #include <sys/cdefs.h> +#include <sys/stat.h> + +#include <dirent.h> #include <stdio.h> #include <libuserangel-powerbox.h> @@ -53,6 +56,12 @@ /** Set the user angel */ void ua_set(int fd); +/** Check access rights via the User Angel */ +int ua_access(const char *access, int mode); + +/** Check file status via the User Angel */ +int ua_stat(const char *path, struct stat *s); + /** Open a file via the User Angel */ int ua_open(const char *path, int flags); @@ -62,6 +71,9 @@ /** Open a file stream via the User Angel */ FILE* ua_fopen(const char *path, const char *mode); +/** Open a directory via the User Angel */ +DIR* ua_opendir(const char *filename); + /* Low-level API */ @@ -69,7 +81,10 @@ enum ua_request_t { UA_NO_OP = 0, /* do nothing (useful for debugging) */ + UA_CHECK_ACCESS, /* access() substitute */ + UA_STAT, /* stat() substitute */ UA_OPEN_PATH, /* open() substitute */ + UA_OPEN_DIR, /* opendir() substitute */ UA_LOAD_LIBRARY, /* load a shared library */ UA_POWERBOX /* ask the user for file descriptor(s) */ }; @@ -114,6 +129,7 @@ /* Unmarshalling functions; return the number of bytes unmarshalled (or -1) */ int ua_unmarshall_int(const struct ua_datum *d, int32_t *value); +int ua_unmarshall_bytes(const struct ua_datum *d, char *value, unsigned int *len); int ua_unmarshall_string(const struct ua_datum *d, char *value, unsigned int *len); int ua_unmarshall_error(const struct ua_datum *d, int *errnum, char *msg, int *msglen); int ua_unmarshall_powerbox(const struct ua_datum *d, struct ua_powerbox_options *options);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200907231356.n6NDukpX028367>