Skip to contentSkip to conversation

minor trinity patch

DISCUSSION Nico Golde 2 participants 6 messages May 22, 2013 – Jun 24, 2013 6 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Nico Golde

author
#6 lore raw

Hi,
I'm not sure if you would like to change this....
In devices.c you scan for the device name in /proc/devices using the %as string.
On bionic this doesn't seem to work and therefore no devices are found and because of this no ioctl group ever matches.

I changed the code the following way:
@@ -21,7 +22,8 @@ static size_t bldevs, chrdevs, miscdevs;
static void parse_proc_devices(void)

 {
        FILE *fp;
-       char *name, *line = NULL;
+       char *line = NULL;
+       char name[32];
        size_t n = 0;
        int block, major;
        void *new;
@@ -33,30 +35,31 @@ static void parse_proc_devices(void)
        block = 0;

        while (getline(&line, &n, fp) >= 0) {
+               memset(name, 0, sizeof(name));
                if (strcmp("Block devices:\n", line) == 0)
                        block = 1;
-               else if (sscanf(line, "%d %as", &major, &name) == 2) {
+               else if (strcmp("Character devices:\n", line) == 0)
+                       block = 0;
+               else if (sscanf(line, "%d %s", &major, &name) == 2) {
                        if (block) {
                                new = realloc(block_devs, (bldevs+1)*sizeof(*block_devs));
                                if (!new) {
-                                       free(name);
                                        continue;
                                }
                                block_devs = new;
                                block_devs[bldevs].major = major;
                                block_devs[bldevs].minor = 0;
-                               block_devs[bldevs].name = name;
+                               block_devs[bldevs].name = strdup(name);
                                bldevs++;
                        } else {
                                new = realloc(char_devs, (chrdevs+1)*sizeof(*char_devs));
                                if (!new) {
-                                       free(name);
                                        continue;
                                }
                                char_devs = new;
                                char_devs[chrdevs].major = major;
                                char_devs[chrdevs].minor = 0;
-                               char_devs[chrdevs].name = name;
+                               char_devs[chrdevs].name = strdup(name);
                                chrdevs++;
                        }
                }

I also added the additional strcmp just to make sure that the code works also if the order of character devices/block devices in /proc/devices changes at some point.

Cheers
Nico

#7 lore raw

2013/5/22 Nico Golde <[email protected]>:

Quoted 6 lines

Hi,
I'm not sure if you would like to change this....
In devices.c you scan for the device name in /proc/devices using the %as string.
On bionic this doesn't seem to work and therefore no devices are found and because of this no ioctl group ever matches.

No objections to changing this.

Review static size_t bldevs, chrdevs, miscdevs; · line 26

Quoted 8 lines

I changed the code the following way:

(quoted diff) +2 -1
@@ -21,7 +22,8 @@ static size_t bldevs, chrdevs, miscdevs;
 static void parse_proc_devices(void)
 {
        FILE *fp;
-       char *name, *line = NULL;
+       char *line = NULL;
+       char name[32];

It would be IMO preferable to not hardcode the max length.

Quoted 48 lines
(quoted diff) +6 -5
        size_t n = 0;
        int block, major;
        void *new;
@@ -33,30 +35,31 @@ static void parse_proc_devices(void)
        block = 0;
 
        while (getline(&line, &n, fp) >= 0) {
+               memset(name, 0, sizeof(name));
                if (strcmp("Block devices:\n", line) == 0)
                        block = 1;
-               else if (sscanf(line, "%d %as", &major, &name) == 2) {
+               else if (strcmp("Character devices:\n", line) == 0)
+                       block = 0;
+               else if (sscanf(line, "%d %s", &major, &name) == 2) {
                        if (block) {
                                new = realloc(block_devs, (bldevs+1)*sizeof(*block_devs));
                                if (!new) {
-                                       free(name);
                                        continue;
                                }
                                block_devs = new;
                                block_devs[bldevs].major = major;
                                block_devs[bldevs].minor = 0;
-                               block_devs[bldevs].name = name;
+                               block_devs[bldevs].name = strdup(name);
                                bldevs++;
                        } else {
                                new = realloc(char_devs, (chrdevs+1)*sizeof(*char_devs));
                                if (!new) {
-                                       free(name);
                                        continue;
                                }
                                char_devs = new;
                                char_devs[chrdevs].major = major;
                                char_devs[chrdevs].minor = 0;
-                               char_devs[chrdevs].name = name;
+                               char_devs[chrdevs].name = strdup(name);
                                chrdevs++;
                        }
                }

I also added the additional strcmp just to make sure that the code works also if the order of character devices/block devices in /proc/devices changes at some point.

Cheers
Nico

author
#8 lore raw

Hi,

Quoted 11 lines · Tommi Rantala

2013/5/22 Nico Golde <[email protected]>:

Review static size_t bldevs, chrdevs, miscdevs; · line 26

Quoted 8 lines

I changed the code the following way:

(quoted diff) +2 -1
@@ -21,7 +22,8 @@ static size_t bldevs, chrdevs, miscdevs;
 static void parse_proc_devices(void)
 {
        FILE *fp;
-       char *name, *line = NULL;
+       char *line = NULL;
+       char name[32];

It would be IMO preferable to not hardcode the max length.

I agree, this is actually left over from quickly changing this.
Are you aware of a proper maximum defined in the kernel headers or do we want to allocate this completely dynamically?

Cheers
Nico

#9 lore raw

2013/5/24 Nico Golde <[email protected]>:

Quoted 14 lines

Hi,

Quoted 11 lines · Tommi Rantala

2013/5/22 Nico Golde <[email protected]>:

Review static size_t bldevs, chrdevs, miscdevs; · line 26

Quoted 8 lines

I changed the code the following way:

(quoted diff) +2 -1
@@ -21,7 +22,8 @@ static size_t bldevs, chrdevs, miscdevs;
 static void parse_proc_devices(void)
 {
        FILE *fp;
-       char *name, *line = NULL;
+       char *line = NULL;
+       char name[32];

It would be IMO preferable to not hardcode the max length.

Quoted 3 lines

I agree, this is actually left over from quickly changing this.
Are you aware of a proper maximum defined in the kernel headers or do we want to allocate this completely dynamically?

dynamic please!

Quoted 6 lines

Cheers
Nico
--
To unsubscribe from this list: send the line "unsubscribe trinity" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#10 lore raw

Hi,

Quoted 14 lines · Tommi Rantala

2013/5/24 Nico Golde <[email protected]>:

Quoted 13 lines · Tommi Rantala
Quoted 11 lines

2013/5/22 Nico Golde <[email protected]>:

Review static size_t bldevs, chrdevs, miscdevs; · line 26

Quoted 8 lines

I changed the code the following way:

(quoted diff) +2 -1
@@ -21,7 +22,8 @@ static size_t bldevs, chrdevs, miscdevs;
 static void parse_proc_devices(void)
 {
        FILE *fp;
-       char *name, *line = NULL;
+       char *line = NULL;
+       char name[32];

It would be IMO preferable to not hardcode the max length.

Quoted 5 lines
Quoted 3 lines

I agree, this is actually left over from quickly changing this.
Are you aware of a proper maximum defined in the kernel headers or do we want to allocate this completely dynamically?

dynamic please!

Sorry I had no time to look into this for a while. Patch attached.

Cheers
Nico

Signature · 1 line
Nico Golde - XMPP: [email protected] - GPG: 0xA0A0AAAA

From 9acc7c59c6ae2643ffba4edc17b04477f5cdaee0 Mon Sep 17 00:00:00 2001
From: Nico Golde <[email protected]>
Date: Fri, 21 Jun 2013 15:18:58 +0200
Subject: [PATCH] devices.c: dont use %as scanf format string, but instead
allocate and parse the device name manually for compatibility with non-glibc libcs

---
 devices.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
devices.c +9 -2
index 43433bc..d3916f9 100644
@@ -21,7 +21,7 @@ static size_t bldevs, chrdevs, miscdevs;
 static void parse_proc_devices(void)
 {
 	FILE *fp;
-	char *name, *line = NULL;
+	char *p, *name, *line = NULL;
 	size_t n = 0;
 	int block, major;
 	void *new;
@@ -35,7 +35,14 @@ static void parse_proc_devices(void)
 	while (getline(&line, &n, fp) >= 0) {
 		if (strcmp("Block devices:\n", line) == 0)
 			block = 1;
-		else if (sscanf(line, "%d %as", &major, &name) == 2) {
+		else if (strcmp("Character devices:\n", line) == 0)
+			block = 0;
+		else if (sscanf(line, "%d %*s", &major) == 1) {
+			if ((p = strrchr(line, ' ')) == NULL)
+				continue;
+			p++;
+			name = strdup(p);
+
 			if (block) {
 				new = realloc(block_devs, (bldevs+1)*sizeof(*block_devs));
 				if (!new) {
Signature · 1 line
1.7.10.4
author
#11 lore raw

Hi,

Quoted 2 lines · Nico Golde

Sorry I had no time to look into this for a while. Patch attached.

Attached is another patch on top of the previous one. I didn't see that the device name includes the newline so that map_dev() will fail. This was kind of hard to see unless you have a driver and you know how the command ids should look like.

Thanks
Nico

Signature · 1 line
Nico Golde - XMPP: [email protected] - GPG: 0xA0A0AAAA

From f850d64ba34c0a0d203912740c00fff03e8f8f3e Mon Sep 17 00:00:00 2001
From: Nico Golde <[email protected]>
Date: Mon, 24 Jun 2013 17:56:19 +0200
Subject: [PATCH] parse_proc_devices: cut off device string on newline

---
 devices.c |    2 ++
 1 file changed, 2 insertions(+)
devices.c +2 -0
index d3916f9..0166a53 100644
@@ -38,6 +38,8 @@ static void parse_proc_devices(void)
 		else if (strcmp("Character devices:\n", line) == 0)
 			block = 0;
 		else if (sscanf(line, "%d %*s", &major) == 1) {
+			if ((p = strchr(line, '\n')) != NULL)
+				*p = 0;
 			if ((p = strrchr(line, ' ')) == NULL)
 				continue;
 			p++;
Signature · 1 line
1.7.10.4