summaryrefslogtreecommitdiff
path: root/quickexec/quickexec.cpp
blob: 772ab4ce487fc9d9361fb5ae117d2f37767cfe14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>

#define QUICKEXEC "/tmp/.quickexec"

#include <signal.h>
#include <sys/wait.h>

void sigchildhandler(int) {
    wait(0);
}

int exec( const char *lib, int argc, char** argv )
{
    setpgid(0,0);
// printf("loadlib %s, argc=%d\n", lib, argc );
    void *handle = dlopen ( lib, RTLD_LAZY);
    if (!handle) {
	fprintf( stderr, "%s\n", dlerror());
	exit(1);
    }
    typedef int (*Mainfunc)(int, char**);
    Mainfunc mainfunc;
    mainfunc = (Mainfunc) dlsym(handle, "main");
    char *error;
    if ((error = dlerror()) != NULL)  {
	fprintf (stderr, "%s\n", error);
	exit(1);
    }
    (*mainfunc)(argc,argv);
    return 0;
}

#define BUFFER_SIZE 1024
int main( int argc, char** argv )
{
    signal( SIGCHLD, sigchildhandler );
    (void) unlink( QUICKEXEC );
    if ( mkfifo( QUICKEXEC, S_IFIFO | S_IWUSR | S_IRUSR ) == -1 ) {
	perror( QUICKEXEC );
	exit(1);
    }
    
    if ( argc > 1 && fork() == 0 ) 
	return exec( argv[1], argc-2, argc > 2 ? argv+2 : 0 );
    
    char buf[BUFFER_SIZE];
    int p = 0;
    int r;
    int fd = open( QUICKEXEC, O_RDONLY );
    if ( fd == -1 ) {
	perror( QUICKEXEC );
	exit(1);
    }
    for ( ;; ) {
	r = read( fd, buf+p, BUFFER_SIZE-p );
	p += r;
	if ( r == 0 || p >= BUFFER_SIZE - 1 ) {
	    buf[p] = '\0';
	    close ( fd );
	    if ( fork() == 0 ) {
		int argc = -1;
		int i = 0;
		int k = 0;
		for ( i = 0; i <= p; i++ )
		    if ( buf[i] == '\0' )
			argc++;
		char** argv = new char*[argc];
		for ( i = 0; i < p; i++ ) {
		    if ( buf[i] == '\0' )
			argv[k++]=buf+i+1;
		}
		return exec( buf, argc, argv );
	    }
	    p = 0;
	    fd = open( QUICKEXEC, O_RDONLY );
	}
    }
    return 0;
}