~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to lib/win_util.cpp

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
// You should have received a copy of the GNU Lesser General Public License
16
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
17
 
18
 
 
19
 
#ifdef _BOINC_DLL
20
 
#include "stdafx.h"
21
 
#else
 
18
#if   defined(_WIN32) && !defined(__STDWX_H__)
22
19
#include "boinc_win.h"
 
20
#elif defined(_WIN32) && defined(__STDWX_H__)
 
21
#include "stdwx.h"
23
22
#endif
 
23
 
 
24
#include "diagnostics.h"
24
25
#include "win_util.h"
25
26
 
26
27
 
85
86
        {
86
87
            // In Windows 2000 we need to use the Product Suite APIs
87
88
            // Don't static link because it won't load on non-Win2000 systems
88
 
            hmodNtDll = GetModuleHandle( "NTDLL.DLL" );
 
89
            hmodNtDll = GetModuleHandleA( "NTDLL.DLL" );
89
90
            if (hmodNtDll != NULL)
90
91
            {
91
92
                pfnVerSetConditionMask = (PFnVerSetConditionMask )GetProcAddress( hmodNtDll, "VerSetConditionMask");
92
93
                if (pfnVerSetConditionMask != NULL)
93
94
                {
94
95
                    dwlConditionMask = (*pfnVerSetConditionMask)( dwlConditionMask, VER_SUITENAME, VER_AND );
95
 
                    hmodK32 = GetModuleHandle( "KERNEL32.DLL" );
 
96
                    hmodK32 = GetModuleHandleA( "KERNEL32.DLL" );
96
97
                    if (hmodK32 != NULL)
97
98
                    {
98
99
                        pfnVerifyVersionInfoA = (PFnVerifyVersionInfoA)GetProcAddress( hmodK32, "VerifyVersionInfoA") ;
354
355
      pace = (ACCESS_ALLOWED_ACE *)HeapAlloc(
355
356
            GetProcessHeap(),
356
357
            HEAP_ZERO_MEMORY,
357
 
            sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) -
358
 
                  sizeof(DWORD));
 
358
            sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) - sizeof(DWORD)
 
359
      );
359
360
 
360
361
      if (pace == NULL)
361
362
         throw;
362
363
 
363
364
      pace->Header.AceType  = ACCESS_ALLOWED_ACE_TYPE;
364
365
      pace->Header.AceFlags = CONTAINER_INHERIT_ACE |
365
 
                   INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE;
366
 
      pace->Header.AceSize  = sizeof(ACCESS_ALLOWED_ACE) +
367
 
                   GetLengthSid(psid) - sizeof(DWORD);
 
366
                              INHERIT_ONLY_ACE |
 
367
                              OBJECT_INHERIT_ACE;
 
368
      pace->Header.AceSize  = (WORD)sizeof(ACCESS_ALLOWED_ACE) + 
 
369
                              (WORD)GetLengthSid(psid) - 
 
370
                              (WORD)sizeof(DWORD);
368
371
      pace->Mask            = GENERIC_ALL;
369
372
 
370
373
      if (!CopySid(GetLengthSid(psid), &pace->SidStart, psid))
650
653
 
651
654
BOOL
652
655
GetAccountSid(
653
 
    LPCTSTR SystemName,
654
 
    LPCTSTR AccountName,
 
656
    LPCSTR SystemName,
 
657
    LPCSTR AccountName,
655
658
    PSID *Sid
656
659
    )
657
660
{
658
 
    LPTSTR ReferencedDomain=NULL;
 
661
    LPSTR ReferencedDomain=NULL;
659
662
    DWORD cbSid=128;    // initial allocation attempt
660
663
    DWORD cchReferencedDomain=16; // initial allocation size
661
664
    SID_NAME_USE peUse;
670
673
 
671
674
        if(*Sid == NULL) throw;
672
675
 
673
 
        ReferencedDomain = (LPTSTR)HeapAlloc(
 
676
        ReferencedDomain = (LPSTR)HeapAlloc(
674
677
                        GetProcessHeap(),
675
678
                        0,
676
 
                        cchReferencedDomain * sizeof(TCHAR)
 
679
                        cchReferencedDomain * sizeof(CHAR)
677
680
                        );
678
681
 
679
682
        if(ReferencedDomain == NULL) throw;
681
684
        //
682
685
        // Obtain the SID of the specified account on the specified system.
683
686
        //
684
 
        while(!LookupAccountName(
 
687
        while(!LookupAccountNameA(
685
688
                        SystemName,         // machine to lookup account on
686
689
                        AccountName,        // account to lookup
687
690
                        *Sid,               // SID of interest
702
705
                            );
703
706
                if(*Sid == NULL) throw;
704
707
 
705
 
                ReferencedDomain = (LPTSTR)HeapReAlloc(
 
708
                ReferencedDomain = (LPSTR)HeapReAlloc(
706
709
                            GetProcessHeap(),
707
710
                            0,
708
711
                            ReferencedDomain,
709
 
                            cchReferencedDomain * sizeof(TCHAR)
 
712
                            cchReferencedDomain * sizeof(CHAR)
710
713
                            );
711
714
                if(ReferencedDomain == NULL) throw;
712
715
            }
738
741
    return bSuccess;
739
742
}
740
743
 
741
 
// Suspend or resume the threads in a given process.
 
744
// signature of OpenThread()
 
745
//
 
746
typedef HANDLE (WINAPI *tOT)(
 
747
    DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId
 
748
);
 
749
 
 
750
// Suspend or resume the threads in a given process,
 
751
// but don't suspend 'calling_thread'.
 
752
//
742
753
// The only way to do this on Windows is to enumerate
743
754
// all the threads in the entire system,
744
755
// and find those belonging to the process (ugh!!)
745
756
//
746
757
 
747
 
// OpenThread
748
 
typedef HANDLE (WINAPI *tOT)(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId);
749
 
 
750
 
int suspend_or_resume_threads(DWORD pid, bool resume) { 
 
758
int suspend_or_resume_threads(
 
759
    DWORD pid, DWORD calling_thread_id, bool resume
 
760
) { 
751
761
    HANDLE threads, thread;
752
 
    HMODULE hKernel32Lib = NULL;
 
762
    static HMODULE hKernel32Lib = NULL;
753
763
    THREADENTRY32 te = {0}; 
754
 
    tOT pOT = NULL;
 
764
    static tOT pOT = NULL;
755
765
 
756
766
    // Dynamically link to the proper function pointers.
757
 
    hKernel32Lib = GetModuleHandle("kernel32.dll");
758
 
    pOT = (tOT) GetProcAddress( hKernel32Lib, "OpenThread" );
 
767
    if (!hKernel32Lib) {
 
768
        hKernel32Lib = GetModuleHandleA("kernel32.dll");
 
769
    }
 
770
    if (!pOT) {
 
771
        pOT = (tOT) GetProcAddress( hKernel32Lib, "OpenThread" );
 
772
    }
759
773
 
760
774
    if (!pOT) {
761
775
        return -1;
771
785
    }
772
786
 
773
787
    do { 
 
788
        if (!diagnostics_is_thread_exempt_suspend(te.th32ThreadID)) continue;
 
789
        if (te.th32ThreadID == calling_thread_id) continue;
774
790
        if (te.th32OwnerProcessID == pid) {
775
791
            thread = pOT(THREAD_SUSPEND_RESUME, FALSE, te.th32ThreadID);
776
792
            resume ?  ResumeThread(thread) : SuspendThread(thread);
829
845
        if (hkSetupHive) RegCloseKey(hkSetupHive);
830
846
    if (lpszRegistryValue) free(lpszRegistryValue);
831
847
}
 
848
 
 
849
 
 
850
// return true if running under remote desktop
 
851
// (in which case CUDA apps don't work)
 
852
//
 
853
typedef BOOL (__stdcall *tWTSQSI)( IN HANDLE, IN DWORD, IN DWORD, OUT LPTSTR*, OUT DWORD* );
 
854
typedef VOID (__stdcall *tWTSFM)( IN PVOID );
 
855
 
 
856
bool is_remote_desktop() {
 
857
    static HMODULE wtsapi32lib = NULL;
 
858
    static tWTSQSI pWTSQSI = NULL;
 
859
    static tWTSFM pWTSFM = NULL;
 
860
    LPTSTR pBuf = NULL;
 
861
    DWORD dwLength;
 
862
 
 
863
    if (!wtsapi32lib) {
 
864
        wtsapi32lib = LoadLibrary(_T("wtsapi32.dll"));
 
865
        if (wtsapi32lib) {
 
866
            pWTSQSI = (tWTSQSI)GetProcAddress(wtsapi32lib, "WTSQuerySessionInformationA");
 
867
            pWTSFM = (tWTSFM)GetProcAddress(wtsapi32lib, "WTSFreeMemory");
 
868
        }
 
869
    }
 
870
 
 
871
    // WTSQuerySessionInformation(
 
872
    //   WTS_CURRENT_SERVER_HANDLE,
 
873
    //   WTS_CURRENT_SESSION,
 
874
    //   WTSClientProtocolType,
 
875
    //   &pBuf,
 
876
    //   &dwLength
 
877
    // );
 
878
    if (pWTSQSI) {
 
879
        if (pWTSQSI(
 
880
            (HANDLE)NULL,
 
881
            (DWORD)-1,
 
882
            (DWORD)16,
 
883
            &pBuf,
 
884
            &dwLength
 
885
        )) {
 
886
            USHORT prot = *(USHORT*)pBuf;
 
887
            pWTSFM(pBuf);
 
888
            if (prot == 2) return true;
 
889
        }
 
890
    }
 
891
 
 
892
    return false;
 
893
}
 
894