/*
 * This is in a different file so we don't drag in 
 * libthread (for threadmalloc) unless it's actually
 * getting used.
 */

#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/auth.h>
#include <9pm/fcall.h>
#include <9pm/thread.h>
#include "9p.h"

typedef struct Postcrud Postcrud;
struct Postcrud {
	int fd[2];
	Srv *s;
};

static void
threadsrvproc(void *v)
{
	Postcrud *p;

	p = v;
	if(!p->s->nopipe)
		close(p->fd[0]);
	srv(p->s);
	if(p->s->end)
		p->s->end(p->s);
	threadexits(nil);
}

void
threadpostmountsrv(Srv *s, char *name, char *mtpt, int flag)
{
	Postcrud *p;

	p = emalloc9p(sizeof(*p));
	if(s->nopipe){
		if(name || mtpt){
			threadprint(2, "threadpostmountsrv: can't post or mount with nopipe\n");
			threadexitsall("nopipe");
		}
	}else{
		if(pipe(p->fd) < 0){
			threadprint(2, "pipe: %r\n");
			threadexitsall("pipe");
		}
		if(name)
			if(postfd(name, p->fd[0]) < 0){
				threadprint(2, "postfd %s: %r", name);
				threadexitsall("postfd");
			}
		s->infd = p->fd[1];
		s->outfd = p->fd[1];
	}
	p->s = s;

	if(procrfork(threadsrvproc, p, 32*1024, RFFDG|RFNAMEG) < 0) {
		threadprint(2, "procrfork: %r\n");
		threadexitsall("procrfork");
	}

	if(!s->nopipe){
		if(mtpt)
			if(amount(p->fd[0], mtpt, flag, "") < 0)
				threadprint(2, "mount at %s fails: %r\n", mtpt);
		close(p->fd[0]);
	}
}
