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.
 
 
 
 

186 lines
3.8 KiB

  1. /* Copyright (C) 2011-2022 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 <string.h>
  18. #include <unistd.h>
  19. #include <time.h>
  20. #include <signal.h>
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include "nife.h"
  28. #include "mth.h"
  29. #include "err.h"
  30. #include "debug.h"
  31. #include "tasks.h"
  32. #include "foncs.h"
  33. #include "histo.h"
  34. #include "stackN.h"
  35. #include "stackF.h"
  36. #include "stackL.h"
  37. int ITASK=0; /* no de tache */
  38. #define MAX_TASK 10
  39. static pid_t TASKp[MAX_TASK]; /* pid */
  40. static void * TASKf[MAX_TASK]; /* func */
  41. void IF_NewTask (void)
  42. {
  43. int i;
  44. for (i=0;i<MAX_TASK;i++) if (TASKp[i]==0) break;
  45. if (i<MAX_TASK) {
  46. FctInTask = i+1;
  47. } else {
  48. FctInTask = -1;
  49. messErr(26);
  50. }
  51. }
  52. static char FLib[24];
  53. static char * FicCons(int t)
  54. {
  55. sprintf(FLib,".nife/Cons%d.log",t);
  56. return FLib;
  57. }
  58. int MakeTask (void * A)
  59. {
  60. int i, n, pid;
  61. char * NF, buf[50];
  62. i = FctInTask-1;
  63. if ((pid = fork()) == -1) stopErr("IF_NewTask","fork");
  64. if (pid == 0) { /* fils */
  65. ITASK=FctInTask; /* TASK 0 is the interractive one */
  66. NF = FicCons(ITASK);
  67. n = i+1;
  68. if ((i=open(NF,O_CREAT|O_RDWR|O_TRUNC,0600)) < 0) perror(NF);
  69. else {
  70. sprintf(buf,"Task #%d console :\n",n);
  71. write(i,buf,strlen(buf));
  72. dup2(i,1);
  73. dup2(i,2);
  74. close(i);
  75. close(0);
  76. }
  77. } else {
  78. TASKp[i] = pid;
  79. putLong(FctInTask);
  80. TASKf[i]=A;
  81. FctInTask=0;
  82. }
  83. return(pid);
  84. }
  85. void IF_show_Tasks(void)
  86. {
  87. int i;
  88. for (i=0;i<MAX_TASK;i++) {
  89. if (TASKp[i] != 0) {
  90. printf(" %.2d : %s (",i+1,codByAddr(TASKf[i]));
  91. if (kill(TASKp[i],SIGUSR1) ==0) printf("running");
  92. else printf("stopped");
  93. printf(")\n");
  94. }
  95. }
  96. printf("<end of tasks list>\n");
  97. }
  98. void IFD_show_Tasks(void)
  99. {
  100. _IFD_BEGIN_
  101. IF_show_Tasks();
  102. _IFD_END_
  103. }
  104. void IF_statusTask(void)
  105. {
  106. long V;
  107. int i;
  108. if (getParLong(&V)) {
  109. i = V -1;
  110. if (TASKp[i] != 0) {
  111. if (kill(TASKp[i],SIGUSR1)==0) {
  112. putBool(TRUE);
  113. return;
  114. }
  115. }
  116. }
  117. putBool(FALSE);
  118. }
  119. void IF_stopTask(void)
  120. {
  121. long V;
  122. int i;
  123. if (getParLong(&V)) {
  124. i = V -1;
  125. if (TASKp[i] != 0) {
  126. _MODIF_WAITPID_(1);
  127. if (kill(TASKp[i],SIGKILL) == 0)
  128. waitpid(TASKp[i],NULL,0);
  129. _MODIF_WAITPID_(0);
  130. }
  131. }
  132. }
  133. void IF_delTask(void)
  134. {
  135. long V;
  136. int i;
  137. if (getParLong(&V)) {
  138. i = V -1;
  139. if (TASKp[i] != 0) {
  140. if (kill(TASKp[i],SIGUSR1)==0) {
  141. messErr(27);
  142. return;
  143. }
  144. }
  145. TASKp[i] = 0;
  146. }
  147. }
  148. void IF_showCons(void)
  149. {
  150. long V;
  151. int i;
  152. char * NF, comm[30];
  153. if (getParLong(&V)) {
  154. i = V -1;
  155. if (TASKp[i] != 0) {
  156. NF = FicCons((int)V);
  157. sprintf(comm,"more %s",NF);
  158. runCommand(comm);
  159. }
  160. else messErr(28);
  161. }
  162. }
  163. void IFD_showCons(void)
  164. {
  165. _IFD_BEGIN_
  166. IF_showCons();
  167. _IFD_END_
  168. }