-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtty_attach.c
179 lines (161 loc) · 4.97 KB
/
tty_attach.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
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.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/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;
static char *devname;
static char *init_string;
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] tty_device\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");
fprintf(stderr, "-i init_string: send init string to device\n\n");
fprintf(stderr, "Please also see: tty_bus, 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);
}
int tty_connect(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 (connect(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
perror("Cannot connect to socket");
syslog(LOG_ERR, "Cannot connect to socket");
exit(-1);
}
return connect_fd;
}
int main(int argc, char *argv[]) {
int fd;
struct pollfd pfd[2];
int pollret, r;
char buffer[BUFFER_SIZE];
int realdev;
int daemonize = 0;
while (1) {
int c;
c = getopt(argc, argv, "dhi:s:");
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;
case 'i':
init_string = strdup(optarg);
break;
default:
usage(argv[0]); // implies exit
}
}
if (optind != (argc - 1))
usage(argv[0]); // implies exit
if (daemonize)
daemon(0, 0);
devname = strdup(argv[optind]);
if (!tty_bus_path)
tty_bus_path = strdup("/tmp/ttybus");
fprintf(stderr, "Connecting to bus: %s\n", tty_bus_path);
syslog(LOG_INFO, "Connecting to bus: %s\n", tty_bus_path);
fd = tty_connect(tty_bus_path);
realdev = open(devname, O_RDWR);
if (realdev < 0) {
perror("opening device");
exit(3);
}
if (init_string) {
pfd[0].fd = realdev;
pfd[0].events = POLLOUT;
pollret = poll(&pfd[0], 1, 50);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
syslog(LOG_ERR, "Poll error: %s\n", strerror(errno));
exit(1);
}
if (pfd[0].revents & POLLOUT) {
write(realdev, init_string, strlen(init_string));
write(realdev, "\n", 1);
} else {
fprintf(stderr, "Device is busy, cannot send init string.\n");
syslog(LOG_WARNING, "Device is busy, cannot send init string.\n");
}
}
for (;;) {
pfd[0].fd = realdev;
pfd[0].events = POLLIN;
pfd[1].fd = fd;
pfd[1].events = POLLIN;
pollret = poll(pfd, 2, 1000);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
syslog(LOG_ERR, "Poll error: %s\n", strerror(errno));
exit(1);
}
if (pollret == 0)
continue;
if ((pfd[0].revents & POLLHUP || pfd[0].revents & POLLERR || pfd[0].revents & POLLNVAL) ||
(pfd[1].revents & POLLHUP || pfd[1].revents & POLLERR || pfd[1].revents & POLLNVAL))
exit(1);
if (pfd[0].revents & POLLIN) {
r = read(realdev, buffer, BUFFER_SIZE);
pfd[1].events = POLLOUT;
pollret = poll(&pfd[1], 1, 50);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
syslog(LOG_ERR, "Poll error: %s\n", strerror(errno));
exit(1);
}
if (pfd[1].revents & POLLOUT)
write(fd, buffer, r);
}
if (pfd[1].revents & POLLIN) {
r = read(fd, buffer, BUFFER_SIZE);
pfd[0].fd = realdev;
pfd[0].events = POLLOUT;
pollret = poll(&pfd[0], 1, 50);
if (pollret < 0) {
fprintf(stderr, "Poll error: %s\n", strerror(errno));
syslog(LOG_ERR, "Poll error: %s\n", strerror(errno));
exit(1);
}
if (pfd[0].revents & POLLOUT)
write(realdev, buffer, r);
}
}
}