changeset 1:1a8257a34493

no compile errors
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 11 Nov 2012 18:16:27 +0900
parents 7785dd06c62f
children 1a35a6cb23dd
files ie-virsh.c
diffstat 1 files changed, 79 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ie-virsh.c	Sun Nov 11 16:26:00 2012 +0900
+++ b/ie-virsh.c	Sun Nov 11 18:16:27 2012 +0900
@@ -1,6 +1,4 @@
 
-#define command "/bin/ls"
-#define arg1 "/Users/kana/Desktop"
 
 #include <stdlib.h>
 #include <unistd.h>
@@ -11,6 +9,8 @@
 #include <signal.h>
 #include <strings.h>
 
+#include <regex.h>
+
 /********************************************
  * Wrapper - Secure Yourself                *
  *                                          *
@@ -22,21 +22,80 @@
  
  /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
 
+#define command "/bin/ls"
+#define list_command "/bin/ls -l /Users/kono/Desktop"
+#define start_command "-l"
+#define stop_command "-l"
+
+#define VMNAME_MAX (512)
+
+typedef struct vmlist {
+    char name[VMNAME_MAX];
+    struct vmlist *next;
+} VMLIST, *VMLISTPTR;
+
+#define NEW(type)  ((type*)malloc(sizeof(type)))
+
 /* Define global variables */
 
-int gid;
+
+
+VMLISTPTR
+get_vmlist(regex_t *list_pattern)
+{
+    VMLISTPTR list = NEW(VMLIST);
+    VMLISTPTR p = list ;
+    p->name[0] = 0;
+    p->next = 0;
+    FILE *fp = popen(list_command,"r");
+    while(fgets(p->name,VMNAME_MAX,fp)!=NULL) {
+        if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue;
+        p->next = NEW(VMLIST);
+        p = p->next;
+    }
+    p->name[0] = 0;
+    pclose(fp);
+    return list;
+}
+
+void 
+print_vmlist(VMLISTPTR list) 
+{
+    for(;list && list->name[0]; list=list->next) {
+        fprintf(stdout, "   %s\n",list->name);
+    }
+}
+
+int
+check_vmlist_name(VMLISTPTR list, char *arg)
+{
+    for(;list && list->name[0]; list=list->next) {
+        if (strcmp(list->name,arg)==0) return 1;
+    }
+    return 0;
+}
 
 /* main(int argc, char **argv) - main process loop */
 
 int main(int argc, char **argv)
 {
+    int gid;
+    int uid;
 
 /* Set euid and egid to actual user */
 
+ char *name = getlogin();
+ uid = getuid();
  gid = getgid();
+ printf("uid %d gid %d name %s\n", uid,gid,name);
  setegid(getgid());
  seteuid(getuid());
 
+ regex_t *pattern = NEW(regex_t);
+ if (regcomp(pattern, name, REG_NOSPEC) != 0) {
+    exit(0);
+ }
+
 /* Confirm user is in GROUP(999) group */
 
 /*
@@ -46,13 +105,6 @@
  }
  */
 
-/* Check argc count only at this point */
-
- if ( argc != 2 ) {
-  printf("Usage: COMMAND [start|stop]\n");
-  exit(1);
- }
-
 /* Set uid, gid, euid and egid to root */
 
  setegid(0);
@@ -60,22 +112,35 @@
  setgid(0);
  setuid(0);
 
+ VMLISTPTR vmlist = get_vmlist(pattern);
+
+ if (argc==2) {
+    if (check_vmlist_name(vmlist, argv[2])!=0) {
+        fprintf(stderr, "bad vmname\n");
+        print_vmlist(vmlist);
+        exit(0);
+    }
+ }
+
 /* Check argv for proper arguments and run 
  * the corresponding script, if invoked.
  */
 
- if ( strncmp(argv[1], "start", 5) == 0 ) {
-  if (execl(command, command, arg1, NULL) < 0) {
+ if ( strncmp(argv[1], "list", 4) == 0 ) {
+    print_vmlist(vmlist);
+ } else if ( strncmp(argv[1], "start", 5) == 0 ) {
+  if (execl(command, command, start_command, argv[2], NULL) < 0) {
    perror("Execl:");
   }
  } else if ( strncmp(argv[1], "stop", 4) == 0 ) {
-  if (execl(command, command, arg1, NULL) < 0) {
+  if (execl(command, command, stop_command, argv[2], NULL) < 0) {
    perror("Execl:");
   }
  } else {
-  printf("Usage: COMMAND [start|stop]\n");
+  printf("Usage: COMMAND [list|start|stop] [vm-name]\n");
   exit(1);
  }
  exit(0);
 }
 
+/* end */