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.

716 lines
13 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
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. return builtin_mytop();
  322. }
  323. else
  324. {
  325. return 0;
  326. }
  327. }
  328. int builtin_cd(char **args)
  329. {
  330. if (args[1] == NULL)
  331. {
  332. return 1;
  333. }
  334. else
  335. {
  336. if (chdir(args[1]) != 0)
  337. {
  338. perror("yeeshell");
  339. }
  340. return 1;
  341. }
  342. }
  343. int builtin_history(char **args)
  344. {
  345. int n = 0;
  346. if (args[1] == NULL)
  347. {
  348. n = cmdline_amount;
  349. }
  350. else
  351. {
  352. n = atoi(args[1]) < cmdline_amount ? atoi(args[1]) : cmdline_amount;
  353. }
  354. printf("ID\tCommandline\n");
  355. for (int i = 0; i < n; i++)
  356. {
  357. printf("%d\t%s\n", i + 1, history[i]);
  358. }
  359. return 1;
  360. }
  361. int builtin_mytop()
  362. {
  363. int memory_flag = 0, CPU_flag = 0;
  364. memory_flag = mytop_memory();
  365. CPU_flag = mytop_CPU();
  366. if (!memory_flag)
  367. {
  368. return -1;
  369. }
  370. else if (!CPU_flag)
  371. {
  372. return -2;
  373. }
  374. else
  375. {
  376. return 1;
  377. }
  378. }
  379. int mytop_memory()
  380. {
  381. FILE *fp = NULL;
  382. int pagesize;
  383. long total = 0, free = 0, cached = 0;
  384. if ((fp = fopen("/proc/meminfo", "r")) == NULL)
  385. {
  386. return 0;
  387. }
  388. fscanf(fp, "%u %lu %lu %lu", &pagesize, &total, &free, &cached);
  389. fclose(fp);
  390. printf("memmory(KBytes):\t%ld total\t%ld free\t%ld cached\n", (pagesize * total) / 1024, (pagesize * free) / 1024, (pagesize * cached) / 1024);
  391. return 1;
  392. }
  393. int mytop_CPU()
  394. {
  395. int cputimemode = 1;
  396. getkinfo();
  397. get_procs();
  398. if (prev_proc == NULL)
  399. {
  400. get_procs();
  401. }
  402. print_procs(prev_proc, proc, cputimemode);
  403. return 0;
  404. }
  405. void get_procs()
  406. {
  407. struct proc *p;
  408. int i;
  409. p = prev_proc;
  410. prev_proc = proc;
  411. proc = p;
  412. if (proc == NULL)
  413. {
  414. proc = malloc(nr_total * sizeof(proc[0]));
  415. if (proc == NULL)
  416. {
  417. fprintf(stderr, "Out of memory!\n");
  418. exit(1);
  419. }
  420. }
  421. for (i = 0; i < nr_total; i++)
  422. proc[i].p_flags = 0;
  423. parse_dir();
  424. }
  425. void parse_dir()
  426. {
  427. DIR *p_dir;
  428. struct dirent *p_ent;
  429. pid_t pid;
  430. char *end;
  431. if ((p_dir = opendir("/proc")) == NULL)
  432. {
  433. perror("opendir on " _PATH_PROC);
  434. exit(1);
  435. }
  436. for (p_ent = readdir(p_dir); p_ent != NULL; p_ent = readdir(p_dir))
  437. {
  438. pid = strtol(p_ent->d_name, &end, 10);
  439. if (!end[0] && pid != 0)
  440. {
  441. parse_file(pid);
  442. }
  443. }
  444. closedir(p_dir);
  445. }
  446. void parse_file(pid_t pid)
  447. {
  448. char path[PATH_MAX], name[256], type, state;
  449. int version, endpt, effuid;
  450. unsigned long cycles_hi, cycles_lo;
  451. FILE *fp;
  452. struct proc *p;
  453. int slot;
  454. int i;
  455. sprintf(path, "%d/psinfo", pid);
  456. if ((fp = fopen(path, "r")) == NULL)
  457. return;
  458. if (fscanf(fp, "%d", &version) != 1)
  459. {
  460. fclose(fp);
  461. return;
  462. }
  463. if (version != PSINFO_VERSION)
  464. {
  465. fputs("procfs version mismatch!\n", stderr);
  466. exit(1);
  467. }
  468. if (fscanf(fp, " %c %d", &type, &endpt) != 2)
  469. {
  470. fclose(fp);
  471. return;
  472. }
  473. slot = SLOT_NR(endpt);
  474. if (slot < 0 || slot >= nr_total)
  475. {
  476. //fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
  477. fclose(fp);
  478. return;
  479. }
  480. p = &proc[slot];
  481. if (type == TYPE_TASK)
  482. {
  483. p->p_flags |= IS_TASK;
  484. }
  485. else if (type == TYPE_SYSTEM)
  486. {
  487. p->p_flags |= IS_SYSTEM;
  488. }
  489. p->p_endpoint = endpt;
  490. p->p_pid = pid;
  491. 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)
  492. {
  493. fclose(fp);
  494. return;
  495. }
  496. strncpy(p->p_name, name, sizeof(p->p_name) - 1);
  497. p->p_name[sizeof(p->p_name) - 1] = 0;
  498. if (state != STATE_RUN)
  499. {
  500. p->p_flags |= BLOCKED;
  501. }
  502. p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
  503. p->p_memory = 0L;
  504. if (!(p->p_flags & IS_TASK))
  505. {
  506. int j;
  507. if ((j = fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u", &p->p_memory, &effuid, &p->p_nice)) != 3)
  508. {
  509. fclose(fp);
  510. return;
  511. }
  512. p->p_effuid = effuid;
  513. }
  514. else
  515. {
  516. p->p_effuid = 0;
  517. }
  518. for (i = 1; i < CPUTIMENAMES; i++)
  519. {
  520. if (fscanf(fp, " %lu %lu", &cycles_hi, &cycles_lo) == 2)
  521. {
  522. p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
  523. }
  524. else
  525. {
  526. p->p_cpucycles[i] = 0;
  527. }
  528. }
  529. if ((p->p_flags & IS_TASK))
  530. {
  531. if (fscanf(fp, " %lu", &p->p_memory) != 1)
  532. {
  533. p->p_memory = 0;
  534. }
  535. }
  536. p->p_flags |= USED;
  537. fclose(fp);
  538. }
  539. void getkinfo()
  540. {
  541. FILE *fp;
  542. if ((fp = fopen("/proc/kinfo", "r")) == NULL)
  543. {
  544. fprintf(stderr, "opening " _PATH_PROC "kinfo failed\n");
  545. exit(1);
  546. }
  547. if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2)
  548. {
  549. fprintf(stderr, "reading from " _PATH_PROC "kinfo failed\n");
  550. exit(1);
  551. }
  552. fclose(fp);
  553. nr_total = (int)(nr_procs + nr_tasks);
  554. }
  555. void print_procs(struct proc *proc1, struct proc *proc2, int cputimemode)
  556. {
  557. int p, nprocs;
  558. u64_t systemticks = 0;
  559. u64_t userticks = 0;
  560. u64_t total_ticks = 0;
  561. int blockedseen = 0;
  562. static struct tp *tick_procs = NULL;
  563. if (tick_procs == NULL)
  564. {
  565. tick_procs = malloc(nr_total * sizeof(tick_procs[0]));
  566. if (tick_procs == NULL)
  567. {
  568. fprintf(stderr, "Out of memory!\n");
  569. exit(1);
  570. }
  571. }
  572. for (p = nprocs = 0; p < nr_total; p++)
  573. {
  574. u64_t uticks;
  575. if (!(proc2[p].p_flags & USED))
  576. {
  577. continue;
  578. }
  579. tick_procs[nprocs].p = proc2 + p;
  580. tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
  581. uticks = cputicks(&proc1[p], &proc2[p], 1);
  582. total_ticks = total_ticks + uticks;
  583. if (!(proc2[p].p_flags & IS_TASK))
  584. {
  585. if (proc2[p].p_flags & IS_SYSTEM)
  586. {
  587. systemticks = systemticks + tick_procs[nprocs].ticks;
  588. }
  589. else
  590. {
  591. userticks = userticks + tick_procs[nprocs].ticks;
  592. }
  593. }
  594. nprocs++;
  595. }
  596. if (total_ticks == 0)
  597. {
  598. return;
  599. }
  600. printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
  601. printf("%6.2f%% system", 100.0 * systemticks / total_ticks);
  602. printf("%6.2f%% in total\n", 100.0 * (systemticks + userticks) / total_ticks);
  603. }
  604. u64_t cputicks(struct proc *p1, struct proc *p2, int timemode)
  605. {
  606. int i;
  607. u64_t t = 0;
  608. for (i = 0; i < CPUTIMENAMES; i++)
  609. {
  610. if (!CPUTIME(timemode, i))
  611. {
  612. continue;
  613. }
  614. if (p1->p_endpoint == p2->p_endpoint)
  615. {
  616. {
  617. t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
  618. }
  619. }
  620. else
  621. {
  622. t = t + p2->p_cpucycles[i];
  623. }
  624. }
  625. return t;
  626. }