3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <bluetooth/bluetooth.h>
48 #include <bluetooth/rfcomm.h>
53 #define PROC_BASE "/proc"
55 static int for_each_port(int (*func)(struct rfcomm_dev_info *, unsigned long), unsigned long arg)
57 struct rfcomm_dev_list_req *dl;
58 struct rfcomm_dev_info *di;
62 sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
64 perror("Can't open RFCOMM control socket");
68 dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
70 perror("Can't allocate request memory");
75 dl->dev_num = RFCOMM_MAX_DEV;
78 if (ioctl(sk, RFCOMMGETDEVLIST, (void *) dl) < 0) {
79 perror("Can't get device list");
83 for (i = 0; i < dl->dev_num; i++) {
84 r = func(di + i, arg);
93 static int uses_rfcomm(char *path, char *dev)
105 while ((de = readdir(dir)) != NULL) {
106 char link[PATH_MAX + 1];
107 int len = readlink(de->d_name, link, PATH_MAX);
110 if (strstr(link, dev)) {
122 static int find_pppd(int id, pid_t *pid)
125 char path[PATH_MAX + 1];
130 dir = opendir(PROC_BASE);
136 sprintf(dev, "rfcomm%d", id);
139 while ((de = readdir(dir)) != NULL) {
141 if (isdigit(de->d_name[0])) {
142 sprintf(path, "%s/%s/fd", PROC_BASE, de->d_name);
143 if (uses_rfcomm(path, dev)) {
144 *pid = atoi(de->d_name);
152 fprintf(stderr, "%s is empty (not mounted ?)\n", PROC_BASE);
157 static int dun_exec(char *tty, char *prog, char **args)
176 for (fd = 3; fd < 20; fd++)
181 syslog(LOG_ERR, "Error while executing %s", prog);
186 static int dun_create_tty(int sk, char *tty, int size)
188 struct sockaddr_rc sa;
193 struct rfcomm_dev_req req = {
194 .flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
199 if (getpeername(sk, (struct sockaddr *) &sa, &alen) < 0)
201 bacpy(&req.dst, &sa.rc_bdaddr);
204 if (getsockname(sk, (struct sockaddr *) &sa, &alen) < 0)
206 bacpy(&req.src, &sa.rc_bdaddr);
207 req.channel = sa.rc_channel;
209 id = ioctl(sk, RFCOMMCREATEDEV, &req);
213 snprintf(tty, size, "/dev/rfcomm%d", id);
214 while (stat(tty, &st) < 0) {
215 snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id);
216 if (stat(tty, &st) < 0) {
217 snprintf(tty, size, "/dev/rfcomm%d", id);
223 memset(&req, 0, sizeof(req));
225 ioctl(sk, RFCOMMRELEASEDEV, &req);
239 int dun_cleanup(void)
244 static int show_conn(struct rfcomm_dev_info *di, unsigned long arg)
248 if (di->state == BT_CONNECTED &&
249 (di->flags & (1<<RFCOMM_REUSE_DLC)) &&
250 (di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
251 (di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
253 if (find_pppd(di->id, &pid)) {
255 ba2str(&di->dst, dst);
257 printf("rfcomm%d: %s channel %d pppd pid %d\n",
258 di->id, dst, di->channel, pid);
264 static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg)
266 bdaddr_t *dst = (bdaddr_t *) arg;
269 if (di->state == BT_CONNECTED &&
270 (di->flags & (1<<RFCOMM_REUSE_DLC)) &&
271 (di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
272 (di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
274 if (dst && bacmp(&di->dst, dst))
277 if (find_pppd(di->id, &pid)) {
278 if (kill(pid, SIGINT) < 0)
289 int dun_show_connections(void)
291 for_each_port(show_conn, 0);
295 int dun_kill_connection(uint8_t *dst)
297 for_each_port(kill_conn, (unsigned long) dst);
301 int dun_kill_all_connections(void)
303 for_each_port(kill_conn, 0);
307 int dun_open_connection(int sk, char *pppd, char **args, int wait)
312 if (dun_create_tty(sk, tty, sizeof(tty) - 1) < 0) {
313 syslog(LOG_ERR, "RFCOMM TTY creation failed. %s(%d)", strerror(errno), errno);
319 args[2] = "nodetach";
321 pid = dun_exec(tty, pppd, args);
323 syslog(LOG_ERR, "Exec failed. %s(%d)", strerror(errno), errno);
329 waitpid(pid, &status, 0);
330 /* FIXME: Check for waitpid errors */