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
|
/* $OpenBSD: ldattach.c,v 1.4 2008/01/05 17:33:28 mbalmer Exp $ */
/*
* Copyright (c) 2007, 2008 Marc Balmer <mbalmer@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* A replacement for slattach(8) and nmeaattach(8) that can be used from
* the commandline or from init(8) (using entries in /etc/ttys).
*/
#include <sys/ioctl.h>
#include <sys/limits.h>
#include <sys/ttycom.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
__dead void usage(void);
void coroner(int);
volatile sig_atomic_t dying = 0;
__dead void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-27dehmo] [-s baudrate] "
"[-t cond] discipline device\n", __progname);
exit(1);
}
int
main(int argc, char *argv[])
{
struct termios tty;
struct tstamps tstamps;
const char *errstr;
sigset_t sigset;
pid_t ppid;
int ch, fd, ldisc, nodaemon = 0;
int bits = 0, parity = 0, stop = 0, flowcl = 0, hupcl = 1;
speed_t speed = 0;
char devn[32], *dev, *disc;
tstamps.ts_set = tstamps.ts_clr = 0;
if ((ppid = getppid()) == 1)
nodaemon = 1;
while ((ch = getopt(argc, argv, "27dehmos:t:")) != -1) {
switch (ch) {
case '2':
stop = 2;
break;
case '7':
bits = 7;
break;
case 'd':
nodaemon = 1;
break;
case 'e':
parity = 'e';
break;
case 'h':
flowcl = 1;
break;
case 'm':
hupcl = 0;
break;
case 'o':
parity = 'o';
break;
case 's':
speed = (speed_t)strtonum(optarg, 0, UINT_MAX, &errstr);
if (errstr) {
if (ppid != 1)
errx(1, "speed is %s: %s", errstr,
optarg);
else
goto bail_out;
}
break;
case 't':
if (!strcasecmp(optarg, "dcd"))
tstamps.ts_set |= TIOCM_CAR;
else if (!strcasecmp(optarg, "!dcd"))
tstamps.ts_clr |= TIOCM_CAR;
else if (!strcasecmp(optarg, "cts"))
tstamps.ts_set |= TIOCM_CTS;
else if (!strcasecmp(optarg, "!cts"))
tstamps.ts_clr |= TIOCM_CTS;
else {
if (ppid != 1)
errx(1, "'%s' not supported for "
"timestamping", optarg);
else
goto bail_out;
}
break;
default:
if (ppid != -1)
usage();
}
}
argc -= optind;
argv += optind;
if (ppid != 1 && argc != 2)
usage();
disc = *argv++;
dev = *argv;
if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
(void)snprintf(devn, sizeof(devn),
"%s%s", _PATH_DEV, dev);
dev = devn;
}
if (!strcmp(disc, "slip")) {
bits = 8; /* make sure we use 8 databits */
ldisc = SLIPDISC;
} else if (!strcmp(disc, "nmea")) {
ldisc = NMEADISC;
if (speed == 0)
speed = B4800; /* default is 4800 baud for nmea */
} else if (!strcmp(disc, "msts")) {
ldisc = MSTSDISC;
} else {
syslog(LOG_ERR, "unknown line discipline %s", disc);
goto bail_out;
}
syslog(LOG_INFO, "attach %s on %s", disc, dev);
if ((fd = open(dev, O_RDWR)) < 0)
goto bail_out;
/*
* Get the current line attributes, modify only values that are
* either requested on the command line or that are needed by
* the line discipline (e.g. nmea has a default baudrate of
* 4800 instead of 9600).
*/
if (tcgetattr(fd, &tty) < 0) {
if (ppid != 1)
warnx("tcgetattr");
goto bail_out;
}
if (bits == 7) {
tty.c_cflag &= ~CS8;
tty.c_cflag |= CS7;
} else if (bits == 8) {
tty.c_cflag &= ~CS7;
tty.c_cflag |= CS8;
}
if (parity != 0)
tty.c_cflag |= PARENB;
if (parity == 'o')
tty.c_cflag |= PARODD;
else
tty.c_cflag &= ~PARODD;
if (stop == 2)
tty.c_cflag |= CSTOPB;
else
tty.c_cflag &= ~CSTOPB;
if (flowcl)
tty.c_cflag |= CRTSCTS;
if (hupcl == 0)
tty.c_cflag &= ~HUPCL;
if (speed != 0)
cfsetspeed(&tty, speed);
/* setup common to all line disciplines */
if (ioctl(fd, TIOCSDTR, 0) < 0)
warn("TIOCSDTR");
if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
syslog(LOG_ERR, "can't set the %s line discipline on %s", disc,
dev);
goto bail_out;
}
/* line discpline specific setup */
switch (ldisc) {
case NMEADISC:
if (ioctl(fd, TIOCSTSTAMP, &tstamps) < 0) {
warnx("TIOCSTSTAMP");
goto bail_out;
}
break;
case SLIPDISC:
tty.c_iflag = 0;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
break;
}
/* finally set the line attributes */
if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
if (ppid != 1)
warnx("tcsetattr");
goto bail_out;
}
if (!nodaemon && daemon(0, 0))
errx(1, "can't daemonize");
signal(SIGHUP, coroner);
signal(SIGTERM, coroner);
sigemptyset(&sigset);
while (!dying)
sigsuspend(&sigset);
bail_out:
if (ppid == 1)
sleep(30); /* delay restart when called from init */
return 0;
}
/* ARGSUSED */
void
coroner(int useless)
{
dying = 1;
}
|