0001
0002
0003
0004
0005
0006 #include <stdarg.h>
0007 #include <stdio.h>
0008 #include <stdlib.h>
0009 #include <unistd.h>
0010 #include <errno.h>
0011 #include <signal.h>
0012 #include <string.h>
0013 #include <termios.h>
0014 #include <sys/wait.h>
0015 #include <sys/mman.h>
0016 #include <sys/utsname.h>
0017 #include <sys/random.h>
0018 #include <init.h>
0019 #include <os.h>
0020
0021 void stack_protections(unsigned long address)
0022 {
0023 if (mprotect((void *) address, UM_THREAD_SIZE,
0024 PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
0025 panic("protecting stack failed, errno = %d", errno);
0026 }
0027
0028 int raw(int fd)
0029 {
0030 struct termios tt;
0031 int err;
0032
0033 CATCH_EINTR(err = tcgetattr(fd, &tt));
0034 if (err < 0)
0035 return -errno;
0036
0037 cfmakeraw(&tt);
0038
0039 CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
0040 if (err < 0)
0041 return -errno;
0042
0043
0044
0045
0046
0047 return 0;
0048 }
0049
0050 void setup_machinename(char *machine_out)
0051 {
0052 struct utsname host;
0053
0054 uname(&host);
0055 #ifdef UML_CONFIG_UML_X86
0056 # ifndef UML_CONFIG_64BIT
0057 if (!strcmp(host.machine, "x86_64")) {
0058 strcpy(machine_out, "i686");
0059 return;
0060 }
0061 # else
0062 if (!strcmp(host.machine, "i686")) {
0063 strcpy(machine_out, "x86_64");
0064 return;
0065 }
0066 # endif
0067 #endif
0068 strcpy(machine_out, host.machine);
0069 }
0070
0071 void setup_hostinfo(char *buf, int len)
0072 {
0073 struct utsname host;
0074
0075 uname(&host);
0076 snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
0077 host.release, host.version, host.machine);
0078 }
0079
0080
0081
0082
0083
0084
0085
0086 static inline void __attribute__ ((noreturn)) uml_abort(void)
0087 {
0088 sigset_t sig;
0089
0090 fflush(NULL);
0091
0092 if (!sigemptyset(&sig) && !sigaddset(&sig, SIGABRT))
0093 sigprocmask(SIG_UNBLOCK, &sig, 0);
0094
0095 for (;;)
0096 if (kill(getpid(), SIGABRT) < 0)
0097 exit(127);
0098 }
0099
0100 ssize_t os_getrandom(void *buf, size_t len, unsigned int flags)
0101 {
0102 return getrandom(buf, len, flags);
0103 }
0104
0105
0106
0107
0108 void os_fix_helper_signals(void)
0109 {
0110 signal(SIGWINCH, SIG_IGN);
0111 signal(SIGINT, SIG_DFL);
0112 signal(SIGTERM, SIG_DFL);
0113 }
0114
0115 void os_dump_core(void)
0116 {
0117 int pid;
0118
0119 signal(SIGSEGV, SIG_DFL);
0120
0121
0122
0123
0124
0125
0126
0127
0128 signal(SIGTERM, SIG_IGN);
0129 kill(0, SIGTERM);
0130
0131
0132
0133
0134
0135 kill(0, SIGCONT);
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)
0154 os_kill_ptraced_process(pid, 0);
0155
0156 uml_abort();
0157 }
0158
0159 void um_early_printk(const char *s, unsigned int n)
0160 {
0161 printf("%.*s", n, s);
0162 }
0163
0164 static int quiet_info;
0165
0166 static int __init quiet_cmd_param(char *str, int *add)
0167 {
0168 quiet_info = 1;
0169 return 0;
0170 }
0171
0172 __uml_setup("quiet", quiet_cmd_param,
0173 "quiet\n"
0174 " Turns off information messages during boot.\n\n");
0175
0176 void os_info(const char *fmt, ...)
0177 {
0178 va_list list;
0179
0180 if (quiet_info)
0181 return;
0182
0183 va_start(list, fmt);
0184 vfprintf(stderr, fmt, list);
0185 va_end(list);
0186 }
0187
0188 void os_warn(const char *fmt, ...)
0189 {
0190 va_list list;
0191
0192 va_start(list, fmt);
0193 vfprintf(stderr, fmt, list);
0194 va_end(list);
0195 }