⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 execnt.c

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 C
📖 第 1 页 / 共 3 页
字号:
        /* read in the available data */        if ( bytesAvailable > 0 )        {            /* we only read in the available bytes, to avoid blocking */            if (                ReadFile( in, ioBuffer,                    bytesAvailable <= IO_BUFFER_SIZE ? bytesAvailable : IO_BUFFER_SIZE,                    &bytesInBuffer, NULL )                )            {                if ( bytesInBuffer > 0 )                {                    /* clean up non-ascii chars */                    int i;                    for ( i = 0; i < bytesInBuffer; ++i )                    {                        if ((unsigned char)ioBuffer[i] < 1 ||                            (unsigned char)ioBuffer[i] > 127 )                        {                            ioBuffer[i] = '?';                        }                    }                    /* null, terminate */                    ioBuffer[bytesInBuffer] = '\0';                    /* append to the output */                    string_append(out,ioBuffer);                    /* subtract what we read in */                    bytesAvailable -= bytesInBuffer;                }                else                {                    /* likely read a error, bail out. */                    bytesAvailable = 0;                }            }            else            {                /* definitely read a error, bail out. */                bytesAvailable = 0;            }        }    }    while ( bytesAvailable > 0 );}static void read_output(){    int i;        for ( i = 0; i < globs.jobs && i < MAXJOBS; ++i )    {        /* read stdout data */        if (cmdtab[i].pipe_out[0])            read_pipe( cmdtab[i].pipe_out[0], & cmdtab[i].buffer_out );        /* read stderr data */        if (cmdtab[i].pipe_err[0])            read_pipe( cmdtab[i].pipe_err[0], & cmdtab[i].buffer_err );    }}/*  waits for a single child process command to complete, or the    timeout, whichever is first. returns the index of the completed    command, or -1. */static int try_wait(int timeoutMillis){    int i, num_active, waiting;    HANDLE active_handles[MAXJOBS];    int active_procs[MAXJOBS];    for ( waiting = 1; waiting;  )    {        /* find the first completed child process */        for ( num_active = 0, i = 0; i < globs.jobs; ++i )        {            /* if we have an already dead process, return it. */            cmdtab[i].exitcode = 0;            if ( GetExitCodeProcess( cmdtab[i].pi.hProcess, &cmdtab[i].exitcode ) )            {                if ( STILL_ACTIVE != cmdtab[i].exitcode )                {                    return i;                }            }            /* it's running, add it to the list to watch for */            active_handles[num_active] = cmdtab[i].pi.hProcess;            active_procs[num_active] = i;            num_active += 1;        }                /* wait for a child to complete, or for our timeout window to expire */        if ( waiting )        {            WaitForMultipleObjects( num_active, active_handles, FALSE, timeoutMillis );            waiting = 0;        }    }        return -1;}static int try_kill_one(){    /* only need to check if a timeout was specified with the -l option. */    if ( globs.timeout > 0 )    {        int i;                for ( i = 0; i < globs.jobs; ++i )        {            double t = running_time(cmdtab[i].pi.hProcess);            if ( t > (double)globs.timeout )            {                /* the job may have left an alert dialog around,                try and get rid of it before killing */                close_alert(cmdtab[i].pi.hProcess);                /* we have a "runaway" job, kill it */                kill_process_tree(0,cmdtab[i].pi.hProcess);                /* and return it as complete, with the failure code */                GetExitCodeProcess( cmdtab[i].pi.hProcess, &cmdtab[i].exitcode );                /* mark it as a timeout */                cmdtab[i].exit_reason = EXIT_TIMEOUT;                return i;            }        }    }    return -1;}static void close_alerts(){    /* we only attempt this every 5 seconds, or so, because it's    not a cheap operation, and we'll catch the alerts eventually.    this check uses floats as some compilers define CLOCKS_PER_SEC    as a float or double. */    if ( ((float)clock() / (float)(CLOCKS_PER_SEC*5)) < (1.0/5.0) )    {        int i;        for ( i = 0; i < globs.jobs; ++i )        {            close_alert(cmdtab[i].pi.hProcess);        }    }}/* calc the current running time of an *active* process */static double running_time(HANDLE process){    FILETIME creation, exit, kernel, user, current;    if (GetProcessTimes(process, &creation, &exit, &kernel, &user))    {        /* Compute the elapsed time */        GetSystemTimeAsFileTime(&current);        {            double delta = filetime_seconds(                add_FILETIME( current, negate_FILETIME(creation) )                );            return delta;        }    }    return 0.0;}/* it's just stupidly silly that one has to do this! */typedef struct PROCESS_BASIC_INFORMATION__ {    LONG ExitStatus;    PVOID PebBaseAddress;    ULONG AffinityMask;    LONG BasePriority;    ULONG UniqueProcessId;    ULONG InheritedFromUniqueProcessId;    } PROCESS_BASIC_INFORMATION_;typedef LONG (__stdcall * NtQueryInformationProcess__)(    HANDLE ProcessHandle,    LONG ProcessInformationClass,    PVOID ProcessInformation,    ULONG ProcessInformationLength,    PULONG ReturnLength);static NtQueryInformationProcess__ NtQueryInformationProcess_ = NULL;static HMODULE NTDLL_ = NULL;DWORD get_process_id(HANDLE process){    PROCESS_BASIC_INFORMATION_ pinfo;    if ( ! NtQueryInformationProcess_ )    {        if ( ! NTDLL_ )        {            NTDLL_ = GetModuleHandleA("ntdll");        }        if ( NTDLL_ )        {            NtQueryInformationProcess_                = (NtQueryInformationProcess__)GetProcAddress( NTDLL_,"NtQueryInformationProcess" );        }    }    if ( NtQueryInformationProcess_ )    {        LONG r = (*NtQueryInformationProcess_)(            process,/* ProcessBasicInformation == */ 0,&pinfo,sizeof(PROCESS_BASIC_INFORMATION_),NULL);        return pinfo.UniqueProcessId;    }    else    {        return 0;    }}/* not really optimal, or efficient, but it's easier this way, and it's notlike we are going to be killing thousands, or even tens of processes. */static void kill_process_tree(DWORD pid, HANDLE process){    HANDLE process_snapshot_h = INVALID_HANDLE_VALUE;    if ( !pid )    {        pid = get_process_id(process);    }    process_snapshot_h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);        if (INVALID_HANDLE_VALUE != process_snapshot_h)    {        BOOL ok = TRUE;        PROCESSENTRY32 pinfo;        pinfo.dwSize = sizeof(PROCESSENTRY32);        for (            ok = Process32First(process_snapshot_h,&pinfo);            TRUE == ok;            ok = Process32Next(process_snapshot_h,&pinfo) )        {            if (pinfo.th32ParentProcessID == pid)            {                /* found a child, recurse to kill it and anything else below it */                HANDLE ph = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pinfo.th32ProcessID);                if (NULL != ph)                {                    kill_process_tree(pinfo.th32ProcessID,ph);                    CloseHandle(ph);                }            }        }        CloseHandle(process_snapshot_h);    }    /* now that the children are all dead, kill the root */    TerminateProcess(process,-2);}static double creation_time(HANDLE process){    FILETIME creation, exit, kernel, user, current;    if (GetProcessTimes(process, &creation, &exit, &kernel, &user))    {        return filetime_seconds(creation);    }    return 0.0;}/* Recursive check if first process is parent (directly or indirectly) of the second one. Both processes are passed as process ids, not handles.Special return value 2 means that the second process is smss.exe and its parent process is System (first argument is ignored) */static int is_parent_child(DWORD parent, DWORD child){    HANDLE process_snapshot_h = INVALID_HANDLE_VALUE;    if (!child)        return 0;    if (parent == child)        return 1;    process_snapshot_h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);    if (INVALID_HANDLE_VALUE != process_snapshot_h)    {        BOOL ok = TRUE;        PROCESSENTRY32 pinfo;        pinfo.dwSize = sizeof(PROCESSENTRY32);        for (            ok = Process32First(process_snapshot_h, &pinfo);             ok == TRUE;             ok = Process32Next(process_snapshot_h, &pinfo) )        {            if (pinfo.th32ProcessID == child)            {                /*                Unfortunately, process ids are not really unique. There might                 be spurious "parent and child" relationship match between                two non-related processes if real parent process of a given                process has exited (while child process kept running as an                 "orphan") and the process id of such parent process has been                 reused by internals of the operating system when creating                 another process. Thus additional check is needed - process                creation time. This check may fail (ie. return 0) for system                 processes due to insufficient privileges, and that's OK. */                double tchild = 0.0;                double tparent = 0.0;                HANDLE hchild = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pinfo.th32ProcessID);                CloseHandle(process_snapshot_h);                /* csrss.exe may display message box like following:                    xyz.exe - Unable To Locate Component                    This application has failed to start because                     boost_foo-bar.dll was not found. Re-installing the                     application may fix the problem                This actually happens when starting test process that depends                on a dynamic library which failed to build. We want to                 automatically close these message boxes even though csrss.exe                is not our child process. We may depend on the fact that (in                all current versions of Windows) csrss.exe is directly                 child of smss.exe process, which in turn is directly child of                System process, which always has process id == 4 .                This check must be performed before comparison of process                 creation time */                if (stricmp(pinfo.szExeFile, "csrss.exe") == 0                    && is_parent_child(parent, pinfo.th32ParentProcessID) == 2)                {                    return 1;                }                else if (stricmp(pinfo.szExeFile, "smss.exe") == 0                    && pinfo.th32ParentProcessID == 4)                {                    return 2;                }                if (hchild != 0)                {                    HANDLE hparent = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pinfo.th32ParentProcessID);                    if (hparent != 0)                    {                        tchild = creation_time(hchild);                        tparent = creation_time(hparent);                                                CloseHandle(hparent);                    }                    CloseHandle(hchild);                }                /* return 0 if one of the following is true:                1. we failed to read process creation time                2. child was created before alleged parent */                if (tchild == 0.0 || tparent == 0.0 || tchild < tparent)                    return 0;                return is_parent_child(parent, pinfo.th32ParentProcessID) & 1;            }        }        CloseHandle(process_snapshot_h);    }    return 0;}typedef struct PROCESS_HANDLE_ID {HANDLE h; DWORD pid;} PROCESS_HANDLE_ID;/* This function is called by the operating system for each topmost window. */BOOL CALLBACK close_alert_window_enum(HWND hwnd, LPARAM lParam){    char buf[7] = {0};    PROCESS_HANDLE_ID p = *((PROCESS_HANDLE_ID*) (lParam));    DWORD pid = 0;    DWORD tid = 0;    /* we want to find and close any window that:    1. is visible and    2. is a dialog and    3. is displayed by any of our child processes */    if (!IsWindowVisible(hwnd))        return TRUE;    if (!GetClassNameA(hwnd, buf, sizeof(buf)))        return TRUE; /* failed to read class name; presume it's not a dialog */     if (strcmp(buf, "#32770") != 0)        return TRUE; /* not a dialog */    /* GetWindowThreadProcessId returns 0 on error, otherwise thread id    of window message pump thread */    tid = GetWindowThreadProcessId(hwnd, &pid);     if (tid && is_parent_child(p.pid, pid))    {        /* ask really nice */        PostMessageA(hwnd, WM_CLOSE, 0, 0);        /* now wait and see if it worked. If not, insist */        if (WaitForSingleObject(p.h, 200) == WAIT_TIMEOUT)        {            PostThreadMessageA(tid, WM_QUIT, 0, 0);            WaitForSingleObject(p.h, 300);        }                /* done, we do not want to check any other window now */        return FALSE;    }    return TRUE;}static void close_alert(HANDLE process){    DWORD pid = get_process_id(process);    /* If process already exited or we just cannot get its process id, do not     go any further */    if (pid)    {        PROCESS_HANDLE_ID p;        p.h = process;       	p.pid = pid;        EnumWindows(&close_alert_window_enum, (LPARAM) &p);    }}# endif /* USE_EXECNT */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -