OS2021_Project1.Shell
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

701 lignes
12 KiB

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