Skip to contentSkip to conversation

getdents.2: Use 'ssize_t' instead of 'int'

PATCH In review Alejandro Colomar 2 participants 7 messages Oct 28, 2020 – Oct 29, 2020 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 getdents.2: Use 'ssize_t' instead of 'int'

The glibc wrapper for getdents64() uses ssize_t. And let's use it also for getdents().
It makes more sense than int:
It's a count of bytes, and can report an error (-1).

---
 man2/getdents.2 | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
man2/getdents.2 +6 -4
index a187fbcef..d660f1bc1 100644
@@ -33,13 +33,13 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "ssize_t getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
+.BI "ssize_t getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
 .BI "             unsigned int " count );
 .fi
 .PP
@@ -266,6 +266,7 @@ inode#    file type  d_reclen  d_off   d_name
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
+#include <sys/types.h>
 
 #define handle_error(msg) \e
         do { perror(msg); exit(EXIT_FAILURE); } while (0)
@@ -282,7 +283,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    ssize_t nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +303,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (ssize_t bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
Signature · 1 line
2.28.0
#1 lore raw

Hi Alex,

Quoted 1 line · Alejandro Colomar

The glibc wrapper for getdents64() uses ssize_t.

It also changed the types for the arguments, so those need to be fixed too.

Quoted 1 line

And let's use it also for getdents().

I actually think we should *not* change that. So long as their is no wrapper, we should show pretty much what the ABI exposes. (That makes me think that the return type should really be long; see what you think about DEFINE_SYSCALL3 in the kernel sources.)

And you added an include for <sys/types.h>. I'm not sure whether that's needed, but it should be explained in the commit message.

Thanks,

Michael

Quoted 56 lines

It makes more sense than int:
It's a count of bytes, and can report an error (-1).

---
 man2/getdents.2 | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
man2/getdents.2 +6 -4
index a187fbcef..d660f1bc1 100644
@@ -33,13 +33,13 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "ssize_t getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
+.BI "ssize_t getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
 .BI "             unsigned int " count );
 .fi
 .PP
@@ -266,6 +266,7 @@ inode#    file type  d_reclen  d_off   d_name
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
+#include <sys/types.h>
 
 #define handle_error(msg) \e
         do { perror(msg); exit(EXIT_FAILURE); } while (0)
@@ -282,7 +283,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    ssize_t nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +303,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (ssize_t bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#2 lore raw

[[ CC += linux-kernel, libc-alpha ]]

Hi Michael,

Quoted 7 lines · Michael Kerrisk (man-pages)

Hi Alex,

Quoted 1 line · Alejandro Colomar

The glibc wrapper for getdents64() uses ssize_t.

It also changed the types for the arguments, so those need to be fixed too.

I saw that, but I sent an email to glibc asking if it was a bug. I'll hold this patch, and I'll CC this list in that other thread.

Quoted 7 lines
Quoted 2 lines

And let's use it also for getdents().

I actually think we should *not* change that. So long as their is no wrapper, we should show pretty much what the ABI exposes. (That makes me think that the return type should really be long; see what you think about DEFINE_SYSCALL3 in the kernel sources.)

Agreed; then getdents() (and all syscalls without a wrapper) should use 'long', as you said.

BTW: My mind explodes when I try to read through SYSCALL_DEFINEx :)

Quoted 4 lines

And you added an include for <sys/types.h>. I'm not sure whether that's needed, but it should be explained in the commit message.

Agreed.
I should be able to check if it's needed, in ssize_t(3) :p ... Actually it's not needed (<stdio.h> already provides it).

Thanks,

Alex

#3 lore raw

PATCH v2 1/1 getdents.2: Use appropriate types

getdents():
This function has no glibc wrapper.
As such, we should use the same types the Linux kernel uses:
Use 'long' as the return type.

getdents64():
The glibc wrapper uses ssize_t for the return type, and 'size_t' for the count argument.

---
 man2/getdents.2 | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
man2/getdents.2 +6 -5
index a187fbcef..e14627e6e 100644
@@ -33,14 +33,14 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "long getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
-.BI "             unsigned int " count );
+.BI "ssize_t getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
+.BI "             size_t " count );
 .fi
 .PP
 .IR Note :
@@ -282,7 +282,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    long nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +302,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (long bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
Signature · 1 line
2.28.0
#4 lore raw

Re: [PATCH v2] getdents.2: Use appropriate types

Hi Alex,

Quoted 9 lines · Alejandro Colomar

getdents():
This function has no glibc wrapper.
As such, we should use the same types the Linux kernel uses:
Use 'long' as the return type.

getdents64():
The glibc wrapper uses ssize_t for the return type, and 'size_t' for the count argument.

Take a look in the header file at the argument types for getdents64(); there's still some changes needed.

Thanks,

Michael

Quoted 49 lines
---
 man2/getdents.2 | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
man2/getdents.2 +6 -6
index a187fbcef..e14627e6e 100644
@@ -33,14 +33,14 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "long getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
-.BI "             unsigned int " count );
+.BI "ssize_t getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
+.BI "             size_t " count );
 .fi
 .PP
 .IR Note :
@@ -282,7 +282,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    long nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +302,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (long bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
--

2.28.0

Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
#5 lore raw

PATCH v3 1/1 getdents.2: Use appropriate types

getdents():
This function has no glibc wrapper.
As such, we should use the same types the Linux kernel uses:
Use 'long' as the return type.

getdents64():
The glibc wrapper uses:
ssize_t getdents64(int, void *, size_t);

---

Hi Michael,

Sorry, I'm being a bit distracted these days :)
It should be good enough now, I think.

Cheers,

Alex

 man2/getdents.2 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
man2/getdents.2 +5 -5
index a187fbcef..ed3bb40b1 100644
@@ -33,14 +33,13 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "long getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
-.BI "             unsigned int " count );
+.BI "ssize_t getdents64(int " fd ", void *" dirp ", size_t " count );
 .fi
 .PP
 .IR Note :
@@ -282,7 +281,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    long nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +301,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (long bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
Signature · 1 line
2.28.0
#6 lore raw

Re: [PATCH v3] getdents.2: Use appropriate types

Quoted 10 lines · Alejandro Colomar

getdents():
This function has no glibc wrapper.
As such, we should use the same types the Linux kernel uses:
Use 'long' as the return type.

getdents64():
The glibc wrapper uses:
ssize_t getdents64(int, void *, size_t);

Thanks, Alex. Applied.

Cheers,

Michael

Quoted 55 lines

---

Hi Michael,

Sorry, I'm being a bit distracted these days :)
It should be good enough now, I think.

Cheers,

Alex

 man2/getdents.2 | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
man2/getdents.2 +5 -5
index a187fbcef..ed3bb40b1 100644
@@ -33,14 +33,13 @@
 getdents, getdents64 \- get directory entries
 .SH SYNOPSIS
 .nf
-.BI "int getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
+.BI "long getdents(unsigned int " fd ", struct linux_dirent *" dirp ,
 .BI "             unsigned int " count );
 .PP
 .BR "#define _GNU_SOURCE" "        /* See feature_test_macros(7) */"
 .B #include <dirent.h>
 .PP
-.BI "int getdents64(unsigned int " fd ", struct linux_dirent64 *" dirp ,
-.BI "             unsigned int " count );
+.BI "ssize_t getdents64(int " fd ", void *" dirp ", size_t " count );
 .fi
 .PP
 .IR Note :
@@ -282,7 +281,8 @@ struct linux_dirent {
 int
 main(int argc, char *argv[])
 {
-    int fd, nread;
+    int fd;
+    long nread;
     char buf[BUF_SIZE];
     struct linux_dirent *d;
     char d_type;
@@ -301,7 +301,7 @@ main(int argc, char *argv[])
 
         printf("\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- nread=%d \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en", nread);
         printf("inode#    file type  d_reclen  d_off   d_name\en");
-        for (int bpos = 0; bpos < nread;) {
+        for (long bpos = 0; bpos < nread;) {
             d = (struct linux_dirent *) (buf + bpos);
             printf("%8ld  ", d\->d_ino);
             d_type = *(buf + bpos + d\->d_reclen \- 1);
Signature · 3 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/