| 1 |
/* |
| 2 |
* Ikarus Scheme -- A compiler for R6RS Scheme. |
| 3 |
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, but |
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "bootfileloc.h" |
| 21 |
#include "ikarus-data.h" |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <string.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <gmp.h> |
| 30 |
#include <signal.h> |
| 31 |
#include <sys/mman.h> |
| 32 |
|
| 33 |
|
| 34 |
void register_handlers(); |
| 35 |
void register_alt_stack(); |
| 36 |
|
| 37 |
|
| 38 |
ikpcb* the_pcb; |
| 39 |
|
| 40 |
int |
| 41 |
file_exists(char* filename){ |
| 42 |
struct stat sb; |
| 43 |
int s = stat(filename, &sb); |
| 44 |
return (s == 0); |
| 45 |
} |
| 46 |
|
| 47 |
extern int cpu_has_sse2(); |
| 48 |
|
| 49 |
int ikarus_main(int argc, char** argv, char* boot_file){ |
| 50 |
if(! cpu_has_sse2()){ |
| 51 |
fprintf(stderr, "Ikarus Scheme cannot run on your computer because\n"); |
| 52 |
fprintf(stderr, "your CPU does not support the SSE2 instruction set.\n"); |
| 53 |
fprintf(stderr, "Refer to the Ikarus Scheme User's Guide for the\n"); |
| 54 |
fprintf(stderr, "minimum hardware requirements.\n"); |
| 55 |
exit(-1); |
| 56 |
} |
| 57 |
if(sizeof(mp_limb_t) != sizeof(long int)){ |
| 58 |
fprintf(stderr, "ERROR: limb size does not match\n"); |
| 59 |
exit(-1); |
| 60 |
} |
| 61 |
if(mp_bits_per_limb != (8*sizeof(long int))){ |
| 62 |
fprintf(stderr, "ERROR: invalid bits_per_limb=%d\n", mp_bits_per_limb); |
| 63 |
exit(-1); |
| 64 |
} |
| 65 |
ikpcb* pcb = ik_make_pcb(); |
| 66 |
the_pcb = pcb; |
| 67 |
{ /* set up arg_list */ |
| 68 |
ikptr arg_list = null_object; |
| 69 |
int i = argc-1; |
| 70 |
while(i > 0){ |
| 71 |
char* s = argv[i]; |
| 72 |
int n = strlen(s); |
| 73 |
ikptr bv = ik_unsafe_alloc(pcb, align(disp_bytevector_data+n+1)) |
| 74 |
+ bytevector_tag; |
| 75 |
ref(bv, off_bytevector_length) = fix(n); |
| 76 |
memcpy((char*)(bv+off_bytevector_data), s, n+1); |
| 77 |
ikptr p = ik_unsafe_alloc(pcb, pair_size); |
| 78 |
ref(p, disp_car) = bv; |
| 79 |
ref(p, disp_cdr) = arg_list; |
| 80 |
arg_list = p+pair_tag; |
| 81 |
i--; |
| 82 |
} |
| 83 |
pcb->arg_list = arg_list; |
| 84 |
} |
| 85 |
register_handlers(); |
| 86 |
register_alt_stack(); |
| 87 |
ik_fasl_load(pcb, boot_file); |
| 88 |
/* |
| 89 |
fprintf(stderr, "collect time: %d.%03d utime, %d.%03d stime (%d collections)\n", |
| 90 |
pcb->collect_utime.tv_sec, |
| 91 |
pcb->collect_utime.tv_usec/1000, |
| 92 |
pcb->collect_stime.tv_sec, |
| 93 |
pcb->collect_stime.tv_usec/1000, |
| 94 |
pcb->collection_id ); |
| 95 |
*/ |
| 96 |
ik_delete_pcb(pcb); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
#if 0 |
| 101 |
Notice how the bsd manpages have incorrect type for the handler. |
| 102 |
|
| 103 |
#include <signal.h> |
| 104 |
|
| 105 |
struct sigaction { |
| 106 |
union { |
| 107 |
void (*__sa_handler)(int); |
| 108 |
void (*__sa_sigaction)(int, struct __siginfo *, void *); |
| 109 |
} __sigaction_u; /* signal handler */ |
| 110 |
int sa_flags; /* see signal options below */ |
| 111 |
sigset_t sa_mask; /* signal mask to apply */ |
| 112 |
}; |
| 113 |
|
| 114 |
#define sa_handler __sigaction_u.__sa_handler |
| 115 |
#define sa_sigaction __sigaction_u.__sa_sigaction |
| 116 |
|
| 117 |
int |
| 118 |
sigaction(int sig, const struct sigaction * restrict act, |
| 119 |
struct sigaction * restrict oact); |
| 120 |
#endif |
| 121 |
|
| 122 |
void handler(int signo, siginfo_t* info, void* uap){ |
| 123 |
signo=signo; info=info; uap=uap; /* no warning */ |
| 124 |
the_pcb->engine_counter = fix(-1); |
| 125 |
/* This must be set to non-zero. Signal numbers are positive, according to |
| 126 |
Posix, so using them is alright. */ |
| 127 |
the_pcb->interrupted = signo; |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
register_handlers(){ |
| 132 |
/* The signals whose default action is process termination. FPE, ILL, and |
| 133 |
SEGV are excluded because they're low-level and the Scheme side shouldn't |
| 134 |
try to handle. PIPE is excluded so it can be ignored below. KILL is |
| 135 |
excluded because it's not possible to catch it. */ |
| 136 |
static int signals[] = {SIGABRT, SIGALRM, SIGBUS, /*SIGFPE,*/ SIGHUP, |
| 137 |
/*SIGILL,*/ SIGINT, /*SIGKILL,*/ /*SIGPIPE,*/ SIGQUIT, |
| 138 |
/*SIGSEGV,*/ SIGTERM, SIGUSR1, SIGUSR2, SIGPOLL, |
| 139 |
SIGPROF, SIGSYS, SIGTRAP, SIGVTALRM, SIGXCPU, SIGXFSZ, |
| 140 |
0}; |
| 141 |
struct sigaction sa; |
| 142 |
sa.sa_sigaction = handler; |
| 143 |
#ifdef __CYGWIN__ |
| 144 |
sa.sa_flags = SA_SIGINFO; |
| 145 |
#else |
| 146 |
sa.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 147 |
#endif |
| 148 |
sigemptyset(&sa.sa_mask); |
| 149 |
int i; |
| 150 |
for (i=0; signals[i]; i++){ |
| 151 |
int err = sigaction(signals[i], &sa, 0); |
| 152 |
if(err){ |
| 153 |
fprintf(stderr, "Sigaction Failed: %s\n", strerror(errno)); |
| 154 |
exit(-1); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/* ignore sigpipes */ |
| 159 |
{ |
| 160 |
sigset_t set; |
| 161 |
sigprocmask(0, 0, &set); /* get the set */ |
| 162 |
sigaddset(&set, SIGPIPE); |
| 163 |
int err = sigprocmask(SIG_SETMASK, &set, &set); |
| 164 |
if(err){ |
| 165 |
fprintf(stderr, "Sigprocmask Failed: %s\n", strerror(errno)); |
| 166 |
exit(-1); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
#if 0 |
| 173 |
SYNOPSIS |
| 174 |
#include <sys/types.h> |
| 175 |
#include <signal.h> |
| 176 |
|
| 177 |
struct sigaltstack { |
| 178 |
char *ss_sp; |
| 179 |
int ss_size; |
| 180 |
int ss_flags; |
| 181 |
}; |
| 182 |
|
| 183 |
int |
| 184 |
sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss); |
| 185 |
#endif |
| 186 |
|
| 187 |
void |
| 188 |
register_alt_stack(){ |
| 189 |
#if HAVE_SIGALTSTACK |
| 190 |
char* stk = mmap(0, SIGSTKSZ, PROT_READ|PROT_WRITE|PROT_EXEC, |
| 191 |
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 192 |
// char* stk = ik_mmap(SIGSTKSZ); |
| 193 |
if(stk == (char*)-1){ |
| 194 |
fprintf(stderr, "Cannot maloc an alt stack\n"); |
| 195 |
exit(-1); |
| 196 |
} |
| 197 |
|
| 198 |
stack_t sa; |
| 199 |
sa.ss_sp = stk; |
| 200 |
sa.ss_size = SIGSTKSZ; |
| 201 |
sa.ss_flags = 0; |
| 202 |
int err = sigaltstack(&sa, 0); |
| 203 |
if(err){ |
| 204 |
fprintf(stderr, "Cannot set alt stack: %s\n", strerror(errno)); |
| 205 |
exit(-1); |
| 206 |
} |
| 207 |
#endif |
| 208 |
} |