Mercurial > hg > Applications > virsh-wrapper
annotate ie-virsh.c @ 50:7a02dee972c4
easy fix randomUUID
author | kono |
---|---|
date | Tue, 30 Jan 2018 22:13:22 +0900 |
parents | b8887c40f8b8 |
children | e45676e6b6cd |
rev | line source |
---|---|
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <stdlib.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include <unistd.h> |
43 | 4 #include <stdbool.h> |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include <stdio.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 #include <sys/types.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 #include <unistd.h> |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 #include <signal.h> |
2 | 10 #include <string.h> |
1 | 11 #include <regex.h> |
39 | 12 #include <pwd.h> |
46
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
13 #include <sys/socket.h> |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
14 #include <netinet/in.h> |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
15 #include <arpa/inet.h> |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
16 #include <time.h> |
1 | 17 |
21 | 18 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 |
29 | 20 #define command "/usr/bin/virsh" |
21 /* #define command "/usr/local/bin/virsh" */ | |
22 #define list_command "/usr/bin/virsh list --all" | |
23 /* #define list_command "/usr/local/bin/virsh list --all" */ | |
2 | 24 #define start_command "start" |
3 | 25 #define stop_command "destroy" |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
26 #define define_command "define" |
9 | 27 #define undefine_command "undefine" |
4
349bbbd3fbd5
added dumpxml command
Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
parents:
3
diff
changeset
|
28 #define dumpxml_command "dumpxml" |
22 | 29 #define console_command "console" |
32 | 30 #define debug_command "debug" |
43 | 31 #define define_gdb_command "define-gdb" |
1 | 32 |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
33 |
50 | 34 const char *guests[] = {"145165B"}; |
34 | 35 const char *managers[] = {"atton"}; |
40 | 36 const char *students_identifier = "student"; |
39 | 37 const char *teachers_identifier = "teacher"; |
38 const char *students_sym = "students"; | |
39 const char *teachers_sym = "teachers"; | |
40 const char *managers_sym = "managers"; | |
41 const char *guests_sym = "guests"; | |
42 const char *delimiter = "_"; | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
43 |
32 | 44 static char bad_name[] = "Bad vmname. Try 01 or 02 ... 04\n"; |
9 | 45 |
41 | 46 const char newvm_command[] = "/usr/local/bin/newvm.py -c /etc/libvirt/qemu/ie-virsh-template.xml -n "; |
35 | 47 |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
48 enum { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
49 NAME_LENGTH = 50, |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
50 VM_NAME_LENGTH = 50 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
51 }; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
52 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
53 enum { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
54 STUDENTS, |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
55 GUESTS, |
39 | 56 MANAGERS, |
57 TEACHERS | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
58 }; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
59 |
1 | 60 #define VMNAME_MAX (512) |
61 | |
62 typedef struct vmlist { | |
63 char name[VMNAME_MAX]; | |
64 struct vmlist *next; | |
65 } VMLIST, *VMLISTPTR; | |
66 | |
67 #define NEW(type) ((type*)malloc(sizeof(type))) | |
68 | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 /* Define global variables */ |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 |
22 | 71 VMLISTPTR |
37 | 72 get_vmlist(char *list_pattern_str) |
1 | 73 { |
37 | 74 regex_t *list_pattern = NEW(regex_t); |
75 if (regcomp(list_pattern, list_pattern_str, 0) != 0) { | |
76 exit(0); | |
77 } | |
78 | |
1 | 79 VMLISTPTR list = NEW(VMLIST); |
80 VMLISTPTR p = list ; | |
81 p->name[0] = 0; | |
82 p->next = 0; | |
83 FILE *fp = popen(list_command,"r"); | |
84 while(fgets(p->name,VMNAME_MAX,fp)!=NULL) { | |
85 if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue; | |
86 p->next = NEW(VMLIST); | |
87 p = p->next; | |
88 } | |
89 p->name[0] = 0; | |
90 pclose(fp); | |
37 | 91 regfree(list_pattern); |
1 | 92 return list; |
93 } | |
94 | |
36 | 95 void |
96 print_vmlist(VMLISTPTR list) | |
1 | 97 { |
98 for(;list && list->name[0]; list=list->next) { | |
99 fprintf(stdout, " %s\n",list->name); | |
100 } | |
101 } | |
102 | |
37 | 103 void |
104 check_vmlist_name(VMLISTPTR list, char * name) | |
1 | 105 { |
106 for(;list && list->name[0]; list=list->next) { | |
37 | 107 if (strstr(list->name, name)!=0) return; |
1 | 108 } |
37 | 109 |
110 fprintf(stderr, bad_name); | |
111 print_vmlist(list); | |
112 exit(1); | |
1 | 113 } |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
115 void |
29 | 116 change_delimiter_to_slash(char *name) |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
117 { |
29 | 118 long size = sizeof(char) * VM_NAME_LENGTH; |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
119 int i = 0; |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
120 for (; i < size; i++) { |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
121 if (name[i] == '_') { |
29 | 122 name[i] = '/'; |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
123 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
124 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
125 } |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
126 |
22 | 127 int |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
128 check_name(const char *p) |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
129 { |
21 | 130 if (!p) return 1; |
131 for(;*p;p++) { | |
132 char c = *p; | |
133 if (c<=' ') return 1; | |
134 if (('a'<=c && c<='z') || | |
135 ('0'<=c && c<='9') || | |
136 ('/'==c ) || | |
137 ('-'==c )) continue; | |
138 return 1; | |
139 printf("%c", c); | |
140 } | |
141 return 0; | |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
142 } |
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
143 |
36 | 144 void |
3 | 145 usage() |
146 { | |
49 | 147 printf("Usage: COMMAND [define|define-gdb|list|start|destroy|dumpxml|vncdisplay|undefine|console] [vm-name]\n"); |
32 | 148 printf(" vm-name should be 01 - 04\n"); |
3 | 149 } |
150 | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
151 int |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
152 check_user_name(const char *account_name) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
153 { |
39 | 154 const char *home = getpwuid(getuid())->pw_dir; |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
155 |
39 | 156 if (strstr(home, students_identifier) != NULL) { |
157 return STUDENTS; | |
158 } else if (strstr(home, teachers_identifier) != NULL) { | |
159 return TEACHERS; | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
160 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
161 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
162 const int managers_num = sizeof(managers) / sizeof(managers[0]); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
163 int i = 0; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
164 for (; i < managers_num; i++) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
165 if (strncmp(account_name, managers[i], NAME_LENGTH) == 0) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
166 return MANAGERS; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
167 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
168 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
169 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
170 const int guests_num = sizeof(guests) / sizeof(guests[0]); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
171 int j = 0; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
172 for (; j < guests_num; j++) { |
50 | 173 if (strncmp(account_name, guests[j], NAME_LENGTH) == 0) { return GUESTS; |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
174 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
175 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
176 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
177 return -1; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
178 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
179 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
180 void |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
181 bind_name(char *name, const char *first, const char *second) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
182 { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
183 strncat(name, first, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
184 strncat(name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
185 strncat(name, second, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
186 strncat(name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
187 return; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
188 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
189 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
190 void |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
191 make_vm_name(char *vm_name, const int account_type, const char *account_name, const char *vm_num) |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
192 { |
36 | 193 const char *regex = "0[1-4]"; |
194 regex_t *num_pattern = NEW(regex_t); | |
195 | |
196 if (regcomp(num_pattern, regex, REG_EXTENDED|REG_NEWLINE) != 0) { | |
197 exit(0); | |
198 } | |
199 | |
200 int ret = regexec(num_pattern, vm_num, (size_t) 0, NULL, 0); | |
201 regfree(num_pattern); | |
202 | |
203 if (ret){ | |
204 fprintf(stderr, "This name is invalid number: %s\n", vm_num); | |
205 exit(0); | |
206 } | |
207 | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
208 switch(account_type) { |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
209 case STUDENTS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
210 strncat(vm_name, students_sym, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
211 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
212 strncat(vm_name, account_name, 3); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
213 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
214 strncat(vm_name, account_name, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
215 strncat(vm_name, delimiter, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
216 break; |
39 | 217 case TEACHERS: |
218 strncat(vm_name, teachers_sym, VM_NAME_LENGTH); | |
219 strncat(vm_name, delimiter, VM_NAME_LENGTH); | |
220 strncat(vm_name, account_name, VM_NAME_LENGTH); | |
221 strncat(vm_name, delimiter, VM_NAME_LENGTH); | |
222 break; | |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
223 case GUESTS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
224 bind_name(vm_name, guests_sym, account_name); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
225 break; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
226 case MANAGERS: |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
227 bind_name(vm_name, managers_sym, account_name); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
228 break; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
229 default : |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
230 fprintf(stderr, "Error: no registered type name."); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
231 return; |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
232 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
233 |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
234 strncat(vm_name, vm_num, VM_NAME_LENGTH); |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
235 } |
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
236 |
46
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
237 int |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
238 get_kernel_debug_port(void){ |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
239 struct sockaddr_in addr; |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
240 memset(&addr, 0, sizeof(addr)); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
241 addr.sin_family = AF_INET; |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
242 addr.sin_addr.s_addr = htonl(INADDR_ANY); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
243 int socket_fd = socket(AF_INET, SOCK_STREAM, 0); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
244 |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
245 int i; |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
246 srand(time(NULL)); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
247 for (i = 0; i < 10; i++) { |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
248 addr.sin_port = htons((rand() % 20000) + 10000); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
249 |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
250 if ((bind(socket_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr) ) == 0) && (listen(socket_fd, 1) == 0)) { |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
251 printf("Port %d was allocated for your kernel debug.\n", ntohs(addr.sin_port)); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
252 close(socket_fd); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
253 return ntohs(addr.sin_port); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
254 } else { |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
255 printf("Port %d was used. Trying another port...\n", ntohs(addr.sin_port)); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
256 } |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
257 } |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
258 printf("Cannot allocate debug port.\n"); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
259 exit(EXIT_FAILURE); |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
260 } |
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
261 |
37 | 262 void |
44
546504699402
Generate xml which enabled kernel debug. but needs reload in virsh
atton
parents:
43
diff
changeset
|
263 create_new_vm(const char const *name, const char const *vm_name, char* xml_name, bool debug_kernel){ |
37 | 264 if (check_name(name)) { |
265 fprintf(stderr, bad_name); | |
266 exit(0); | |
267 } | |
268 char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); | |
269 strncpy(vm_path, vm_name, VM_NAME_LENGTH); | |
270 change_delimiter_to_slash(vm_path); | |
271 char exec[1024]; | |
43 | 272 if (debug_kernel) { |
46
6b1dddd65d7c
Generate kernel debug port randomly between 10000 to 30000
atton
parents:
44
diff
changeset
|
273 snprintf(exec, 1024, "%s %s -d %d", newvm_command, vm_path, get_kernel_debug_port()); |
42 | 274 } else { |
43 | 275 snprintf(exec, 1024, "%s %s", newvm_command, vm_path); |
42 | 276 } |
37 | 277 fprintf(stdout, "excuting %s\n",exec ); |
278 system(exec); | |
279 strncpy(xml_name, vm_path,900); | |
280 strncat(xml_name,".xml",1000); | |
281 free(vm_path); | |
282 } | |
283 | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
284 /* main(int argc, char **argv) - main process loop */ |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
285 |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
286 int main(int argc, char **argv) |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
287 { |
35 | 288 /* Set euid and egid to actual user */ |
289 int uid = getuid(); | |
290 int gid = getgid(); | |
291 char *name = getlogin(); | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
292 |
21 | 293 printf("uid %d gid %d name %s\n", uid,gid,name); |
294 setegid(getgid()); | |
295 seteuid(getuid()); | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
296 |
27
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
297 int account_type = check_user_name(name); |
b731ce75cd35
add change_delimiter function that change '_' to '/'
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
298 if (account_type < 0) { |
30
2888c7ec9013
can't define vm name is not number.
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
299 fprintf(stderr, "Parmission denied. :%s\n", name); |
37 | 300 exit(1); |
21 | 301 } |
1 | 302 |
26
76bd473f3b78
change how to define and be used name
taiki <taiki@cr.ie.u-ryukyu.ac.jp>
parents:
23
diff
changeset
|
303 |
21 | 304 /* Confirm user is in GROUP(999) group */ |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
305 |
21 | 306 /* |
307 if ( gid != 999 ) { | |
308 printf("User Not Authorized! Exiting...\n"); | |
309 exit(1); | |
310 } | |
311 */ | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
312 |
35 | 313 if (argc<2) { |
314 usage(); | |
315 exit(0); | |
316 } | |
317 | |
21 | 318 /* Set uid, gid, euid and egid to root */ |
319 | |
320 setegid(0); | |
321 seteuid(0); | |
322 setgid(0); | |
323 setuid(0); | |
37 | 324 |
29 | 325 char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); |
326 vm_name[0] = '\0'; | |
37 | 327 if ((argc > 2) && (strncmp(argv[1], list_command, 4) != 0)) { make_vm_name(vm_name, account_type, name, argv[2]); } |
6
25c14d1144a8
add xml generation and define
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
5
diff
changeset
|
328 |
37 | 329 VMLISTPTR vmlist = get_vmlist(name); |
1 | 330 |
36 | 331 |
332 /* Check argv for proper arguments and run | |
21 | 333 * the corresponding script, if invoked. |
334 */ | |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
335 |
22 | 336 if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { |
21 | 337 print_vmlist(vmlist); |
22 | 338 } else if (strncmp(argv[1], start_command, 5) == 0) { |
37 | 339 if (argc<3) {usage(); exit(1);} |
340 check_vmlist_name(vmlist, argv[2]); | |
28 | 341 if (execl(command, command, start_command, vm_name, NULL) < 0) { |
21 | 342 perror("Execl:"); |
343 } | |
22 | 344 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { |
37 | 345 if (argc<3) {usage(); exit(1);} |
346 check_vmlist_name(vmlist, argv[2]); | |
28 | 347 if (execl(command, command, stop_command, vm_name, NULL) < 0) { |
21 | 348 perror("Execl:"); |
349 } | |
22 | 350 } else if ( strncmp(argv[1], dumpxml_command, 7) == 0 ) { |
37 | 351 if (argc<3) {usage(); exit(1);} |
352 check_vmlist_name(vmlist, argv[2]); | |
28 | 353 if (execl(command, command, dumpxml_command, vm_name, NULL) < 0) { |
21 | 354 perror("Execl:"); |
355 } | |
49 | 356 } else if ( strncmp(argv[1], "vncdisplay", 10) == 0 ) { |
357 if (argc<3) {usage(); exit(1);} | |
358 check_vmlist_name(vmlist, argv[2]); | |
359 if (execl(command, command, "vncdisplay", vm_name, NULL) < 0) { | |
360 perror("Execl:"); | |
361 } | |
22 | 362 } else if ( strncmp(argv[1], console_command, 8) == 0 ) { |
37 | 363 if (argc<3) {usage(); exit(1);} |
364 check_vmlist_name(vmlist, argv[2]); | |
28 | 365 if (execl(command, command, console_command, vm_name, NULL) < 0) { |
22 | 366 perror("Execl:"); |
367 } | |
44
546504699402
Generate xml which enabled kernel debug. but needs reload in virsh
atton
parents:
43
diff
changeset
|
368 } else if (strcmp(argv[1], define_command) == 0 ) { |
37 | 369 if (argc<3) {usage(); exit(1);} |
370 char xml_name[1024]; | |
371 xml_name[0] = 0; | |
43 | 372 create_new_vm(argv[2], vm_name, xml_name, false); |
42 | 373 /* chdir("/usr/local/etc/libvirt/qemu"); */ |
374 chdir("/etc/libvirt/qemu"); | |
375 if (execl(command, command, define_command, xml_name, NULL) < 0) { | |
376 perror("Execl:"); | |
377 } | |
43 | 378 } else if (strcmp(argv[1], define_gdb_command) == 0 ) { |
42 | 379 if (argc<3) {usage(); exit(1);} |
380 char xml_name[1024]; | |
381 xml_name[0] = 0; | |
43 | 382 create_new_vm(argv[2], vm_name, xml_name, true); |
29 | 383 /* chdir("/usr/local/etc/libvirt/qemu"); */ |
384 chdir("/etc/libvirt/qemu"); | |
37 | 385 if (execl(command, command, define_command, xml_name, NULL) < 0) { |
21 | 386 perror("Execl:"); |
387 } | |
22 | 388 } else if ( strncmp(argv[1], undefine_command, 8) == 0 ) { |
29 | 389 /* chdir("/usr/local/etc/libvirt/qemu"); */ |
390 chdir("/etc/libvirt/qemu"); | |
28 | 391 if (execl(command, command, undefine_command, vm_name, NULL) < 0) { |
21 | 392 perror("Execl:"); |
393 } | |
394 } else { | |
37 | 395 printf("%s is invalid command.\n", argv[1]); |
21 | 396 usage(); |
397 exit(1); | |
398 } | |
29 | 399 free(vm_name); |
21 | 400 exit(0); |
0
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
401 } |
7785dd06c62f
Virsh Wrapper initial version
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
402 |
1 | 403 /* end */ |