Dan Newcome, blog

I'm bringing cyber back

Making system calls in OS X

leave a comment »

I’ve been hacking away on my new Mac ever since I got it last month, and I’m slowly getting all of the native stuff I used to do under Linux moved over to my new environment.

Some of the things I’ve gotten installed so far are homebrew (needed to install just about anything that isn’t distributed directly as a disk image or precompiled binary), Xcode command line tools (needed to build many homebrew packages), rvm (ruby version manager), git (via homebrew) and many other homebrew packages.

So anyway, I’ve been working on getting some low-latency HID (keyboard) code working under Windows and I’m eventually going to try to get it working on the Mac. I’m able to make system calls on Windows pretty easily by now, but I was wondering how it worked on Mac.

There is a big list of all the system calls for OS X here. Notice that the integer ID for EXIT is 1.

The following code snippet calls exit() via syscall() and returns an exit code of 7:

#include <stdio.h>
#include <sys/syscall.h>

int main( int argc, char** argv ) {

        /* system call 1 is EXIT */
        syscall(1, 7);
        
        /* we will never get here */
        printf("arg0: %s \n", argv[0] );
        return 0;
}

Compile with gcc and run like this:

./a.out ; echo $?

Here we output the return code to the console so that we can see it.

About these ads

Written by newcome

February 1, 2013 at 1:33 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: