remove go part; prettify c code

This commit is contained in:
dragonmuffin 2025-01-07 21:56:21 +05:00
parent 1fa11f66e3
commit ebc4311c86
21 changed files with 86 additions and 742 deletions

View file

@ -1,8 +1,4 @@
#include<sys/prctl.h>
#include<sys/mount.h>
#include<sys/stat.h>
#include<unistd.h>
#include<linux/limits.h>
#include "starter.h"
#define CGROUP_NAME_SIZE 20
char cgroup_name[CGROUP_NAME_SIZE+1];
@ -74,3 +70,4 @@ void remove_cgroup() {
rmdir(cgroup_name);
chdir(cwd);
}

View file

@ -13,5 +13,4 @@ rm -rf $SCRIPT_DIR/$NAME
$SCRIPT_DIR/alpine-make-rootfs/alpine-make-rootfs --packages 'python3 gcc libc-dev' $SCRIPT_DIR/$NAME
#chown -R nobody:nogroup $SCRIPT_DIR/$NAME
#rm -r ../$NAME
#mv $NAME ../$NAME

View file

@ -1,8 +1,6 @@
#include<sys/prctl.h>
#include<sys/mount.h>
#include<sys/stat.h>
#include "starter.h"
static void procfs_prepare()
void procfs_prepare()
{
if (mkdir("/proc", 0555) && errno != EEXIST) die("Failed to mkdir /proc: %m\n");
if (mount("proc", "/proc", "proc", 0, "")) die("Failed to mount proc: %m\n");
@ -43,7 +41,7 @@ void await_setup(int pipe) { // wait for signal from parent
if (read(pipe, buf, 2) != 2) die("Failed to read from pipe: %m\n");
}
static int nsrun(void* arg) {
int nsrun(void* arg) {
//die when parent dies
if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n");
struct params *params = (struct params*) arg;

View file

@ -1,43 +1,4 @@
#define _GNU_SOURCE
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
#include <linux/sched.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include "util.h"
struct params {
char* shared_folder;
int fd[2];
int out_fd;
char **argv;
};
struct limits {
size_t memory;
int core;
int time;
int processes;
};
struct killparams{
int time;
int pid;
};
#include "ns_exec.c"
#include "cgroup_prepare.c"
#include "starter.h"
uint64_t time_start;
@ -107,8 +68,8 @@ static void prepare_userns(int pid) {
write_file(path, line);
}
static void get_real_path(char* path) {
if (realpath (argv[0], path) == 0) die("unable to resolve real path: %m");// get absolute path to executable
static void get_real_path(char* path, char* call_str) {
if (realpath (call_str, path) == 0) die("unable to resolve real path: %m");// get absolute path to executable
for(int i = strlen(path); i > 0 && path[i] != '/';i--) path[i]=0;// cut filename to get directory name
}
@ -117,7 +78,7 @@ int main(int argc,char** argv) {
if(setgid(0)) die("must be run as root");
// get binary path
char real_path[PATH_MAX];
get_real_path(real_path);
get_real_path(real_path, argv[0]);
if(chdir(real_path)) die("unable to chdir to binary path: %m");
// set random seed
srand(time(NULL));

53
starter/starter.h Normal file
View file

@ -0,0 +1,53 @@
#define _GNU_SOURCE
#include <stddef.h>
#include <stdint.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <linux/limits.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <sys/syscall.h>
#include <linux/sched.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#define die(...) {fprintf(stderr,"file %s,line %d\n",__FILE__,__LINE__);die_func(__VA_ARGS__);exit(1);}
void remove_cgroup();
struct params {
char* shared_folder;
int fd[2];
int out_fd;
char **argv;
};
struct limits {
size_t memory;
int core;
int time;
int processes;
};
struct killparams{
int time;
int pid;
};
extern void prepare_cgroup(struct limits* limits);
extern void add_to_cgroup(int pid);
extern int check_mem();
extern void remove_cgroup();
extern void procfs_prepare();
extern void await_setup(int pipe);
extern int nsrun(void* arg);
extern void die_func(const char *fmt, ...);
extern void write_file(char* path, char* line);

View file

@ -1,18 +1,17 @@
#define die(...) {fprintf(stderr,"file %s,line %d\n",__FILE__,__LINE__);die_func(__VA_ARGS__);}
void remove_cgroup();
#include "starter.h"
static void die_func(const char *fmt, ...)
void die_func(const char *fmt, ...)
{
va_list params;
va_start(params, fmt);
vfprintf(stderr, fmt, params);
puts("\n");
va_end(params);
remove_cgroup();
//remove_cgroup();
exit(1);
}
static void write_file(char* path, char* line)
void write_file(char* path, char* line)
{
FILE *f = fopen(path, "w");
if (f == NULL) {die("Failed to open file %s: %m\n", path);}