Date: Thu, 25 Jul 2013 11:02:17 GMT From: dpl@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r255155 - soc2013/dpl Message-ID: <201307251102.r6PB2HvU057947@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dpl Date: Thu Jul 25 11:02:17 2013 New Revision: 255155 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255155 Log: We now have a working child process that is ready to get commands and send the results. We still have to design the basic protocol, but it will be done reading and writing from/to buf. Modified: soc2013/dpl/caller.c Modified: soc2013/dpl/caller.c ============================================================================== --- soc2013/dpl/caller.c Thu Jul 25 10:29:40 2013 (r255154) +++ soc2013/dpl/caller.c Thu Jul 25 11:02:17 2013 (r255155) @@ -7,76 +7,94 @@ #include <signal.h> #include <stdio.h> +#include <stdlib.h> +#include <strings.h> -int childpid = 0; -int sv[2], buf[1024], i; -int buf[1024]; - -void listenAndServe(); -void setChildSignals(); -void getCommand(); -void sendCommand(int, int *); -void gotIO(int); -int startChild(); +pid_t child = 0; +int sv[2], i; +int buf[32]; + +pid_t startChild(); +void worker(); +void setSignals(); +void waitCommand(); +void * command(); +void killChild(); int main() { - int sv; - if(childpid == 0) - sv = startChild(); - sendCommand(0, buf); + if(child == 0) + child = startChild(); + if(child < 0){ + perror("error"); + exit(1); + } + + atexit(killChild); + printf("buf:\n"); + for( i=0; i<32; ++i){ + printf("%d\t", buf[i]); + if( (i+1) % 8 == 0 ) + printf("\n"); + } return 0; } int startChild() { - int buf[10] = { 0,1, 2, 3, 4, 5, 6, 7, 8, 9 }; + bzero(&buf, sizeof(buf)); if( socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) < 0 ) return -1; - if( (childpid = fork()) == -1 ) { + if( (child = fork()) == -1 ) { return -1; - } else if (childpid == 0){ - struct pollfd fds[1]; - int buf[1], p; - - cap_rights_limit(STDIN_FILENO, CAP_WRITE); - close(STDIN_FILENO); - close(STDERR_FILENO); - - setChildSignals(); - - fds[0].fd = sv[1]; - fds[0].events = POLLIN; - fds[0].revents = 0; - while( (p = poll(fds, 1, -1)) > 0 ) - printf("Received: %d\n", read(sv[1], buf, sizeof(int))); - printf("Poll has returned: %d\n", p); - } else { - for( i=0; i<10; i++){ - printf("sending: %d\n", buf[i]); - write(sv[0], (void *)buf[i], sizeof(int)); - } + } else if (child == 0){ + worker(); } - return sv[0]; + return child; } -void setChildSignals() -{ - +void worker(){ + cap_rights_limit(STDIN_FILENO, CAP_WRITE); + close(STDIN_FILENO); + close(STDERR_FILENO); + + waitCommand(); } + /* Wait for commands, and execute them */ -void listenAndServe() +void +waitCommand() { - + int p; + pid_t pid; + struct pollfd fds[1]; + fds[0].fd = sv[1]; + fds[0].events = POLLIN|POLLPRI|POLLOUT; + fds[0].revents = 0; + + while(1) { + if ((p = poll(fds, 1, 0)) > 0){ + if( fds[0].revents & POLLIN || fds[0].revents & POLLPRI){ + read(sv[1], buf, sizeof(buf)); + } else if( fds[0].revents & POLLOUT) { + } + } + } + printf("Child's out of while.\n"); } -void sendCommand(int c, int *buf){ +void * +command(){ + return NULL; } -void getCommand(){ - +void +killChild() +{ + printf("About to kill %d\n", child); + kill(child, SIGINT); } \ No newline at end of file
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307251102.r6PB2HvU057947>