diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ae76b4b..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -container -root \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index af0225c..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Day-3 - -Processes and Signals diff --git a/day2/exec1 b/day2/exec1 new file mode 100644 index 0000000..8f7906f Binary files /dev/null and b/day2/exec1 differ diff --git a/day2/exec1.cpp b/day2/exec1.cpp new file mode 100644 index 0000000..ad0f1ab --- /dev/null +++ b/day2/exec1.cpp @@ -0,0 +1,21 @@ +#include +#include//process id +#include + +int main(int argc, char *argv[]) { + printf("PID OF exec1.c = %d\n", getpid());//process id + + char *args[] ={ "hello", "from" , "parent", NULL};// arguments for the execv() call + + //replace the current process with the exec2 + + execv("./exec2", args); + + +//this line will not be reached if execv() is successful +printf("back to exec1"); +return 0; + + + +} \ No newline at end of file diff --git a/day2/exec2 b/day2/exec2 new file mode 100644 index 0000000..f626202 Binary files /dev/null and b/day2/exec2 differ diff --git a/day2/exec2.cpp b/day2/exec2.cpp new file mode 100644 index 0000000..6b07ee4 --- /dev/null +++ b/day2/exec2.cpp @@ -0,0 +1,11 @@ +#include +#include//process id +#include + +int main (int argc, char *argv[]){ +printf("hello from exec2\n"); +printf("PID of the exec2 is :%d\n", getpid());//process id +return 0; + + +} diff --git a/day2/fork b/day2/fork new file mode 100644 index 0000000..c1a94f9 Binary files /dev/null and b/day2/fork differ diff --git a/day2/fork.c b/day2/fork.c new file mode 100644 index 0000000..35759a0 --- /dev/null +++ b/day2/fork.c @@ -0,0 +1,30 @@ +#include//input and output +#include +#include//getpid() +#include + +int main(int argc, char *argv){ + //fork to create a new process + pid_t pid = fork(); + + //check if fork was successful + if (pid == 0 ){ + //code for the child + printf("this is the child process and the pid is:%d\n", getpid());//shows the simultaneous run ofchild and parets + exit(0);// terminate the child process + } + + else if (pid >0){ + printf("this is the parent process and pid is :%d\n",getpid()); + //parent + } + + //error handling + else{ + printf("error while forking\n"); + exit(EXIT_FAILURE); + } +return 0; + + +} \ No newline at end of file diff --git a/day3/alarm.cpp b/day3/alarm.cpp new file mode 100644 index 0000000..3c67bc2 --- /dev/null +++ b/day3/alarm.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + +static int alarm_fired = 0; + +void ding(int sig){ + alarm_fired = 1; +} + +int main(){ + pid_t pid; + std::cout<<"alarm starting!\n";//not using namespacestd thats why std + pid = fork(); + + switch(pid){ + case -1: + { + //error handling + perror("fork failed\n"); + exit(1); + break; + } + case 0: + { + //child block + sleep(5); + pid_t parentpid = getpid(); + + kill(parentpid, SIGUSR1); + exit(0); + break; + } + default: + { + //parent + std::cout<<"waiting for alarm to go off\n"; + (void) signal(SIGUSR1, ding); + pause(); + if(alarm_fired)std::cout<<"alarm fired\n"; + std::cout<<"done\n"; + + } + } + exit(0); +} \ No newline at end of file diff --git a/day3/wait.c b/day3/wait.c new file mode 100644 index 0000000..d9678ed --- /dev/null +++ b/day3/wait.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +int main(){ + //create a child + pid_t child = fork();//fork returns 0 + int exitStatus , childPid; + + if(child == 0){ + //display childnrunning + //put the child to sleep + printf("child: running and pid:%d\n", getpid()); + + sleep(5); + + //exit the child + exit(40); + }else if(child > 0){ + //parent + printf("parent: running with pid:%d\n", getpid()); + childPid = wait(&exitStatus); + printf("parent: child finished it had the pid: %d, exitstatus:%d\n", getpid(), WEXITSTATUS(exitStatus)); + + }else{ + printf("cant fork\n"); + exit(1); + } + return 0; +} \ No newline at end of file diff --git a/day5/container b/day5/container new file mode 100644 index 0000000..da4bf9d Binary files /dev/null and b/day5/container differ diff --git a/day5/container.cpp b/day5/container.cpp new file mode 100644 index 0000000..ba17baf --- /dev/null +++ b/day5/container.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define CGROUP_SUBTREE "/sys/fs/cgroup/cgroup.subtree_control" +#define REQ_CGROUP "/sys/fs/cgroup/cntr" + +void WRITE(const char* path, const char* value) { + int fd = open(path, O_WRONLY | O_APPEND); + if (fd == -1) { + perror("Error in opening"); + exit(1); + } + + ssize_t bytes = write(fd, value, strlen(value)); + if (bytes == -1) { + perror("Error in writing"); + } + + close(fd); +} + +void makeCgroup() { + WRITE(CGROUP_SUBTREE, "+cpu +memory +pids"); // Corrected subsystem names + mkdir(REQ_CGROUP, S_IRUSR | S_IWUSR | S_IXUSR); // Added execute permission +} + +int main() { + cout << "parent pid: " << getpid() << endl; + makeCgroup(); + + return 0; +} \ No newline at end of file diff --git a/day7/container.cpp b/day7/container.cpp new file mode 100644 index 0000000..b448f12 --- /dev/null +++ b/day7/container.cpp @@ -0,0 +1,95 @@ +#include "parser.h" +#include +#include +#include +#include +#include +#include +#define concat(a,b) (a"" b) +#define CGROUP_MEM "/sys/fs/cgroup/cntr/memory.max" +#define CGROUP_CPU "/sys/fs/cgroup/cntr/cpu.max" +#define CGROUP_SUBTREE "/sys/fs/cgroup/cgroup.subtree_control" +#define REQ_CGROUP "/sys/fs/cgroup/cntr" +using namespace std; + + +int isOK(int status,const char* msg){ + if(status==-1){ + perror(msg); + exit(EXIT_FAILURE); + } + return status; +} + +void WRITE(const char* path,const char* value){ + int fd = open(path,O_WRONLY | O_APPEND); + if(fd == -1){ + cout<<"Error in opening\n"; + exit(1); + } + ssize_t bytes = write(fd,value,strlen(value)); + if(bytes==-1){ + cout<<"error in writing\n"; + exit(1); + } + close(fd); + +} +char* stack_mem(){ + const int stacksize = 65*1024; + auto *stack = new (nothrow) char[stacksize]; + if(stack==nullptr){ + cout<<"Can't allocate memory"; + exit(EXIT_FAILURE); + } + return stack+stacksize; +} +void cloneProcess(int (*function)(void*),int flags){ + auto pid= clone(function,stack_mem(),flags,0); + isOK(pid,"Clone Process Error"); + wait(nullptr); + +} +void setupENV(){ + clearenv(); + setenv("TERM","xterm-256color",0); + setenv("PATH","/bin/:/sbin/:/usr/sbin",0); +} +void setupChild(){ + map c=parse("container_config.ini"); + const char* root = c["custom_root"].c_str(); + const char* cpuManage = c["cpu_manage"].c_str(); + const char* memory = c["memory"].c_str(); + const char* maxProcess = c["maxProcess"].c_str(); + const char* hostname = c["host_name"].c_str(); + WRITE(CGROUP_MEM,memory); + WRITE(CGROUP_CPU,cpuManage); + WRITE(concat(REQ_CGROUP,"/pid.max"),maxProcess); + + isOK(sethostname(hostname,strlen(hostname)),"hostname error"); + setupENV(); + + + + + + +} +int childProcess(void *args){ + setupChild(); + return 0; +} +void makeCgroup(){ + WRITE(CGROUP_SUBTREE,"+cpu +memory +pids"); + mkdir(REQ_CGROUP,S_IRUSR | S_IWUSR); + const char* pid = to_string(getpid()).c_str(); + // cout< +#include +#include #include "header.h" +using namespace std; int isOK(int status,const char* msg){ @@ -10,16 +14,15 @@ int isOK(int status,const char* msg){ return status; } - void WRITE(const char* path,const char* value){ int fd = open(path,O_WRONLY | O_APPEND); if(fd == -1){ - std::cout<<"Error in opening\n"; + cout<<"Error in opening\n"; exit(1); } ssize_t bytes = write(fd,value,strlen(value)); if(bytes==-1){ - std::cout<<"error in writing\n"; + cout<<"error in writing\n"; exit(1); } close(fd); @@ -28,42 +31,36 @@ void WRITE(const char* path,const char* value){ //custom run function for the shell int run(const char* name){ - char* _args[] = {(char*) name, (char*)0}; - return execvp(name, _args); + char* args[] = {(char*)name,(char*)0}; + return execvp(name,args ); } - - char* stack_mem(){ const int stacksize = 65*1024; auto *stack = new (nothrow) char[stacksize]; if(stack==nullptr){ - std::cout<<"Can't allocate memory"; + cout<<"Can't allocate memory"; exit(EXIT_FAILURE); } return stack+stacksize; } - - void cloneProcess(int (*function)(void*),int flags){ auto pid= clone(function,stack_mem(),flags,0); isOK(pid,"Clone Process Error"); wait(nullptr); -} - +} void setupENV(){ clearenv(); setenv("TERM","xterm-256color",0); setenv("PATH","/bin/:/sbin/:/usr/sbin",0); } - void setupRoot(const char* folder){ - isOK(chroot(folder),"cant't set root: "); + isOK(chroot(folder), " cant set root:"); isOK(chdir("/"),"chdir: "); -} -void setupJail(){ +} +void setupjail(){ map c=parse("container_config.ini"); const char* root = c["custom_root"].c_str(); const char* cpuManage = c["cpu_manage"].c_str(); @@ -72,7 +69,7 @@ void setupJail(){ const char* hostname = c["host_name"].c_str(); WRITE(CGROUP_MEM,memory); WRITE(CGROUP_CPU,cpuManage); - WRITE(concat(REQ_CGROUP,"/pid.max"),maxProcess); + //WRITE(concat(REQ_CGROUP,"/pid.max"),maxProcess); isOK(sethostname(hostname,strlen(hostname)),"hostname error"); setupENV(); @@ -80,47 +77,38 @@ void setupJail(){ } - int jail(void *args){ - setupJail(); + setupjail(); - //attach the procfs to our main fail hierarchy + //attach the procfs to our main file heirarchy mount("proc","/proc","proc",0,0); - pid_t shellPid = fork(); - isOK(shellPid,"can't create fork: "); - if(shellPid == 0) - { + isOK(shellPid, "cant create fork"); + if(shellPid == 0){ run("/bin/bash"); exit(0); } - //wait till all child processes are finished - while(wait(nullptr)>0); + //wait till all chid processes + while(wait(nullptr)>0); + //zombie check out //unmount the procfs when all processes are finished umount("proc"); - return EXIT_SUCCESS; } - - void makeCgroup(){ WRITE(CGROUP_SUBTREE,"+cpu +memory +pids"); mkdir(REQ_CGROUP,S_IRUSR | S_IWUSR); const char* pid = to_string(getpid()).c_str(); - std::cout<<"Child Pid: "< +#include +#include +#include +#include +#include +#define concat(a,b) (a"" b) +#define CGROUP_MEM "/sys/fs/cgroup/cntr/memory.max" +#define CGROUP_CPU "/sys/fs/cgroup/cntr/cpu.max" +#define CGROUP_SUBTREE "/sys/fs/cgroup/cgroup.subtree_control" +#define REQ_CGROUP "/sys/fs/cgroup/cntr" \ No newline at end of file diff --git a/makefile b/day8/makefile similarity index 98% rename from makefile rename to day8/makefile index ddbf3cf..45cbe0c 100644 --- a/makefile +++ b/day8/makefile @@ -1,4 +1,3 @@ container: container.cpp rm -f container g++ container.cpp -o container - diff --git a/day8/parser.h b/day8/parser.h new file mode 100644 index 0000000..edc600f --- /dev/null +++ b/day8/parser.h @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +using namespace std; + +map parse(const string& filename){ + map config; + ifstream file(filename); + if(!file.is_open()){ + cerr<<"Error in opening of config file"< c = parse("container_config.ini"); + const char *root = c["custom_root"].c_str(); + const char *cpuManage = c["cpu_manage"].c_str(); + const char *memory = c["memory"].c_str(); + const char *maxProcess = c["maxProcess"].c_str(); + const char *hostname = c["host_name"].c_str(); + WRITE(CGROUP_MEM, memory); + WRITE(CGROUP_CPU, cpuManage); + + logMessage("Setting up jail environment"); // Log setup information + + isOK(sethostname(hostname, strlen(hostname)), "hostname error"); + setupENV(); + setupRoot("./root"); +} + +int jail(void *args) { + setupjail(); + + mount("proc", "/proc", "proc", 0, 0); + pid_t shellPid = fork(); + isOK(shellPid, "can't create fork"); + if (shellPid == 0) { + logMessage("Executing /bin/bash"); // Log command execution + run("/bin/bash"); + exit(0); + } + + // wait till all child processes + while (wait(nullptr) > 0); + + // zombie check out + // unmount the procfs when all processes are finished + umount("proc"); + logMessage("Container execution completed"); // Log container completion + return EXIT_SUCCESS; +} + +void makeCgroup() { + WRITE(CGROUP_SUBTREE, "+cpu +memory +pids"); + mkdir(REQ_CGROUP, S_IRUSR | S_IWUSR); + const char *pid = to_string(getpid()).c_str(); + WRITE(concat(REQ_CGROUP, "/cgroup.procs"), pid); +} + +int main() { + logFile.open("container_log.txt", ios::out | ios::app); // Open log file + + logMessage(("Parent Pid: " + to_string(getpid())).c_str()); + + makeCgroup(); + cloneProcess(jail, CLONE_NEWPID | CLONE_NEWUTS | SIGCHLD); + + logFile.close(); // Close log file + return EXIT_SUCCESS; +} diff --git a/mini_project/logg_container/container_config.ini b/mini_project/logg_container/container_config.ini new file mode 100644 index 0000000..b6baaba --- /dev/null +++ b/mini_project/logg_container/container_config.ini @@ -0,0 +1,6 @@ +[container] +custom_root = ./root +maxProcess = 7 +cpu_manage = 20000 100000 +memory = 2G +host_name = container \ No newline at end of file diff --git a/mini_project/logg_container/container_log.txt b/mini_project/logg_container/container_log.txt new file mode 100644 index 0000000..31f1a0d --- /dev/null +++ b/mini_project/logg_container/container_log.txt @@ -0,0 +1,4 @@ +[2023-12-24 01:52:22] Parent Pid: 14597 +[2023-12-24 01:52:22] Setting up jail environment +[2023-12-23 20:22:22] Executing /bin/bash +[2023-12-23 20:22:33] Container execution completed diff --git a/header.h b/mini_project/logg_container/header.h similarity index 79% rename from header.h rename to mini_project/logg_container/header.h index d74d7bd..887215f 100644 --- a/header.h +++ b/mini_project/logg_container/header.h @@ -1,15 +1,18 @@ #include -#include -#include -#include #include +#include +#include +#include #include +#include +#include #include -#include #include -#include -#include -#include + +#include +#include +#include +#include #define concat(a,b) (a"" b) #define CGROUP_MEM "/sys/fs/cgroup/cntr/memory.max" #define CGROUP_CPU "/sys/fs/cgroup/cntr/cpu.max" diff --git a/mini_project/logg_container/intro.txt b/mini_project/logg_container/intro.txt new file mode 100644 index 0000000..f0f3081 --- /dev/null +++ b/mini_project/logg_container/intro.txt @@ -0,0 +1,3 @@ +implementation of logging mechanisms within the container. +the image of output is provided in the folder itself , also the txt form i.e. container_log.txt +is obtained. \ No newline at end of file diff --git a/mini_project/logg_container/makefile b/mini_project/logg_container/makefile new file mode 100644 index 0000000..45cbe0c --- /dev/null +++ b/mini_project/logg_container/makefile @@ -0,0 +1,3 @@ +container: container.cpp + rm -f container + g++ container.cpp -o container diff --git a/mini_project/logg_container/output.jpg b/mini_project/logg_container/output.jpg new file mode 100644 index 0000000..d038d30 Binary files /dev/null and b/mini_project/logg_container/output.jpg differ diff --git a/mini_project/logg_container/parser.h b/mini_project/logg_container/parser.h new file mode 100644 index 0000000..edc600f --- /dev/null +++ b/mini_project/logg_container/parser.h @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +using namespace std; + +map parse(const string& filename){ + map config; + ifstream file(filename); + if(!file.is_open()){ + cerr<<"Error in opening of config file"<