[klibc] Problems with fread

Martin Schlemmer azarah at nosferatu.za.org
Tue Feb 7 21:50:47 PST 2006


On Tue, 2006-02-07 at 07:49 -0800, H. Peter Anvin wrote:
> Martin Schlemmer wrote:
> > Hi,
> > 
> > I have a port here of device-mapper to use klibc (still trying to get it
> > upstream, but oh well).  It used to work fine, but with latish klibc's I
> > seem to have issues with fread().
> > 
> > Same code with glibc works just fine, but with klibc fread only seems to
> > read the first two lines when it tries to get the major device number
> > for misc and device-mapper from /proc/devices - then it keeps returning
> > 0.
> > 
> > Anybody have seen similar issues, or know of related changes to fread()
> > that could be causing this?
> > 
> 
> Okay, you're using fread()... what's your buffer size?

Well, since you have to check line for line, and klibc did not provide
fscanf() which was originally used, I used fread+sscanf and reading one
byte at a time into a 300 byte buffer.

-----
@@ -122,29 +122,34 @@ static int _get_proc_number(const char *
                            uint32_t *number)
 {
        FILE *fl;
-       char nm[256];
-       int c;
-       uint32_t num;
+       char nm[256], buf[300];
+       uint32_t num, size;

        if (!(fl = fopen(file, "r"))) {
                log_error("%s: fopen failed: %s", file, strerror(errno));
                return 0;
        }

-       while (!feof(fl)) {
-               if (fscanf(fl, "%d %255s\n", &num, &nm[0]) == 2) {
+       /* Use fread+sscanf for klibc compatibility. */
+       do {
+               size = 0;
+               do {
+                       num = fread(&buf[size], sizeof(char), 1, fl);
+                       if (num > 0)
+                               size++;
+               } while (num > 0 && buf[size - 1] != '\n');
+               buf[size] = '\0';
+               if (sscanf(buf, "%d %255s\n", number, &nm[0]) == 2) {
                        if (!strcmp(name, nm)) {
                                if (number) {
-                                       *number = num;
+                                       *number = #
                                        fclose(fl);
                                        return 1;
                                }
                                dm_bit_set(_dm_bitset, num);
                        }
-               } else do {
-                       c = fgetc(fl);
-               } while (c != EOF && c != '\n');
-       }
+               }
+       } while (num > 0);
        fclose(fl);

        if (number) {
-----

>   Can you do an strace?
> 

Sure.

-----
execve("./dmsetup/dmsetup", ["./dmsetup/dmsetup", "table"], [/* 53 vars */]) = 0
mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) = 0x2af148599000
open("/proc/devices", O_RDONLY)         = 3
read(3, "C", 1)                         = 1
read(3, "h", 1)                         = 1
read(3, "a", 1)                         = 1
read(3, "r", 1)                         = 1
read(3, "a", 1)                         = 1
read(3, "c", 1)                         = 1
read(3, "t", 1)                         = 1
read(3, "e", 1)                         = 1
read(3, "r", 1)                         = 1
read(3, " ", 1)                         = 1
read(3, "d", 1)                         = 1
read(3, "e", 1)                         = 1
read(3, "v", 1)                         = 1
read(3, "i", 1)                         = 1
read(3, "c", 1)                         = 1
read(3, "e", 1)                         = 1
read(3, "s", 1)                         = 1
read(3, ":", 1)                         = 1
read(3, "\n", 1)                        = 1
read(3, " ", 1)                         = 1
read(3, " ", 1)                         = 1
read(3, "1", 1)                         = 1
read(3, " ", 1)                         = 1
read(3, "m", 1)                         = 1
read(3, "e", 1)                         = 1
read(3, "m", 1)                         = 1
read(3, "\n", 1)                        = 1
read(3, "", 1)                          = 0
close(3)                                = 0
write(2, "/proc/devices: No entry for misc"..., 38/proc/devices: No entry for misc found) = 38
write(2, "\n", 1
)                       = 1
write(1, "Failed to get device number!", 28Failed to get device number!) = 28
write(1, "\n", 1
)                       = 1
write(2, "Is device-mapper driver missing "..., 44Is device-mapper driver missing from kernel?) = 44
write(2, "\n", 1
)                       = 1
stat("/dev/mapper/control", {st_mode=S_IFCHR|0660, st_rdev=makedev(10, 63), ...}) = 0
open("/dev/mapper/control", O_RDWR)     = 3
ioctl(3, DM_VERSION, 0x2af148599160)    = 0
ioctl(3, DM_LIST_DEVICES, 0x2af1485990c0) = 0
ioctl(3, DM_TABLE_STATUS, 0x2af14859d1c0) = 0
write(1, "nsw1: ", 6nsw1: )                   = 6
write(1, "0 312398172 striped 2 256 8:0 10"..., 480 312398172 striped 2 256 8:0 102400 8:16 102400) = 48
write(1, "\n", 1
)                       = 1
ioctl(3, DM_TABLE_STATUS, 0x2af14859d1c0) = 0
write(1, "nsw0: ", 6nsw0: )                   = 6
write(1, "0 102400 linear 8:0 0", 210 102400 linear 8:0 0)   = 21
write(1, "\n", 1
)                       = 1
ioctl(3, DM_TABLE_STATUS, 0x2af14859d1c0) = 0
write(1, "nsw1p2: ", 8nsw1p2: )                 = 8
write(1, "0 308367675 linear 254:1 4016250", 320 308367675 linear 254:1 4016250) = 32
write(1, "\n", 1
)                       = 1
ioctl(3, DM_TABLE_STATUS, 0x2af14859d1c0) = 0
write(1, "nsw0p1: ", 8nsw0p1: )                 = 8
write(1, "0 96327 linear 254:0 63", 230 96327 linear 254:0 63) = 23
write(1, "\n", 1
)                       = 1
ioctl(3, DM_TABLE_STATUS, 0x2af14859d1c0) = 0
write(1, "nsw1p1: ", 8nsw1p1: )                 = 8
write(1, "0 4016187 linear 254:1 63", 250 4016187 linear 254:1 63) = 25
write(1, "\n", 1
)                       = 1
_exit(0)                                = ?
Process 30121 detached
-----

Returns 0 after reading the first two lines.  I tried to just loop and
print one byte at an time without the rest of above's patch snippit, but
as clock work, it stops after the first two lines.

x86_64 btw.


Thanks,

-- 
Martin Schlemmer

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://www.zytor.com/pipermail/klibc/attachments/20060207/b1e4a6c6/attachment.bin


More information about the klibc mailing list