[PATCH] uml: search from uml_net in a more reasonable PATH
[linux-2.6.git] / arch / um / os-Linux / main.c
blob3a0ac38e978bffa982f68badb8e9dd1e168bd3f6
1 /*
2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <errno.h>
12 #include <sys/resource.h>
13 #include <sys/mman.h>
14 #include <sys/user.h>
15 #include <asm/page.h>
16 #include "user_util.h"
17 #include "kern_util.h"
18 #include "mem_user.h"
19 #include "irq_user.h"
20 #include "user.h"
21 #include "init.h"
22 #include "mode.h"
23 #include "choose-mode.h"
24 #include "uml-config.h"
25 #include "os.h"
27 /* Set in set_stklim, which is called from main and __wrap_malloc.
28 * __wrap_malloc only calls it if main hasn't started.
30 unsigned long stacksizelim;
32 /* Set in main */
33 char *linux_prog;
35 #define PGD_BOUND (4 * 1024 * 1024)
36 #define STACKSIZE (8 * 1024 * 1024)
37 #define THREAD_NAME_LEN (256)
39 static void set_stklim(void)
41 struct rlimit lim;
43 if(getrlimit(RLIMIT_STACK, &lim) < 0){
44 perror("getrlimit");
45 exit(1);
47 if((lim.rlim_cur == RLIM_INFINITY) || (lim.rlim_cur > STACKSIZE)){
48 lim.rlim_cur = STACKSIZE;
49 if(setrlimit(RLIMIT_STACK, &lim) < 0){
50 perror("setrlimit");
51 exit(1);
54 stacksizelim = (lim.rlim_cur + PGD_BOUND - 1) & ~(PGD_BOUND - 1);
57 static __init void do_uml_initcalls(void)
59 initcall_t *call;
61 call = &__uml_initcall_start;
62 while (call < &__uml_initcall_end){;
63 (*call)();
64 call++;
68 static void last_ditch_exit(int sig)
70 signal(SIGINT, SIG_DFL);
71 signal(SIGTERM, SIG_DFL);
72 signal(SIGHUP, SIG_DFL);
73 uml_cleanup();
74 exit(1);
77 #define UML_LIB_PATH ":/usr/lib/uml"
79 static void setup_env_path(void)
81 char *new_path = NULL;
82 char *old_path = NULL;
83 int path_len = 0;
85 old_path = getenv("PATH");
86 /* if no PATH variable is set or it has an empty value
87 * just use the default + /usr/lib/uml
89 if (!old_path || (path_len = strlen(old_path)) == 0) {
90 putenv("PATH=:/bin:/usr/bin/" UML_LIB_PATH);
91 return;
94 /* append /usr/lib/uml to the existing path */
95 path_len += strlen("PATH=" UML_LIB_PATH) + 1;
96 new_path = malloc(path_len);
97 if (!new_path) {
98 perror("coudn't malloc to set a new PATH");
99 return;
101 snprintf(new_path, path_len, "PATH=%s" UML_LIB_PATH, old_path);
102 putenv(new_path);
105 extern int uml_exitcode;
107 extern void scan_elf_aux( char **envp);
109 int main(int argc, char **argv, char **envp)
111 char **new_argv;
112 int ret, i, err;
114 #ifdef UML_CONFIG_CMDLINE_ON_HOST
115 /* Allocate memory for thread command lines */
116 if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){
118 char padding[THREAD_NAME_LEN] = {
119 [ 0 ... THREAD_NAME_LEN - 2] = ' ', '\0'
122 new_argv = malloc((argc + 2) * sizeof(char*));
123 if(!new_argv) {
124 perror("Allocating extended argv");
125 exit(1);
128 new_argv[0] = argv[0];
129 new_argv[1] = padding;
131 for(i = 2; i <= argc; i++)
132 new_argv[i] = argv[i - 1];
133 new_argv[argc + 1] = NULL;
135 execvp(new_argv[0], new_argv);
136 perror("execing with extended args");
137 exit(1);
139 #endif
141 linux_prog = argv[0];
143 set_stklim();
145 setup_env_path();
147 new_argv = malloc((argc + 1) * sizeof(char *));
148 if(new_argv == NULL){
149 perror("Mallocing argv");
150 exit(1);
152 for(i=0;i<argc;i++){
153 new_argv[i] = strdup(argv[i]);
154 if(new_argv[i] == NULL){
155 perror("Mallocing an arg");
156 exit(1);
159 new_argv[argc] = NULL;
161 set_handler(SIGINT, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
162 set_handler(SIGTERM, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
163 set_handler(SIGHUP, last_ditch_exit, SA_ONESHOT | SA_NODEFER, -1);
165 scan_elf_aux( envp);
167 do_uml_initcalls();
168 ret = linux_main(argc, argv);
170 /* Disable SIGPROF - I have no idea why libc doesn't do this or turn
171 * off the profiling time, but UML dies with a SIGPROF just before
172 * exiting when profiling is active.
174 change_sig(SIGPROF, 0);
176 /* This signal stuff used to be in the reboot case. However,
177 * sometimes a SIGVTALRM can come in when we're halting (reproducably
178 * when writing out gcov information, presumably because that takes
179 * some time) and cause a segfault.
182 /* stop timers and set SIG*ALRM to be ignored */
183 disable_timer();
185 /* disable SIGIO for the fds and set SIGIO to be ignored */
186 err = deactivate_all_fds();
187 if(err)
188 printf("deactivate_all_fds failed, errno = %d\n", -err);
190 /* Let any pending signals fire now. This ensures
191 * that they won't be delivered after the exec, when
192 * they are definitely not expected.
194 unblock_signals();
196 /* Reboot */
197 if(ret){
198 printf("\n");
199 execvp(new_argv[0], new_argv);
200 perror("Failed to exec kernel");
201 ret = 1;
203 printf("\n");
204 return(uml_exitcode);
207 #define CAN_KMALLOC() \
208 (kmalloc_ok && CHOOSE_MODE((os_getpid() != tracing_pid), 1))
210 extern void *__real_malloc(int);
212 void *__wrap_malloc(int size)
214 void *ret;
216 if(!CAN_KMALLOC())
217 return(__real_malloc(size));
218 else if(size <= PAGE_SIZE) /* finding contiguos pages can be hard*/
219 ret = um_kmalloc(size);
220 else ret = um_vmalloc(size);
222 /* glibc people insist that if malloc fails, errno should be
223 * set by malloc as well. So we do.
225 if(ret == NULL)
226 errno = ENOMEM;
228 return(ret);
231 void *__wrap_calloc(int n, int size)
233 void *ptr = __wrap_malloc(n * size);
235 if(ptr == NULL) return(NULL);
236 memset(ptr, 0, n * size);
237 return(ptr);
240 extern void __real_free(void *);
242 extern unsigned long high_physmem;
244 void __wrap_free(void *ptr)
246 unsigned long addr = (unsigned long) ptr;
248 /* We need to know how the allocation happened, so it can be correctly
249 * freed. This is done by seeing what region of memory the pointer is
250 * in -
251 * physical memory - kmalloc/kfree
252 * kernel virtual memory - vmalloc/vfree
253 * anywhere else - malloc/free
254 * If kmalloc is not yet possible, then either high_physmem and/or
255 * end_vm are still 0 (as at startup), in which case we call free, or
256 * we have set them, but anyway addr has not been allocated from those
257 * areas. So, in both cases __real_free is called.
259 * CAN_KMALLOC is checked because it would be bad to free a buffer
260 * with kmalloc/vmalloc after they have been turned off during
261 * shutdown.
262 * XXX: However, we sometimes shutdown CAN_KMALLOC temporarily, so
263 * there is a possibility for memory leaks.
266 if((addr >= uml_physmem) && (addr < high_physmem)){
267 if(CAN_KMALLOC())
268 kfree(ptr);
270 else if((addr >= start_vm) && (addr < end_vm)){
271 if(CAN_KMALLOC())
272 vfree(ptr);
274 else __real_free(ptr);