[PATCH] Switch from numerical to defined constants for permissions.

Érico Nogueira ericonr@disroot.org
Mon Nov 2 01:49:46 GMT 2020


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



More information about the Elfutils-devel mailing list