Changeset 6625 for trunk/tools/CmdQd/CmdQd.c
- Timestamp:
- Sep 2, 2001, 4:51:41 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/CmdQd/CmdQd.c
r6623 r6625 1 /* $Id: CmdQd.c,v 1. 3 2001-09-01 22:48:10bird Exp $1 /* $Id: CmdQd.c,v 1.4 2001-09-02 02:51:41 bird Exp $ 2 2 * 3 3 * Command Queue Daemon / Client. … … 95 95 96 96 97 /* 98 * Memory debugging. 99 */ 100 #ifdef DEBUGMEMORY 101 void my_free(void *); 102 void *my_malloc(size_t); 103 104 #undef free 105 #undef malloc 106 107 #define free(pv) my_free(pv) 108 #define malloc(cb) my_malloc(cb) 109 110 #endif 111 97 112 /******************************************************************************* 98 113 * Defined Constants And Macros * 99 114 *******************************************************************************/ 100 #define SHARED_MEM_NAME "\\SHAREMEM\\CmdQd "115 #define SHARED_MEM_NAME "\\SHAREMEM\\CmdQd2" 101 116 #define SHARED_MEM_SIZE 65536 102 117 #define IDLE_TIMEOUT_MS -1 //(60*1000*3) … … 125 140 msgWaitResponse = 4, 126 141 msgKill = 5, 127 msgKillResponse = 6 142 msgKillResponse = 6, 143 msgDying = 0xff 128 144 } enmMsgType; 129 145 … … 234 250 int Daemon(int cWorkers); 235 251 int DaemonInit(int cWorkers); 236 void signalhandler(int sig); 252 void signalhandlerDaemon(int sig); 253 void signalhandlerClient(int sig); 237 254 void Worker(void * iWorkerId); 238 255 char*WorkerArguments(char *pszArg, const char *pszzEnv, const char *pszCommand, char *pszCurDir, PPATHCACHE pPathCache); … … 371 388 char szArg[CCHMAXPATH + 32]; 372 389 373 if (!getenv("COMSPEC"))374 {375 Error("Fatal error: env. var. COMSPEC not found!\n");376 return 0;377 }378 379 390 sprintf(&szArg[0], "%s\t!Daemon! %d", arg0, cWorkers); 380 391 szArg[strlen(arg0)] = '\0'; … … 499 510 * Wait for output. 500 511 */ 501 rc = DosWaitEventSem(hevJobQueueFine, SEM_INDEFINITE_WAIT); 502 if (rc) 512 /*rc = DosWaitEventSem(hevJobQueueFine, SEM_INDEFINITE_WAIT); - there is some timing problem here, */ 513 rc = DosWaitEventSem(hevJobQueueFine, 1000); /* timeout after 1 second. */ 514 if (rc && rc != ERROR_TIMEOUT) 503 515 break; 516 rc = NO_ERROR; /* in case of TIMEOUT */ 504 517 505 518 /* … … 620 633 621 634 /* 635 * Set dying msg type. shrmemFree posts the hevClient so clients 636 * waiting for the daemon to respond will quit. 637 */ 638 pShrMem->enmMsgType = msgDying; 639 640 /* 622 641 * Cleanup. 623 642 */ … … 676 695 if (!rc) 677 696 { 678 DosSetMaxFH(cWorkers * 3+ 20);697 DosSetMaxFH(cWorkers * 6 + 20); 679 698 return 0; /* success! */ 680 699 } … … 709 728 * Daemon signal handler. 710 729 */ 711 void signalhandler(int sig) 712 { 713 sig = sig; 730 void signalhandlerDaemon(int sig) 731 { 732 /* 733 * Set dying msg type. shrmemFree posts the hevClient so clients 734 * waiting for the daemon to respond will quit. 735 */ 736 pShrMem->enmMsgType = msgDying; 737 738 /* 739 * Free and exit. 740 */ 714 741 shrmemFree(); 715 742 exit(-42); 716 } 743 sig = sig; 744 } 745 746 747 /** 748 * Client signal handler. 749 */ 750 void signalhandlerClient(int sig) 751 { 752 shrmemFree(); 753 exit(-42); 754 sig = sig; 755 } 756 717 757 718 758 … … 774 814 * Redirect output and start process. 775 815 */ 776 WorkerArguments( szArg, &pJob->JobInfo.szzEnv, &pJob->JobInfo.szCommand[0],816 WorkerArguments(&szArg[0], &pJob->JobInfo.szzEnv[0], &pJob->JobInfo.szCommand[0], 777 817 &pJob->JobInfo.szCurrentDir[0], &PathCache); 778 818 rc = DosCreatePipe(&hPipeR, &hPipeW, sizeof(pJobOutput->szOutput) - 1); … … 826 866 ULONG cchRead2 = 0; 827 867 828 cchRead = sizeof(pJobOutput->szOutput) - 1;868 cchRead = sizeof(pJobOutput->szOutput) - pJobOutput->cchOutput - 1; 829 869 while (((rc = DosRead(hPipeR, 830 870 &pJobOutput->szOutput[pJobOutput->cchOutput], … … 1399 1439 * Install signal handlers. 1400 1440 */ 1401 signal(SIGSEGV, signalhandler );1402 signal(SIGTERM, signalhandler );1403 signal(SIGABRT, signalhandler );1404 signal(SIGINT, signalhandler );1405 signal(SIGBREAK,signalhandler );1441 signal(SIGSEGV, signalhandlerDaemon); 1442 signal(SIGTERM, signalhandlerDaemon); 1443 signal(SIGABRT, signalhandlerDaemon); 1444 signal(SIGINT, signalhandlerDaemon); 1445 signal(SIGBREAK,signalhandlerDaemon); 1406 1446 1407 1447 return rc; … … 1468 1508 if (rc) 1469 1509 { 1470 Error("Fatal error: Failed to open aquire ownership of client mutex semaphore. rc=%d\n");1510 Error("Fatal error: Failed to open take ownership of client mutex semaphore. rc=%d\n"); 1471 1511 shrmemFree(); 1472 1512 return rc; … … 1476 1516 if (rc) 1477 1517 { 1478 Error("Fatal error: Failed to open aquire ownership of mutex semaphore. rc=%d\n");1518 Error("Fatal error: Failed to open take ownership of mutex semaphore. rc=%d\n"); 1479 1519 shrmemFree(); 1480 1520 return rc; … … 1485 1525 * Install signal handlers. 1486 1526 */ 1487 signal(SIGSEGV, signalhandler );1488 signal(SIGTERM, signalhandler );1489 signal(SIGABRT, signalhandler );1490 signal(SIGINT, signalhandler );1491 signal(SIGBREAK,signalhandler );1527 signal(SIGSEGV, signalhandlerClient); 1528 signal(SIGTERM, signalhandlerClient); 1529 signal(SIGABRT, signalhandlerClient); 1530 signal(SIGINT, signalhandlerClient); 1531 signal(SIGBREAK,signalhandlerClient); 1492 1532 1493 1533 return rc; … … 1502 1542 if (!pShrMem) 1503 1543 return; 1544 /* wakeup any clients */ 1545 DosPostEventSem(pShrMem->hevClient); 1546 /* free stuff */ 1504 1547 DosReleaseMutexSem(pShrMem->hmtxClient); 1505 1548 DosReleaseMutexSem(pShrMem->hmtx); … … 1547 1590 1548 1591 if (!rc) 1592 { 1549 1593 rc = DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT); 1594 if (rc == ERROR_SEM_OWNER_DIED) 1595 { 1596 DosCloseMutexSem(pShrMem->hmtx); 1597 pShrMem->hmtx = NULLHANDLE; 1598 rc = DosCreateMutexSem(NULL, &pShrMem->hmtx, DC_SEM_SHARED, TRUE); 1599 } 1600 } 1601 1550 1602 if (rc && rc != ERROR_INTERRUPT) 1551 1603 Error("Internal error: failed to get next message from daemon, rc=%d\n", rc); … … 1581 1633 { 1582 1634 rc = DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT); 1635 if (rc == ERROR_SEM_OWNER_DIED) 1636 { 1637 Error("Internal error: shared mem mutex owner died.\n"); 1638 return -1; 1639 } 1640 1583 1641 if (!rc && pShrMem->enmMsgType != enmMsgTypeResponse) 1584 1642 { 1585 Error("Internal error: Invalid response message. response=%d expected=%d\n", 1586 pShrMem->enmMsgType, enmMsgTypeResponse); 1643 if (pShrMem->enmMsgType != msgDying) 1644 Error("Internal error: Invalid response message. response=%d expected=%d\n", 1645 pShrMem->enmMsgType, enmMsgTypeResponse); 1646 else 1647 Error("Fatal error: daemon just died!\n"); 1587 1648 return -1; 1588 1649 } … … 1612 1673 } 1613 1674 1675 1676 #ifdef DEBUGMEMORY 1677 void my_free(void *pv) 1678 { 1679 DosFreeMem((PVOID)((unsigned)pv & 0xffff0000)); 1680 } 1681 1682 void *my_malloc(size_t cb) 1683 { 1684 APIRET rc; 1685 PVOID pv; 1686 ULONG cbAlloc; 1687 char szMsg[200]; 1688 1689 cbAlloc = (cb + 0x1fff) & (~0x0fff); 1690 1691 rc = DosAllocMem(&pv, cbAlloc, PAG_READ | PAG_WRITE); 1692 if (!rc) 1693 { 1694 rc = DosSetMem(pv, cbAlloc - 0x1000, PAG_READ | PAG_WRITE | PAG_COMMIT); 1695 if (rc) 1696 __interrupt(3); 1697 if (cb & 0xfff) 1698 pv = (PVOID)((unsigned)pv + 0x1000 - (cb & 0x0fff)); 1699 } 1700 1701 strcpy(szMsg, "malloc("); 1702 _itoa(cb, szMsg + strlen(szMsg), 16); 1703 strcat(szMsg, ") -> "); 1704 _itoa(pv, szMsg + strlen(szMsg), 16); 1705 strcat(szMsg, "\r\n"); 1706 1707 DosPutMessage(1, strlen(szMsg), szMsg); 1708 1709 return rc ? NULL : pv; 1710 } 1711 #endif
Note:
See TracChangeset
for help on using the changeset viewer.