1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
void eval(char *cmdline) { sigset_t mask, mask_prev, mask_one; char *argv[MAXARGS]; char buf[MAXLINE]; int bg; pid_t pid; strcpy(buf, cmdline); bg = parseline(buf, argv) ? BG : FG; if(argv[0]==NULL) return ; sigemptyset(&mask_one); sigaddset(&mask_one, SIGCHLD); sigfillset(&mask); if(!builtin_cmd(argv)) { sigprocmask(SIG_BLOCK, &mask_one, &mask_prev); if((pid=fork())==0) { sigprocmask(SIG_SETMASK, &mask_prev, NULL); setpgid(0, 0); if(execve(argv[0], argv, environ)<0) printf("%s: Command not found.\n", argv[0]); exit(0); } sigprocmask(SIG_BLOCK, &mask, NULL); addjob(jobs, pid, bg, cmdline); sigprocmask(SIG_SETMASK, &mask_one, NULL); if(bg==FG) waitfg(pid); else { sigprocmask(SIG_BLOCK , &mask, NULL); struct job_t *job = getjobpid(jobs, pid); printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline); } sigprocmask(SIG_SETMASK, &mask_prev, NULL); } return; }
int builtin_cmd(char **argv) { sigset_t mask, mask_prev; if(strcmp(argv[0], "quit")==0) { _exit(0); } else if(strcmp(argv[0], "fg")==0 || strcmp(argv[0], "bg")==0) { do_bgfg(argv); return 1; } else if(strcmp(argv[0], "&")==0) return 1; else if(strcmp(argv[0], "jobs")==0) { sigfillset(&mask); sigprocmask(SIG_BLOCK, &mask, &mask_prev); listjobs(jobs); sigprocmask(SIG_SETMASK, &mask_prev, NULL); return 1; } return 0; }
void do_bgfg(char **argv) { sigset_t mask, mask_prev; struct job_t *job; sigfillset(&mask); if(argv[1]==NULL) { printf("%s command requires PID or %%jobid argument\n", argv[0]); return ; } if(argv[1][0]=='%') { int jid=atoi(&argv[1][1]); if(!jid) { printf("%s: argument must be a PID or %%jobid\n", argv[0]); return ; } sigprocmask(SIG_BLOCK, &mask, &mask_prev); job = getjobjid(jobs, jid); sigprocmask(SIG_SETMASK, &mask_prev, NULL); } else { int pid=atoi(argv[1]); if(!pid) { printf("%s: argument must be a PID or %%jobid\n", argv[0]); return ; } sigprocmask(SIG_BLOCK, &mask, &mask_prev); job = getjobpid(jobs, pid); sigprocmask(SIG_SETMASK, &mask_prev, NULL); } sigprocmask(SIG_BLOCK, &mask, &mask_prev); if(job==NULL) { if(argv[1][0]=='%') printf("%s: No such job\n", argv[1]); else printf("(%s): No such process\n", argv[1]); sigprocmask(SIG_SETMASK, &mask_prev, NULL); return ; } if(strcmp(argv[0], "bg")==0) { if(job->state==ST) { job->state = BG; kill(-job->pid, SIGCONT); printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline); } } else { if(job->state==BG) { job->state = FG; waitfg(job->pid); } else if(job->state==ST) { job->state = FG; kill(-job->pid, SIGCONT); waitfg(job->pid); } } sigprocmask(SIG_SETMASK, &mask_prev, NULL); return; }
void waitfg(pid_t pid) { sigset_t mask; sigemptyset(&mask); fg_stop_or_exit = 0; while(!fg_stop_or_exit) sigsuspend(&mask); return; }
void sigchld_handler(int sig) { sigset_t mask, mask_prev; int olderrno=errno, status; pid_t pid; struct job_t *job; sigfillset(&mask); while((pid=waitpid(-1, &status, WNOHANG | WUNTRACED))>0) { sigprocmask(SIG_BLOCK, &mask, &mask_prev); job = getjobpid(jobs, pid); if(pid==fgpid(jobs)) fg_stop_or_exit = 1; if(WIFSTOPPED(status)) { job->state = ST; printf("Job [%d] (%d) stopped by signal %d\n", job->jid, pid, WSTOPSIG(status)); } else { if(WIFSIGNALED(status)) printf("Job [%d] (%d) terminated by signal %d\n", job->jid, pid, WTERMSIG(status)); deletejob(jobs, pid); } sigprocmask(SIG_SETMASK, &mask_prev, NULL); } errno = olderrno; return; }
void sigint_handler(int sig) { sigset_t mask, mask_prev; int olderrno=errno; pid_t pid; sigfillset(&mask); sigprocmask(SIG_BLOCK, &mask, &mask_prev); pid = fgpid(jobs); sigprocmask(SIG_SETMASK, &mask_prev, NULL); if(pid) kill(-pid, SIGINT); errno = olderrno; return; }
void sigtstp_handler(int sig) { sigset_t mask, mask_prev; int olderrno=errno; pid_t pid; sigfillset(&mask); sigprocmask(SIG_BLOCK, &mask, &mask_prev); pid = fgpid(jobs); sigprocmask(SIG_SETMASK, &mask_prev, NULL); if(pid) kill(-pid, SIGTSTP); errno = olderrno; return; }
|