devproc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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 "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "../port/edf.h"
  16. #include "tos.h"
  17. #include <trace.h>
  18. #include "ureg.h"
  19. enum
  20. {
  21. Qdir,
  22. Qtrace,
  23. Qtracepids,
  24. Qargs,
  25. Qctl,
  26. Qfd,
  27. Qfpregs,
  28. Qgdbregs,
  29. Qkregs,
  30. Qmem,
  31. Qnote,
  32. Qnoteid,
  33. Qnotepg,
  34. Qns,
  35. Qproc,
  36. Qregs,
  37. Qsegment,
  38. Qstatus,
  39. Qstrace,
  40. Qtext,
  41. Qwait,
  42. Qprofile,
  43. Qsyscall,
  44. Qcore,
  45. Qtls,
  46. Qpager,
  47. };
  48. enum
  49. {
  50. CMclose,
  51. CMclosefiles,
  52. CMfixedpri,
  53. CMhang,
  54. CMkill,
  55. CMnohang,
  56. CMnoswap,
  57. CMpri,
  58. CMprivate,
  59. CMprofile,
  60. CMstart,
  61. CMstartstop,
  62. CMstartsyscall,
  63. CMstop,
  64. CMwaitstop,
  65. CMwired,
  66. CMtrace,
  67. /* real time */
  68. CMperiod,
  69. CMdeadline,
  70. CMcost,
  71. CMsporadic,
  72. CMdeadlinenotes,
  73. CMadmit,
  74. CMextra,
  75. CMexpel,
  76. CMevent,
  77. CMcore,
  78. CMstraceme,
  79. CMstraceall,
  80. CMstraceoff,
  81. };
  82. enum{
  83. Nevents = 0x4000,
  84. Emask = Nevents - 1,
  85. Ntracedpids = 1024,
  86. };
  87. #define STATSIZE (2*KNAMELEN+NUMSIZE + 9*NUMSIZE + 6*NUMSIZE + 2*NUMSIZE + 1)
  88. /*
  89. * Status, fd, and ns are left fully readable (0444) because of their use in debugging,
  90. * particularly on shared servers.
  91. * Arguably, ns and fd shouldn't be readable; if you'd prefer, change them to 0000
  92. */
  93. Dirtab procdir[] =
  94. {
  95. "args", {Qargs}, 0, 0660,
  96. "ctl", {Qctl}, 0, 0000,
  97. "fd", {Qfd}, 0, 0444,
  98. "fpregs", {Qfpregs}, 0, 0000,
  99. "kregs", {Qkregs}, sizeof(Ureg), 0600,
  100. "mem", {Qmem}, 0, 0000,
  101. "note", {Qnote}, 0, 0000,
  102. "noteid", {Qnoteid}, 0, 0664,
  103. "notepg", {Qnotepg}, 0, 0000,
  104. "ns", {Qns}, 0, 0444,
  105. "proc", {Qproc}, 0, 0400,
  106. "gdbregs", {Qgdbregs}, GDB_NUMREGBYTES, 0000,
  107. "regs", {Qregs}, sizeof(Ureg), 0000,
  108. "segment", {Qsegment}, 0, 0444,
  109. "status", {Qstatus}, STATSIZE, 0444,
  110. "strace", {Qstrace}, 0, 0666,
  111. "text", {Qtext}, 0, 0000,
  112. "wait", {Qwait}, 0, 0400,
  113. "profile", {Qprofile}, 0, 0400,
  114. "syscall", {Qsyscall}, 0, 0400,
  115. "core", {Qcore}, 0, 0444,
  116. "tls", {Qtls}, 0, 0600,
  117. "pager", {Qpager}, 0, 0600|DMEXCL,
  118. };
  119. static
  120. Cmdtab proccmd[] = {
  121. CMclose, "close", 2,
  122. CMclosefiles, "closefiles", 1,
  123. CMfixedpri, "fixedpri", 2,
  124. CMhang, "hang", 1,
  125. CMnohang, "nohang", 1,
  126. CMnoswap, "noswap", 1,
  127. CMkill, "kill", 1,
  128. CMpri, "pri", 2,
  129. CMprivate, "private", 1,
  130. CMprofile, "profile", 1,
  131. CMstart, "start", 1,
  132. CMstartstop, "startstop", 1,
  133. CMstartsyscall, "startsyscall", 1,
  134. CMstop, "stop", 1,
  135. CMwaitstop, "waitstop", 1,
  136. CMwired, "wired", 2,
  137. CMtrace, "trace", 0,
  138. CMperiod, "period", 2,
  139. CMdeadline, "deadline", 2,
  140. CMcost, "cost", 2,
  141. CMsporadic, "sporadic", 1,
  142. CMdeadlinenotes, "deadlinenotes", 1,
  143. CMadmit, "admit", 1,
  144. CMextra, "extra", 1,
  145. CMexpel, "expel", 1,
  146. CMevent, "event", 1,
  147. CMcore, "core", 2,
  148. CMstraceme, "straceme", 0,
  149. CMstraceall, "straceall", 0,
  150. CMstraceoff, "straceoff", 0,
  151. };
  152. /*
  153. * Qids are, in path:
  154. * 4 bits of file type (qids above)
  155. * 23 bits of process slot number + 1
  156. * in vers,
  157. * 32 bits of pid, for consistency checking
  158. * If notepg, c->pgrpid.path is pgrp slot, .vers is noteid.
  159. */
  160. #define QSHIFT 5 /* location in qid of proc slot # */
  161. #define SLOTBITS 23 /* number of bits in the slot */
  162. #define QIDMASK ((1<<QSHIFT)-1)
  163. #define SLOTMASK ((1<<SLOTBITS)-1 << QSHIFT)
  164. #define QID(q) ((((uint32_t)(q).path)&QIDMASK)>>0)
  165. #define SLOT(q) (((((uint32_t)(q).path)&SLOTMASK)>>QSHIFT)-1)
  166. #define PID(q) ((q).vers)
  167. #define NOTEID(q) ((q).vers)
  168. static void procctlreq(Proc*, char*, int);
  169. static int procctlmemio(Proc*, uintptr_t, int, void*, int);
  170. static Chan* proctext(Chan*, Proc*);
  171. static Segment* txt2data(Proc*, Segment*);
  172. static int procstopped(void*);
  173. static void mntscan(Mntwalk*, Proc*);
  174. static Traceevent *tevents;
  175. static char *tpids, *tpidsc, *tpidse;
  176. static Lock tlock;
  177. static int topens;
  178. static int tproduced, tconsumed;
  179. static void notrace(Proc*, int, int64_t);
  180. void (*proctrace)(Proc*, int, int64_t) = notrace;
  181. static void
  182. profclock(Ureg *ur, Timer *ti)
  183. {
  184. Proc *up = externup();
  185. Tos *tos;
  186. if(up == nil || up->state != Running)
  187. return;
  188. /* user profiling clock */
  189. if(userureg(ur)){
  190. tos = (Tos*)(USTKTOP-sizeof(Tos));
  191. tos->clock += TK2MS(1);
  192. segclock(userpc(ur));
  193. }
  194. }
  195. static int
  196. procgen(Chan *c, char *name, Dirtab *tab, int j, int s, Dir *dp)
  197. {
  198. Proc *up = externup();
  199. Qid qid;
  200. Proc *p;
  201. char *ename;
  202. int pid, sno;
  203. uint32_t path, perm, len;
  204. if(s == DEVDOTDOT){
  205. mkqid(&qid, Qdir, 0, QTDIR);
  206. devdir(c, qid, "#p", 0, eve, 0555, dp);
  207. return 1;
  208. }
  209. if(c->qid.path == Qdir){
  210. if(s == 0){
  211. strcpy(up->genbuf, "trace");
  212. mkqid(&qid, Qtrace, -1, QTFILE);
  213. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  214. return 1;
  215. }
  216. if(s == 1){
  217. strcpy(up->genbuf, "tracepids");
  218. mkqid(&qid, Qtracepids, -1, QTFILE);
  219. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  220. return 1;
  221. }
  222. s -= 2;
  223. if(name != nil){
  224. /* ignore s and use name to find pid */
  225. pid = strtol(name, &ename, 10);
  226. if(pid<=0 || ename[0]!='\0')
  227. return -1;
  228. s = psindex(pid);
  229. if(s < 0)
  230. return -1;
  231. }
  232. else if(s >= conf.nproc)
  233. return -1;
  234. if((p = psincref(s)) == nil || (pid = p->pid) == 0)
  235. return 0;
  236. snprint(up->genbuf, sizeof up->genbuf, "%u", pid);
  237. /*
  238. * String comparison is done in devwalk so
  239. * name must match its formatted pid.
  240. */
  241. if(name != nil && strcmp(name, up->genbuf) != 0)
  242. return -1;
  243. mkqid(&qid, (s+1)<<QSHIFT, pid, QTDIR);
  244. devdir(c, qid, up->genbuf, 0, p->user, DMDIR|0555, dp);
  245. psdecref(p);
  246. return 1;
  247. }
  248. if(c->qid.path == Qtrace){
  249. strcpy(up->genbuf, "trace");
  250. mkqid(&qid, Qtrace, -1, QTFILE);
  251. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  252. return 1;
  253. }
  254. if(c->qid.path == Qtracepids){
  255. strcpy(up->genbuf, "tracepids");
  256. mkqid(&qid, Qtrace, -1, QTFILE);
  257. devdir(c, qid, up->genbuf, 0, eve, 0444, dp);
  258. return 1;
  259. }
  260. if(s >= nelem(procdir))
  261. return -1;
  262. if(tab)
  263. panic("procgen");
  264. tab = &procdir[s];
  265. path = c->qid.path&~(((1<<QSHIFT)-1)); /* slot component */
  266. if((p = psincref(SLOT(c->qid))) == nil)
  267. return -1;
  268. perm = tab->perm;
  269. if(perm == 0)
  270. perm = p->procmode;
  271. else /* just copy read bits */
  272. perm |= p->procmode & 0444;
  273. len = tab->length;
  274. switch(QID(c->qid)) {
  275. case Qwait:
  276. len = p->nwait; /* incorrect size, but >0 means there's something to read */
  277. break;
  278. case Qprofile: /* TODO(aki): test this */
  279. len = 0;
  280. for(sno = 0; sno < NSEG; sno++){
  281. if(p->seg[sno] != nil && (p->seg[sno]->type & SG_EXEC) != 0){
  282. Segment *s;
  283. s = p->seg[sno];
  284. if(s->profile)
  285. len += ((s->top-s->base)>>LRESPROF) * sizeof s->profile[0];
  286. }
  287. }
  288. break;
  289. }
  290. mkqid(&qid, path|tab->qid.path, c->qid.vers, QTFILE);
  291. devdir(c, qid, tab->name, len, p->user, perm, dp);
  292. psdecref(p);
  293. return 1;
  294. }
  295. static void
  296. notrace(Proc* p, int n, int64_t m)
  297. {
  298. }
  299. static Lock tlck;
  300. static void
  301. _proctrace(Proc* p, int etype, int64_t ts)
  302. {
  303. Traceevent *te;
  304. int tp;
  305. ilock(&tlck);
  306. if (p->trace == 0 || topens == 0 ||
  307. tproduced - tconsumed >= Nevents){
  308. iunlock(&tlck);
  309. return;
  310. }
  311. tp = tproduced++;
  312. iunlock(&tlck);
  313. te = &tevents[tp&Emask];
  314. te->pid = p->pid;
  315. te->etype = etype;
  316. if (ts == 0)
  317. te->time = todget(nil);
  318. else
  319. te->time = ts;
  320. te->core = machp()->machno;
  321. }
  322. void
  323. proctracepid(Proc *p)
  324. {
  325. if(p->trace == 1 && proctrace != notrace){
  326. p->trace = 2;
  327. ilock(&tlck);
  328. tpidsc = seprint(tpidsc, tpidse, "%d %s\n", p->pid, p->text);
  329. iunlock(&tlck);
  330. }
  331. }
  332. static void
  333. procinit(void)
  334. {
  335. if(conf.nproc >= (SLOTMASK>>QSHIFT) - 1)
  336. print("warning: too many procs for devproc\n");
  337. addclock0link((void (*)(void))profclock, 113); /* Relative prime to HZ */
  338. }
  339. static Chan*
  340. procattach(char *spec)
  341. {
  342. return devattach('p', spec);
  343. }
  344. static Walkqid*
  345. procwalk(Chan *c, Chan *nc, char **name, int nname)
  346. {
  347. return devwalk(c, nc, name, nname, 0, 0, procgen);
  348. }
  349. static int32_t
  350. procstat(Chan *c, uint8_t *db, int32_t n)
  351. {
  352. return devstat(c, db, n, 0, 0, procgen);
  353. }
  354. /*
  355. * none can't read or write state on other
  356. * processes. This is to contain access of
  357. * servers running as none should they be
  358. * subverted by, for example, a stack attack.
  359. */
  360. static void
  361. nonone(Proc *p)
  362. {
  363. Proc *up = externup();
  364. if(p == up)
  365. return;
  366. if(strcmp(up->user, "none") != 0)
  367. return;
  368. if(iseve())
  369. return;
  370. error(Eperm);
  371. }
  372. static Chan*
  373. procopen(Chan *c, int omode)
  374. {
  375. Proc *up = externup();
  376. Proc *p;
  377. Pgrp *pg;
  378. Chan *tc;
  379. int pid;
  380. if(c->qid.type & QTDIR)
  381. return devopen(c, omode, 0, 0, procgen);
  382. if(QID(c->qid) == Qtrace){
  383. if (omode != OREAD)
  384. error(Eperm);
  385. lock(&tlock);
  386. if (waserror()){
  387. unlock(&tlock);
  388. nexterror();
  389. }
  390. if (topens > 0)
  391. error("already open");
  392. topens++;
  393. if (tevents == nil){
  394. tevents = (Traceevent*)malloc(sizeof(Traceevent) * Nevents);
  395. if(tevents == nil)
  396. error(Enomem);
  397. tpids = malloc(Ntracedpids * 20);
  398. if(tpids == nil){
  399. free(tpids);
  400. tpids = nil;
  401. error(Enomem);
  402. }
  403. tpidsc = tpids;
  404. tpidse = tpids + Ntracedpids * 20;
  405. *tpidsc = 0;
  406. tproduced = tconsumed = 0;
  407. }
  408. proctrace = _proctrace;
  409. poperror();
  410. unlock(&tlock);
  411. c->mode = openmode(omode);
  412. c->flag |= COPEN;
  413. c->offset = 0;
  414. return c;
  415. }
  416. if(QID(c->qid) == Qtracepids){
  417. if (omode != OREAD)
  418. error(Eperm);
  419. c->mode = openmode(omode);
  420. c->flag |= COPEN;
  421. c->offset = 0;
  422. return c;
  423. }
  424. if((p = psincref(SLOT(c->qid))) == nil)
  425. error(Eprocdied);
  426. qlock(&p->debug);
  427. if(waserror()){
  428. qunlock(&p->debug);
  429. psdecref(p);
  430. nexterror();
  431. }
  432. pid = PID(c->qid);
  433. if(p->pid != pid)
  434. error(Eprocdied);
  435. omode = openmode(omode);
  436. switch(QID(c->qid)){
  437. case Qtext:
  438. if(omode != OREAD)
  439. error(Eperm);
  440. tc = proctext(c, p);
  441. tc->offset = 0;
  442. poperror();
  443. qunlock(&p->debug);
  444. psdecref(p);
  445. cclose(c);
  446. return tc;
  447. case Qproc:
  448. case Qkregs:
  449. case Qsegment:
  450. case Qprofile:
  451. case Qfd:
  452. if(omode != OREAD)
  453. error(Eperm);
  454. break;
  455. case Qnote:
  456. if(p->privatemem)
  457. error(Eperm);
  458. break;
  459. case Qmem:
  460. case Qctl:
  461. if(p->privatemem)
  462. error(Eperm);
  463. nonone(p);
  464. break;
  465. case Qtls:
  466. if(p->pid != up->pid)
  467. error(Eperm);
  468. nonone(p);
  469. break;
  470. case Qargs:
  471. case Qnoteid:
  472. case Qstatus:
  473. case Qwait:
  474. case Qgdbregs:
  475. case Qregs:
  476. case Qfpregs:
  477. case Qsyscall:
  478. case Qcore:
  479. nonone(p);
  480. break;
  481. case Qpager:
  482. p->resp = qopen(1024, Qmsg, nil, 0);
  483. p->req = qopen(1024, Qmsg, nil, 0);
  484. print("p %d sets resp %p req %p\n", p->pid, p->resp, p->req);
  485. c->aux = p;
  486. break;
  487. case Qns:
  488. if(omode != OREAD)
  489. error(Eperm);
  490. c->aux = malloc(sizeof(Mntwalk));
  491. break;
  492. case Qnotepg:
  493. nonone(p);
  494. pg = p->pgrp;
  495. if(pg == nil)
  496. error(Eprocdied);
  497. if(omode!=OWRITE || pg->pgrpid == 1)
  498. error(Eperm);
  499. c->pgrpid.path = pg->pgrpid+1;
  500. c->pgrpid.vers = p->noteid;
  501. break;
  502. case Qstrace:
  503. if (!p->strace)
  504. error("Process does not have tracing enabled");
  505. incref(&p->strace->users);
  506. c->aux = p->strace;
  507. break;
  508. default:
  509. poperror();
  510. qunlock(&p->debug);
  511. psdecref(p);
  512. pprint("procopen %#llx\n", c->qid.path);
  513. error(Egreg);
  514. }
  515. /* Affix pid to qid */
  516. if(p->state != Dead)
  517. c->qid.vers = p->pid;
  518. /* make sure the process slot didn't get reallocated while we were playing */
  519. coherence();
  520. if(p->pid != pid)
  521. error(Eprocdied);
  522. tc = devopen(c, omode, 0, 0, procgen);
  523. poperror();
  524. qunlock(&p->debug);
  525. psdecref(p);
  526. return tc;
  527. }
  528. static int32_t
  529. procwstat(Chan *c, uint8_t *db, int32_t n)
  530. {
  531. Proc *up = externup();
  532. Proc *p;
  533. Dir *d;
  534. if(c->qid.type & QTDIR)
  535. error(Eperm);
  536. if(QID(c->qid) == Qtrace)
  537. return devwstat(c, db, n);
  538. if((p = psincref(SLOT(c->qid))) == nil)
  539. error(Eprocdied);
  540. nonone(p);
  541. d = nil;
  542. qlock(&p->debug);
  543. if(waserror()){
  544. qunlock(&p->debug);
  545. psdecref(p);
  546. free(d);
  547. nexterror();
  548. }
  549. if(p->pid != PID(c->qid))
  550. error(Eprocdied);
  551. if(strcmp(up->user, p->user) != 0 && strcmp(up->user, eve) != 0)
  552. error(Eperm);
  553. d = smalloc(sizeof(Dir)+n);
  554. n = convM2D(db, n, &d[0], (char*)&d[1]);
  555. if(n == 0)
  556. error(Eshortstat);
  557. if(!emptystr(d->uid) && strcmp(d->uid, p->user) != 0){
  558. if(strcmp(up->user, eve) != 0)
  559. error(Eperm);
  560. else
  561. kstrdup(&p->user, d->uid);
  562. }
  563. if(d->mode != (uint32_t)~0UL)
  564. p->procmode = d->mode&0777;
  565. poperror();
  566. qunlock(&p->debug);
  567. psdecref(p);
  568. free(d);
  569. return n;
  570. }
  571. static int32_t
  572. procoffset(int32_t offset, char *va, int *np)
  573. {
  574. if(offset > 0) {
  575. offset -= *np;
  576. if(offset < 0) {
  577. memmove(va, va+*np+offset, -offset);
  578. *np = -offset;
  579. }
  580. else
  581. *np = 0;
  582. }
  583. return offset;
  584. }
  585. static int
  586. procqidwidth(Chan *c)
  587. {
  588. char buf[32];
  589. return sprint(buf, "%lu", c->qid.vers);
  590. }
  591. int
  592. procfdprint(Chan *c, int fd, int w, char *s, int ns)
  593. {
  594. int n;
  595. if(w == 0)
  596. w = procqidwidth(c);
  597. n = snprint(s, ns, "%3d %.2s %C %4ud (%.16llx %*lu %.2x) %5ld %8lld %s\n",
  598. fd,
  599. &"r w rw"[(c->mode&3)<<1],
  600. c->dev->dc, c->devno,
  601. c->qid.path, w, c->qid.vers, c->qid.type,
  602. c->iounit, c->offset, c->path->s);
  603. return n;
  604. }
  605. static int
  606. procfds(Proc *p, char *va, int count, int32_t offset)
  607. {
  608. Proc *up = externup();
  609. Fgrp *f;
  610. Chan *c;
  611. char buf[256];
  612. int n, i, w, ww;
  613. char *a;
  614. /* print to buf to avoid holding fgrp lock while writing to user space */
  615. if(count > sizeof buf)
  616. count = sizeof buf;
  617. a = buf;
  618. qlock(&p->debug);
  619. f = p->fgrp;
  620. if(f == nil){
  621. qunlock(&p->debug);
  622. return 0;
  623. }
  624. lock(&f->r.l);
  625. if(waserror()){
  626. unlock(&f->r.l);
  627. qunlock(&p->debug);
  628. nexterror();
  629. }
  630. n = readstr(0, a, count, p->dot->path->s);
  631. n += snprint(a+n, count-n, "\n");
  632. offset = procoffset(offset, a, &n);
  633. /* compute width of qid.path */
  634. w = 0;
  635. for(i = 0; i <= f->maxfd; i++) {
  636. c = f->fd[i];
  637. if(c == nil)
  638. continue;
  639. ww = procqidwidth(c);
  640. if(ww > w)
  641. w = ww;
  642. }
  643. for(i = 0; i <= f->maxfd; i++) {
  644. c = f->fd[i];
  645. if(c == nil)
  646. continue;
  647. n += procfdprint(c, i, w, a+n, count-n);
  648. offset = procoffset(offset, a, &n);
  649. }
  650. poperror();
  651. unlock(&f->r.l);
  652. qunlock(&p->debug);
  653. /* copy result to user space, now that locks are released */
  654. memmove(va, buf, n);
  655. return n;
  656. }
  657. static void
  658. procclose(Chan * c)
  659. {
  660. if(QID(c->qid) == Qtrace){
  661. lock(&tlock);
  662. if(topens > 0)
  663. topens--;
  664. if(topens == 0)
  665. proctrace = notrace;
  666. unlock(&tlock);
  667. }
  668. if (QID(c->qid) == Qsyscall) {
  669. if (c->aux)
  670. qclose(c->aux);
  671. c->aux = nil;
  672. }
  673. if(QID(c->qid) == Qpager){
  674. print("leaking queueus for pager\n");
  675. }
  676. if(QID(c->qid) == Qns && c->aux != 0)
  677. free(c->aux);
  678. if (QID(c->qid) == Qstrace && c->aux != 0) {
  679. Strace *s = c->aux;
  680. // TODO: leak!
  681. print("are we leaking strace?\n");
  682. decref(&s->users);
  683. c->aux = nil;
  684. }
  685. }
  686. static void
  687. int2flag(int flag, char *s)
  688. {
  689. if(flag == 0){
  690. *s = '\0';
  691. return;
  692. }
  693. *s++ = '-';
  694. if(flag & MAFTER)
  695. *s++ = 'a';
  696. if(flag & MBEFORE)
  697. *s++ = 'b';
  698. if(flag & MCREATE)
  699. *s++ = 'c';
  700. if(flag & MCACHE)
  701. *s++ = 'C';
  702. *s = '\0';
  703. }
  704. static int
  705. procargs(Proc *p, char *buf, int nbuf)
  706. {
  707. int j, k, m;
  708. char *a;
  709. int n;
  710. a = p->args;
  711. if(p->setargs){
  712. snprint(buf, nbuf, "%s [%s]", p->text, p->args);
  713. return strlen(buf);
  714. }
  715. n = p->nargs;
  716. for(j = 0; j < nbuf - 1; j += m){
  717. if(n <= 0)
  718. break;
  719. if(j != 0)
  720. buf[j++] = ' ';
  721. m = snprint(buf+j, nbuf-j, "%q", a);
  722. k = strlen(a) + 1;
  723. a += k;
  724. n -= k;
  725. }
  726. return j;
  727. }
  728. static int
  729. eventsavailable(void *v)
  730. {
  731. return tproduced > tconsumed;
  732. }
  733. static int32_t
  734. procread(Chan *c, void *va, int32_t n, int64_t off)
  735. {
  736. Strace *strace;
  737. Proc *up = externup();
  738. Proc *p;
  739. Mach *ac, *wired;
  740. int32_t l, r;
  741. Waitq *wq;
  742. Ureg kur;
  743. uint8_t *rptr;
  744. Confmem *cm;
  745. Mntwalk *mw;
  746. Segment *sg, *s;
  747. int i, j, navail, pid, rsize, sno;
  748. char flag[10], *sps, *srv, *statbuf;
  749. uintptr_t offset, profoff, u;
  750. int tesz;
  751. uintptr_t gdbregs[DBG_MAX_REG_NUM];
  752. if(c->qid.type & QTDIR)
  753. return devdirread(c, va, n, 0, 0, procgen);
  754. offset = off;
  755. if(QID(c->qid) == Qtrace){
  756. if(!eventsavailable(nil))
  757. return 0;
  758. rptr = va;
  759. tesz = BIT32SZ + BIT32SZ + BIT64SZ + BIT32SZ;
  760. navail = tproduced - tconsumed;
  761. if(navail > n / tesz)
  762. navail = n / tesz;
  763. while(navail > 0) {
  764. PBIT32(rptr, tevents[tconsumed & Emask].pid);
  765. rptr += BIT32SZ;
  766. PBIT32(rptr, tevents[tconsumed & Emask].etype);
  767. rptr += BIT32SZ;
  768. PBIT64(rptr, tevents[tconsumed & Emask].time);
  769. rptr += BIT64SZ;
  770. PBIT32(rptr, tevents[tconsumed & Emask].core);
  771. rptr += BIT32SZ;
  772. tconsumed++;
  773. navail--;
  774. }
  775. return rptr - (uint8_t*)va;
  776. }
  777. if(QID(c->qid) == Qtracepids)
  778. if(tpids == nil)
  779. return 0;
  780. else
  781. return readstr(off, va, n, tpids);
  782. if((p = psincref(SLOT(c->qid))) == nil || p->pid != PID(c->qid))
  783. error(Eprocdied);
  784. switch(QID(c->qid)){
  785. default:
  786. psdecref(p);
  787. break;
  788. case Qargs:
  789. qlock(&p->debug);
  790. j = procargs(p, up->genbuf, sizeof up->genbuf);
  791. qunlock(&p->debug);
  792. psdecref(p);
  793. if(offset >= j)
  794. return 0;
  795. if(offset+n > j)
  796. n = j-offset;
  797. memmove(va, &up->genbuf[offset], n);
  798. return n;
  799. case Qsyscall:
  800. if(p->syscalltrace == nil)
  801. return 0;
  802. return readstr(offset, va, n, p->syscalltrace);
  803. case Qcore:
  804. i = 0;
  805. ac = p->ac;
  806. wired = p->wired;
  807. if(ac != nil)
  808. i = ac->machno;
  809. else if(wired != nil)
  810. i = wired->machno;
  811. statbuf = smalloc(STATSIZE);
  812. snprint(statbuf, STATSIZE, "%d\n", i);
  813. n = readstr(offset, va, n, statbuf);
  814. free(statbuf);
  815. return n;
  816. case Qmem:
  817. if(offset < KZERO || (offset >= USTKTOP-USTKSIZE && offset < USTKTOP)){
  818. r = procctlmemio(p, offset, n, va, 1);
  819. psdecref(p);
  820. return r;
  821. }
  822. if(!iseve()){
  823. psdecref(p);
  824. error(Eperm);
  825. }
  826. /* validate kernel addresses */
  827. if(offset < PTR2UINT(end)) {
  828. if(offset+n > PTR2UINT(end))
  829. n = PTR2UINT(end) - offset;
  830. memmove(va, UINT2PTR(offset), n);
  831. psdecref(p);
  832. return n;
  833. }
  834. for(i=0; i<nelem(conf.mem); i++){
  835. cm = &conf.mem[i];
  836. /* klimit-1 because klimit might be zero! */
  837. if(cm->kbase <= offset && offset <= cm->klimit-1){
  838. if(offset+n >= cm->klimit-1)
  839. n = cm->klimit - offset;
  840. memmove(va, UINT2PTR(offset), n);
  841. psdecref(p);
  842. return n;
  843. }
  844. }
  845. psdecref(p);
  846. error(Ebadarg);
  847. case Qprofile:
  848. profoff = 0;
  849. for(sno = 0; sno < NSEG; sno++){
  850. if(p->seg[sno] == nil)
  851. continue;
  852. if((p->seg[sno]->type & SG_EXEC) == 0)
  853. continue;
  854. if(p->seg[sno]->profile == nil)
  855. continue;
  856. s = p->seg[sno];
  857. i = ((s->top-s->base)>>LRESPROF) * sizeof s->profile[0];
  858. if(offset >= profoff+i){
  859. profoff += i;
  860. continue;
  861. }
  862. if(offset+n > profoff+i)
  863. n = profoff+i - offset;
  864. memmove(va, ((char*)s->profile)+(offset-profoff), n);
  865. psdecref(p);
  866. return n;
  867. }
  868. psdecref(p);
  869. if(sno == NSEG)
  870. error("profile is off");
  871. return 0;
  872. case Qnote:
  873. qlock(&p->debug);
  874. if(waserror()){
  875. qunlock(&p->debug);
  876. psdecref(p);
  877. nexterror();
  878. }
  879. if(p->pid != PID(c->qid))
  880. error(Eprocdied);
  881. if(n < 1) /* must accept at least the '\0' */
  882. error(Etoosmall);
  883. if(p->nnote == 0)
  884. n = 0;
  885. else {
  886. i = strlen(p->note[0].msg) + 1;
  887. if(i > n)
  888. i = n;
  889. rptr = va;
  890. memmove(rptr, p->note[0].msg, i);
  891. rptr[i-1] = '\0';
  892. p->nnote--;
  893. memmove(p->note, p->note+1, p->nnote*sizeof(Note));
  894. n = i;
  895. }
  896. if(p->nnote == 0)
  897. p->notepending = 0;
  898. poperror();
  899. qunlock(&p->debug);
  900. psdecref(p);
  901. return n;
  902. case Qproc:
  903. if(offset >= sizeof(Proc)){
  904. psdecref(p);
  905. return 0;
  906. }
  907. if(offset+n > sizeof(Proc))
  908. n = sizeof(Proc) - offset;
  909. memmove(va, ((char*)p)+offset, n);
  910. psdecref(p);
  911. return n;
  912. case Qregs:
  913. rptr = (uint8_t*)p->dbgreg;
  914. rsize = sizeof(Ureg);
  915. regread:
  916. if(rptr == 0){
  917. psdecref(p);
  918. error(Enoreg);
  919. }
  920. if(offset >= rsize){
  921. psdecref(p);
  922. return 0;
  923. }
  924. if(offset+n > rsize)
  925. n = rsize - offset;
  926. memmove(va, rptr+offset, n);
  927. psdecref(p);
  928. return n;
  929. /* Sorry about the code duplication. TODO: clean this up? */
  930. case Qgdbregs:
  931. rptr = (uint8_t*)&gdbregs[0];
  932. // not sizeof; it's an odd number of 32-bit words ... yuck.
  933. rsize = GDB_NUMREGBYTES;
  934. if(rptr == 0){
  935. psdecref(p);
  936. error(Enoreg);
  937. }
  938. if(offset >= rsize){
  939. psdecref(p);
  940. return 0;
  941. }
  942. if(offset+n > rsize)
  943. n = rsize - offset;
  944. ureg2gdb(p->dbgreg, gdbregs);
  945. print("Qgdbregs: va %p, rptr +offset %p, n %d\n", va, rptr+offset, n);
  946. memmove(va, rptr+offset, n);
  947. psdecref(p);
  948. return n;
  949. case Qkregs:
  950. memset(&kur, 0, sizeof(Ureg));
  951. setkernur(&kur, p);
  952. rptr = (uint8_t*)&kur;
  953. rsize = sizeof(Ureg);
  954. goto regread;
  955. case Qfpregs:
  956. r = fpudevprocio(p, va, n, offset, 0);
  957. psdecref(p);
  958. return r;
  959. case Qstrace:
  960. strace = c->aux;
  961. n = qread(strace->q, va, n);
  962. return n;
  963. case Qstatus:
  964. if(offset >= STATSIZE){
  965. psdecref(p);
  966. return 0;
  967. }
  968. if(offset+n > STATSIZE)
  969. n = STATSIZE - offset;
  970. sps = p->psstate;
  971. if(sps == 0)
  972. sps = statename[p->state];
  973. statbuf = smalloc(STATSIZE);
  974. memset(statbuf, ' ', STATSIZE);
  975. sprint(statbuf, "%-*.*s%-*.*s%-12.11s",
  976. KNAMELEN, KNAMELEN-1, p->text,
  977. KNAMELEN, KNAMELEN-1, p->user,
  978. sps);
  979. j = 2*KNAMELEN + 12;
  980. for(i = 0; i < 6; i++) {
  981. l = p->time[i];
  982. if(i == TReal)
  983. l = sys->ticks - l;
  984. l = TK2MS(l);
  985. readnum(0, statbuf+j+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
  986. }
  987. /* ignore stacks, which are typically not faulted in */
  988. u = 0;
  989. for(i=0; i<NSEG; i++){
  990. s = p->seg[i];
  991. if(s != nil && (s->type&SG_TYPE) != SG_STACK)
  992. u += s->top - s->base;
  993. }
  994. readnum(0, statbuf+j+NUMSIZE*6, NUMSIZE, u>>10u, NUMSIZE); /* wrong size */
  995. readnum(0, statbuf+j+NUMSIZE*7, NUMSIZE, p->basepri, NUMSIZE);
  996. readnum(0, statbuf+j+NUMSIZE*8, NUMSIZE, p->priority, NUMSIZE);
  997. /*
  998. * NIX: added # of traps, syscalls, and iccs
  999. */
  1000. readnum(0, statbuf+j+NUMSIZE*9, NUMSIZE, p->ntrap, NUMSIZE);
  1001. readnum(0, statbuf+j+NUMSIZE*10, NUMSIZE, p->nintr, NUMSIZE);
  1002. readnum(0, statbuf+j+NUMSIZE*11, NUMSIZE, p->nsyscall, NUMSIZE);
  1003. readnum(0, statbuf+j+NUMSIZE*12, NUMSIZE, p->nicc, NUMSIZE);
  1004. readnum(0, statbuf+j+NUMSIZE*13, NUMSIZE, p->nactrap, NUMSIZE);
  1005. readnum(0, statbuf+j+NUMSIZE*14, NUMSIZE, p->nacsyscall, NUMSIZE);
  1006. /*
  1007. * external pager support, random stuff.
  1008. */
  1009. if (0) print("qstatus p %p pid %d req %p\n", p, p->pid, p->req);
  1010. readnum(0,statbuf+j+NUMSIZE*15, NUMSIZE, p->req ? 1 : 0, NUMSIZE);
  1011. readnum(0,statbuf+j+NUMSIZE*16, NUMSIZE, p->resp ? 1 : 0, NUMSIZE);
  1012. statbuf[j+NUMSIZE*17] = '\n';
  1013. if(offset+n > j+NUMSIZE*17+1)
  1014. n = j+NUMSIZE*17+1-offset;
  1015. /*
  1016. if (p->strace)
  1017. s = seprintf(s, e, " %d trace users %d traced procs",
  1018. kref_refcnt(&p->strace->users),
  1019. kref_refcnt(&p->strace->procs));
  1020. */
  1021. memmove(va, statbuf+offset, n);
  1022. free(statbuf);
  1023. psdecref(p);
  1024. return n;
  1025. case Qsegment:
  1026. j = 0;
  1027. statbuf = smalloc(STATSIZE);
  1028. for(i = 0; i < NSEG; i++) {
  1029. sg = p->seg[i];
  1030. if(sg == 0)
  1031. continue;
  1032. j += sprint(statbuf+j, "%-6s %c%c%c %c %p %p %4d\n",
  1033. segtypes[sg->type&SG_TYPE],
  1034. (sg->type&SG_READ) != 0 ? 'r' : '-',
  1035. (sg->type&SG_WRITE) != 0 ? 'w' : '-',
  1036. (sg->type&SG_EXEC) != 0 ? 'x' : '-',
  1037. sg->profile ? 'P' : ' ',
  1038. sg->base, sg->top, sg->r.ref);
  1039. }
  1040. psdecref(p);
  1041. if(offset >= j){
  1042. free(statbuf);
  1043. return 0;
  1044. }
  1045. if(offset+n > j)
  1046. n = j-offset;
  1047. if(n == 0 && offset == 0){
  1048. free(statbuf);
  1049. exhausted("segments");
  1050. }
  1051. memmove(va, statbuf+offset, n);
  1052. free(statbuf);
  1053. return n;
  1054. case Qwait:
  1055. if(!canqlock(&p->qwaitr)){
  1056. psdecref(p);
  1057. error(Einuse);
  1058. }
  1059. if(waserror()) {
  1060. qunlock(&p->qwaitr);
  1061. psdecref(p);
  1062. nexterror();
  1063. }
  1064. lock(&p->exl);
  1065. if(up == p && p->nchild == 0 && p->waitq == 0) {
  1066. unlock(&p->exl);
  1067. error(Enochild);
  1068. }
  1069. pid = p->pid;
  1070. while(p->waitq == 0) {
  1071. unlock(&p->exl);
  1072. sleep(&p->waitr, haswaitq, p);
  1073. if(p->pid != pid)
  1074. error(Eprocdied);
  1075. lock(&p->exl);
  1076. }
  1077. wq = p->waitq;
  1078. p->waitq = wq->next;
  1079. p->nwait--;
  1080. unlock(&p->exl);
  1081. poperror();
  1082. qunlock(&p->qwaitr);
  1083. psdecref(p);
  1084. n = snprint(va, n, "%d %lu %lu %lu %q",
  1085. wq->w.pid,
  1086. wq->w.time[TUser], wq->w.time[TSys], wq->w.time[TReal],
  1087. wq->w.msg);
  1088. free(wq);
  1089. return n;
  1090. case Qns:
  1091. qlock(&p->debug);
  1092. if(waserror()){
  1093. qunlock(&p->debug);
  1094. psdecref(p);
  1095. nexterror();
  1096. }
  1097. if(p->pgrp == nil || p->pid != PID(c->qid))
  1098. error(Eprocdied);
  1099. mw = c->aux;
  1100. if(mw->cddone){
  1101. poperror();
  1102. qunlock(&p->debug);
  1103. psdecref(p);
  1104. return 0;
  1105. }
  1106. mntscan(mw, p);
  1107. if(mw->mh == 0){
  1108. mw->cddone = 1;
  1109. i = snprint(va, n, "cd %s\n", p->dot->path->s);
  1110. poperror();
  1111. qunlock(&p->debug);
  1112. psdecref(p);
  1113. return i;
  1114. }
  1115. int2flag(mw->cm->mflag, flag);
  1116. if(strcmp(mw->cm->to->path->s, "#M") == 0){
  1117. srv = srvname(mw->cm->to->mchan);
  1118. i = snprint(va, n, "mount %s %s %s %s\n", flag,
  1119. srv==nil? mw->cm->to->mchan->path->s : srv,
  1120. mw->mh->from->path->s, mw->cm->spec? mw->cm->spec : "");
  1121. free(srv);
  1122. }else
  1123. i = snprint(va, n, "bind %s %s %s\n", flag,
  1124. mw->cm->to->path->s, mw->mh->from->path->s);
  1125. poperror();
  1126. qunlock(&p->debug);
  1127. psdecref(p);
  1128. return i;
  1129. case Qnoteid:
  1130. r = readnum(offset, va, n, p->noteid, NUMSIZE);
  1131. psdecref(p);
  1132. return r;
  1133. case Qfd:
  1134. r = procfds(p, va, n, offset);
  1135. psdecref(p);
  1136. return r;
  1137. case Qtls:
  1138. statbuf = smalloc(STATSIZE);
  1139. j = snprint(statbuf, STATSIZE, "tls 0x%p\n", p->tls);
  1140. psdecref(p);
  1141. if(offset >= j){
  1142. free(statbuf);
  1143. return 0;
  1144. }
  1145. if(offset+n > j)
  1146. n = j-offset;
  1147. memmove(va, statbuf+offset, n);
  1148. free(statbuf);
  1149. return n;
  1150. case Qpager:
  1151. p = c->aux;
  1152. n = qread(p->req, va, n);
  1153. print("read pager: %p\n", n);
  1154. break;
  1155. }
  1156. error(Egreg);
  1157. return 0; /* not reached */
  1158. }
  1159. static void
  1160. mntscan(Mntwalk *mw, Proc *p)
  1161. {
  1162. Pgrp *pg;
  1163. Mount *t;
  1164. Mhead *f;
  1165. int best, i, last, nxt;
  1166. pg = p->pgrp;
  1167. rlock(&pg->ns);
  1168. nxt = 0;
  1169. best = (int)(~0U>>1); /* largest 2's complement int */
  1170. last = 0;
  1171. if(mw->mh)
  1172. last = mw->cm->mountid;
  1173. for(i = 0; i < MNTHASH; i++) {
  1174. for(f = pg->mnthash[i]; f; f = f->hash) {
  1175. for(t = f->mount; t; t = t->next) {
  1176. if(mw->mh == 0 ||
  1177. (t->mountid > last && t->mountid < best)) {
  1178. mw->cm = t;
  1179. mw->mh = f;
  1180. best = mw->cm->mountid;
  1181. nxt = 1;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. if(nxt == 0)
  1187. mw->mh = 0;
  1188. runlock(&pg->ns);
  1189. }
  1190. static int32_t
  1191. procwrite(Chan *c, void *va, int32_t n, int64_t off)
  1192. {
  1193. Proc *up = externup();
  1194. Proc *p, *t;
  1195. int i, id, l;
  1196. char *args, buf[ERRMAX];
  1197. uintptr_t offset;
  1198. if(c->qid.type & QTDIR)
  1199. error(Eisdir);
  1200. /* Use the remembered noteid in the channel rather
  1201. * than the process pgrpid
  1202. */
  1203. if(QID(c->qid) == Qnotepg) {
  1204. pgrpnote(NOTEID(c->pgrpid), va, n, NUser);
  1205. return n;
  1206. }
  1207. if((p = psincref(SLOT(c->qid))) == nil)
  1208. error(Eprocdied);
  1209. qlock(&p->debug);
  1210. if(waserror()){
  1211. qunlock(&p->debug);
  1212. psdecref(p);
  1213. nexterror();
  1214. }
  1215. if(p->pid != PID(c->qid))
  1216. error(Eprocdied);
  1217. offset = off;
  1218. switch(QID(c->qid)){
  1219. case Qargs:
  1220. if(n == 0)
  1221. error(Eshort);
  1222. if(n >= ERRMAX)
  1223. error(Etoobig);
  1224. memmove(buf, va, n);
  1225. args = malloc(n+1);
  1226. if(args == nil)
  1227. error(Enomem);
  1228. memmove(args, buf, n);
  1229. l = n;
  1230. if(args[l-1] != 0)
  1231. args[l++] = 0;
  1232. free(p->args);
  1233. p->nargs = l;
  1234. p->args = args;
  1235. p->setargs = 1;
  1236. break;
  1237. case Qmem:
  1238. if(p->state != Stopped)
  1239. error(Ebadctl);
  1240. n = procctlmemio(p, offset, n, va, 0);
  1241. break;
  1242. case Qregs:
  1243. if(offset >= sizeof(Ureg))
  1244. n = 0;
  1245. else if(offset+n > sizeof(Ureg))
  1246. n = sizeof(Ureg) - offset;
  1247. if(p->dbgreg == 0)
  1248. error(Enoreg);
  1249. setregisters(p->dbgreg, (char*)(p->dbgreg)+offset, va, n);
  1250. break;
  1251. case Qfpregs:
  1252. n = fpudevprocio(p, va, n, offset, 1);
  1253. break;
  1254. case Qctl:
  1255. procctlreq(p, va, n);
  1256. break;
  1257. case Qnote:
  1258. if(p->kp)
  1259. error(Eperm);
  1260. if(n >= ERRMAX-1)
  1261. error(Etoobig);
  1262. memmove(buf, va, n);
  1263. buf[n] = 0;
  1264. if(!postnote(p, 0, buf, NUser))
  1265. error("note not posted");
  1266. break;
  1267. case Qnoteid:
  1268. id = atoi(va);
  1269. if(id == p->pid) {
  1270. p->noteid = id;
  1271. break;
  1272. }
  1273. for(i = 0; (t = psincref(i)) != nil; i++){
  1274. if(t->state == Dead || t->noteid != id){
  1275. psdecref(t);
  1276. continue;
  1277. }
  1278. if(strcmp(p->user, t->user) != 0){
  1279. psdecref(t);
  1280. error(Eperm);
  1281. }
  1282. psdecref(t);
  1283. p->noteid = id;
  1284. break;
  1285. }
  1286. if(p->noteid != id)
  1287. error(Ebadarg);
  1288. break;
  1289. case Qtls:
  1290. if(n >= sizeof buf)
  1291. error(Etoobig);
  1292. memmove(buf, va, n);
  1293. buf[n] = '\0';
  1294. if(memcmp(buf, "tls ", 4) == 0){
  1295. char *s;
  1296. for(s = buf; *s != '\0' && (*s < '0' || *s > '9'); s++)
  1297. ;
  1298. if(*s >= '0' && *s <= '9'){
  1299. p->tls = (uintptr_t)strtoull(s, nil, 0); // a-tol-whex! a-tol-whex!
  1300. poperror();
  1301. qunlock(&p->debug);
  1302. psdecref(p);
  1303. return n;
  1304. }
  1305. }
  1306. error(Ebadarg);
  1307. case Qpager:
  1308. p = c->aux;
  1309. if (p && p->resp)
  1310. n = qwrite(p->resp, va, n);
  1311. break;
  1312. /* this lets your write a marker into the data stream,
  1313. * which is a very powerful tool. */
  1314. case Qstrace:
  1315. assert(c->aux);
  1316. /* it is possible that the q hungup and is closed. that would be
  1317. * the case if all of the procs closed and decref'd. if the q is
  1318. * closed, qwrite() will throw an error. */
  1319. n = qwrite(((Strace*)c->aux)->q, va, n);
  1320. break;
  1321. default:
  1322. poperror();
  1323. qunlock(&p->debug);
  1324. psdecref(p);
  1325. pprint("unknown qid %#llx in procwrite\n", c->qid.path);
  1326. error(Egreg);
  1327. }
  1328. poperror();
  1329. qunlock(&p->debug);
  1330. psdecref(p);
  1331. return n;
  1332. }
  1333. Dev procdevtab = {
  1334. .dc = 'p',
  1335. .name = "proc",
  1336. .reset = devreset,
  1337. .init = procinit,
  1338. .shutdown = devshutdown,
  1339. .attach = procattach,
  1340. .walk = procwalk,
  1341. .stat = procstat,
  1342. .open = procopen,
  1343. .create = devcreate,
  1344. .close = procclose,
  1345. .read = procread,
  1346. .bread = devbread,
  1347. .write = procwrite,
  1348. .bwrite = devbwrite,
  1349. .remove = devremove,
  1350. .wstat = procwstat,
  1351. };
  1352. static Chan*
  1353. proctext(Chan *c, Proc *p)
  1354. {
  1355. Proc *up = externup();
  1356. Chan *tc;
  1357. Image *i;
  1358. Segment *s;
  1359. int sno;
  1360. for(sno = 0; sno < NSEG; sno++)
  1361. if(p->seg[sno] != nil)
  1362. if((p->seg[sno]->type & SG_EXEC) != 0)
  1363. break;
  1364. if(sno == NSEG)
  1365. error(Enonexist);
  1366. s = p->seg[sno];
  1367. if(p->state==Dead)
  1368. error(Eprocdied);
  1369. lock(&s->r.l);
  1370. i = s->image;
  1371. if(i == 0) {
  1372. unlock(&s->r.l);
  1373. error(Eprocdied);
  1374. }
  1375. unlock(&s->r.l);
  1376. lock(&i->r.l);
  1377. if(waserror()) {
  1378. unlock(&i->r.l);
  1379. nexterror();
  1380. }
  1381. tc = i->c;
  1382. if(tc == 0)
  1383. error(Eprocdied);
  1384. if(incref(&tc->r) == 1 || (tc->flag&COPEN) == 0 || tc->mode!=OREAD) {
  1385. cclose(tc);
  1386. error(Eprocdied);
  1387. }
  1388. if(p->pid != PID(c->qid)){
  1389. cclose(tc);
  1390. error(Eprocdied);
  1391. }
  1392. poperror();
  1393. unlock(&i->r.l);
  1394. return tc;
  1395. }
  1396. void
  1397. procstopwait(Proc *p, int ctl)
  1398. {
  1399. Proc *up = externup();
  1400. int pid;
  1401. if(p->pdbg)
  1402. error(Einuse);
  1403. if(procstopped(p) || p->state == Broken)
  1404. return;
  1405. if(ctl != 0)
  1406. p->procctl = ctl;
  1407. p->pdbg = up;
  1408. pid = p->pid;
  1409. qunlock(&p->debug);
  1410. up->psstate = "Stopwait";
  1411. if(waserror()) {
  1412. p->pdbg = 0;
  1413. qlock(&p->debug);
  1414. nexterror();
  1415. }
  1416. sleep(&up->sleep, procstopped, p);
  1417. poperror();
  1418. qlock(&p->debug);
  1419. if(p->pid != pid)
  1420. error(Eprocdied);
  1421. }
  1422. static void
  1423. procctlcloseone(Proc *p, Fgrp *f, int fd)
  1424. {
  1425. Chan *c;
  1426. c = f->fd[fd];
  1427. if(c == nil)
  1428. return;
  1429. f->fd[fd] = nil;
  1430. unlock(&f->r.l);
  1431. qunlock(&p->debug);
  1432. cclose(c);
  1433. qlock(&p->debug);
  1434. lock(&f->r.l);
  1435. }
  1436. void
  1437. procctlclosefiles(Proc *p, int all, int fd)
  1438. {
  1439. int i;
  1440. Fgrp *f;
  1441. f = p->fgrp;
  1442. if(f == nil)
  1443. error(Eprocdied);
  1444. lock(&f->r.l);
  1445. f->r.ref++;
  1446. if(all)
  1447. for(i = 0; i < f->maxfd; i++)
  1448. procctlcloseone(p, f, i);
  1449. else
  1450. procctlcloseone(p, f, fd);
  1451. unlock(&f->r.l);
  1452. closefgrp(f);
  1453. }
  1454. static char *
  1455. parsetime(int64_t *rt, char *s)
  1456. {
  1457. uint64_t ticks;
  1458. uint32_t l;
  1459. char *e, *p;
  1460. static int p10[] = {100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
  1461. if (s == nil)
  1462. return("missing value");
  1463. ticks=strtoul(s, &e, 10);
  1464. if (*e == '.'){
  1465. p = e+1;
  1466. l = strtoul(p, &e, 10);
  1467. if(e-p > nelem(p10))
  1468. return "too many digits after decimal point";
  1469. if(e-p == 0)
  1470. return "ill-formed number";
  1471. l *= p10[e-p-1];
  1472. }else
  1473. l = 0;
  1474. if (*e == '\0' || strcmp(e, "s") == 0){
  1475. ticks = 1000000000 * ticks + l;
  1476. }else if (strcmp(e, "ms") == 0){
  1477. ticks = 1000000 * ticks + l/1000;
  1478. }else if (strcmp(e, "µs") == 0 || strcmp(e, "us") == 0){
  1479. ticks = 1000 * ticks + l/1000000;
  1480. }else if (strcmp(e, "ns") != 0)
  1481. return "unrecognized unit";
  1482. *rt = ticks;
  1483. return nil;
  1484. }
  1485. #if 0
  1486. static void strace_shutdown(Strace *strace)
  1487. {
  1488. qhangup(strace->q, "No more traces");
  1489. }
  1490. static void strace_release(Strace *strace)
  1491. {
  1492. qfree(strace->q);
  1493. free(strace);
  1494. }
  1495. #endif
  1496. static void
  1497. procctlreq(Proc *p, char *va, int n)
  1498. {
  1499. Strace *strace;
  1500. Proc *up = externup();
  1501. Segment *s;
  1502. int npc, pri, core, sno;
  1503. Cmdbuf *cb;
  1504. Cmdtab *ct;
  1505. int64_t time;
  1506. char *e;
  1507. if(p->kp) /* no ctl requests to kprocs */
  1508. error(Eperm);
  1509. cb = parsecmd(va, n);
  1510. if(waserror()){
  1511. free(cb);
  1512. nexterror();
  1513. }
  1514. ct = lookupcmd(cb, proccmd, nelem(proccmd));
  1515. switch(ct->index){
  1516. case CMstraceall:
  1517. case CMstraceme:
  1518. /* common allocation. if we inherited, we might have one already */
  1519. if (!p->strace) {
  1520. print("Set up tracing for pid %d\n", p->pid);
  1521. strace = mallocz(sizeof(*p->strace), 1);
  1522. strace->q = qopen(65536, Qmsg|Qcoalesce, 0, 0);
  1523. /* both of these refs are put when the proc is freed. procs is for
  1524. * every process that has this p->strace. users is procs + every
  1525. * user (e.g. from open()).
  1526. */
  1527. incref(&strace->procs);
  1528. incref(&strace->users);
  1529. /* Do we need to worry about this? Or does the kernel protect it.
  1530. * I suspect that DMEXCL might help here.
  1531. * I.e. what if two procs do this command? */
  1532. p->strace = strace;
  1533. }
  1534. break;
  1535. }
  1536. switch(ct->index){
  1537. case CMclose:
  1538. procctlclosefiles(p, 0, atoi(cb->f[1]));
  1539. break;
  1540. case CMclosefiles:
  1541. procctlclosefiles(p, 1, 0);
  1542. break;
  1543. case CMhang:
  1544. p->hang = 1;
  1545. break;
  1546. case CMkill:
  1547. switch(p->state) {
  1548. case Broken:
  1549. unbreak(p);
  1550. break;
  1551. case Stopped:
  1552. case Semdown:
  1553. p->procctl = Proc_exitme;
  1554. postnote(p, 0, "sys: killed", NExit);
  1555. ready(p);
  1556. break;
  1557. default:
  1558. p->procctl = Proc_exitme;
  1559. postnote(p, 0, "sys: killed", NExit);
  1560. }
  1561. break;
  1562. case CMnohang:
  1563. p->hang = 0;
  1564. break;
  1565. case CMnoswap:
  1566. p->noswap = 1;
  1567. break;
  1568. case CMpri:
  1569. pri = atoi(cb->f[1]);
  1570. if(pri > PriNormal && !iseve())
  1571. error(Eperm);
  1572. procpriority(p, pri, 0);
  1573. break;
  1574. case CMfixedpri:
  1575. pri = atoi(cb->f[1]);
  1576. if(pri > PriNormal && !iseve())
  1577. error(Eperm);
  1578. procpriority(p, pri, 1);
  1579. break;
  1580. case CMprivate:
  1581. p->privatemem = 1;
  1582. break;
  1583. case CMprofile:
  1584. for(sno = 0; sno < NSEG; sno++){
  1585. if(p->seg[sno] != nil && (p->seg[sno]->type & SG_EXEC) != 0){
  1586. s = p->seg[sno];
  1587. if(s->profile != 0)
  1588. free(s->profile);
  1589. npc = (s->top-s->base)>>LRESPROF;
  1590. s->profile = malloc(npc * sizeof s->profile[0]);
  1591. if(s->profile == 0)
  1592. error(Enomem);
  1593. }
  1594. }
  1595. break;
  1596. case CMstart:
  1597. if(p->state != Stopped)
  1598. error(Ebadctl);
  1599. ready(p);
  1600. break;
  1601. case CMstartstop:
  1602. if(p->state != Stopped)
  1603. error(Ebadctl);
  1604. p->procctl = Proc_traceme;
  1605. ready(p);
  1606. procstopwait(p, Proc_traceme);
  1607. break;
  1608. case CMstartsyscall:
  1609. if(p->state != Stopped)
  1610. error(Ebadctl);
  1611. p->procctl = Proc_tracesyscall;
  1612. ready(p);
  1613. procstopwait(p, Proc_tracesyscall);
  1614. break;
  1615. case CMstop:
  1616. procstopwait(p, Proc_stopme);
  1617. break;
  1618. case CMwaitstop:
  1619. procstopwait(p, 0);
  1620. break;
  1621. case CMwired:
  1622. core = atoi(cb->f[1]);
  1623. procwired(p, core);
  1624. sched();
  1625. break;
  1626. case CMtrace:
  1627. switch(cb->nf){
  1628. case 1:
  1629. p->trace ^= 1;
  1630. break;
  1631. case 2:
  1632. p->trace = (atoi(cb->f[1]) != 0);
  1633. break;
  1634. default:
  1635. error("args");
  1636. }
  1637. break;
  1638. /* real time */
  1639. case CMperiod:
  1640. if(p->edf == nil)
  1641. edfinit(p);
  1642. if(e=parsetime(&time, cb->f[1])) /* time in ns */
  1643. error(e);
  1644. edfstop(p);
  1645. p->edf->T = time/1000; /* Edf times are in µs */
  1646. break;
  1647. case CMdeadline:
  1648. if(p->edf == nil)
  1649. edfinit(p);
  1650. if(e=parsetime(&time, cb->f[1]))
  1651. error(e);
  1652. edfstop(p);
  1653. p->edf->D = time/1000;
  1654. break;
  1655. case CMcost:
  1656. if(p->edf == nil)
  1657. edfinit(p);
  1658. if(e=parsetime(&time, cb->f[1]))
  1659. error(e);
  1660. edfstop(p);
  1661. p->edf->C = time/1000;
  1662. break;
  1663. case CMsporadic:
  1664. if(p->edf == nil)
  1665. edfinit(p);
  1666. p->edf->flags |= Sporadic;
  1667. break;
  1668. case CMdeadlinenotes:
  1669. if(p->edf == nil)
  1670. edfinit(p);
  1671. p->edf->flags |= Sendnotes;
  1672. break;
  1673. case CMadmit:
  1674. if(p->edf == 0)
  1675. error("edf params");
  1676. if(e = edfadmit(p))
  1677. error(e);
  1678. break;
  1679. case CMextra:
  1680. if(p->edf == nil)
  1681. edfinit(p);
  1682. p->edf->flags |= Extratime;
  1683. break;
  1684. case CMexpel:
  1685. if(p->edf)
  1686. edfstop(p);
  1687. break;
  1688. case CMevent:
  1689. if(up->trace)
  1690. proctrace(up, SUser, 0);
  1691. break;
  1692. case CMcore:
  1693. core = atoi(cb->f[1]);
  1694. if(core >= MACHMAX)
  1695. error("wrong core number");
  1696. else if(core == 0){
  1697. if(p->ac == nil)
  1698. error("not running in an ac");
  1699. p->procctl = Proc_totc;
  1700. if(p != up && p->state == Exotic){
  1701. /* see the comment in postnote */
  1702. intrac(p);
  1703. }
  1704. }else{
  1705. if(p->ac != nil)
  1706. error("running in an ac");
  1707. if(core < 0)
  1708. p->ac = getac(p, -1);
  1709. else
  1710. p->ac = getac(p, core);
  1711. p->procctl = Proc_toac;
  1712. p->prepagemem = 1;
  1713. }
  1714. break;
  1715. case CMstraceme:
  1716. p->strace_on = 1;
  1717. p->strace_inherit = 0;
  1718. break;
  1719. case CMstraceall:
  1720. p->strace_on = 1;
  1721. p->strace_inherit = 1;
  1722. break;
  1723. case CMstraceoff:
  1724. p->strace_on = 0;
  1725. p->strace_inherit = 0;
  1726. break;
  1727. }
  1728. poperror();
  1729. free(cb);
  1730. }
  1731. static int
  1732. procstopped(void *a)
  1733. {
  1734. Proc *p = a;
  1735. return p->state == Stopped;
  1736. }
  1737. static int
  1738. procctlmemio(Proc *p, uintptr_t offset, int n, void *va, int read)
  1739. {
  1740. Proc *up = externup();
  1741. KMap *k;
  1742. Pte *pte;
  1743. Page *pg;
  1744. Segment *s;
  1745. uintptr_t soff, l; /* hmmmm */
  1746. uint8_t *b;
  1747. uintmem pgsz;
  1748. for(;;) {
  1749. s = seg(p, offset, 1);
  1750. if(s == 0)
  1751. error(Ebadarg);
  1752. if(offset+n >= s->top)
  1753. n = s->top-offset;
  1754. if(!read && ((s->type&SG_TYPE) == SG_TEXT || (s->type&SG_TYPE) == SG_LOAD))
  1755. s = txt2data(p, s);
  1756. s->steal++;
  1757. soff = offset-s->base;
  1758. if(waserror()) {
  1759. s->steal--;
  1760. nexterror();
  1761. }
  1762. if(fixfault(s, offset, read, 0, s->color) == 0)
  1763. break;
  1764. poperror();
  1765. s->steal--;
  1766. }
  1767. poperror();
  1768. pte = s->map[soff/PTEMAPMEM];
  1769. if(pte == 0)
  1770. panic("procctlmemio");
  1771. pgsz = sys->pgsz[s->pgszi];
  1772. pg = pte->pages[(soff&(PTEMAPMEM-1))/pgsz];
  1773. if(pagedout(pg))
  1774. panic("procctlmemio1");
  1775. l = pgsz - (offset&(pgsz-1));
  1776. if(n > l)
  1777. n = l;
  1778. k = kmap(pg);
  1779. if(waserror()) {
  1780. s->steal--;
  1781. kunmap(k);
  1782. nexterror();
  1783. }
  1784. b = (uint8_t*)VA(k);
  1785. b += offset&(pgsz-1);
  1786. if(read == 1)
  1787. memmove(va, b, n); /* This can fault */
  1788. else
  1789. memmove(b, va, n);
  1790. poperror();
  1791. kunmap(k);
  1792. /* Ensure the process sees text page changes */
  1793. if(s->flushme)
  1794. memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
  1795. s->steal--;
  1796. if(read == 0)
  1797. p->newtlb = 1;
  1798. return n;
  1799. }
  1800. static Segment*
  1801. txt2data(Proc *p, Segment *s)
  1802. {
  1803. int i;
  1804. Segment *ps;
  1805. ps = newseg(SG_DATA|SG_READ|SG_WRITE|SG_EXEC, s->base, s->size);
  1806. ps->image = s->image;
  1807. incref(&ps->image->r);
  1808. ps->ldseg = s->ldseg;
  1809. ps->flushme = 1;
  1810. qlock(&p->seglock);
  1811. for(i = 0; i < NSEG; i++)
  1812. if(p->seg[i] == s)
  1813. break;
  1814. if(i == NSEG)
  1815. panic("segment gone");
  1816. qunlock(&s->lk);
  1817. putseg(s);
  1818. qlock(&ps->lk);
  1819. p->seg[i] = ps;
  1820. qunlock(&p->seglock);
  1821. return ps;
  1822. }
  1823. Segment*
  1824. data2txt(Segment *s)
  1825. {
  1826. Segment *ps;
  1827. ps = newseg(SG_TEXT|SG_READ|SG_EXEC, s->base, s->size);
  1828. ps->image = s->image;
  1829. incref(&ps->image->r);
  1830. ps->ldseg = s->ldseg;
  1831. ps->flushme = 1;
  1832. return ps;
  1833. }