|
|
1.1 root 1: /*
2: * daemon.c -- main control routines for spider and phone dpd daemons,
3: * and for line printer daemon.
4: */
5:
6: long snsum = 0; /*number of characters written*/
7:
8: #include "daemon0.c"
9:
10: #include <setjmp.h>
11: #include <sys/dir.h>
12:
13: struct direct dbuf;
14: int LDIRNAM = 0; /*length of directory name. MRW*/
15: int LCHAR = 0; /*length of file name thru 'df'.*/
16: int LPID = 0; /*length of file name to process-id.*/
17: int retcode = 1;
18: int nwait = 0;
19: char *fowner; /*RBB*/
20: char baddf[30]; /*name for unsendable df-files.*/
21: FILE *popen();
22:
23: #if LPD
24: int waittm = 10;
25: #else
26: int waittm = 60;
27: #endif
28:
29: main()
30: {
31: register char *p1, *p2;
32: FILE *df;
33:
34: dem_setup();
35: LDIRNAM = 0; /*calculate length of directory name. MRW*/
36: while(dfname[LDIRNAM]) LDIRNAM++;
37: LCHAR = LDIRNAM + 2;
38: LPID = LDIRNAM + 3;
39:
40: again:
41: snsum = 0;
42: if(access(lock, 0) < 0){
43: logerr("Lock has disappeared.");
44: dem_dis();
45: exit(1);
46: }
47: df = fopen(dpd, "r");
48: if (df) {
49: do {
50: if(fread((char *)&dbuf, sizeof dbuf, 1, df) < 1){
51: dem_dis(); /*disconnect phone line. MRW*/
52: unlink(lock);
53: exit(0);
54: }
55: } while (dbuf.d_ino==0 || dbuf.d_name[0]!='d' || dbuf.d_name[1]!='f');
56: fclose(df);
57: }
58: p1 = dbuf.d_name;
59: p2 = &dfname[LDIRNAM];
60: while (p1 < &dbuf.d_name[DIRSIZ])
61: *p2++ = *p1++;
62: if (trysend() == 0) {
63: waittm = 60;
64: nwait = 0;
65: goto again;
66: }
67: #if PHONE
68: if(nwait > 10){ /*after 3 hours try fresh daemon. MRW*/
69: unlink(lock);
70: execl("/usr/lib/dpd", "dpd", (char *)0);
71: execl("/etc/dpd", "dpd", (char *)0);
72: logerr("Can't find dpd.");
73: exit(1);
74: }
75: #endif
76: sleep(waittm);
77: #if PHONE || SPIDER
78: #ifndef DEBUG
79: if (waittm <= 8*60)
80: waittm *= 2;
81: else
82: nwait++;
83: #endif
84: #endif
85: goto again;
86: }
87:
88: /*
89: * The remaining part is the device interface, to spider, the dataphone,
90: * or the line printer.
91: */
92:
93: #define MXLINE 128
94: #define MXMESS 500
95:
96: char *snumb;
97: char line[MXLINE];
98: char message[MXMESS+1];
99: char *mesp;
100: char mailfname[64];
101: int linel;
102: FILE *dfb = NULL;
103: jmp_buf env;
104: char ff = '\014'; /*formfeed*/
105: char *copline();
106:
107: trysend()
108: {
109: if(retcode != 0)
110: if(retcode = dem_con()) /*connect phone line.*/
111: return(retcode);
112: retcode = 1;
113: if(setjmp(env))
114: return(retcode);
115: xtrysend();
116: return(0);
117: }
118:
119: xtrysend()
120: {
121: int i;
122:
123: dem_open(dfname); /*open spider connection.*/
124: if((dfb = fopen(dfname, "r")) == NULL){
125: if(LDIRNAM < (i = sizeof(baddf)-1)){
126: strncpy(baddf, dfname, i);
127: baddf[i] = '\0';
128: baddf[LDIRNAM] = 'b';
129: link(dfname, baddf);
130: }
131: unlink(dfname);
132: retcode = 0;
133: trouble("Can't read %s.", dfname);
134: }
135: getowner(dfname); /*RBB*/
136: mesp = message;
137: *mesp = 0;
138: while (getline()) switch (line[0]) {
139:
140: case 'S':
141: get_snumb(); /*get snumb for GCOS.*/
142: continue;
143:
144: case 'B':
145: if(sascii(0))
146: trouble("Can't send %s.", &line[1]);
147: continue;
148:
149: case 'F':
150: if(sascii(1))
151: trouble("Can't send %s.", &line[1]);
152: continue;
153:
154: case 'I': /*mail back $IDENT card. MRW*/
155: mesp = copline(&line[1], linel-1, mesp);
156:
157: case 'L':
158: lwrite(); /*write a literal line.*/
159: continue;
160:
161: case 'M':
162: continue;
163:
164: case 'N': /*mail back file name. MRW*/
165: copline(&line[1], linel-1, mailfname);
166: continue;
167:
168: case 'Q': /*additional text to mail back*/
169: if(mesp+linel <= message+MXMESS)
170: mesp = copline(&line[1], linel-1, mesp);
171:
172: case 'U':
173: continue;
174: }
175: /*
176: * Second pass.
177: * Unlink files and send mail.
178: */
179: alarm(0);
180: fseek(dfb, (long)0, 0);
181: while (getline()) switch (line[0]) {
182:
183: default:
184: continue;
185:
186: case 'U':
187: unlink(&line[1]);
188: continue;
189:
190: case 'M':
191: sendmail();
192: continue;
193: }
194: FCLOSE(dfb);
195: dem_close(); /*close connection to spider.*/
196: unlink(dfname);
197: retcode = 0;
198: trouble("OK: %-5s %-7s %-8s", snumb, fowner, dfname+LDIRNAM); /*RBB*/
199: }
200:
201: #define LUBM 30 /*length of mail command line.*/
202: #define IARG1 5 /*start of arg to mail command.*/
203: char mail[LUBM+1] = "mail ";
204: FILE *pmail;
205:
206: sendmail()
207: {
208: register i;
209:
210: alarm(0);
211: i = 0;
212: while((mail[IARG1+i] = line[i+1]) && (++i < LUBM-IARG1));
213: mail[IARG1+i] = 0;
214: if((pmail = popen(mail, "w")) == NULL){
215: logerr("Can't mail: %-5s %-8s",snumb,dfname+LDIRNAM); /*MRW*/
216: return;
217: }
218: maildname();
219: pclose(pmail);
220: }
221:
222: #if LPD == 0
223:
224: maildname() /*break up dfname into command name, and process-id
225: to send back in the mail. MRW*/
226: {
227: char c;
228: char *command;
229:
230: fprintf(pmail,"Sent %-5s: file %s%s",snumb,mailfname,message);
231: /*
232: c = dfname[LCHAR];
233: switch (c){
234: case 'A':
235: case 'a':
236: command = "dpr";
237: break;
238:
239: case 'G':
240: case 'g':
241: command = "gcat";
242: break;
243:
244: case 'J':
245: case 'j':
246: command = "ibm";
247: break;
248:
249: case 'N':
250: case 'n':
251: command = "fsend";
252: break;
253:
254: case 'T':
255: case 't':
256: command = "fget";
257: break;
258:
259: default:
260: command = &dfname[LDIRNAM];
261: break;
262: }
263: fprintf(pmail, "%s process-id was %s\n", command, &dfname[LPID]);
264: */
265: }
266:
267: #endif
268:
269:
270: getline()
271: {
272: register char *lp;
273: register c;
274:
275: lp = line;
276: linel = 0;
277: while ((c = getc(dfb)) != '\n' && linel < MXLINE-2) {
278: if (c == EOF)
279: return(0);
280: if (c=='\t') {
281: do {
282: *lp++ = ' ';
283: linel++;
284: } while ((linel & 07) != 0);
285: continue;
286: }
287: *lp++ = c;
288: linel++;
289: }
290: *lp++ = 0;
291: return(1);
292: }
293:
294: char *
295: copline(ab, n, ml)
296: char *ab;
297: int n;
298: char ml[64];
299: {
300: register char *b, *p, *eb;
301:
302: b = ab;
303: eb = b+n;
304: p = ml;
305: while (b<eb && p < &ml[63])
306: *p++ = *b++;
307: *p++ = '\n';
308: *p = 0; /*RBB*/
309: return(p);
310: }
311:
312: #if LPD == 0
313:
314: /*
315: * the following code determines who the file's owner is.
316: * /*RBB*/
317:
318: struct passwd *getpwuid();
319:
320: getowner(file)
321: char *file;
322: {
323: struct passwd *f;
324:
325: if(stat(file, &statbuf) < 0)
326: return;
327: if((f = getpwuid(statbuf.st_uid)) == NULL)
328: fowner = "";
329: else
330: fowner = f->pw_name;
331: }
332:
333: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.