util.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <sys/mman.h>
  10. #include <sys/stat.h>
  11. #include <sys/utsname.h>
  12. #include <sys/param.h>
  13. #include <sys/time.h>
  14. #include "asm/types.h"
  15. #include <ctype.h>
  16. #include <signal.h>
  17. #include <wait.h>
  18. #include <errno.h>
  19. #include <stdarg.h>
  20. #include <sched.h>
  21. #include <termios.h>
  22. #include <string.h>
  23. #include "kern_util.h"
  24. #include "user.h"
  25. #include "mem_user.h"
  26. #include "init.h"
  27. #include "ptrace_user.h"
  28. #include "uml-config.h"
  29. #include "os.h"
  30. #include "longjmp.h"
  31. #include "kern_constants.h"
  32. void stack_protections(unsigned long address)
  33. {
  34. if(mprotect((void *) address, UM_THREAD_SIZE,
  35. PROT_READ | PROT_WRITE | PROT_EXEC) < 0)
  36. panic("protecting stack failed, errno = %d", errno);
  37. }
  38. int raw(int fd)
  39. {
  40. struct termios tt;
  41. int err;
  42. CATCH_EINTR(err = tcgetattr(fd, &tt));
  43. if(err < 0)
  44. return -errno;
  45. cfmakeraw(&tt);
  46. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  47. if(err < 0)
  48. return -errno;
  49. /* XXX tcsetattr could have applied only some changes
  50. * (and cfmakeraw() is a set of changes) */
  51. return 0;
  52. }
  53. void setup_machinename(char *machine_out)
  54. {
  55. struct utsname host;
  56. uname(&host);
  57. #ifdef UML_CONFIG_UML_X86
  58. # ifndef UML_CONFIG_64BIT
  59. if (!strcmp(host.machine, "x86_64")) {
  60. strcpy(machine_out, "i686");
  61. return;
  62. }
  63. # else
  64. if (!strcmp(host.machine, "i686")) {
  65. strcpy(machine_out, "x86_64");
  66. return;
  67. }
  68. # endif
  69. #endif
  70. strcpy(machine_out, host.machine);
  71. }
  72. void setup_hostinfo(char *buf, int len)
  73. {
  74. struct utsname host;
  75. uname(&host);
  76. snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
  77. host.release, host.version, host.machine);
  78. }
  79. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  80. {
  81. va_list args;
  82. jmp_buf buf;
  83. int n;
  84. n = UML_SETJMP(&buf);
  85. if(n == 0){
  86. va_start(args, proc);
  87. (*proc)(&buf, &args);
  88. }
  89. va_end(args);
  90. return n;
  91. }
  92. void os_dump_core(void)
  93. {
  94. signal(SIGSEGV, SIG_DFL);
  95. abort();
  96. }