3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
6 * Copyright (C) 2002-2009 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);
92 static int uses_rfcomm(char *path, char *dev)
104 while ((de = readdir(dir)) != NULL) {
105 char link[PATH_MAX + 1];
106 int len = readlink(de->d_name, link, sizeof(link));
109 if (strstr(link, dev))
119 static int find_pppd(int id, pid_t *pid)
122 char path[PATH_MAX + 1];
127 dir = opendir(PROC_BASE);
133 sprintf(dev, "rfcomm%d", id);
136 while ((de = readdir(dir)) != NULL) {
138 if (isdigit(de->d_name[0])) {
139 sprintf(path, "%s/%s/fd", PROC_BASE, de->d_name);
140 if (uses_rfcomm(path, dev)) {
141 *pid = atoi(de->d_name);
149 fprintf(stderr, "%s is empty (not mounted ?)\n", PROC_BASE);
154 static int dun_exec(char *tty, char *prog, char **args)
173 for (fd = 3; fd < 20; fd++)
178 syslog(LOG_ERR, "Error while executing %s", prog);
183 static int dun_create_tty(int sk, char *tty, int size)
185 struct sockaddr_rc sa;
190 struct rfcomm_dev_req req = {
191 flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
196 if (getpeername(sk, (struct sockaddr *) &sa, &alen) < 0)
198 bacpy(&req.dst, &sa.rc_bdaddr);
201 if (getsockname(sk, (struct sockaddr *) &sa, &alen) < 0)
203 bacpy(&req.src, &sa.rc_bdaddr);
204 req.channel = sa.rc_channel;
206 id = ioctl(sk, RFCOMMCREATEDEV, &req);
210 snprintf(tty, size, "/dev/rfcomm%d", id);
211 while (stat(tty, &st) < 0) {
212 snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id);
213 if (stat(tty, &st) < 0) {
214 snprintf(tty, size, "/dev/rfcomm%d", id);
220 memset(&req, 0, sizeof(req));
222 ioctl(sk, RFCOMMRELEASEDEV, &req);
236 int dun_cleanup(void)
241 static int show_conn(struct rfcomm_dev_info *di, unsigned long arg)
245 if (di->state == BT_CONNECTED &&
246 (di->flags & (1<<RFCOMM_REUSE_DLC)) &&
247 (di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
248 (di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
250 if (find_pppd(di->id, &pid)) {
252 ba2str(&di->dst, dst);
254 printf("rfcomm%d: %s channel %d pppd pid %d\n",
255 di->id, dst, di->channel, pid);
261 static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg)
263 bdaddr_t *dst = (bdaddr_t *) arg;
266 if (di->state == BT_CONNECTED &&
267 (di->flags & (1<<RFCOMM_REUSE_DLC)) &&
268 (di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
269 (di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
271 if (dst && bacmp(&di->dst, dst))
274 if (find_pppd(di->id, &pid)) {
275 if (kill(pid, SIGINT) < 0)
286 int dun_show_connections(void)
288 for_each_port(show_conn, 0);
292 int dun_kill_connection(uint8_t *dst)
294 for_each_port(kill_conn, (unsigned long) dst);
298 int dun_kill_all_connections(void)
300 for_each_port(kill_conn, 0);
304 int dun_open_connection(int sk, char *pppd, char **args, int wait)
309 if (dun_create_tty(sk, tty, sizeof(tty) - 1) < 0) {
310 syslog(LOG_ERR, "RFCOMM TTY creation failed. %s(%d)", strerror(errno), errno);
316 args[2] = "nodetach";
318 pid = dun_exec(tty, pppd, args);
320 syslog(LOG_ERR, "Exec failed. %s(%d)", strerror(errno), errno);
326 waitpid(pid, &status, 0);
327 /* FIXME: Check for waitpid errors */