Skip to contentSkip to conversation

safe_macros: Add fallback pwrite() and pread() declarations

RFC PATCH New Cyril Hrubis 2 participants 6 messages Jan 2, 2017 – Jan 12, 2017

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

This fixes numerous "implicit function declaration" on slightly older distributions where pwrite() and pread() needs to defined either of _XOPEN_SOURCE or _GNU_SOURCE.

This is a bit hacky solution to the problem, but consider that:

o These functions has to be inlined in the header because the prototype contains off_t whose size depends on compile time settings

o Defining _GNU_SOURCE in the safe macros header is not a solution either since it has to be defined before we include any libc headers in order to take any effect

Also note that glibc uses __REDIRECT() assembler tricks or preprocessor rename to pread64() and pwrite64() in case that large file support is turned on but that should not matter to us since these functions are not used in 99% of the testcases and these that use it has to define _GNU_SOURCE either way and gets these tricks included, at least that is the theory.

---

This is the last patch that should fix the numerous "implicit function declaration" warnings. Either we do this or we need to create a separate safe_* header for the safe_pread and safe_pwrite. Or has anybody a better idea?

 include/tst_safe_macros.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
include/tst_safe_macros.h +10 -1
index df858be..b51c025 100644
@@ -98,8 +98,11 @@ static inline int safe_dup(const char *file, const int lineno,
 	safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
 
 /*
- * inline function that uses off_t since sizeof(off_t) depends on compile flags
+ * Fallback function definition, older glibc does not export it unless we
+ * define _XOPEN_SOURCE or _GNU_SOURCE.
  */
+ssize_t pread(int fd, void *buf, size_t count, off_t offset);
+
 static inline ssize_t safe_pread(const char *file, const int lineno,
 		char len_strict, int fildes, void *buf, size_t nbyte,
 		off_t offset)
@@ -167,6 +170,12 @@ pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);
 #define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
 	safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
 
+/*
+ * Fallback function definition, older glibc does not export it unless we
+ * define _XOPEN_SOURCE or _GNU_SOURCE.
+ */
+ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
+
 static inline ssize_t safe_pwrite(const char *file, const int lineno,
 		char len_strict, int fildes, const void *buf, size_t nbyte,
 		off_t offset)
Signature · 6 lines
2.7.3


-- 
Cyril Hrubis
[email protected]
#1 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

Hi!
I was thinking about this, and I think that we should rather go with a safer approach, i.e. moving the safe_pread/safe_pwrite into a separate header. Something as the following patch.

Signature · 9 lines
Cyril Hrubis
[email protected]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-safe_macros-Move-SAFE_PREAD-and-SAFE_PWRITE-to-separ.patch
Type: text/x-diff
Size: 6083 bytes
Desc: not available
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170111/27db3d8b/attachment.patch>
#2 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

----- Original Message -----

Quoted 9 lines

From: "Cyril Hrubis" <[email protected]>
To: [email protected]
Sent: Wednesday, 11 January, 2017 3:41:26 PM
Subject: Re: [LTP] [RFC] [PATCH] safe_macros: Add fallback pwrite() and pread() declarations

Hi!
I was thinking about this, and I think that we should rather go with a safer approach, i.e. moving the safe_pread/safe_pwrite into a separate header. Something as the following patch.

+1 for safer approach, I didn't know about glibc macros. Does this affect oldlib too?

Regards,
Jan

Quoted 6 lines

--
Cyril Hrubis
[email protected]

Quoted 7 lines

[Text Documents:0001-safe_macros-Move-SAFE_PREAD-and-SAFE_PWRITE-to-separ.patch]

--
Mailing list info: https://lists.linux.it/listinfo/ltp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/x-diff
Size: 6083 bytes
Desc: not available
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170112/b53422cd/attachment-0001.diff>

#3 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

Hi!

Quoted 6 lines
Quoted 3 lines

I was thinking about this, and I think that we should rather go with a safer approach, i.e. moving the safe_pread/safe_pwrite into a separate header. Something as the following patch.

+1 for safer approach, I didn't know about glibc macros. Does this affect oldlib too?

Not anymore, there was one user with zero offset which was converted to plain old SAFE_WRITE() and these two safe macros were then removed from oldlib safe macros in f4b028122.

Signature · 2 lines
Cyril Hrubis
[email protected]
#4 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

----- Original Message -----

Quoted 11 lines

From: "Cyril Hrubis" <[email protected]>
To: "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Thursday, 12 January, 2017 10:02:32 AM
Subject: Re: [LTP] [RFC] [PATCH] safe_macros: Add fallback pwrite() and pread() declarations

Hi!

Quoted 4 lines
Quoted 3 lines

I was thinking about this, and I think that we should rather go with a safer approach, i.e. moving the safe_pread/safe_pwrite into a separate header. Something as the following patch.

Quoted 6 lines
Quoted 2 lines

+1 for safer approach, I didn't know about glibc macros. Does this affect oldlib too?

Not anymore, there was one user with zero offset which was converted to plain old SAFE_WRITE() and these two safe macros were then removed from oldlib safe macros in f4b028122.

Ah, I see. No objections to patch from my side.

Regards,
Jan

Quoted 5 lines

--
Cyril Hrubis
[email protected]

#5 lore raw

RFC PATCH 1/1 safe_macros: Add fallback pwrite() and pread() declarations

Hi!

Quoted 5 lines
Quoted 3 lines

Not anymore, there was one user with zero offset which was converted to plain old SAFE_WRITE() and these two safe macros were then removed from oldlib safe macros in f4b028122.

Ah, I see. No objections to patch from my side.

Pushed with your ack.

Signature · 2 lines
Cyril Hrubis
[email protected]