Skip to content

Instantly share code, notes, and snippets.

@hazirguo
Created September 21, 2013 09:00
Show Gist options
  • Save hazirguo/6648755 to your computer and use it in GitHub Desktop.
Save hazirguo/6648755 to your computer and use it in GitHub Desktop.
inline assembly with gcc to write a syscall function.
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <errno.h>
int main()
{
long rc;
char *file_name = "/etc/passwd";
unsigned short mode = 0444;
asm(
"int $0x80"
: "=a" (rc)
: "0" (SYS_chmod), "b" (file_name), "c" (mode)
);
if ((unsigned long)rc >= (unsigned long)-129) {
errno = -rc;
rc = -1;
}
if (rc == -1)
fprintf(stderr, "chmode failed, errno = %d\n", errno);
else
printf("success!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment