util.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "user_util.h"
  24. #include "kern_util.h"
  25. #include "user.h"
  26. #include "mem_user.h"
  27. #include "init.h"
  28. #include "ptrace_user.h"
  29. #include "uml-config.h"
  30. #include "os.h"
  31. #include "longjmp.h"
  32. void stack_protections(unsigned long address)
  33. {
  34. int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  35. if(mprotect((void *) address, page_size(), prot) < 0)
  36. panic("protecting stack failed, errno = %d", errno);
  37. }
  38. void task_protections(unsigned long address)
  39. {
  40. unsigned long guard = address + page_size();
  41. unsigned long stack = guard + page_size();
  42. int prot = 0, pages;
  43. #ifdef notdef
  44. if(mprotect((void *) stack, page_size(), prot) < 0)
  45. panic("protecting guard page failed, errno = %d", errno);
  46. #endif
  47. pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
  48. prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  49. if(mprotect((void *) stack, pages * page_size(), prot) < 0)
  50. panic("protecting stack failed, errno = %d", errno);
  51. }
  52. int raw(int fd)
  53. {
  54. struct termios tt;
  55. int err;
  56. CATCH_EINTR(err = tcgetattr(fd, &tt));
  57. if(err < 0)
  58. return -errno;
  59. cfmakeraw(&tt);
  60. CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  61. if(err < 0)
  62. return -errno;
  63. /* XXX tcsetattr could have applied only some changes
  64. * (and cfmakeraw() is a set of changes) */
  65. return(0);
  66. }
  67. void setup_machinename(char *machine_out)
  68. {
  69. struct utsname host;
  70. uname(&host);
  71. #ifdef UML_CONFIG_UML_X86
  72. # ifndef UML_CONFIG_64BIT
  73. if (!strcmp(host.machine, "x86_64")) {
  74. strcpy(machine_out, "i686");
  75. return;
  76. }
  77. # else
  78. if (!strcmp(host.machine, "i686")) {
  79. strcpy(machine_out, "x86_64");
  80. return;
  81. }
  82. # endif
  83. #endif
  84. strcpy(machine_out, host.machine);
  85. }
  86. char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
  87. void setup_hostinfo(void)
  88. {
  89. struct utsname host;
  90. uname(&host);
  91. sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
  92. host.release, host.version, host.machine);
  93. }
  94. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  95. {
  96. va_list args;
  97. jmp_buf buf;
  98. int n;
  99. n = UML_SETJMP(&buf);
  100. if(n == 0){
  101. va_start(args, proc);
  102. (*proc)(&buf, &args);
  103. }
  104. va_end(args);
  105. return n;
  106. }