5 ms·
System calls can return them separately, but don’t have to. On Linux a failing syscall returns -errno; the syscall wrapper assumes any return value in the range
by nneonneo 7y ago
System calls can return them separately, but don’t have to. On Linux a failing syscall returns -errno; the syscall wrapper assumes any return value in the range -1 to -4095 is an error, stashes the positive error number in the C errno variable and translates the return value to -1.
- tyingq 7y agoposix_spawn() is reasonably portable and returns them separately.
- klodolph 7y agoIt’s also not a system call :-/ at least with Linux/glibc. Although this is not really the point of the discussion, I’m not aware of system calls that return their result separately the way that posix_spawn does. They obviously could.
- tyingq 7y agoI was comparing to fork() setting the errno global.
- kazinator 7y agoModern errno is a macro that designates a thread-specific location.
- klodolph 7y agoWhat posix_spawn does is something like this: posix_spawn() { ... pid_t pid = fork(); if (pid == 0) { ... } else if (pid > 0) { ... if (...) { ... return ernno_from_child; } ... } else { return errno; } ... } I’m just pointing out that you’re still using errno, you’ve just hidden it behind a function. All of your syscalls are still getting their results stored in errno. It’s just fork() / exec() with some extra sauce, not a syscall in itself. I’m not trying to argue, I’m just pointing out that this is just a wrapper function on Linux anyway, and if you want to find an example of a different way to report errors from the kernel, it’s not posix_spawn().
- tyingq 7y agoPosix_spawn on Linux uses the clone() system call, and passes a pointer called args. Errors come back at args.err
- klodolph 7y agoposix_spawn() actually uses fork() internally, not clone(), as far as that distinction is possible to make. Both fork() and clone() are implemented with the same kernel entry point, which is called "clone". Here is the relevant line in glibc: https://github.com/bminor/glibc/blob/b5b7fb76e15c0db545aa11a3ce88f836e5d01a19/sysdeps/posix/spawni.c#L303 https://github.com/bminor/glibc/blob/b5b7fb76e15c0db545aa11a... Errors are returned as the function return value.
- tyingq 7y agohttps://github.com/bminor/glibc/blob/master/sysdeps/unix/sysv/linux/spawni.c#L36 https://github.com/bminor/glibc/blob/master/sysdeps/unix/sys... "The Linux implementation of posix_spawn{p} uses the clone syscall directly with CLONE_VM and CLONE_VFORK flags..." Though you're right about errno, line 305.