Ignore:
Timestamp:
Sep 2, 2001, 4:51:41 AM (24 years ago)
Author:
bird
Message:

Corrected some memory output buffer stuff.

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:10 bird Exp $
     1/* $Id: CmdQd.c,v 1.4 2001-09-02 02:51:41 bird Exp $
    22 *
    33 * Command Queue Daemon / Client.
     
    9595
    9696
     97/*
     98 * Memory debugging.
     99 */
     100#ifdef DEBUGMEMORY
     101void my_free(void *);
     102void *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
    97112/*******************************************************************************
    98113*   Defined Constants And Macros                                               *
    99114*******************************************************************************/
    100 #define SHARED_MEM_NAME     "\\SHAREMEM\\CmdQd"
     115#define SHARED_MEM_NAME     "\\SHAREMEM\\CmdQd2"
    101116#define SHARED_MEM_SIZE     65536
    102117#define IDLE_TIMEOUT_MS     -1 //(60*1000*3)
     
    125140        msgWaitResponse = 4,
    126141        msgKill = 5,
    127         msgKillResponse = 6
     142        msgKillResponse = 6,
     143        msgDying = 0xff
    128144    }           enmMsgType;
    129145
     
    234250int  Daemon(int cWorkers);
    235251int  DaemonInit(int cWorkers);
    236 void signalhandler(int sig);
     252void signalhandlerDaemon(int sig);
     253void signalhandlerClient(int sig);
    237254void Worker(void * iWorkerId);
    238255char*WorkerArguments(char *pszArg, const char *pszzEnv, const char *pszCommand, char *pszCurDir, PPATHCACHE pPathCache);
     
    371388    char            szArg[CCHMAXPATH + 32];
    372389
    373     if (!getenv("COMSPEC"))
    374     {
    375         Error("Fatal error: env. var. COMSPEC not found!\n");
    376         return 0;
    377     }
    378 
    379390    sprintf(&szArg[0], "%s\t!Daemon! %d", arg0, cWorkers);
    380391    szArg[strlen(arg0)] = '\0';
     
    499510                     * Wait for output.
    500511                     */
    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)
    503515                        break;
     516                    rc = NO_ERROR;      /* in case of TIMEOUT */
    504517
    505518                    /*
     
    620633
    621634    /*
     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    /*
    622641     * Cleanup.
    623642     */
     
    676695                         if (!rc)
    677696                         {
    678                              DosSetMaxFH(cWorkers * 3 + 20);
     697                             DosSetMaxFH(cWorkers * 6 + 20);
    679698                             return 0;      /* success! */
    680699                         }
     
    709728 * Daemon signal handler.
    710729 */
    711 void signalhandler(int sig)
    712 {
    713     sig = sig;
     730void 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     */
    714741    shrmemFree();
    715742    exit(-42);
    716 }
     743    sig = sig;
     744}
     745
     746
     747/**
     748 * Client signal handler.
     749 */
     750void signalhandlerClient(int sig)
     751{
     752    shrmemFree();
     753    exit(-42);
     754    sig = sig;
     755}
     756
    717757
    718758
     
    774814             * Redirect output and start process.
    775815             */
    776             WorkerArguments(szArg, &pJob->JobInfo.szzEnv, &pJob->JobInfo.szCommand[0],
     816            WorkerArguments(&szArg[0], &pJob->JobInfo.szzEnv[0], &pJob->JobInfo.szCommand[0],
    777817                            &pJob->JobInfo.szCurrentDir[0], &PathCache);
    778818            rc = DosCreatePipe(&hPipeR, &hPipeW, sizeof(pJobOutput->szOutput) - 1);
     
    826866                    ULONG       cchRead2 = 0;
    827867
    828                     cchRead = sizeof(pJobOutput->szOutput) - 1;
     868                    cchRead = sizeof(pJobOutput->szOutput) - pJobOutput->cchOutput - 1;
    829869                    while (((rc = DosRead(hPipeR,
    830870                                         &pJobOutput->szOutput[pJobOutput->cchOutput],
     
    13991439     * Install signal handlers.
    14001440     */
    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);
    14061446
    14071447    return rc;
     
    14681508    if (rc)
    14691509    {
    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");
    14711511        shrmemFree();
    14721512        return rc;
     
    14761516    if (rc)
    14771517    {
    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");
    14791519        shrmemFree();
    14801520        return rc;
     
    14851525     * Install signal handlers.
    14861526     */
    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);
    14921532
    14931533    return rc;
     
    15021542    if (!pShrMem)
    15031543        return;
     1544    /* wakeup any clients */
     1545    DosPostEventSem(pShrMem->hevClient);
     1546    /* free stuff */
    15041547    DosReleaseMutexSem(pShrMem->hmtxClient);
    15051548    DosReleaseMutexSem(pShrMem->hmtx);
     
    15471590
    15481591        if (!rc)
     1592        {
    15491593            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
    15501602        if (rc && rc != ERROR_INTERRUPT)
    15511603            Error("Internal error: failed to get next message from daemon, rc=%d\n", rc);
     
    15811633        {
    15821634            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
    15831641            if (!rc && pShrMem->enmMsgType != enmMsgTypeResponse)
    15841642            {
    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");
    15871648                return -1;
    15881649            }
     
    16121673}
    16131674
     1675
     1676#ifdef DEBUGMEMORY
     1677void my_free(void *pv)
     1678{
     1679    DosFreeMem((PVOID)((unsigned)pv & 0xffff0000));
     1680}
     1681
     1682void *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.