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.


4b7590c46c6d1efb5ce0b75d7e060557a6be5061
[palacios.git] / palacios / src / gears / ext_env_inject.c
1 /* 
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Kyle C. Hale <kh@u.norhtwestern.edu>
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Kyle C. Hale <kh@u.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19 #include <palacios/vmm.h>
20 #include <palacios/vm_guest.h>
21 #include <palacios/vmm_intr.h>
22 #include <palacios/vmm_extensions.h>
23
24 #include <gears/process_environment.h>
25 #include <gears/execve_hook.h>
26 #include <gears/env_inject.h>
27
28 static struct v3_env_injects env_injects;
29
30 static int free_env_inject (struct v3_vm_info * vm, struct v3_env_inject_info * inject) {
31     list_del(&(inject->inject_node)); 
32     V3_Free(inject);
33     return 0;
34 }
35
36 static int v3_env_inject_handler (struct guest_info * core, void * priv_data) {
37     int i = 0;
38     struct v3_env_inject_info * inject = (struct v3_env_inject_info*)priv_data;
39
40     for (; i < inject->num_env_vars; i++) {
41         PrintDebug(core->vm_info, core, "Envvar[%d]: %s\n", i, inject->env_vars[i]);
42     }
43
44     int ret = v3_inject_strings(core, (const char**)NULL, 
45                                 (const char**)inject->env_vars, 0, inject->num_env_vars);
46     if (ret == -1) {
47         PrintDebug(core->vm_info, core, "Error injecting strings in v3_env_inject_handler\n");
48         return -1;
49     }
50
51     return 0;
52 }
53
54 static int init_env_inject (struct v3_vm_info * vm, v3_cfg_tree_t * cfg, void ** priv_data) {
55     struct v3_env_injects * injects = &env_injects;
56     INIT_LIST_HEAD(&(injects->env_inject_list));
57     return 0;
58 }
59
60
61 static int deinit_env_inject (struct v3_vm_info * vm, void * priv_data) {
62     struct v3_env_injects * injects = &env_injects;
63     struct v3_env_inject_info * inject = NULL;
64     struct v3_env_inject_info * tmp = NULL;
65
66     list_for_each_entry_safe(inject, tmp, &(injects->env_inject_list), inject_node) {
67         free_env_inject(vm, inject);
68     }
69
70     return 0;
71 }
72
73
74 int v3_insert_env_inject (void * ginfo, char ** strings, int num_strings, char * bin_name) {
75     struct v3_env_injects * injects = &env_injects;
76     struct v3_env_inject_info * inject = V3_Malloc(sizeof(struct v3_env_inject_info));
77
78     if (!inject) {
79         PrintError(VM_NONE, VCORE_NONE, "Cannot allocate in inserting environment inject\n");
80         return -1;
81     }
82
83
84     memset(inject, 0, sizeof(struct v3_env_inject_info));
85
86     inject->env_vars = strings;
87     inject->num_env_vars = num_strings;
88     inject->bin_name = bin_name;
89
90     list_add(&(inject->inject_node), &(injects->env_inject_list));
91
92     v3_hook_executable((struct v3_vm_info *)ginfo, bin_name, v3_env_inject_handler, (void*)inject);
93
94     return 0;
95 }
96
97
98 int v3_remove_env_inject (struct v3_vm_info * vm, struct v3_env_inject_info * inject) {
99
100     if (v3_unhook_executable(vm, inject->bin_name) < 0) {
101         PrintError(vm, VCORE_NONE, "Problem unhooking executable in v3_remove_env_inject\n");
102         return -1;
103     }
104
105     free_env_inject(vm, inject);
106     return 0;
107 }
108
109
110 static struct v3_extension_impl env_inject_impl = {
111         .name = "env_inject",
112         .init = init_env_inject,
113         .deinit = deinit_env_inject,
114         .core_init = NULL,
115         .core_deinit = NULL,
116         .on_entry = NULL,
117         .on_exit = NULL
118 };
119
120 register_extension(&env_inject_impl);
121