9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] a pair nec bugs
Date: Fri, 20 May 2011 23:27:05 -0400	[thread overview]
Message-ID: <edcfbdc9e2482002efd4dc8adb011fa7@brasstown.quanstro.net> (raw)
In-Reply-To: <BANLkTim3p0FqDzey=gaBRDordN2BSB5j2A@mail.gmail.com>

here's an improved version.  the previous version had a problem when
shared memory but not shared fd in this situation
	p0:
		nsec()	-> fd 3
		...
	p1:
		open()	-> fd 3
		nsec()	-> fail

one potential modification is to ditch the malloc and allocate
two privalloc entries.

one interesting bit that is not documented in the privalloc(2) man
page is that privalloc() allocations are shared and inherited when
a process forks, but the memory spaces are not.  thus a privalloc
for each new pid would be an error.

it would require a modification to kexit() to change this.  but it might
make sense, because sharing privalloc entries seems exactly the
opposite of what privalloc is supposed to be doing.

- erik

---

#include <u.h>
#include <libc.h>
#include <tos.h>

typedef	struct	Nfd	Nfd;
struct Nfd {
	int	pid;
	int	fd;
};

static	void	**nsecpriv;
#define	Fd	((Nfd*)nsecpriv[0])


static uvlong order = 0x0001020304050607ULL;

static void
be2vlong(vlong *to, uchar *f)
{
	uchar *t, *o;
	int i;

	t = (uchar*)to;
	o = (uchar*)&order;
	for(i = 0; i < sizeof order; i++)
		t[o[i]] = f[i];
}

static Nfd*
getfd(void)
{
	Nfd *p;

	if(nsecpriv != nil && Fd->pid == _tos->pid)
		return Fd;
	if(nsecpriv == nil){
		/*
		 * privalloc's allocates slots on a shared
		 * basis, even though the memory slots
		 * themselves are proc-private.
		 */
		nsecpriv = privalloc();
		if(nsecpriv == nil)
			return nil;
		*nsecpriv = p = malloc(sizeof *p);
		if(p == nil)
			return nil;
	}else
		p = *nsecpriv;
	p->fd = -1;
	return p;
}

vlong
nsec(void)
{
	uchar b[8];
	vlong t;
	Nfd *p;

	if((p = getfd()) == nil)
		return 0;
	if(p->fd == -1){
		p->fd = open("/dev/bintime", OREAD|OCEXEC);
		p->pid = _tos->pid;
	}
	if(pread(p->fd, b, sizeof b, 0) == sizeof b){
		be2vlong(&t, b);
		return t;
	}
	return 0;
}



  parent reply	other threads:[~2011-05-21  3:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-20  1:30 erik quanstrom
2011-05-20  5:05 ` [9fans] a pair nsec bugs erik quanstrom
2011-05-20 10:43 ` [9fans] a pair nec bugs ron minnich
2011-05-20 10:52   ` roger peppe
2011-05-20 10:57     ` ron minnich
2011-05-20 12:47   ` erik quanstrom
2011-05-20 19:03     ` ron minnich
2011-05-20 19:16       ` erik quanstrom
2011-05-21  3:27       ` erik quanstrom [this message]
2011-05-21  9:59         ` Charles Forsyth
2011-05-21 12:16           ` erik quanstrom
2011-05-21 21:50           ` erik quanstrom
2011-05-21 22:14             ` Charles Forsyth
2011-05-22  3:06               ` erik quanstrom
2011-05-22 13:30                 ` Charles Forsyth
2011-06-02 19:16                   ` erik quanstrom
2011-06-03  9:52                     ` Charles Forsyth
2011-06-04 18:12                       ` adriano
2011-06-03 13:13 erik quanstrom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=edcfbdc9e2482002efd4dc8adb011fa7@brasstown.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).