Lines 32-37
Link Here
|
32 |
*/ |
32 |
*/ |
33 |
|
33 |
|
34 |
#include <sys/stat.h> |
34 |
#include <sys/stat.h> |
|
|
35 |
#include <sys/types.h> |
35 |
#include <bluetooth.h> |
36 |
#include <bluetooth.h> |
36 |
#include <ctype.h> |
37 |
#include <ctype.h> |
37 |
#include <err.h> |
38 |
#include <err.h> |
Lines 49-54
Link Here
|
49 |
#include <syslog.h> |
50 |
#include <syslog.h> |
50 |
#include <termios.h> |
51 |
#include <termios.h> |
51 |
#include <unistd.h> |
52 |
#include <unistd.h> |
|
|
53 |
#include <libutil.h> |
52 |
|
54 |
|
53 |
#define SPPD_IDENT "rfcomm_sppd" |
55 |
#define SPPD_IDENT "rfcomm_sppd" |
54 |
#define SPPD_BUFFER_SIZE 1024 |
56 |
#define SPPD_BUFFER_SIZE 1024 |
Lines 58-64
int rfcomm_channel_lookup (bdaddr_t const *local,
Link Here
|
58 |
bdaddr_t const *remote, |
60 |
bdaddr_t const *remote, |
59 |
int service, int *channel, int *error); |
61 |
int service, int *channel, int *error); |
60 |
|
62 |
|
61 |
static int sppd_ttys_open (char const *tty, int *amaster, int *aslave); |
63 |
static int sppd_ttys_open (char **tty, int *amaster, int *aslave); |
62 |
static int sppd_read (int fd, char *buffer, int size); |
64 |
static int sppd_read (int fd, char *buffer, int size); |
63 |
static int sppd_write (int fd, char *buffer, int size); |
65 |
static int sppd_write (int fd, char *buffer, int size); |
64 |
static void sppd_sighandler (int s); |
66 |
static void sppd_sighandler (int s); |
Lines 74-80
main(int argc, char *argv[])
Link Here
|
74 |
struct sockaddr_rfcomm ra; |
76 |
struct sockaddr_rfcomm ra; |
75 |
bdaddr_t addr; |
77 |
bdaddr_t addr; |
76 |
int n, background, channel, service, |
78 |
int n, background, channel, service, |
77 |
s, amaster, aslave, fd, doserver; |
79 |
s, amaster, aslave, fd, doserver, |
|
|
80 |
dopty; |
78 |
fd_set rfd; |
81 |
fd_set rfd; |
79 |
char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE]; |
82 |
char *tty = NULL, *ep = NULL, buf[SPPD_BUFFER_SIZE]; |
80 |
|
83 |
|
Lines 82-90
main(int argc, char *argv[])
Link Here
|
82 |
background = channel = 0; |
85 |
background = channel = 0; |
83 |
service = SDP_SERVICE_CLASS_SERIAL_PORT; |
86 |
service = SDP_SERVICE_CLASS_SERIAL_PORT; |
84 |
doserver = 0; |
87 |
doserver = 0; |
|
|
88 |
dopty = 0; |
85 |
|
89 |
|
86 |
/* Parse command line options */ |
90 |
/* Parse command line options */ |
87 |
while ((n = getopt(argc, argv, "a:bc:t:hS")) != -1) { |
91 |
while ((n = getopt(argc, argv, "a:bc:thS")) != -1) { |
88 |
switch (n) { |
92 |
switch (n) { |
89 |
case 'a': /* BDADDR */ |
93 |
case 'a': /* BDADDR */ |
90 |
if (!bt_aton(optarg, &addr)) { |
94 |
if (!bt_aton(optarg, &addr)) { |
Lines 130-140
main(int argc, char *argv[])
Link Here
|
130 |
background = 1; |
134 |
background = 1; |
131 |
break; |
135 |
break; |
132 |
|
136 |
|
133 |
case 't': /* Slave TTY name */ |
137 |
case 't': /* Open pseudo TTY */ |
134 |
if (optarg[0] != '/') |
138 |
dopty = 1; |
135 |
asprintf(&tty, "%s%s", _PATH_DEV, optarg); |
|
|
136 |
else |
137 |
tty = optarg; |
138 |
break; |
139 |
break; |
139 |
|
140 |
|
140 |
case 'S': |
141 |
case 'S': |
Lines 173-190
main(int argc, char *argv[])
Link Here
|
173 |
err(1, "Could not sigaction(SIGCHLD)"); |
174 |
err(1, "Could not sigaction(SIGCHLD)"); |
174 |
|
175 |
|
175 |
/* Open TTYs */ |
176 |
/* Open TTYs */ |
176 |
if (tty == NULL) { |
177 |
if (dopty) { |
|
|
178 |
if (sppd_ttys_open(&tty, &amaster, &aslave) < 0) |
179 |
exit(1); |
180 |
|
181 |
fd = amaster; |
182 |
} else { |
177 |
if (background) |
183 |
if (background) |
178 |
usage(); |
184 |
usage(); |
179 |
|
185 |
|
180 |
amaster = STDIN_FILENO; |
186 |
amaster = STDIN_FILENO; |
181 |
fd = STDOUT_FILENO; |
187 |
fd = STDOUT_FILENO; |
182 |
} else { |
188 |
} |
183 |
if (sppd_ttys_open(tty, &amaster, &aslave) < 0) |
|
|
184 |
exit(1); |
185 |
|
186 |
fd = amaster; |
187 |
} |
188 |
|
189 |
|
189 |
/* Open RFCOMM connection */ |
190 |
/* Open RFCOMM connection */ |
190 |
|
191 |
|
Lines 287-292
main(int argc, char *argv[])
Link Here
|
287 |
openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); |
288 |
openlog(SPPD_IDENT, LOG_NDELAY|LOG_PERROR|LOG_PID, LOG_DAEMON); |
288 |
syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout"); |
289 |
syslog(LOG_INFO, "Starting on %s...", (tty != NULL)? tty : "stdin/stdout"); |
289 |
|
290 |
|
|
|
291 |
/* Print used tty on stdout for wrappers to pick up */ |
292 |
if (!background) |
293 |
fprintf(stdout, "%s\n", tty); |
294 |
|
290 |
for (done = 0; !done; ) { |
295 |
for (done = 0; !done; ) { |
291 |
FD_ZERO(&rfd); |
296 |
FD_ZERO(&rfd); |
292 |
FD_SET(amaster, &rfd); |
297 |
FD_SET(amaster, &rfd); |
Lines 359-428
main(int argc, char *argv[])
Link Here
|
359 |
|
364 |
|
360 |
/* Open TTYs */ |
365 |
/* Open TTYs */ |
361 |
static int |
366 |
static int |
362 |
sppd_ttys_open(char const *tty, int *amaster, int *aslave) |
367 |
sppd_ttys_open(char **tty, int *amaster, int *aslave) |
363 |
{ |
368 |
{ |
364 |
char pty[PATH_MAX], *slash; |
369 |
char pty[PATH_MAX]; |
365 |
struct group *gr = NULL; |
|
|
366 |
gid_t ttygid; |
367 |
struct termios tio; |
370 |
struct termios tio; |
368 |
|
371 |
|
369 |
/* |
372 |
cfmakeraw(&tio); |
370 |
* Construct master PTY name. The slave tty name must be less then |
|
|
371 |
* PATH_MAX characters in length, must contain '/' character and |
372 |
* must not end with '/'. |
373 |
*/ |
374 |
|
375 |
if (strlen(tty) >= sizeof(pty)) { |
376 |
syslog(LOG_ERR, "Slave tty name is too long"); |
377 |
return (-1); |
378 |
} |
379 |
|
380 |
strlcpy(pty, tty, sizeof(pty)); |
381 |
slash = strrchr(pty, '/'); |
382 |
if (slash == NULL || slash[1] == '\0') { |
383 |
syslog(LOG_ERR, "Invalid slave tty name (%s)", tty); |
384 |
return (-1); |
385 |
} |
386 |
|
387 |
slash[1] = 'p'; |
388 |
|
389 |
if (strcmp(pty, tty) == 0) { |
390 |
syslog(LOG_ERR, "Master and slave tty are the same (%s)", tty); |
391 |
return (-1); |
392 |
} |
393 |
|
394 |
if ((*amaster = open(pty, O_RDWR, 0)) < 0) { |
395 |
syslog(LOG_ERR, "Could not open(%s). %s", pty, strerror(errno)); |
396 |
return (-1); |
397 |
} |
398 |
|
399 |
/* |
400 |
* Slave TTY |
401 |
*/ |
402 |
|
403 |
if ((gr = getgrnam("tty")) != NULL) |
404 |
ttygid = gr->gr_gid; |
405 |
else |
406 |
ttygid = -1; |
407 |
|
408 |
(void) chown(tty, getuid(), ttygid); |
409 |
(void) chmod(tty, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
410 |
(void) revoke(tty); |
411 |
|
373 |
|
412 |
if ((*aslave = open(tty, O_RDWR, 0)) < 0) { |
374 |
if (openpty(amaster, aslave, pty, &tio, NULL) == -1) { |
413 |
syslog(LOG_ERR, "Could not open(%s). %s", tty, strerror(errno)); |
375 |
syslog(LOG_ERR, "Could not openpty(). %s", strerror(errno)); |
414 |
close(*amaster); |
|
|
415 |
return (-1); |
376 |
return (-1); |
416 |
} |
377 |
} |
417 |
|
378 |
|
418 |
/* |
379 |
if ((*tty = strdup(pty)) == NULL) { |
419 |
* Make slave TTY raw |
380 |
syslog(LOG_ERR, "Could not strdup(). %s", strerror(errno)); |
420 |
*/ |
|
|
421 |
|
422 |
cfmakeraw(&tio); |
423 |
|
424 |
if (tcsetattr(*aslave, TCSANOW, &tio) < 0) { |
425 |
syslog(LOG_ERR, "Could not tcsetattr(). %s", strerror(errno)); |
426 |
close(*aslave); |
381 |
close(*aslave); |
427 |
close(*amaster); |
382 |
close(*amaster); |
428 |
return (-1); |
383 |
return (-1); |
Lines 496-502
usage(void)
Link Here
|
496 |
"\t-a address Peer address (required in client mode)\n" \ |
451 |
"\t-a address Peer address (required in client mode)\n" \ |
497 |
"\t-b Run in background\n" \ |
452 |
"\t-b Run in background\n" \ |
498 |
"\t-c channel RFCOMM channel to connect to or listen on\n" \ |
453 |
"\t-c channel RFCOMM channel to connect to or listen on\n" \ |
499 |
"\t-t tty TTY name (required in background mode)\n" \ |
454 |
"\t-t use slave pseudo tty (required in background mode)\n" \ |
500 |
"\t-S Server mode\n" \ |
455 |
"\t-S Server mode\n" \ |
501 |
"\t-h Display this message\n", SPPD_IDENT); |
456 |
"\t-h Display this message\n", SPPD_IDENT); |
502 |
exit(255); |
457 |
exit(255); |