Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use system calls #6708

Closed
nepx opened this issue Jun 16, 2018 · 5 comments
Closed

Unable to use system calls #6708

nepx opened this issue Jun 16, 2018 · 5 comments
Labels

Comments

@nepx
Copy link

nepx commented Jun 16, 2018

I am trying to leverage system calls in Emscripten. Here's syscall.c, a small test I wrote:

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <signal.h>

int main(int argc, char *argv[]) {
    syscall(SYS_close, -1);
    syscall(SYS_exit, 15);
}

And got the following when I compiled with Emscripten:

$ emcc syscall.c -o syscall.html
syscall.c:10:12: error: implicit declaration of function 'syscall' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
           syscall(SYS_close, -1);
           ^

However, the same program compiles on GCC and Clang.

I know that Emscripten supports system calls (library_syscall.js), but they seem to be hidden from the user, or at least inaccessible.

Are there any flags that enable this or any defines I need to use? I tried adding #include <emscripten.h> but that doesn't work either.

@kripken
Copy link
Member

kripken commented Jun 18, 2018

I think we don't have the generic syscall method, and instead implemented direct calls to the various ones, so that we can analyze them statically, which is useful for reducing code size. See details in ./system/lib/libc/musl/src/internal/syscall.h.

@nepx
Copy link
Author

nepx commented Jun 19, 2018

How would one go around calling something like, say, SYS_close? Is there an API of some sort or some header files I have to include (because musl seems to use syscall directly? I'm not sure)?

And I know that using system calls isn't the most portable of assumptions to make, but since I'm already doing a lot of Emscripten-specific stuff in my project I thought I might as well take advantage of the browser-based system calls.

@kripken
Copy link
Member

kripken commented Jun 19, 2018

Not sure there would be much benefit to calling them directly, as opposed to calling musl. Calling musl would also keep working if we change the syscall API, which might happen.

But if you want to, I'd look at how musl does it: something like __syscall(SYS_close, fd); appears in the code. iirc, __syscall is a macro that expands into a static call.

@nepx
Copy link
Author

nepx commented Jun 20, 2018

In my project, I decided to just go with the wrappers, (i.e. close rather than syscall(SYS_close)), but it would be nice to have a standardized system call interface (to call things like, say, renameat2, which has no glibc wrapper). Exposing the syscall interface might make low-level code easier to port.

@stale
Copy link

stale bot commented Sep 18, 2019

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants