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.

700 lines
12 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <dirent.h>
  6. #include <signal.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <termcap.h>
  10. #include <termios.h>
  11. #include <curses.h>
  12. #include <limits.h>
  13. #include <pwd.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <sys/stat.h>
  17. #include <sys/times.h>
  18. #include <sys/time.h>
  19. #include <sys/select.h>
  20. #include <minix/com.h>
  21. #include <minix/config.h>
  22. #include <minix/type.h>
  23. #include <minix/endpoint.h>
  24. #include <minix/const.h>
  25. #include <minix/u64.h>
  26. #include <paths.h>
  27. #include <minix/procfs.h>
  28. #include "yeeshell.h"
  29. /* record cmdline history */
  30. char *history[CMDLINE_HISTORY_MAX_QUANTITY];
  31. int cmdline_amount = 0;
  32. struct proc *proc = NULL, *prev_proc = NULL;
  33. int nr_total;
  34. unsigned int nr_procs, nr_tasks;
  35. int main()
  36. {
  37. char *cmdline = NULL, *pwd = NULL;
  38. char *args[ARGS_MAX_QUANTITY];
  39. int status = 1;
  40. pwd = (char *)calloc(PATH_MAX_SIZE, sizeof(char));
  41. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  42. {
  43. history[i] = (char *)calloc(CMDLINE_MAX_SIZE, sizeof(char));
  44. }
  45. /* execute the shell's read, parse and execution loop */
  46. do
  47. {
  48. if (!getcwd(pwd, PATH_MAX_SIZE))
  49. {
  50. printf("yeeshell: The current path cannot be obtained!\n");
  51. exit(0);
  52. }
  53. printf("[root@yeeshell %s]# ", pwd);
  54. cmdline = readline();
  55. strcpy(history[cmdline_amount++], cmdline);
  56. status = execute(cmdline, args);
  57. free(cmdline);
  58. } while (status);
  59. for (int i = 0; i < CMDLINE_HISTORY_MAX_QUANTITY; i++)
  60. {
  61. free(history[i]);
  62. }
  63. exit(EXIT_SUCCESS);
  64. }
  65. char *readline()
  66. {
  67. char *cmdline = NULL;
  68. ssize_t bufsize = 0;
  69. getline(&cmdline, &bufsize, stdin);
  70. return cmdline;
  71. }
  72. int parseline(char *cmdline, char **args)
  73. {
  74. static char array[CMDLINE_MAX_SIZE]; /* holds local copy of command line */
  75. char *buf = array; /* ptr that traverses command line */
  76. char *delim; /* points to first space delimiter */
  77. int argc; /* number of args */
  78. int bg; /* background job? */
  79. strcpy(buf, cmdline);
  80. buf[strlen(buf) - 1] = ' '; /* replace trailing '\n' with space */
  81. while (*buf && (*buf == ' ')) /* ignore leading spaces */
  82. {
  83. buf++;
  84. }
  85. /* Build the argv list */
  86. argc = 0;
  87. if (*buf == '\'')
  88. {
  89. buf++;
  90. delim = strchr(buf, '\'');
  91. }
  92. else
  93. {
  94. delim = strchr(buf, ' ');
  95. }
  96. while (delim)
  97. {
  98. args[argc++] = buf;
  99. *delim = '\0';
  100. buf = delim + 1;
  101. while (*buf && (*buf == ' ')) /* ignore spaces */
  102. {
  103. buf++;
  104. }
  105. if (*buf == '\'')
  106. {
  107. buf++;
  108. delim = strchr(buf, '\'');
  109. }
  110. else
  111. {
  112. delim = strchr(buf, ' ');
  113. }
  114. }
  115. args[argc] = NULL;
  116. if (argc == 0) /* ignore blank line */
  117. {
  118. return 1;
  119. }
  120. /* should the job run in the background? */
  121. if ((bg = (*args[argc - 1] == '&')) != 0)
  122. {
  123. args[--argc] = NULL;
  124. }
  125. return bg;
  126. }
  127. int check_redirect(char **args, char *redirect_filename, char **redirect_args)
  128. {
  129. int i = 0, j = 0, redirect_flag = REDIRECT_NO;
  130. while (args[i] != NULL)
  131. {
  132. if (!strcmp(args[i], ">"))
  133. {
  134. redirect_flag = REDIRECT_OUT;
  135. break;
  136. }
  137. else if (!strcmp(args[i], "<"))
  138. {
  139. redirect_flag = REDIRECT_IN;
  140. break;
  141. }
  142. i++;
  143. }
  144. if ((redirect_flag == 1) || (redirect_flag == 2))
  145. {
  146. strcpy(redirect_filename, args[i + 1]);
  147. for (j = 0; j < i; j++)
  148. {
  149. redirect_args[j] = args[j];
  150. }
  151. }
  152. return redirect_flag;
  153. }
  154. int do_redirect(int redirect_flag, char *redirect_filename, char **redirect_args)
  155. {
  156. pid_t pid;
  157. int fd = 1;
  158. if ((pid = fork()) == 0) /* Child process */
  159. {
  160. if (redirect_flag == 1) /* in or out? */
  161. {
  162. fd = open(redirect_filename, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
  163. close(1);
  164. dup(fd);
  165. }
  166. else if (redirect_flag == 2)
  167. {
  168. fd = open(redirect_filename, O_RDONLY, S_IRUSR);
  169. close(0);
  170. dup(fd);
  171. }
  172. if (execvp(redirect_args[0], redirect_args) <= 0)
  173. {
  174. printf("%s: Command not found\n", redirect_args[0]);
  175. exit(0);
  176. }
  177. close(fd);
  178. }
  179. else /* parent process */
  180. {
  181. waitpid(pid, NULL, 0);
  182. }
  183. return 1;
  184. }
  185. int check_pipe(char **args, char **pipe_arg_1, char **pipe_arg_2)
  186. {
  187. int pipe_flag = 0, i = 0, j = 0;
  188. while (args[i] != NULL)
  189. {
  190. if (!strcmp(args[i], "|"))
  191. {
  192. pipe_flag = 1;
  193. break;
  194. }
  195. pipe_arg_1[j++] = args[i++];
  196. }
  197. pipe_arg_1[j] = NULL;
  198. j = 0;
  199. i++;
  200. while (args[i] != NULL)
  201. {
  202. pipe_arg_2[j++] = args[i++];
  203. }
  204. pipe_arg_2[j] = NULL;
  205. return pipe_flag;
  206. }
  207. int do_pipe(char **pipe_arg_1, char **pipe_arg_2)
  208. {
  209. int fds[2];
  210. pipe(fds);
  211. pid_t prog_1, prog_2;
  212. if ((prog_1 = fork()) == 0) /* Child process 1 */
  213. {
  214. close(1);
  215. dup(fds[1]);
  216. close(fds[0]);
  217. close(fds[1]);
  218. if (execvp(pipe_arg_1[0], pipe_arg_1) <= 0)
  219. {
  220. printf("%s: Command not found\n", pipe_arg_1[0]);
  221. exit(0);
  222. }
  223. }
  224. if ((prog_2 = fork()) == 0) /* Child process 1 */
  225. {
  226. dup2(fds[0], 0);
  227. close(fds[0]);
  228. close(fds[1]);
  229. if (execvp(pipe_arg_2[0], pipe_arg_2) <= 0)
  230. {
  231. printf("%s: Command not found\n", pipe_arg_2[0]);
  232. exit(0);
  233. }
  234. }
  235. close(fds[0]);
  236. close(fds[1]);
  237. waitpid(prog_1, NULL, 0);
  238. waitpid(prog_2, NULL, 0);
  239. return 1;
  240. }
  241. int do_bg_fg(char **args, int bg)
  242. {
  243. pid_t pid;
  244. if ((pid = fork()) == 0)
  245. {
  246. if (execvp(args[0], args) <= 0)
  247. {
  248. printf("%s: Command not found\n", args[0]);
  249. exit(0);
  250. }
  251. }
  252. else
  253. {
  254. if (bg)
  255. {
  256. signal(SIGCHLD, SIG_IGN);
  257. }
  258. else
  259. {
  260. waitpid(pid, NULL, 0);
  261. }
  262. }
  263. return 1;
  264. }
  265. int execute(char *cmdline, char **args)
  266. {
  267. int bg = 0, i = 0, redirect_flag = 0, pipe_num = 0;
  268. pid_t pid;
  269. char *redirect_filename = NULL;
  270. char *redirect_args[ARGS_MAX_QUANTITY];
  271. char *pipe_arg_1[ARGS_MAX_QUANTITY];
  272. char *pipe_arg_2[ARGS_MAX_QUANTITY];
  273. redirect_filename = (char *)calloc(REDIRECT_FILENAME_MAX_SIZE, sizeof(char));
  274. memset(redirect_args, NULL, sizeof(redirect_args));
  275. bg = parseline(cmdline, args);
  276. redirect_flag = check_redirect(args, redirect_filename, redirect_args);
  277. pipe_num = check_pipe(args, pipe_arg_1, pipe_arg_2);
  278. if (args[0] == NULL)
  279. {
  280. return 1;
  281. }
  282. if (pipe_num == 0) /* no pipe */
  283. {
  284. if (!built_in(args)) /* built-in cmd? */
  285. {
  286. if (redirect_flag != 0) /* redirection? */
  287. {
  288. return do_redirect(redirect_flag, redirect_filename, redirect_args);
  289. }
  290. else
  291. {
  292. return do_bg_fg(args, bg);
  293. }
  294. }
  295. else
  296. {
  297. return 1;
  298. }
  299. }
  300. else
  301. {
  302. return do_pipe(pipe_arg_1, pipe_arg_2);
  303. }
  304. }
  305. int built_in(char **args)
  306. {
  307. if (!strcmp(args[0], "exit"))
  308. {
  309. exit(0);
  310. }
  311. else if (!strcmp(args[0], "cd"))
  312. {
  313. return builtin_cd(args);
  314. }
  315. else if (!strcmp(args[0], "history"))
  316. {
  317. return builtin_history(args);
  318. }
  319. else if (!strcmp(args[0], "mytop"))
  320. {
  321. int flag = builtin_mytop();
  322. if (flag == -1)
  323. {
  324. printf("yeeshell: unable to get memory info.\n");
  325. }
  326. else if (flag == -2)
  327. {
  328. printf("yeeshell: unable to get CPU info.\n");
  329. }
  330. return flag;
  331. }
  332. else
  333. {
  334. return 0;
  335. }
  336. }
  337. int builtin_cd(char **args)
  338. {
  339. if (args[1] == NULL)
  340. {
  341. return 1;
  342. }
  343. else
  344. {
  345. if (chdir(args[1]) != 0)
  346. {
  347. perror("yeeshell");
  348. }
  349. return 1;
  350. }
  351. }
  352. int builtin_history(char **args)
  353. {
  354. int n = 0;
  355. if (args[1] == NULL)
  356. {
  357. n = cmdline_amount;
  358. }
  359. else
  360. {
  361. n = atoi(args[1]) < cmdline_amount ? atoi(args[1]) : cmdline_amount;
  362. }
  363. printf("ID\tCommandline\n");
  364. for (int i = 0; i < n; i++)
  365. {
  366. printf("%d\t%s\n", i + 1, history[i]);
  367. }
  368. return 1;
  369. }
  370. int builtin_mytop()
  371. {
  372. int memory_flag = 0, CPU_flag = 0;
  373. memory_flag = mytop_memory();
  374. CPU_flag = mytop_CPU();
  375. if (!memory_flag)
  376. {
  377. return -1;
  378. }
  379. else if (!CPU_flag)
  380. {
  381. return -2;
  382. }
  383. else
  384. {
  385. return 1;
  386. }
  387. }
  388. int mytop_memory()
  389. {
  390. FILE *fp = NULL;
  391. int pagesize;
  392. long total = 0, free = 0, cached = 0;
  393. if ((fp = fopen("/proc/meminfo", "r")) == NULL)
  394. {
  395. return 0;
  396. }
  397. fscanf(fp, "%u %lu %lu %lu", &pagesize, &total, &free, &cached);
  398. fclose(fp);
  399. printf("memmory(KBytes):\t%ld total\t%ld free\t%ld cached\n", (pagesize * total) / 1024, (pagesize * free) / 1024, (pagesize * cached) / 1024);
  400. return 1;
  401. }
  402. int mytop_CPU()
  403. {
  404. int cputimemode = 1;
  405. getkinfo();
  406. print_memory();
  407. get_procs();
  408. if (prev_proc == NULL)
  409. {
  410. get_procs();
  411. }
  412. print_procs(prev_proc, proc, cputimemode);
  413. return 0;
  414. }
  415. void get_procs()
  416. {
  417. struct proc *p;
  418. int i;
  419. p = prev_proc;
  420. prev_proc = proc;
  421. proc = p;
  422. if (proc == NULL)
  423. {
  424. proc = malloc(nr_total * sizeof(proc[0]));
  425. if (proc == NULL)
  426. {
  427. fprintf(stderr, "Out of memory!\n");
  428. exit(1);
  429. }
  430. }
  431. for (i = 0; i < nr_total; i++)
  432. proc[i].p_flags = 0;
  433. parse_dir();
  434. }
  435. void parse_dir()
  436. {
  437. DIR *p_dir;
  438. struct dirent *p_ent;
  439. pid_t pid;
  440. char *end;
  441. if ((p_dir = opendir("/proc")) == NULL)
  442. {
  443. perror("opendir on " _PATH_PROC);
  444. exit(1);
  445. }
  446. for (p_ent = readdir(p_dir); p_ent != NULL; p_ent = readdir(p_dir))
  447. {
  448. pid = strtol(p_ent->d_name, &end, 10);
  449. if (!end[0] && pid != 0)
  450. {
  451. parse_file(pid);
  452. }
  453. }
  454. closedir(p_dir);
  455. }
  456. void parse_file(pid_t pid)
  457. {
  458. char path[PATH_MAX], name[256], type, state;
  459. int version, endpt, effuid;
  460. unsigned long cycles_hi, cycles_lo;
  461. FILE *fp;
  462. struct proc *p;
  463. int i;
  464. sprintf(path, "/proc/%d/psinfo", pid);
  465. if ((fp = fopen(path, "r")) == NULL)
  466. {
  467. return;
  468. }
  469. if (fscanf(fp, "%d", &version) != 1)
  470. {
  471. fclose(fp);
  472. return;
  473. }
  474. if (version != PSINFO_VERSION)
  475. {
  476. fputs("procfs version mismatch!\n", stderr);
  477. exit(1);
  478. }
  479. if (fscanf(fp, " %c %d", &type, &endpt) != 2)
  480. {
  481. fclose(fp);
  482. return;
  483. }
  484. slot++;
  485. if (slot < 0 || slot >= nr_total)
  486. {
  487. fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
  488. fclose(fp);
  489. return;
  490. }
  491. p = &proc[slot];
  492. if (type == TYPE_TASK)
  493. {
  494. p->p_flags |= IS_TASK;
  495. }
  496. else if (type == TYPE_SYSTEM)
  497. {
  498. p->p_flags |= IS_SYSTEM;
  499. }
  500. p->p_endpoint = endpt;
  501. p->p_pid = pid;
  502. if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu", name, &state, &p->p_blocked, &p->p_priority, &p->p_user_time, &cycles_hi, &cycles_lo) != 7)
  503. {
  504. fclose(fp);
  505. return;
  506. }
  507. strncpy(p->p_name, name, sizeof(p->p_name) - 1);
  508. p->p_name[sizeof(p->p_name) - 1] = 0;
  509. if (state != STATE_RUN)
  510. {
  511. p->p_flags |= BLOCKED;
  512. }
  513. p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
  514. p->p_memory = 0L;
  515. if (!(p->p_flags & IS_TASK))
  516. {
  517. int j;
  518. if ((j = fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u", &p->p_memory, &effuid, &p->p_nice)) != 3)
  519. {
  520. fclose(fp);
  521. return;
  522. }
  523. p->p_effuid = effuid;
  524. }
  525. else
  526. {
  527. p->p_effuid = 0;
  528. }
  529. for (i = 1; i < CPUTIMENAMES; i++)
  530. {
  531. if (fscanf(fp, " %lu %lu", &cycles_hi, &cycles_lo) == 2)
  532. {
  533. p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
  534. }
  535. else
  536. {
  537. p->p_cpucycles[i] = 0;
  538. }
  539. }
  540. if ((p->p_flags & IS_TASK))
  541. {
  542. if (fscanf(fp, " %lu", &p->p_memory) != 1)
  543. {
  544. p->p_memory = 0;
  545. }
  546. }
  547. p->p_flags |= USED;
  548. fclose(fp);
  549. }
  550. void getkinfo()
  551. {
  552. FILE *fp;
  553. if ((fp = fopen("/proc/kinfo", "r")) == NULL)
  554. {
  555. fprintf(stderr, "opening " _PATH_PROC "kinfo failed\n");
  556. exit(1);
  557. }
  558. if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2)
  559. {
  560. fprintf(stderr, "reading from " _PATH_PROC "kinfo failed\n");
  561. exit(1);
  562. }
  563. fclose(fp);
  564. nr_total = (int)(nr_procs + nr_tasks);
  565. }
  566. void print_procs(struct proc *proc1, struct proc *proc2, int cputimemode)
  567. {
  568. int p, nprocs;
  569. u64_t systemticks = 0;
  570. u64_t userticks = 0;
  571. u64_t total_ticks = 0;
  572. int blockedseen = 0;
  573. static struct tp *tick_procs = NULL;
  574. if (tick_procs == NULL)
  575. {
  576. tick_procs = malloc(nr_total * sizeof(tick_procs[0]));
  577. if (tick_procs == NULL)
  578. {
  579. fprintf(stderr, "Out of memory!\n");
  580. exit(1);
  581. }
  582. }
  583. for (p = nprocs = 0; p < nr_total; p++)
  584. {
  585. u64_t uticks;
  586. if (!(proc2[p].p_flags & USED))
  587. {
  588. continue;
  589. }
  590. tick_procs[nprocs].p = proc2 + p;
  591. tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
  592. uticks = cputicks(&proc1[p], &proc2[p], 1);
  593. total_ticks = total_ticks + uticks;
  594. if (!(proc2[p].p_flags & IS_TASK))
  595. {
  596. if (proc2[p].p_flags & IS_SYSTEM)
  597. {
  598. systemticks = systemticks + tick_procs[nprocs].ticks;
  599. }
  600. else
  601. {
  602. userticks = userticks + tick_procs[nprocs].ticks;
  603. }
  604. }
  605. nprocs++;
  606. }
  607. if (total_ticks == 0)
  608. {
  609. return;
  610. }
  611. printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
  612. printf("%6.2f%% system", 100.0 * systemticks / total_ticks);
  613. printf("%6.2f%% in total\n", 100.0 * (systemticks + userticks) / total_ticks);
  614. }