summaryrefslogtreecommitdiff
path: root/quickexec/quickexec.cpp
Unidiff
Diffstat (limited to 'quickexec/quickexec.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--quickexec/quickexec.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/quickexec/quickexec.cpp b/quickexec/quickexec.cpp
new file mode 100644
index 0000000..772ab4c
--- a/dev/null
+++ b/quickexec/quickexec.cpp
@@ -0,0 +1,98 @@
1#include <stdio.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <dlfcn.h>
7
8#define QUICKEXEC "/tmp/.quickexec"
9
10#include <signal.h>
11#include <sys/wait.h>
12
13void sigchildhandler(int) {
14 wait(0);
15}
16
17int exec( const char *lib, int argc, char** argv )
18{
19 setpgid(0,0);
20// printf("loadlib %s, argc=%d\n", lib, argc );
21 void *handle = dlopen ( lib, RTLD_LAZY);
22 if (!handle) {
23 fprintf( stderr, "%s\n", dlerror());
24 exit(1);
25 }
26 typedef int (*Mainfunc)(int, char**);
27 Mainfunc mainfunc;
28 mainfunc = (Mainfunc) dlsym(handle, "main");
29 char *error;
30 if ((error = dlerror()) != NULL) {
31 fprintf (stderr, "%s\n", error);
32 exit(1);
33 }
34 (*mainfunc)(argc,argv);
35 return 0;
36}
37
38#define BUFFER_SIZE 1024
39int main( int argc, char** argv )
40{
41 signal( SIGCHLD, sigchildhandler );
42 (void) unlink( QUICKEXEC );
43 if ( mkfifo( QUICKEXEC, S_IFIFO | S_IWUSR | S_IRUSR ) == -1 ) {
44 perror( QUICKEXEC );
45 exit(1);
46 }
47
48 if ( argc > 1 && fork() == 0 )
49 return exec( argv[1], argc-2, argc > 2 ? argv+2 : 0 );
50
51 char buf[BUFFER_SIZE];
52 int p = 0;
53 int r;
54 int fd = open( QUICKEXEC, O_RDONLY );
55 if ( fd == -1 ) {
56 perror( QUICKEXEC );
57 exit(1);
58 }
59 for ( ;; ) {
60 r = read( fd, buf+p, BUFFER_SIZE-p );
61 p += r;
62 if ( r == 0 || p >= BUFFER_SIZE - 1 ) {
63 buf[p] = '\0';
64 close ( fd );
65 if ( fork() == 0 ) {
66 int argc = -1;
67 int i = 0;
68 int k = 0;
69 for ( i = 0; i <= p; i++ )
70 if ( buf[i] == '\0' )
71 argc++;
72 char** argv = new char*[argc];
73 for ( i = 0; i < p; i++ ) {
74 if ( buf[i] == '\0' )
75 argv[k++]=buf+i+1;
76 }
77 return exec( buf, argc, argv );
78 }
79 p = 0;
80 fd = open( QUICKEXEC, O_RDONLY );
81 }
82 }
83 return 0;
84}
85
86
87
88
89
90
91
92
93
94
95
96
97
98