Date: Sat, 27 Jul 2013 21:10:31 GMT From: dpl@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r255234 - soc2013/dpl Message-ID: <201307272110.r6RLAVQs010037@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dpl Date: Sat Jul 27 21:10:31 2013 New Revision: 255234 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255234 Log: This toy implementation it's almost working. There's only a problem when reading back from buf[] the return value of sum_(). Modified: soc2013/dpl/caller.c Modified: soc2013/dpl/caller.c ============================================================================== --- soc2013/dpl/caller.c Sat Jul 27 20:47:01 2013 (r255233) +++ soc2013/dpl/caller.c Sat Jul 27 21:10:31 2013 (r255234) @@ -10,87 +10,127 @@ #include <stdlib.h> #include <strings.h> + +struct pollfd fdread[1], fdwrite[1]; pid_t child = 0; int sv[2], i; -/* commands will live here */ -/* I will malloc it this afternoon */ -int buf[32]; +int *buf[32]; pid_t startChild(); -void worker(); -void setSignals(); void waitCommand(); -void * command(); void killChild(); +/* Toy lib */ +int sum(int a); +int sum_(int a); + int main() { - if(child == 0) - child = startChild(); - if(child < 0){ - perror("error"); - exit(1); - } - + int ret, a = 1; + ret = sum(a); + fflush(stdout); + printf("ret: %d (should be 2)\n", ret); + fflush(stdout); + atexit(killChild); return 0; } -int startChild() +int sum(int a) { - bzero(&buf, sizeof(buf)); - if( socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) < 0 ) - return -1; - - if( (child = fork()) == -1 ) { - return -1; - } else if (child == 0){ - worker(); + /* Caller overhead */ + /* Much better if we do this in a function */ + int ptr = (int)sum_; + fdwrite[0].fd = sv[0]; + fdwrite[0].events = POLLOUT; + fdwrite[0].revents = 0; + fdread[0].fd = sv[0]; + fdread[0].events = POLLIN|POLLPRI; + fdread[0].revents = 0; + if( child == 0) + startChild(); + + /* Real stuff */ + buf[0] = &ptr; + buf[1] = &a; + + /* Should be in a function as well */ + /* sendAndWait() (?) */ + while(1){ + if(poll(fdwrite, 1, 0) > 0 ){ + write(sv[0], buf, sizeof(buf)); + break; + } } - return child; + /* Here, the stuff gets done in child. */ + while(1){ + if(poll(fdread, 1, 0) > 0 ){ + read(sv[0], buf, sizeof(buf)); + break; + } + } + printf("done: %d\n", *buf[0]); + + return *buf[0]; } -void worker(){ - cap_rights_limit(STDIN_FILENO, CAP_WRITE); - close(STDIN_FILENO); - close(STDERR_FILENO); - - waitCommand(); - signal( - /* Should we call signal() to exit more cleanly? */ +int sum_(int a) +{ + return ++a; } +int startChild() +{ + socketpair(PF_LOCAL, SOCK_STREAM, 0, sv); + if( (child = fork()) == 0 ){ + cap_rights_limit(STDOUT_FILENO, CAP_WRITE); + close(STDIN_FILENO); + close(STDERR_FILENO); + cap_enter(); + waitCommand(); + } + + return child; +} /* Wait for commands, and execute them */ 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; - + struct pollfd fdread[1], fdwrite[1]; + fdread[0].fd = sv[1]; + fdread[0].events = POLLIN|POLLPRI; + fdread[0].revents = 0; + fdwrite[0].fd = sv[1]; + fdwrite[0].events = POLLOUT; + fdwrite[0].revents = 0; while(1) { - p = poll(fds, 1, 0); - if (p > 0){ - if( fds[0].revents & POLLIN || fds[0].revents & POLLPRI){ - /* Read command */ - read(sv[1], buf, sizeof(buf)); - } else if( fds[0].revents & POLLOUT) { - /* Write answer */ + while(1){ + if( poll(fdread, 1, 0) > 0){ + read(sv[1], buf, sizeof(buf)); + break; } } - } -} + if( (void *)*buf[0] == (void *) sum_ ){ + /* This is a C exercise }:-) */ + int ret = ((int (*) (int)) *buf[0])(*buf[1]); + /* int ret = sum_(*buf[1]); */ + printf("ret:%d\n", ret); + fflush(stdout); + bzero(&buf, sizeof(buf)); + buf[0] = &ret; + } -void * -command(){ - return NULL; + while(1) { + if (poll(fdwrite, 1, 0) > 0){ + write(sv[1], buf, sizeof(buf)); + break; + } + } + } } void
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201307272110.r6RLAVQs010037>