syscall.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <sys.h>
  12. #include <fcall.h>
  13. char buf[1048576];
  14. #define NARG 5
  15. uintptr arg[NARG];
  16. /* system calls not defined in libc.h */
  17. int sysr1(void);
  18. int _stat(char*, char*);
  19. int _fstat(int, char*);
  20. int _errstr(char*);
  21. int _wstat(char*, char*);
  22. int _fwstat(int, char*);
  23. int _read(int, void*, int);
  24. int _write(int, void*, int);
  25. int _read9p(int, void*, int);
  26. int _write9p(int, void*, int);
  27. int brk_(void*);
  28. int _nfstat(int, void*, int);
  29. int _nstat(char*, void*, int);
  30. int _nfwstat(int, void*, int);
  31. int _nwstat(char*, void*, int);
  32. int _fsession(char*, void*, int);
  33. int _mount(int, char*, int, char*);
  34. int _wait(void*);
  35. struct{
  36. char *name;
  37. int (*func)(...);
  38. }tab[]={
  39. #include "tab.h"
  40. 0, 0
  41. };
  42. uintptr parse(char *);
  43. void catch(void*, char*);
  44. char*
  45. xctime(uint32_t t)
  46. {
  47. char *buf, *s;
  48. s = ctime(t);
  49. s[strlen(s)-1] = '\0'; /* remove newline */
  50. buf = malloc(512);
  51. if(buf == nil)
  52. sysfatal("can't malloc: %r");
  53. snprint(buf, 512, "%s (%lud)", s, t);
  54. return buf;
  55. }
  56. char*
  57. lstime(int32_t l)
  58. {
  59. static char buf[32];
  60. char *t;
  61. int32_t clk;
  62. clk = time(0);
  63. t = ctime(l);
  64. /* 6 months in the past or a day in the future */
  65. if(l<clk-180L*24*60*60 || clk+24L*60*60<l){
  66. memmove(buf, t+4, 7); /* month and day */
  67. memmove(buf+7, t+23, 5); /* year */
  68. }else
  69. memmove(buf, t+4, 12); /* skip day of week */
  70. buf[12] = 0;
  71. return buf;
  72. }
  73. void
  74. main(int argc, char *argv[])
  75. {
  76. int i, j, c;
  77. int oflag, xflag, sflag;
  78. vlong r;
  79. Dir d;
  80. char strs[1024];
  81. char ebuf[1024];
  82. fmtinstall('M', dirmodefmt);
  83. oflag = 0;
  84. xflag = 0;
  85. sflag = 0;
  86. ARGBEGIN{
  87. case 'o':
  88. oflag++;
  89. break;
  90. case 's':
  91. sflag++;
  92. break;
  93. case 'x':
  94. xflag++;
  95. break;
  96. default:
  97. goto Usage;
  98. }ARGEND
  99. if(argc<1 || argc>1+NARG){
  100. Usage:
  101. fprint(2, "usage: syscall [-ox] entry [args; buf==1MB buffer]\n");
  102. fprint(2, "\tsyscall write 1 hello 5\n");
  103. fprint(2, "\tsyscall -o errstr buf 1024\n");
  104. fprint(2, "\tsyscall -[xs] stat file buf 1024\n");
  105. exits("usage");
  106. }
  107. for(i=1; i<argc; i++)
  108. arg[i-1] = parse(argv[i]);
  109. notify(catch);
  110. for(i=0; tab[i].name; i++)
  111. if(strcmp(tab[i].name, argv[0])==0){
  112. /* special case for seek, pread, pwrite; vlongs are problematic */
  113. if(strcmp(argv[0], "seek") == 0)
  114. r=seek(arg[0], strtoll(argv[2], 0, 0), arg[2]);
  115. else if(strcmp(argv[0], "pread") == 0)
  116. r=pread(arg[0], (void*)arg[1], arg[2], strtoll(argv[4], 0, 0));
  117. else if(strcmp(argv[0], "pwrite") == 0)
  118. r=pwrite(arg[0], (void*)arg[1], arg[2], strtoll(argv[4], 0, 0));
  119. else
  120. r=(*tab[i].func)(arg[0], arg[1], arg[2], arg[3], arg[4]);
  121. if(r == -1){
  122. errstr(ebuf, sizeof ebuf);
  123. fprint(2, "syscall: return %lld, error:%s\n", r, ebuf);
  124. }else{
  125. ebuf[0] = 0;
  126. fprint(2, "syscall: return %lld, no error\n", r);
  127. }
  128. if(oflag)
  129. print("%s\n", buf);
  130. if(xflag){
  131. for(j=0; j<r; j++){
  132. if(j%16 == 0)
  133. print("%.4x\t", j);
  134. c = buf[j]&0xFF;
  135. if('!'<=c && c<='~')
  136. print(" %c ", c);
  137. else
  138. print("%.2ux ", c);
  139. if(j%16 == 15)
  140. print("\n");
  141. }
  142. print("\n");
  143. }
  144. if(sflag && r > 0){
  145. r = convM2D((uchar*)buf, r, &d, strs);
  146. if(r <= BIT16SZ)
  147. print("short stat message\n");
  148. else{
  149. print("[%s] ", d.muid);
  150. print("(%.16llux %lud %.2ux) ", d.qid.path, d.qid.vers, d.qid.type);
  151. print("%M (%luo) ", d.mode, d.mode);
  152. print("%c %d ", d.type, d.dev);
  153. print("%s %s ", d.uid, d.gid);
  154. print("%lld ", d.length);
  155. print("%s ", lstime(d.mtime));
  156. print("%s\n", d.name);
  157. print("\tmtime: %s\n\tatime: %s\n", xctime(d.mtime), xctime(d.atime));
  158. }
  159. }
  160. exits(ebuf);
  161. }
  162. fprint(2, "syscall: %s not known\n", argv[0]);
  163. exits("unknown");
  164. }
  165. uintptr
  166. parse(char *s)
  167. {
  168. char *t;
  169. uintptr l;
  170. if(strcmp(s, "buf") == 0)
  171. return (uintptr)buf;
  172. l = strtoull(s, &t, 0);
  173. if(t>s && *t==0)
  174. return l;
  175. return (uintptr)s;
  176. }
  177. void
  178. catch(void *, char *msg)
  179. {
  180. fprint(2, "syscall: received note='%s'\n", msg);
  181. noted(NDFLT);
  182. }