Changeset 4705 for trunk/Ohana/src/opihi/pantasks/ControllerOps.c
- Timestamp:
- Aug 3, 2005, 5:50:08 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/pantasks/ControllerOps.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/pantasks/ControllerOps.c
r4697 r4705 1 1 # include "psched.h" 2 # define CONTROLLER_TIMEOUT 20 2 /* adding a new host can delay controller up to a second or so */ 3 # define CONTROLLER_TIMEOUT 200 3 4 # define CONNECT_TIMEOUT 300 4 5 5 6 /* local static variables to hold the connection to the controller */ 6 static int status = FALSE;7 static int ControllerStatus = FALSE; 7 8 static int stdin_cntl, stdout_cntl, stderr_cntl; 8 9 static IOBuffer stdout_buffer; 9 10 static IOBuffer stderr_buffer; 11 static int ControllerPID = 0; 10 12 11 13 /* test if the controller is running */ 12 14 int CheckControllerStatus () { 13 return ( status);15 return (ControllerStatus); 14 16 } 15 17 … … 29 31 int CheckControllerJobStatus (Job *job) { 30 32 31 int outstate ;33 int outstate, status; 32 34 char cmd[128], status_string[64]; 33 35 char *p; … … 41 43 switch (status) { 42 44 case CONTROLLER_DOWN: 43 fprintf (stderr, "controller is down\n");45 if (VerboseMode()) fprintf (stderr, "controller is down\n"); 44 46 FreeIOBuffer (&buffer); 45 47 return (FALSE); 46 48 47 49 case CONTROLLER_HUNG: 48 fprintf (stderr, "controller is not responding\n");50 if (VerboseMode()) fprintf (stderr, "controller is not responding\n"); 49 51 FreeIOBuffer (&buffer); 50 52 return (FALSE); 51 53 52 54 case CONTROLLER_GOOD: 53 fprintf (stderr, "message received (CheckControllerJobStatus)\n");55 if (VerboseMode()) fprintf (stderr, "message received (CheckControllerJobStatus)\n"); 54 56 break; 55 57 56 58 default: 57 fprintf (stderr, "unknown status for controller command: programming error\n");59 if (VerboseMode()) fprintf (stderr, "unknown status for controller command: programming error\n"); 58 60 exit (1); 59 61 } … … 105 107 106 108 /* send command to get appropriate channel */ 107 ALLOCATE (line, char, MAX (1, strlen(cmd) + 15)); 108 sprintf (line, "%s %d\n", cmd, pid); 109 status = write (stdin_cntl, line, strlen(line)); 110 free (line); 109 status = write_fmt (stdin_cntl, "%s %d\n", cmd, pid); 111 110 112 111 /* is pipe still open? */ … … 126 125 if (status == -1) return (CONTROLLER_HUNG); 127 126 128 fprintf (stderr, "message received (GetJobOutput : %s)\n", cmd);127 if (VerboseMode()) fprintf (stderr, "message received (GetJobOutput : %s)\n", cmd); 129 128 /* drop extra bytes from pcontrol (not pclient:job) */ 130 129 buffer[0].Nbuffer = Nstart + Nbytes; … … 161 160 162 161 /* construct the controller command portion */ 163 if (!strc mp (job[0].task[0].host, "NONE")) {162 if (!strcasecmp (job[0].task[0].host, "ANYHOST")) { 164 163 sprintf (cmd, "job"); 165 164 } else { … … 177 176 } 178 177 179 fprintf (stderr, "sending command to controller: %s\n", cmd);180 178 InitIOBuffer (&buffer, 0x100); 181 179 ControllerCommand (cmd, CONTROLLER_PROMPT, &buffer); 182 180 free (cmd); 183 184 fprintf (stderr, "response from controller: %s\n", buffer.buffer);185 181 186 182 /* extract the job PID from the controller response */ … … 203 199 char *p; 204 200 char **argv, cmd[128]; 205 int i, pid ;201 int i, pid, status; 206 202 int stdin_fd[2], stdout_fd[2], stderr_fd[2]; 207 203 IOBuffer buffer; 208 204 209 if ( status) return (TRUE);205 if (ControllerStatus) return (TRUE); 210 206 211 207 if (VarConfig ("CONTROLLER", "%s", cmd) == NULL) strcpy (cmd, "pcontrol"); … … 262 258 263 259 /* send handshake command */ 264 sprintf (cmd, "echo CONNECTED\n"); 265 status = write (stdin_fd[1], cmd, strlen(cmd)); 260 status = write_fmt (stdin_fd[1], "echo CONNECTED\n"); 266 261 if ((status == -1) && (errno == EPIPE)) goto pipe_error; 267 262 … … 286 281 InitIOBuffer (&stderr_buffer, 0x100); 287 282 283 ControllerPID = pid; 284 ControllerStatus = TRUE; 288 285 fprintf (stderr, "Connected\n"); 289 286 return (TRUE); … … 316 313 FlushIOBuffer (buffer); 317 314 318 fprintf (stderr, "send: %s\n", cmd); 319 320 /* send command to client (adding on \n) */ 321 ALLOCATE (line, char, MAX (1, strlen(cmd))); 322 sprintf (line, "%s\n", cmd); 323 status = write (stdin_cntl, line, strlen(line)); 324 free (line); 325 326 /* is pipe still open? */ 315 if (VerboseMode()) fprintf (stderr, "send: %s\n", cmd); 316 317 /* send command, is pipe still open? */ 318 status = write_fmt (stdin_cntl, "%s\n", cmd); 327 319 if ((status == -1) && (errno == EPIPE)) return (CONTROLLER_DOWN); 328 320 … … 335 327 if (status == -1) usleep (10000); 336 328 } 337 if (status == 0) return (CONTROLLER_DOWN); 329 if (status == 0) { 330 ControllerStatus = FALSE; 331 return (CONTROLLER_DOWN); 332 } 338 333 if (status == -1) return (CONTROLLER_HUNG); 339 /* fprintf (stderr, "buffer.buffer: %s\n", buffer[0].buffer); */ 334 335 /* need to strip off the prompt */ 336 line = memstr (buffer[0].buffer, response, buffer[0].Nbuffer); 337 if (line != NULL) { 338 buffer[0].Nbuffer = line - buffer[0].buffer; 339 bzero (buffer[0].buffer + buffer[0].Nbuffer, buffer[0].Nalloc - buffer[0].Nbuffer); 340 } 340 341 return (CONTROLLER_GOOD); 341 342 } … … 345 346 int Nread; 346 347 347 if (! status) return (TRUE);348 if (!ControllerStatus) return (TRUE); 348 349 349 350 /* read stdout buffer */ … … 390 391 IOBuffer buffer; 391 392 392 if (!C heckControllerStatus()) return (TRUE);393 if (!ControllerStatus) return (TRUE); 393 394 394 395 sprintf (cmd, "kill %d", job[0].pid); … … 403 404 int QuitController () { 404 405 405 int status;406 int i, status, waitstatus, result; 406 407 char cmd[128]; 407 408 IOBuffer buffer; 408 409 409 if (!C heckControllerStatus()) return (TRUE);410 if (!ControllerStatus) return (TRUE); 410 411 411 412 sprintf (cmd, "quit"); 412 413 InitIOBuffer (&buffer, 0x100); 413 414 status = ControllerCommand (cmd, CONTROLLER_PROMPT, &buffer); 414 return (TRUE); 415 } 416 417 /* memstr returns a view, not an allocated string : don't free */ 415 ControllerStatus = FALSE; 416 result = waitpid (ControllerPID, &waitstatus, WNOHANG); 417 for (i = 0; (i < 10) && (result == 0); i++) { 418 usleep (10000); /* 10 ms is min */ 419 result = waitpid (ControllerPID, &waitstatus, WNOHANG); 420 } 421 ControllerPID = 0; 422 close (stdin_cntl); 423 close (stdout_cntl); 424 close (stderr_cntl); 425 426 return (TRUE); 427 }
Note:
See TracChangeset
for help on using the changeset viewer.
