Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Minor fix
[palacios.git] / gears / service_setup / inject_code_template.c
1 #include <fcntl.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4
5
6 /* 32-bit syscall numbers */
7 #define __NR_exit          1
8 #define __NR_fork          2
9 #define __NR_write         4
10 #define __NR_open          5
11 #define __NR_close         6
12 #define __NR_waitpid       7
13 #define __NR_execve       11
14
15 /* 32-bit system call conventions 
16  *
17  * eax = syscall nr
18  * ebx = arg 1
19  * ecx = arg 2
20  * edx = arg 3
21  * esi = arg 4
22  * edi = arg 5
23  * ebp = arg 6
24  */
25 int _start() {
26
27     int FD, bytes_written, status, exec_ret;
28     int flags = O_RDWR|O_CREAT; 
29     int mode = S_IRUSR|S_IWUSR|S_IXUSR;
30     pid_t pid, ret;
31     char * env[1];
32
33     env[0] = 0;
34
35 #include "generated.h"
36
37 #ifdef DO_WRITE
38     /* open("FILENAME, O_RDWR | O_CREAT,  */
39     asm volatile ("pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" 
40                 : "=a" (FD)
41                 : "0" (__NR_open), "r" (FILE_NAME), "c" (flags), "d" (mode)); 
42
43     if (!FD)
44         goto die;
45
46
47     /* write(FD, INJECT_FILE, FILE_LENGTH) */
48     asm volatile ("pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx" 
49                         : "=a" (bytes_written)
50                         : "0" (__NR_write), "r" (FD), "c" (inject_file), "d" (FILE_LENGTH));
51
52     if (!bytes_written)
53         goto die;
54
55
56     /* close(FD) */
57     asm volatile ("pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx" 
58                       : : "a" (__NR_close), "r" (FD));
59 #endif 
60
61
62 #ifdef DO_FORKEXEC
63     /* pid = fork() */
64     asm volatile ("int $0x80" : "=a" (pid) : "0" (__NR_fork));
65
66
67     if (pid < 0) {
68         goto die;
69     } else if (pid > 0) {
70
71         do {
72             /* ret = waitpid(pid, &status, 0) */
73             asm volatile ("pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
74                         : "=a" (ret)
75                         : "0" (__NR_waitpid), "r" (pid), "c" (&status), "d" (0));
76
77         } while (ret == -1);
78
79     } else {
80
81         /* execve("command", "arg0" , ..., "argN" , env) */
82         asm volatile ("pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
83                     : "=a" (exec_ret)
84                     : "0" (__NR_execve), "r" (CMD), "c" (args), "d" (env));
85         
86         if (exec_ret < 0)
87             /* exit(127) */
88             asm volatile ("pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
89                           : : "a" (__NR_exit), "r" (127));
90     }
91 #endif
92
93     die:
94         /* hypercall(f001) <=> exit(0) */
95         asm volatile ("movl $0xf001, %eax");
96         asm volatile ("vmmcall");
97         /* exit(1) */
98         asm volatile ("pushl %%ebx; movl %1,%%ebx; int $0x80; popl %%ebx"
99                       : : "a" (__NR_exit), "r" (1));
100 }