From owner-svn-soc-all@FreeBSD.ORG Sat Jul 27 21:10:31 2013 Return-Path: Delivered-To: svn-soc-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id AE71DEA5 for ; Sat, 27 Jul 2013 21:10:31 +0000 (UTC) (envelope-from dpl@FreeBSD.org) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 957EE256C for ; Sat, 27 Jul 2013 21:10:31 +0000 (UTC) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.14.7/8.14.7) with ESMTP id r6RLAVvV010049 for ; Sat, 27 Jul 2013 21:10:31 GMT (envelope-from dpl@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.14.7/8.14.6/Submit) id r6RLAVQs010037 for svn-soc-all@FreeBSD.org; Sat, 27 Jul 2013 21:10:31 GMT (envelope-from dpl@FreeBSD.org) Date: Sat, 27 Jul 2013 21:10:31 GMT Message-Id: <201307272110.r6RLAVQs010037@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to dpl@FreeBSD.org using -f From: dpl@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r255234 - soc2013/dpl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Jul 2013 21:10:31 -0000 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 #include + +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