OS2021_Project1.Shell
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

603 regels
12 KiB

3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
3 jaren geleden
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include "yeeshell.h"
  11. /* record cmdline history */
  12. char *history[CMDLINE_HISTORY_MAX_QUANTITY];
  13. int cmdline_amount = 0;
  14. /* if true, print additional output */
  15. int verbose = 0;
  16. /* next job ID to allocate */
  17. int nextjid = 1;
  18. /* environment variable */
  19. extern char **environ;
  20. /* The job list */
  21. struct job_t jobs[JOBS_MAX_QUANTITY];
  22. int main()
  23. {
  24. char *cmdline = NULL, *pwd = NULL;
  25. char *args[ARGS_MAX_QUANTITY];
  26. int status = 1;
  27. pwd = (char *)calloc(PATH_MAX, sizeof(char));
  28. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  29. {
  30. history[i] = (char *)calloc(CMDLINE_MAX_SIZE, sizeof(char));
  31. }
  32. /* Install the signal handlers */
  33. Signal(SIGINT, sigint_handler); /* ctrl-c */
  34. Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */
  35. Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */
  36. Signal(SIGQUIT, sigquit_handler);
  37. /* initiate job list */
  38. initjobs(jobs);
  39. /* execute the shell's read, parse and execution loop */
  40. do
  41. {
  42. if (!getcwd(pwd, PATH_MAX))
  43. {
  44. printf("yeeshell: The current path cannot be obtained!\n");
  45. exit(0);
  46. }
  47. printf("[root@yeeshell %s]# ", pwd);
  48. cmdline = readline();
  49. strcpy(history[cmdline_amount++], cmdline);
  50. status = execute(cmdline, args);
  51. free(cmdline);
  52. } while (status);
  53. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  54. {
  55. free(history[i]);
  56. }
  57. exit(EXIT_SUCCESS);
  58. }
  59. char *readline()
  60. {
  61. char *cmdline = NULL;
  62. ssize_t bufsize = 0;
  63. getline(&cmdline, &bufsize, stdin);
  64. return cmdline;
  65. }
  66. int parseline(char *cmdline, char **args)
  67. {
  68. static char array[CMDLINE_MAX_SIZE]; /* holds local copy of command line */
  69. char *buf = array; /* ptr that traverses command line */
  70. char *delim; /* points to first space delimiter */
  71. int argc; /* number of args */
  72. int bg; /* background job? */
  73. strcpy(buf, cmdline);
  74. buf[strlen(buf) - 1] = ' '; /* replace trailing '\n' with space */
  75. while (*buf && (*buf == ' ')) /* ignore leading spaces */
  76. {
  77. buf++;
  78. }
  79. /* Build the argv list */
  80. argc = 0;
  81. if (*buf == '\'')
  82. {
  83. buf++;
  84. delim = strchr(buf, '\'');
  85. }
  86. else
  87. {
  88. delim = strchr(buf, ' ');
  89. }
  90. while (delim)
  91. {
  92. args[argc++] = buf;
  93. *delim = '\0';
  94. buf = delim + 1;
  95. while (*buf && (*buf == ' ')) /* ignore spaces */
  96. {
  97. buf++;
  98. }
  99. if (*buf == '\'')
  100. {
  101. buf++;
  102. delim = strchr(buf, '\'');
  103. }
  104. else
  105. {
  106. delim = strchr(buf, ' ');
  107. }
  108. }
  109. args[argc] = NULL;
  110. if (argc == 0) /* ignore blank line */
  111. {
  112. return 1;
  113. }
  114. /* should the job run in the background? */
  115. if ((bg = (*args[argc - 1] == '&')) != 0)
  116. {
  117. args[--argc] = NULL;
  118. }
  119. return bg;
  120. }
  121. int execute(char *cmdline, char **args)
  122. {
  123. int bg = 0, i = 0;
  124. pid_t pid;
  125. sigset_t mask_all, mask_prev;
  126. sigprocmask(SIG_BLOCK, NULL, &mask_all);
  127. sigaddset(&mask_all, SIGCHLD);
  128. bg = parseline(cmdline, args);
  129. if (args[0] == NULL)
  130. {
  131. return 1;
  132. }
  133. if (!built_in(args))
  134. {
  135. /* Prevent child processes from ending between parent processes, that is, between addJob and deleteJob. */
  136. sigprocmask(SIG_BLOCK, &mask_all, &mask_prev); /* Shield SIGCHLD */
  137. if ((pid = fork()) == 0) /* Child process */
  138. {
  139. /* The child process inherits the parent's signal mask and will inherit it after exec,
  140. so it needs to restore the signal mask before executing. */
  141. sigprocmask(SIG_SETMASK, &mask_prev, NULL); /* Child process, unblock SIGCHLD */
  142. /* Set the pid of the current process to the group number of the process group it belongs to. */
  143. /* avoid being grouped with tsh */
  144. setpgid(0, 0);
  145. if (execvp(args[0], args) <= 0)
  146. {
  147. printf("%s: Command not found\n", args[0]);
  148. exit(0);
  149. }
  150. }
  151. else
  152. {
  153. do
  154. {
  155. } while (!args[i++]);
  156. if (bg) /* bg task */
  157. {
  158. addjob(jobs, pid, BG, cmdline);
  159. }
  160. else /* fg task */
  161. {
  162. addjob(jobs, pid, FG, cmdline);
  163. }
  164. sigprocmask(SIG_SETMASK, &mask_prev, NULL);
  165. if (bg) /* Don't wait for background tasks to finish */
  166. {
  167. printf("[%d](%d)%s", pid2jid(pid), pid, cmdline);
  168. }
  169. else /* Wait for foreground tasks to finish */
  170. {
  171. waitfg(pid);
  172. }
  173. }
  174. }
  175. return 1;
  176. }
  177. int built_in(char **args)
  178. {
  179. if (!strcmp(args[0], "exit"))
  180. {
  181. exit(0);
  182. }
  183. else if (!strcmp(args[0], "cd"))
  184. {
  185. return builtin_cd(args);
  186. }
  187. else if (!strcmp(args[0], "history"))
  188. {
  189. return builtin_history(args);
  190. }
  191. else if (!strcmp(args[0], "mytop"))
  192. {
  193. return builtin_mytop(args);
  194. }
  195. else if (!strcmp(args[0], "jobs"))
  196. {
  197. return builtin_jobs(args);
  198. }
  199. else
  200. {
  201. return 0;
  202. }
  203. }
  204. int builtin_cd(char **args)
  205. {
  206. if (args[1] == NULL)
  207. {
  208. return 1;
  209. }
  210. else
  211. {
  212. if (chdir(args[1]) != 0)
  213. {
  214. perror("yeeshell");
  215. }
  216. return 1;
  217. }
  218. }
  219. int builtin_history(char **args)
  220. {
  221. int n = 0;
  222. if (args[1] == NULL)
  223. {
  224. n = cmdline_amount;
  225. }
  226. else
  227. {
  228. n = atoi(args[1]) < cmdline_amount ? atoi(args[1]) : cmdline_amount;
  229. }
  230. printf("ID\tCommandline\n");
  231. for (int i = 0; i < n; i++)
  232. {
  233. printf("%d\t%s\n", i + 1, history[i]);
  234. }
  235. return 1;
  236. }
  237. int builtin_jobs(char **args)
  238. {
  239. /* To prevent interruptions, block all signals. */
  240. sigset_t mask_all, mask_prev;
  241. sigfillset(&mask_all);
  242. sigprocmask(SIG_SETMASK, &mask_all, &mask_prev);
  243. for (int i = 0; i < JOBS_MAX_QUANTITY; i++)
  244. {
  245. if (jobs[i].pid != 0)
  246. {
  247. printf("[%d] (%d) ", jobs[i].jid, jobs[i].pid);
  248. switch (jobs[i].state)
  249. {
  250. case BG:
  251. printf("Running ");
  252. break;
  253. case FG:
  254. printf("Foreground ");
  255. break;
  256. case ST:
  257. printf("Stopped ");
  258. break;
  259. default:
  260. printf("listjobs: Internal error: job[%d].state=%d ",
  261. i, jobs[i].state);
  262. }
  263. printf("%s", jobs[i].cmdline);
  264. }
  265. }
  266. sigprocmask(SIG_SETMASK, &mask_prev, NULL); /* unclock */
  267. return 1;
  268. }
  269. int builtin_mytop(char **args)
  270. {
  271. }
  272. int do_bgfg(char **args)
  273. {
  274. /* initialize variables */
  275. struct job_t *currentJob;
  276. int jid;
  277. pid_t pid;
  278. sigset_t mask_all, mask_prev;
  279. sigfillset(&mask_all);
  280. sigprocmask(SIG_SETMASK, &mask_all, &mask_prev);
  281. /* bg or fg has the argument? */
  282. if (args[1] == NULL)
  283. {
  284. printf("%s command requires PID or %%jobid argument\n", args[0]);
  285. return 1;
  286. }
  287. /* if process by jid, gets the corresponding Job structure*/
  288. else if (args[1][0] == '%')
  289. {
  290. jid = atoi(args[1][1]);
  291. currentJob = getjobjid(jobs, jid);
  292. if (currentJob == NULL)
  293. {
  294. printf("%%%d: No such job\n", jid);
  295. return 1;
  296. }
  297. pid = currentJob->pid;
  298. }
  299. /* if process by pid, gets the corresponding Job structure */
  300. else
  301. {
  302. pid = atoi(args[1]);
  303. currentJob = getjobpid(jobs, pid);
  304. if (pid <= 0)
  305. {
  306. printf("%s: argument must be a PID or %%jobid\n", args[0]);
  307. return 1;
  308. }
  309. currentJob = getjobpid(jobs, pid);
  310. if (currentJob == NULL)
  311. {
  312. printf("(%d): No such process\n", pid);
  313. return 1;
  314. }
  315. }
  316. /* bg or fg? */
  317. if (!strcmp(args[0], "bg")) /* if bg */
  318. {
  319. currentJob->state = BG;
  320. printf("[%d] (%d) %s", currentJob->jid, pid, currentJob->cmdline);
  321. sigprocmask(SIG_SETMASK, &mask_prev, NULL);
  322. kill(-pid, SIGCONT); /* send the SIGCONT to the pid */
  323. return 1;
  324. }
  325. else if (!strcmp(args[0], "fg")) /* if fg */
  326. {
  327. currentJob->state = FG;
  328. sigprocmask(SIG_SETMASK, &mask_prev, NULL);
  329. kill(-pid, SIGCONT); /* send the SIGCONT to the pid */
  330. waitfg(currentJob->pid); /* Child process switched to FG, so wait for it to finish */
  331. return 1;
  332. }
  333. else if (!strcmp(args[0], "kill"))
  334. {
  335. sigprocmask(SIG_SETMASK, &mask_prev, NULL);
  336. kill(-pid, SIGQUIT);
  337. return 1;
  338. }
  339. return 1;
  340. }
  341. void waitfg(pid_t pid)
  342. {
  343. sigset_t m;
  344. sigemptyset(&m);
  345. while (pid == fgpid(jobs))
  346. {
  347. /* Wake up when there is a signal to check whether the foreground process PID change, */
  348. /* change means that the foreground process is over. */
  349. sigsuspend(&m);
  350. }
  351. return;
  352. }
  353. void initjobs(struct job_t *jobs)
  354. {
  355. int i;
  356. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  357. clearjob(&jobs[i]);
  358. }
  359. int addjob(struct job_t *jobs, pid_t pid, int state, char *cmdline)
  360. {
  361. int i;
  362. if (pid < 1)
  363. return 0;
  364. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  365. {
  366. if (jobs[i].pid == 0)
  367. {
  368. jobs[i].pid = pid;
  369. jobs[i].state = state;
  370. jobs[i].jid = nextjid++;
  371. if (nextjid > JOBS_MAX_QUANTITY)
  372. nextjid = 1;
  373. strcpy(jobs[i].cmdline, cmdline);
  374. if (verbose)
  375. {
  376. printf("Added job [%d] %d %s\n", jobs[i].jid, jobs[i].pid, jobs[i].cmdline);
  377. }
  378. return 1;
  379. }
  380. }
  381. printf("Tried to create too many jobs\n");
  382. return 0;
  383. }
  384. void listjobs(struct job_t *jobs)
  385. {
  386. for (int i = 0; i < JOBS_MAX_QUANTITY; i++)
  387. {
  388. if (jobs[i].pid != 0)
  389. {
  390. printf("[%d] (%d) ", jobs[i].jid, jobs[i].pid);
  391. switch (jobs[i].state)
  392. {
  393. case BG:
  394. printf("Running ");
  395. break;
  396. case FG:
  397. printf("Foreground ");
  398. break;
  399. case ST:
  400. printf("Stopped ");
  401. break;
  402. default:
  403. printf("listjobs: Internal error: job[%d].state=%d ", i, jobs[i].state);
  404. }
  405. printf("%s", jobs[i].cmdline);
  406. }
  407. }
  408. }
  409. int deletejob(struct job_t *jobs, pid_t pid)
  410. {
  411. int i;
  412. if (pid < 1)
  413. return 0;
  414. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  415. {
  416. if (jobs[i].pid == pid)
  417. {
  418. clearjob(&jobs[i]);
  419. nextjid = maxjid(jobs) + 1;
  420. return 1;
  421. }
  422. }
  423. return 0;
  424. }
  425. void clearjob(struct job_t *job)
  426. {
  427. job->pid = 0;
  428. job->jid = 0;
  429. job->state = UNDF;
  430. job->cmdline[0] = '\0';
  431. }
  432. int pid2jid(pid_t pid)
  433. {
  434. int i;
  435. if (pid < 1)
  436. return 0;
  437. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  438. if (jobs[i].pid == pid)
  439. {
  440. return jobs[i].jid;
  441. }
  442. return 0;
  443. }
  444. pid_t fgpid(struct job_t *jobs)
  445. {
  446. for (int i = 0; i < JOBS_MAX_QUANTITY; i++)
  447. if (jobs[i].state == FG)
  448. return jobs[i].pid;
  449. return 0;
  450. }
  451. struct job_t *getjobpid(struct job_t *jobs, pid_t pid)
  452. {
  453. int i;
  454. if (pid < 1)
  455. return NULL;
  456. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  457. if (jobs[i].pid == pid)
  458. return &jobs[i];
  459. return NULL;
  460. }
  461. struct job_t *getjobjid(struct job_t *jobs, int jid)
  462. {
  463. int i;
  464. if (jid < 1)
  465. return NULL;
  466. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  467. if (jobs[i].jid == jid)
  468. return &jobs[i];
  469. return NULL;
  470. }
  471. int maxjid(struct job_t *jobs)
  472. {
  473. int i, max = 0;
  474. for (i = 0; i < JOBS_MAX_QUANTITY; i++)
  475. {
  476. if (jobs[i].jid > max)
  477. {
  478. max = jobs[i].jid;
  479. }
  480. }
  481. return max;
  482. }
  483. handler_t *Signal(int signum, handler_t *handler)
  484. {
  485. struct sigaction action, old_action;
  486. action.sa_handler = handler;
  487. sigemptyset(&action.sa_mask); /* block sigs of type being handled */
  488. action.sa_flags = SA_RESTART; /* restart syscalls if possible */
  489. if (sigaction(signum, &action, &old_action) < 0)
  490. fprintf(stdout, "%s: %s\n", 'Signal Error', strerror(errno));
  491. return (old_action.sa_handler);
  492. }
  493. void sigchld_handler(int signal)
  494. {
  495. pid_t pid;
  496. int status;
  497. while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0)
  498. {
  499. if (WIFEXITED(status)) /* Normal termination */
  500. {
  501. deletejob(jobs, pid);
  502. }
  503. if (WIFSTOPPED(status)) /* Task suspension */
  504. {
  505. struct job_t *job = getjobpid(jobs, pid);
  506. int jid = pid2jid(pid);
  507. printf("Job [%d] (%d) stopped by signal %d\n", jid, pid, WSTOPSIG(status));
  508. job->state = ST;
  509. }
  510. if (WIFSIGNALED(status)) /* Task terminated */
  511. {
  512. int jid = pid2jid(pid);
  513. printf("Job [%d] (%d) terminated by signal %d\n", jid, pid, WTERMSIG(status));
  514. deletejob(jobs, pid);
  515. }
  516. }
  517. return;
  518. }
  519. void sigint_handler(int signal)
  520. {
  521. pid_t pid = fgpid(jobs);
  522. if (pid != 0)
  523. {
  524. kill(-pid, signal);
  525. }
  526. return;
  527. }
  528. void sigtstp_handler(int signal)
  529. {
  530. pid_t pid = fgpid(jobs);
  531. if (pid > 0)
  532. {
  533. kill(-pid, signal);
  534. }
  535. return;
  536. }
  537. void sigquit_handler(int signal)
  538. {
  539. printf("Terminating after receipt of SIGQUIT signal\n");
  540. exit(1);
  541. }