Mercurial > hg > Applications > virsh-wrapper
changeset 37:3b8858a63694
Refactor
author | atton |
---|---|
date | Fri, 06 Nov 2015 20:30:26 +0900 |
parents | ebeeb6c20d0a |
children | 16840a2375ae |
files | ie-virsh.c |
diffstat | 1 files changed, 52 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/ie-virsh.c Thu Nov 05 19:25:56 2015 +0900 +++ b/ie-virsh.c Fri Nov 06 20:30:26 2015 +0900 @@ -60,8 +60,13 @@ /* Define global variables */ VMLISTPTR -get_vmlist(regex_t *list_pattern) +get_vmlist(char *list_pattern_str) { + regex_t *list_pattern = NEW(regex_t); + if (regcomp(list_pattern, list_pattern_str, 0) != 0) { + exit(0); + } + VMLISTPTR list = NEW(VMLIST); VMLISTPTR p = list ; p->name[0] = 0; @@ -74,6 +79,7 @@ } p->name[0] = 0; pclose(fp); + regfree(list_pattern); return list; } @@ -85,13 +91,16 @@ } } -int -check_vmlist_name(VMLISTPTR list, char *arg) +void +check_vmlist_name(VMLISTPTR list, char * name) { for(;list && list->name[0]; list=list->next) { - if (strstr(list->name,arg)!=0) return 1; + if (strstr(list->name, name)!=0) return; } - return 0; + + fprintf(stderr, bad_name); + print_vmlist(list); + exit(1); } void @@ -219,6 +228,26 @@ strncat(vm_name, vm_num, VM_NAME_LENGTH); } +void +create_new_vm(const char const *name, const char const *vm_name, char* xml_name){ + if (check_name(name)) { + fprintf(stderr, bad_name); + exit(0); + } + char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); + strncpy(vm_path, vm_name, VM_NAME_LENGTH); + change_delimiter_to_slash(vm_path); + char exec[1024]; + strncpy(exec, newvm_command ,900); + strncat(exec, vm_path,1000); + fprintf(stdout, "excuting %s\n",exec ); + printf("%s\n", vm_path); + system(exec); + strncpy(xml_name, vm_path,900); + strncat(xml_name,".xml",1000); + free(vm_path); +} + /* main(int argc, char **argv) - main process loop */ int main(int argc, char **argv) @@ -235,6 +264,7 @@ int account_type = check_user_name(name); if (account_type < 0) { fprintf(stderr, "Parmission denied. :%s\n", name); + exit(1); } @@ -258,55 +288,12 @@ seteuid(0); setgid(0); setuid(0); + char *vm_name = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); vm_name[0] = '\0'; - - if ((argc > 2) && (strncmp(argv[1], "list", 4) != 0)) { make_vm_name(vm_name, account_type, name, argv[2]); } - - if (argc>=3) { - if ( strncmp(argv[1], "define", 6) == 0 ) { - if (check_name(argv[2])) { - fprintf(stderr, bad_name); - exit(0); - } - char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); - strncpy(vm_path, vm_name, VM_NAME_LENGTH); - change_delimiter_to_slash(vm_path); - char exec[1024]; - strncpy(exec, newvm_command ,900); - strncat(exec, vm_path,1000); - fprintf(stdout, "excuting %s\n",exec ); - system(exec); - free(vm_path); - } - } + if ((argc > 2) && (strncmp(argv[1], list_command, 4) != 0)) { make_vm_name(vm_name, account_type, name, argv[2]); } - regex_t *pattern = NEW(regex_t); - if (regcomp(pattern, name, 0) != 0) { - exit(0); - } - - VMLISTPTR vmlist = get_vmlist(pattern); - - char name_xml[1024]; - name_xml[0] = 0; - - if (argc>=3) { - if ( strncmp(argv[1], "define", 6) == 0 ) { - char *vm_path = (char *)malloc(sizeof(char) * VM_NAME_LENGTH); - vm_path[0] = '\0'; - strncpy(vm_path, vm_name, VM_NAME_LENGTH); - change_delimiter_to_slash(vm_path); - printf("%s\n", vm_path); - strncpy(name_xml, vm_path,900); - strncat(name_xml,".xml",1000); - free(vm_path); - } else if (check_vmlist_name(vmlist, argv[2])==0) { - fprintf(stderr, bad_name); - print_vmlist(vmlist); - exit(0); - } - } + VMLISTPTR vmlist = get_vmlist(name); /* Check argv for proper arguments and run @@ -316,25 +303,37 @@ if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) { print_vmlist(vmlist); } else if (strncmp(argv[1], start_command, 5) == 0) { + if (argc<3) {usage(); exit(1);} + check_vmlist_name(vmlist, argv[2]); if (execl(command, command, start_command, vm_name, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], stop_command, 4) == 0 ) { + if (argc<3) {usage(); exit(1);} + check_vmlist_name(vmlist, argv[2]); if (execl(command, command, stop_command, vm_name, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], dumpxml_command, 7) == 0 ) { + if (argc<3) {usage(); exit(1);} + check_vmlist_name(vmlist, argv[2]); if (execl(command, command, dumpxml_command, vm_name, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], console_command, 8) == 0 ) { + if (argc<3) {usage(); exit(1);} + check_vmlist_name(vmlist, argv[2]); if (execl(command, command, console_command, vm_name, NULL) < 0) { perror("Execl:"); } } else if (strncmp(argv[1], define_command, 6) == 0 ) { + if (argc<3) {usage(); exit(1);} + char xml_name[1024]; + xml_name[0] = 0; + create_new_vm(argv[2], vm_name, xml_name); /* chdir("/usr/local/etc/libvirt/qemu"); */ chdir("/etc/libvirt/qemu"); - if (execl(command, command, define_command, name_xml, NULL) < 0) { + if (execl(command, command, define_command, xml_name, NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], undefine_command, 8) == 0 ) { @@ -362,6 +361,7 @@ // strncat(gdb_exec, " /usr/lib/debug/lib/modules/3.17.4-200.fc20.x86_64/vmlinux", 900); // system(gdb_exec); } else { + printf("%s is invalid command.\n", argv[1]); usage(); exit(1); }