-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a small set of Linux system call examples.
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*~ | ||
a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |