kernel bug?

Aleksandar Kanchev <[email protected]>
Newsgroups gmane.linux.bluez.devel
Message-ID <[email protected]>
Hello list,

recently I've stumbled upon something that I think might be a bluez bug.
My nokia phone was messed up, so it dropped rfcomm connections 
immediately after they were established. This caused my glibmm (glib2) 
based program to exit because of a glib error. After researching a bit I 
found out, that I was flushing a glib io channel after the channel was 
polled for IO_OUT, which is right. The flush caused a write() to the 
socket and glib expects write() to return either a negative number (an 
error) or positive > 0, a return = 0 is not expected probably because 
one should first wait for the IO_OUT event.
What was happening is that the connection was being dropped right after 
the socket was polled and this caused glib to crash most of the time. 
Should't bluez close the socket and return < 0 instead of returning 0?
I've attached a small program to better demonstrate what happened. I've 
also attached a copy of the email which I sent to the gtkmm mailing 
list, since I first thought it was a glib issue.

Thanks

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel
bluez-bug.c (text/x-csrc, 1.4 KB)
/*
 * write() should not return 0 after
 * the remote device has terminated the connetion.
 * This makes glib's io_channel terminate
 * the running program.
 *                [email protected]
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>


int main(int argc, char **argv)
{
	char dummy;
	int sock, r;
	struct sockaddr_rc saddr;
	struct pollfd pollfd;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s <bluetooth address> <rfcomm channel>\n", argv[0]);
		return -1;
	}

	if ((sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
		perror("socket()");
		return -1;
	}

	memset(&saddr, 0, sizeof(struct sockaddr_rc));
	str2ba(argv[1], &saddr.rc_bdaddr);
	saddr.rc_family = AF_BLUETOOTH;
    saddr.rc_channel = (uint8_t) atoi(argv[2]);

	printf("Connecting to %s ...\n", argv[1]);
	if (connect(sock, (struct sockaddr *) &saddr, sizeof(struct sockaddr_rc)) < 0) {
		close(sock);
		perror("connect()");
		return -1;
	}

	pollfd.fd = sock;
	pollfd.events = POLLOUT;
	pollfd.revents = 0;
	if (poll(&pollfd, 1, -1) <= 0) {
		close(sock);
		perror("poll()");
		return -1;
	}

	printf("Turn off the bluetooth device %s now!\nPress enter to continue...", argv[1]);
	fflush(stdout);
	read(0, &dummy, 1);

	r = write(sock, "Hello", 5);
	printf("write() returned %d\n", r);

	close(sock);

	return 0;
}
Attached Message (message/rfc822, 2.3 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.