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
|
/*
* KISS PTY bridge for max25-bcpr (HyBBX attach via kiss_link symlink).
*/
#define _GNU_SOURCE
#include "bcpr/bcpr_kiss.h"
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#if defined(__linux__)
#include <pty.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#else
#include <util.h>
#endif
static int ensure_dir(const char *path)
{
char tmp[256];
char *p;
size_t len;
if (!path || !path[0]) {
return -1;
}
snprintf(tmp, sizeof(tmp), "%s", path);
len = strlen(tmp);
if (len == 0) {
return -1;
}
if (tmp[len - 1] == '/') {
tmp[len - 1] = '\0';
}
for (p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
(void)mkdir(tmp, 0755);
*p = '/';
}
}
return mkdir(tmp, 0755) == 0 || errno == EEXIST ? 0 : -1;
}
int bcpr_kiss_pty_open(bcpr_kiss_pty_t *kp, int idx, const char *link_path,
const char *state_dir)
{
int master = -1, slave = -1;
char slave_name[128];
char dir[128];
const char *slash;
memset(kp, 0, sizeof(*kp));
kp->idx = idx;
kp->master_fd = -1;
kp->slave_fd = -1;
snprintf(kp->link_path, sizeof(kp->link_path), "%s", link_path);
slash = strrchr(link_path, '/');
if (slash && slash > link_path) {
size_t n = (size_t)(slash - link_path);
if (n >= sizeof(dir)) {
n = sizeof(dir) - 1;
}
memcpy(dir, link_path, n);
dir[n] = '\0';
(void)ensure_dir(dir);
} else {
(void)ensure_dir(state_dir);
}
if (openpty(&master, &slave, slave_name, NULL, NULL) != 0) {
perror("max25-bcpr openpty");
return -1;
}
if (fchmod(slave, 0660) != 0) {
perror("max25-bcpr fchmod kiss slave");
} else {
struct group *gr = getgrnam("dialout");
if (gr == NULL) {
gr = getgrnam("uucp");
}
if (gr == NULL) {
gr = getgrnam("tty");
}
if (gr != NULL && fchown(slave, (uid_t)-1, gr->gr_gid) != 0) {
perror("max25-bcpr fchown kiss slave");
}
}
unlink(link_path);
if (symlink(slave_name, link_path) != 0) {
perror("max25-bcpr symlink kiss_link");
close(slave);
close(master);
return -1;
}
fcntl(master, F_SETFL, O_NONBLOCK);
kp->master_fd = master;
close(slave);
kp->slave_fd = -1;
fprintf(stderr, "max25-bcpr: max25e0:bc%d KISS → %s -> %s\n", idx, link_path,
slave_name);
return 0;
}
void bcpr_kiss_pty_close(bcpr_kiss_pty_t *kp)
{
if (!kp) {
return;
}
if (kp->slave_fd >= 0) {
close(kp->slave_fd);
kp->slave_fd = -1;
}
if (kp->master_fd >= 0) {
close(kp->master_fd);
kp->master_fd = -1;
}
if (kp->link_path[0]) {
unlink(kp->link_path);
}
}
|