debuggers.hg

view extras/mini-os/main.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 5c0bf00e371d
line source
1 /*
2 * POSIX-compatible main layer
3 *
4 * Samuel Thibault <Samuel.Thibault@eu.citrix.net>, October 2007
5 */
7 #ifdef HAVE_LIBC
8 #include <os.h>
9 #include <sched.h>
10 #include <console.h>
11 #include <netfront.h>
12 #include <time.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fs.h>
16 #include <xenbus.h>
17 #include <events.h>
19 extern int main(int argc, char *argv[], char *envp[]);
20 extern void __libc_init_array(void);
21 extern void __libc_fini_array(void);
23 struct thread *main_thread;
25 #if 0
26 #include <stdio.h>
27 int main(int argc, char *argv[], char *envp[])
28 {
29 printf("Hello, World!\n");
30 return 1;
31 }
32 #endif
34 void _init(void)
35 {
36 }
38 void _fini(void)
39 {
40 }
42 extern char __app_bss_start, __app_bss_end;
43 static void call_main(void *p)
44 {
45 char *args, /**path,*/ *msg, *c;
46 #ifdef CONFIG_QEMU
47 char *domargs;
48 #endif
49 int argc;
50 char **argv;
51 char *envp[] = { NULL };
52 char *vm;
53 int i;
54 char path[128];
56 /* Let other parts initialize (including console output) before maybe
57 * crashing. */
58 //sleep(1);
60 sparse((unsigned long) &__app_bss_start, &__app_bss_end - &__app_bss_start);
61 start_networking();
62 init_fs_frontend();
64 #ifdef CONFIG_QEMU
65 if (!fs_import) {
66 printk("No FS backend found, is it running?\n");
67 do_exit();
68 }
70 /* Fetch argc, argv from XenStore */
71 int domid;
72 domid = xenbus_read_integer("target");
73 if (domid == -1) {
74 printk("Couldn't read target\n");
75 do_exit();
76 }
78 snprintf(path, sizeof(path), "/local/domain/%d/vm", domid);
79 msg = xenbus_read(XBT_NIL, path, &vm);
80 if (msg) {
81 printk("Couldn't read vm path\n");
82 do_exit();
83 }
84 printk("dom vm is at %s\n", vm);
86 snprintf(path, sizeof(path), "%s/image/dmargs", vm);
87 free(vm);
88 msg = xenbus_read(XBT_NIL, path, &domargs);
90 if (msg) {
91 printk("Couldn't get stubdom args: %s\n", msg);
92 domargs = strdup("");
93 }
94 #endif
96 msg = xenbus_read(XBT_NIL, "vm", &vm);
97 if (msg) {
98 printk("Couldn't read vm path\n");
99 do_exit();
100 }
102 printk("my vm is at %s\n", vm);
103 snprintf(path, sizeof(path), "%s/image/cmdline", vm);
104 free(vm);
105 msg = xenbus_read(XBT_NIL, path, &args);
107 if (msg) {
108 printk("Couldn't get my args: %s\n", msg);
109 args = strdup("");
110 }
112 argc = 1;
114 #define PARSE_ARGS(ARGS,START,END) \
115 c = ARGS; \
116 while (*c) { \
117 if (*c != ' ') { \
118 START; \
119 while (*c && *c != ' ') \
120 c++; \
121 } else { \
122 END; \
123 while (*c == ' ') \
124 c++; \
125 } \
126 }
128 PARSE_ARGS(args, argc++, );
129 #ifdef CONFIG_QEMU
130 PARSE_ARGS(domargs, argc++, );
131 #endif
133 argv = alloca((argc + 1) * sizeof(char *));
134 argv[0] = "main";
135 argc = 1;
137 PARSE_ARGS(args, argv[argc++] = c, *c++ = 0)
138 #ifdef CONFIG_QEMU
139 PARSE_ARGS(domargs, argv[argc++] = c, *c++ = 0)
140 #endif
142 argv[argc] = NULL;
144 for (i = 0; i < argc; i++)
145 printf("\"%s\" ", argv[i]);
146 printf("\n");
148 __libc_init_array();
149 environ = envp;
150 tzset();
152 exit(main(argc, argv, envp));
153 }
155 void _exit(int ret)
156 {
157 close_all_files();
158 __libc_fini_array();
159 printk("main returned %d\n", ret);
160 unbind_all_ports();
161 if (!ret) {
162 /* No problem, just shutdown. */
163 struct sched_shutdown sched_shutdown = { .reason = SHUTDOWN_poweroff };
164 HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
165 }
166 do_exit();
167 }
169 int app_main(start_info_t *si)
170 {
171 printk("Dummy main: start_info=%p\n", si);
172 main_thread = create_thread("main", call_main, si);
173 return 0;
174 }
175 #endif