util.c 2.4 KB

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