PERFORCE change 164193 for review
Jonathan Anderson
jona at FreeBSD.org
Fri Jun 12 18:13:01 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=164193
Change 164193 by jona at jona-trustedbsd-kentvm on 2009/06/12 18:12:34
Some more testing of user_angel IPC
Affected files ...
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/fdtest.c#3 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/protocol.c#7 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/protocol.h#6 edit
Differences ...
==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/fdtest.c#3 (text+ko) ====
@@ -84,7 +84,7 @@
- struct cap_wire_datum *d = cap_marshall_string(message, 7);
+ struct cap_wire_datum *d = cap_marshall_string(message, strlen(message));
if(cap_send_fd(sock, "message and FDs", d, fds, fdlen) < 0)
err(EX_IOERR, "Error sending data/FD");
@@ -98,6 +98,51 @@
if(cap_recv_fd(sock, &name, &d, fd_array, &fdlen) < 0)
err(EX_IOERR, "Error receiving data/FD");
+ printf("Received datum:\n");
+ printf(" type: %i\n", d->type);
+ printf(" length: %i\n", d->length);
+ printf(" value: ");
+
+ int len = 50;
+ char tmp[len + 1];
+
+ switch(d->type & TYPE_PRIMITIVE_MASK)
+ {
+ case INTEGER:
+ // TODO: long, short, etc.
+ ;
+ int value;
+ if(cap_unmarshall_int(d, &value) < 0)
+ err(EX_SOFTWARE, "Error unmarshalling int");
+
+ printf("%i ", value);
+ break;
+
+/*
+ case FLOAT:
+ if(d->type & LONG) printf("%g ", *((double*) value));
+ else printf("%f ", *((float*) value));
+ break;
+*/
+ case STRING:
+ ;
+ if(cap_unmarshall_string(d, tmp, &len) < 0)
+ err(EX_SOFTWARE, "Error unmarshalling string");
+
+ tmp[len] = '\0';
+ printf("'%s' ", tmp);
+ break;
+
+ case CAPBOX_OPTIONS:
+ printf("capbox_options (TODO) ");
+ break;
+
+ default:
+ printf("unknown type 0x%x ", d->type);
+ }
+ printf("\n");
+
+ free(d);
printf("Received FDs: ");
for(int i = 0; i < fdlen; i++) printf("%i ", fd_array[i]);
==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/protocol.c#7 (text+ko) ====
@@ -58,6 +58,21 @@
}
+
+int cap_unmarshall_int(struct cap_wire_datum *datum, int32_t *value)
+{
+ if(datum->length != 4)
+ {
+ fprintf(stderr, "Error unmarshalling int: should be 4B long, not %i\n",
+ datum->length);
+ return -1;
+ }
+
+ memcpy(value, ((void*) datum) + sizeof(struct cap_wire_datum), 4);
+ return datum->length;
+}
+
+
struct cap_wire_datum* cap_marshall_string(char *value, int len)
{
int size = sizeof(struct cap_wire_datum) + len;
@@ -65,10 +80,25 @@
d->type = STRING;
d->length = len;
- memcpy(d + sizeof(struct cap_wire_datum), value, len);
+ memcpy(((char*) d) + sizeof(struct cap_wire_datum), value, len);
return d;
}
+
+
+int cap_unmarshall_string(struct cap_wire_datum *datum, char *value, int *len)
+{
+ *len = datum->length;
+ if(datum->length < 0)
+ {
+ fprintf(stderr, "Error unmarshalling int: should be positive, not %i\n",
+ datum->length);
+ return -1;
+ }
+
+ memcpy(value, ((void*) datum) + sizeof(struct cap_wire_datum), datum->length);
+ return datum->length;
+}
/*
int cap_send(int sock, struct cap_wire_datum* datum);
*/
@@ -140,7 +170,7 @@
// make room for it
*d = (struct cap_wire_datum*) malloc(to_receive);
struct iovec iov;
- iov.iov_base = d;
+ iov.iov_base = *d;
iov.iov_len = to_receive;
==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/protocol.h#6 (text+ko) ====
@@ -38,11 +38,16 @@
struct cap_wire_datum
{
uint32_t type;
- #define INTEGER 0x00000001
- #define FLOAT 0x00000002
- #define STRING 0x00000004
- #define LONG 0x00000100
- #define SHORT 0x00000200
+ #define TYPE_PRIMITIVE_MASK 0x000000ff
+ #define INTEGER 0x00000001
+ #define FLOAT 0x00000002
+ #define STRING 0x00000004
+ #define CAPBOX_OPTIONS 0x00000008
+
+ #define TYPE_MODIFIER_MASK 0x0000ff00
+ #define LONG 0x00000100
+ #define SHORT 0x00000200
+ #define UNSIGNED 0x00000400
uint32_t length;
@@ -60,16 +65,23 @@
+/* Unmarshalling functions; calling programs should free the result */
struct cap_wire_datum* cap_marshall_int(int32_t value);
struct cap_wire_datum* cap_marshall_string(char *value, int len);
+/* Unmarshalling functions; return the number of bytes unmarshalled (or -1) */
+int cap_unmarshall_int(struct cap_wire_datum *datum, int32_t *value);
+int cap_unmarshall_string(struct cap_wire_datum *datum, char* value, int *len);
+
+/* Sending, with or without file descriptors */
int cap_send(int sock, char *name, struct cap_wire_datum *d);
int cap_send_fd(int sock, const char *name, struct cap_wire_datum *d,
int32_t fd_array[], int32_t fdlen);
+/* Receiving, with or without file descriptors */
int cap_recv(int sock, char **name, struct cap_wire_datum **d);
-/** You supply the FD array and say how big it is; I'll tell you how many FDs you actually received. */
+/* You supply the FD array and say how big it is; I'll tell you how many FDs you actually received. */
int cap_recv_fd(int sock, char **name, struct cap_wire_datum **d,
int32_t fd_array[], int32_t *fdlen);
More information about the p4-projects
mailing list