/*1* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)2* Licensed under the GPL3*/45#include <stdio.h>6#include <stdlib.h>7#include <unistd.h>8#include <errno.h>9#include <signal.h>10#include <string.h>11#include <termios.h>12#include <wait.h>13#include <sys/mman.h>14#include <sys/utsname.h>15#include "kern_constants.h"16#include "os.h"17#include "user.h"1819void stack_protections(unsigned long address)20{21if (mprotect((void *) address, UM_THREAD_SIZE,22PROT_READ | PROT_WRITE | PROT_EXEC) < 0)23panic("protecting stack failed, errno = %d", errno);24}2526int raw(int fd)27{28struct termios tt;29int err;3031CATCH_EINTR(err = tcgetattr(fd, &tt));32if (err < 0)33return -errno;3435cfmakeraw(&tt);3637CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));38if (err < 0)39return -errno;4041/*42* XXX tcsetattr could have applied only some changes43* (and cfmakeraw() is a set of changes)44*/45return 0;46}4748void setup_machinename(char *machine_out)49{50struct utsname host;5152uname(&host);53#ifdef UML_CONFIG_UML_X8654# ifndef UML_CONFIG_64BIT55if (!strcmp(host.machine, "x86_64")) {56strcpy(machine_out, "i686");57return;58}59# else60if (!strcmp(host.machine, "i686")) {61strcpy(machine_out, "x86_64");62return;63}64# endif65#endif66strcpy(machine_out, host.machine);67}6869void setup_hostinfo(char *buf, int len)70{71struct utsname host;7273uname(&host);74snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,75host.release, host.version, host.machine);76}7778/*79* We cannot use glibc's abort(). It makes use of tgkill() which80* has no effect within UML's kernel threads.81* After that glibc would execute an invalid instruction to kill82* the calling process and UML crashes with SIGSEGV.83*/84static inline void __attribute__ ((noreturn)) uml_abort(void)85{86sigset_t sig;8788fflush(NULL);8990if (!sigemptyset(&sig) && !sigaddset(&sig, SIGABRT))91sigprocmask(SIG_UNBLOCK, &sig, 0);9293for (;;)94if (kill(getpid(), SIGABRT) < 0)95exit(127);96}9798void os_dump_core(void)99{100int pid;101102signal(SIGSEGV, SIG_DFL);103104/*105* We are about to SIGTERM this entire process group to ensure that106* nothing is around to run after the kernel exits. The107* kernel wants to abort, not die through SIGTERM, so we108* ignore it here.109*/110111signal(SIGTERM, SIG_IGN);112kill(0, SIGTERM);113/*114* Most of the other processes associated with this UML are115* likely sTopped, so give them a SIGCONT so they see the116* SIGTERM.117*/118kill(0, SIGCONT);119120/*121* Now, having sent signals to everyone but us, make sure they122* die by ptrace. Processes can survive what's been done to123* them so far - the mechanism I understand is receiving a124* SIGSEGV and segfaulting immediately upon return. There is125* always a SIGSEGV pending, and (I'm guessing) signals are126* processed in numeric order so the SIGTERM (signal 15 vs127* SIGSEGV being signal 11) is never handled.128*129* Run a waitpid loop until we get some kind of error.130* Hopefully, it's ECHILD, but there's not a lot we can do if131* it's something else. Tell os_kill_ptraced_process not to132* wait for the child to report its death because there's133* nothing reasonable to do if that fails.134*/135136while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)137os_kill_ptraced_process(pid, 0);138139uml_abort();140}141142void um_early_printk(const char *s, unsigned int n)143{144printf("%.*s", n, s);145}146147148