os.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <sys/types.h>
  2. #include <time.h>
  3. #include <termios.h>
  4. #include <signal.h>
  5. #include <pwd.h>
  6. #include <sched.h>
  7. #include <sys/resource.h>
  8. #include <sys/wait.h>
  9. #include <sys/time.h>
  10. #include <stdint.h>
  11. #include "dat.h"
  12. #include "fns.h"
  13. #include "error.h"
  14. #include <semaphore.h>
  15. #include <raise.h>
  16. /* glibc 2.3.3-NTPL messes up getpid() by trying to cache the result, so we'll do it ourselves */
  17. #include <sys/syscall.h>
  18. #define getpid() syscall(SYS_getpid)
  19. enum
  20. {
  21. DELETE = 0x7f,
  22. CTRLC = 'C'-'@',
  23. NSTACKSPERALLOC = 16,
  24. X11STACK= 256*1024
  25. };
  26. char *hosttype = "Linux";
  27. typedef sem_t Sem;
  28. extern int dflag;
  29. int gidnobody = -1;
  30. int uidnobody = -1;
  31. static struct termios tinit;
  32. static void
  33. sysfault(char *what, void *addr)
  34. {
  35. char buf[64];
  36. snprint(buf, sizeof(buf), "sys: %s%#p", what, addr);
  37. disfault(nil, buf);
  38. }
  39. static void
  40. trapILL(int signo, siginfo_t *si, void *a)
  41. {
  42. USED(signo);
  43. USED(a);
  44. sysfault("illegal instruction pc=", si->si_addr);
  45. }
  46. static int
  47. isnilref(siginfo_t *si)
  48. {
  49. return si != 0 && (si->si_addr == (void*)~(uintptr_t)0 || (uintptr_t)si->si_addr < 512);
  50. }
  51. static void
  52. trapmemref(int signo, siginfo_t *si, void *a)
  53. {
  54. USED(a); /* ucontext_t*, could fetch pc in machine-dependent way */
  55. if(isnilref(si))
  56. disfault(nil, exNilref);
  57. else if(signo == SIGBUS)
  58. sysfault("bad address addr=", si->si_addr); /* eg, misaligned */
  59. else
  60. sysfault("segmentation violation addr=", si->si_addr);
  61. }
  62. static void
  63. trapFPE(int signo, siginfo_t *si, void *a)
  64. {
  65. char buf[64];
  66. USED(signo);
  67. USED(a);
  68. snprint(buf, sizeof(buf), "sys: fp: exception status=%.4lux pc=%#p", getfsr(), si->si_addr);
  69. disfault(nil, buf);
  70. }
  71. static void
  72. trapUSR1(int signo)
  73. {
  74. int intwait;
  75. USED(signo);
  76. intwait = up->intwait;
  77. up->intwait = 0; /* clear it to let proc continue in osleave */
  78. if(up->type != Interp) /* Used to unblock pending I/O */
  79. return;
  80. if(intwait == 0) /* Not posted so it's a sync error */
  81. disfault(nil, Eintr); /* Should never happen */
  82. }
  83. void
  84. oslongjmp(void *regs, osjmpbuf env, int val)
  85. {
  86. USED(regs);
  87. siglongjmp(env, val);
  88. }
  89. static void
  90. termset(void)
  91. {
  92. struct termios t;
  93. tcgetattr(0, &t);
  94. tinit = t;
  95. t.c_lflag &= ~(ICANON|ECHO|ISIG);
  96. t.c_cc[VMIN] = 1;
  97. t.c_cc[VTIME] = 0;
  98. tcsetattr(0, TCSANOW, &t);
  99. }
  100. static void
  101. termrestore(void)
  102. {
  103. tcsetattr(0, TCSANOW, &tinit);
  104. }
  105. void
  106. cleanexit(int x)
  107. {
  108. USED(x);
  109. if(up->intwait) {
  110. up->intwait = 0;
  111. return;
  112. }
  113. if(dflag == 0)
  114. termrestore();
  115. kill(0, SIGKILL);
  116. exit(0);
  117. }
  118. void
  119. osreboot(char *file, char **argv)
  120. {
  121. if(dflag == 0)
  122. termrestore();
  123. execvp(file, argv);
  124. error("reboot failure");
  125. }
  126. void
  127. libinit(char *imod)
  128. {
  129. struct sigaction act;
  130. struct passwd *pw;
  131. Proc *p;
  132. char sys[64];
  133. setsid();
  134. gethostname(sys, sizeof(sys));
  135. kstrdup(&ossysname, sys);
  136. pw = getpwnam("nobody");
  137. if(pw != nil) {
  138. uidnobody = pw->pw_uid;
  139. gidnobody = pw->pw_gid;
  140. }
  141. if(dflag == 0)
  142. termset();
  143. memset(&act, 0, sizeof(act));
  144. act.sa_handler = trapUSR1;
  145. sigaction(SIGUSR1, &act, nil);
  146. act.sa_handler = SIG_IGN;
  147. sigaction(SIGCHLD, &act, nil);
  148. /*
  149. * For the correct functioning of devcmd in the
  150. * face of exiting slaves
  151. */
  152. signal(SIGPIPE, SIG_IGN);
  153. if(signal(SIGTERM, SIG_IGN) != SIG_IGN)
  154. signal(SIGTERM, cleanexit);
  155. if(signal(SIGINT, SIG_IGN) != SIG_IGN)
  156. signal(SIGINT, cleanexit);
  157. if(sflag == 0) {
  158. act.sa_flags = SA_SIGINFO;
  159. act.sa_sigaction = trapILL;
  160. sigaction(SIGILL, &act, nil);
  161. act.sa_sigaction = trapFPE;
  162. sigaction(SIGFPE, &act, nil);
  163. act.sa_sigaction = trapmemref;
  164. sigaction(SIGBUS, &act, nil);
  165. sigaction(SIGSEGV, &act, nil);
  166. act.sa_flags &= ~SA_SIGINFO;
  167. }
  168. p = newproc();
  169. kprocinit(p);
  170. pw = getpwuid(getuid());
  171. if(pw != nil)
  172. kstrdup(&eve, pw->pw_name);
  173. else
  174. print("cannot getpwuid\n");
  175. p->env->uid = getuid();
  176. p->env->gid = getgid();
  177. emuinit(imod);
  178. }
  179. int
  180. readkbd(void)
  181. {
  182. int n;
  183. char buf[1];
  184. n = read(0, buf, sizeof(buf));
  185. if(n < 0)
  186. print("keyboard close (n=%d, %s)\n", n, strerror(errno));
  187. if(n <= 0)
  188. pexit("keyboard thread", 0);
  189. switch(buf[0]) {
  190. case '\r':
  191. buf[0] = '\n';
  192. break;
  193. case DELETE:
  194. buf[0] = 'H' - '@';
  195. break;
  196. case CTRLC:
  197. cleanexit(0);
  198. break;
  199. }
  200. return buf[0];
  201. }
  202. /*
  203. * Return an abitrary millisecond clock time
  204. */
  205. long
  206. osmillisec(void)
  207. {
  208. static long sec0 = 0, usec0;
  209. struct timeval t;
  210. if(gettimeofday(&t,(struct timezone*)0)<0)
  211. return 0;
  212. if(sec0 == 0) {
  213. sec0 = t.tv_sec;
  214. usec0 = t.tv_usec;
  215. }
  216. return (t.tv_sec-sec0)*1000+(t.tv_usec-usec0+500)/1000;
  217. }
  218. /*
  219. * Return the time since the epoch in nanoseconds and microseconds
  220. * The epoch is defined at 1 Jan 1970
  221. */
  222. vlong
  223. osnsec(void)
  224. {
  225. struct timeval t;
  226. gettimeofday(&t, nil);
  227. return (vlong)t.tv_sec*1000000000L + t.tv_usec*1000;
  228. }
  229. vlong
  230. osusectime(void)
  231. {
  232. struct timeval t;
  233. gettimeofday(&t, nil);
  234. return (vlong)t.tv_sec * 1000000 + t.tv_usec;
  235. }
  236. int
  237. osmillisleep(ulong milsec)
  238. {
  239. struct timespec time;
  240. time.tv_sec = milsec/1000;
  241. time.tv_nsec= (milsec%1000)*1000000;
  242. nanosleep(&time, NULL);
  243. return 0;
  244. }
  245. int
  246. limbosleep(ulong milsec)
  247. {
  248. return osmillisleep(milsec);
  249. }