add the listen1 command.
authorRonald G. Minnich <rminnich@google.com>
Tue, 17 Jun 2014 22:22:21 +0000 (15:22 -0700)
committerRonald G. Minnich <rminnich@google.com>
Tue, 17 Jun 2014 22:22:21 +0000 (15:22 -0700)
To use this command, e.g.
listen1 tcp!*!23 /bin/ash

Listens on port 23. The best thing we've found to talk to
it is netcat.

To make it available to localhost at 5555
qemu ... \
-net user,hostfwd=tcp::5555-:23 \

to talk to it:
netcat localhost 5555

Signed-off-by: Ronald G. Minnich <rminnich@google.com>
Signed-off-by: Ronald G. Minnich <rminnich@google.com>
scripts/plan9user/FIX
tests/listen1.c [new file with mode: 0644]

index bf6c659..2a26be8 100644 (file)
@@ -1,6 +1,6 @@
-spatch --in-place --sp-file ~/akaros/scripts/plan9user/scalar.cocci $1
-spatch --in-place --sp-file ~/akaros/scripts/plan9user/typedef.cocci $1
-spatch --in-place --sp-file ~/akaros/scripts/plan9user/plan9.cocci $1
+spatch --in-place --sp-file ~/projects/akaros/scripts/plan9user/scalar.cocci $1
+spatch --in-place --sp-file ~/projects/akaros/scripts/plan9user/typedef.cocci $1
+spatch --in-place --sp-file ~/projects/akaros/scripts/plan9user/plan9.cocci $1
 sh ~/akaros/scripts/plan9user/fixincludes $1
 sed -i 's/= nil/= NULL/' $1
 sed -i 's/ OREAD/ O_RDONLY/' $1
diff --git a/tests/listen1.c b/tests/listen1.c
new file mode 100644 (file)
index 0000000..508e229
--- /dev/null
@@ -0,0 +1,129 @@
+/* 
+ * This file is part of the UCB release of Plan 9. It is subject to the license
+ * terms in the LICENSE file found in the top-level directory of this
+ * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
+ * part of the UCB release of Plan 9, including this file, may be copied,
+ * modified, propagated, or distributed except according to the terms contained
+ * in the LICENSE file.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <parlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <iplib.h>
+#include <icmp.h>
+#include <ctype.h>
+#include <pthread.h>
+#include <spinlock.h>
+#include <timing.h>
+#include <tsc-compat.h>
+#include <printf-ext.h>
+#include <alarm.h>
+#include <ndb.h>
+#include <fcntl.h>
+
+int verbose;
+
+void
+usage(void)
+{
+       fprintf(stderr, "usage: listen1 [-tv] address cmd args...\n");
+       fprintf(stderr, "usage");
+       exit(1);
+}
+
+char*
+remoteaddr(char *dir)
+{
+       static char buf[128];
+       char *p;
+       int n, fd;
+
+       snprintf(buf, sizeof buf, "%s/remote", dir);
+       fd = open(buf, O_RDONLY);
+       if(fd < 0)
+               return "";
+       n = read(fd, buf, sizeof(buf));
+       close(fd);
+       if(n > 0){
+               buf[n] = 0;
+               p = strchr(buf, '!');
+               if(p)
+                       *p = 0;
+               return buf;
+       }
+       return "";
+}
+
+void
+main(int argc, char **argv)
+{
+       static char data[1024], dir[1024], ndir[1024];
+       int ctl, nctl, fd;
+
+       verbose = 1;
+
+       if(!verbose){
+               close(1);
+               fd = open("/dev/null", O_WRONLY);
+               if(fd != 1){
+                       dup2(fd, 1);
+                       close(fd);
+               }
+       }
+
+       argc--, argv++;
+       printf("listen started\n");
+       ctl = announce(argv[0], dir);
+       if(ctl < 0){
+               fprintf(stderr, "announce %s: %r", argv[0]);
+               exit(1);
+       }
+
+       for(;;){
+               nctl = listen(dir, ndir);
+               if(nctl < 0){
+                       fprintf(stderr, "listen %s: %r", argv[0]);
+                       exit(1);
+               }
+
+               //switch(rfork(RFFDG|RFPROC|RFNOWAIT|RFENVG|RFNAMEG|RFNOTEG)){
+               switch(fork()){
+               case -1:
+                       reject(nctl, ndir, "host overloaded");
+                       close(nctl);
+                       continue;
+               case 0:
+                       fd = accept(nctl, ndir);
+                       if(fd < 0){
+                               fprintf(stderr, "accept %s: can't open  %s/data: %r\n",
+                                       argv[0], ndir);
+                               exit(1);
+                       }
+                       printf("incoming call for %s from %s in %s\n", argv[0],
+                               remoteaddr(ndir), ndir);
+                       //fprintf(nctl, "keepalive");
+                       close(ctl);
+                       close(nctl);
+                       //putenv("net", ndir);
+                       /* this is for children that open /dev/cons. Too bad. 
+                       snprintf(data, sizeof data, "%s/data", ndir);
+                       bind(data, "/dev/cons", MREPL);
+                       */
+                       dup2(fd, 0);
+                       dup2(fd, 1);
+                       dup2(fd, 2);
+                       close(fd);
+                       execv(argv[1], argv+1);
+//                     if(argv[1][0] != '/')
+//                             exec(smprintf("%s", argv[1]), argv+1);
+                       fprintf(stderr, "exec: %r\n");
+                       exit(1);
+               default:
+                       close(nctl);
+                       break;
+               }
+       }
+}