├── .gitignore ├── README.md └── iszc462-np ├── ec1 ├── P1 │ ├── P1 │ ├── P1.o │ ├── makefile │ └── P1.c ├── P2 │ ├── P2 │ ├── P2.o │ ├── makefile │ └── P2.c └── P3 │ ├── P3 │ ├── P3.o │ ├── makefile │ └── P3.c └── lab ├── environ ├── pipes ├── process ├── process_creation ├── process.c ├── environ.c ├── pipes.c └── process_creation.c /.gitignore: -------------------------------------------------------------------------------- 1 | *swp 2 | *old 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | bits-msss 2 | ========= 3 | 4 | Assignments and other stuff 5 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P1/P1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P1/P1 -------------------------------------------------------------------------------- /iszc462-np/ec1/P1/P1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P1/P1.o -------------------------------------------------------------------------------- /iszc462-np/ec1/P2/P2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P2/P2 -------------------------------------------------------------------------------- /iszc462-np/ec1/P2/P2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P2/P2.o -------------------------------------------------------------------------------- /iszc462-np/ec1/P3/P3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P3/P3 -------------------------------------------------------------------------------- /iszc462-np/ec1/P3/P3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/ec1/P3/P3.o -------------------------------------------------------------------------------- /iszc462-np/lab/environ: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/lab/environ -------------------------------------------------------------------------------- /iszc462-np/lab/pipes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/lab/pipes -------------------------------------------------------------------------------- /iszc462-np/lab/process: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/lab/process -------------------------------------------------------------------------------- /iszc462-np/lab/process_creation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7h3rAm/bits-msss/master/iszc462-np/lab/process_creation -------------------------------------------------------------------------------- /iszc462-np/lab/process.c: -------------------------------------------------------------------------------- 1 | //process.c 2 | 3 | #include 4 | 5 | int main() { 6 | printf("I am running in a process whose deatils are as follows\n"); 7 | printf("process id (pid) = %d, parent process id(ppid) = %d, user id(uid) = %d\n",getpid(), getppid(), getuid()); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /iszc462-np/lab/environ.c: -------------------------------------------------------------------------------- 1 | //environ.c 2 | 3 | int main(int argc, char *argv[]) { 4 | int i; 5 | char **ptr; 6 | extern char **environ; 7 | 8 | for(i = 0; i < argc; i++) /* echo all command-line args */ { 9 | printf("argv[%d]: %s\n", i, argv[i]); 10 | } 11 | 12 | for(ptr = environ; *ptr != 0; ptr++) /* and all env strings */ { 13 | printf("i%s\n", *ptr); 14 | } 15 | 16 | exit(0); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P1/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -O3 -c -ggdb -fstack-protector-all -Wstack-protector -D_FORIFY_SOURCE=2 -z noexecstack -fpie -pie 3 | LDFALGS=-Wl,-z,relro,-z,now 4 | SOURCES=P1.c 5 | OBJECTS=$(SOURCES:.c=.o) 6 | EXECUTABLE=P1 7 | 8 | all: $(SOURCES) $(EXECUTABLE) 9 | 10 | $(EXECUTABLE): $(OBJECTS) 11 | $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 12 | 13 | .c.o: 14 | $(CC) $(CFLAGS) $< -o $@ 15 | 16 | clean: 17 | rm -rf $(OBJECTS) $(EXECUTABLE) 18 | 19 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P2/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -O3 -c -ggdb -fstack-protector-all -Wstack-protector -D_FORIFY_SOURCE=2 -z noexecstack -fpie -pie 3 | LDFALGS=-Wl,-z,relro,-z,now 4 | SOURCES=P2.c 5 | OBJECTS=$(SOURCES:.c=.o) 6 | EXECUTABLE=P2 7 | 8 | all: $(SOURCES) $(EXECUTABLE) 9 | 10 | $(EXECUTABLE): $(OBJECTS) 11 | $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 12 | 13 | .c.o: 14 | $(CC) $(CFLAGS) $< -o $@ 15 | 16 | clean: 17 | rm -rf $(OBJECTS) $(EXECUTABLE) 18 | 19 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P3/makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -O3 -c -ggdb -fstack-protector-all -Wstack-protector -D_FORIFY_SOURCE=2 -z noexecstack -fpie -pie 3 | LDFALGS=-Wl,-z,relro,-z,now 4 | SOURCES=P3.c 5 | OBJECTS=$(SOURCES:.c=.o) 6 | EXECUTABLE=P3 7 | 8 | all: $(SOURCES) $(EXECUTABLE) 9 | 10 | $(EXECUTABLE): $(OBJECTS) 11 | $(CC) $(LDFLAGS) $(OBJECTS) -o $@ 12 | 13 | .c.o: 14 | $(CC) $(CFLAGS) $< -o $@ 15 | 16 | clean: 17 | rm -rf $(OBJECTS) $(EXECUTABLE) 18 | 19 | -------------------------------------------------------------------------------- /iszc462-np/lab/pipes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int pfds[2]; 8 | 9 | pipe(pfds); 10 | 11 | if (!fork()) { 12 | close(1); /* close normal stdout */ 13 | dup(pfds[1]); /* make stdout same as pfds[1] */ 14 | close(pfds[0]); /* we don't need this */ 15 | execlp("ls", "ls", NULL); 16 | } else { 17 | close(0); /* close normal stdin */ 18 | dup(pfds[0]); /* make stdin same as pfds[0] */ 19 | close(pfds[1]); /* we don't need this */ 20 | execlp("wc", "wc", "-l", NULL); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /iszc462-np/lab/process_creation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int glob = 6; //global variable 7 | 8 | int main() { 9 | int var; 10 | pid_t pid; 11 | 12 | var = 88; 13 | printf("pid: %d, glob=%d, var=%d\n", getpid (), glob, var); 14 | printf("Before fork\n"); 15 | 16 | if ((pid = fork ()) < 0) 17 | perror ("fork"); //function to print error that occurred in the process 18 | else if (pid == 0) { 19 | glob++; 20 | var++; 21 | printf ("pid = %d, glob=%d, var=%d\n", getpid (), glob, var); 22 | exit (0); 23 | } 24 | else { 25 | printf ("pid = %d, glob=%d, var=%d\n", getpid (), glob, var); 26 | exit (0); 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P2/P2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define PFDREAD 0 /* pipe's read end file descriptor */ 10 | #define PFDWRITE 1 /* pipe's write end file descriptor */ 11 | 12 | #define FALSE 0 /* verbose boolean flags */ 13 | #define TRUE !FALSE 14 | 15 | #define MAXCMDBUFSIZE 512 /* buffer size for parsed user commands */ 16 | #define MAXINPUTBUFSIZE 1024 /* buffer size for raw user input */ 17 | 18 | 19 | pid_t pid; /* stores pid from fork */ 20 | char *commandbuf[MAXCMDBUFSIZE]; /* buffer to contain parsed commands */ 21 | char infdbuf[MAXINPUTBUFSIZE]; /* buffer to read raw piped commands */ 22 | int commandcount = 0; /* global commands counter */ 23 | 24 | 25 | void doWait(int); /* wait for spwaned childs to complete execution */ 26 | char *trimWsp(char *); /* trim leading whitespaces in a character buffer */ 27 | int doPipe(int, int, int); /* tweak in/out fds and pipe/fork/dup2/execvp */ 28 | void tokenizeCommands(char *); /* extract command and argument tokens from user input */ 29 | int doExecute(char *, int, int, int); /* wrapper over tokenizeCommands and doPipe */ 30 | 31 | 32 | int main(int argc, char *argv[]) { 33 | char *prompt = "(commands) ", *banner = "ISZC462 - Network Programming | EC1 - P2\n" 34 | "Program to demo pipes based IPC mechanism\n" 35 | "Ankur Tyagi (2012HZ13084)\n"; 36 | 37 | printf("\n%s\n", banner); 38 | while (TRUE) { 39 | printf("%s", prompt); 40 | fflush(NULL); /* force printing of program banner + prompt */ 41 | 42 | /* read user command into input buffer; exit if read fails */ 43 | if (!fgets(infdbuf, MAXINPUTBUFSIZE, stdin)) { return EXIT_FAILURE; } 44 | 45 | int infd = 0; /* input file descriptor */ 46 | int startcommand = 1; /* flag to indicate if this is the first command in pipeline */ 47 | 48 | char *command = infdbuf; /* pointer to commands pipeline */ 49 | char *nextcommand = strchr(command, '|'); /* extract the */ 50 | 51 | while (nextcommand != NULL) { /* extract all commands from the pipeline */ 52 | *nextcommand = '\0'; 53 | infd = doExecute(command, infd, startcommand, 0); /* execute each command while sharing its in out file descriptors */ 54 | command = nextcommand + 1; /* point to the next command in pipeline */ 55 | nextcommand = strchr(command, '|'); 56 | startcommand = 0; /* mark completion of first command in pipeline */ 57 | } 58 | 59 | infd = doExecute(command, infd, startcommand, 1); /* execute the last command in pipeline */ 60 | doWait(commandcount); /* wait for child processes to complete before exiting */ 61 | commandcount = 0; /* reset command counter */ 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | int doExecute(char* command, int infd, int startcommand, int last) { 68 | tokenizeCommands(command); /* extract tokens from commands pipeline */ 69 | 70 | if (commandbuf[0] != NULL) { /* if we have recieved a valid command token */ 71 | if (strcmp(commandbuf[0], "exit") == 0) { exit(EXIT_SUCCESS); } 72 | commandcount += 1; /* update command count */ 73 | return doPipe(infd, startcommand, last); /* try to execute it as a piped command */ 74 | } 75 | 76 | return 0; 77 | } 78 | 79 | void tokenizeCommands(char *command) { 80 | command = trimWsp(command); /* trim leading whitespaces from command buffer */ 81 | char *nextcommand = strchr(command, ' '); /* extract offset of next token from command buffer */ 82 | int temp = 0; 83 | 84 | while(nextcommand != NULL) { /* while we have commands in pipeline */ 85 | nextcommand[0] = '\0'; 86 | commandbuf[temp] = command; /* copy each command to commands buffer */ 87 | ++temp; 88 | 89 | command = trimWsp(nextcommand + 1); /* trim leading whitespaces for subsequent commands */ 90 | nextcommand = strchr(command, ' '); /* extract offset */ 91 | } 92 | 93 | if (command[0] != '\0') { /* check if command buffer is non-empty */ 94 | commandbuf[temp] = command; /* copy first command to command buffer */ 95 | ++temp; 96 | 97 | nextcommand = strchr(command, '\n'); /* extract offset of next '\n' delimited token */ 98 | nextcommand[0] = '\0'; /* mark string termination */ 99 | } 100 | 101 | commandbuf[temp] = NULL; 102 | } 103 | 104 | char *trimWsp(char *string) { 105 | while (isspace(*string)) { ++string; } /* skip over any whitespace characters */ 106 | return string; 107 | } 108 | 109 | int doPipe(int infd, int startcommand, int last) { 110 | int pipesfd[2]; /* pipe file descriptors array */ 111 | 112 | pipe(pipesfd); /* create a new unnamed pipe */ 113 | pid = fork(); /* fork the current process */ 114 | 115 | if (pid == 0) { /* we're in child process */ 116 | if (startcommand == 1 && infd == 0 && last == 0) { dup2(pipesfd[PFDWRITE], STDOUT_FILENO); } /* this is the first command, infd should be 0 */ 117 | else if (startcommand == 0 && infd != 0 && last == 0) { /* this is the last command, infd should be nonzero */ 118 | dup2(infd, STDIN_FILENO); 119 | dup2(pipesfd[PFDWRITE], STDOUT_FILENO); 120 | } else { dup2(infd, STDIN_FILENO); } /* this is some command from within the pipeline */ 121 | 122 | if (execvp(commandbuf[0], commandbuf) == -1) { exit(EXIT_FAILURE); } /* pipe is ready, let's execvp the requested command */ 123 | } 124 | 125 | if (infd != 0) { close(infd); } /* except for first command, make sure you close the infd */ 126 | /* this is required so that the next command in pipeline can open it for reading */ 127 | 128 | close(pipesfd[PFDWRITE]); /* ditto for the outfd */ 129 | 130 | if (last == 1) { close(pipesfd[PFDREAD]); } /* for the last command close the infd of pipe (outfd is stdout and as such remains open) */ 131 | 132 | return pipesfd[PFDREAD]; 133 | } 134 | 135 | void doWait(int commandcount) { 136 | int temp; 137 | for (temp=0; temp 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define FALSE 0 /* verbose boolean flags */ 12 | #define TRUE !FALSE 13 | #define DEBUG !TRUE 14 | 15 | #define MAXINPUTBUFSIZE 1024 /* buffer size for raw user input */ 16 | 17 | 18 | pid_t pid1, pid2, pid3; 19 | pid_t pid4, pid5, pid6, pid7; /* stores pid from fork */ 20 | int buflen; /* length of input string */ 21 | char infdbuf[MAXINPUTBUFSIZE]; /* buffer to read raw string input */ 22 | 23 | key_t key; /* stores a key for msgsnd/rcv (uid in this example) */ 24 | int mask, msgid; /* store message queue file permissions mask and identifier */ 25 | 26 | struct msgbuf { /* message buffer for IPC */ 27 | long msgtype; 28 | char message[MAXINPUTBUFSIZE]; 29 | }; 30 | struct msgbuf mbuf; 31 | 32 | 33 | int doP3(char []); /* function that forks child processes and facilitates IPC ia message queues */ 34 | 35 | 36 | int main(int argc, char *argv[]) { 37 | char *prompt = "(commands) ", *banner = "ISZC462 - Network Programming | EC1 - P3\n" 38 | "Program to demo message queue based IPC mechanism\n" 39 | "Ankur Tyagi (2012HZ13084)\n"; 40 | 41 | printf("\n%s\n", banner); 42 | while (TRUE) { 43 | printf("%s", prompt); 44 | fflush(NULL); /* force printing of program banner + prompt */ 45 | 46 | /* read user command into input buffer; exit if read fails */ 47 | if (!fgets(infdbuf, MAXINPUTBUFSIZE, stdin)) { return EXIT_FAILURE; } 48 | 49 | buflen = strlen(infdbuf); /* get length of the input string */ 50 | 51 | if (!strcmp(infdbuf, "") || !strcmp(infdbuf, "\n") || !strcmp(infdbuf, " ") || buflen <= 0) { 52 | continue; /* loop untill we receive some input */ 53 | } else { 54 | if (infdbuf[buflen-1] == '\n') { /* remove newline form received string */ 55 | infdbuf[buflen-1] = '\0'; 56 | } 57 | if (!strcmp(infdbuf, "exit")) { /* exit if requested */ 58 | printf("c1: received exit!\n"); 59 | printf("\n"); 60 | exit(EXIT_SUCCESS); 61 | } else { 62 | doP3(infdbuf); /* fork/communicate with child processes */ 63 | 64 | int temp; 65 | for(temp=0; temp<6; ++temp) { wait(NULL); } /* cheap trick to wait untill all child processes complete execution */ 66 | 67 | return EXIT_SUCCESS; 68 | } 69 | } 70 | 71 | } 72 | 73 | return EXIT_SUCCESS; 74 | } 75 | 76 | int doP3(char infdbuf[]) { 77 | printf("\n"); 78 | 79 | pid1 = getpid(); /* get pid of parent process, c1 */ 80 | printf("c1 (#%d): received message \'%s\'\n", pid1, infdbuf); 81 | char *str = infdbuf; 82 | for(; *str; ++str) { *str = tolower(*str); } /* perform transform */ 83 | printf("c1 (#%d): transform: message -> tolower -> \'%s\'\n", pid1, infdbuf); 84 | 85 | key = getuid(); /* instantiate key with current uid */ 86 | mask = 0666; /* set octal mask for message queue */ 87 | 88 | mbuf.msgtype = 1; /* message type identifier to be shared with child processes */ 89 | strcpy(mbuf.message, infdbuf); 90 | size_t msize = strlen(mbuf.message); 91 | 92 | msgid = msgget(key, mask | IPC_CREAT); /* create a new message queue with key and mask */ 93 | if (DEBUG) { printf("c1 (#%d): key: %d | mask: %o | msgid = %d | msgtype: %ld | msize: %d\n", pid1, key, mask, msgid, mbuf.msgtype, msize); } 94 | 95 | if (msgid != -1) { 96 | if (DEBUG) { printf("c1 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid1, mbuf.message, msize, msgid); } 97 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); /* add c1 transformed string queue */ 98 | if (rv == -1) { 99 | if (DEBUG) { printf("c1 (#%d): msgsnd failed!\n", pid1); } 100 | perror("msgsnd"); 101 | } else { 102 | if (DEBUG) { printf("c1 (#%d): added message to message queue: %d\n", pid1, msgid); } 103 | } 104 | } 105 | 106 | if (DEBUG) { printf("c1 (#%d): spawning child c2 ...\n", pid1); } 107 | 108 | if ((pid2 = fork()) == -1) { 109 | printf("c1 (#%d): forking c2 failed!\n", pid1); 110 | exit(EXIT_FAILURE); 111 | 112 | } else if (pid2 == 0) { /* first child of c1 -> c2 */ 113 | pid2 = getpid(); 114 | if (DEBUG) { printf("c2 (#%d): am alive!\n", pid2); } 115 | 116 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 117 | if (DEBUG) { printf("c2 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid2, mbuf.message, strlen(mbuf.message), msgid); } 118 | strcat(mbuf.message, "C2"); 119 | printf("c2 (#%d): transform: message -> append \'C2\' -> \'%s\'\n", pid2, mbuf.message); 120 | if (DEBUG) { printf("c2 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid2, mbuf.message, msize, msgid); } 121 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 122 | if (rv == -1) { 123 | printf("c2 (#%d): msgsnd failed!\n", pid2); 124 | perror("msgsnd"); 125 | } else { 126 | if (DEBUG) { printf("c2 (#%d): added message to message queue: %d\n", pid2, msgid); } 127 | } 128 | 129 | if (DEBUG) { printf("c2 (#%d): spawning child c3 ...\n", pid2); } 130 | 131 | if ((pid3 = fork()) == -1) { 132 | printf("c2 (#%d): forking c3 failed!\n", pid2); 133 | exit(EXIT_FAILURE); 134 | 135 | } else if (pid3 == 0) { /* first child of c2 -> c3 */ 136 | pid3 = getpid(); 137 | if (DEBUG) { printf("c3 (#%d): am alive!\n", pid3); } 138 | 139 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 140 | if (DEBUG) { printf("c3 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid3, mbuf.message, strlen(mbuf.message), msgid); } 141 | char temp[MAXINPUTBUFSIZE]; 142 | strcpy(temp, mbuf.message); 143 | strcpy(mbuf.message, "C3"); 144 | strcat(mbuf.message, temp); 145 | printf("c3 (#%d): transform: message -> prepend \'C3\' -> \'%s\'\n", pid3, mbuf.message); 146 | if (DEBUG) { printf("c3 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid3, mbuf.message, msize, msgid); } 147 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 148 | if (rv == -1) { 149 | printf("c3 (#%d): msgsnd failed!\n", pid3); 150 | perror("msgsnd"); 151 | } else { 152 | if (DEBUG) { printf("c3 (#%d): added message to message queue: %d\n", pid3, msgid); } 153 | } 154 | 155 | if (DEBUG) { printf("c3 (#%d): spawning child c5 ...\n", pid3); } 156 | 157 | if ((pid5 = fork()) == -1) { 158 | printf("c3 (#%d): forking c5 failed!\n", pid3); 159 | exit(EXIT_FAILURE); 160 | 161 | } else if (pid5 == 0) { /* first child of c3 -> c5 */ 162 | pid5 = getpid(); 163 | if (DEBUG) { printf("c5 (#%d): am alive!\n", pid5); } 164 | 165 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 166 | if (DEBUG) { printf("c5 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid5, mbuf.message, strlen(mbuf.message), msgid); } 167 | 168 | int count = 0; 169 | char *str = mbuf.message; 170 | for(; *str; ++str) { 171 | ++count; 172 | if (count == 5) { *str = toupper(*str); } 173 | } 174 | 175 | printf("c5 (#%d): transform: message -> 5th char toupper -> \'%s\'\n", pid5, mbuf.message); 176 | if (DEBUG) { printf("c5 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid5, mbuf.message, msize, msgid); } 177 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 178 | if (rv == -1) { 179 | printf("c5 (#%d): msgsnd failed!\n", pid5); 180 | perror("msgsnd"); 181 | } else { 182 | if (DEBUG) { printf("c5 (#%d): added message to message queue: %d\n", pid5, msgid); } 183 | } 184 | 185 | if (DEBUG) { printf("c5 (#%d): m done!\n", pid5); } 186 | return EXIT_SUCCESS; 187 | 188 | } else { /* in c3 */ 189 | if (DEBUG) { printf("c3 (#%d): spawning child c6 ...\n", pid3); } 190 | 191 | if ((pid6 = fork()) == -1) { 192 | printf("c3 (#%d): forking c6 failed!\n", pid3); 193 | printf("\n"); 194 | exit(EXIT_FAILURE); 195 | 196 | } else if (pid6 == 0) { /* second child of c3 -> c6 */ 197 | pid6 = getpid(); 198 | if (DEBUG) { printf("c6 (#%d): am alive!\n", pid6); } 199 | 200 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 201 | if (DEBUG) { printf("c6 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid6, mbuf.message, strlen(mbuf.message), msgid); } 202 | 203 | int count = 0; 204 | char *str = mbuf.message; 205 | for(; *str; ++str) { 206 | ++count; 207 | if (count == 6) { *str = toupper(*str); } 208 | } 209 | 210 | printf("c6 (#%d): transform: message -> 6th char toupper -> \'%s\'\n", pid6, mbuf.message); 211 | if (DEBUG) { printf("c6 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid6, mbuf.message, msize, msgid); } 212 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 213 | if (rv == -1) { 214 | printf("c6 (#%d): msgsnd failed!\n", pid6); 215 | perror("msgsnd"); 216 | } else { 217 | if (DEBUG) { printf("c6 (#%d): added message to message queue: %d\n", pid6, msgid); } 218 | } 219 | 220 | if (DEBUG) { printf("c6 (#%d): m done!\n", pid6); } 221 | return EXIT_SUCCESS; 222 | 223 | } else { /* in c3 */ 224 | if (DEBUG) { printf("c3 (#%d): spawning child c7 ...\n", pid3); } 225 | 226 | if ((pid7 = fork()) == -1) { 227 | printf("c3 (#%d): forking c7 failed!\n", pid3); 228 | printf("\n"); 229 | exit(EXIT_FAILURE); 230 | 231 | } else if (pid7 == 0) { /* third child of c3 -> c7 */ 232 | pid7 = getpid(); 233 | if (DEBUG) { printf("c7 (#%d): am alive!\n", pid7); } 234 | 235 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 236 | if (DEBUG) { printf("c7 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid7, mbuf.message, strlen(mbuf.message), msgid); } 237 | 238 | int count = 0; 239 | char *str = mbuf.message; 240 | for(; *str; ++str) { 241 | ++count; 242 | if (count == 7) { *str = toupper(*str); } 243 | } 244 | 245 | printf("c7 (#%d): transform: message -> 7th char toupper -> \'%s\'\n", pid7, mbuf.message); 246 | if (DEBUG) { printf("c7 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid7, mbuf.message, msize, msgid); } 247 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 248 | if (rv == -1) { 249 | printf("c7 (#%d): msgsnd failed!\n", pid7); 250 | perror("msgsnd"); 251 | } else { 252 | if (DEBUG) { printf("c7 (#%d): added message to message queue: %d\n", pid7, msgid); } 253 | } 254 | 255 | if (DEBUG) { printf("c7 (#%d): m done!\n", pid7); } 256 | return EXIT_SUCCESS; 257 | 258 | } else { /* in c3 */ 259 | if (DEBUG) { printf("c3 (#%d): waiting for child c7 to complete ...\n", pid3); } 260 | //wait(NULL); 261 | waitpid(pid7, NULL, NULL); 262 | if (DEBUG) { printf("c3 (#%d): child c7 done!\n", pid3); } 263 | return EXIT_SUCCESS; 264 | } 265 | 266 | if (DEBUG) { printf("c3 (#%d): waiting for child c6 to complete ...\n", pid3); } 267 | //wait(NULL); 268 | waitpid(pid6, NULL, NULL); 269 | if (DEBUG) { printf("c3 (#%d): child c6 done!\n", pid3); } 270 | return EXIT_SUCCESS; 271 | } 272 | 273 | if (DEBUG) { printf("c3 (#%d): waiting for child c5 to complete ...\n", pid3); } 274 | //wait(NULL); 275 | waitpid(pid5, NULL, NULL); 276 | if (DEBUG) { printf("c3 (#%d): child c5 done!\n", pid3); } 277 | return EXIT_SUCCESS; 278 | } 279 | 280 | if (DEBUG) { printf("c3 (#%d): m done!\n", pid3); } 281 | return EXIT_SUCCESS; 282 | 283 | } else { /* in c2 */ 284 | if (DEBUG) { printf("c2 (#%d): spawning child c4 ...\n", pid2); } 285 | 286 | if ((pid4 = fork()) == -1) { 287 | printf("c2 (#%d): forking c4 failed!\n", pid2); 288 | printf("\n"); 289 | exit(EXIT_FAILURE); 290 | 291 | } else if (pid4 == 0) { /* second child of c2 -> c4 */ 292 | pid4 = getpid(); 293 | if (DEBUG) { printf("c4 (#%d): am alive!\n", pid4); } 294 | 295 | msgrcv(msgid, &mbuf, sizeof(mbuf) - sizeof(long), mbuf.msgtype, 0); 296 | if (DEBUG) { printf("c4 (#%d): read message \'%s\' of len %dB from message queue: %d\n", pid4, mbuf.message, strlen(mbuf.message), msgid); } 297 | 298 | int count = 0; 299 | char *str = mbuf.message; 300 | for(; *str; ++str) { 301 | ++count; 302 | if (count == 4) { *str = toupper(*str); } 303 | } 304 | 305 | printf("c4 (#%d): transform: message -> 4th char toupper -> \'%s\'\n", pid4, mbuf.message); 306 | if (DEBUG) { printf("c4 (#%d): adding message \'%s\' of len %dB to message queue: %d\n", pid4, mbuf.message, msize, msgid); } 307 | int rv = msgsnd(msgid, &mbuf, sizeof(mbuf) - sizeof(long), 0); 308 | if (rv == -1) { 309 | printf("c4 (#%d): msgsnd failed!\n", pid4); 310 | perror("msgsnd"); 311 | } else { 312 | if (DEBUG) { printf("c4 (#%d): added message to message queue: %d\n", pid4, msgid); } 313 | } 314 | 315 | if (DEBUG) { printf("c4 (#%d): m done!\n", pid4); } 316 | return EXIT_SUCCESS; 317 | 318 | } else { /* in c2 */ 319 | if (DEBUG) { printf("c2 (#%d): waiting for child c4 to complete ...\n", pid2); } 320 | //wait(NULL); 321 | waitpid(pid4, NULL, NULL); 322 | if (DEBUG) { printf("c2 (#%d): child c4 done!\n", pid2); } 323 | return EXIT_SUCCESS; 324 | } 325 | 326 | if (DEBUG) { printf("c2 (#%d): waiting for child c3 to complete ...\n", pid2); } 327 | //wait(NULL); 328 | waitpid(pid3, NULL, NULL); 329 | if (DEBUG) { printf("c2 (#%d): child c3 done!\n", pid2); } 330 | return EXIT_SUCCESS; 331 | } 332 | 333 | } else { /* in c1 */ 334 | if (DEBUG) { printf("c1 (#%d): waiting for child c2 to complete ...\n", pid1); } 335 | //wait(NULL); 336 | waitpid(pid2, NULL, NULL); 337 | if (DEBUG) { printf("c1 (#%d): child c2 done!\n", pid1); } 338 | } 339 | 340 | if (DEBUG) { printf("c1 (#%d): removing message queue: %d\n", pid1, msgid); } 341 | int rv = msgctl(msgid, IPC_RMID, NULL); 342 | if (rv == -1) { 343 | printf("c1 (#%d): msgctl - IPC_RMID failed!\n", pid1); 344 | perror("msgctl"); 345 | } 346 | 347 | printf("\n"); 348 | return EXIT_SUCCESS; 349 | } 350 | 351 | -------------------------------------------------------------------------------- /iszc462-np/ec1/P1/P1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAXHOSTSIZE 512 /* buffer size to hold host name */ 10 | #define MAXCMDSIZE 512 /* buffer size to hold command and its arguments */ 11 | #define MAXCWDSIZE 512 /* buffer size to hold current workign directory */ 12 | 13 | #define TRUE 1 14 | #define FALSE !TRUE 15 | #define DEBUG !TRUE /* flag to toggle debugging output */ 16 | 17 | extern char **environ; /* extern pointer to environment variables */ 18 | 19 | typedef struct { /* struct to hold command info */ 20 | pid_t pid; /* pid of the command */ 21 | char *cliargs[MAXCMDSIZE]; /* arguments list ([0] is the command itself) */ 22 | unsigned int background; /* flag to indicate if the command is to be executed in background */ 23 | unsigned int argscount; /* count of arguments (analogous to the argc argument to main) */ 24 | } commandInfo; 25 | 26 | typedef struct job { /* struct to hold background job info */ 27 | pid_t pid; /* pid of the job */ 28 | unsigned int id; /* id assigned to a running job (incremental; starts from 1) */ 29 | char command[MAXCMDSIZE]; /* arguments list for this job */ 30 | struct job *next; /* pointer to the next job in the list */ 31 | } jobInfo; 32 | 33 | static jobInfo *jobslist = NULL; /* global linked list of all running background jobs */ 34 | static unsigned int jobscount = 0; /* global static counter for currently active number of jobs */ 35 | 36 | void doExit(int); /* builtin "exit" command handler */ 37 | int doInfo(void); /* builtin "info" command handler */ 38 | int doJobs(void); /* builtin "jobs" command handler */ 39 | int doCd(commandInfo *); /* builtin "cd" command handler */ 40 | int readCommand(char *); /* read a command from stdin and null terminate it */ 41 | int parseCommand(char *, commandInfo *); /* parse command and populate command info struct */ 42 | int showCommand(commandInfo *); /* show parsed command and its args list (only if DEBUG is on) */ 43 | int checkCommand(commandInfo *cinfo); /* check if the parsed command is available or not */ 44 | pid_t doExecvp(commandInfo *); /* fork and execvp parsed command (populate jobs struct if reqd.) */ 45 | void handleInterrupt(int); /* interrupt handler for registered signals */ 46 | 47 | int main(int cliargsc, char **cliargsv) { 48 | int rc = -1; /* signed int to hold recturn code from syscalls/functions */ 49 | char *banner = "Yet Another Unix Shell - v0.1\n" /* banner string */ 50 | "ISZC426 - Network Programming | EC1 - P1\n" 51 | "Ankur Tyagi - 2012HZ13084 | 27/AUG/2013\n"; 52 | char *user = NULL; /* poniter -> char that holds username */ 53 | char hostname[MAXHOSTSIZE] = "\0"; /* buffer to hold hostname */ 54 | char cwd[MAXCWDSIZE] = "\0"; /* buffer to hold current working directory */ 55 | char *prompt = NULL; /* pointer -> char that hold the prompt string */ 56 | 57 | printf("\n%s\n", banner); /* start with a banner */ 58 | while (TRUE) { /* command parsing/execution loop */ 59 | char cmdline[MAXCMDSIZE]; /* buffer to hold commandline */ 60 | commandInfo cinfo; /* struct to hold command info */ 61 | memset(&cinfo, '\0', sizeof(cinfo)); /* zero out the buffer */ 62 | 63 | if ((user = getenv("LOGNAME")) == NULL) { user = "user"; } /* read username of currently logged in user (default: "user") */ 64 | if (gethostname(hostname, sizeof(hostname))) { strcpy(hostname, "localhost"); } /* read hostname (default: "localhost") */ 65 | if (getcwd(cwd, sizeof(cwd)) == NULL) { strcpy(cwd, "\0"); } /* read current working directory (default: null) */ 66 | printf("[%s] at [%s] in [%s] ", user, hostname, cwd); /* show an informative prompt made up of username, hostname and cwd */ 67 | 68 | readCommand(cmdline); /* read a command from stdin */ 69 | if (!strcmp(cmdline, "") || !strcmp(cmdline, "\n") || !strcmp(cmdline, " ")) { /* continue if only a newline or space was received */ 70 | continue; 71 | } 72 | 73 | if (parseCommand(cmdline, &cinfo) == -1) { /* parse commandline and populate the command info struct */ 74 | return EXIT_FAILURE; 75 | } else { 76 | parseCommand(cmdline, &cinfo); /* if no errors were reported, continue parsing command */ 77 | } 78 | 79 | if (DEBUG) { showCommand(&cinfo); } /* if DEBUG is on, show parsed command */ 80 | 81 | signal(SIGABRT, handleInterrupt); /* register signals with a custom handler */ 82 | signal(SIGHUP, handleInterrupt); 83 | signal(SIGILL, handleInterrupt); 84 | signal(SIGINT, handleInterrupt); 85 | signal(SIGKILL, handleInterrupt); 86 | signal(SIGQUIT, handleInterrupt); 87 | signal(SIGSEGV, handleInterrupt); 88 | signal(SIGTERM, handleInterrupt); 89 | signal(SIGSTOP, handleInterrupt); 90 | signal(SIGTSTP, handleInterrupt); 91 | signal(SIGCHLD, handleInterrupt); 92 | 93 | if (!strcmp(cinfo.cliargs[0], "exit")) { /* call builtin hanlder for exit */ 94 | if (DEBUG) { printf("[D] main: calling exit handler: doExit\n"); } 95 | doExit(EXIT_SUCCESS); 96 | } else if (!strcmp(cinfo.cliargs[0], "info")) { /* call builtin handler for info and show return status */ 97 | if (DEBUG) { printf("[D] main: calling info handler: doInfo\n"); } 98 | rc = doInfo(); 99 | if (DEBUG) { printf("[D] main: returned from info handler: doInfo (RC: %d)\n", rc); } 100 | } else if (!strcmp(cinfo.cliargs[0], "jobs")) { /* call builtin handler for jobs and show return status */ 101 | if (DEBUG) { printf("[D] main: calling jobs handler: doJobs\n"); } 102 | rc = doJobs(); 103 | if (DEBUG) { printf("[D] main: returned from jobs handler: doJobs (RC: %d)\n", rc); } 104 | } else if (!strcmp(cinfo.cliargs[0], "cd")) { /* call builtin handler for cd and show return status */ 105 | if (DEBUG) { printf("[D] main: calling cd handler: doCd\n"); } 106 | rc = doCd(&cinfo); 107 | if (DEBUG) { printf("[D] main: returned from cd handler: doCd (RC: %d)\n", rc); } 108 | } else { /* if its a non builtin command */ 109 | if (!checkCommand(&cinfo)) { /* check if its a valid command (not a shell builtin) */ 110 | pid_t pid = doExecvp(&cinfo);/* fork/execvp this command */ 111 | 112 | if (cinfo.background == FALSE) { /* if command is not to be executed in background */ 113 | pid_t done_pid = waitpid(pid, NULL, 0); /* wait for the child to complete execution */ 114 | if (done_pid != -1 && done_pid != 0) { /* when waitpid returns successfully */ 115 | printf("\n"); /* evaluate and print exit status of child */ 116 | printf("[+] main: process %d has changed state (execution complete)\n", done_pid); 117 | } 118 | } else if (cinfo.background == TRUE) { /* if comamnd is to be executed in background */ 119 | pid_t done_pid = waitpid(pid, NULL, WNOHANG); /* nonblocking wait for the child to complete execution */ 120 | if (done_pid != -1 && done_pid != 0) { /* when waitpid return successfully */ 121 | printf("\n"); /* evaluate and print exit status of child */ 122 | printf("[+] main: background process %d has changed state (execution complete)\n", done_pid); 123 | } 124 | } 125 | } 126 | } 127 | } 128 | 129 | return 0; 130 | } 131 | 132 | void doExit(int rc) { /* builtin "exit" command handler */ 133 | printf("\n"); 134 | 135 | if (jobscount == 0) { /* if there are no background jobs */ 136 | exit(rc); /* exit cleanly */ 137 | } else { /* else wait for background jobs to complete */ 138 | printf("[+] doExit: waiting for %d jobs to complete", jobscount); 139 | doJobs(); /* show a listing of currently active background jobs */ 140 | } 141 | } 142 | 143 | int doInfo(void) { /* builtin "info" command handler */ 144 | pid_t pid = getpid(); /* read pid of shell */ 145 | pid_t ppid = getppid(); /* read pid of parent */ 146 | uid_t ruid = getuid(); /* get user id of currently loggedin user */ 147 | uid_t euid = geteuid(); /* get effective user id of currently loggedin user */ 148 | char *cwd = getcwd(NULL, 0); /* get current working directory */ 149 | 150 | printf("\n"); 151 | printf("--------+--------+--------+--------+-----\n"); 152 | printf(" PID | PPID | RUID | EUID | CWD \n"); 153 | printf("--------+--------+--------+--------+-----\n"); 154 | printf(" %-6d | %-6d | %-6d | %-6d | %-s\n", pid, ppid, ruid, euid, cwd); /* show a tabular listing of above details */ 155 | printf("\n"); 156 | 157 | return 0; 158 | } 159 | 160 | int doJobs(void) { /* builtin "jobs" command handler */ 161 | printf("\n"); 162 | 163 | jobInfo *job = jobslist; /* pointer to the start of the global jobs linked list */ 164 | 165 | printf("--------+--------+---------\n"); 166 | printf(" ID# | PID | Command \n"); 167 | printf("--------+--------+---------\n"); 168 | while(job != NULL) { /* loop over the jobs linked list */ 169 | printf(" %-6d | %-6d | %s\n", job->id, job->pid, job->command); /* print job info */ 170 | job = job->next; 171 | } 172 | 173 | printf("\n"); 174 | printf("Total: %d\n", jobscount); /* show count of currently active jobs */ 175 | printf("\n"); 176 | 177 | return 0; 178 | } 179 | 180 | int doCd(commandInfo *cinfo) { /* builtin "cd" handler */ 181 | if (cinfo->argscount == 1) { /* if no argument was passed to cd */ 182 | chdir(getenv("HOME")); /* change cwd to loggedin user's HOME */ 183 | return EXIT_SUCCESS; 184 | } else if (chdir(cinfo->cliargs[1]) == -1) { /* else change cwd to requested directory */ 185 | perror("[-] doCd"); /* if requested directory is not available show error message */ 186 | return EXIT_FAILURE; 187 | } 188 | 189 | return EXIT_SUCCESS; 190 | } 191 | 192 | int readCommand(char *str) { /* reads user command from stdin */ 193 | fgets(str, MAXCMDSIZE, stdin); 194 | if (strlen(str) >= 2) { /* if command has printable characters */ 195 | str[strlen(str)-1] = '\0'; /* add null terminater after last character */ 196 | } else { /* else null terminate the original string */ 197 | str = '\0'; 198 | } 199 | 200 | return EXIT_SUCCESS; 201 | } 202 | 203 | int parseCommand(char *cli, commandInfo *cinfo) { /* parse command and populate command info struct */ 204 | char *cliargs; 205 | char *cliargss[MAXCMDSIZE]; 206 | 207 | int argscount = 0; 208 | cliargs = strtok(cli, " "); /* read command tokens */ 209 | while (cliargs != NULL) { /* populate command info struct */ 210 | cliargss[argscount] = cliargs; 211 | cliargs = strtok(NULL, " "); 212 | argscount++; /* update arguments count */ 213 | } 214 | 215 | if (argscount == 0) { return EXIT_FAILURE; } /* if argscount is 0, return */ 216 | 217 | int i, icliargs = 0; 218 | for (i = 0; i < argscount; i++) { /* populate command arguments */ 219 | cinfo->cliargs[icliargs++] = cliargss[i]; 220 | } 221 | 222 | if (!strcmp(cinfo->cliargs[argscount-1], "&")) { /* if command has to be requested in background */ 223 | cinfo->cliargs[argscount-1] = NULL; /* remove the '&' character */ 224 | cinfo->background = TRUE; /* enable flag indicating this job is to be executed in background */ 225 | } 226 | 227 | if (cinfo->argscount == 0) { cinfo->argscount = argscount; } /* updae arguments count */ 228 | 229 | return EXIT_SUCCESS; 230 | } 231 | 232 | int showCommand(commandInfo *cinfo) { /* show parsed command and its arguments */ 233 | int i; 234 | 235 | for (i = 0; cinfo->cliargs[i] != NULL; i++) { /* loop over command arguments */ 236 | printf("[+] showCommands: cliargs[%d]=\"%s\"\n", i, cinfo->cliargs[i]); /* show arguments */ 237 | } 238 | 239 | printf("[+] showCommands: background: %d\n", cinfo->background); /* show command's background status */ 240 | printf("[+] showCommands: argscount: %d\n", cinfo->argscount); /* show count of command arguments */ 241 | 242 | return EXIT_SUCCESS; 243 | } 244 | 245 | int checkCommand(commandInfo *cinfo) { /* check if the command is available or not */ 246 | return EXIT_SUCCESS; 247 | } 248 | 249 | pid_t doExecvp(commandInfo *cinfo) { /* fork and execvp the command */ 250 | pid_t pid = fork(); /* fork the parent */ 251 | 252 | if (cinfo->background == TRUE) { /* if this is a background command */ 253 | jobInfo *job = malloc(sizeof(jobInfo));/* create a new node to add to the global jobs list */ 254 | job->pid = pid; /* update job pid */ 255 | job->id = ++jobscount; /* update job id */ 256 | strcpy(job->command, cinfo->cliargs[0]); 257 | job->next = NULL; /* sanitize the next pointer */ 258 | 259 | if (jobslist == NULL) { /* if this is teh first background job */ 260 | jobslist = job; /* point global job pointer to this node */ 261 | } else { 262 | jobInfo *temp = jobslist; /* else create a temporary node (to point to the current last node) */ 263 | while(jobslist != NULL) { /* loop over the jobs linked list */ 264 | temp = temp->next; 265 | } 266 | temp->next = job; /* update the next pointer of last node to the new node */ 267 | } 268 | } 269 | 270 | if (pid < 0) { /* if fork returned a negative return id */ 271 | perror("[!] doExecvp: fork failed!");/* show error message and exit */ 272 | return EXIT_FAILURE; 273 | } else if (pid == 0) { /* if fork passed and we're in parent process */ 274 | execvp(cinfo->cliargs[0], cinfo->cliargs); /* overwrite the child with requested command */ 275 | perror("[-] doExecvp"); /* if execvp failed, show error and exit */ 276 | exit(EXIT_FAILURE); 277 | } else { /* if fork passed and we're in child process */ 278 | printf("[+] doExecvp: child process (pid: %d)\n", pid); /* show child pid and continue executing the child */ 279 | printf("\n"); 280 | } 281 | 282 | return pid; /* return fork pid */ 283 | } 284 | 285 | void handleInterrupt(int sig) { /* interrup handlet for registered signals */ 286 | int status; 287 | pid_t pid; 288 | 289 | switch(sig) { /* select the caught signal */ 290 | case SIGCHLD: /* if it is SIGCHLD */ 291 | pid = wait(&status); /* wait for the child to complete execution */ 292 | if (pid > 0) { /* when child returns */ 293 | jobInfo *prev = NULL, *temp = jobslist; /* prep to delete the returned child from jobs linked list */ 294 | while(temp != NULL) { /* loop over the linked list of background jobs */ 295 | if (pid == temp->pid) { /* if the returned child is found in the jobs linked list */ 296 | if (prev == NULL) { /* if the child node is the first node in the list */ 297 | jobslist = temp->next; /* point the global jobslist pointer to child's next */ 298 | } else if (temp->next == NULL) { /* if the child node is teh last node in the list */ 299 | prev->next = NULL;/* point child's prev node to NULL */ 300 | } else { /* if the child node is neither first nor last */ 301 | prev->next = temp->next; /* update child's prev to point to child's next */ 302 | } 303 | --jobscount; /* update global jobs count */ 304 | free(temp); /* free up the child node and return */ 305 | 306 | printf("\n"); /* evaluate and print exit status of child */ 307 | printf("[+] main: background process %d has changed state (execution complete)\n", pid); 308 | 309 | return; 310 | } 311 | prev = temp; /* if child node is not yet found, keep traversing the list */ 312 | temp = temp->next; 313 | } 314 | } 315 | break; 316 | 317 | case SIGABRT: /* call builtin "exit" handler */ 318 | printf("\n[+] handleInterrupt: recieved SIGABRT! exiting.\n"); 319 | doExit(EXIT_SUCCESS); 320 | break; 321 | 322 | case SIGHUP: /* call builtin "exit" handler */ 323 | printf("\n[+] handleInterrupt: recieved SIGHUP! exiting.\n"); 324 | doExit(EXIT_SUCCESS); 325 | break; 326 | 327 | case SIGILL: /* call builtin "exit" handler */ 328 | printf("\n[+] handleInterrupt: recieved SIGILL! exiting.\n"); 329 | doExit(EXIT_SUCCESS); 330 | break; 331 | 332 | case SIGINT: /* call builtin "exit" handler */ 333 | printf("\n[+] handleInterrupt: recieved SIGINT! exiting.\n"); 334 | doExit(EXIT_SUCCESS); 335 | break; 336 | 337 | case SIGKILL: /* call builtin "exit" handler */ 338 | printf("\n[+] handleInterrupt: recieved SIGKILL! exiting.\n"); 339 | doExit(EXIT_SUCCESS); 340 | break; 341 | 342 | case SIGQUIT: /* call builtin "exit" handler */ 343 | printf("\n[+] handleInterrupt: recieved SIGQUIT! exiting.\n"); 344 | doExit(EXIT_SUCCESS); 345 | break; 346 | 347 | case SIGSEGV: /* call builtin "exit" handler */ 348 | printf("\n[+] handleInterrupt: recieved SIGSEGV! exiting.\n"); 349 | doExit(EXIT_SUCCESS); 350 | break; 351 | 352 | case SIGTERM: /* call builtin "exit" handler */ 353 | printf("\n[+] handleInterrupt: recieved SIGTERM! exiting.\n"); 354 | doExit(EXIT_SUCCESS); 355 | break; 356 | 357 | case SIGSTOP: /* call builtin "exit" handler */ 358 | printf("\n[+] handleInterrupt: recieved SIGSTOP! exiting.\n"); 359 | doExit(EXIT_SUCCESS); 360 | break; 361 | 362 | case SIGTSTP: /* call builtin "exit" handler */ 363 | printf("\n[+] handleInterrupt: recieved SIGTSTP! exiting.\n"); 364 | doExit(EXIT_SUCCESS); 365 | break; 366 | 367 | default: 368 | printf("[+] handleInterrupt: handling default ..."); 369 | break; 370 | } 371 | } 372 | --------------------------------------------------------------------------------