|
|
1.1 root 1: /*
2: * dial.c
3: *
4: * Implement the dial(3) function calls.
5: *
6: * Copyright 1987 (c) John F. Haugh II
7: * Changes Copyright 1989-91 (c) Mark Williams Company
8: */
9:
10: #include <stdio.h>
11: #include <signal.h>
12: #include <sys/fcntl.h>
13: #include "modemcap.h"
14: #include "dial.h"
15: #include "dcp.h"
16: #include "ldev.h"
17:
18:
19: char *devname = NULL; /* Communications Device Name Connected */
20: char *rdevname = NULL; /* Remote device name */
21:
22: static char login_lock[15];
23: static char enableme[16];
24: static int modemfd = -1;
25: extern char *strtok();
26: extern int hupcatch();
27:
28: /*
29: * dial(cp) CALL cp;
30: *
31: * Dial and initiate the call specified via the given CALL data structure.
32: * Returns the opened file descriptor to be used for reads and writes to
33: * the determined device line. If there is an error, then the return value
34: * is less than zero, and the variable "merror" is set appropriately.
35: */
36:
37: dial(cp)
38: CALL *cp;
39: {
40: char modemline[64]; /* device name */
41: char *modemname = "\0";
42: char *strcpy (),
43: *strcat ();
44: int fd, err;
45:
46: fd = -1; /* channel illegal until line is opened */
47: if ( (err=findline(cp, &modemname)) <= 0 )
48: goto error;
49: strcat(strcpy(modemline, DEVDIR), cp->line);
50: if ((fd = open (modemline, O_RDWR)) < 0) { /* can't open modem line */
51: err = M_L_PROB;
52: goto error;
53: }
54: if ( (err=ttyinit(fd, cp->baud)) != 0 )
55: goto error;
56:
57: initmodem(modemname, fd); /* setup modemcap variables */
58: if (cp->telno == NULL) /* no phone number, connection complete */
59: goto okay;
60: if (! DI) { /* modem has no ACU!!! */
61: /* plog("Says no acu to attatch to???"); */
62: err = M_A_PROB; /* no ACU to attach to */
63: goto error;
64: }
65: if (BD != cp->baud) { /* is connection desired at high speed? */
66: if (BL != cp->baud) {/* is connection desired at low speed? */
67: err = M_ILL_BD; /* modem can't handle this speed */
68: goto error;
69: }
70: BD = BL; /* set baud to low baud rate */
71: CO = CL; /* set connect reply to low baud reply */
72: }
73: if (err = mdial (cp->telno, fd)) /* some error trying to dial */
74: goto error;
75:
76: okay:
77: return (modemfd = fd);
78: error:
79: hangup(fd);
80: return (merrno = err);
81: }
82:
83: /* undial()
84: * removes the lock on the remote device if it exists and reenables
85: * the port. Undial() is called by hangup(), which was called by
86: * sysend(). Bob H. 11/22/91.
87: */
88: undial (fd)
89: int fd;
90: {
91: close (fd); /* close the port */
92:
93: /* If lock removal fails, print message. */
94:
95: if ( (strcmp(rdevname,"-") != 0) && lockttyexist(rdevname) ){
96: if(unlocktty(rdevname) == -1){
97: printmsg(M_DEBUG,"Undial: tty lock file removal failed");
98: plog(M_CALL,"Undial: tty lock file removal failed");
99: }
100: }
101: /* If lock removal failed, then do not re enable the port because we
102: * no longer know who did what to the remote port. Re enabling the
103: * port could result in a race condition we don't want.
104: */
105:
106: if ((enableme[0] != '\0') && (lockttyexist(rdevname) == 0)){
107: plog(M_CALL, "Enabling tty line %s", enableme);
108: exec_stat("enable", enableme);
109: strcpy(enableme, "");
110: }else{
111: if(enableme[0] != '\0'){
112: printmsg(M_DEBUG,"Undial: Can not re-enable port due to tty lock file.");
113: plog(M_CALL,"Undial: Could not re-enable port due to tty lock file.");
114: }
115: }
116: rdevname = NULL;
117: }
118:
119: static
120: findline(callp, brand)
121: CALL *callp;
122: char **brand;
123: {
124: int exists = 0; /* device exists at some baud rate */
125: int tried = 0; /* found a device but it was locked */
126: int devflag, telflag;
127: char *l_lline; /* tty device local name */
128: char *l_rline; /* tty device remote name */
129: char *l_type; /* ACU, DIR, etc. */
130: char *l_brand; /* modemcap brand name */
131: int l_baud; /* tty baud rate */
132: int retval; /* Place to stash return of exec_stat */
133:
134: ldev_open();
135: if ( ((devflag=(callp->line != NULL)) &&
136: (telflag=(callp->telno != NULL))) || (!devflag && !telflag) )
137: return(M_DEV_TEL);
138:
139: while ( ldev_next() ) {
140: l_type = ldev_value(type_e);
141: l_lline = ldev_value(lline_e);
142: l_rline = ldev_value(rline_e);
143: l_baud = atoi(ldev_value(baud_e));
144: l_brand = ldev_value(brand_e);
145:
146: if ( strcmp(l_type, "ACU") == 0 ) {
147: if ( devflag )
148: continue;
149: exists++;
150: if (l_baud != callp->baud)
151: continue;
152: } else if ( strcmp(l_type, "DIR") == 0 ) {
153: if ( telflag )
154: continue;
155: if ( strcmp(l_lline, callp->line) )
156: continue;
157: callp->baud = l_baud;
158: } else {
159: continue;
160: }
161: ++tried; /* found device at desired baud rate */
162:
163:
164: /* If the Ldev remote line is not a '-', then see if a lock
165: * exists on the remote device. If a lock exists, then we don't
166: * want to disable the remote before calling out on the local
167: * local device for fear of booting off a logged in process.
168: */
169:
170: /* Check for a lock on the remote device */
171: if ((strcmp(l_rline,"-")!=0) && (0 != lockttyexist(l_rline))) {
172: plog(M_CALL,"Remote tty device %s locked, cannot disable.",
173: l_rline);
174: continue;
175: } else {
176: enableme[0] = '\0';
177: if(strcmp(l_rline,"-") !=0){
178: /* Disable the remote device and then create a lock on it.
179: * If the lock fails, abort.
180: * Note that we will then sleep for 5 seconds to make sure that
181: * the port gets closed after the disable.
182: */
183: if (locktty(l_rline) < 0) {
184: plog(M_CALL,"Remote tty device %s locked, cannot disable.",
185: l_rline);
186: continue;
187: }
188:
189: /* Note that disable could be terminated by
190: * a SIGHUP when the port is disabled.
191: */
192: if (0!=(retval=exec_stat("disable", l_rline)) &&
193: SIGHUP<<8 != retval) {
194: plog(M_CALL,"Disable of tty line %s failed",
195: l_rline);
196: continue;
197: }else{
198: plog(M_CALL,"Disabling tty line %s", l_rline);
199: sleep(5);
200: strcpy(enableme,l_rline);
201: }
202: }
203: }
204: devname = l_lline;
205: rdevname = l_rline;
206: *brand = l_brand;
207: ldev_close();
208: callp->line = l_lline;
209: return (1);
210: }
211: if (tried)
212: return (merrno = M_DV_NT_A);
213: else if (exists) {
214: return (merrno = M_ILL_BD);
215: } else {
216: /* plog("Device not known: %s", brand); */
217: return (merrno = M_DV_NT_K);
218: }
219: }
220:
221: exec_stat(command, line)
222: char *command;
223: char *line;
224: {
225: int pid;
226: int waitstat;
227: static char etccommand[32];
228:
229: strcpy(etccommand, "/etc/");
230: strcat(etccommand, command);
231: /* plog("%s (%s) on line %s", command, etccommand, line);
232: */
233: pid = fork();
234: if (pid == 0) {
235: execl(etccommand, command, line, NULL);
236: exit(1);
237: } else
238: wait(&waitstat);
239:
240: return waitstat;
241: }
242:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.