-
Notifications
You must be signed in to change notification settings - Fork 41
/
tty_bus.c
271 lines (235 loc) · 7.04 KB
/
tty_bus.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
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "configure.h"
#define MAX_TTY 256
#define BUFFER_SIZE 4096
#define POLL_R_TIMEOUT 100
#define POLL_W_TIMEOUT 50
static char *tty_bus_path = NULL;
static void usage(char *app) {
fprintf(stderr, "%s, Ver %s.%s.%s\n", basename(app), MAJORV, MINORV, SVNVERSION);
fprintf(stderr, "Usage: %s [-h] [-s bus_path]\n", app);
fprintf(stderr, "-h: shows this help\n");
fprintf(stderr, "-d: detach from terminal and run as daemon\n");
fprintf(stderr, "-s bus_path: uses bus_path as bus path name (default: /tmp/ttybus)\n\n");
fprintf(stderr, "Please also see: tty_attach, tty_fake, tty_plug, dpipe\n");
fprintf(stderr, "Example of usage:\n");
fprintf(stderr, " Create a new bus called /tmp/ttyS0mux\n");
fprintf(stderr, " tty_bus -d -s /tmp/ttyS0mux\n");
fprintf(stderr, " Connect a real device to the bus /tmp/ttyS0mux\n");
fprintf(stderr, " tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0\n");
fprintf(stderr, " Create two fake ttyS0 devices, attached to the bus /tmp/ttyS0mux\n");
fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.0\n");
fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.1\n");
exit(2);
}
void exiting(void) {
unlink(tty_bus_path);
}
void signaled(int signo) {
exit(0);
}
int bus_init(char *path) {
struct sockaddr_un sun;
int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0);
memset(&sun, 0, sizeof(struct sockaddr_un));
sun.sun_family = AF_UNIX;
strncpy(sun.sun_path, path, strlen(path));
if (bind(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
if ((errno == EADDRINUSE)) {
printf("Could not bind to socket '%s': %s\n", path, strerror(errno));
syslog(LOG_ERR, "Could not bind to socket '%s': %s\n", path, strerror(errno));
exit(-1);
} else if (bind(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
printf("Could not bind to socket '%s' (second attempt): %s", path, strerror(errno));
syslog(LOG_ERR, "Could not bind to socket '%s' (second attempt): %s", path, strerror(errno));
exit(-1);
}
}
chmod(sun.sun_path, 0777);
if (listen(connect_fd, 1) < 0) {
printf("Could not listen on fd %d: %s", connect_fd, strerror(errno));
syslog(LOG_ERR, "Could not listen on fd %d: %s", connect_fd, strerror(errno));
exit(-1);
}
return connect_fd;
}
void bus_destroy(int fd, char *path) {
close(fd);
unlink(path);
}
int prepare_poll(int *tty, struct pollfd **ppfd, int lfd, int flags) {
struct pollfd *pfd = (struct pollfd *) *ppfd;
int i, fdcount = 0;
if (lfd != -1)
pfd[fdcount++].fd = lfd;
for (i = 0; i < MAX_TTY; i++) {
if (tty[i] != -1)
pfd[fdcount++].fd = tty[i];
}
for (i = 0; i < fdcount; i++)
pfd[i].events = flags;
return fdcount;
}
void init_dev_array(int **ptty) {
int i;
int *tty = *ptty;
for (i = 0; i < MAX_TTY; i++)
tty[i] = -1;
}
int check_poll_errors(struct pollfd *pfd, int n, int *tty) {
int i, j;
int err = 0;
for (i = 0; i < n; i++) {
if (pfd[i].revents & POLLHUP || pfd[i].revents & POLLERR || pfd[i].revents & POLLNVAL) {
for (j = 0; j < MAX_TTY; j++) {
if (tty[j] == pfd[i].fd) {
close(tty[j]);
tty[j] = -1;
++err;
break;
}
}
}
}
return err;
}
void recvbuff(int src, char *buf, int size, int *tty) {
struct pollfd *wpfd;
int n, i;
int pollret;
wpfd = (struct pollfd *) malloc(sizeof(struct pollfd) * MAX_TTY);
if (!wpfd) {
fprintf(stderr, "alloc error: %s\n", strerror(errno));
syslog(LOG_INFO, "alloc error: %s\n", strerror(errno));
}
n = prepare_poll(tty, (struct pollfd **) &wpfd, -1, POLLOUT | POLLHUP);
printf("Writing to %d clients: %d bytes\n", n, size);
if (n > 1) {
pollret = poll(wpfd, n, POLL_W_TIMEOUT);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
syslog(LOG_WARNING, "Poll error: %s\n", strerror(errno));
sleep(1);
return;
}
(void) check_poll_errors(wpfd, n, tty);
for (i = 0; i < n; i++) {
if (wpfd[i].revents & POLLOUT && wpfd[i].fd != src)
write(wpfd[i].fd, buf, size);
}
}
free(wpfd);
}
int main(int argc, char *argv[]) {
int n = 0;
int listenfd = -1;
int i, r = 0;
int pollret;
struct pollfd *pfd;
int *tty;
char buffer[BUFFER_SIZE];
int daemonize = 0;
pfd = (struct pollfd *) malloc(sizeof(struct pollfd) * (1 + MAX_TTY));
tty = (int *) malloc(sizeof(int) * MAX_TTY);
if (!pfd || !tty) {
fprintf(stderr, "alloc error: %s\n", strerror(errno));
syslog(LOG_ERR, "alloc error: %s\n", strerror(errno));
exit(4);
}
while (1) {
int c;
c = getopt(argc, argv, "dhs:");
if (c == -1)
break;
switch (c) {
case 'd':
daemonize = 1;
break;
case 'h':
usage(argv[0]); // implies exit
break;
case 's':
tty_bus_path = strdup(optarg);
break;
default:
usage(argv[0]); // implies exit
}
}
if (optind < argc)
usage(argv[0]); // implies exit
if (daemonize)
daemon(0, 0);
if (!tty_bus_path)
tty_bus_path = strdup("/tmp/ttybus");
fprintf(stderr, "Creating bus: %s\n", tty_bus_path);
syslog(LOG_INFO, "Creating bus: %s\n", tty_bus_path);
atexit(exiting);
sigset(SIGTERM, signaled);
sigset(SIGINT, signaled);
init_dev_array((int **) &tty);
listenfd = bus_init(tty_bus_path);
if (listenfd < 0) {
fprintf(stderr, "Cannot bind to %s: %s\n", tty_bus_path, strerror(errno));
syslog(LOG_ERR, "Cannot bind to %s: %s\n", tty_bus_path, strerror(errno));
exit(1);
}
for (;;) {
n = prepare_poll(tty, (struct pollfd **) &pfd, listenfd, POLLIN | POLLHUP);
pollret = poll(pfd, n, POLL_R_TIMEOUT);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s, n is %d\n", strerror(errno), n);
syslog(LOG_WARNING, "Poll error: %s, n is %d\n", strerror(errno), n);
sleep(1);
continue;
}
if (check_poll_errors(pfd, n, tty) > 0)
continue;
if (pfd[0].revents & POLLIN) {
struct sockaddr_un cliaddr;
socklen_t len = sizeof(struct sockaddr_un);
int connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
if (connfd == -1) {
bus_destroy(listenfd, tty_bus_path);
listenfd = bus_init(tty_bus_path);
} else {
for (i = 0; i < MAX_TTY; i++)
if (tty[i] == -1) {
tty[i] = connfd;
break;
}
}
continue;
}
if (pollret > 0) {
for (;;) {
if (pfd[i].revents & POLLIN) {
r = read(pfd[i].fd, buffer, BUFFER_SIZE);
if (r > 0)
recvbuff(pfd[i].fd, buffer, r, tty);
break;
}
if (++i >= n)
i = 1;
}
}
}
}