9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] Unix trampoline.
@ 2003-02-17  4:20 okamoto
  0 siblings, 0 replies; 5+ messages in thread
From: okamoto @ 2003-02-17  4:20 UTC (permalink / raw)
  To: 9fans

> Now, if you'll excuse me, I've
> got to catch a train to St. Petersburg....

Then, you can enjoy the play of orchestra conducted
by Gergiev (Spell?).
I envy you.

Kenji



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] Unix trampoline.
  2003-02-17  3:37 Dan Cross
  2003-02-17  3:47 ` Dan Cross
@ 2003-02-17  7:16 ` Dan Cross
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Cross @ 2003-02-17  7:16 UTC (permalink / raw)
  To: 9fans

(Of course, one can also use sshnet, but where's the fun in that?)

	- Dan C.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] Unix trampoline.
@ 2003-02-17  4:21 okamoto
  0 siblings, 0 replies; 5+ messages in thread
From: okamoto @ 2003-02-17  4:21 UTC (permalink / raw)
  To: 9fans

> Now, if you'll excuse me, I've
> got to catch a train to St. Petersburg....

Then, you can enjoy the play of orchestra conducted
by Gergiev (Spell?).
I envy you.

Kenji



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] Unix trampoline.
  2003-02-17  3:37 Dan Cross
@ 2003-02-17  3:47 ` Dan Cross
  2003-02-17  7:16 ` Dan Cross
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Cross @ 2003-02-17  3:47 UTC (permalink / raw)
  To: 9fans

> It works like a charm; sources is now mounted on /n/sources.  If I:
>
> 	bind /n/sources /n/dist

Whoops, did you catch the error here?  It should be:

	bind /n/sources/plan9 /n/dist

> 	mount -c /srv/boot /n/inst
> 	replica/pull -v /n/dist/dist/replica/inst

[...]

> 	#!/bin/rc
> 	rfork n
> 	srv -m tcp!sources.cs.bell-labs.com sources /n/dist

Similarily here.  This should be:

	9fs sources
	bind /n/sources/plan9 /n/dist

> 	mount -c /srv/boot /n/inst
> 	replica/pull -v /n/dist/dist/replica/inst
>
> Put this into a script and you're good to go; that's how I update my
> system.

Okay, I lied; I use a working script.  Now, if you'll excuse me, I've
got to catch a train to St. Petersburg....

	- Dan C.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [9fans] Unix trampoline.
@ 2003-02-17  3:37 Dan Cross
  2003-02-17  3:47 ` Dan Cross
  2003-02-17  7:16 ` Dan Cross
  0 siblings, 2 replies; 5+ messages in thread
From: Dan Cross @ 2003-02-17  3:37 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 2694 bytes --]

In an effort to avoid spending all day messing with computers, I left
my laptop at home today.  However, I then came down with a fever, and
haven't been able to do any of the things I wanted to do other than
play with a computer.  (Ever try conjugating verbs at 105 degrees?
Wow, talk about trippy.  I actually thought I was in Moscow at one
point.)

I decided during the course of the day that I'd really like to pull
from sources, but couldn't do that because my file server isn't
currently hooked up to the Internet.  However, my girlfriend's laptop
is, and is running MacOS X, and has X11 and drawterm installed.  So, I
sat down and wrote a quick clone of aux/trampoline for Unix.  This did
the trick.

Here's how it works: her airport card provides the connection to the
Internet.  The built-in ethernet connects to the network with the file
and CPU server and gets assigned the name, ``rudra''.  I run trampoline
on her laptop twice to forward connections for both 9fs and ticket to
sources.  I run drawterm and connect to the CPU server, where I sweep a
window, fudge with my namespace a little, and run an ndb/cs that
returns rudra when queried for the auth server for the
outside.plan9.bell-labs.com domain.  Then I start a factotum and just:

	srv -m tcp!rudra sources /n/sources

It works like a charm; sources is now mounted on /n/sources.  If I:

	bind /n/sources /n/dist
	mount -c /srv/boot /n/inst
	replica/pull -v /n/dist/dist/replica/inst

I end up updating from sources more or less normally.  Note, that's a
handy way to do an update if you're on a fileserver.  You could throw:

	#!/bin/rc
	rfork n
	srv -m tcp!sources.cs.bell-labs.com sources /n/dist
	mount -c /srv/boot /n/inst
	replica/pull -v /n/dist/dist/replica/inst

Into a script and you're good to go; that's how I update my system.

Anyway, I'm including the Unix version of trampoline here for those who
might find it useful.  Note that I turn off nagle everywhere I possibly
can (probably more places than I need), and I also don't worry about a
xfer9p function.  Since (most) versions of Unix don't do il, and il is
going away anyway, it's not needed.  TCP already deals with coalescing
packets; xfer9p is only needed for 9p over il, where message delimiters
are preseved by individual read and write calls.  It's also interesting
to look at the hoops I jump through to avoid using select().  I fork a
couple of processes to handle the I/O, which is okay, but synchronize
them using process groups; Unix process groups are cheesey.

Any comments are appreciated.  I've compiled it under MacOS X and
Solaris 8, but only tested it on the Mac.

	- Dan C.


[-- Attachment #2: Type: text/plain, Size: 2876 bytes --]

/*
 *  This is useful for mounting something that serves 9p.
 *
 *  Dan Cross <cross@math.psu.edu>
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#include <netdb.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
nonagle(int sd)
{
	int	one;

	one = 1;
	setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
}

int
announceon(int port)
{
	struct	sockaddr_in srv;
	int	sd;

	sd = socket(AF_INET, SOCK_STREAM, 0);
	if (sd < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}
	memset(&srv, 0, sizeof(srv));
	srv.sin_family = AF_INET;
	srv.sin_port = htons(port);
	srv.sin_addr.s_addr = htonl(INADDR_ANY);
	if (bind(sd, (struct sockaddr *)&srv, sizeof(srv)) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}
	if (listen(sd, 5) < 0) {
		perror("listen");
		exit(EXIT_FAILURE);
	}
	return(sd);
}

int
listento(int sd)
{
	struct	sockaddr_in cli;
	int	csize;

	csize = sizeof(cli);
	memset(&cli, 0, csize);
	sd = accept(sd, (struct sockaddr *)&cli, &csize);
	if (sd < 0)
		return(sd);
	nonagle(sd);
	return(sd);
}

int
dialto(char *host, int port)
{
	struct	hostent *h;
	struct	sockaddr_in srv;
	int	sd;

	h = gethostbyname(host);
	if (h == NULL) {
		return(-1);
	}
	sd = socket(AF_INET, SOCK_STREAM, 0);
	if (sd < 0) {
		return(-1);
	}
	memset(&srv, 0, sizeof(srv));
	srv.sin_family = AF_INET;
	srv.sin_port = htons(port);
	memmove(&srv.sin_addr.s_addr, h->h_addr, h->h_length);
	if (connect(sd, (struct sockaddr *)&srv, sizeof(srv)) < 0) {
		close(sd);
		return(-1);
	}
	nonagle(sd);
	return(sd);
}

void
xfer(int from, int to)
{
	int	n;
	char	buf[8192];

	for ( ; ; ) {
		n = read(from, buf, sizeof(buf));
		if (n <= 0)
			return;
		if (write(to, buf, n) != n)
			return;
	}
}

void
srv(int myport, char *host, int port)
{
	int	ad, sd, fd, pid;

	ad = announceon(myport);
	for ( ; ; ) {
		sd = listento(ad);
		if (sd < 0) {
			perror("listento");
			continue;
		}
		fd = dialto(host, port);
		if (fd < 0) {
			perror("dialto");
			close(sd);
			continue;
		}
		pid = fork();
		if (pid != 0) {		/*  Parent or error.  */
			close(sd);
			close(fd);
			continue;
		}
		/*  This is the child.  */
		setpgid(0, getpid());
		pid = fork();
		if (pid < 0)
			_exit(EXIT_FAILURE);
		if (pid == 0)
			xfer(fd, sd);
		else
			xfer(sd, fd);
		killpg(getpgid(0), SIGTERM);
	}
}

void
reaper(int sig)
{
	wait(NULL);
}

int
main(int argc, char *argv[])
{

	if (argc != 4) {
		fprintf(stderr, "Usage: trampoline port host port\n");
		exit(EXIT_FAILURE);
	}
	/*  Handlers stay installed with signal() these days.  */
	signal(SIGCHLD, reaper);
	srv(atoi(argv[1]), argv[2], atoi(argv[3]));

	return(EXIT_SUCCESS);
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-02-17  7:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-17  4:20 [9fans] Unix trampoline okamoto
  -- strict thread matches above, loose matches on Subject: below --
2003-02-17  4:21 okamoto
2003-02-17  3:37 Dan Cross
2003-02-17  3:47 ` Dan Cross
2003-02-17  7:16 ` Dan Cross

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).