Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
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      * XXX tcsetattr could have applied only some changes
0045      * (and cfmakeraw() is a set of changes)
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  * We cannot use glibc's abort(). It makes use of tgkill() which
0082  * has no effect within UML's kernel threads.
0083  * After that glibc would execute an invalid instruction to kill
0084  * the calling process and UML crashes with SIGSEGV.
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  * UML helper threads must not handle SIGWINCH/INT/TERM
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      * We are about to SIGTERM this entire process group to ensure that
0123      * nothing is around to run after the kernel exits.  The
0124      * kernel wants to abort, not die through SIGTERM, so we
0125      * ignore it here.
0126      */
0127 
0128     signal(SIGTERM, SIG_IGN);
0129     kill(0, SIGTERM);
0130     /*
0131      * Most of the other processes associated with this UML are
0132      * likely sTopped, so give them a SIGCONT so they see the
0133      * SIGTERM.
0134      */
0135     kill(0, SIGCONT);
0136 
0137     /*
0138      * Now, having sent signals to everyone but us, make sure they
0139      * die by ptrace.  Processes can survive what's been done to
0140      * them so far - the mechanism I understand is receiving a
0141      * SIGSEGV and segfaulting immediately upon return.  There is
0142      * always a SIGSEGV pending, and (I'm guessing) signals are
0143      * processed in numeric order so the SIGTERM (signal 15 vs
0144      * SIGSEGV being signal 11) is never handled.
0145      *
0146      * Run a waitpid loop until we get some kind of error.
0147      * Hopefully, it's ECHILD, but there's not a lot we can do if
0148      * it's something else.  Tell os_kill_ptraced_process not to
0149      * wait for the child to report its death because there's
0150      * nothing reasonable to do if that fails.
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 }