Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>4 #include <ctype.h>5 #include <mach.h>6 #define Extern extern7 #include "acid.h"8 #include "y.tab.h"10 static void install(int);12 void13 sproc(int xpid)14 {15 Lsym *s;16 int i;18 if(symmap == 0)19 error("no map");21 if(pid == xpid)22 return;24 if(xpid <= 0)25 error("bad pid");27 unmapproc(cormap);28 unmapfile(corhdr, cormap);29 free(correg);30 correg = nil;32 if(mapproc(xpid, cormap, &correg) < 0)33 error("setproc %d: %r", xpid);35 /* XXX check text file here? */37 pid = xpid;38 s = look("pid");39 s->v->store.u.ival = pid;41 for(i=0; i<cormap->nseg; i++)42 if(cormap->seg[i].file == nil){43 if(strcmp(cormap->seg[i].name, "data") == 0)44 cormap->seg[i].name = "*data";45 if(strcmp(cormap->seg[i].name, "text") == 0)46 cormap->seg[i].name = "*text";47 }48 install(pid);49 }51 int52 nproc(char **argv)53 {54 int pid, i;56 pid = fork();57 switch(pid) {58 case -1:59 error("new: fork %r");60 case 0:61 rfork(RFNAMEG|RFNOTEG);62 if(ctlproc(getpid(), "hang") < 0)63 fatal("new: hang %d: %r", getpid());65 close(0);66 close(1);67 close(2);68 for(i = 3; i < NFD; i++)69 close(i);71 open("/dev/tty", OREAD);72 open("/dev/tty", OWRITE);73 open("/dev/tty", OWRITE);74 exec(argv[0], argv);75 fatal("new: exec %s: %r");76 default:77 install(pid);78 msg(pid, "waitstop");79 notes(pid);80 sproc(pid);81 dostop(pid);82 break;83 }85 return pid;86 }88 void89 notes(int pid)90 {91 Lsym *s;92 Value *v;93 int i, n;94 char **notes;95 List *l, **tail;97 s = look("notes");98 if(s == 0)99 return;101 v = s->v;102 n = procnotes(pid, ¬es);103 if(n < 0)104 error("procnotes pid=%d: %r", pid);106 v->set = 1;107 v->type = TLIST;108 v->store.u.l = 0;109 tail = &v->store.u.l;110 for(i=0; i<n; i++) {111 l = al(TSTRING);112 l->store.u.string = strnode(notes[i]);113 l->store.fmt = 's';114 *tail = l;115 tail = &l->next;116 }117 free(notes);118 }120 void121 dostop(int pid)122 {123 Lsym *s;124 Node *np, *p;126 s = look("stopped");127 if(s && s->proc) {128 np = an(ONAME, ZN, ZN);129 np->sym = s;130 np->store.fmt = 'D';131 np->type = TINT;132 p = con(pid);133 p->store.fmt = 'D';134 np = an(OCALL, np, p);135 execute(np);136 }137 }139 static void140 install(int pid)141 {142 Lsym *s;143 List *l;144 int i, new, p;146 new = -1;147 for(i = 0; i < Maxproc; i++) {148 p = ptab[i].pid;149 if(p == pid)150 return;151 if(p == 0 && new == -1)152 new = i;153 }154 if(new == -1)155 error("no free process slots");157 ptab[new].pid = pid;159 s = look("proclist");160 l = al(TINT);161 l->store.fmt = 'D';162 l->store.u.ival = pid;163 l->next = s->v->store.u.l;164 s->v->store.u.l = l;165 s->v->set = 1;166 }168 static int169 installed(int pid)170 {171 int i;173 for(i=0; i<Maxproc; i++)174 if(ptab[i].pid == pid)175 return 1;176 return 0;177 }179 void180 deinstall(int pid)181 {182 int i;183 Lsym *s;184 List *f, **d;186 for(i = 0; i < Maxproc; i++) {187 if(ptab[i].pid == pid) {188 detachproc(pid);189 // close(ptab[i].ctl);190 ptab[i].pid = 0;191 s = look("proclist");192 d = &s->v->store.u.l;193 for(f = *d; f; f = f->next) {194 if(f->store.u.ival == pid) {195 *d = f->next;196 break;197 }198 }199 s = look("pid");200 if(s->v->store.u.ival == pid)201 s->v->store.u.ival = 0;202 return;203 }204 }205 }207 void208 msg(int pid, char *msg)209 {210 int i;211 char err[ERRMAX];213 for(i = 0; i < Maxproc; i++) {214 if(ptab[i].pid == pid) {215 if(ctlproc(pid, msg) < 0){216 errstr(err, sizeof err);217 if(strcmp(err, "process exited") == 0)218 deinstall(pid);219 error("msg: pid=%d %s: %s", pid, msg, err);220 }221 return;222 }223 }224 error("msg: pid=%d: not found for %s", pid, msg);225 }227 char *228 getstatus(int pid)229 {230 int fd;231 char *p;233 static char buf[128];235 sprint(buf, "/proc/%d/status", pid);236 fd = open(buf, OREAD);237 if(fd < 0)238 error("open %s: %r", buf);239 read(fd, buf, sizeof(buf));240 close(fd);241 p = buf+56+12; /* Do better! */242 while(*p == ' ')243 p--;244 p[1] = '\0';245 return buf+56; /* ditto */246 }