public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Switch from numerical to defined constants for permissions.
@ 2020-11-02  1:49 Érico Nogueira
  2020-11-03 15:11 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Érico Nogueira @ 2020-11-02  1:49 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Érico Rolim

From: Érico Rolim <erico.erc@gmail.com>

Use defined constants for permission values. Also add fallback
definitions for them in system.h, to allow for compatibility with
systems that don't provide these macros.

Include system.h in all tests/ files that required it.

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
---

I'm a bit unsure about the ChangeLog entry, since these changes were
tree-wide.

 ChangeLog                      |  4 ++++
 debuginfod/debuginfod-client.c |  6 +++---
 lib/system.h                   | 12 ++++++++++++
 src/unstrip.c                  |  4 ++--
 tests/alldts.c                 |  3 ++-
 tests/arextract.c              |  2 +-
 tests/ecp.c                    |  2 +-
 tests/elfstrtab.c              |  7 ++++---
 tests/emptyfile.c              |  7 ++++---
 tests/fillfile.c               |  7 ++++---
 tests/newdata.c                |  3 ++-
 tests/update1.c                |  3 ++-
 tests/update2.c                |  3 ++-
 tests/update3.c                |  3 ++-
 tests/update4.c                |  3 ++-
 tests/vendorelf.c              |  5 +++--
 16 files changed, 50 insertions(+), 24 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 128da6c6..bac81431 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
+
+	* Switch from numerical to defined constants for permissions.
+
 2020-10-28  Mark Wielaard  <mark@klomp.org>
 
 	* configure.ac: Set version to 0.182.
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index 0e5177bc..ce1d819b 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -212,13 +212,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return 0;
 
   /* Create the cache and config files as necessary.  */
-  if (stat(cache_path, &st) != 0 && mkdir(cache_path, 0777) < 0)
+  if (stat(cache_path, &st) != 0 && mkdir(cache_path, ACCESSPERMS) < 0)
     return -errno;
 
   int fd = -1;
 
   /* init cleaning interval config file.  */
-  fd = open(interval_path, O_CREAT | O_RDWR, 0666);
+  fd = open(interval_path, O_CREAT | O_RDWR, DEFFILEMODE);
   if (fd < 0)
     return -errno;
 
@@ -227,7 +227,7 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
 
   /* init max age config file.  */
   if (stat(maxage_path, &st) != 0
-      && (fd = open(maxage_path, O_CREAT | O_RDWR, 0666)) < 0)
+      && (fd = open(maxage_path, O_CREAT | O_RDWR, DEFFILEMODE)) < 0)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
diff --git a/lib/system.h b/lib/system.h
index 292082bd..7b650f11 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -85,6 +85,18 @@
      __res; })
 #endif
 
+#ifndef ACCESSPERMS
+#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
+#endif
+
+#ifndef ALLPERMS
+#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
+#endif
+
+#ifndef DEFFILEMODE
+#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
+#endif
+
 static inline ssize_t __attribute__ ((unused))
 pwrite_retry (int fd, const void *buf, size_t len, off_t off)
 {
diff --git a/src/unstrip.c b/src/unstrip.c
index 0257d9cc..c99ee612 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -315,7 +315,7 @@ make_directories (const char *path)
   if (dir == NULL)
     error(EXIT_FAILURE, errno, _("memory exhausted"));
 
-  while (mkdir (dir, 0777) < 0 && errno != EEXIST)
+  while (mkdir (dir, ACCESSPERMS) < 0 && errno != EEXIST)
     {
       if (errno == ENOENT)
         make_directories (dir);
@@ -2192,7 +2192,7 @@ DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
 
       /* Copy the unstripped file and then modify it.  */
       int outfd = open (output_file, O_RDWR | O_CREAT,
-			  stripped_ehdr->e_type == ET_REL ? 0666 : 0777);
+			  stripped_ehdr->e_type == ET_REL ? DEFFILEMODE : ACCESSPERMS);
       if (outfd < 0)
 	error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file);
       Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
diff --git a/tests/alldts.c b/tests/alldts.c
index 28b3063c..3e9f9fe6 100644
--- a/tests/alldts.c
+++ b/tests/alldts.c
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -68,7 +69,7 @@ main (void)
   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
 
   /* Open the file.  */
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %m\n", fname);
diff --git a/tests/arextract.c b/tests/arextract.c
index 2c4dc758..936d7f55 100644
--- a/tests/arextract.c
+++ b/tests/arextract.c
@@ -95,7 +95,7 @@ Failed to get base address for the archive element: %s\n",
 	    }
 
 	  /* Open the output file.  */
-	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
+	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE);
 	  if (outfd == -1)
 	    {
 	      printf ("cannot open output file: %m");
diff --git a/tests/ecp.c b/tests/ecp.c
index 1df40a32..44a7bda2 100644
--- a/tests/ecp.c
+++ b/tests/ecp.c
@@ -43,7 +43,7 @@ main (int argc, char *argv[])
     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
 	   argv[1], elf_errmsg (-1));
 
-  int outfd = creat (argv[2], 0666);
+  int outfd = creat (argv[2], DEFFILEMODE);
   if (outfd == -1)
     error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
 
diff --git a/tests/elfstrtab.c b/tests/elfstrtab.c
index c27d6cfb..145a8aa9 100644
--- a/tests/elfstrtab.c
+++ b/tests/elfstrtab.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -134,7 +135,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0;
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -280,7 +281,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Read the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
@@ -349,7 +350,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/emptyfile.c b/tests/emptyfile.c
index 6d086246..a236fba1 100644
--- a/tests/emptyfile.c
+++ b/tests/emptyfile.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -67,7 +68,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -125,7 +126,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -208,7 +209,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/fillfile.c b/tests/fillfile.c
index 915e249d..186dcad0 100644
--- a/tests/fillfile.c
+++ b/tests/fillfile.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -201,7 +202,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -266,7 +267,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   /* Reread the ELF from disk now.  */
   printf ("Rereading %s\n", fname);
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -347,7 +348,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   // And read it in one last time.
   printf ("Rereading %s again\n", fname);
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/newdata.c b/tests/newdata.c
index 9af99564..0d662f68 100644
--- a/tests/newdata.c
+++ b/tests/newdata.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -243,7 +244,7 @@ check_elf (int class, int use_mmap)
 
   printf ("\ncheck_elf: %s\n", fname);
 
-  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 00666);
+  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 0DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot create `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update1.c b/tests/update1.c
index f4c14753..b7be4e5f 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -38,7 +39,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Ehdr *ehdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update2.c b/tests/update2.c
index 5805163d..71455633 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -39,7 +40,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Phdr *phdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update3.c b/tests/update3.c
index 7a4224dd..62f67f74 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(dwelf)
 
@@ -46,7 +47,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update4.c b/tests/update4.c
index a9bd4bf9..a703b592 100644
--- a/tests/update4.c
+++ b/tests/update4.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(dwelf)
 
@@ -50,7 +51,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/vendorelf.c b/tests/vendorelf.c
index bc13cce3..e1341c76 100644
--- a/tests/vendorelf.c
+++ b/tests/vendorelf.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -36,7 +37,7 @@ check_elf (const char *fname, int class, int use_mmap)
 {
   printf ("\nfname: %s\n", fname);
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -124,7 +125,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
-- 
2.29.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Switch from numerical to defined constants for permissions.
  2020-11-02  1:49 [PATCH] Switch from numerical to defined constants for permissions Érico Nogueira
@ 2020-11-03 15:11 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2020-11-03 15:11 UTC (permalink / raw)
  To: Érico Nogueira, elfutils-devel; +Cc: Érico Rolim

[-- Attachment #1: Type: text/plain, Size: 14148 bytes --]

Hi Érico,

On Sun, 2020-11-01 at 22:49 -0300, Érico Nogueira via Elfutils-devel
wrote:
> From: Érico Rolim <erico.erc@gmail.com>
> 
> Use defined constants for permission values. Also add fallback
> definitions for them in system.h, to allow for compatibility with
> systems that don't provide these macros.
> 
> Include system.h in all tests/ files that required it.

That is a nice cleanup.

> Signed-off-by: Érico Rolim <erico.erc@gmail.com>
> ---
> 
> I'm a bit unsure about the ChangeLog entry, since these changes were
> tree-wide.

I added them for you since I was reviewing the changes anyway.
I also made a few small tweaks, see below.

> diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-
> client.c
> index 0e5177bc..ce1d819b 100644
> --- a/debuginfod/debuginfod-client.c
> +++ b/debuginfod/debuginfod-client.c
> @@ -212,13 +212,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
>      return 0;
>  
>    /* Create the cache and config files as necessary.  */
> -  if (stat(cache_path, &st) != 0 && mkdir(cache_path, 0777) < 0)
> +  if (stat(cache_path, &st) != 0 && mkdir(cache_path, ACCESSPERMS) < 0)
>      return -errno;

OK for mkdir.
 
>    int fd = -1;
>  
>    /* init cleaning interval config file.  */
> -  fd = open(interval_path, O_CREAT | O_RDWR, 0666);
> +  fd = open(interval_path, O_CREAT | O_RDWR, DEFFILEMODE);
>    if (fd < 0)
>      return -errno;

OK for open with O_CREAT.

> @@ -227,7 +227,7 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
>  
>    /* init max age config file.  */
>    if (stat(maxage_path, &st) != 0
> -      && (fd = open(maxage_path, O_CREAT | O_RDWR, 0666)) < 0)
> +      && (fd = open(maxage_path, O_CREAT | O_RDWR, DEFFILEMODE)) < 0)
>      return -errno;

OK.

>    if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
> diff --git a/lib/system.h b/lib/system.h
> index 292082bd..7b650f11 100644
> --- a/lib/system.h
> +++ b/lib/system.h
> @@ -85,6 +85,18 @@
>       __res; })
>  #endif
>  
> +#ifndef ACCESSPERMS
> +#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
> +#endif
> +
> +#ifndef ALLPERMS
> +#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
> +#endif
> +
> +#ifndef DEFFILEMODE
> +#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
> +#endif

Definitions look correct.

>  static inline ssize_t __attribute__ ((unused))
>  pwrite_retry (int fd, const void *buf, size_t len, off_t off)
>  {
> diff --git a/src/unstrip.c b/src/unstrip.c
> index 0257d9cc..c99ee612 100644
> --- a/src/unstrip.c
> +++ b/src/unstrip.c
> @@ -315,7 +315,7 @@ make_directories (const char *path)
>    if (dir == NULL)
>      error(EXIT_FAILURE, errno, _("memory exhausted"));
>  
> -  while (mkdir (dir, 0777) < 0 && errno != EEXIST)
> +  while (mkdir (dir, ACCESSPERMS) < 0 && errno != EEXIST)

OK

>      {
>        if (errno == ENOENT)
>          make_directories (dir);
> @@ -2192,7 +2192,7 @@ DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
>  
>        /* Copy the unstripped file and then modify it.  */
>        int outfd = open (output_file, O_RDWR | O_CREAT,
> -			  stripped_ehdr->e_type == ET_REL ? 0666 : 0777);
> +			  stripped_ehdr->e_type == ET_REL ? DEFFILEMODE : ACCESSPERMS);

OK assumes executables and shared libraries should have execute perms
set. The line gets a but long though. I moved part of it to the next
line.

>        if (outfd < 0)
>  	error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file);
>        Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
> diff --git a/tests/alldts.c b/tests/alldts.c
> index 28b3063c..3e9f9fe6 100644
> --- a/tests/alldts.c
> +++ b/tests/alldts.c
> @@ -32,6 +32,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  
>  int
> @@ -68,7 +69,7 @@ main (void)
>    (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
>  
>    /* Open the file.  */
> -  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %m\n", fname);

OK.

> diff --git a/tests/arextract.c b/tests/arextract.c
> index 2c4dc758..936d7f55 100644
> --- a/tests/arextract.c
> +++ b/tests/arextract.c
> @@ -95,7 +95,7 @@ Failed to get base address for the archive element: %s\n",
>  	    }
>  
>  	  /* Open the output file.  */
> -	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
> +	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE);
>  	  if (outfd == -1)
>  	    {
>  	      printf ("cannot open output file: %m");

OK.

> diff --git a/tests/ecp.c b/tests/ecp.c
> index 1df40a32..44a7bda2 100644
> --- a/tests/ecp.c
> +++ b/tests/ecp.c
> @@ -43,7 +43,7 @@ main (int argc, char *argv[])
>      error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
>  	   argv[1], elf_errmsg (-1));
>  
> -  int outfd = creat (argv[2], 0666);
> +  int outfd = creat (argv[2], DEFFILEMODE);
>    if (outfd == -1)
>      error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);

OK.

> diff --git a/tests/elfstrtab.c b/tests/elfstrtab.c
> index c27d6cfb..145a8aa9 100644
> --- a/tests/elfstrtab.c
> +++ b/tests/elfstrtab.c
> @@ -26,6 +26,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(elf)
>  #include <gelf.h>
> @@ -134,7 +135,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    printf ("\nfname: %s\n", fname);
>    stridx = 0;
>  
> -  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));

OK.

> @@ -280,7 +281,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    close (fd);
>  
>    /* Read the ELF from disk now.  */
> -  fd = open (fname, O_RDWR, 0666);
> +  fd = open (fname, O_RDWR, DEFFILEMODE);

This looks like an existing "bug". No mode necessary for O_RDWR without
O_CREAT. I removed it.

>    if (fd == -1)
>      {
>        printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
> @@ -349,7 +350,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    close (fd);
>  
>    // And read it in one last time.
> -  fd = open (fname, O_RDONLY, 0666);
> +  fd = open (fname, O_RDONLY, DEFFILEMODE);

Likewise.

>    if (fd == -1)
>      {
>        printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
> diff --git a/tests/emptyfile.c b/tests/emptyfile.c
> index 6d086246..a236fba1 100644
> --- a/tests/emptyfile.c
> +++ b/tests/emptyfile.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(elf)
>  #include <gelf.h>
> @@ -67,7 +68,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    printf ("\nfname: %s\n", fname);
>    stridx = 0; // Reset strtab strings index
>  
> -  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));

OK.

> @@ -125,7 +126,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    close (fd);
>  
>    /* Reread the ELF from disk now.  */
> -  fd = open (fname, O_RDWR, 0666);
> +  fd = open (fname, O_RDWR, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
> @@ -208,7 +209,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    close (fd);
>  
>    // And read it in one last time.
> -  fd = open (fname, O_RDONLY, 0666);
> +  fd = open (fname, O_RDONLY, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));

Both don't need a mode as above.

> diff --git a/tests/fillfile.c b/tests/fillfile.c
> index 915e249d..186dcad0 100644
> --- a/tests/fillfile.c
> +++ b/tests/fillfile.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(elf)
>  #include <gelf.h>
> @@ -201,7 +202,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    printf ("\nfname: %s\n", fname);
>    stridx = 0; // Reset strtab strings index
>  
> -  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));

OK.

> @@ -266,7 +267,7 @@ check_elf (const char *fname, int class, int use_mmap)
>  
>    /* Reread the ELF from disk now.  */
>    printf ("Rereading %s\n", fname);
> -  fd = open (fname, O_RDWR, 0666);
> +  fd = open (fname, O_RDWR, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
> @@ -347,7 +348,7 @@ check_elf (const char *fname, int class, int use_mmap)
>  
>    // And read it in one last time.
>    printf ("Rereading %s again\n", fname);
> -  fd = open (fname, O_RDONLY, 0666);
> +  fd = open (fname, O_RDONLY, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));

Again as above, removed the mode.

> diff --git a/tests/newdata.c b/tests/newdata.c
> index 9af99564..0d662f68 100644
> --- a/tests/newdata.c
> +++ b/tests/newdata.c
> @@ -26,6 +26,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(elf)
>  #include <gelf.h>
> @@ -243,7 +244,7 @@ check_elf (int class, int use_mmap)
>  
>    printf ("\ncheck_elf: %s\n", fname);
>  
> -  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 00666);
> +  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 0DEFFILEMODE);

The extra 0 prefix seems wrong. Removed.

>    if (fd == -1)
>      {
>        printf ("cannot create `%s': %s\n", fname, strerror (errno));
> diff --git a/tests/update1.c b/tests/update1.c
> index f4c14753..b7be4e5f 100644
> --- a/tests/update1.c
> +++ b/tests/update1.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  
>  int
> @@ -38,7 +39,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
>    Elf32_Ehdr *ehdr;
>    int i;
>  
> -  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));
> diff --git a/tests/update2.c b/tests/update2.c
> index 5805163d..71455633 100644
> --- a/tests/update2.c
> +++ b/tests/update2.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  
>  int
> @@ -39,7 +40,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
>    Elf32_Phdr *phdr;
>    int i;
>  
> -  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));
> diff --git a/tests/update3.c b/tests/update3.c
> index 7a4224dd..62f67f74 100644
> --- a/tests/update3.c
> +++ b/tests/update3.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(dwelf)
>  
> @@ -46,7 +47,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
>    Dwelf_Strent *shstrtabse;
>    int i;
>  
> -  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));
> diff --git a/tests/update4.c b/tests/update4.c
> index a9bd4bf9..a703b592 100644
> --- a/tests/update4.c
> +++ b/tests/update4.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(dwelf)
>  
> @@ -50,7 +51,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
>    Dwelf_Strent *shstrtabse;
>    int i;
>  
> -  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));

All these look fine.

> diff --git a/tests/vendorelf.c b/tests/vendorelf.c
> index bc13cce3..e1341c76 100644
> --- a/tests/vendorelf.c
> +++ b/tests/vendorelf.c
> @@ -27,6 +27,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#include "system.h"
>  
>  #include ELFUTILS_HEADER(elf)
>  #include <gelf.h>
> @@ -36,7 +37,7 @@ check_elf (const char *fname, int class, int use_mmap)
>  {
>    printf ("\nfname: %s\n", fname);
>  
> -  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
> +  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s': %s\n", fname, strerror (errno));

OK.

> @@ -124,7 +125,7 @@ check_elf (const char *fname, int class, int use_mmap)
>    close (fd);
>  
>    /* Reread the ELF from disk now.  */
> -  fd = open (fname, O_RDONLY, 0666);
> +  fd = open (fname, O_RDONLY, DEFFILEMODE);
>    if (fd == -1)
>      {
>        printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));

Mode removed.

I pushed the attached variant of your patch.

Thanks,

Mark

[-- Attachment #2: 0001-Switch-from-numerical-to-defined-constants-for-permi.patch --]
[-- Type: text/x-patch, Size: 15272 bytes --]

From ced66615bd8c3671c50252cba254fb939b04e362 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=89rico=20Rolim?= <erico.erc@gmail.com>
Date: Sun, 1 Nov 2020 22:49:46 -0300
Subject: [PATCH] Switch from numerical to defined constants for permissions.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use defined constants for permission values. Also add fallback
definitions for them in system.h, to allow for compatibility with
systems that don't provide these macros.

Include system.h in all tests/ files that required it.

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 debuginfod/ChangeLog           |  5 +++++
 debuginfod/debuginfod-client.c |  6 +++---
 lib/ChangeLog                  |  6 ++++++
 lib/system.h                   | 12 ++++++++++++
 src/ChangeLog                  |  6 ++++++
 src/unstrip.c                  |  5 +++--
 tests/ChangeLog                | 15 +++++++++++++++
 tests/alldts.c                 |  3 ++-
 tests/arextract.c              |  2 +-
 tests/ecp.c                    |  2 +-
 tests/elfstrtab.c              |  7 ++++---
 tests/emptyfile.c              |  7 ++++---
 tests/fillfile.c               |  7 ++++---
 tests/newdata.c                |  3 ++-
 tests/update1.c                |  3 ++-
 tests/update2.c                |  3 ++-
 tests/update3.c                |  3 ++-
 tests/update4.c                |  3 ++-
 tests/vendorelf.c              |  5 +++--
 19 files changed, 79 insertions(+), 24 deletions(-)

diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index 5b743d76..a02643e1 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,8 @@
+2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
+
+	* debuginfod-client.c (debuginfod_init_cache): Use ACCESSPERMS for
+	mkdir, DEFFILEMODE for open with O_CREAT.
+
 2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
 
 	* debuginfod.cxx: include libintl.h.
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index 0e5177bc..ce1d819b 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -212,13 +212,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return 0;
 
   /* Create the cache and config files as necessary.  */
-  if (stat(cache_path, &st) != 0 && mkdir(cache_path, 0777) < 0)
+  if (stat(cache_path, &st) != 0 && mkdir(cache_path, ACCESSPERMS) < 0)
     return -errno;
 
   int fd = -1;
 
   /* init cleaning interval config file.  */
-  fd = open(interval_path, O_CREAT | O_RDWR, 0666);
+  fd = open(interval_path, O_CREAT | O_RDWR, DEFFILEMODE);
   if (fd < 0)
     return -errno;
 
@@ -227,7 +227,7 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
 
   /* init max age config file.  */
   if (stat(maxage_path, &st) != 0
-      && (fd = open(maxage_path, O_CREAT | O_RDWR, 0666)) < 0)
+      && (fd = open(maxage_path, O_CREAT | O_RDWR, DEFFILEMODE)) < 0)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
diff --git a/lib/ChangeLog b/lib/ChangeLog
index f4ab6d48..663a7aa5 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,9 @@
+2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
+
+	* system.h (ACCESSPERMS): Define macro if it doesn't exist.
+	(ALLPERMS): Likewise.
+	(DEFFILEMODE): Likewise.
+
 2020-06-11  Mark Wielaaard  <mark@klomp.org>
 
 	* printversion.c (print_version): Update copyright year.
diff --git a/lib/system.h b/lib/system.h
index 292082bd..7b650f11 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -85,6 +85,18 @@
      __res; })
 #endif
 
+#ifndef ACCESSPERMS
+#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
+#endif
+
+#ifndef ALLPERMS
+#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
+#endif
+
+#ifndef DEFFILEMODE
+#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
+#endif
+
 static inline ssize_t __attribute__ ((unused))
 pwrite_retry (int fd, const void *buf, size_t len, off_t off)
 {
diff --git a/src/ChangeLog b/src/ChangeLog
index ab7ae3f6..668b18b0 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
+
+	* unstrip.c (make_directories): Use ACCESSPERMS for mkdir.
+	(handle_file): Use DEFFILEMODE for open with O_CREAT for ET_REL
+	files, ACCESSPERMS otherwise.
+
 2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
 
 	* Makefile.am (nm_LDADD): Add obstack_LIBS.
diff --git a/src/unstrip.c b/src/unstrip.c
index 0257d9cc..231b941d 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -315,7 +315,7 @@ make_directories (const char *path)
   if (dir == NULL)
     error(EXIT_FAILURE, errno, _("memory exhausted"));
 
-  while (mkdir (dir, 0777) < 0 && errno != EEXIST)
+  while (mkdir (dir, ACCESSPERMS) < 0 && errno != EEXIST)
     {
       if (errno == ENOENT)
         make_directories (dir);
@@ -2192,7 +2192,8 @@ DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
 
       /* Copy the unstripped file and then modify it.  */
       int outfd = open (output_file, O_RDWR | O_CREAT,
-			  stripped_ehdr->e_type == ET_REL ? 0666 : 0777);
+			(stripped_ehdr->e_type == ET_REL
+			 ? DEFFILEMODE : ACCESSPERMS));
       if (outfd < 0)
 	error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file);
       Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 57fc4c8e..f7b78c83 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,18 @@
+2020-11-01  Érico N. Rolim  <erico.erc@gmail.com>
+	    Mark Wielaard  <mark@klomp.org>
+
+	* alldts.c (main): Use DEFFILEMODE for open with O_CREAT.
+	* arextract.c (main): Likewise.
+	* ecp.c (main): Likewise for creat.
+	* elfstrtab.c (check_elf): Use DEFFILEMODE for open with O_CREAT,
+	remove mode from open calls without O_CREAT.
+	* emptyfile.c (check_elf): Likewise.
+	* fillfile.c (check_elf): Likewise.
+	* vendorelf.c (check_elf): Likewise.
+	* newdata.c (checkelf): Use DEFFILEMODE for open with O_CREAT.
+	* update{1,2,3,4}.c (main): Likewise.
+	*
+
 2020-10-31  Mark Wielaard  <mark@klomp.org>
 
 	* dwfl-proc-attach.c (dlopen): New external function override.
diff --git a/tests/alldts.c b/tests/alldts.c
index 28b3063c..3e9f9fe6 100644
--- a/tests/alldts.c
+++ b/tests/alldts.c
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -68,7 +69,7 @@ main (void)
   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
 
   /* Open the file.  */
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %m\n", fname);
diff --git a/tests/arextract.c b/tests/arextract.c
index 2c4dc758..936d7f55 100644
--- a/tests/arextract.c
+++ b/tests/arextract.c
@@ -95,7 +95,7 @@ Failed to get base address for the archive element: %s\n",
 	    }
 
 	  /* Open the output file.  */
-	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
+	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, DEFFILEMODE);
 	  if (outfd == -1)
 	    {
 	      printf ("cannot open output file: %m");
diff --git a/tests/ecp.c b/tests/ecp.c
index 1df40a32..44a7bda2 100644
--- a/tests/ecp.c
+++ b/tests/ecp.c
@@ -43,7 +43,7 @@ main (int argc, char *argv[])
     error (EXIT_FAILURE, 0, "problems opening '%s' as ELF file: %s",
 	   argv[1], elf_errmsg (-1));
 
-  int outfd = creat (argv[2], 0666);
+  int outfd = creat (argv[2], DEFFILEMODE);
   if (outfd == -1)
     error (EXIT_FAILURE, errno, "cannot open output file '%s'", argv[2]);
 
diff --git a/tests/elfstrtab.c b/tests/elfstrtab.c
index c27d6cfb..5fae37e0 100644
--- a/tests/elfstrtab.c
+++ b/tests/elfstrtab.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -134,7 +135,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0;
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -280,7 +281,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Read the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
@@ -349,7 +350,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/emptyfile.c b/tests/emptyfile.c
index 6d086246..9124c397 100644
--- a/tests/emptyfile.c
+++ b/tests/emptyfile.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -67,7 +68,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -125,7 +126,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -208,7 +209,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   // And read it in one last time.
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/fillfile.c b/tests/fillfile.c
index 915e249d..7f622536 100644
--- a/tests/fillfile.c
+++ b/tests/fillfile.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -201,7 +202,7 @@ check_elf (const char *fname, int class, int use_mmap)
   printf ("\nfname: %s\n", fname);
   stridx = 0; // Reset strtab strings index
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -266,7 +267,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   /* Reread the ELF from disk now.  */
   printf ("Rereading %s\n", fname);
-  fd = open (fname, O_RDWR, 0666);
+  fd = open (fname, O_RDWR);
   if (fd == -1)
     {
       printf ("cannot (re)open `%s': %s\n", fname, strerror (errno));
@@ -347,7 +348,7 @@ check_elf (const char *fname, int class, int use_mmap)
 
   // And read it in one last time.
   printf ("Rereading %s again\n", fname);
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
diff --git a/tests/newdata.c b/tests/newdata.c
index 9af99564..fcf26acf 100644
--- a/tests/newdata.c
+++ b/tests/newdata.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -243,7 +244,7 @@ check_elf (int class, int use_mmap)
 
   printf ("\ncheck_elf: %s\n", fname);
 
-  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, 00666);
+  int fd = open (fname, O_RDWR|O_CREAT|O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot create `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update1.c b/tests/update1.c
index f4c14753..b7be4e5f 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -38,7 +39,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Ehdr *ehdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update2.c b/tests/update2.c
index 5805163d..71455633 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 
 int
@@ -39,7 +40,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Elf32_Phdr *phdr;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update3.c b/tests/update3.c
index 7a4224dd..62f67f74 100644
--- a/tests/update3.c
+++ b/tests/update3.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(dwelf)
 
@@ -46,7 +47,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/update4.c b/tests/update4.c
index a9bd4bf9..a703b592 100644
--- a/tests/update4.c
+++ b/tests/update4.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(dwelf)
 
@@ -50,7 +51,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   Dwelf_Strent *shstrtabse;
   int i;
 
-  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
diff --git a/tests/vendorelf.c b/tests/vendorelf.c
index bc13cce3..8ce1e5ec 100644
--- a/tests/vendorelf.c
+++ b/tests/vendorelf.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "system.h"
 
 #include ELFUTILS_HEADER(elf)
 #include <gelf.h>
@@ -36,7 +37,7 @@ check_elf (const char *fname, int class, int use_mmap)
 {
   printf ("\nfname: %s\n", fname);
 
-  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
+  int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
   if (fd == -1)
     {
       printf ("cannot open `%s': %s\n", fname, strerror (errno));
@@ -124,7 +125,7 @@ check_elf (const char *fname, int class, int use_mmap)
   close (fd);
 
   /* Reread the ELF from disk now.  */
-  fd = open (fname, O_RDONLY, 0666);
+  fd = open (fname, O_RDONLY);
   if (fd == -1)
     {
       printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno));
-- 
2.18.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-03 15:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-02  1:49 [PATCH] Switch from numerical to defined constants for permissions Érico Nogueira
2020-11-03 15:11 ` Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).