Nife version Beta
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.
 
 
 
 

168 lines
3.5 KiB

  1. /* Copyright (C) 2011-2013 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. #include "conf.h"
  14. /* task.c gestion des taches */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <time.h>
  19. #include <signal.h>
  20. #include <sys/time.h>
  21. #include <sys/resource.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include "nife.h"
  27. #include "mth.h"
  28. #include "err.h"
  29. #include "tasks.h"
  30. #include "foncs.h"
  31. #include "histo.h"
  32. #include "stackN.h"
  33. #include "stackF.h"
  34. #include "stackL.h"
  35. int ITASK=0; /* no de tache */
  36. #define MAX_TASK 10
  37. static pid_t TASKp[MAX_TASK]; /* pid */
  38. static void * TASKf[MAX_TASK]; /* func */
  39. void IF_NewTask (void)
  40. {
  41. int i;
  42. for (i=0;i<MAX_TASK;i++) if (TASKp[i]==0) break;
  43. if (i<MAX_TASK) {
  44. FctInTask = i+1;
  45. } else {
  46. FctInTask = -1;
  47. messErr(26);
  48. }
  49. }
  50. static char FLib[24];
  51. static char * FicCons(int t)
  52. {
  53. sprintf(FLib,".nife/Cons%d.log",t);
  54. return FLib;
  55. }
  56. int MakeTask (void * A)
  57. {
  58. int i, pid;
  59. char * NF;
  60. i = FctInTask-1;
  61. if ((pid = fork()) == -1) stopErr("IF_NewTask","fork");
  62. if (pid == 0) { /* fils */
  63. ITASK=FctInTask; /* TASK 0 is the interractive */
  64. NF = FicCons(ITASK);
  65. if ((i=open(NF,O_CREAT|O_RDWR|O_TRUNC,0600)) < 0) perror(NF);
  66. else {
  67. dup2(i,1);
  68. dup2(i,2);
  69. close(i);
  70. close(0);
  71. }
  72. } else {
  73. TASKp[i] = pid;
  74. putLong(FctInTask);
  75. TASKf[i]=A;
  76. FctInTask=0;
  77. }
  78. return(pid);
  79. }
  80. void IF_show_Tasks(void)
  81. {
  82. int i;
  83. for (i=0;i<MAX_TASK;i++) {
  84. if (TASKp[i] != 0) {
  85. printf(" %.2d : %s (",i+1,codByAddr(TASKf[i]));
  86. if (kill(TASKp[i],SIGUSR1) ==0) printf("running");
  87. else printf("stopped");
  88. printf(")\n");
  89. }
  90. }
  91. printf("<end of tasks list>\n");
  92. }
  93. void IF_statusTask(void)
  94. {
  95. long V;
  96. int i;
  97. if (getParLong(&V)) {
  98. i = V -1;
  99. if (TASKp[i] != 0) {
  100. if (kill(TASKp[i],SIGUSR1)==0) {
  101. putBool(TRUE);
  102. return;
  103. }
  104. }
  105. }
  106. putBool(FALSE);
  107. }
  108. void IF_stopTask(void)
  109. {
  110. long V;
  111. int i;
  112. if (getParLong(&V)) {
  113. i = V -1;
  114. if (TASKp[i] != 0) {
  115. _MODIF_WAITPID_(1);
  116. if (kill(TASKp[i],SIGKILL) == 0)
  117. waitpid(TASKp[i],NULL,0);
  118. _MODIF_WAITPID_(0);
  119. }
  120. }
  121. }
  122. void IF_delTask(void)
  123. {
  124. long V;
  125. int i;
  126. if (getParLong(&V)) {
  127. i = V -1;
  128. if (TASKp[i] != 0) {
  129. if (kill(TASKp[i],SIGUSR1)==0) {
  130. messErr(27);
  131. return;
  132. }
  133. }
  134. }
  135. TASKp[i] = 0;
  136. }
  137. void IF_showCons( void)
  138. {
  139. long V;
  140. int i;
  141. char * NF, comm[30];
  142. if (getParLong(&V)) {
  143. i = V -1;
  144. if (TASKp[i] != 0) {
  145. NF = FicCons((int)V);
  146. sprintf(comm,"more %s",NF);
  147. runCommand(comm);
  148. }
  149. else messErr(28);
  150. }
  151. }