Skip to content

Instantly share code, notes, and snippets.

@time-river
Created December 27, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save time-river/a554700a47d4927071ee933c075bae6d to your computer and use it in GitHub Desktop.
Save time-river/a554700a47d4927071ee933c075bae6d to your computer and use it in GitHub Desktop.
syscall test
#ifndef __COMMON_H
#define __COMMON_H
#define ERRNO_OFFSET 0x80
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <spawn.h>
#include <limits.h>
#include <signal.h>
#include "common.h"
#define REPORT(func, status) \
do { \
if (WIFEXITED((status)) && \
WEXITSTATUS((status)) > ERRNO_OFFSET) { \
fprintf(stdout, "run '%s' failed during other syscall: %s\n", \
(func), strerror(WEXITSTATUS((status))-ERRNO_OFFSET)); \
} else if (WIFEXITED((status)) && \
WEXITSTATUS((status)) != EXIT_SUCCESS) { \
fprintf(stdout, "'%s' test failed: %s\n", \
(func), strerror(WEXITSTATUS((status)))); \
} else if (WIFEXITED((status)) && \
WEXITSTATUS((status)) == EXIT_SUCCESS) { \
fprintf(stdout, "'%s' test pass!\n", (func)); \
} else { \
fprintf(stdout, "'%s' test terminated abnormally\n", func); \
} \
} while(0)
char **environ;
int main(int argc, char *argv[]) {
int retval, status;
pid_t prog;
char path[PATH_MAX] = {0}, prefix[PATH_MAX] = {0};
char *syscalls[] = {
"write"
};
if (getcwd(prefix, sizeof(prefix)) == NULL) {
fprintf(stderr, "getcwd failed: %s\n", strerror(errno));
return 0;
}
for (int i = 0; i < sizeof(syscalls)/sizeof(char *); i++) {
retval = snprintf(path, sizeof(path), "%s/%s", prefix, syscalls[i]);
path[retval] = '\0';
retval = posix_spawnp(&prog, path, NULL, NULL, argv, environ);
if (retval != 0) {
fprintf(stderr, "posix_spawnp('%s') failed: %s\n",
path, strerror(errno));
}
while (waitpid(prog, &status, 0) != prog && errno == EINTR)
continue;
if (retval == 0)
REPORT(syscalls[i], status);
}
return 0;
}
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "common.h"
int main(int argc, char *argv[]) {
int fd;
char buffer[64] = "1234";
fd = syscall(__NR_open, "/dev/null", O_WRONLY);
if (fd < 0)
return errno+ERRNO_OFFSET;
syscall(__NR_write, fd, buffer, strlen(buffer));
return errno;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment