All pastes #735188 Raw Edit

Anonymous

public c v1 · immutable
·published 2007-10-13 12:23 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <unistd.h>#include <errno.h>#include <sys/socket.h>#include <sys/types.h>#include <sys/wait.h>#include <fcntl.h>#include <string.h>#include <signal.h>#define FIFO_NAME "/tmp/ezxcompress"int recvSocket[2]; // we will receive data from ssh via thisint fd;int pid;int wpid;int status;void onUsr1(){    kill(pid, SIGUSR1);}void onTerm(){    // We will quit later when ssh die    printf("SIGKILL received. Killing ssh\n");    kill(pid, SIGTERM);}int main(int argc, char*argv[]){    if (argc!=2)    {	printf("No connection specifed.\n");	return 1;    }        char args[20][100];    char s[100];    int  argsps=0;    int found=0;    char *ezxConfig;        setenv("LD_LIBRARY_PATH","/mmc/mmca1/.system/lib",1);    ezxConfig=getenv("EZXCOMPRESS_CONFIG");    if (!ezxConfig)    {	printf("EZXCOMPRESS_CONFIG is not set\n");	return;    }    struct sigaction act;    // Compression info on SIGUSR1    memset(&act, 0, sizeof(act));    act.sa_handler=onUsr1;    act.sa_flags=SA_RESTART;    sigaction(SIGUSR1, &act, NULL);		    act.sa_handler=onTerm;    act.sa_flags=SA_RESTART;    sigaction(SIGTERM, &act, NULL);		    FILE *fp=fopen(ezxConfig,"r");    if (fp==0)    {	printf("No ezxcompress.cfg found.\n");	return 0;    }    // Default options for all connections    while (fscanf(fp,"%s",args[argsps])==1)    {	if (args[argsps][0]=='!')	    break;	argsps++;    }    // Search for profile    while (1)    {	fscanf(fp,"%s",&s);	if (!strcmp(s,argv[1]))	{	    found=1;	    break;	}	// Skip profile content	while (fscanf(fp,"%s",&s)==1)	{	    if (s[0]=='!')		break;	}	if (s[0]!='!')	    break;    }    if (!found)    {	printf("Profile \"%s\" not found.\n",argv[1]);	return 1;    }    while (fscanf(fp,"%s",args[argsps])==1)    {	if (args[argsps][0]=='!')	    break;	argsps++;    }        fclose(fp);        if (socketpair(AF_UNIX, SOCK_STREAM, 0, recvSocket))    {	perror("Unable to create socket: ");	return;    }    fcntl(recvSocket[0], F_SETFL, O_NONBLOCK);        signal(SIGPIPE, SIG_IGN); // Ignore sigpipe    pid=fork();    if (!pid)    {//	sleep(1);	printf("Will launch ssh now\n");	const char *rargs[50];	int i;		for (i=0; i<argsps; i++)	{	    printf("-> %s\n",args[i]);	    rargs[i]=args[i];	}	rargs[argsps]=NULL;        dup2(recvSocket[1], STDOUT_FILENO);	dup2(recvSocket[1], STDERR_FILENO);	close(recvSocket[0]);	execvp(rargs[0], (char* const*)rargs);	perror("Unable to launch ssh\n");	return;    }    printf("ssh pid=%d\n",pid);    close(recvSocket[1]);    if (mkfifo(FIFO_NAME, 0666) == -1 && errno!=EEXIST)    {	perror("Unable to create FIFO\n");	return -1;    }    printf("Opening fifo\n");    fd=open(FIFO_NAME, O_RDWR);    if (fd<0)    {	perror("Unable to open FIFO\n");	return -1;    }    printf("FIFO openend\n");    char buf[1001];    int nr;    while (1)    {	nr=read(recvSocket[0],buf,sizeof(buf)-1);	if (nr<=0)	{	    if (errno==11)	    {		wpid=waitpid(pid, &status, WNOHANG);		if (!wpid)		{			sleep(1);			continue;		}		else		{			printf("ssh terminated\n");			break;		}	    }	    perror("Unable to read from recvSocket: ");	    break;	}	buf[nr]=0x00;	write(fd,buf,nr);	printf("%s",buf);    }}