util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #if defined(UML_CONFIG_UML_X86) && !defined(UML_CONFIG_64BIT)
  72. if (!strcmp(host.machine, "x86_64")) {
  73. strcpy(machine_out, "i686");
  74. return;
  75. }
  76. #endif
  77. strcpy(machine_out, host.machine);
  78. }
  79. char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
  80. void setup_hostinfo(void)
  81. {
  82. struct utsname host;
  83. uname(&host);
  84. sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
  85. host.release, host.version, host.machine);
  86. }
  87. int setjmp_wrapper(void (*proc)(void *, void *), ...)
  88. {
  89. va_list args;
  90. jmp_buf buf;
  91. int n;
  92. n = UML_SETJMP(&buf);
  93. if(n == 0){
  94. va_start(args, proc);
  95. (*proc)(&buf, &args);
  96. }
  97. va_end(args);
  98. return n;
  99. }