All of lore.kernel.org
 help / color / mirror / Atom feed
* [RESEND] - Fixes rfcomm program error codes.
@ 2005-06-16 20:00 Luiz Fernando Capitulino
  2005-06-16 21:24 ` [Bluez-devel] " Marcel Holtmann
  0 siblings, 1 reply; 5+ messages in thread
From: Luiz Fernando Capitulino @ 2005-06-16 20:00 UTC (permalink / raw)
  To: bluez-devel, marcel

[-- Attachment #1: Type: text/plain, Size: 733 bytes --]

Hello all,

I sent this patch some days ago, but I saw that my e-mail client
took the bluez-users address. Hopes to not annoy you sending it
again (to the right place now).

rfcomm program does not return proper error codes to the environment,
this makes hard to use it from a shell script or from another program.

Here goes a patch to fix it. I didn't have much time to test it, so
any feedback is welcome.

- Changes functions to return -1 on error and 0 on success
- Adds missing error checks
- Adds missing error messages
- Fixes rfcomm exit() return codes

  rfcomm/main.c |  192 
+++++++++++++++++++++++++++++++++++++++-------------------
  1 files changed, 131 insertions(+), 61 deletions(-)

-- 
Luiz Fernando N. Capitulino

[-- Attachment #2: rfcomm_return_codes_1.patch --]
[-- Type: text/x-patch, Size: 11281 bytes --]


- Changes functions to return -1 on error and 0 on success
- Adds missing error checks
- Adds missing error messages
- Fixed rfcomm exit() return codes

 rfcomm/main.c |  192 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 131 insertions(+), 61 deletions(-)


diff -X /home/lcapitulino/kernels/2.6/dontdiff -Nparu a/rfcomm/main.c a~/rfcomm/main.c
--- a/rfcomm/main.c	2004-11-25 10:05:16.000000000 -0200
+++ a~/rfcomm/main.c	2005-06-12 20:09:07.000000000 -0300
@@ -120,7 +120,7 @@ static void print_dev_info(struct rfcomm
 		di->flags ? rfcomm_flagstostr(di->flags) : "");
 }
 
-static void print_dev_list(int ctl, int flags)
+static int print_dev_list(int ctl, int flags)
 {
 	struct rfcomm_dev_list_req *dl;
 	struct rfcomm_dev_info *di;
@@ -129,7 +129,7 @@ static void print_dev_list(int ctl, int 
 	dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
 	if (!dl) {
 		perror("Can't allocate memory");
-		exit(1);
+		return -1;
 	}
 
 	dl->dev_num = RFCOMM_MAX_DEV;
@@ -137,11 +137,13 @@ static void print_dev_list(int ctl, int 
 
 	if (ioctl(ctl, RFCOMMGETDEVLIST, (void *) dl) < 0) {
 		perror("Can't get device list");
-		exit(1);
+		return -1;
 	}
 
 	for (i = 0; i < dl->dev_num; i++)
 		print_dev_info(di + i);
+
+	return 0;
 }
 
 static int create_dev(int ctl, int dev, uint32_t flags, bdaddr_t *bdaddr, int argc, char **argv)
@@ -165,7 +167,7 @@ static int create_dev(int ctl, int dev, 
 
 		if (bacmp(&req.dst, BDADDR_ANY) == 0) {
 			fprintf(stderr, "Can't find a config entry for rfcomm%d\n", dev);
-			return -EFAULT;
+			return -1;
 		}
 		
 	} else {
@@ -205,7 +207,8 @@ static int create_all(int ctl)
 		req.channel = rfcomm_opts[i].channel;
 
 		if (bacmp(&req.dst, BDADDR_ANY) != 0)
-			ioctl(ctl, RFCOMMCREATEDEV, &req);
+			if (ioctl(ctl, RFCOMMCREATEDEV, &req) < 0)
+				perror("Can't create RFCOMM device");
 	}
 
 	return 0;
@@ -234,7 +237,7 @@ static int release_all(int ctl)
 	dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
 	if (!dl) {
 		perror("Can't allocate memory");
-		exit(1);
+		return -1;
 	}
 
 	dl->dev_num = RFCOMM_MAX_DEV;
@@ -242,7 +245,7 @@ static int release_all(int ctl)
 
 	if (ioctl(ctl, RFCOMMGETDEVLIST, (void *) dl) < 0) {
 		perror("Can't get device list");
-		exit(1);
+		return -1;
 	}
 
 	for (i = 0; i < dl->dev_num; i++)
@@ -251,7 +254,7 @@ static int release_all(int ctl)
 	return 0;
 }
 
-static void cmd_connect(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+static int cmd_connect(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
 	struct sockaddr_rc laddr, raddr;
 	struct rfcomm_dev_req req;
@@ -268,7 +271,7 @@ static void cmd_connect(int ctl, int dev
 	if (argc < 2) {
 		if (rfcomm_read_config(rfcomm_config_file) < 0) {
 			perror("Can't open RFCOMM config file");
-			return;
+			return -1;
 		}
 
 		raddr.rc_family = AF_BLUETOOTH;
@@ -277,7 +280,7 @@ static void cmd_connect(int ctl, int dev
 
 		if (bacmp(&raddr.rc_bdaddr, BDADDR_ANY) == 0) {
 			fprintf(stderr, "Can't find a config entry for rfcomm%d\n", dev);
-			return;
+			return -1;
 		}
 	} else {
 		raddr.rc_family = AF_BLUETOOTH;
@@ -291,26 +294,26 @@ static void cmd_connect(int ctl, int dev
 
 	if ((sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
 		perror("Can't create RFCOMM socket");
-		return;
+		return -1;
 	}
 
 	if (bind(sk, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) {
 		perror("Can't bind RFCOMM socket");
 		close(sk);
-		return;
+		return -1;
 	}
 
 	if (connect(sk, (struct sockaddr *)&raddr, sizeof(raddr)) < 0) {
 		perror("Can't connect RFCOMM socket");
 		close(sk);
-		return;
+		return -1;
 	}
 
 	alen = sizeof(laddr);
 	if (getsockname(sk, (struct sockaddr *)&laddr, &alen) < 0) {
 		perror("Can't get RFCOMM socket name");
 		close(sk);
-		return;
+		return -1;
 	}
 
 	memset(&req, 0, sizeof(req));
@@ -324,7 +327,7 @@ static void cmd_connect(int ctl, int dev
 	if ((dev = ioctl(sk, RFCOMMCREATEDEV, &req)) < 0) {
 		perror("Can't create RFCOMM TTY");
 		close(sk);
-		return;
+		return -1;
 	}
 
 	snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
@@ -344,35 +347,60 @@ static void cmd_connect(int ctl, int dev
 			ioctl(ctl, RFCOMMRELEASEDEV, &req);
 
 			close(sk);
-			return;
+			return -1;
 		}
 	}
 
+	close(sk);
+
 	if (rfcomm_raw_tty) {
-		tcflush(fd, TCIOFLUSH);
+		if (tcflush(fd, TCIOFLUSH) < 0) {
+			perror("Can't flush device data");
+			return -1;
+		}
 
 		cfmakeraw(&ti);
-		tcsetattr(fd, TCSANOW, &ti);
-	}
 
-	close(sk);
+		if (tcsetattr(fd, TCSANOW, &ti) < 0) {
+			perror("Can't set terminal attributes");
+			return -1;
+		}
+	}
 
 	ba2str(&req.dst, dst);
 	printf("Connected %s to %s on channel %d\n", devname, dst, req.channel);
-	printf("Press CTRL-C for hangup\n");
 
 	memset(&sa, 0, sizeof(sa));
 	sa.sa_flags   = SA_NOCLDSTOP;
 	sa.sa_handler = SIG_IGN;
-	sigaction(SIGCHLD, &sa, NULL);
-	sigaction(SIGPIPE, &sa, NULL);
+	if (sigaction(SIGCHLD, &sa, NULL) < 0) {
+		perror("Can't change SIGCHLD behaivor");
+		return -1;
+	}
+
+	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
+		perror("Can't change SIGPIPE behaivor");
+		return -1;
+	}
 
 	sa.sa_handler = sig_term;
-	sigaction(SIGTERM, &sa, NULL);
-	sigaction(SIGINT,  &sa, NULL);
+	if (sigaction(SIGTERM, &sa, NULL) < 0) {
+		perror("Can't change SIGTERM behaivor");
+		return -1;
+	}
+
+	if (sigaction(SIGINT,  &sa, NULL) < 0) {
+		perror("Can't change SIGINT behaivor");
+		return -1;
+	}
+
+	printf("Press CTRL-C for hangup\n");
 
 	sa.sa_handler = sig_hup;
-	sigaction(SIGHUP, &sa, NULL);
+	if (sigaction(SIGHUP, &sa, NULL) < 0) {
+		perror("Can't change SIGHUP baivor");
+		return -1;
+	}
 
 	p.fd = fd;
 	p.events = POLLERR | POLLHUP;
@@ -386,9 +414,10 @@ static void cmd_connect(int ctl, int dev
 	printf("Disconnected\n");
 
 	close(fd);
+	return 0;
 }
 
-static void cmd_listen(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+static int cmd_listen(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
 	struct sockaddr_rc laddr, raddr;
 	struct rfcomm_dev_req req;
@@ -404,27 +433,36 @@ static void cmd_listen(int ctl, int dev,
 
 	if ((sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
 		perror("Can't create RFCOMM socket");
-		return;
+		return -1;
 	}
 
 	if (bind(sk, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) {
 		perror("Can't bind RFCOMM socket");
 		close(sk);
-		return;
+		return -1;
 	}
 
 	printf("Waiting for connection on channel %d\n", laddr.rc_channel);
 
-	listen(sk, 10);
+	if (listen(sk, 10) < 0) {
+		perror("Can't listen RFCOMM socket");
+		close(sk);
+		return -1;
+	}
 
 	alen = sizeof(raddr);
-	nsk = accept(sk, (struct sockaddr *) &raddr, &alen);
+	if ((nsk = accept(sk, (struct sockaddr *) &raddr, &alen)) < 0) {
+		perror("Can't accept connection in RFCOMM socket");
+		close(sk);
+		return -1;
+	}
 
 	alen = sizeof(laddr);
 	if (getsockname(nsk, (struct sockaddr *)&laddr, &alen) < 0) {
 		perror("Can't get RFCOMM socket name");
+		close(sk);
 		close(nsk);
-		return;
+		return -1;
 	}
 
 	memset(&req, 0, sizeof(req));
@@ -438,7 +476,8 @@ static void cmd_listen(int ctl, int dev,
 	if ((dev = ioctl(nsk, RFCOMMCREATEDEV, &req)) < 0) {
 		perror("Can't create RFCOMM TTY");
 		close(sk);
-		return;
+		close(nsk);
+		return -1;
 	}
 
 	snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
@@ -458,36 +497,61 @@ static void cmd_listen(int ctl, int dev,
 			ioctl(ctl, RFCOMMRELEASEDEV, &req);
 
 			close(sk);
-			return;
+			return -1;
 		}
 	}
 
+	close(sk);
+	close(nsk);
+
 	if (rfcomm_raw_tty) {
-		tcflush(fd, TCIOFLUSH);
+		if (tcflush(fd, TCIOFLUSH) < 0) {
+			perror("Can't flush device data");
+			return -1;
+		}
 
 		cfmakeraw(&ti);
-		tcsetattr(fd, TCSANOW, &ti);
-	}
 
-	close(sk);
-	close(nsk);
+		if (tcsetattr(fd, TCSANOW, &ti) < 0) {
+			perror("Can't set terminal attributes");
+			return -1;
+		}
+	}
 
 	ba2str(&req.dst, dst);
 	printf("Connection from %s to %s\n", dst, devname);
-	printf("Press CTRL-C for hangup\n");
 
 	memset(&sa, 0, sizeof(sa));
 	sa.sa_flags   = SA_NOCLDSTOP;
 	sa.sa_handler = SIG_IGN;
-	sigaction(SIGCHLD, &sa, NULL);
-	sigaction(SIGPIPE, &sa, NULL);
+	if (sigaction(SIGCHLD, &sa, NULL) < 0) {
+		perror("Can't change SIGCHLD behaivor");
+		return -1;
+	}
+
+	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
+		perror("Can't change SIGPIPE behaivor");
+		return -1;
+	}
 
 	sa.sa_handler = sig_term;
-	sigaction(SIGTERM, &sa, NULL);
-	sigaction(SIGINT,  &sa, NULL);
+	if (sigaction(SIGTERM, &sa, NULL) < 0) {
+		perror("Can't change SIGTERM behaivor");
+		return -1;
+	}
+
+	if (sigaction(SIGINT,  &sa, NULL) < 0) {
+		perror("Can't change SIGINT behaivor");
+		return -1;
+	}
+
+	printf("Press CTRL-C for hangup\n");
 
 	sa.sa_handler = sig_hup;
-	sigaction(SIGHUP, &sa, NULL);
+	if (sigaction(SIGHUP, &sa, NULL) < 0) {
+		perror("Can't change SIGHUP baivor");
+		return -1;
+	}
 
 	p.fd = fd;
 	p.events = POLLERR | POLLHUP;
@@ -501,43 +565,49 @@ static void cmd_listen(int ctl, int dev,
 	printf("Disconnected\n");
 
 	close(fd);
+	return 0;
 }
 
-static void cmd_create(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+static int cmd_create(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
 	if (strcmp(argv[0], "all") == 0)
-		create_all(ctl);
+		return create_all(ctl);
 	else
-		create_dev(ctl, dev, 0, bdaddr, argc, argv);
+		return create_dev(ctl, dev, 0, bdaddr, argc, argv);
 }
 
-static void cmd_release(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+static int cmd_release(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
 	if (strcmp(argv[0], "all") == 0)
-		release_all(ctl);
+		return release_all(ctl);
 	else
-		release_dev(ctl, dev, 0);
+		return release_dev(ctl, dev, 0);
 }
 
-static void cmd_show(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+static int cmd_show(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
+	int ret;
+
 	if (strcmp(argv[0], "all") == 0)
-		print_dev_list(ctl, 0);
+		ret = print_dev_list(ctl, 0);
 	else {
 		struct rfcomm_dev_info di = { id: atoi(argv[0]) };
 		if (ioctl(ctl, RFCOMMGETDEVINFO, &di) < 0) {
 			perror("Get info failed");
-			exit(1);
+			return -1;
 		}
 
 		print_dev_info(&di);
+		ret = 0;
 	}
+
+	return ret;
 }
 
 struct {
 	char *cmd;
 	char *alt;
-	void (*func)(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv);
+	int (*func)(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv);
 	char *opt;
 	char *doc;
 } command[] = {
@@ -587,7 +657,7 @@ int main(int argc, char *argv[]) 
 {
 
 	bdaddr_t bdaddr;
-	int i, opt, ctl, dev_id, show_all = 0;
+	int i, err, opt, ctl, dev_id, show_all = 0;
 
 	bacpy(&bdaddr, BDADDR_ANY);
 
@@ -616,7 +686,7 @@ int main(int argc, char *argv[]) 
 			exit(0);
 
 		default:
-			exit(0);
+			exit(1);
 		}
 	}
 
@@ -633,9 +703,9 @@ int main(int argc, char *argv[]) 
 	}
 
 	if (show_all) {
-		print_dev_list(ctl, 0);
+		err = print_dev_list(ctl, 0);
 		close(ctl);
-		exit(0);
+		err ? exit(1) : exit(0);
 	}
 
 	if (strncmp(argv[1], "/dev/rfcomm", 11) == 0)
@@ -650,9 +720,9 @@ int main(int argc, char *argv[]) 
 			continue;
 		argc--;
 		argv++;
-		command[i].func(ctl, dev_id, &bdaddr, argc, argv);
+		err = command[i].func(ctl, dev_id, &bdaddr, argc, argv);
 		close(ctl);
-		exit(0);
+		err ? exit(1) : exit(0);
 	}
 
 	usage();

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RESEND] - Fixes rfcomm program error codes.
  2005-06-16 21:24 ` [Bluez-devel] " Marcel Holtmann
@ 2005-06-16 20:51   ` Luiz Fernando Capitulino
  2005-06-16 22:08     ` [Bluez-devel] " Marcel Holtmann
  0 siblings, 1 reply; 5+ messages in thread
From: Luiz Fernando Capitulino @ 2005-06-16 20:51 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: bluez-devel

Hi Marcel,

Marcel Holtmann wrote:
> Hi Luiz,
> 
> 
>>I sent this patch some days ago, but I saw that my e-mail client
>>took the bluez-users address. Hopes to not annoy you sending it
>>again (to the right place now).
>>
>>rfcomm program does not return proper error codes to the environment,
>>this makes hard to use it from a shell script or from another program.
>>
>>Here goes a patch to fix it. I didn't have much time to test it, so
>>any feedback is welcome.
>>
>>- Changes functions to return -1 on error and 0 on success
>>- Adds missing error checks
>>- Adds missing error messages
>>- Fixes rfcomm exit() return codes
> 
> 
> I don't see the advantages from changing "exit(1)" into "return -1".
> This doesn't fix program error codes.

 Sure, but actually, the functions are returning -1, while main()
checks this return value and does return 'exit(1)' on error. So, this
patch fixes the error codes.

 I guess you're speaking about some changes from 'exit(1)' to 'return -1'
I've made. I did that change, as I changed some 'return -errno' or added
proper return value to void functions.

 I saw that most programs (but not all) in the utils package calls exit(1)
directly in the functions when an error happens. IMHO, making functions
return -1 (instead of aborting directly) is pretty better because: 1) system
calls and libc follows that standard; 2) it's easier to copy and paste functions
in other programs; and 3) your program has only one exit point (in main()).

But of course, maybe it's not an issue, and if you wants to, I can change it to
exit in functions directly.

Thank you for reveiwing my patches,
 
-- 
Luiz Fernando N. Capitulino

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bluez-devel] Re: [RESEND] - Fixes rfcomm program error codes.
  2005-06-16 20:00 [RESEND] - Fixes rfcomm program error codes Luiz Fernando Capitulino
@ 2005-06-16 21:24 ` Marcel Holtmann
  2005-06-16 20:51   ` Luiz Fernando Capitulino
  0 siblings, 1 reply; 5+ messages in thread
From: Marcel Holtmann @ 2005-06-16 21:24 UTC (permalink / raw)
  To: Luiz Fernando Capitulino; +Cc: bluez-devel

Hi Luiz,

> I sent this patch some days ago, but I saw that my e-mail client
> took the bluez-users address. Hopes to not annoy you sending it
> again (to the right place now).
> 
> rfcomm program does not return proper error codes to the environment,
> this makes hard to use it from a shell script or from another program.
> 
> Here goes a patch to fix it. I didn't have much time to test it, so
> any feedback is welcome.
> 
> - Changes functions to return -1 on error and 0 on success
> - Adds missing error checks
> - Adds missing error messages
> - Fixes rfcomm exit() return codes

I don't see the advantages from changing "exit(1)" into "return -1".
This doesn't fix program error codes.

Regards

Marcel




-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bluez-devel] Re: [RESEND] - Fixes rfcomm program error codes.
  2005-06-16 20:51   ` Luiz Fernando Capitulino
@ 2005-06-16 22:08     ` Marcel Holtmann
  2005-06-17 16:53       ` Luiz Fernando Capitulino
  0 siblings, 1 reply; 5+ messages in thread
From: Marcel Holtmann @ 2005-06-16 22:08 UTC (permalink / raw)
  To: Luiz Fernando Capitulino; +Cc: bluez-devel

Hi Luiz,

> > I don't see the advantages from changing "exit(1)" into "return -1".
> > This doesn't fix program error codes.
> 
>  Sure, but actually, the functions are returning -1, while main()
> checks this return value and does return 'exit(1)' on error. So, this
> patch fixes the error codes.
> 
>  I guess you're speaking about some changes from 'exit(1)' to 'return -1'
> I've made. I did that change, as I changed some 'return -errno' or added
> proper return value to void functions.
> 
>  I saw that most programs (but not all) in the utils package calls exit(1)
> directly in the functions when an error happens. IMHO, making functions
> return -1 (instead of aborting directly) is pretty better because: 1) system
> calls and libc follows that standard; 2) it's easier to copy and paste functions
> in other programs; and 3) your program has only one exit point (in main()).
> 
> But of course, maybe it's not an issue, and if you wants to, I can change it to
> exit in functions directly.

lets keep it using "exit(1)". It is an historic issue. Send a patch for
the few cases we did it wrong.

And btw I support people copying the code, but I also like to see that
people understand what the code does. If they have to change "exit(1)"
to have proper return values in their own code, they at least looked at
the code once.

Regards

Marcel




-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RESEND] - Fixes rfcomm program error codes.
  2005-06-16 22:08     ` [Bluez-devel] " Marcel Holtmann
@ 2005-06-17 16:53       ` Luiz Fernando Capitulino
  0 siblings, 0 replies; 5+ messages in thread
From: Luiz Fernando Capitulino @ 2005-06-17 16:53 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: bluez-devel

[-- Attachment #1: Type: text/plain, Size: 903 bytes --]

Hello Marcel,

Marcel Holtmann wrote:

> lets keep it using "exit(1)". It is an historic issue. Send a patch for
> the few cases we did it wrong.

Okay, it's attached. Note that:

1) Some functions returns -1 on error, I didn't change it
2) Create all devices and release all devices returns success if all devices
could be created (or released), otherwise it returns error (IOW, if we get
an error to create or release at least one device, the program returns error)
3) I've added some missing checks
4) If you enter with an unknown parameter it does 'exit(1)' instead of
'exit(0)', because it's an error.

rfcomm/main.c |  135 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 95 insertions(+), 40 deletions(-)

PS: The parameters for raw terminal mode (-r) and configfile (-f)
are not documented, I think a patch for it doesn't pay off. :)

-- 
Luiz Fernando N. Capitulino

[-- Attachment #2: rfcomm_return_codes.patch --]
[-- Type: text/x-patch, Size: 8084 bytes --]

 rfcomm/main.c |  135 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 95 insertions(+), 40 deletions(-)


diff -X /home/lcapitulino/kernels/2.6/dontdiff -Nparu a/rfcomm/main.c a~/rfcomm/main.c
--- a/rfcomm/main.c	2004-11-25 10:05:16.000000000 -0200
+++ a~/rfcomm/main.c	2005-06-16 21:08:58.000000000 -0300
@@ -165,7 +165,7 @@ static int create_dev(int ctl, int dev, 
 
 		if (bacmp(&req.dst, BDADDR_ANY) == 0) {
 			fprintf(stderr, "Can't find a config entry for rfcomm%d\n", dev);
-			return -EFAULT;
+			return -1;
 		}
 		
 	} else {
@@ -193,6 +193,8 @@ static int create_all(int ctl)
 		return err;
 	}
 
+	err = -1;
+
 	for (i = 0; i < RFCOMM_MAX_DEV; i++) {
 		if (!rfcomm_opts[i].bind)
 			continue;
@@ -204,11 +206,15 @@ static int create_all(int ctl)
 		bacpy(&req.dst, &rfcomm_opts[i].bdaddr);
 		req.channel = rfcomm_opts[i].channel;
 
-		if (bacmp(&req.dst, BDADDR_ANY) != 0)
-			ioctl(ctl, RFCOMMCREATEDEV, &req);
+		if (bacmp(&req.dst, BDADDR_ANY) != 0) {
+			if (ioctl(ctl, RFCOMMCREATEDEV, &req) < 0)
+				fprintf(stderr, "Warning: failed to create RFCOMM device: rfcomm%d\n", i);
+			else
+				err = 0;
+		}
 	}
 
-	return 0;
+	return err;
 }
 
 static int release_dev(int ctl, int dev, uint32_t flags)
@@ -229,7 +235,7 @@ static int release_all(int ctl)
 {
 	struct rfcomm_dev_list_req *dl;
 	struct rfcomm_dev_info *di;
-	int i;
+	int i, err = 0;
 
 	dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
 	if (!dl) {
@@ -246,9 +252,10 @@ static int release_all(int ctl)
 	}
 
 	for (i = 0; i < dl->dev_num; i++)
-		release_dev(ctl, (di + i)->id, 0);
+		if (release_dev(ctl, (di + i)->id, 0) < 0)
+			err = -1;
 
-	return 0;
+	return err;
 }
 
 static void cmd_connect(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
@@ -268,7 +275,7 @@ static void cmd_connect(int ctl, int dev
 	if (argc < 2) {
 		if (rfcomm_read_config(rfcomm_config_file) < 0) {
 			perror("Can't open RFCOMM config file");
-			return;
+			exit(1);
 		}
 
 		raddr.rc_family = AF_BLUETOOTH;
@@ -277,7 +284,7 @@ static void cmd_connect(int ctl, int dev
 
 		if (bacmp(&raddr.rc_bdaddr, BDADDR_ANY) == 0) {
 			fprintf(stderr, "Can't find a config entry for rfcomm%d\n", dev);
-			return;
+			exit(1);
 		}
 	} else {
 		raddr.rc_family = AF_BLUETOOTH;
@@ -291,26 +298,26 @@ static void cmd_connect(int ctl, int dev
 
 	if ((sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
 		perror("Can't create RFCOMM socket");
-		return;
+		exit(1);
 	}
 
 	if (bind(sk, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) {
 		perror("Can't bind RFCOMM socket");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	if (connect(sk, (struct sockaddr *)&raddr, sizeof(raddr)) < 0) {
 		perror("Can't connect RFCOMM socket");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	alen = sizeof(laddr);
 	if (getsockname(sk, (struct sockaddr *)&laddr, &alen) < 0) {
 		perror("Can't get RFCOMM socket name");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	memset(&req, 0, sizeof(req));
@@ -324,7 +331,7 @@ static void cmd_connect(int ctl, int dev
 	if ((dev = ioctl(sk, RFCOMMCREATEDEV, &req)) < 0) {
 		perror("Can't create RFCOMM TTY");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
@@ -344,7 +351,7 @@ static void cmd_connect(int ctl, int dev
 			ioctl(ctl, RFCOMMRELEASEDEV, &req);
 
 			close(sk);
-			return;
+			exit(1);
 		}
 	}
 
@@ -352,7 +359,10 @@ static void cmd_connect(int ctl, int dev
 		tcflush(fd, TCIOFLUSH);
 
 		cfmakeraw(&ti);
-		tcsetattr(fd, TCSANOW, &ti);
+		if (tcsetattr(fd, TCSANOW, &ti) < 0) {
+			perror("Can't set attributes");
+			exit(1);
+		}
 	}
 
 	close(sk);
@@ -364,15 +374,30 @@ static void cmd_connect(int ctl, int dev
 	memset(&sa, 0, sizeof(sa));
 	sa.sa_flags   = SA_NOCLDSTOP;
 	sa.sa_handler = SIG_IGN;
-	sigaction(SIGCHLD, &sa, NULL);
-	sigaction(SIGPIPE, &sa, NULL);
+	if (sigaction(SIGCHLD, &sa, NULL) < 0) {
+		perror("Can't change SIGCHLD disposition");
+		exit(1);
+	}
+	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
+		perror("Can't change SIGPIPE disposition");
+		exit(1);
+	}
 
 	sa.sa_handler = sig_term;
-	sigaction(SIGTERM, &sa, NULL);
-	sigaction(SIGINT,  &sa, NULL);
+	if (sigaction(SIGTERM, &sa, NULL) < 0) {
+		perror("Can't change SIGTERM disposition");
+		exit(1);
+	}
+	if (sigaction(SIGINT,  &sa, NULL) < 0) {
+		perror("Can't change SIGINT disposition");
+		exit(1);
+	}
 
 	sa.sa_handler = sig_hup;
-	sigaction(SIGHUP, &sa, NULL);
+	if (sigaction(SIGHUP, &sa, NULL) < 0) {
+		perror("Can't change SIGHUP disposition");
+		exit(1);
+	}
 
 	p.fd = fd;
 	p.events = POLLERR | POLLHUP;
@@ -404,27 +429,33 @@ static void cmd_listen(int ctl, int dev,
 
 	if ((sk = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
 		perror("Can't create RFCOMM socket");
-		return;
+		exit(1);
 	}
 
 	if (bind(sk, (struct sockaddr *)&laddr, sizeof(laddr)) < 0) {
 		perror("Can't bind RFCOMM socket");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	printf("Waiting for connection on channel %d\n", laddr.rc_channel);
 
-	listen(sk, 10);
+	if (listen(sk, 10) < 0) {
+		perror("Can't set socket queue limit");
+		exit(1);
+	}
 
 	alen = sizeof(raddr);
-	nsk = accept(sk, (struct sockaddr *) &raddr, &alen);
+	if ((nsk = accept(sk, (struct sockaddr *) &raddr, &alen)) < 0) {
+		perror("Can't accept connection");
+		exit(1);
+	}
 
 	alen = sizeof(laddr);
 	if (getsockname(nsk, (struct sockaddr *)&laddr, &alen) < 0) {
 		perror("Can't get RFCOMM socket name");
 		close(nsk);
-		return;
+		exit(1);
 	}
 
 	memset(&req, 0, sizeof(req));
@@ -438,7 +469,7 @@ static void cmd_listen(int ctl, int dev,
 	if ((dev = ioctl(nsk, RFCOMMCREATEDEV, &req)) < 0) {
 		perror("Can't create RFCOMM TTY");
 		close(sk);
-		return;
+		exit(1);
 	}
 
 	snprintf(devname, MAXPATHLEN - 1, "/dev/rfcomm%d", dev);
@@ -458,7 +489,7 @@ static void cmd_listen(int ctl, int dev,
 			ioctl(ctl, RFCOMMRELEASEDEV, &req);
 
 			close(sk);
-			return;
+			exit(1);
 		}
 	}
 
@@ -466,7 +497,10 @@ static void cmd_listen(int ctl, int dev,
 		tcflush(fd, TCIOFLUSH);
 
 		cfmakeraw(&ti);
-		tcsetattr(fd, TCSANOW, &ti);
+		if (tcsetattr(fd, TCSANOW, &ti) < 0) {
+			perror("Can't set attributes");
+			exit(1);
+		}
 	}
 
 	close(sk);
@@ -479,15 +513,32 @@ static void cmd_listen(int ctl, int dev,
 	memset(&sa, 0, sizeof(sa));
 	sa.sa_flags   = SA_NOCLDSTOP;
 	sa.sa_handler = SIG_IGN;
-	sigaction(SIGCHLD, &sa, NULL);
-	sigaction(SIGPIPE, &sa, NULL);
+	if (sigaction(SIGCHLD, &sa, NULL) < 0) {
+		perror("Can't change SIGCHLD disposition");
+		exit(1);
+	}
+
+	if (sigaction(SIGPIPE, &sa, NULL) < 0) {
+		perror("Can't change SIGPIPE disposition");
+		exit(1);
+	}
 
 	sa.sa_handler = sig_term;
-	sigaction(SIGTERM, &sa, NULL);
-	sigaction(SIGINT,  &sa, NULL);
+	if (sigaction(SIGTERM, &sa, NULL) < 0) {
+		perror("Can't change SIGTERM disposition");
+		exit(1);
+	}
+
+	if (sigaction(SIGINT,  &sa, NULL) < 0) {
+		perror("Can't change SIGINT disposition");
+		exit(1);
+	}
 
 	sa.sa_handler = sig_hup;
-	sigaction(SIGHUP, &sa, NULL);
+	if (sigaction(SIGHUP, &sa, NULL) < 0) {
+		perror("Can't change SIGHUP disposition");
+		exit(1);
+	}
 
 	p.fd = fd;
 	p.events = POLLERR | POLLHUP;
@@ -505,10 +556,13 @@ static void cmd_listen(int ctl, int dev,
 
 static void cmd_create(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
 {
-	if (strcmp(argv[0], "all") == 0)
-		create_all(ctl);
-	else
-		create_dev(ctl, dev, 0, bdaddr, argc, argv);
+	if (strcmp(argv[0], "all") == 0) {
+		if (create_all(ctl) < 0)
+			exit(1);
+	} else {
+		if (create_dev(ctl, dev, 0, bdaddr, argc, argv) < 0)
+			exit(1);
+	}
 }
 
 static void cmd_release(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
@@ -516,7 +570,8 @@ static void cmd_release(int ctl, int dev
 	if (strcmp(argv[0], "all") == 0)
 		release_all(ctl);
 	else
-		release_dev(ctl, dev, 0);
+		if (release_dev(ctl, dev, 0) < 0)
+			exit(1);
 }
 
 static void cmd_show(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
@@ -616,7 +671,7 @@ int main(int argc, char *argv[]) 
 			exit(0);
 
 		default:
-			exit(0);
+			exit(1);
 		}
 	}
 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-06-17 16:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-16 20:00 [RESEND] - Fixes rfcomm program error codes Luiz Fernando Capitulino
2005-06-16 21:24 ` [Bluez-devel] " Marcel Holtmann
2005-06-16 20:51   ` Luiz Fernando Capitulino
2005-06-16 22:08     ` [Bluez-devel] " Marcel Holtmann
2005-06-17 16:53       ` Luiz Fernando Capitulino

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.