prettify code, fix bugs, make it isolated compile with gcc

This commit is contained in:
root 2024-08-21 17:25:19 +05:00
parent 6c3b64ed39
commit bb8d9fa5ca
7 changed files with 116 additions and 36 deletions

View file

@ -25,7 +25,10 @@ void prepare_cgroup(struct limits* limits) {
asprintf(&cpus_string,"%d\n",limits->core);
write_file("cpuset.cpus",cpus_string);
free(cpus_string);
write_file("pids.max","1\n");
char* processes_string=NULL;
asprintf(&processes_string,"%d\n",limits->processes);
write_file("pids.max",processes_string);
free(processes_string);
//write_file("cpuset.cpus","3\n");
chdir(cwd);
}

View file

@ -10,7 +10,7 @@ NAME=minrootfs
SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE}")")
rm -rf $SCRIPT_DIR/$NAME
sudo $SCRIPT_DIR/alpine-make-rootfs/alpine-make-rootfs $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

@ -38,8 +38,8 @@ void ro_fs(char* shared_mountpoint) {
char* shared_folder = malloc(strlen(shared_mountpoint)+4);
strcpy(shared_folder,"../");
strcat(shared_folder,shared_mountpoint);
if (mount("/","/","ext4",MS_REMOUNT | MS_RDONLY | MS_BIND,"")) die("failed to mount: %m");
if (mount(shared_mountpoint,"shared","ext4",MS_REMOUNT | MS_RDONLY | MS_BIND,"")) die("failed to mount: %m");
if (mount("/","/","ext4",MS_REMOUNT | MS_RDONLY| MS_BIND,"")) die("failed to mount: %m");
if (mount(shared_mountpoint,"shared","ext4",MS_REMOUNT | MS_BIND,"")) die("failed to mount: %m");
free(shared_folder);
}
//#undef shared_mountpoint
@ -56,11 +56,19 @@ static int nsrun(void* arg) {
if (prctl(PR_SET_PDEATHSIG, SIGKILL)) die("cannot PR_SET_PDEATHSIG for child process: %m\n");
struct params *params = (struct params*) arg;
await_setup(params->fd[0]);
if(chdir("shared")) die("failed to chdir: %m");
//int out_fd=open("out",O_WRONLY|O_CREAT);
//if(out_fd==-1) die("unable to open out file:%m");
//else dup2(out_fd,STDOUT_FILENO);
if(chdir("..")) die("failed to chdir: %m");
char cwd[PATH_MAX];
if(getcwd(cwd,sizeof(cwd))==NULL) die("getcwd error: %m");
if(chdir(params->shared_folder)) die("failed to chdir: %m");
int out_fd=open("out",O_WRONLY|O_CREAT,0666);
if(out_fd==-1) die("unable to open output file:%m");
else dup2(out_fd,STDOUT_FILENO);
int in_fd=open("in",O_RDONLY|O_CREAT,0666);
if(in_fd==-1) die("unable to open input file:%m");
else dup2(in_fd,STDIN_FILENO);
int err_fd=open("err",O_WRONLY|O_CREAT,0666);
if(err_fd==-1) die("unable to open error file:%m");
else dup2(err_fd,STDERR_FILENO);
if(chdir(cwd)) die("failed to chdir: %m");
mnt_prepare("minrootfs",params->shared_folder);
sethostname(hostname,sizeof(hostname));
ro_fs(params->shared_folder);

View file

@ -28,6 +28,7 @@ struct limits {
size_t memory;
int core;
int time;
int processes;
};
struct killparams{
@ -65,11 +66,13 @@ static char nmstack[STACK_SIZE];
static char killstack[STACK_SIZE];
static void parse_args(int argc, char **argv, struct params *params,struct limits *limits){
if (argc < 6) {
puts("usage:\n starter <core_id> <memory_amount> <time limit in ms> <shared folder> <command, arg1,arg2,...>");
if (argc < 7) {
puts("usage:\n starter <max processes number> <core_id> <memory_amount> <time limit in ms> <shared folder> <command, arg1,arg2,...>");
exit(0);
}
argc--; argv++;
limits->processes = atoi(argv[0]);
argc--; argv++;
limits->core = atoi(argv[0]);
argc--; argv++;
limits->memory = atoi(argv[0]);