2011-11-16 13:19:47

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 00/17] Fix errno handling convention

Hi,

This series fix various places where variables which get errno values (usually
named "err") are not following the convention of always being negative. The
last 3 patches are simple fixes found during this review.

Best Regards,
--
Anderson Lizardo
Instituto Nokia de Tecnologia (INdT)
Manaus - Brazil



2011-11-24 05:58:41

by Jaganath Kanakkassery

[permalink] [raw]
Subject: Regarding MAP status

Hi,

May I know the current status of MAP?
Some work is going on at
https://github.com/bulislaw/obexd-map/commits/final-backend/.
Is there any plan to upload this code base to mainstream?

Thanks,
Jaganath


2011-11-17 11:58:21

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 00/17] Fix errno handling convention

Hi Lizardo,

On Wed, Nov 16, 2011, Anderson Lizardo wrote:
> This series fix various places where variables which get errno values (usually
> named "err") are not following the convention of always being negative. The
> last 3 patches are simple fixes found during this review.

All patches have been applied. Thanks.

Johan

2011-11-16 13:20:04

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 17/17] Fix return value check for bt_audio_service_open()

Only negative values (namely -1) are errors for this function.
---
audio/gstavdtpsink.c | 2 +-
audio/pcm_bluetooth.c | 2 +-
test/ipctest.c | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/audio/gstavdtpsink.c b/audio/gstavdtpsink.c
index f379469..dca9d67 100644
--- a/audio/gstavdtpsink.c
+++ b/audio/gstavdtpsink.c
@@ -1448,7 +1448,7 @@ static gboolean gst_avdtp_sink_start(GstBaseSink *basesink)
self->watch_id = 0;

sk = bt_audio_service_open();
- if (sk <= 0) {
+ if (sk < 0) {
err = -errno;
GST_ERROR_OBJECT(self, "Cannot open connection to bt "
"audio service: %s %d", strerror(-err), -err);
diff --git a/audio/pcm_bluetooth.c b/audio/pcm_bluetooth.c
index 4d85f33..85566a3 100644
--- a/audio/pcm_bluetooth.c
+++ b/audio/pcm_bluetooth.c
@@ -1666,7 +1666,7 @@ static int bluetooth_init(struct bluetooth_data *data,
data->stream.fd = -1;

sk = bt_audio_service_open();
- if (sk <= 0) {
+ if (sk < 0) {
err = -errno;
goto failed;
}
diff --git a/test/ipctest.c b/test/ipctest.c
index 4968443..cbfd78d 100644
--- a/test/ipctest.c
+++ b/test/ipctest.c
@@ -214,7 +214,7 @@ static int init_bt(struct userdata *u)
DBG("bt_audio_service_open");

u->service_fd = bt_audio_service_open();
- if (u->service_fd <= 0) {
+ if (u->service_fd < 0) {
int err = -errno;

ERR("bt_audio_service_open() failed: %s (%d)", strerror(-err),
--
1.7.0.4


2011-11-16 13:20:03

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 16/17] csr_bcsp: Fix missing errno attribution

do_command() is supposed to set errno if returning -1.
---
tools/csr_bcsp.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/tools/csr_bcsp.c b/tools/csr_bcsp.c
index df247a2..7bfaf5a 100644
--- a/tools/csr_bcsp.c
+++ b/tools/csr_bcsp.c
@@ -219,6 +219,7 @@ static int do_command(uint16_t command, uint16_t seqnum, uint16_t varid, uint8_t

if (timeout++ > 5000) {
fprintf(stderr, "Operation timed out\n");
+ errno = ETIMEDOUT;
return -1;
}
}
--
1.7.0.4


2011-11-16 13:20:02

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 15/17] bccmd: Fix overwriting errno value

On error, transport_read() returns -1 and sets errno.
---
tools/bccmd.c | 32 ++++++++------------------------
1 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/tools/bccmd.c b/tools/bccmd.c
index ca9302c..952bf13 100644
--- a/tools/bccmd.c
+++ b/tools/bccmd.c
@@ -253,10 +253,8 @@ static int cmd_builddef(int transport, int argc, char *argv[])
array[1] = def >> 8;

err = transport_read(transport, CSR_VARID_GET_NEXT_BUILDDEF, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
break;
- }

nextdef = array[2] | (array[3] << 8);

@@ -286,10 +284,8 @@ static int cmd_keylen(int transport, int argc, char *argv[])
array[1] = handle >> 8;

err = transport_read(transport, CSR_VARID_CRYPT_KEY_LENGTH, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

handle = array[0] | (array[1] << 8);
keylen = array[2] | (array[3] << 8);
@@ -310,10 +306,8 @@ static int cmd_clock(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_BT_CLOCK, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

clock = array[2] | (array[3] << 8) | (array[0] << 16) | (array[1] << 24);

@@ -333,10 +327,8 @@ static int cmd_rand(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_RAND, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

rand = array[0] | (array[1] << 8);

@@ -357,10 +349,8 @@ static int cmd_chiprev(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_CHIPREV, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

rev = array[0] | (array[1] << 8);

@@ -417,10 +407,8 @@ static int cmd_buildname(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_READ_BUILD_NAME, array, 128);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

for (i = 0; i < sizeof(name); i++)
name[i] = array[(i * 2) + 4];
@@ -441,10 +429,8 @@ static int cmd_panicarg(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_PANIC_ARG, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

error = array[0] | (array[1] << 8);

@@ -465,10 +451,8 @@ static int cmd_faultarg(int transport, int argc, char *argv[])
memset(array, 0, sizeof(array));

err = transport_read(transport, CSR_VARID_FAULT_ARG, array, 8);
- if (err < 0) {
- errno = -err;
+ if (err < 0)
return -1;
- }

error = array[0] | (array[1] << 8);

--
1.7.0.4


2011-11-16 13:20:01

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 14/17] btio: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
btio/btio.c | 14 ++++++++------
1 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/btio/btio.c b/btio/btio.c
index 6db1756..a129bf9 100644
--- a/btio/btio.c
+++ b/btio/btio.c
@@ -153,16 +153,18 @@ static gboolean connect_cb(GIOChannel *io, GIOCondition cond,
return FALSE;

if (cond & G_IO_OUT) {
- int err = 0, sock = g_io_channel_unix_get_fd(io);
- socklen_t len = sizeof(err);
+ int err, sk_err = 0, sock = g_io_channel_unix_get_fd(io);
+ socklen_t len = sizeof(sk_err);

- if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
- err = errno;
+ if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
+ err = -errno;
+ else
+ err = -sk_err;

- if (err)
+ if (err < 0)
g_set_error(&gerr, BT_IO_ERROR,
BT_IO_ERROR_CONNECT_FAILED, "%s (%d)",
- strerror(err), err);
+ strerror(-err), -err);
} else if (cond & (G_IO_HUP | G_IO_ERR))
g_set_error(&gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
"HUP or ERR on socket");
--
1.7.0.4


2011-11-16 13:20:00

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 13/17] input: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.

Also remove a few unnecessary errno assignments.
---
input/device.c | 43 +++++++++++++++++++------------------------
1 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/input/device.c b/input/device.c
index 16eb155..a1ecdd7 100644
--- a/input/device.c
+++ b/input/device.c
@@ -164,10 +164,10 @@ static int uinput_create(char *name)
if (fd < 0) {
fd = open("/dev/misc/uinput", O_RDWR);
if (fd < 0) {
- err = errno;
+ err = -errno;
error("Can't open input device: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}
}
}
@@ -182,12 +182,11 @@ static int uinput_create(char *name)
dev.id.version = 0x0000;

if (write(fd, &dev, sizeof(dev)) < 0) {
- err = errno;
+ err = -errno;
error("Can't write device information: %s (%d)",
- strerror(err), err);
+ strerror(-err), -err);
close(fd);
- errno = err;
- return -err;
+ return err;
}

ioctl(fd, UI_SET_EVBIT, EV_KEY);
@@ -200,12 +199,11 @@ static int uinput_create(char *name)
ioctl(fd, UI_SET_KEYBIT, KEY_PAGEDOWN);

if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
- err = errno;
+ err = -errno;
error("Can't create uinput device: %s (%d)",
- strerror(err), err);
+ strerror(-err), -err);
close(fd);
- errno = err;
- return -err;
+ return err;
}

return fd;
@@ -333,9 +331,11 @@ static void rfcomm_connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
*/
fake->uinput = uinput_create(idev->name);
if (fake->uinput < 0) {
+ int err = fake->uinput;
+
g_io_channel_shutdown(chan, TRUE, NULL);
reply = btd_error_failed(iconn->pending_connect,
- strerror(errno));
+ strerror(-err));
goto failed;
}

@@ -534,11 +534,11 @@ static int ioctl_connadd(struct hidp_connadd_req *req)
return -errno;

if (ioctl(ctl, HIDPCONNADD, req) < 0)
- err = errno;
+ err = -errno;

close(ctl);

- return -err;
+ return err;
}

static void encrypt_completed(uint8_t status, gpointer user_data)
@@ -680,7 +680,7 @@ static int connection_disconnect(struct input_conn *iconn, uint32_t flags)
struct fake_input *fake = iconn->fake;
struct hidp_conndel_req req;
struct hidp_conninfo ci;
- int ctl, err;
+ int ctl, err = 0;

/* Fake input disconnect */
if (fake) {
@@ -706,7 +706,7 @@ static int connection_disconnect(struct input_conn *iconn, uint32_t flags)
bacpy(&ci.bdaddr, &idev->dst);
if ((ioctl(ctl, HIDPGETCONNINFO, &ci) < 0) ||
(ci.state != BT_CONNECTED)) {
- errno = ENOTCONN;
+ err = -ENOTCONN;
goto fail;
}

@@ -714,21 +714,16 @@ static int connection_disconnect(struct input_conn *iconn, uint32_t flags)
bacpy(&req.bdaddr, &idev->dst);
req.flags = flags;
if (ioctl(ctl, HIDPCONNDEL, &req) < 0) {
+ err = -errno;
error("Can't delete the HID device: %s(%d)",
- strerror(errno), errno);
+ strerror(-err), -err);
goto fail;
}

- close(ctl);
-
- return 0;
-
fail:
- err = errno;
close(ctl);
- errno = err;

- return -err;
+ return err;
}

static int disconnect(struct input_device *idev, uint32_t flags)
--
1.7.0.4


2011-11-16 13:19:59

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 12/17] network: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
network/common.c | 24 ++++++++++++------------
1 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/network/common.c b/network/common.c
index 6da9f0a..c498c25 100644
--- a/network/common.c
+++ b/network/common.c
@@ -108,10 +108,10 @@ int bnep_init(void)
ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP);

if (ctl < 0) {
- int err = errno;
+ int err = -errno;
error("Failed to open control socket: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}

return 0;
@@ -131,10 +131,10 @@ int bnep_kill_connection(bdaddr_t *dst)
baswap((bdaddr_t *)&req.dst, dst);
req.flags = 0;
if (ioctl(ctl, BNEPCONNDEL, &req)) {
- int err = errno;
+ int err = -errno;
error("Failed to kill connection: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}
return 0;
}
@@ -150,10 +150,10 @@ int bnep_kill_all_connections(void)
req.cnum = 7;
req.ci = ci;
if (ioctl(ctl, BNEPGETCONNLIST, &req)) {
- err = errno;
+ err = -errno;
error("Failed to get connection list: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}

for (i = 0; i < req.cnum; i++) {
@@ -177,10 +177,10 @@ int bnep_connadd(int sk, uint16_t role, char *dev)
req.sock = sk;
req.role = role;
if (ioctl(ctl, BNEPCONNADD, &req) < 0) {
- int err = errno;
+ int err = -errno;
error("Failed to add device %s: %s(%d)",
- dev, strerror(err), err);
- return -err;
+ dev, strerror(-err), -err);
+ return err;
}

strncpy(dev, req.device, 16);
--
1.7.0.4


2011-11-16 13:19:58

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 11/17] sap-u8500: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
sap/sap-u8500.c | 16 ++++++++--------
1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/sap/sap-u8500.c b/sap/sap-u8500.c
index e699fd7..ef7d95c 100644
--- a/sap/sap-u8500.c
+++ b/sap/sap-u8500.c
@@ -571,9 +571,9 @@ static int simd_connect(void *sap_data)

sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
- err = errno;
- sap_error("creating socket failed: %s", strerror(err));
- return -err;
+ err = -errno;
+ sap_error("creating socket failed: %s", strerror(-err));
+ return err;
}

memset(&addr, 0, sizeof(addr));
@@ -581,14 +581,14 @@ static int simd_connect(void *sap_data)
memcpy(addr.sun_path, STE_SIMD_SOCK, sizeof(STE_SIMD_SOCK) - 1);

if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err = errno;
- sap_error("connect to the socket failed: %s", strerror(err));
+ err = -errno;
+ sap_error("connect to the socket failed: %s", strerror(-err));
goto failed;
}

if (fcntl(sock, F_SETFL, O_NONBLOCK) > 0) {
- err = errno;
- sap_error("setting up socket failed: %s", strerror(err));
+ err = -errno;
+ sap_error("setting up socket failed: %s", strerror(-err));
goto failed;
}

@@ -598,7 +598,7 @@ static int simd_connect(void *sap_data)

failed:
close(sock);
- return -err;
+ return err;
}

void sap_connect_req(void *sap_device, uint16_t maxmsgsize)
--
1.7.0.4


2011-11-16 13:19:57

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 10/17] sdp-client: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
src/sdp-client.c | 18 +++++++++---------
1 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/sdp-client.c b/src/sdp-client.c
index ebd760e..f119313 100644
--- a/src/sdp-client.c
+++ b/src/sdp-client.c
@@ -217,22 +217,22 @@ static gboolean connect_watch(GIOChannel *chan, GIOCondition cond,
sdp_list_t *search, *attrids;
uint32_t range = 0x0000ffff;
socklen_t len;
- int sk, err = 0;
+ int sk, err, sk_err = 0;

sk = g_io_channel_unix_get_fd(chan);
ctxt->io_id = 0;

- len = sizeof(err);
- if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
- err = errno;
- goto failed;
- }
+ len = sizeof(sk_err);
+ if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
+ err = -errno;
+ else
+ err = -sk_err;

if (err != 0)
goto failed;

if (sdp_set_notify(ctxt->session, search_completed_cb, ctxt) < 0) {
- err = EIO;
+ err = -EIO;
goto failed;
}

@@ -242,7 +242,7 @@ static gboolean connect_watch(GIOChannel *chan, GIOCondition cond,
search, SDP_ATTR_REQ_RANGE, attrids) < 0) {
sdp_list_free(attrids, NULL);
sdp_list_free(search, NULL);
- err = EIO;
+ err = -EIO;
goto failed;
}

@@ -260,7 +260,7 @@ failed:
ctxt->session = NULL;

if (ctxt->cb)
- ctxt->cb(NULL, -err, ctxt->user_data);
+ ctxt->cb(NULL, err, ctxt->user_data);

search_context_cleanup(ctxt);

--
1.7.0.4


2011-11-16 13:19:56

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 09/17] textfile: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
src/textfile.c | 56 ++++++++++++++++++++++++++++----------------------------
1 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/src/textfile.c b/src/textfile.c
index 3fb3c01..2712cd8 100644
--- a/src/textfile.c
+++ b/src/textfile.c
@@ -149,7 +149,7 @@ static inline int write_key_value(int fd, const char *key, const char *value)
sprintf(str, "%s %s\n", key, value);

if (write(fd, str, size) < 0)
- err = errno;
+ err = -errno;

free(str);

@@ -191,12 +191,12 @@ static int write_key(const char *pathname, const char *key, const char *value, i
return -errno;

if (flock(fd, LOCK_EX) < 0) {
- err = errno;
+ err = -errno;
goto close;
}

if (fstat(fd, &st) < 0) {
- err = errno;
+ err = -errno;
goto unlock;
}

@@ -213,7 +213,7 @@ static int write_key(const char *pathname, const char *key, const char *value, i
map = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_LOCKED, fd, 0);
if (!map || map == MAP_FAILED) {
- err = errno;
+ err = -errno;
goto unlock;
}

@@ -232,7 +232,7 @@ static int write_key(const char *pathname, const char *key, const char *value, i

end = strnpbrk(off, size, "\r\n");
if (!end) {
- err = EILSEQ;
+ err = -EILSEQ;
goto unmap;
}

@@ -247,7 +247,7 @@ static int write_key(const char *pathname, const char *key, const char *value, i
if (!len) {
munmap(map, size);
if (ftruncate(fd, base) < 0) {
- err = errno;
+ err = -errno;
goto unlock;
}
lseek(fd, base, SEEK_SET);
@@ -258,13 +258,13 @@ static int write_key(const char *pathname, const char *key, const char *value, i
}

if (len < 0 || len > size) {
- err = EILSEQ;
+ err = -EILSEQ;
goto unmap;
}

str = malloc(len);
if (!str) {
- err = errno;
+ err = -errno;
goto unmap;
}

@@ -272,7 +272,7 @@ static int write_key(const char *pathname, const char *key, const char *value, i

munmap(map, size);
if (ftruncate(fd, base) < 0) {
- err = errno;
+ err = -errno;
free(str);
goto unlock;
}
@@ -281,7 +281,7 @@ static int write_key(const char *pathname, const char *key, const char *value, i
err = write_key_value(fd, key, value);

if (write(fd, str, len) < 0)
- err = errno;
+ err = -errno;

free(str);

@@ -297,9 +297,9 @@ close:
fdatasync(fd);

close(fd);
- errno = err;
+ errno = -err;

- return -err;
+ return err;
}

static char *read_key(const char *pathname, const char *key, int icase)
@@ -314,12 +314,12 @@ static char *read_key(const char *pathname, const char *key, int icase)
return NULL;

if (flock(fd, LOCK_SH) < 0) {
- err = errno;
+ err = -errno;
goto close;
}

if (fstat(fd, &st) < 0) {
- err = errno;
+ err = -errno;
goto unlock;
}

@@ -327,26 +327,26 @@ static char *read_key(const char *pathname, const char *key, int icase)

map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (!map || map == MAP_FAILED) {
- err = errno;
+ err = -errno;
goto unlock;
}

len = strlen(key);
off = find_key(map, size, key, len, icase);
if (!off) {
- err = EILSEQ;
+ err = -EILSEQ;
goto unmap;
}

end = strnpbrk(off, size - (map - off), "\r\n");
if (!end) {
- err = EILSEQ;
+ err = -EILSEQ;
goto unmap;
}

str = malloc(end - off - len);
if (!str) {
- err = EILSEQ;
+ err = -EILSEQ;
goto unmap;
}

@@ -361,7 +361,7 @@ unlock:

close:
close(fd);
- errno = err;
+ errno = -err;

return str;
}
@@ -408,12 +408,12 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)
return -errno;

if (flock(fd, LOCK_SH) < 0) {
- err = errno;
+ err = -errno;
goto close;
}

if (fstat(fd, &st) < 0) {
- err = errno;
+ err = -errno;
goto unlock;
}

@@ -421,7 +421,7 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)

map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (!map || map == MAP_FAILED) {
- err = errno;
+ err = -errno;
goto unlock;
}

@@ -430,7 +430,7 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)
while (size - (off - map) > 0) {
end = strnpbrk(off, size - (off - map), " ");
if (!end) {
- err = EILSEQ;
+ err = -EILSEQ;
break;
}

@@ -438,7 +438,7 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)

key = malloc(len + 1);
if (!key) {
- err = errno;
+ err = -errno;
break;
}

@@ -448,14 +448,14 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)
off = end + 1;

if (size - (off - map) < 0) {
- err = EILSEQ;
+ err = -EILSEQ;
free(key);
break;
}

end = strnpbrk(off, size - (off - map), "\r\n");
if (!end) {
- err = EILSEQ;
+ err = -EILSEQ;
free(key);
break;
}
@@ -464,7 +464,7 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)

value = malloc(len + 1);
if (!value) {
- err = errno;
+ err = -errno;
free(key);
break;
}
@@ -487,7 +487,7 @@ unlock:

close:
close(fd);
- errno = err;
+ errno = -err;

return 0;
}
--
1.7.0.4


2011-11-16 13:19:55

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 08/17] hciconfig: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
tools/hciconfig.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/hciconfig.c b/tools/hciconfig.c
index cbd0d0e..35b80b1 100644
--- a/tools/hciconfig.c
+++ b/tools/hciconfig.c
@@ -210,9 +210,9 @@ static void cmd_le_addr(int ctl, int hdev, char *opt)

dd = hci_open_dev(hdev);
if (dd < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "Could not open device: %s(%d)\n",
- strerror(err), err);
+ strerror(-err), -err);
exit(1);
}

@@ -230,9 +230,9 @@ static void cmd_le_addr(int ctl, int hdev, char *opt)

ret = hci_send_req(dd, &rq, 1000);
if (status || ret < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "Can't set random address for hci%d: "
- "%s (%d)\n", hdev, strerror(err), err);
+ "%s (%d)\n", hdev, strerror(-err), -err);
}

hci_close_dev(dd);
--
1.7.0.4


2011-11-16 13:19:54

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 07/17] hcitool: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
tools/hcitool.c | 16 ++++++++--------
1 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/tools/hcitool.c b/tools/hcitool.c
index 5c4a0bc..5189d8d 100644
--- a/tools/hcitool.c
+++ b/tools/hcitool.c
@@ -2696,9 +2696,9 @@ static void cmd_lewladd(int dev_id, int argc, char **argv)
hci_close_dev(dd);

if (err < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "Can't add to white list: %s(%d)\n",
- strerror(err), err);
+ strerror(-err), -err);
exit(1);
}
}
@@ -2786,9 +2786,9 @@ static void cmd_lewlsz(int dev_id, int argc, char **argv)
hci_close_dev(dd);

if (err < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "Can't read white list size: %s(%d)\n",
- strerror(err), err);
+ strerror(-err), -err);
exit(1);
}

@@ -2831,9 +2831,9 @@ static void cmd_lewlclr(int dev_id, int argc, char **argv)
hci_close_dev(dd);

if (err < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "Can't clear white list: %s(%d)\n",
- strerror(err), err);
+ strerror(-err), -err);
exit(1);
}
}
@@ -2961,9 +2961,9 @@ static void cmd_lecup(int dev_id, int argc, char **argv)

if (hci_le_conn_update(dd, htobs(handle), htobs(min), htobs(max),
htobs(latency), htobs(timeout), 5000) < 0) {
- int err = errno;
+ int err = -errno;
fprintf(stderr, "Could not change connection params: %s(%d)\n",
- strerror(err), err);
+ strerror(-err), -err);
}

hci_close_dev(dd);
--
1.7.0.4


2011-11-16 13:19:53

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 06/17] serial: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
serial/port.c | 6 +++---
serial/proxy.c | 39 ++++++++++++++++++---------------------
2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/serial/port.c b/serial/port.c
index fa302be..3b36d44 100644
--- a/serial/port.c
+++ b/serial/port.c
@@ -183,16 +183,16 @@ static int port_release(struct serial_port *port)
req.flags = (1 << RFCOMM_HANGUP_NOW);

if (ioctl(rfcomm_ctl, RFCOMMRELEASEDEV, &req) < 0) {
- err = errno;
+ err = -errno;
error("Can't release device %s: %s (%d)",
- port->dev, strerror(err), err);
+ port->dev, strerror(-err), -err);
}

g_free(port->dev);
port->dev = NULL;
port->id = -1;
close(rfcomm_ctl);
- return -err;
+ return err;
}

static void serial_port_free(void *data)
diff --git a/serial/proxy.c b/serial/proxy.c
index 371ad5f..736f690 100644
--- a/serial/proxy.c
+++ b/serial/proxy.c
@@ -289,19 +289,18 @@ static inline int unix_socket_connect(const char *address)
/* Unix socket */
sk = socket(AF_UNIX, SOCK_STREAM, 0);
if (sk < 0) {
- err = errno;
+ err = -errno;
error("Unix socket(%s) create failed: %s(%d)",
- address, strerror(err), err);
- return -err;
+ address, strerror(-err), -err);
+ return err;
}

if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err = errno;
+ err = -errno;
error("Unix socket(%s) connect failed: %s(%d)",
- address, strerror(err), err);
+ address, strerror(-err), -err);
close(sk);
- errno = err;
- return -err;
+ return err;
}

return sk;
@@ -329,18 +328,17 @@ static int tcp_socket_connect(const char *address)

sk = socket(PF_INET, SOCK_STREAM, 0);
if (sk < 0) {
- err = errno;
+ err = -errno;
error("TCP socket(%s) create failed %s(%d)", address,
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err = errno;
+ err = -errno;
error("TCP socket(%s) connect failed: %s(%d)",
- address, strerror(err), err);
+ address, strerror(-err), -err);
close(sk);
- errno = err;
- return -err;
+ return err;
}
return sk;
}
@@ -351,18 +349,17 @@ static inline int tty_open(const char *tty, struct termios *ti)

sk = open(tty, O_RDWR | O_NOCTTY);
if (sk < 0) {
- err = errno;
- error("Can't open TTY %s: %s(%d)", tty, strerror(err), err);
- return -err;
+ err = -errno;
+ error("Can't open TTY %s: %s(%d)", tty, strerror(-err), -err);
+ return err;
}

if (ti && tcsetattr(sk, TCSANOW, ti) < 0) {
- err = errno;
+ err = -errno;
error("Can't change serial settings: %s(%d)",
- strerror(err), err);
+ strerror(-err), -err);
close(sk);
- errno = err;
- return -err;
+ return err;
}

return sk;
--
1.7.0.4


2011-11-16 13:19:52

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 05/17] audio: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
audio/ctl_bluetooth.c | 13 +++++++------
audio/unix.c | 21 ++++++++++++---------
2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/audio/ctl_bluetooth.c b/audio/ctl_bluetooth.c
index d941421..a16f476 100644
--- a/audio/ctl_bluetooth.c
+++ b/audio/ctl_bluetooth.c
@@ -257,18 +257,19 @@ static int bluetooth_read_event(snd_ctl_ext_t *ext, snd_ctl_elem_id_t *id,
struct bluetooth_data *data = ext->private_data;
char buf[BT_SUGGESTED_BUFFER_SIZE];
struct bt_control_ind *ind = (void *) buf;
- int ret;
+ int err;
const char *type, *name;

DBG("ext %p id %p", ext, id);

memset(buf, 0, sizeof(buf));

- ret = recv(data->sock, ind, BT_SUGGESTED_BUFFER_SIZE, MSG_DONTWAIT);
- if (ret < 0) {
- SNDERR("Failed while receiving data: %s (%d)",
- strerror(errno), errno);
- return -errno;
+ err = recv(data->sock, ind, BT_SUGGESTED_BUFFER_SIZE, MSG_DONTWAIT);
+ if (err < 0) {
+ err = -errno;
+ SNDERR("Failed while receiving data: %s (%d)", strerror(-err),
+ -err);
+ return err;
}

type = bt_audio_strtype(ind->h.type);
diff --git a/audio/unix.c b/audio/unix.c
index 4e0a6f9..5199831 100644
--- a/audio/unix.c
+++ b/audio/unix.c
@@ -1864,25 +1864,28 @@ int unix_init(void)

sk = socket(PF_LOCAL, SOCK_STREAM, 0);
if (sk < 0) {
- err = errno;
- error("Can't create unix socket: %s (%d)", strerror(err), err);
- return -err;
+ err = -errno;
+ error("Can't create unix socket: %s (%d)", strerror(-err),
+ -err);
+ return err;
}

if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- error("Can't bind unix socket: %s (%d)", strerror(errno),
- errno);
+ err = -errno;
+ error("Can't bind unix socket: %s (%d)", strerror(-err),
+ -err);
close(sk);
- return -1;
+ return err;
}

set_nonblocking(sk);

if (listen(sk, 1) < 0) {
- error("Can't listen on unix socket: %s (%d)",
- strerror(errno), errno);
+ err = -errno;
+ error("Can't listen on unix socket: %s (%d)", strerror(-err),
+ -err);
close(sk);
- return -1;
+ return err;
}

unix_sock = sk;
--
1.7.0.4


2011-11-16 13:19:51

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 04/17] audio/PCM: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
audio/pcm_bluetooth.c | 21 +++++++++++----------
1 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/audio/pcm_bluetooth.c b/audio/pcm_bluetooth.c
index e633d1a..4d85f33 100644
--- a/audio/pcm_bluetooth.c
+++ b/audio/pcm_bluetooth.c
@@ -420,9 +420,10 @@ static int bluetooth_prepare(snd_pcm_ioplug_t *io)
}

/* wake up any client polling at us */
- err = write(data->pipefd[1], &c, 1);
- if (err < 0)
+ if (write(data->pipefd[1], &c, 1) < 0) {
+ err = -errno;
return err;
+ }

return 0;
}
@@ -968,7 +969,7 @@ static snd_pcm_sframes_t bluetooth_a2dp_read(snd_pcm_ioplug_t *io,

static int avdtp_write(struct bluetooth_data *data)
{
- int ret = 0;
+ int err;
struct rtp_header *header;
struct rtp_payload *payload;
struct bluetooth_a2dp *a2dp = &data->a2dp;
@@ -985,10 +986,10 @@ static int avdtp_write(struct bluetooth_data *data)
header->timestamp = htonl(a2dp->nsamples);
header->ssrc = htonl(1);

- ret = send(data->stream.fd, a2dp->buffer, a2dp->count, MSG_DONTWAIT);
- if (ret < 0) {
- DBG("send returned %d errno %s.", ret, strerror(errno));
- ret = -errno;
+ err = send(data->stream.fd, a2dp->buffer, a2dp->count, MSG_DONTWAIT);
+ if (err < 0) {
+ err = -errno;
+ DBG("send failed: %s (%d)", strerror(-err), -err);
}

/* Reset buffer of data to send */
@@ -997,7 +998,7 @@ static int avdtp_write(struct bluetooth_data *data)
a2dp->samples = 0;
a2dp->seq_num++;

- return ret;
+ return err;
}

static snd_pcm_sframes_t bluetooth_a2dp_write(snd_pcm_ioplug_t *io,
@@ -1546,7 +1547,7 @@ static int audioservice_send(int sk, const bt_audio_msg_header_t *msg)
else {
err = -errno;
SNDERR("Error sending data to audio service: %s(%d)",
- strerror(errno), errno);
+ strerror(-err), -err);
}

return err;
@@ -1567,7 +1568,7 @@ static int audioservice_recv(int sk, bt_audio_msg_header_t *inmsg)
if (ret < 0) {
err = -errno;
SNDERR("Error receiving IPC data from bluetoothd: %s (%d)",
- strerror(errno), errno);
+ strerror(-err), -err);
} else if ((size_t) ret < sizeof(bt_audio_msg_header_t)) {
SNDERR("Too short (%d bytes) IPC packet from bluetoothd", ret);
err = -EINVAL;
--
1.7.0.4


2011-11-16 13:19:50

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 03/17] audio/IPC: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.

This commit also changes places where errno can be replaced with -err
(for consistency) and one perror() usage error.
---
audio/ipc.c | 18 +++++++++---------
test/ipctest.c | 12 ++++++++----
2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/audio/ipc.c b/audio/ipc.c
index 669eeef..02d956b 100644
--- a/audio/ipc.c
+++ b/audio/ipc.c
@@ -56,19 +56,19 @@ int bt_audio_service_open(void)

sk = socket(PF_LOCAL, SOCK_STREAM, 0);
if (sk < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "%s: Cannot open socket: %s (%d)\n",
- __FUNCTION__, strerror(err), err);
- errno = err;
+ __FUNCTION__, strerror(-err), -err);
+ errno = -err;
return -1;
}

if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "%s: connect() failed: %s (%d)\n",
- __FUNCTION__, strerror(err), err);
+ __FUNCTION__, strerror(-err), -err);
close(sk);
- errno = err;
+ errno = -err;
return -1;
}

@@ -96,10 +96,10 @@ int bt_audio_service_get_data_fd(int sk)

ret = recvmsg(sk, &msgh, 0);
if (ret < 0) {
- err = errno;
+ err = -errno;
fprintf(stderr, "%s: Unable to receive fd: %s (%d)\n",
- __FUNCTION__, strerror(err), err);
- errno = err;
+ __FUNCTION__, strerror(-err), -err);
+ errno = -err;
return -1;
}

diff --git a/test/ipctest.c b/test/ipctest.c
index 9fdfac4..4968443 100644
--- a/test/ipctest.c
+++ b/test/ipctest.c
@@ -139,7 +139,7 @@ static int service_send(struct userdata *u, const bt_audio_msg_header_t *msg)
else {
err = -errno;
ERR("Error sending data to audio service: %s(%d)",
- strerror(errno), errno);
+ strerror(-err), -err);
}

return err;
@@ -171,7 +171,7 @@ static int service_recv(struct userdata *u, bt_audio_msg_header_t *rsp)
} else {
err = -errno;
ERR("Error receiving data from audio service: %s(%d)",
- strerror(errno), errno);
+ strerror(-err), -err);
}

return err;
@@ -215,8 +215,12 @@ static int init_bt(struct userdata *u)

u->service_fd = bt_audio_service_open();
if (u->service_fd <= 0) {
- perror(strerror(errno));
- return errno;
+ int err = -errno;
+
+ ERR("bt_audio_service_open() failed: %s (%d)", strerror(-err),
+ -err);
+
+ return err;
}

return 0;
--
1.7.0.4


2011-11-16 13:19:49

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 02/17] AVDTP: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.
---
audio/gstavdtpsink.c | 22 ++++++++++++----------
1 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/audio/gstavdtpsink.c b/audio/gstavdtpsink.c
index 0f3abf3..f379469 100644
--- a/audio/gstavdtpsink.c
+++ b/audio/gstavdtpsink.c
@@ -279,10 +279,10 @@ static gint gst_avdtp_sink_bluetooth_recvmsg_fd(GstAvdtpSink *sink)
g_io_channel_unix_get_fd(sink->server));

if (ret < 0) {
- err = errno;
+ err = -errno;
GST_ERROR_OBJECT(sink, "Unable to receive fd: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}

sink->stream = g_io_channel_unix_new(ret);
@@ -1449,9 +1449,9 @@ static gboolean gst_avdtp_sink_start(GstBaseSink *basesink)

sk = bt_audio_service_open();
if (sk <= 0) {
- err = errno;
+ err = -errno;
GST_ERROR_OBJECT(self, "Cannot open connection to bt "
- "audio service: %s %d", strerror(err), err);
+ "audio service: %s %d", strerror(-err), -err);
goto failed;
}

@@ -1841,7 +1841,7 @@ static int gst_avdtp_sink_audioservice_send(GstAvdtpSink *self,
ssize_t written;
const char *type, *name;
uint16_t length;
- int fd;
+ int fd, err;

length = msg->length ? msg->length : BT_SUGGESTED_BUFFER_SIZE;

@@ -1849,9 +1849,10 @@ static int gst_avdtp_sink_audioservice_send(GstAvdtpSink *self,

written = write(fd, msg, length);
if (written < 0) {
+ err = -errno;
GST_ERROR_OBJECT(self, "Error sending data to audio service:"
- " %s", strerror(errno));
- return -errno;
+ " %s", strerror(-err));
+ return err;
}

type = bt_audio_strtype(msg->type);
@@ -1876,9 +1877,10 @@ static int gst_avdtp_sink_audioservice_recv(GstAvdtpSink *self,

bytes_read = read(fd, inmsg, length);
if (bytes_read < 0) {
+ err = -errno;
GST_ERROR_OBJECT(self, "Error receiving data from "
- "audio service: %s", strerror(errno));
- return -errno;
+ "audio service: %s", strerror(-err));
+ return err;
}

type = bt_audio_strtype(inmsg->type);
--
1.7.0.4


2011-11-16 13:19:48

by Anderson Lizardo

[permalink] [raw]
Subject: [PATCH BlueZ 01/17] AVCTP: Fix errno handling convention

Variables which are assigned to the errno variable (usually called
"err") should be negative, and "-err" should be used where a positive
value is needed.

This patch also fixes a bug related to not using an "err" variable to
store the errno value before calling external library code.
---
audio/avctp.c | 31 +++++++++++++------------------
1 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/audio/avctp.c b/audio/avctp.c
index df3b2b8..5bd5db1 100644
--- a/audio/avctp.c
+++ b/audio/avctp.c
@@ -502,10 +502,10 @@ static int uinput_create(char *name)
if (fd < 0) {
fd = open("/dev/misc/uinput", O_RDWR);
if (fd < 0) {
- err = errno;
+ err = -errno;
error("Can't open input device: %s (%d)",
- strerror(err), err);
- return -err;
+ strerror(-err), -err);
+ return err;
}
}
}
@@ -520,12 +520,11 @@ static int uinput_create(char *name)
dev.id.version = 0x0000;

if (write(fd, &dev, sizeof(dev)) < 0) {
- err = errno;
+ err = -errno;
error("Can't write device information: %s (%d)",
- strerror(err), err);
+ strerror(-err), -err);
close(fd);
- errno = err;
- return -err;
+ return err;
}

ioctl(fd, UI_SET_EVBIT, EV_KEY);
@@ -537,12 +536,11 @@ static int uinput_create(char *name)
ioctl(fd, UI_SET_KEYBIT, key_map[i].uinput);

if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
- err = errno;
+ err = -errno;
error("Can't create uinput device: %s (%d)",
- strerror(err), err);
+ strerror(-err), -err);
close(fd);
- errno = err;
- return -err;
+ return err;
}

return fd;
@@ -895,7 +893,7 @@ int avctp_send_vendordep(struct avctp *session, uint8_t transaction,
struct avctp_header *avctp;
struct avc_header *avc;
uint8_t *pdu;
- int sk, err;
+ int sk, err = 0;
uint16_t size;

if (session->state != AVCTP_STATE_CONNECTED)
@@ -920,14 +918,11 @@ int avctp_send_vendordep(struct avctp *session, uint8_t transaction,

memcpy(pdu, operands, operand_count);

- err = write(sk, buf, size);
- if (err < 0) {
- g_free(buf);
- return -errno;
- }
+ if (write(sk, buf, size) < 0)
+ err = -errno;

g_free(buf);
- return 0;
+ return err;
}

unsigned int avctp_add_state_cb(avctp_state_cb cb, void *user_data)
--
1.7.0.4