2 * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
11 #include <sys/resource.h>
15 #include "user_util.h"
16 #include "kern_util.h"
18 #include "signal_user.h"
22 #include "choose-mode.h"
23 #include "uml-config.h"
25 /* Set in set_stklim, which is called from main and __wrap_malloc.
26 * __wrap_malloc only calls it if main hasn't started.
28 unsigned long stacksizelim
;
33 #define PGD_BOUND (4 * 1024 * 1024)
34 #define STACKSIZE (8 * 1024 * 1024)
35 #define THREAD_NAME_LEN (256)
37 static void set_stklim(void)
41 if(getrlimit(RLIMIT_STACK
, &lim
) < 0){
45 if((lim
.rlim_cur
== RLIM_INFINITY
) || (lim
.rlim_cur
> STACKSIZE
)){
46 lim
.rlim_cur
= STACKSIZE
;
47 if(setrlimit(RLIMIT_STACK
, &lim
) < 0){
52 stacksizelim
= (lim
.rlim_cur
+ PGD_BOUND
- 1) & ~(PGD_BOUND
- 1);
55 static __init
void do_uml_initcalls(void)
59 call
= &__uml_initcall_start
;
60 while (call
< &__uml_initcall_end
){;
66 static void last_ditch_exit(int sig
)
68 CHOOSE_MODE(kmalloc_ok
= 0, (void) 0);
69 signal(SIGINT
, SIG_DFL
);
70 signal(SIGTERM
, SIG_DFL
);
71 signal(SIGHUP
, SIG_DFL
);
76 extern int uml_exitcode
;
78 int main(int argc
, char **argv
, char **envp
)
84 /* Enable all signals except SIGIO - in some environments, we can
85 * enter with some signals blocked
89 sigaddset(&mask
, SIGIO
);
90 if(sigprocmask(SIG_SETMASK
, &mask
, NULL
) < 0){
91 perror("sigprocmask");
95 #ifdef UML_CONFIG_MODE_TT
96 /* Allocate memory for thread command lines */
97 if(argc
< 2 || strlen(argv
[1]) < THREAD_NAME_LEN
- 1){
99 char padding
[THREAD_NAME_LEN
] = {
100 [ 0 ... THREAD_NAME_LEN
- 2] = ' ', '\0'
103 new_argv
= malloc((argc
+ 2) * sizeof(char*));
105 perror("Allocating extended argv");
109 new_argv
[0] = argv
[0];
110 new_argv
[1] = padding
;
112 for(i
= 2; i
<= argc
; i
++)
113 new_argv
[i
] = argv
[i
- 1];
114 new_argv
[argc
+ 1] = NULL
;
116 execvp(new_argv
[0], new_argv
);
117 perror("execing with extended args");
122 linux_prog
= argv
[0];
126 if((new_argv
= malloc((argc
+ 1) * sizeof(char *))) == NULL
){
127 perror("Mallocing argv");
131 if((new_argv
[i
] = strdup(argv
[i
])) == NULL
){
132 perror("Mallocing an arg");
136 new_argv
[argc
] = NULL
;
138 set_handler(SIGINT
, last_ditch_exit
, SA_ONESHOT
| SA_NODEFER
, -1);
139 set_handler(SIGTERM
, last_ditch_exit
, SA_ONESHOT
| SA_NODEFER
, -1);
140 set_handler(SIGHUP
, last_ditch_exit
, SA_ONESHOT
| SA_NODEFER
, -1);
143 ret
= linux_main(argc
, argv
);
148 execvp(new_argv
[0], new_argv
);
149 perror("Failed to exec kernel");
153 return(uml_exitcode
);
156 #define CAN_KMALLOC() \
157 (kmalloc_ok && CHOOSE_MODE((getpid() != tracing_pid), 1))
159 extern void *__real_malloc(int);
161 void *__wrap_malloc(int size
)
164 return(um_kmalloc(size
));
166 return(__real_malloc(size
));
169 void *__wrap_calloc(int n
, int size
)
171 void *ptr
= __wrap_malloc(n
* size
);
173 if(ptr
== NULL
) return(NULL
);
174 memset(ptr
, 0, n
* size
);
178 extern void __real_free(void *);
180 void __wrap_free(void *ptr
)
182 if(CAN_KMALLOC()) kfree(ptr
);
183 else __real_free(ptr
);
187 * Overrides for Emacs so that we follow Linus's tabbing style.
188 * Emacs will notice this stuff at the end of the file and automatically
189 * adjust the settings for this buffer only. This must remain at the end
191 * ---------------------------------------------------------------------------
193 * c-file-style: "linux"