|
|
1.1 root 1: /*
2: * stuff concerned with the table
3: * of processes we've spawned
4: * almost everything in this file should go away;
5: * it's needed only to keep /etc/utmp up to date
6: */
7:
8: #include "mgr.h"
9: #include <utmp.h>
10: #include <wait.h>
11: #include <libc.h>
12:
13: typedef struct {
14: int pid;
15: char *tty;
16: } Proc;
17:
18: #define MAXPROC 200
19: Proc proc[MAXPROC];
20: Proc *maxproc;
21:
22: /*
23: * make ourselves into a new process
24: * returns 0 if we're the parent, or we can't
25: * returns 1 in the child
26: */
27: newproc(rp)
28: Request *rp;
29: {
30: int pid;
31: register Proc *p;
32: char *ttyname(), *tty;
33:
34: if ((pid = fork()) < 0) {
35: logevent("can't fork; gave up\n");
36: close(rp->i->cfd);
37: return (0);
38: }
39: if (pid == 0)
40: return (1);
41: tty = ttyname(rp->i->cfd);
42: close(rp->i->cfd);
43: if (tty==NULL)
44: return (0);
45: logevent("newproc(%d, %s)\n", pid, tty);
46: for (p = proc; p < &proc[MAXPROC]; p++)
47: if (p->pid == 0) {
48: if ((p->tty = strdup(tty)) != NULL)
49: p->pid = pid;
50: return (0);
51: }
52: /* too bad */
53: logevent("no internal proc; didn't bother\n");
54: return (0);
55: }
56:
57: /*
58: * clean up after any dead children
59: */
60: checkkids()
61: {
62: int pid;
63:
64: while ((pid = wait3(NULL, WNOHANG, NULL)) > 0)
65: deadproc(pid);
66: }
67:
68: deadproc(pid)
69: int pid;
70: {
71: register Proc *p;
72:
73: logevent("deadproc(%d)\n", pid);
74: for (p = proc; p < &proc[MAXPROC]; p++)
75: if (p->pid == pid) {
76: rmutmp(p->tty);
77: p->pid = 0;
78: free(p->tty);
79: break;
80: }
81: }
82:
83: /*
84: * this should really be somewhere else ...
85: */
86:
87: rmutmp(tty)
88: char *tty;
89: {
90: static struct utmp ut, vt;
91: int fd;
92: long time();
93:
94: if (strncmp(tty, "/dev/", 5) == 0)
95: tty += 5;
96: strncpy(vt.ut_line, tty, sizeof(vt.ut_line));
97: vt.ut_time = time((long *)0);
98: if ((fd = open("/etc/utmp", 2)) >= 0) {
99: while (read(fd, (char *)&ut, sizeof(ut)) == sizeof(ut))
100: if (strncmp(tty, ut.ut_line, sizeof(ut.ut_line)) == 0) {
101: lseek(fd, (long)-sizeof(ut), 1);
102: write(fd, (char *)&vt, sizeof(vt));
103: break;
104: }
105: close(fd);
106: }
107: if ((fd = open("/usr/adm/wtmp", 1)) >= 0) {
108: lseek(fd, 0L, 2);
109: write(fd, (char *)&vt, sizeof(vt));
110: close(fd);
111: }
112: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.