Fix build issue due to Introduce nat/linux-namespaces.[ch] patch

Message ID 1436474110.5367.27.camel@otta
State New, archived
Headers

Commit Message

Peter Bergner July 9, 2015, 8:35 p.m. UTC
  I have a toolchain that dies building gdb with the following error:

/home/bergner/binutils/binutils-gdb/gdb/nat/linux-namespaces.c:39:1: error: static declaration of ‘setns’ follows non-static declaration
 setns (int fd, int nstype)
 ^
In file included from /opt/at7.0/include/sched.h:41:0,
                 from /home/bergner/binutils/binutils-gdb/gdb/nat/linux-namespaces.c:30:
/opt/at7.0/include/bits/sched.h:91:12: note: previous declaration of ‘setns’ was here
 extern int setns (int __fd, int __nstype) __THROW;
            ^
make[2]: *** [linux-namespaces.o] Error 1

My configure doesn't define HAVE_SETNS because my glibc defines __stub_setns,
so nat/linux-namespaces.c defines its own setns routine.  However, my glibc
also has its sched.h with a extern prototype for setns, which conflicts with
this static version linux-namespaces.c is creating, so we get the error above.
How about the following patch to work around the error?  I can confirm that
it fixes the problem on my multiple systems that do and do not have setns.

Ok for trunk?

Peter

	* nat/linux-namespaces.c (setns): Rename from this ...
	(do_setns): ... to this.  Support calling setns if it exists.
	(mnsh_handle_setns): Call do_setns.
  

Comments

Gary Benson July 14, 2015, 2:46 p.m. UTC | #1
Hi Peter,

Peter Bergner wrote:
> I have a toolchain that dies building gdb with the following error:
> 
> /home/bergner/binutils/binutils-gdb/gdb/nat/linux-namespaces.c:39:1: error: static declaration of ‘setns’ follows non-static declaration
>  setns (int fd, int nstype)
>  ^
> In file included from /opt/at7.0/include/sched.h:41:0,
>                  from /home/bergner/binutils/binutils-gdb/gdb/nat/linux-namespaces.c:30:
> /opt/at7.0/include/bits/sched.h:91:12: note: previous declaration of ‘setns’ was here
>  extern int setns (int __fd, int __nstype) __THROW;
>             ^
> make[2]: *** [linux-namespaces.o] Error 1
> 
> My configure doesn't define HAVE_SETNS because my glibc defines
> __stub_setns, so nat/linux-namespaces.c defines its own setns
> routine.  However, my glibc also has its sched.h with a extern
> prototype for setns, which conflicts with this static version
> linux-namespaces.c is creating, so we get the error above.  How
> about the following patch to work around the error?  I can confirm
> that it fixes the problem on my multiple systems that do and do not
> have setns.
> 
> Ok for trunk?

I'd prefer it without the nested conditionals:

  #ifdef HAVE_SETNS
    return setns (fd, nstype);
  #elif defined __NR_setns
    return syscall (__NR_setns, fd, nstype);
  #else
    errno = ENOSYS;
    return -1;
  #endif

Otherwise ok.  Thanks for doing this work!

Cheers,
Gary
  
Peter Bergner July 14, 2015, 3:49 p.m. UTC | #2
On Tue, 2015-07-14 at 15:46 +0100, Gary Benson wrote:
> I'd prefer it without the nested conditionals:
> 
>   #ifdef HAVE_SETNS
>     return setns (fd, nstype);
>   #elif defined __NR_setns
>     return syscall (__NR_setns, fd, nstype);
>   #else
>     errno = ENOSYS;
>     return -1;
>   #endif
> 
> Otherwise ok.  Thanks for doing this work!

Hmmm, for some reason I thought #elif wasn't supported, since I
agree this looks better.  Maybe I was thinking of some scripting
language limitation?  Anyway, I committed the patch with your
suggested change.  Thanks!

Peter
  
Gary Benson July 15, 2015, 12:32 p.m. UTC | #3
Peter Bergner wrote:
> On Tue, 2015-07-14 at 15:46 +0100, Gary Benson wrote:
> > I'd prefer it without the nested conditionals:
> > 
> >   #ifdef HAVE_SETNS
> >     return setns (fd, nstype);
> >   #elif defined __NR_setns
> >     return syscall (__NR_setns, fd, nstype);
> >   #else
> >     errno = ENOSYS;
> >     return -1;
> >   #endif
> > 
> > Otherwise ok.  Thanks for doing this work!
> 
> Hmmm, for some reason I thought #elif wasn't supported, since I
> agree this looks better.  Maybe I was thinking of some scripting
> language limitation?  Anyway, I committed the patch with your
> suggested change.  Thanks!

Thank you :)

Cheers,
Gary
  

Patch

diff --git a/gdb/nat/linux-namespaces.c b/gdb/nat/linux-namespaces.c
index a7a3e4d..c2f0d2e 100644
--- a/gdb/nat/linux-namespaces.c
+++ b/gdb/nat/linux-namespaces.c
@@ -34,18 +34,20 @@  int debug_linux_namespaces;

 /* Handle systems without setns.  */

-#ifndef HAVE_SETNS
-static int
-setns (int fd, int nstype)
+static inline int
+do_setns (int fd, int nstype)
 {
-#ifdef __NR_setns
-  return syscall (__NR_setns, fd, nstype);
+#ifdef HAVE_SETNS
+  return setns (fd, nstype);
 #else
+# ifdef __NR_setns
+  return syscall (__NR_setns, fd, nstype);
+# else
   errno = ENOSYS;
   return -1;
+# endif
 #endif
 }
-#endif

 /* Handle systems without MSG_CMSG_CLOEXEC.  */

@@ -495,7 +497,7 @@  mnsh_recv_message (int sock, enum mnsh_msg_type *type,
 static ssize_t
 mnsh_handle_setns (int sock, int fd, int nstype)
 {
-  int result = setns (fd, nstype);
+  int result = do_setns (fd, nstype);

   return mnsh_return_int (sock, result, errno);
 }