|
|
1.1 root 1: #include <stdio.h>
2: #include <sys/dkio.h>
3: #include <signal.h>
4: #include <errno.h>
5: #include <ctype.h>
6:
7: char *netfiles = "/dev/dk/dk";
8: #define TRAFFIC 2 /* if unit != b, dkctlchan ignores this anyway */
9: #define DEFUNIT 'a'
10:
11: main(argc, argv)
12: int argc;
13: char **argv;
14: {
15: int unit;
16: int noinit;
17: int fd;
18: int i;
19:
20: unit = DEFUNIT; /* look at netfiles by default */
21: noinit = 0;
22: for (i = 1; i < argc; i++) {
23: if (strcmp(argv[i], "-t") == 0)
24: continue;
25: if (isdigit(*argv[i])) {
26: unit = *argv[i];
27: continue;
28: }
29: if (strcmp(argv[i], "-i") == 0) {
30: noinit = 1;
31: continue;
32: }
33: if (strcmp(argv[i], "-N") == 0 && ++i < argc) {
34: netfiles = argv[i];
35: continue;
36: }
37: fprintf(stderr, "usage: dkhup [-N netfiles] [-t [02]]\n");
38: exit(1);
39: }
40: setup();
41: fd = init(unit, noinit);
42: i = 0;
43: for (;;) {
44: if (takemesg(fd) > 0) {
45: i = 0;
46: continue;
47: }
48: if (++i < 5) { /* give it a few tries */
49: sleep(1);
50: continue;
51: }
52: fd = reset(unit, fd);
53: }
54: }
55:
56: setup()
57: {
58: if (fork())
59: exit(0);
60: signal(SIGINT, SIG_IGN);
61: signal(SIGQUIT, SIG_IGN);
62: signal(SIGHUP, SIG_IGN);
63: signal(SIGTERM, SIG_IGN);
64: signal(SIGPIPE, SIG_IGN);
65: }
66:
67: int
68: init(unit, noinit)
69: int unit, noinit;
70: {
71: int fd;
72:
73: if ((fd = dkctlchan(TRAFFIC, unit)) < 0) {
74: fprintf(stderr, "dkhup %s %c: can't open control channel\n", netfiles, unit);
75: return (-1);
76: }
77: if (noinit == 0) {
78: ioctl(fd, DIOCHUP, 0);
79: ioctl(fd, DIOCHUP, 0);
80: ioctl(fd, DIOCHUP, 0);
81: }
82: ioctl(fd, DIOCNXCL, 0);
83: return (fd);
84: }
85:
86: /*
87: * wait for a message from the CMC
88: * this shouldn't happen;
89: * messages are supposed to be intercepted by the system
90: * if the read returns an error,
91: * the interface shut down
92: */
93:
94: takemesg(fd)
95: int fd;
96: {
97: int n;
98: char buf[64];
99:
100: if ((n = read(fd, buf, sizeof(buf))) <= 0)
101: return (-1);
102: /*
103: * if it was interesting to print the message,
104: * this would be the place to do so
105: */
106: return (1);
107: }
108:
109: /*
110: * reset after an error
111: * close the channel, try to reset the KMC (assumed)
112: */
113:
114: int
115: reset(unit, fd)
116: int unit;
117: int fd;
118: {
119: fprintf(stderr, "dkhup %s %c: error %d; resetting\n", netfiles, unit, errno);
120: ioctl(fd, KIOCSHUT, 0);
121: close(fd);
122: sleep(45);
123: loadkmc(unit);
124: sleep(5);
125: while ((fd = init(unit, 0)) < 0) {
126: fprintf(stderr, "dkhup %s %c: can't reinit\n", netfiles, unit);
127: sleep(60); /* or just exit? */
128: }
129: return (fd);
130: }
131:
132: loadkmc(unit)
133: int unit;
134: {
135: char buf[64];
136:
137: if (unit == DEFUNIT)
138: unit = '2'; /* ugh */
139: sprintf(buf, "cd /usr/dk; /etc/kmcdump %c", unit);
140: system(buf);
141: sprintf(buf, "/etc/kdiload %c", unit);
142: system(buf);
143: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.