-
Notifications
You must be signed in to change notification settings - Fork 27
/
tty_aux.c
414 lines (357 loc) · 9.77 KB
/
tty_aux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Changes for use with the CM11A are
* Copyright 1996, 1997, 1998, 1999 by Daniel B. Suthers,
* Pleasanton Ca. 94588 USA
* E-MAIL [email protected]
*
*/
/*
* Copyright 1986 by Larry Campbell, 73 Concord Street, Maynard MA 01754 USA
* (maynard!campbell).
*
* John Chmielewski (tesla!jlc until 9/1/86, then rogue!jlc) assisted
* by doing the System V port and adding some nice features. Thanks!
*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef SCO
#define _IBCS2
#endif
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
/* msf - added for glibc/rh 5.0 */
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#ifdef HAVE_FEATURE_RFXLAN
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#endif
#include "x10.h"
#include "process.h"
#ifdef pid_t
#define PID_T pid_t
#else
#define PID_T long
#endif
void exit();
char *make_lock_name();
PID_T lockpid(char *);
extern int verbose, i_am_relay;
extern void error();
extern CONFIG config;
extern CONFIG *configp;
extern int sptty;
int ttylock();
int lock_device();
int tty_aux = -1; /* Real tty */
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if !defined(HAVE_STRUCT_TERMIOS) && !defined(HAVE_STRUCT_TERMIO)
# include <sgtty.h>
static struct sgttyb oldsb, newsb;
void hangup();
#else
# ifndef HAVE_STRUCT_TERMIOS
# ifdef HAVE_TERMIO_H
# include <termio.h>
# endif
# ifdef HAVE_SYS_TERMIO_H
# include <sys/termio.h>
# endif
static struct termio oldsb, newsb;
# else
# ifdef HAVE_TERMIOS_H
# include <termios.h>
# endif
# ifdef HAVE_SYS_TERMIOS_H
# include <sys/termios.h>
# endif
static struct termios oldsb, newsb;
# endif
# ifndef NCC
# define NCC NCCS
# endif
#endif
/*---------------------------------------------------------------+
| Aux port baud rate. |
+---------------------------------------------------------------*/
int aux_baudrate ( void )
{
return (configp->auxdev == DEV_W800RF32) ? B4800 :
(configp->auxdev == DEV_MR26A) ? B9600 :
(configp->rfxcom_hibaud == YES) ? B38400 : B4800;
}
/*---------------------------------------------------------------+
| Alternate baud rate for RFXCOM receiver to that specified |
| in the config file. I.e., if RFXCOM_HIBAUD YES is specified |
| in the config file, return the baud rate for NO, and vice- |
| versa. |
+---------------------------------------------------------------*/
int rfxcom_alt_baudrate ( void )
{
return (configp->rfxcom_hibaud == YES) ? B4800 : B38400;
}
int setup_tty_aux( int auxbaud, int lockflag )
{
int flags;
if ( !configp->ttyaux[0] ) {
syslog(LOG_ERR, "No TTY_AUX specified in config file");
return 1;
}
if ( lockflag ) {
if ( ttylock(configp->ttyaux) < 0 ) {
syslog(LOG_ERR, "Other process is using the tty_aux port.");
return 1;
}
}
#ifdef DEBUG
syslog(LOG_ERR, "Opening tty_aux line.");
#endif
#ifdef HAVE_FEATURE_RFXLAN
#if defined(HAVE_NETDB_H) && defined(HAVE_MEMSET) && defined(HAVE_SOCKET)
if ( configp->auxhost[0] && configp->auxport[0] ) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Stream socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
/* Obtain address(es) matching host/port */
s = getaddrinfo(configp->auxhost, configp->auxport, &hints, &result);
if (s != 0) {
syslog(LOG_ERR, "getaddrinfo: %s\n", gai_strerror(s));
} else {
/*
* getaddrinfo() returns a list of address structures.
* Try each address until we successfully connect(2).
* If socket(2) (or connect(2)) fails, we (close the socket
* and) try the next address.
*/
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sfd);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
syslog(LOG_ERR, "Could not connect\n");
} else {
tty_aux = sfd;
return 0;
}
}
}
#else
#error "No system support required for RFXLAN feature, please disable."
#endif
#endif
#ifdef O_NONBLOCK
/* Open with non-blocking I/O, we'll fix after we set CLOCAL */
tty_aux = open(configp->ttyaux, O_RDWR|O_NONBLOCK);
#else
tty_aux = open(configp->ttyaux, O_RDWR);
#endif
if ( tty_aux < 0 ) {
syslog(LOG_ERR, "Can't open tty_aux line.");
return 1;
}
#ifdef DEBUG
else {
syslog(LOG_ERR, "tty_aux line opened.");
}
#endif
#if !defined(HAVE_STRUCT_TERMIOS) && !defined(HAVE_STRUCT_TERMIO)
/* Old-style BSD/v7 sgtty calls */
(void) ioctl(tty_aux, TIOCFLUSH, (struct sgttyb *) NULL);
(void) ioctl(tty_aux, TIOCGETP, &oldsb);
newsb = oldsb;
newsb.sg_flags |= RAW;
newsb.sg_flags &= ~(ECHO | EVENP | ODDP);
hangup();
newsb.sg_ispeed = newsb.sg_ospeed = auxbaud; /* raise DTR & set speed */
(void) ioctl(tty_aux, TIOCSETN, &newsb);
#elif !defined(HAVE_STRUCT_TERMIOS)
/* SVr2-style termio */
if (ioctl(tty_aux, TCGETA, &oldsb) < 0) {
syslog(LOG_ERR,"ioctl get");
exit(1);
}
newsb = oldsb;
newsb.c_lflag = 0;
newsb.c_oflag = 0;
newsb.c_iflag = IGNBRK | IGNPAR;
newsb.c_cflag = (CLOCAL | auxbaud | CS8 | CREAD);
newsb.c_cc[VMIN] = 1;
newsb.c_cc[VTIME] = 0;
newsb.c_cc[VINTR] = 0;
newsb.c_cc[VQUIT] = 0;
#ifdef VSWTCH
newsb.c_cc[VSWTCH] = 0;
#endif
newsb.c_cc[VTIME] = 0;
if (ioctl(tty_aux, TCSETAF, &newsb) < 0) {
syslog(LOG_ERR,"aux ioctl set");
exit(1);
}
#else
{
/* POSIX-style termios */
int s;
s = tcgetattr(tty_aux, &oldsb);
if (s < 0) {
syslog(LOG_ERR,"ttopen tcgetattr");
exit(1);
}
newsb = oldsb;
/* newsb.c_iflag = BRKINT|(oldsb.c_iflag & (IXON|IXANY|IXOFF)); */
newsb.c_iflag = IGNPAR;
newsb.c_oflag = 0;
newsb.c_lflag = 0;
newsb.c_cflag = (CLOCAL | CS8 | CREAD);
for (s = 0; s < NCC; s++)
newsb.c_cc[s] = 0;
newsb.c_cc[VMIN] = 1;
newsb.c_cc[VTIME] = 0;
#ifdef BEFORE
#ifdef VSWTCH
newsb.c_cc[VSWTCH] = 0;
#endif
newsb.c_cc[VSUSP] = 0;
newsb.c_cc[VSTART] = 0;
newsb.c_cc[VSTOP] = 0;
#endif
/* POSIX sets speed separately */
cfsetispeed(&newsb, auxbaud);
cfsetospeed(&newsb, auxbaud);
tcsetattr(tty_aux, TCSADRAIN, &newsb);
}
#endif
#ifdef O_NONBLOCK
/* Now that we have set CLOCAL on the port, we can use blocking I/O */
flags = fcntl(tty_aux, F_GETFL);
fcntl(tty_aux, F_SETFL, flags & ~O_NONBLOCK);
#endif
return(0);
}
void restore_tty_aux()
{
#if !defined(HAVE_STRUCT_TERMIOS) && !defined(HAVE_STRUCT_TERMIO)
hangup();
(void) ioctl(tty_aux, TIOCSETN, &oldsb);
#elif !defined(HAVE_STRUCT_TERMIOS)
(void) ioctl(tty_aux, TCSETAF, &oldsb);
#else
tcsetattr(tty_aux, TCSADRAIN, &oldsb);
#endif
}
#if !defined(HAVE_STRUCT_TERMIOS) && !defined(HAVE_STRUCT_TERMIO)
void hangup_aux()
{
newsb.sg_ispeed = newsb.sg_ospeed = B0; /* drop DTR */
(void) ioctl(tty_aux, TIOCSETN, &newsb);
sleep(SMALLPAUSE);
}
#endif
void quit_aux()
{
if (tty_aux == -1)
exit(1);
restore_tty_aux();
exit(1);
}
/* set up the real and spool psuedo tty file descriptor.
*/
int setup_sp_tty_aux()
{
/* extern char spoolfile[PATH_MAX]; */
extern char spoolfile[PATH_LEN + 1];
if ( tty_aux < 0 ) setup_tty_aux(aux_baudrate(), 0);
if ( tty_aux < 0 ) {
error("Can't open tty_aux line");
syslog(LOG_DAEMON | LOG_ERR, "Can't open tty_aux line\n");
}
sptty = open(spoolfile, O_RDWR|O_APPEND,0x644);/* open the spool file */
if ( sptty < 0 ) {
char tmpbuf[sizeof(spoolfile) + 100];
perror("Can't open spool file");
sprintf(tmpbuf, "Can't open spool file %s", spoolfile);
error(tmpbuf);
strcat(tmpbuf, "\n");
syslog(LOG_DAEMON | LOG_ERR, "Can't open spool file %s", spoolfile);
}
lseek(sptty, 0, SEEK_END); /* seek to end of file */
return(0);
}
int config_tty_dtr_rts ( int tty, unsigned char bits )
{
int status, retcode = 0;
if ( (retcode = ioctl(tty, TIOCMGET, &status)) < 0 )
return retcode;
status &= ~(TIOCM_DTR | TIOCM_RTS);
if ( bits & RAISE_DTR ) status |= TIOCM_DTR;
if ( bits & RAISE_RTS ) status |= TIOCM_RTS;
retcode |= ioctl(tty, TIOCMSET, &status);
return retcode;
}