OS2021_Project1.Shell

697 行
12 KiB

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