Skip to contentSkip to conversation

vfs: Define new syscall getumask.

PATCH Superseded Richard W.M. Jones 12 participants 28 messages Apr 13, 2016 – Apr 18, 2016

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover vfs: Define new syscall getumask.

v1 -> v2:

- Use current_umask() instead of current->fs->umask.

- Retested it.

----------------------------------------------------------------------

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

This patch series adds a trivial system call "getumask" which returns the umask of the current process.

Another approach to this has been attempted before, adding something to /proc, although it didn't go anywhere. See:

http://comments.gmane.org/gmane.linux.kernel/1292109

Another way to solve this would be to add a thread-safe getumask to glibc. Since glibc could own the mutex, this would permit libraries linked to this glibc to read umask safely.

I should also note that man-pages documents getumask(3), but no version of glibc has ever implemented it.

Typical test script:

#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])
{
int r = syscall(329);

  if (r == -1) {
    perror("getumask");
    exit(1);

}
printf("umask = %o\n", r);
exit(0);
}

$ ./getumask 
umask = 22

Rich.

#1 lore raw

PATCH v2 1/2 vfs: Define new syscall getumask.

Define a system call for reading the current umask value.

---
 include/linux/syscalls.h | 1 +
 kernel/sys.c             | 5 +++++
 2 files changed, 6 insertions(+)
include/linux/syscalls.h +1 -0
index d795472..e96e88f 100644
@@ -659,6 +659,7 @@ asmlinkage long sys_prlimit64(pid_t pid, unsigned int resource,
 				struct rlimit64 __user *old_rlim);
 asmlinkage long sys_getrusage(int who, struct rusage __user *ru);
 asmlinkage long sys_umask(int mask);
+asmlinkage long sys_getumask(void);
 
 asmlinkage long sys_msgget(key_t key, int msgflg);
 asmlinkage long sys_msgsnd(int msqid, struct msgbuf __user *msgp,
kernel/sys.c +5 -0
index cf8ba54..9db526c 100644
@@ -1649,6 +1649,11 @@ SYSCALL_DEFINE1(umask, int, mask)
 	return mask;
 }
 
+SYSCALL_DEFINE0(getumask)
+{
+	return current_umask();
+}
+
 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
 {
 	struct fd exe;
Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 2/2 x86: Wire up new getumask system call on x86.

In reply to Richard W.M. Jones
---
 arch/x86/entry/syscalls/syscall_32.tbl | 1 +
 arch/x86/entry/syscalls/syscall_64.tbl | 1 +
 2 files changed, 2 insertions(+)
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index b30dd81..af0a032 100644
@@ -386,3 +386,4 @@
 377	i386	copy_file_range		sys_copy_file_range
 378	i386	preadv2			sys_preadv2
 379	i386	pwritev2		sys_pwritev2
+380	i386	getumask		sys_getumask
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index cac6d17..47c1579 100644
@@ -335,6 +335,7 @@
 326	common	copy_file_range		sys_copy_file_range
 327	64	preadv2			sys_preadv2
 328	64	pwritev2		sys_pwritev2
+329	common	getumask		sys_getumask
 
 #
 # x32-specific system call numbers start at 512 to avoid cache impact
Signature · 1 line
2.7.4
#3 lore raw
Quoted 6 lines · Cyrill Gorcunov
Quoted 3 lines · Richard W.M. Jones

Define a system call for reading the current umask value.

Btw don't we have to declare it in include/uapi/asm-generic/unistd.h as well?

Yes, I think I do. I was following pwritev2 which wasn't added to this file, but other recent system calls (mlock2, copy_file_range) were added.

TBH the documentation for this file is not very clear...

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#7 lore raw
In reply to Richard W.M. Jones
Quoted 37 lines · Richard W.M. Jones

v1 -> v2:

- Use current_umask() instead of current->fs->umask.

- Retested it.

----------------------------------------------------------------------

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

This patch series adds a trivial system call "getumask" which returns the umask of the current process.

Another approach to this has been attempted before, adding something to /proc, although it didn't go anywhere. See:

http://comments.gmane.org/gmane.linux.kernel/1292109

Another way to solve this would be to add a thread-safe getumask to glibc. Since glibc could own the mutex, this would permit libraries linked to this glibc to read umask safely.

I should also note that man-pages documents getumask(3), but no version of glibc has ever implemented it.

Typical test script:

#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])
{
int r = syscall(329);

Quoted 7 lines
  if (r == -1) {
    perror("getumask");
    exit(1);

}
printf("umask = %o\n", r);
exit(0);
}

Why not add this to the ktest infrastructure, we strongly encourage that for new syscalls, along with a man page patch.

thanks,

greg k-h

#4 lore raw
In reply to Richard W.M. Jones
Quoted 11 lines · Richard W.M. Jones
Quoted 6 lines · Cyrill Gorcunov
Quoted 3 lines · Richard W.M. Jones

Define a system call for reading the current umask value.

Btw don't we have to declare it in include/uapi/asm-generic/unistd.h as well?

Yes, I think I do. I was following pwritev2 which wasn't added to this file, but other recent system calls (mlock2, copy_file_range) were added.

Indeed. I'll send a patch to wire up pread/writev2, sorry for causing your conflicts, but adding syscalls is a bit of a mess.

#5 lore raw
In reply to Richard W.M. Jones
Quoted 6 lines · ----- On Apr 13, 2016, at 9:57 AM, Richard W.M. Jones…
Quoted 5 lines · Cyrill Gorcunov
Quoted 3 lines · Richard W.M. Jones

Define a system call for reading the current umask value.

Quoted 7 lines
Quoted 1 line

Btw don't we have to declare it in include/uapi/asm-generic/unistd.h as well?

Yes, I think I do. I was following pwritev2 which wasn't added to this file, but other recent system calls (mlock2, copy_file_range) were added.

TBH the documentation for this file is not very clear...

asm-generic/unistd.h defines the system call for a few architectures.

grep -r asm-generic/unistd.h arch/*/include/
arch/arc/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/arc/include/uapi/asm/unistd.h:/* Generic syscall (fs/filesystems.c - lost in asm-generic/unistd.h */ arch/arm64/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/c6x/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/h8300/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/hexagon/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/metag/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/nios2/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/openrisc/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/score/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/tile/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h> arch/unicore32/include/uapi/asm/unistd.h:#include <asm-generic/unistd.h>

Wiring up the system call in this header means adding support for this system call on all those architectures.

Thanks,

Mathieu

Quoted 9 lines

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#10 lore raw
In reply to Richard W.M. Jones
Quoted 6 lines · ----- On Apr 13, 2016, at 8:57 AM, Richard W.M. Jones…

v1 -> v2:

- Use current_umask() instead of current->fs->umask.

- Retested it.

Quoted 8 lines

----------------------------------------------------------------------

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

This patch series adds a trivial system call "getumask" which returns the umask of the current process.

In addition to this system call, we could extend a variation of my thread_local_abi system call (https://lkml.org/lkml/2016/4/4/455)
(could be without features flags, or an entirely new system call specifically for a umask cache) to register a "current umask" cache located in a TLS area.

Basically, reading the current umask value would be a simple load from a TLS variable. This could also allow quickly blocking and unblocking signal delivery from user-space by storing a mask to this TLS area.

The kernel could then look into the signal mask in this TLS area whenever it needs to deliver a signal (assuming this code path can take user-space faults), in addition to the mask kept within the task struct.

This "tls cache" idea could also apply to setting a CPU affinity to the currently running CPU for short user-space critical sections.

The benefit here is to get _very_ fast operations on the thread umask and cpu affinity.

Are those ideas too far-fetched ?

Thanks,

Mathieu

Quoted 35 lines

Another approach to this has been attempted before, adding something to /proc, although it didn't go anywhere. See:

http://comments.gmane.org/gmane.linux.kernel/1292109

Another way to solve this would be to add a thread-safe getumask to glibc. Since glibc could own the mutex, this would permit libraries linked to this glibc to read umask safely.

I should also note that man-pages documents getumask(3), but no version of glibc has ever implemented it.

Typical test script:

#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])
{
int r = syscall(329);

 if (r == -1) {
   perror("getumask");
   exit(1);

}
printf("umask = %o\n", r);
exit(0);
}

$ ./getumask
umask = 22

Rich.

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#22 lore raw
In reply to Richard W.M. Jones
Quoted 3 lines · Richard W.M. Jones

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

I assume you just want to do this from a shared library so you can determine whether or not you need to call fchown() after making files and the like? If that's the case it'd be good to note it in the commit message.

BTW...it might be a good idea to add a flags argument:
https://lwn.net/Articles/585415/

Did you consider calling this umask2, having the initial version only support retrieving it via a UMASK_GET flag, and lay the groundwork to support setting a threadsafe umask with a UMASK_SET_THREAD flag?

#23 lore raw
Quoted 10 lines · Colin Walters
Quoted 3 lines · Richard W.M. Jones

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

I assume you just want to do this from a shared library so you can determine whether or not you need to call fchown() after making files and the like? If that's the case it'd be good to note it in the commit message.

Yes, the use case is something like that. I write a shared library (libguestfs) and we get bug reports that turn out to be caused by odd umask settings. Of course we fix these on a case-by-case basis, but we also want to include the current umask in debug output so that we can identify the problem quickly in future reports.

Actually I wrote a rather involved getumask substitute:

https://github.com/libguestfs/libguestfs/blob/master/src/launch.c#L477

It works by creating a temporary directory, writing a file inside that directory with mode 0777, then calling fstat(2) to work out what mode the kernel gave it.

It turns out this code is not even correct. It was pointed out to me that there is a filesystem umask mount option (and fmask, dmask too) which stops this from working properly.

So it's a lot of work to read umask safely inside a shared library.

I will update the commit comment with a brief summary of the above.

Quoted 7 lines

BTW...it might be a good idea to add a flags argument:
https://lwn.net/Articles/585415/

Did you consider calling this umask2, having the initial version only support retrieving it via a UMASK_GET flag, and lay the groundwork to support setting a threadsafe umask with a UMASK_SET_THREAD flag?

Can certainly do it like this if that is preferable.

For my needs, getumask as implemented now is sufficient.

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#11 lore raw
In reply to Mathieu Desnoyers
Quoted 17 lines · ----- On Apr 13, 2016, at 11:39 AM, Mathieu Desnoyers…
Quoted 6 lines · ----- On Apr 13, 2016, at 8:57 AM, Richard W.M. Jones…

v1 -> v2:

- Use current_umask() instead of current->fs->umask.

- Retested it.

Quoted 8 lines

----------------------------------------------------------------------

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

This patch series adds a trivial system call "getumask" which returns the umask of the current process.

Quoted 8 lines

In addition to this system call, we could extend a variation of my thread_local_abi system call (https://lkml.org/lkml/2016/4/4/455)
(could be without features flags, or an entirely new system call specifically for a umask cache) to register a "current umask" cache located in a TLS area.

Basically, reading the current umask value would be a simple load from a TLS variable.

I'm actually discussing 3 separate things here: the umask, sigmask, and cpu affinity mask.

Not sure if caching the umask in a TLS would be that useful, though. The caching idea seems to make more sense for signal mask and cpu affinity mask.

Thanks,

Mathieu

Quoted 60 lines

This could also allow quickly blocking and unblocking signal delivery from user-space by storing a mask to this TLS area.

The kernel could then look into the signal mask in this TLS area whenever it needs to deliver a signal (assuming this code path can take user-space faults), in addition to the mask kept within the task struct.

This "tls cache" idea could also apply to setting a CPU affinity to the currently running CPU for short user-space critical sections.

The benefit here is to get _very_ fast operations on the thread umask and cpu affinity.

Are those ideas too far-fetched ?

Thanks,

Mathieu

Quoted 31 lines

Another approach to this has been attempted before, adding something to /proc, although it didn't go anywhere. See:

http://comments.gmane.org/gmane.linux.kernel/1292109

Another way to solve this would be to add a thread-safe getumask to glibc. Since glibc could own the mutex, this would permit libraries linked to this glibc to read umask safely.

I should also note that man-pages documents getumask(3), but no version of glibc has ever implemented it.

Typical test script:

#include <stdio.h>
#include <stdlib.h>
#include <linux/unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])
{
int r = syscall(329);

 if (r == -1) {
   perror("getumask");
   exit(1);

}
printf("umask = %o\n", r);
exit(0);
}

Quoted 4 lines
$ ./getumask
umask = 22

Rich.

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#12 lore raw
Quoted 2 lines · Mathieu Desnoyers

I'm actually discussing 3 separate things here: the umask, sigmask, and cpu affinity mask.

The last two are available in /proc/<pid>/status --- which brings up the question why not just add umask to /proc/<pid>/status?

That way the shared library can read it via /proc/self/status, but this way it would be possible to look at other process's umask values this as well.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Another approach to this has been attempted before, adding something to /proc, although it didn't go anywhere. See:

http://comments.gmane.org/gmane.linux.kernel/1292109

... and indeed that's what I suggested. It looks like from the thread that it petered out due to apathy instead of people not liking the idea.

One other reason to suggest using a /proc file is that you're not at the mercy of the glibc folks to wire up a new system call. (Glibc has
been refusing to wire up getrandom(2), for example. Grrrr.....)

- Ted

#8 lore raw
In reply to Greg KH
Quoted 2 lines · On Wed, 13 Apr 2016 06:59:50 -0700 Greg KH <[email protected]> wrote:

Why not add this to the ktest infrastructure, we strongly encourage that for new syscalls, along with a man page patch.

Do you mean the "selftest infrastructure"? As I don't see how this could be used with ktest.

See tools/testing/selftests/

-- Steve

#21 lore raw
In reply to Mathieu Desnoyers
Quoted 29 lines · Mathieu Desnoyers
Quoted 17 lines · ----- On Apr 13, 2016, at 11:39 AM, Mathieu Desnoyers…
Quoted 6 lines · ----- On Apr 13, 2016, at 8:57 AM, Richard W.M. Jones…

v1 -> v2:

- Use current_umask() instead of current->fs->umask.

- Retested it.

Quoted 8 lines

----------------------------------------------------------------------

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

This patch series adds a trivial system call "getumask" which returns the umask of the current process.

Quoted 8 lines

In addition to this system call, we could extend a variation of my thread_local_abi system call (https://lkml.org/lkml/2016/4/4/455)
(could be without features flags, or an entirely new system call specifically for a umask cache) to register a "current umask" cache located in a TLS area.

Basically, reading the current umask value would be a simple load from a TLS variable.

Quoted 7 lines

I'm actually discussing 3 separate things here: the umask, sigmask, and cpu affinity mask.

Not sure if caching the umask in a TLS would be that useful, though. The caching idea seems to make more sense for signal mask and cpu affinity mask.

I think this is of questionable value.

Keep in mind that every feature like this adds overhead to lots of code paths as well as additional complexity. Such features should be justified by big performance benefits.

Given that processes can easily track their own umasks and signal masks if they care, I don't see why the kernel would want to help.

--Andy

#9 lore raw
In reply to Steven Rostedt
Quoted 11 lines · Steven Rostedt
Quoted 2 lines · On Wed, 13 Apr 2016 06:59:50 -0700 Greg KH <[email protected]> wrote:

Why not add this to the ktest infrastructure, we strongly encourage that for new syscalls, along with a man page patch.

Do you mean the "selftest infrastructure"? As I don't see how this could be used with ktest.

See tools/testing/selftests/

Sorry, yes, I meant selftest, not ktest, sorry for the confusion.

greg k-h

#13 lore raw
In reply to Theodore Ts'o
Quoted 5 lines · Theodore Ts'o

One other reason to suggest using a /proc file is that you're not at the mercy of the glibc folks to wire up a new system call. (Glibc has
been refusing to wire up getrandom(2), for example. Grrrr.....)

This brings right back up the libinux idea. There are continued concerns about type compatibility, but saying "oh, use syscall(3) instead" has worse properties than a Linux-kernel-team maintained libinux. Last I heard the glibc team had (reluctantly?) agreed to do something to deal with linux-specific system calls, but last I heard nothing had happened. The last discussion I see on the glibc mailing list dates back to November, and that thread seems to have died from bikeshedding, again.

There aren't a *lot* of such system calls, but even in that thread the "oh, only two applications need this, let them use syscall(3)" seems to remain.

-hpa

#14 lore raw
Quoted 16 lines · H. Peter Anvin
Quoted 5 lines · Theodore Ts'o

One other reason to suggest using a /proc file is that you're not at the mercy of the glibc folks to wire up a new system call. (Glibc has
been refusing to wire up getrandom(2), for example. Grrrr.....)

This brings right back up the libinux idea. There are continued concerns about type compatibility, but saying "oh, use syscall(3) instead" has worse properties than a Linux-kernel-team maintained libinux. Last I heard the glibc team had (reluctantly?) agreed to do something to deal with linux-specific system calls, but last I heard nothing had happened. The last discussion I see on the glibc mailing list dates back to November, and that thread seems to have died from bikeshedding, again.

Quoted 3 lines

There aren't a *lot* of such system calls, but even in that thread the "oh, only two applications need this, let them use syscall(3)" seems to remain.

And only 2 applications will continue to use it because no one wants to write syscall() wrappers for their individual applications, so it's a vicious cycle :(

I really like the 'libinux' idea, did anyone every hack up a first-pass at this? And I'm guessing we have more syscalls now that would need to be added (like getrandom(), but that shouldn't be too difficult.

thanks,

greg k-h

#24 lore raw
In reply to Colin Walters
Quoted 11 lines · Colin Walters
Quoted 3 lines · Richard W.M. Jones

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

I assume you just want to do this from a shared library so you can determine whether or not you need to call fchown() after making files and the like? If that's the case it'd be good to note it in the commit message.

Quoted 7 lines

BTW...it might be a good idea to add a flags argument:
https://lwn.net/Articles/585415/

Did you consider calling this umask2, having the initial version only support retrieving it via a UMASK_GET flag, and lay the groundwork to support setting a threadsafe umask with a UMASK_SET_THREAD flag?

The comments on that article also list a number of problems with this approach, related to how undefined flags are handled.

In fact, if it wasn't for this exact problem then umask(-1) would have been the logical way to deal with this, but because umask(2) is defined to have an internal & 07777 it becomes infeasible at least in theory. In practice it might work...

However, see previous discussions about making this available in /proc.
Also, I really think there is something to be said for a O_NOUMASK
option...

-hpa

#25 lore raw
Quoted 23 lines · H. Peter Anvin
Quoted 11 lines · Colin Walters
Quoted 3 lines · Richard W.M. Jones

It's not possible to read the process umask without also modifying it, which is what umask(2) does. A library cannot read umask safely, especially if the main program might be multithreaded.

I assume you just want to do this from a shared library so you can determine whether or not you need to call fchown() after making files and the like? If that's the case it'd be good to note it in the commit message.

Quoted 7 lines

BTW...it might be a good idea to add a flags argument:
https://lwn.net/Articles/585415/

Did you consider calling this umask2, having the initial version only support retrieving it via a UMASK_GET flag, and lay the groundwork to support setting a threadsafe umask with a UMASK_SET_THREAD flag?

The comments on that article also list a number of problems with this approach, related to how undefined flags are handled.

Quoted 8 lines

In fact, if it wasn't for this exact problem then umask(-1) would have been the logical way to deal with this, but because umask(2) is defined to have an internal & 07777 it becomes infeasible at least in theory. In practice it might work...

However, see previous discussions about making this available in /proc.
Also, I really think there is something to be said for a O_NOUMASK
option...

O_NOUMASK seems potentially useful to support implementation of umask entirely in userspace, which also addresses thread-safety. A program could read its process umask out at startup, handle umask entirely in userspace (including for threads), and only interact with the system umask after fork and before exec.

- Josh Triplett

#15 lore raw
In reply to Greg KH
Quoted 5 lines · Greg KH
Quoted 4 lines

There aren't a *lot* of such system calls, but even in that thread the "oh, only two applications need this, let them use syscall(3)" seems to remain.

Quoted 8 lines

And only 2 applications will continue to use it because no one wants to write syscall() wrappers for their individual applications, so it's a vicious cycle :(

I really like the 'libinux' idea, did anyone every hack up a first-pass at this? And I'm guessing we have more syscalls now that would need to be added (like getrandom(), but that shouldn't be too difficult.

I haven't hacked anything up yet, but I think it would be easy to simply take the klibc machinery, remove the stuff that isn't needed, and adjust the handling of errno(3). I think it is time I admit that it needs to be put on my TODO list.

-hpa

#16 lore raw
In reply to Greg KH
Quoted 21 lines · Greg KH
Quoted 16 lines · H. Peter Anvin
Quoted 5 lines · Theodore Ts'o

One other reason to suggest using a /proc file is that you're not at the mercy of the glibc folks to wire up a new system call. (Glibc has
been refusing to wire up getrandom(2), for example. Grrrr.....)

This brings right back up the libinux idea. There are continued concerns about type compatibility, but saying "oh, use syscall(3) instead" has worse properties than a Linux-kernel-team maintained libinux. Last I heard the glibc team had (reluctantly?) agreed to do something to deal with linux-specific system calls, but last I heard nothing had happened. The last discussion I see on the glibc mailing list dates back to November, and that thread seems to have died from bikeshedding, again.

Quoted 3 lines

There aren't a *lot* of such system calls, but even in that thread the "oh, only two applications need this, let them use syscall(3)" seems to remain.

Quoted 7 lines

And only 2 applications will continue to use it because no one wants to write syscall() wrappers for their individual applications, so it's a vicious cycle :(

I really like the 'libinux' idea, did anyone every hack up a first-pass at this? And I'm guessing we have more syscalls now that would need to be added (like getrandom(), but that shouldn't be too difficult.

Personally, I'd suggest that libinux should wire up *all* (non-obsolete) syscalls, not just those that haven't already been exposed via any particular libc implementation. Each such syscall function would have minimal overhead, since unlike libc these wrappers would not have any special handling (other than use of the vdso) and would directly map to the kernel syscall signature. Given a standard prefix like sys_ or linux_, that would provide a clear distinction between direct-syscall functions and libc functions, and avoid any future conflict if libc adds a function named the same as the syscall.

As a random example, sys_getpid() would *always* call the getpid syscall, rather than reading a cache within the library. (And sys_gettid would call the gettid syscall, rather than failing to exist.)

#17 lore raw
Quoted 15 lines · Josh Triplett
Quoted 4 lines

I really like the 'libinux' idea, did anyone every hack up a first-pass at this? And I'm guessing we have more syscalls now that would need to be added (like getrandom(), but that shouldn't be too difficult.

Personally, I'd suggest that libinux should wire up *all* (non-obsolete) syscalls, not just those that haven't already been exposed via any particular libc implementation. Each such syscall function would have minimal overhead, since unlike libc these wrappers would not have any special handling (other than use of the vdso) and would directly map to the kernel syscall signature. Given a standard prefix like sys_ or linux_, that would provide a clear distinction between direct-syscall functions and libc functions, and avoid any future conflict if libc adds a function named the same as the syscall.

Quoted 4 lines

As a random example, sys_getpid() would *always* call the getpid syscall, rather than reading a cache within the library. (And sys_gettid would call the gettid syscall, rather than failing to exist.)

I'm not so sure this is a good idea. It has definite pros and cons. In some ways it pushes it more to be like syscall(3).

-hpa

#18 lore raw
Quoted 16 lines · H. Peter Anvin
Quoted 15 lines · Josh Triplett
Quoted 4 lines

I really like the 'libinux' idea, did anyone every hack up a first-pass at this? And I'm guessing we have more syscalls now that would need to be added (like getrandom(), but that shouldn't be too difficult.

Personally, I'd suggest that libinux should wire up *all* (non-obsolete) syscalls, not just those that haven't already been exposed via any particular libc implementation. Each such syscall function would have minimal overhead, since unlike libc these wrappers would not have any special handling (other than use of the vdso) and would directly map to the kernel syscall signature. Given a standard prefix like sys_ or linux_, that would provide a clear distinction between direct-syscall functions and libc functions, and avoid any future conflict if libc adds a function named the same as the syscall.

Quoted 6 lines
Quoted 3 lines

As a random example, sys_getpid() would *always* call the getpid syscall, rather than reading a cache within the library. (And sys_gettid would call the gettid syscall, rather than failing to exist.)

I'm not so sure this is a good idea. It has definite pros and cons. In some ways it pushes it more to be like syscall(3).

It seems like one of the main problems with syscall() is that it forces userspace to handle weird ABI issues, such as syscall numbers varying by architecture, encoding of 64-bit arguments on 32-bit platforms (see the example in the syscall manpage), and other subtleties that will break on architectures other than the one the developer is most likely to be running. libinux bindings would eliminate those issues.

What cases do you have in mind where the libinux binding should look different than the C API of the SYSCALL_DEFINE'd function in the kernel?

Users can still call the libc syscall when they want libc's behavior; for syscalls that have a libc binding, most users will want that version. But I've often needed to call the underlying syscall even for syscalls that *do* have a libc binding, for various purposes, and having a standard way to do that while still having safe type signatures seems helpful.

This would also make it much easier to write an alternative libc, or a language standard library that doesn't want to depend on libc.

- Josh Triplett

#19 lore raw
Quoted 18 lines · Josh Triplett

It seems like one of the main problems with syscall() is that it forces userspace to handle weird ABI issues, such as syscall numbers varying by architecture, encoding of 64-bit arguments on 32-bit platforms (see the example in the syscall manpage), and other subtleties that will break on architectures other than the one the developer is most likely to be running. libinux bindings would eliminate those issues.

What cases do you have in mind where the libinux binding should look different than the C API of the SYSCALL_DEFINE'd function in the kernel?

Users can still call the libc syscall when they want libc's behavior; for syscalls that have a libc binding, most users will want that version. But I've often needed to call the underlying syscall even for syscalls that *do* have a libc binding, for various purposes, and having a standard way to do that while still having safe type signatures seems helpful.

Quoted 3 lines

This would also make it much easier to write an alternative libc, or a language standard library that doesn't want to depend on libc.

The main problem has to do with types, and the fact that the C library may want to intersperse itself around system calls. If people start writing programs that call, say, __linux_umask() then it would make it hard for libc to do something special with umask().

There are other things like it, e.g. where dev_t and __kernel_dev_t are concerned.

Now, we could of course have __linux_getrandom() and make a weak alias for getrandom(), but I really don't understand the use case for exporting all the system calls.

-hpa

#26 lore raw
In reply to Josh Triplett
Quoted 5 lines · Josh Triplett

O_NOUMASK seems potentially useful to support implementation of umask entirely in userspace, which also addresses thread-safety. A program could read its process umask out at startup, handle umask entirely in userspace (including for threads), and only interact with the system umask after fork and before exec.

I had a look at O_NOUMASK and there are a few problems:

It's relatively easy to implement for open(2). A few filesystems implement their own open so I had to go into those filesystems and modify how they handle current_umask too. And FUSE support is tricky so I passed on that.

The real problem is that mkdir/mkdirat/mknod/mknodat are affected by umask, but there is no convenient flags parameter to pass the O_NOUMASK flag. So I think the patch only half-solves the problem.

I have a patch which needs a bit more testing, which I can post if you think that's helpful, but I don't think it would be acceptable in its current state.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#27 lore raw
Quoted 18 lines · Richard W.M. Jones
Quoted 5 lines · Josh Triplett

O_NOUMASK seems potentially useful to support implementation of umask entirely in userspace, which also addresses thread-safety. A program could read its process umask out at startup, handle umask entirely in userspace (including for threads), and only interact with the system umask after fork and before exec.

I had a look at O_NOUMASK and there are a few problems:

It's relatively easy to implement for open(2). A few filesystems implement their own open so I had to go into those filesystems and modify how they handle current_umask too. And FUSE support is tricky so I passed on that.

The real problem is that mkdir/mkdirat/mknod/mknodat are affected by umask, but there is no convenient flags parameter to pass the O_NOUMASK flag. So I think the patch only half-solves the problem.

Quoted 5 lines

I have a patch which needs a bit more testing, which I can post if you think that's helpful, but I don't think it would be acceptable in its current state.

Rich.

Ironically this illustrates one of the limitations with flags arguments: this really belongs in the S_-flags, but we can't assume userspace is clean there... anymore than we can repurpose umask(-1).

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse brevity and formatting.
#20 lore raw
In reply to H. Peter Anvin
Quoted 3 lines · H. Peter Anvin

Now, we could of course have __linux_getrandom() and make a weak alias for getrandom(), but I really don't understand the use case for exporting all the system calls.

If we do create a libinux library, I'd definitely want getrandom(2) to be defined as getrandom(), and not as sys_getrandom() or linux_getrandom().

For bonus points we could also define a OpenBSD compatible getentropy() interface so that programs that were already written to use the OpenBSD interface (and perhaps have a configure test for it already) would get the benefits of the getrandom(2) system call immediately.

- Ted