merge
[linux-based-stubdoms.git] / qemu-wrapper / qemu-wrapper.c
blob301cfe8fd06979bea4ecaf1e4ec28706a914b892
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <xs.h>
4 #include <string.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <sys/mount.h>
9 #include <dirent.h>
11 #define MAX_GUEST_CMDLINE 1024
12 #define PARSE_ARGS(ARGS,START,QUOTE,END) \
13 c = ARGS; \
14 quote = 0; \
15 while (*c) { \
16 if (*c != ' ') { \
17 START; \
18 while (*c) { \
19 if (quote) { \
20 if (*c == quote) { \
21 quote = 0; \
22 QUOTE; \
23 continue; \
24 } \
25 } else if (*c == ' ') \
26 break; \
27 if (*c == '"' || *c == '\'') { \
28 quote = *c; \
29 QUOTE; \
30 continue; \
31 } \
32 c++; \
33 } \
34 } else { \
35 END; \
36 while (*c == ' ') \
37 c++; \
38 } \
39 } \
40 if (quote) {\
41 printf("Warning: unterminated quotation %c\n", quote); \
42 quote = 0; \
44 #define PARSE_ARGS_COUNT(ARGS) PARSE_ARGS(ARGS, argc++, c++, )
45 #define PARSE_ARGS_STORE(ARGS) PARSE_ARGS(ARGS, argv[argc++] = c, memmove(c, c + 1, strlen(c + 1) + 1), *c++ = 0)
47 struct xs_handle *xsh=NULL;
49 int xenbus_read_integer(const char *path)
51 char *buf;
52 int t;
53 unsigned int len;
55 buf = xs_read(xsh,XBT_NULL, path, &len);
56 if (buf == NULL) {
57 printf("Failed to read %s.\n", path);
58 free(buf);
59 return -1;
61 sscanf(buf, "%d", &t);
62 free(buf);
63 return t;
66 int main(void)
68 char *c, quote;
69 char *domargs;
70 int argc;
71 char **argv;
72 char *envp[] = { NULL };
73 char *vm;
74 char path[128];
75 int domid,fd,i;
76 unsigned int len;
77 char cmdline[MAX_GUEST_CMDLINE];
79 DIR *dp;
80 struct dirent *dirp;
82 #if 0
83 /* read cmdline from /proc/cmdline */
84 fd=open("/proc/cmdline",O_RDONLY);
85 if(fd<0){
86 printf("Cannot open /proc/cmdline\n");
87 exit(0);
89 if(read(fd,cmdline,MAX_GUEST_CMDLINE) < 0){
90 printf("Cannot read cmdline from /proc/cmdline\n");
91 exit(0);
93 close(fd);
94 printf("cmdline(%s)\n",cmdline);
95 #endif
97 mount("xenfs","/proc/xen","xenfs",MS_NODEV,"");
99 printf("printf /dev dir\n");
100 if((dp=opendir("/dev")) == NULL)
102 printf("there is no /dev dir\n");
103 exit(0);
105 while((dirp=readdir(dp)) != NULL)
106 printf("%s ",dirp->d_name);
107 closedir(dp);
108 printf(" end\n");
110 xsh=xs_daemon_open();
111 if(xsh==NULL){
112 printf("Cannot open xenstore handle\n");
113 exit(0);
116 /* Fetch argc, argv from XenStore */
117 domid = xenbus_read_integer("target");
118 if (domid == -1) {
119 printf("Couldn't read target\n");
120 exit(0);
123 snprintf(path, sizeof(path), "/local/domain/%d/vm", domid);
124 vm = xs_read(xsh,XBT_NULL, path,&len);
125 if (!len) {
126 printf("Couldn't read vm path\n");
127 exit(0);
130 snprintf(path, sizeof(path), "%s/image/dmargs", vm);
131 free(vm);
133 domargs = xs_read(xsh,XBT_NULL, path, &len);
134 if (!len) {
135 printf("stubdom args is empty\n");
136 domargs = strdup("");
139 xs_daemon_close(xsh);
141 argc = 1;
143 // PARSE_ARGS_COUNT(cmdline);
144 PARSE_ARGS_COUNT(domargs);
146 argv = alloca((argc + 3) * sizeof(char *));
147 argv[0] = "main";
148 argv[1] = "-M";
149 argv[2] = "xenfv";
150 argc = 3;
152 // PARSE_ARGS_STORE(cmdline)
153 PARSE_ARGS_STORE(domargs)
155 argv[argc] = NULL;
157 for (i = 0; i < argc; i++)
158 printf("\"%s\" ", argv[i]);
159 printf("\n");
161 execve("/bin/qemu",argv,envp);
163 return 1;