PERFORCE change 164653 for review

Jonathan Anderson jona at FreeBSD.org
Thu Jun 18 10:51:11 UTC 2009


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

Change 164653 by jona at jona-trustedbsd-kentvm on 2009/06/18 10:50:48

	Some cleanup

Affected files ...

.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/powerbox.c#5 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/powerbox.h#5 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/server.c#6 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/test_client.c#7 edit
.. //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/user_angel.c#4 edit

Differences ...

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/powerbox.c#5 (text+ko) ====

@@ -46,48 +46,26 @@
 int capbox_display(struct capbox_options *options,
                    int fds[], char *names[], int *len)
 {
-	printf("capbox_display()\n");
-	printf("  options:\n");
-	printf("    UI: ");
+	// get the filenames from the user
 	switch(options->ui)
 	{
-		case KDE:		printf("KDE");		break;
-		default:		printf("<unknown:%i>", options->ui);
-	}
-	printf("\n");
+		case KDE:
+			if(dbus_powerbox(options, names, len))
+			{
+				fprintf(stderr, "Error opening DBus powerbox\n");
+				return -1;
+			}
+			break;
 
-	printf("    operation: ");
-	switch(options->operation)
-	{
-		case OPEN_FILE:		printf("OPEN_FILE");	break;
-		case SAVE_FILE:		printf("SAVE_FILE");	break;
-		case SELECT_DIR:	printf("SELECT_DIR");	break;
-		default:		printf("<unknown:%i>", options->operation);
+		default:
+			fprintf(stderr, "Unhandled powerbox UI type %i\n",
+			                options->ui);
 	}
-	printf("\n");
 
-	printf("    window title:    %s\n", options->window_title);
-	printf("    parent window:   %i\n", options->parent_window);
-	printf("    start path:      ");
-	if(options->pathlen > 0) printf("%s\n", options->start_path);
-	else printf("<none>\n");
 
-	printf("    start FD:        %i\n", options->start_fd);
-	printf("    multiple select: %s\n", (options->mult ? "true" : "false"));
-	printf("    filter:          ");
-	if(options->filterlen > 0) printf("%s\n", options->filter);
-	else printf("<none>\n");
-
-	if(dbus_powerbox(options, names, len))
-	{
-		fprintf(stderr, "Error opening powerbox via DBus\n");
-		return -1;
-	}
-
-	printf("File names:\n");
+	// open the files using the requested flags and rights
 	for(int i = 0; i < *len; i++)
 	{
-		printf("  %s\n", names[i]);
 		fds[i] = cap_open(names[i], options->flags, options->rights);
 		if(fds[i] < 0)
 		{
@@ -97,27 +75,5 @@
 	}
 
 	return 0;
-
-	fprintf(stderr, "powerbox not implemented, faking it\n");
-
-	if(*len > 0)
-	{
-		names[0] = (char*) malloc(32);
-		strcpy(names[0], "/etc/hosts");
-
-		fds[0] = open("/etc/hosts", O_RDONLY);
-	}
-
-	if(*len > 1)
-	{
-		names[1] = (char*) malloc(32);
-		strcpy(names[1], "/etc/nsswitch.conf");
-
-		fds[1] = open("/etc/nsswitch.conf", O_RDONLY);
-	}
-
-	if(*len > 2) *len = 2;
-
-	return 0;
 }
 

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/powerbox.h#5 (text+ko) ====

@@ -68,8 +68,6 @@
  * Open a powerbox.
  *
  * @param   options     powerbox options
- * @param   flags       open() flags (e.g. O_RDONLY)
- * @param   rights      capabilities requested (e.g. CAP_SEEK)
  * @param   fds         an array for file descriptors (size len)
  * @param   names       an array for file names (size len)
  * @param   len         the length of the above arrays (modified by this call)

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/server.c#6 (text+ko) ====

@@ -69,11 +69,13 @@
 
 
 
-int	handle_request(int client, enum capangel_req_t req);
 int	bind_to_path(const char *path);
 void	serve(int fd_server, struct fd_set *clients);
 void	accept_client(int fd_server);
 int	service_client(int client);
+int	handle_request(int client, enum capangel_req_t req);
+int	handle_path_request(int client);
+int	handle_powerbox_request(int client);
 void	client_closed(int client);
 
 
@@ -279,127 +281,138 @@
 {
 	printf("Client %4i: ", client);
 
-	struct cap_wire_datum *d;
-	int pathlen = 256;
-	char path[pathlen];
-	int fds[32];
-	int fdlen;
-
 	switch(req)
 	{
 		case FD_FROM_PATH:
-			;
-			fdlen = 1;
-			d = cap_recv_fds(client, fds, &fdlen);
-			if(!d)
-			{
-				perror("Error receiving path from client");
-				return -1;
-			}
+			puts("FD_FROM_PATH");
+			return handle_path_request(client);
+
+		case FD_POWERBOX:
+			puts("FD_POWERBOX");
+			return handle_powerbox_request(client);
+
+		default:
+			fprintf(stderr, "Unknown request %i\n", req);
+			return -1;
+	}
+
+	return 0;
+}
+
+
+int handle_path_request(int client)
+{
+	int fdlen = 0;
+	struct cap_wire_datum *d = cap_recv_fds(client, NULL, &fdlen);
 
-			if(cap_unmarshall_string(d, path, &pathlen) < 0)
-			{
-				fprintf(stderr, "Error unmarshalling path: %s\n", cap_error());
-				return -1;
-			}
-			free(d);			// TODO: why does this fail on attempt #245?
+	if(!d)
+	{
+		perror("Error receiving path from client");
+		return -1;
+	}
 
+	char path[256] = "";
+	int pathlen = 256;
 
-			printf("FD_FROM_PATH: %s\n", path);
-			int cap = cap_open(path, O_RDONLY, CAP_SET_FILE_READ);
+	if(cap_unmarshall_string(d, path, &pathlen) < 0)
+	{
+		fprintf(stderr, "Error unmarshalling path: %s\n", cap_error());
+		return -1;
+	}
+	free(d);
+
 
-			d = cap_marshall_int(1);
-			if(!d)
-			{
-				fprintf(stderr, "Error marshalling FD count: %s\n", cap_error());
-				return -1;
-			}
+	int cap = cap_open(path, O_RDONLY, CAP_SET_FILE_READ);
 
-			if(cap_send(client, d) < 0)
-			{
-				perror("Error sending FD count");
-				return -1;
-			}
-			free(d);
+	d = cap_marshall_int(1);
+	if(!d)
+	{
+		fprintf(stderr, "Error marshalling FD count: %s\n", cap_error());
+		return -1;
+	}
 
-			d = cap_marshall_string(path, pathlen);
-			if(!d)
-			{
-				fprintf(stderr, "Error marshalling FD path: %s\n", cap_error());
-				return -1;
-			}
+	if(cap_send(client, d) < 0)
+	{
+		perror("Error sending FD count");
+		return -1;
+	}
+	free(d);
 
-			if(cap_send_fd(client, d, &cap, 1) < 0)
-			{
-				perror("Error sending FD");
-				return -1;
-			}
-			close(cap);
-			free(d);
+	d = cap_marshall_string(path, pathlen);
+	if(!d)
+	{
+		fprintf(stderr, "Error marshalling FD path: %s\n", cap_error());
+		return -1;
+	}
 
-			return 0;
+	if(cap_send_fd(client, d, &cap, 1) < 0)
+	{
+		perror("Error sending FD");
+		return -1;
+	}
+	close(cap);
+	free(d);
 
-		case FD_POWERBOX:
-			puts("FD_POWERBOX");
+	return 0;
+}
 
-			struct capbox_options options;
-			fdlen = 1;
-			d = cap_recv_fds(client, &options.start_fd, &fdlen);
-			if(!d)
-			{
-				perror("Error receiving powerbox options");
-				return -1;
-			}
 
-			if(cap_unmarshall_capbox(d, &options) < 0)
-			{
-				fprintf(stderr, "Error unmarshalling powerbox options: %s",
-				        cap_error());
-				return -1;
-			}
+int handle_powerbox_request(int client)
+{
+	struct capbox_options options;
+	int fdlen = 1;
 
+	struct cap_wire_datum *d = cap_recv_fds(client, &options.start_fd, &fdlen);
+	if(!d)
+	{
+		perror("Error receiving powerbox options");
+		return -1;
+	}
 
-			// TODO: some more sophisticated per-client state (eg name)
-			options.window_title = (char*) malloc(80);
-			sprintf(options.window_title,
-			        "Powerbox for user_angel client %i", client);
+	if(cap_unmarshall_capbox(d, &options) < 0)
+	{
+		fprintf(stderr, "Error unmarshalling powerbox options: %s",
+		        cap_error());
+		return -1;
+	}
 
 
-			char *names[32];
-			int len = 32;
-			if(capbox_display(&options, fds, names, &len))
-			{
-				fprintf(stderr, "Error in powerbox\n");
-				return 0;
-			}
+	// TODO: some more sophisticated per-client state (eg name)
+	options.window_title = (char*) malloc(80);
+	sprintf(options.window_title,
+	        "Powerbox for user_angel client %i", client);
 
-			free(options.window_title);
 
+	int fds[32];
+	char *names[32];
+	int len = 32;
+	if(capbox_display(&options, fds, names, &len))
+	{
+		fprintf(stderr, "Error in powerbox\n");
+		return 0;
+	}
 
-			struct cap_wire_datum *fdcount = cap_marshall_int(len);
-			if(cap_send(client, fdcount) < 0)
-			{
-				perror("Error sending FD count");
-				return -1;
-			}
+	free(options.window_title);
 
-			for(int i = 0; i < len; i++)
-			{
-				const char *name = names[i];
-				struct cap_wire_datum *d
-					= cap_marshall_string(name, strlen(name));
 
-				if(cap_send_fd(client, d, fds + i, 1) < 0)
-				{
-					printf("Error sending file descriptor");
-					return -1;
-				}
-			}
+	struct cap_wire_datum *fdcount = cap_marshall_int(len);
+	if(cap_send(client, fdcount) < 0)
+	{
+		perror("Error sending FD count");
+		return -1;
+	}
 
+	for(int i = 0; i < len; i++)
+	{
+		const char *name = names[i];
+		struct cap_wire_datum *d
+			= cap_marshall_string(name, strlen(name));
 
-		default:
-			fprintf(stderr, "Unknown request %i\n", req);
+		if(cap_send_fd(client, d, fds + i, 1) < 0)
+		{
+			printf("Error sending file descriptor");
 			return -1;
+		}
 	}
 
 	return 0;

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/test_client.c#7 (text+ko) ====

@@ -17,6 +17,7 @@
 int connect_to_user_angel(void);
 void open_file(int fd_angel, const char *path);
 void open_powerbox(int fd_angel, const char *path, const char *filter, int parent);
+void test_fd(int fd, char *name);
 
 
 int main(int argc, char *argv[])
@@ -62,7 +63,7 @@
 
 	open_file(fd_angel, "/etc/group");
 	open_file(fd_angel, "/etc/passwd");
-	open_powerbox(fd_angel, "~/Desktop/", "*.txt", 0x2a00003);
+	open_powerbox(fd_angel, "~/Desktop/", "*.gz", 0x2a00003);
 
 	return 0;
 }
@@ -135,11 +136,7 @@
 			return;
 		}
 
-		printf("FD %i: %s\n", fd, name);
-
-		FILE *f = fdopen(fd, "r");
-		if(!f) err(EX_IOERR, "Error opening %s", name);
-		fclose(f);
+		test_fd(fd, name);
 	}
 }
 
@@ -156,6 +153,8 @@
 	options.mult = 1;
 	options.filter = filter;
 	options.filterlen = strlen(filter);
+	options.flags = O_RDWR;
+	options.rights = CAP_FSTAT | CAP_READ | CAP_WRITE | CAP_SEEK;
 
 
 	struct cap_wire_datum *data[2];
@@ -197,11 +196,28 @@
 			return;
 		}
 
-		printf("FD %i: %s\n", fd, name);
+		test_fd(fd, name);
+	}
+}
+
+
+void test_fd(int fd, char *name)
+{
+	printf("FD %i: %s\n", fd, name);
+
+	FILE *rf = fdopen(fd, "r");
+	if(!rf) err(EX_IOERR, "Error opening %s", name);
+	printf("Opened %s for reading\n", name);
+	fclose(rf);
 
-		FILE *f = fdopen(fd, "r");
-		if(!f) err(EX_IOERR, "Error opening %s", name);
-		fclose(f);
+	FILE *wf = fdopen(fd, "w");
+	if(wf)
+	{
+		printf("Opened %s for writing\n", name);
+		fclose(wf);
 	}
+	else printf("Couldn't open %s for writing\n", name);
+
+	close(fd);
 }
 

==== //depot/projects/trustedbsd/capabilities/src/tools/cap/user_angel/user_angel.c#4 (text+ko) ====

@@ -50,13 +50,6 @@
 
 #include "server.h"
 
-#define	BASE_CAPS	(CAP_IOCTL | CAP_EVENT | CAP_SEEK | CAP_FSTAT)
-#define	STDIN_CAPS	(BASE_CAPS | CAP_READ)
-#define	STDOUT_CAPS	(BASE_CAPS | CAP_WRITE)
-#define	STDERR_CAPS	(BASE_CAPS | CAP_WRITE)
-#define	BIN_CAPS	(CAP_SEEK | CAP_FSTAT | CAP_FSTATFS | CAP_READ | \
-			    CAP_FEXECVE | CAP_MMAP | CAP_MAPEXEC)
-
 
 void sighandle(int sig)
 {
@@ -94,83 +87,12 @@
 		return 1;
 	}
 
-
-
 	char address[128];
 	sprintf(address, "%s/.user-angel", homedir);
+
 	run_server(address);
-	/*
-	printf("Creating control socket at %s\n", control_socket_name);
-
-	struct sockaddr_un addr;
-	addr.sun_family = AF_UNIX;
-	strcpy(addr.sun_path, control_socket_name);
-
-	
-	fd_control = socket(AF_UNIX, SOCK_STREAM, 0);
-	if(fd_control == 0)
-	{
-		perror("Error creating control socket");
-		user_angel_shutdown();
-		return -1;
-	}
-
-	if(bind(fd_control, (struct sockaddr*) &addr, sizeof(struct sockaddr_un)))
-	{
-		perror("Error binding control socket");
-		user_angel_shutdown();
-		return -1;
-	}
-
-
-	if(listen(fd_control, 10))
-	{
-		perror("Error listening for connections");
-		user_angel_shutdown();
-		return -1;
-	}
-
-	int client;
-	struct sockaddr_un clientaddr;
-	int clientaddrlen;
-
-	client = accept(fd_control, &clientaddr, &clientaddrlen);
-	if(client <= 0)
-	{
-		perror("Error accepting client");
-		user_angel_shutdown();
-		return -1;
-	}
-
-	printf("Accepted client: %i\n", client);
-*/
 	user_angel_server_shutdown();
-	return 0;
-/*	fd_control = open(control_socket_name, O_RDONLY | O_NONBLOCK);
-
-
-	enum user_angel_request req;
-	while(1)
-	{
-		int bytes = read(fd_control, &req, 4);
-
-		if(bytes == 0) usleep(100);
-		else if(bytes > 0)
-		{
-			printf("Read %i bytes\n", bytes);
-			printf("Req: %x\n", req);
-		}
-		else
-		{
-			if(shutting_down) return 0;
-
-			perror("Error reading from control pipe");
-			break;
-		}
-	}
 
-	user_angel_server_shutdown();
-*/
 	return 0;
 }
 


More information about the p4-projects mailing list