Skip to content

Commit

Permalink
Added a small set of Linux system call examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
dondi committed Feb 5, 2013
1 parent 7147f50 commit 57cdf35
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions syscall-samples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*~
a.out
2 changes: 2 additions & 0 deletions syscall-samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*Note*: Each C source file here is a self-contained Linux program.
`gcc` them individually to build and run.
24 changes: 24 additions & 0 deletions syscall-samples/getcwd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This program demonstrates invocation of the getcwd
* system call (183) using the syscall function.
*/
#include <linux/types.h>
#include <linux/unistd.h>
#include <string.h>

int main(int argc, char *argv[]) {
char result[1024]; // Lots of room!

// We keep the hardcoded system call numbers to illustrate
// that they are still just that: numbers.
syscall(183, result);

// Append a newline for better looks.
int originalLength = strlen(result);
result[originalLength] = '\n';

// Must re-terminate the string, of course.
result[originalLength + 1] = '\0';

syscall(4, 0, result, strlen(result));
}
22 changes: 22 additions & 0 deletions syscall-samples/mkdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This program demonstrates invocation of the mkdir
* system call (39) using the syscall function.
*/
#include <linux/types.h>
#include <linux/unistd.h>
#include <string.h>

int main(int argc, char *argv[]) {
// We demonstrate the use of command-line arguments here.
// But note the non-existent error handling (all the better
// to illustrate the error code below).
int result = syscall(39, argv[1], 0777 /* Note: octal */);

// A result of -1 means that something went wrong. Otherwise,
// check for your new directory!
if (result == -1) {
// Don't use this error message in "real" programs. O_o
char *errorMessage = "Herp derp mkderp\n";
syscall(4, 2, errorMessage, strlen(errorMessage));
}
}
24 changes: 24 additions & 0 deletions syscall-samples/sysinfo-ram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* This program demonstrates invocation of the sysinfo
* system call (116) using the syscall function. Some
* of its output is then displayed.
*/
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/unistd.h>

// For convenience, we just use the stdio functions to display
// the output for this one.
#include <stdio.h>

int main(int argc, char *argv[]) {
// The sysinfo structure is in kernel.h.
struct sysinfo result;

// We keep the hardcoded system call numbers to illustrate
// that they are still just that: numbers.
syscall(116, &result);

// Display some of the returned RAM information.
printf("Total RAM: %ld, free RAM: %ld\n", result.totalram, result.freeram);
}

0 comments on commit 57cdf35

Please sign in to comment.