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.


make v3_launch fail on ioctl error
[palacios.git] / linux_usr / v3_env_inject.c
1 /* 
2  * V3 Environment Variable Injection Utility
3  * This code allows a user to inject environment variables into a process
4  * marked by a specific binary name in a running guest.
5  *
6  * (c) Kyle C. Hale, 2012
7  */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
16
17 #include "iface-env-inject.h"
18
19
20 static void usage (char* bin) {
21         fprintf(stderr, "usage: %s /dev/v3-vm<N> env-file inject-point-exe\n", bin);
22 }
23
24 int main (int argc, char **argv) {
25         char *vm_dev, *env_file, *bin_name;
26         int vm_fd, err, bytes_read, num_strings;
27     struct stat t_stat;
28     struct env_data env;
29     char * strings[MAX_NUM_STRINGS];
30     char tmp_str[MAX_STRING_LEN];
31     int i = 0;
32     FILE * t_fd;
33
34         if (argc < 4) {
35                 usage(argv[0]);
36                 return -1;
37         }
38
39         vm_dev = argv[1];
40     env_file = argv[2];
41     bin_name = argv[3];
42
43         t_fd = fopen(env_file, "r");
44         if (!t_fd) {
45                 fprintf(stderr, "Error opening environment variable file: %s\n", env_file);
46                 return -1;
47         }
48
49     /* copy in the vars line by line */
50     while (fgets(tmp_str, MAX_STRING_LEN, t_fd) != NULL) {
51         int len = strlen(tmp_str) - 1;
52         if (tmp_str[len] == '\n')
53             tmp_str[len] = 0;
54         strings[i] = (char*)malloc(MAX_STRING_LEN);
55         if (!strings[i]) {
56                 fprintf(stderr, "Error allocating space for variable\n");
57                 return -1;
58         }
59         strcpy(strings[i], tmp_str);
60         i++;
61     }
62
63     env.num_strings = i;
64     printf("Found %d environment variables to inject\n", i);
65
66     env.strings = (char**) strings;
67
68     if (!bin_name) {
69         fprintf(stderr, "Error: no binary hook provided\n");
70         return -1;
71     }
72
73     strncpy(env.bin_name, bin_name, MAX_STRING_LEN);
74
75         vm_fd = open(vm_dev, O_RDONLY);
76         if (vm_fd == -1) {
77                 fprintf(stderr, "Error opening VM device: %s\n", vm_dev);
78                 return -1;
79         }
80
81     printf("Transferring control to Palacios\n");
82         err = ioctl(vm_fd, V3_VM_ENV_INJECT, &env);
83         if (err < 0) {
84                 fprintf(stderr, "Error providing env var data to palacios\n");
85                 return -1;
86         }
87     
88         close(t_fd);
89         close(vm_fd);
90         return 0;
91 }