mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH v1] add renameat2 linux syscall wrapper
@ 2024-04-21 15:36 Tony Ambardar
  2024-04-22 22:47 ` Rich Felker
  2024-04-23 23:43 ` [musl] [PATCH v2] " Tony Ambardar
  0 siblings, 2 replies; 11+ messages in thread
From: Tony Ambardar @ 2024-04-21 15:36 UTC (permalink / raw)
  To: musl; +Cc: Tony Ambardar

This syscall is available since Linux 3.15 and also implemented in glibc
from version 2.28. It is commonly used in filesystem or security contexts.

Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
_GNU_SOURCE as with glibc.

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
---
 include/stdio.h       | 7 +++++++
 src/linux/renameat2.c | 8 ++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 src/linux/renameat2.c

diff --git a/include/stdio.h b/include/stdio.h
index cb858618..8312c3bf 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -158,6 +158,13 @@ char *ctermid(char *);
 #define L_ctermid 20
 #endif
 
+#if defined(_GNU_SOURCE)
+#define RENAME_NOREPLACE (1 << 0)
+#define RENAME_EXCHANGE (1 << 1)
+#define RENAME_WHITEOUT (1 << 2)
+
+int renameat2(int, const char *, int, const char *, unsigned int);
+#endif
 
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
  || defined(_BSD_SOURCE)
diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
new file mode 100644
index 00000000..3062aa15
--- /dev/null
+++ b/src/linux/renameat2.c
@@ -0,0 +1,8 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include "syscall.h"
+
+int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
+{
+	return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
+}
-- 
2.34.1


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

* Re: [musl] [PATCH v1] add renameat2 linux syscall wrapper
  2024-04-21 15:36 [musl] [PATCH v1] add renameat2 linux syscall wrapper Tony Ambardar
@ 2024-04-22 22:47 ` Rich Felker
  2024-04-23  4:39   ` Tony Ambardar
  2024-04-23 14:49   ` enh
  2024-04-23 23:43 ` [musl] [PATCH v2] " Tony Ambardar
  1 sibling, 2 replies; 11+ messages in thread
From: Rich Felker @ 2024-04-22 22:47 UTC (permalink / raw)
  To: Tony Ambardar; +Cc: musl

On Sun, Apr 21, 2024 at 08:36:40AM -0700, Tony Ambardar wrote:
> This syscall is available since Linux 3.15 and also implemented in glibc
> from version 2.28. It is commonly used in filesystem or security contexts.
> 
> Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> _GNU_SOURCE as with glibc.
> 
> Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> ---
>  include/stdio.h       | 7 +++++++
>  src/linux/renameat2.c | 8 ++++++++
>  2 files changed, 15 insertions(+)
>  create mode 100644 src/linux/renameat2.c
> 
> diff --git a/include/stdio.h b/include/stdio.h
> index cb858618..8312c3bf 100644
> --- a/include/stdio.h
> +++ b/include/stdio.h
> @@ -158,6 +158,13 @@ char *ctermid(char *);
>  #define L_ctermid 20
>  #endif
>  
> +#if defined(_GNU_SOURCE)
> +#define RENAME_NOREPLACE (1 << 0)
> +#define RENAME_EXCHANGE (1 << 1)
> +#define RENAME_WHITEOUT (1 << 2)
> +
> +int renameat2(int, const char *, int, const char *, unsigned int);
> +#endif

s/unsigned int/unsigned/ and maybe just write out the constants? I
think that's the style musl uses most places.

>  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
>   || defined(_BSD_SOURCE)
> diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> new file mode 100644
> index 00000000..3062aa15
> --- /dev/null
> +++ b/src/linux/renameat2.c
> @@ -0,0 +1,8 @@
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include "syscall.h"
> +
> +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
> +{
> +	return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> +}
> -- 
> 2.34.1

This probably at least needs to support flags==0 on kernels without
SYS_renameat2 by calling renameat in that case. Then I'm not sure if
ENOSYS should be kept if the new syscall is missing, or just EINVAL or
whatever is used to report unsupported flags.

Rich

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

* Re: [musl] [PATCH v1] add renameat2 linux syscall wrapper
  2024-04-22 22:47 ` Rich Felker
@ 2024-04-23  4:39   ` Tony Ambardar
  2024-04-23 15:51     ` Rich Felker
  2024-04-23 14:49   ` enh
  1 sibling, 1 reply; 11+ messages in thread
From: Tony Ambardar @ 2024-04-23  4:39 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

On Mon, Apr 22, 2024 at 06:47:26PM -0400, Rich Felker wrote:
> On Sun, Apr 21, 2024 at 08:36:40AM -0700, Tony Ambardar wrote:
> > This syscall is available since Linux 3.15 and also implemented in glibc
> > from version 2.28. It is commonly used in filesystem or security contexts.
> > 
> > Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> > _GNU_SOURCE as with glibc.
> > 
> > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> > ---
> >  include/stdio.h       | 7 +++++++
> >  src/linux/renameat2.c | 8 ++++++++
> >  2 files changed, 15 insertions(+)
> >  create mode 100644 src/linux/renameat2.c
> > 
> > diff --git a/include/stdio.h b/include/stdio.h
> > index cb858618..8312c3bf 100644
> > --- a/include/stdio.h
> > +++ b/include/stdio.h
> > @@ -158,6 +158,13 @@ char *ctermid(char *);
> >  #define L_ctermid 20
> >  #endif
> >  
> > +#if defined(_GNU_SOURCE)
> > +#define RENAME_NOREPLACE (1 << 0)
> > +#define RENAME_EXCHANGE (1 << 1)
> > +#define RENAME_WHITEOUT (1 << 2)
> > +
> > +int renameat2(int, const char *, int, const char *, unsigned int);
> > +#endif
> 
> s/unsigned int/unsigned/ and maybe just write out the constants? I
> think that's the style musl uses most places.

Right, the constants-by-shift mirrored glibc and I saw these also in musl
e.g. hwcap.h, elf.h, mount.h, syslog.h, etc. I'm not wedded to either but
just wanted to be consistent. Please confirm a preference and I'll update.

> 
> >  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> >   || defined(_BSD_SOURCE)
> > diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> > new file mode 100644
> > index 00000000..3062aa15
> > --- /dev/null
> > +++ b/src/linux/renameat2.c
> > @@ -0,0 +1,8 @@
> > +#define _GNU_SOURCE
> > +#include <stdio.h>
> > +#include "syscall.h"
> > +
> > +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
> > +{
> > +	return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> > +}
> > -- 
> > 2.34.1
> 
> This probably at least needs to support flags==0 on kernels without
> SYS_renameat2 by calling renameat in that case. Then I'm not sure if
> ENOSYS should be kept if the new syscall is missing, or just EINVAL or
> whatever is used to report unsupported flags.

I had wondered about this too and looked at all 'arch/.../syscall.h.in'
files. All arches support SYS_renameat2, but riscv, aarch64, loongarch64
and or1k are missing SYS_rename and/or SYS_renameat. Also, current code in
stdio/rename.c and unistd/renameat.c may fall back _unconditionally_ to
use SYS_renameat2 in these cases, with the assumption it's always present.

I think this means we're OK? Or do we allow for old kernels which lack
SYS_renameat2, in which case some existing code would need updating?

> Rich

Thanks,
Tony

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

* Re: [musl] [PATCH v1] add renameat2 linux syscall wrapper
  2024-04-22 22:47 ` Rich Felker
  2024-04-23  4:39   ` Tony Ambardar
@ 2024-04-23 14:49   ` enh
  2024-04-23 15:48     ` Rich Felker
  1 sibling, 1 reply; 11+ messages in thread
From: enh @ 2024-04-23 14:49 UTC (permalink / raw)
  To: musl; +Cc: Tony Ambardar

fwiw, i wrote out the constants with shifts in bionic because that's
what the uapi headers do (<linux/fs.h> iirc), and the preprocessor
won't get upset if someone includes both this way.

On Mon, Apr 22, 2024 at 3:47 PM Rich Felker <dalias@libc.org> wrote:
>
> On Sun, Apr 21, 2024 at 08:36:40AM -0700, Tony Ambardar wrote:
> > This syscall is available since Linux 3.15 and also implemented in glibc
> > from version 2.28. It is commonly used in filesystem or security contexts.
> >
> > Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> > _GNU_SOURCE as with glibc.
> >
> > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> > ---
> >  include/stdio.h       | 7 +++++++
> >  src/linux/renameat2.c | 8 ++++++++
> >  2 files changed, 15 insertions(+)
> >  create mode 100644 src/linux/renameat2.c
> >
> > diff --git a/include/stdio.h b/include/stdio.h
> > index cb858618..8312c3bf 100644
> > --- a/include/stdio.h
> > +++ b/include/stdio.h
> > @@ -158,6 +158,13 @@ char *ctermid(char *);
> >  #define L_ctermid 20
> >  #endif
> >
> > +#if defined(_GNU_SOURCE)
> > +#define RENAME_NOREPLACE (1 << 0)
> > +#define RENAME_EXCHANGE (1 << 1)
> > +#define RENAME_WHITEOUT (1 << 2)
> > +
> > +int renameat2(int, const char *, int, const char *, unsigned int);
> > +#endif
>
> s/unsigned int/unsigned/ and maybe just write out the constants? I
> think that's the style musl uses most places.
>
> >  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> >   || defined(_BSD_SOURCE)
> > diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> > new file mode 100644
> > index 00000000..3062aa15
> > --- /dev/null
> > +++ b/src/linux/renameat2.c
> > @@ -0,0 +1,8 @@
> > +#define _GNU_SOURCE
> > +#include <stdio.h>
> > +#include "syscall.h"
> > +
> > +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
> > +{
> > +     return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> > +}
> > --
> > 2.34.1
>
> This probably at least needs to support flags==0 on kernels without
> SYS_renameat2 by calling renameat in that case. Then I'm not sure if
> ENOSYS should be kept if the new syscall is missing, or just EINVAL or
> whatever is used to report unsupported flags.
>
> Rich

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

* Re: [musl] [PATCH v1] add renameat2 linux syscall wrapper
  2024-04-23 14:49   ` enh
@ 2024-04-23 15:48     ` Rich Felker
  0 siblings, 0 replies; 11+ messages in thread
From: Rich Felker @ 2024-04-23 15:48 UTC (permalink / raw)
  To: enh; +Cc: musl, Tony Ambardar

On Tue, Apr 23, 2024 at 07:49:47AM -0700, enh wrote:
> fwiw, i wrote out the constants with shifts in bionic because that's
> what the uapi headers do (<linux/fs.h> iirc), and the preprocessor
> won't get upset if someone includes both this way.

In many cases, there's potential breakage if both are included, so I
don't think this is necessarily a good reason. OTOH I don't think
we're really consistent about this in musl, so I don't have a strong
preference either way.


> On Mon, Apr 22, 2024 at 3:47 PM Rich Felker <dalias@libc.org> wrote:
> >
> > On Sun, Apr 21, 2024 at 08:36:40AM -0700, Tony Ambardar wrote:
> > > This syscall is available since Linux 3.15 and also implemented in glibc
> > > from version 2.28. It is commonly used in filesystem or security contexts.
> > >
> > > Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> > > _GNU_SOURCE as with glibc.
> > >
> > > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> > > ---
> > >  include/stdio.h       | 7 +++++++
> > >  src/linux/renameat2.c | 8 ++++++++
> > >  2 files changed, 15 insertions(+)
> > >  create mode 100644 src/linux/renameat2.c
> > >
> > > diff --git a/include/stdio.h b/include/stdio.h
> > > index cb858618..8312c3bf 100644
> > > --- a/include/stdio.h
> > > +++ b/include/stdio.h
> > > @@ -158,6 +158,13 @@ char *ctermid(char *);
> > >  #define L_ctermid 20
> > >  #endif
> > >
> > > +#if defined(_GNU_SOURCE)
> > > +#define RENAME_NOREPLACE (1 << 0)
> > > +#define RENAME_EXCHANGE (1 << 1)
> > > +#define RENAME_WHITEOUT (1 << 2)
> > > +
> > > +int renameat2(int, const char *, int, const char *, unsigned int);
> > > +#endif
> >
> > s/unsigned int/unsigned/ and maybe just write out the constants? I
> > think that's the style musl uses most places.
> >
> > >  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> > >   || defined(_BSD_SOURCE)
> > > diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> > > new file mode 100644
> > > index 00000000..3062aa15
> > > --- /dev/null
> > > +++ b/src/linux/renameat2.c
> > > @@ -0,0 +1,8 @@
> > > +#define _GNU_SOURCE
> > > +#include <stdio.h>
> > > +#include "syscall.h"
> > > +
> > > +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
> > > +{
> > > +     return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> > > +}
> > > --
> > > 2.34.1
> >
> > This probably at least needs to support flags==0 on kernels without
> > SYS_renameat2 by calling renameat in that case. Then I'm not sure if
> > ENOSYS should be kept if the new syscall is missing, or just EINVAL or
> > whatever is used to report unsupported flags.
> >
> > Rich

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

* Re: [musl] [PATCH v1] add renameat2 linux syscall wrapper
  2024-04-23  4:39   ` Tony Ambardar
@ 2024-04-23 15:51     ` Rich Felker
  0 siblings, 0 replies; 11+ messages in thread
From: Rich Felker @ 2024-04-23 15:51 UTC (permalink / raw)
  To: Tony Ambardar; +Cc: musl

On Mon, Apr 22, 2024 at 09:39:17PM -0700, Tony Ambardar wrote:
> On Mon, Apr 22, 2024 at 06:47:26PM -0400, Rich Felker wrote:
> > On Sun, Apr 21, 2024 at 08:36:40AM -0700, Tony Ambardar wrote:
> > > This syscall is available since Linux 3.15 and also implemented in glibc
> > > from version 2.28. It is commonly used in filesystem or security contexts.
> > > 
> > > Defines RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> > > _GNU_SOURCE as with glibc.
> > > 
> > > Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> > > ---
> > >  include/stdio.h       | 7 +++++++
> > >  src/linux/renameat2.c | 8 ++++++++
> > >  2 files changed, 15 insertions(+)
> > >  create mode 100644 src/linux/renameat2.c
> > > 
> > > diff --git a/include/stdio.h b/include/stdio.h
> > > index cb858618..8312c3bf 100644
> > > --- a/include/stdio.h
> > > +++ b/include/stdio.h
> > > @@ -158,6 +158,13 @@ char *ctermid(char *);
> > >  #define L_ctermid 20
> > >  #endif
> > >  
> > > +#if defined(_GNU_SOURCE)
> > > +#define RENAME_NOREPLACE (1 << 0)
> > > +#define RENAME_EXCHANGE (1 << 1)
> > > +#define RENAME_WHITEOUT (1 << 2)
> > > +
> > > +int renameat2(int, const char *, int, const char *, unsigned int);
> > > +#endif
> > 
> > s/unsigned int/unsigned/ and maybe just write out the constants? I
> > think that's the style musl uses most places.
> 
> Right, the constants-by-shift mirrored glibc and I saw these also in musl
> e.g. hwcap.h, elf.h, mount.h, syslog.h, etc. I'm not wedded to either but
> just wanted to be consistent. Please confirm a preference and I'll update.
> 
> > 
> > >  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> > >   || defined(_BSD_SOURCE)
> > > diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> > > new file mode 100644
> > > index 00000000..3062aa15
> > > --- /dev/null
> > > +++ b/src/linux/renameat2.c
> > > @@ -0,0 +1,8 @@
> > > +#define _GNU_SOURCE
> > > +#include <stdio.h>
> > > +#include "syscall.h"
> > > +
> > > +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned int flags)
> > > +{
> > > +	return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> > > +}
> > > -- 
> > > 2.34.1
> > 
> > This probably at least needs to support flags==0 on kernels without
> > SYS_renameat2 by calling renameat in that case. Then I'm not sure if
> > ENOSYS should be kept if the new syscall is missing, or just EINVAL or
> > whatever is used to report unsupported flags.
> 
> I had wondered about this too and looked at all 'arch/.../syscall.h.in'
> files. All arches support SYS_renameat2, but riscv, aarch64, loongarch64
> and or1k are missing SYS_rename and/or SYS_renameat. Also, current code in
> stdio/rename.c and unistd/renameat.c may fall back _unconditionally_ to
> use SYS_renameat2 in these cases, with the assumption it's always present.
> 
> I think this means we're OK? Or do we allow for old kernels which lack
> SYS_renameat2, in which case some existing code would need updating?

No, this just means it's unconditionally present on some archs
(because they were added after SYS_renameat2 was added), not that it
can be assumed to be present in general.

Rich

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

* [musl] [PATCH v2] add renameat2 linux syscall wrapper
  2024-04-21 15:36 [musl] [PATCH v1] add renameat2 linux syscall wrapper Tony Ambardar
  2024-04-22 22:47 ` Rich Felker
@ 2024-04-23 23:43 ` Tony Ambardar
  2024-05-06 14:50   ` Rich Felker
  2024-05-07  3:28   ` [musl] [PATCH v3] " Tony Ambardar
  1 sibling, 2 replies; 11+ messages in thread
From: Tony Ambardar @ 2024-04-23 23:43 UTC (permalink / raw)
  To: musl; +Cc: Tony Ambardar

This syscall is available since Linux 3.15 and also implemented in glibc
from version 2.28. It is commonly used in filesystem or security contexts.

Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
_GNU_SOURCE as with glibc.

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
---
v1 -> v2:
 * align related constants
 * drop 'int' from 'unsigned int'
 * add fallback to SYS_renameat where applicable
---
 include/stdio.h       |  7 +++++++
 src/linux/renameat2.c | 12 ++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 src/linux/renameat2.c

diff --git a/include/stdio.h b/include/stdio.h
index cb858618..4ea4c170 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -158,6 +158,13 @@ char *ctermid(char *);
 #define L_ctermid 20
 #endif
 
+#if defined(_GNU_SOURCE)
+#define RENAME_NOREPLACE (1 << 0)
+#define RENAME_EXCHANGE  (1 << 1)
+#define RENAME_WHITEOUT  (1 << 2)
+
+int renameat2(int, const char *, int, const char *, unsigned);
+#endif
 
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
  || defined(_BSD_SOURCE)
diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
new file mode 100644
index 00000000..48ee8d53
--- /dev/null
+++ b/src/linux/renameat2.c
@@ -0,0 +1,12 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include "syscall.h"
+
+int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
+{
+	int r = __syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
+#ifdef SYS_renameat
+	if (r==-ENOSYS && !flags) r = __syscall(SYS_renameat, oldfd, old, newfd, new);
+#endif
+	return __syscall_ret(r);
+}
-- 
2.34.1


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

* Re: [musl] [PATCH v2] add renameat2 linux syscall wrapper
  2024-04-23 23:43 ` [musl] [PATCH v2] " Tony Ambardar
@ 2024-05-06 14:50   ` Rich Felker
  2024-05-06 23:42     ` Tony Ambardar
  2024-05-07  3:28   ` [musl] [PATCH v3] " Tony Ambardar
  1 sibling, 1 reply; 11+ messages in thread
From: Rich Felker @ 2024-05-06 14:50 UTC (permalink / raw)
  To: Tony Ambardar; +Cc: musl

On Tue, Apr 23, 2024 at 04:43:55PM -0700, Tony Ambardar wrote:
> This syscall is available since Linux 3.15 and also implemented in glibc
> from version 2.28. It is commonly used in filesystem or security contexts.
> 
> Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> _GNU_SOURCE as with glibc.
> 
> Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
> ---
> v1 -> v2:
>  * align related constants
>  * drop 'int' from 'unsigned int'
>  * add fallback to SYS_renameat where applicable
> ---
>  include/stdio.h       |  7 +++++++
>  src/linux/renameat2.c | 12 ++++++++++++
>  2 files changed, 19 insertions(+)
>  create mode 100644 src/linux/renameat2.c
> 
> diff --git a/include/stdio.h b/include/stdio.h
> index cb858618..4ea4c170 100644
> --- a/include/stdio.h
> +++ b/include/stdio.h
> @@ -158,6 +158,13 @@ char *ctermid(char *);
>  #define L_ctermid 20
>  #endif
>  
> +#if defined(_GNU_SOURCE)
> +#define RENAME_NOREPLACE (1 << 0)
> +#define RENAME_EXCHANGE  (1 << 1)
> +#define RENAME_WHITEOUT  (1 << 2)
> +
> +int renameat2(int, const char *, int, const char *, unsigned);
> +#endif
>  
>  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
>   || defined(_BSD_SOURCE)
> diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> new file mode 100644
> index 00000000..48ee8d53
> --- /dev/null
> +++ b/src/linux/renameat2.c
> @@ -0,0 +1,12 @@
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include "syscall.h"
> +
> +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
> +{
> +	int r = __syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> +#ifdef SYS_renameat
> +	if (r==-ENOSYS && !flags) r = __syscall(SYS_renameat, oldfd, old, newfd, new);
> +#endif
> +	return __syscall_ret(r);
> +}
> -- 
> 2.34.1

If flags is 0, the SYS_renameat syscall is semantically equivalent to
the SYS_renameat2 one, so it would be better to just unconditionally
do that first rather than failing and falling back.

Rich

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

* Re: [musl] [PATCH v2] add renameat2 linux syscall wrapper
  2024-05-06 14:50   ` Rich Felker
@ 2024-05-06 23:42     ` Tony Ambardar
  2024-05-07  0:01       ` Rich Felker
  0 siblings, 1 reply; 11+ messages in thread
From: Tony Ambardar @ 2024-05-06 23:42 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

On Mon, May 06, 2024 at 10:50:57AM -0400, Rich Felker wrote:
> On Tue, Apr 23, 2024 at 04:43:55PM -0700, Tony Ambardar wrote:
> > This syscall is available since Linux 3.15 and also implemented in glibc
> > from version 2.28. It is commonly used in filesystem or security contexts.
> > 

[SNIP]

> 
> If flags is 0, the SYS_renameat syscall is semantically equivalent to
> the SYS_renameat2 one, so it would be better to just unconditionally
> do that first rather than failing and falling back.
> 

Do you mean rearranging and dropping the ENOSYS conditional, e.g. something
like below?

>       int r;
> #ifdef SYS_renameat
>       if (!flags) r = __syscall(SYS_renameat, oldfd, old, newfd, new);
>       else
> #endif
>       r = __syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
>       return __syscall_ret(r);

Please clarify and I'll update.

Thanks,
Tony

> Rich

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

* Re: [musl] [PATCH v2] add renameat2 linux syscall wrapper
  2024-05-06 23:42     ` Tony Ambardar
@ 2024-05-07  0:01       ` Rich Felker
  0 siblings, 0 replies; 11+ messages in thread
From: Rich Felker @ 2024-05-07  0:01 UTC (permalink / raw)
  To: Tony Ambardar; +Cc: musl

On Mon, May 06, 2024 at 04:42:10PM -0700, Tony Ambardar wrote:
> On Mon, May 06, 2024 at 10:50:57AM -0400, Rich Felker wrote:
> > On Tue, Apr 23, 2024 at 04:43:55PM -0700, Tony Ambardar wrote:
> > > This syscall is available since Linux 3.15 and also implemented in glibc
> > > from version 2.28. It is commonly used in filesystem or security contexts.
> > > 
> 
> [SNIP]
> 
> > 
> > If flags is 0, the SYS_renameat syscall is semantically equivalent to
> > the SYS_renameat2 one, so it would be better to just unconditionally
> > do that first rather than failing and falling back.
> > 
> 
> Do you mean rearranging and dropping the ENOSYS conditional, e.g. something
> like below?
> 
> >       int r;
> > #ifdef SYS_renameat
> >       if (!flags) r = __syscall(SYS_renameat, oldfd, old, newfd, new);
> >       else
> > #endif
> >       r = __syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> >       return __syscall_ret(r);
> 
> Please clarify and I'll update.
> 
> Thanks,
> Tony

Yes, or just (simpler, without any "int r;"):

	if (!flags) return syscall(SYS_renameat, ...);

etc. The use of __syscall and manually calling __syscall_ret is only
helpful if you want to peek/poke at error codes prior to setting errno
and returning.

Rich

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

* [musl] [PATCH v3] add renameat2 linux syscall wrapper
  2024-04-23 23:43 ` [musl] [PATCH v2] " Tony Ambardar
  2024-05-06 14:50   ` Rich Felker
@ 2024-05-07  3:28   ` Tony Ambardar
  1 sibling, 0 replies; 11+ messages in thread
From: Tony Ambardar @ 2024-05-07  3:28 UTC (permalink / raw)
  To: musl; +Cc: Tony Ambardar, Rich Felker

This syscall is available since Linux 3.15 and also implemented in glibc
from version 2.28. It is commonly used in filesystem or security contexts.

Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
_GNU_SOURCE as with glibc.

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
---
v2 -> v3:
 * call SYS_renameat first if applicable
 * drop unneeded error code handling

v1 -> v2:
 * align related constants
 * drop 'int' from 'unsigned int'
 * add fallback to SYS_renameat where applicable
---
 include/stdio.h       |  7 +++++++
 src/linux/renameat2.c | 11 +++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 src/linux/renameat2.c

diff --git a/include/stdio.h b/include/stdio.h
index cb858618..4ea4c170 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -158,6 +158,13 @@ char *ctermid(char *);
 #define L_ctermid 20
 #endif
 
+#if defined(_GNU_SOURCE)
+#define RENAME_NOREPLACE (1 << 0)
+#define RENAME_EXCHANGE  (1 << 1)
+#define RENAME_WHITEOUT  (1 << 2)
+
+int renameat2(int, const char *, int, const char *, unsigned);
+#endif
 
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
  || defined(_BSD_SOURCE)
diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
new file mode 100644
index 00000000..b8060388
--- /dev/null
+++ b/src/linux/renameat2.c
@@ -0,0 +1,11 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include "syscall.h"
+
+int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
+{
+#ifdef SYS_renameat
+	if (!flags) return syscall(SYS_renameat, oldfd, old, newfd, new);
+#endif
+	return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
+}
-- 
2.34.1


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

end of thread, other threads:[~2024-05-07  3:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-21 15:36 [musl] [PATCH v1] add renameat2 linux syscall wrapper Tony Ambardar
2024-04-22 22:47 ` Rich Felker
2024-04-23  4:39   ` Tony Ambardar
2024-04-23 15:51     ` Rich Felker
2024-04-23 14:49   ` enh
2024-04-23 15:48     ` Rich Felker
2024-04-23 23:43 ` [musl] [PATCH v2] " Tony Ambardar
2024-05-06 14:50   ` Rich Felker
2024-05-06 23:42     ` Tony Ambardar
2024-05-07  0:01       ` Rich Felker
2024-05-07  3:28   ` [musl] [PATCH v3] " Tony Ambardar

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).