-
Notifications
You must be signed in to change notification settings - Fork 1
/
montty.c
283 lines (266 loc) · 6.68 KB
/
montty.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
/*
* Unobtrusively log data coming in on a serial device.
*
* Diomidis Spinellis, December 2001 - July 2016
*
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <poll.h>
#include <syslog.h>
#include <string.h>
#include <termios.h>
static int lockpid;
/*
* Set terminal fd speed to s; clear non-blocking mode to make poll work
*/
static void
init_term(int fd, int s)
{
struct termios tio;
int val;
if (tcgetattr(fd, &tio) < 0) {
syslog(LOG_ERR, "unable get termios: %m");
exit(1);
}
tio.c_iflag |= IGNBRK;
tio.c_iflag &= ~(ICRNL | IMAXBEL | IXON | BRKINT);
tio.c_oflag &= ~(OPOST | ONLCR);
tio.c_cflag |= CS8;
tio.c_cflag &= ~(CRTSCTS);
tio.c_lflag |= NOFLSH;
tio.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
if (cfsetspeed(&tio, s) < 0) {
syslog(LOG_ERR, "unable to set speed to %d: %m", s);
exit(1);
}
if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
syslog(LOG_ERR, "unable set termios: %m");
exit(1);
}
if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
syslog(LOG_ERR, "fcntl(F_GETFL) failed: %m");
exit(1);
}
val &= ~O_NONBLOCK;
if (fcntl(fd, F_SETFL, val) < 0) {
syslog(LOG_ERR, "fcntl(F_SETFL) failed: %m");
exit(1);
}
}
/*
* Expand src to dst
*/
static void
expand(char *src, char *dst, int len)
{
int n = 0;
char *p;
for (p = src, n = 0; *p && n < len - 1; p++, n++)
if (*p == '\\')
switch (*++p) {
case '\\': *dst++ = '\\'; break;
case 'a': *dst++ = '\a'; break;
case 'b': *dst++ = '\b'; break;
case 'f': *dst++ = '\f'; break;
case 't': *dst++ = '\t'; break;
case 'r': *dst++ = '\r'; break;
case 'n': *dst++ = '\n'; break;
case 'v': *dst++ = '\v'; break;
case '0': *dst++ = '\0'; break;
default:
syslog(LOG_ERR, "invalid escape in %s", src);
exit(1);
}
else
*dst++ = *p;
*dst = 0;
}
/* First argv used for initialisation */
#define INIT_ARGV 2
#ifndef UU_LOCK_OK
/* Return values from uu_lock(). */
#define UU_LOCK_INUSE 1
#define UU_LOCK_OK 0
#define UU_LOCK_OPEN_ERR (-1)
#define UU_LOCK_READ_ERR (-2)
#define UU_LOCK_CREAT_ERR (-3)
#define UU_LOCK_WRITE_ERR (-4)
#define UU_LOCK_LINK_ERR (-5)
#define UU_LOCK_TRY_ERR (-6)
#define UU_LOCK_OWNER_ERR (-7)
static int
uu_lock(const char *ttyname)
{
char try[1024];
char final[1024];
int fd;
char buff[50];
snprintf(try, sizeof(try), "/var/lock/LCK..%s.%d", ttyname,
lockpid);
snprintf(final, sizeof(final), "/var/lock/LCK..%s", ttyname);
(void)unlink(try);
if ((fd = open(try, O_CREAT | O_TRUNC | O_WRONLY, 0755)) == -1)
return UU_LOCK_CREAT_ERR;
sprintf(buff, "%d\n", lockpid);
if (write(fd, buff, strlen(buff)) != strlen(buff)) {
close(fd);
return UU_LOCK_WRITE_ERR;
}
close(fd);
if (rename(try, final) == -1) {
(void)unlink(try);
return UU_LOCK_INUSE;
} else {
(void)unlink(try);
return UU_LOCK_OK;
}
}
static int
uu_unlock(const char *ttyname)
{
char lockname[1024];
snprintf(lockname, sizeof(lockname), "/var/lock/LCK..%s", ttyname);
return unlink(lockname);
}
static const char *
uu_lockerr(int e)
{
switch (e) {
case UU_LOCK_INUSE:
return "Device is in use";
case UU_LOCK_OPEN_ERR:
return "File open error";
case UU_LOCK_READ_ERR:
return "File read error";
case UU_LOCK_CREAT_ERR:
return "File creation error";
case UU_LOCK_WRITE_ERR:
return "File write error";
case UU_LOCK_LINK_ERR:
return "File link error";
case UU_LOCK_TRY_ERR:
return "File try error";
case UU_LOCK_OWNER_ERR:
return "File owner error";
}
}
#endif
main(int argc, char *argv[])
{
FILE *f;
char logname[1024];
char buff[1024];
char devname[1024];
struct pollfd pfd[1];
int n;
/* True when initialisation strings must be sent */
int need_init = 1;
int init_index = INIT_ARGV;
int lockresult;
if (argc < 2) {
fprintf(stderr, "usage: %s line [initialisation string] ...\n"
"e.g. %s ttyACM0 'ATS82=76\\r\\n'\n", argv[0], argv[0]);
exit(1);
}
daemon(0, 0);
lockpid = getpid();
snprintf(logname, sizeof(logname), "montty.%s", argv[1]);
openlog(logname, 0, LOG_LOCAL0);
syslog(LOG_INFO, "starting up: pid %d", getpid());
snprintf(buff, sizeof(buff), "/var/run/montty.%s.pid", argv[1]);
if ((f = fopen(buff, "w")) == NULL) {
syslog(LOG_ERR, "unable to open pid file %s: %m", buff);
exit(1);
} else {
fprintf(f, "%d\n", getpid());
fclose(f);
}
snprintf(devname, sizeof(devname), "/dev/%s", argv[1]);
if ((pfd[0].fd = open(devname, O_RDWR | O_NONBLOCK)) < 0) {
syslog(LOG_ERR, "unable to open monitor file %s: %m", devname);
exit(1);
}
syslog(LOG_INFO, "monitoring %s", devname);
pfd[0].events = POLLIN | POLLRDNORM | POLLERR;
for (;;) {
if (!need_init) {
/* No initialisation needed, just wait for input */
syslog(LOG_DEBUG, "waiting for input");
if (poll(pfd, 1, -1) < 0) {
syslog(LOG_ERR, "poll(INFTIM) failed: %m");
exit(1);
}
}
/*
* We have input, or we need to initialise the device;
* acquire a lock.
*/
switch (lockresult = uu_lock(argv[1])) {
case UU_LOCK_OK:
syslog(LOG_DEBUG, "acquired lock");
/*
* Now that we have the lock,
* check if we need to write the initialisation data.
*/
if (need_init) {
if (init_index == INIT_ARGV) {
syslog(LOG_DEBUG, "recycle fd");
/* Refresh fd */
close(pfd[0].fd);
if ((pfd[0].fd = open(devname, O_RDWR | O_NONBLOCK)) < 0) {
syslog(LOG_ERR, "unable to re-open monitor file %s: %m", devname);
exit(1);
}
init_term(pfd[0].fd, B115200);
}
if (init_index < argc) {
expand(argv[init_index], buff, sizeof(buff));
syslog(LOG_DEBUG, "write: %s", buff);
if (write(pfd[0].fd, buff, strlen(buff)) != strlen(buff)) {
syslog(LOG_ERR, "write failed: %m");
exit(1);
}
init_index++;
sleep(1);
}
if (init_index == argc)
need_init = 0;
syslog(LOG_DEBUG, "sent init string");
}
/* Check if there is still something to read. */
if (poll(pfd, 1, 0) < 0) {
syslog(LOG_ERR, "poll(0) failed: %m");
exit(1);
}
if (pfd[0].revents & (POLLIN | POLLRDNORM)) {
if ((n = read(pfd[0].fd, buff, sizeof(buff))) == -1) {
syslog(LOG_ERR, "read failed: %m");
exit(1);
}
syslog(LOG_DEBUG, "read %d bytes", n);
buff[n] = 0;
syslog(LOG_INFO, "%s", buff);
}
if (uu_unlock(argv[1]) == -1) {
syslog(LOG_ERR, "uu_unlock error %m");
exit(1);
}
syslog(LOG_DEBUG, "lock released");
break;
case UU_LOCK_INUSE:
syslog(LOG_DEBUG, "lock in use; sleeping");
sleep(1);
/* Someone is using the device, we must re-init it */
need_init = 1;
init_index = INIT_ARGV;
break;
default:
syslog(LOG_ERR, "uu_lock error %s", uu_lockerr(lockresult));
}
}
}