devproc.c 32 KB

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