void clipsrv(void *foo) { HAB hab; HMQ hmq; ULONG Cmd; ULONG len; char *text; void *shmem = "the text"; hab = NULLHANDLE; if ((WinOpenClipbrd(hab) == TRUE) && ((text = (char *) WinQueryClipbrdData(hab, CF_TEXT)) != 0)) { DosGetSharedMem(text, PAG_READ); len = strlen(text); puts(text); } WinCloseClipbrd(hab); len = strlen(shmem); if (len) { DosAllocSharedMem((void **)&text, 0, len + 1, PAG_WRITE | PAG_COMMIT | OBJ_GIVEABLE | OBJ_GETTABLE); strcpy(text, shmem); } if (WinOpenClipbrd(hab) == TRUE) { if (!WinSetClipbrdData(hab, (ULONG) text, CF_TEXT, CFI_POINTER)) DosBeep(100, 1500); WinCloseClipbrd(hab); } }
MRESULT khs_umStoreMsg( HWND hwnd, MPARAM mp1, MPARAM mp2 ) { PKHSCD pkhscd = WinQueryWindowPtr( hwnd, 0 ); DosGetSharedMem( mp1, PAG_READ | PAG_WRITE ); fprintf( pkhscd->fp, "%s\n", ( PSZ )mp1 ); fflush( pkhscd->fp ); return 0; }
APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m, apr_os_shm_t *osshm, apr_pool_t *pool) { int rc; apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t)); ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE; newm->pool = pool; rc = DosGetSharedMem(&(newm->memblock), flags); if (rc) { return APR_FROM_OS_ERROR(rc); } *m = newm; return APR_SUCCESS; }
static DWORD WINAPI giveMemThread(LPVOID) { DWORD pid=GetCurrentProcessId(); char npname[256]; wsprintf(npname,"\\\\.\\pipe\\os2sharemem_give_ipc_%08x",pid); HANDLE hPipe=CreateNamedPipe(npname, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT, 1, 128, 128, 150, NULL); if(hPipe==INVALID_HANDLE_VALUE) { ExitThread(0); } for(;;) { BOOL b=ConnectNamedPipe(hPipe,NULL); if(!b) { CloseHandle(hPipe); ExitThread(0); } struct { PVOID objectbaseaddress; ULONG flag; } s; DWORD bytesRead; b=ReadFile(hPipe, &s, sizeof(s), &bytesRead, NULL); if(b && bytesRead==4) { APIRET rc=DosGetSharedMem(s.objectbaseaddress,s.flag); DWORD bytesWritten; WriteFile(hPipe, &rc, sizeof(rc), &bytesWritten, NULL); } DisconnectNamedPipe(hPipe); } //CloseHandle(hPipe); //ExitThread(0); }
APIRET os2APIENTRY DosGetNamedSharedMem(PPVOID ppb, PCSZ pszName, ULONG flag) { if(!pszName) return 8; PoolLock pl; SM_Pool *ppool=lockPool(&pl); for(int i=0; i<MAXSHAREDMEMOBJECTS; i++) { if(ppool->SM_Object[i].SM_ReferenceCount && stricmp(ppool->SM_Object[i].SM_Name,pszName)==0) break; } if(i>=MAXSHAREDMEMOBJECTS) { unlockPool(&pl); return 2; //error_file_not_found } *ppb = (PVOID)ppool->SM_Object[i].SM_BaseAddress; unlockPool(&pl); return DosGetSharedMem(*ppb,flag); }
MRESULT EXPENTRY fnwpMain(HWND hwndFrame, ULONG msg, MPARAM mp1, MPARAM mp2) { PSZ szData; PDDESTRUCT pddeStruct; ULONG mem; CHAR szBuffer[200]; switch (msg) { // all answers to the WinDDEInitate call arrive here case WM_DDE_INITIATEACK: { PDDEINIT pddeInit; PSZ szInApp, szInTopic; static BOOL bNetscapeAnswered = FALSE; pddeInit = (PDDEINIT)mp2; szInApp = pddeInit->pszAppName; szInTopic = pddeInit->pszTopic; G_hServerWnd = (HWND)mp1; ShowMessage("WM_DDE_INITIATEACK (resp to WinDDEInitiate)"); ShowMessage(" application: \"%s\"", pddeInit->pszAppName); ShowMessage(" topic: \"%s\"", pddeInit->pszTopic); // RDP 2000-07-07 07:24:18 // There was no check on which application responded. // This made NETSCDDE fail when another DDE-aware application, // like EPM, was running. // Now the handle from mp1 is only assigned if the application // responding is Netscape. // If the app is not Netscape then the handle is nullified. // I don't know if assigning 0 to the handle is correct but // is seems to solve the problem. // V0.9.19 (2002-03-28) [umoeller] // Opera fix: use stricmp instead of strcmp if (!stricmp(pddeInit->pszAppName, G_szDDENetscape)) // V0.9.16 (2001-10-02) [umoeller] { // ShowMessage("!! Netscape answered."); G_hServerWnd = (HWND)mp1; bNetscapeAnswered = TRUE; } else { // ShowMessage("!! Other application aswered."); G_hServerWnd = (HWND)0; } } break; // all answers to DDE requests arrive here case WM_DDE_DATA: { ShowMessage("!! Received data from Netscape: "); pddeStruct = (PDDESTRUCT) mp2; DosGetSharedMem(pddeStruct, PAG_READ | PAG_WRITE); szData = (BYTE *) (pddeStruct + (pddeStruct->offabData)); ShowMessage(szData); } break; // menu item processing (in debug mode, otherwise these // WM_COMMAND msgs have been posted automatically) case WM_COMMAND: switch (SHORT1FROMMP(mp1)) { // start DDE conversation: this was posted // by "main" before the PM loop was entered // (even if we're in debug mode) case IDM_INITIATE: // WinPostMsg(G_hwndListbox, LM_DELETEALL, 0, 0); ShowMessage("IDM_INITIATE:"); ShowMessage("Topic: \"%s\"", G_szOpenURLTopic); G_context.cb = sizeof(CONVCONTEXT); G_context.fsContext = 0; WinDdeInitiate(hwndFrame, G_szDDENetscape, G_szOpenURLTopic, &G_context); if (!G_optDebug) // if we're not in debug mode, post subsequent // menu commands automatically WinPostMsg(hwndFrame, WM_COMMAND, MPFROM2SHORT(IDM_CHAIN2, 0), 0); break; // "Open URL": request data from server case IDM_OPENURL: { ShowMessage("IDM_OPENURL"); ShowMessage(" URL: \"%s\"", G_szURL); strlcpy(szBuffer, G_szURL, sizeof(szBuffer)); strlcat(szBuffer, ",,0xFFFFFFFF,0x0", sizeof(szBuffer)); DDERequest(hwndFrame, szBuffer); } break; // "Open URL in new window": request data from server, // but with different parameters case IDM_OPENURLNEW: { ShowMessage("IDM_OPENURLNEW"); ShowMessage(" URL: \"%s\"", G_szURL); strlcpy(szBuffer, G_szURL, sizeof(szBuffer)); strlcat(szBuffer, ",,0x0,0x0", sizeof(szBuffer)); DDERequest(hwndFrame, szBuffer); } break; /* * IDM_CHAIN2: * this is posted after DDE_INITIATE was * successful */ case IDM_CHAIN2: { if (G_optNewWindow) WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_OPENURLNEW, 0), 0); else WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_OPENURL, 0), 0); WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_CHAIN3, 0), 0); } break; /* * IDM_CHAIN3: * this is posted to close the whole thing; we just need * another msg before going for IDM_CLOSE, or some DDE * msgs might get lost */ case IDM_CHAIN3: WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_CLOSE, 0), 0); break; case IDM_FULLSEQUENCE: WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_INITIATE, 0), 0); WinPostMsg(G_hwndDebug, WM_COMMAND, MPFROM2SHORT(IDM_CHAIN2, 0), 0); break; /* * IDM_CLOSE: * this is posted to close the whole thing */ case IDM_CLOSE: WinDdePostMsg(G_hServerWnd, hwndFrame, WM_DDE_TERMINATE, NULL, DDEPM_RETRY); ShowMessage("DDE connection closed."); if (!G_optDebug) WinPostMsg(hwndFrame, WM_COMMAND, MPFROM2SHORT(IDM_DELAYEXIT, 0), 0); break; /* * IDM_DELAYEXIT: * this is posted after IDM_CLOSE; we will now * check for whether the DDE conversation with * Netscape was successful and, if not, start * a new instance of Netscape according to the * command line parameters */ case IDM_DELAYEXIT: { if ( (!G_NetscapeFound) && (G_optExecute) ) { CHAR szStart[256]; GetNLSString(szStart, sizeof(szStart), ID_NDSI_STARTNETSCAPE); // confirm start netscape if ( (!G_optConfirmStart) // get rid of this hideously ugly dialog || (WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szStart, (PSZ)NETSCDDE_TITLE, 0, MB_YESNO | MB_MOVEABLE) == MBID_YES) /* WinCenteredDlgBox(HWND_DESKTOP, G_hwndDebug, WinDefDlgProc, G_hmodNLS, ID_NDD_QUERYSTART, NULL) == DID_OK) */ ) { UCHAR achObjBuf[256] = ""; CHAR szArgs[CCHMAXPATH]; HWND hwndNotify = HWND_DESKTOP; PROGDETAILS pd; HAPP happ; // destroy "Contacting", create "Starting Netscape" // window WinDestroyWindow(G_hwndContacting); G_hwndContacting = NULLHANDLE; if (!G_optQuiet) { G_hwndContacting = WinLoadDlg(HWND_DESKTOP, G_hwndDebug, WinDefDlgProc, G_hmodNLS, ID_NDD_STARTING, 0); WinShowWindow(G_hwndContacting, TRUE); } strlcpy(szArgs, G_szNetscapeParams, sizeof(szArgs)); strlcat(szArgs, " ", sizeof(szArgs)); strlcat(szArgs, G_szURL, sizeof(szArgs)); // now start app memset(&pd, 0, sizeof(pd)); pd.Length = sizeof(pd); pd.progt.progc = PROG_DEFAULT; pd.progt.fbVisible = SHE_VISIBLE; pd.pszExecutable = G_szNetscapeApp; pd.pszParameters = szArgs; pd.pszStartupDir = G_szStartupDir; if (!(happ = WinStartApp(NULLHANDLE, &pd, szArgs, NULL, SAF_INSTALLEDCMDLINE))) { DisplayError("WinStartApp failed for app \"%s\", params \"%s\", startup dir \"%s\"", G_szNetscapeApp, szArgs, G_szStartupDir); } } } // keep "Contacting" / "Starting" window visible for two seconds G_idTimer = WinStartTimer(G_hab, hwndFrame, 1, 2000); break; } // User closes the window case IDM_EXIT: WinPostMsg(hwndFrame, WM_CLOSE, 0, 0); break; } break; case WM_TIMER: // after two seconds, close status window WinStopTimer(G_hab, hwndFrame, G_idTimer); WinPostMsg(hwndFrame, WM_CLOSE, 0, 0); break; // Send the message to the usual WC_FRAME WndProc default: return G_SysWndProc(hwndFrame, msg, mp1, mp2); } return FALSE; }
static void * tf (void *arg) { if (pthread_mutex_lock (&lock)) { puts ("1st locking of lock failed"); exit (1); } struct flock fl = { .l_type = F_WRLCK, .l_start = 0, .l_whence = SEEK_SET, .l_len = 10 }; if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0) { puts ("fourth fcntl failed"); exit (1); } pthread_mutex_unlock (&lock); pthread_mutex_lock (&lock2); return NULL; } static int do_test (void) { fd = create_temp_file("tst-flock2-", NULL); if (fd == -1) { puts ("create_temp_file failed"); return 1; } int i; for (i = 0; i < 20; ++i) write (fd, "foobar xyzzy", 12); pthread_barrier_t *b; #ifdef __EMX__ APIRET arc; arc = DosAllocSharedMem ((PPVOID) &b, NULL, sizeof (pthread_barrier_t), PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_GETTABLE); if (arc) { puts ("DosAllocSharedMem failed"); return 1; } b->hmtx = NULLHANDLE; arc = DosCreateMutexSem (NULL, &b->hmtx, DC_SEM_SHARED, FALSE); if (arc) { puts ("DosCreateMutexSem failed"); return 1; } b->hev = NULLHANDLE; arc = DosCreateEventSem (NULL, &b->hev, DC_SEM_SHARED | DCE_AUTORESET, FALSE); if (arc) { puts ("DosCreateEventSem failed"); return 1; } b->cnt = 0; b->cnt_max = 2; #else b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (b == MAP_FAILED) { puts ("mmap failed"); return 1; } pthread_barrierattr_t ba; if (pthread_barrierattr_init (&ba) != 0) { puts ("barrierattr_init failed"); return 1; } if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0) { puts ("barrierattr_setpshared failed"); return 1; } if (pthread_barrier_init (b, &ba, 2) != 0) { puts ("barrier_init failed"); return 1; } if (pthread_barrierattr_destroy (&ba) != 0) { puts ("barrierattr_destroy failed"); return 1; } #endif struct flock fl = { .l_type = F_WRLCK, .l_start = 0, .l_whence = SEEK_SET, .l_len = 10 }; if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0) { puts ("first fcntl failed"); return 1; } pid_t pid = fork (); if (pid == -1) { puts ("fork failed"); return 1; } if (pid == 0) { #ifdef __EMX__ arc = DosGetSharedMem (b, PAG_READ | PAG_WRITE); if (arc) { puts ("DosGetSharedMem failed"); return 1; } arc = DosOpenMutexSem (NULL, &b->hmtx); if (arc) { puts ("DosOpenMutexSem failed"); return 1; } arc = DosOpenEventSem (NULL, &b->hev); if (arc) { puts ("DosOpenEventSem failed"); return 1; } #endif /* Make sure the child does not stay around indefinitely. */ alarm (10); /* Try to get the lock. */ if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0) { puts ("child: second flock succeeded"); return 1; } } pthread_barrier_wait (b); if (pid != 0) { fl.l_type = F_UNLCK; if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0) { puts ("third fcntl failed"); return 1; } } pthread_barrier_wait (b); pthread_t th; if (pid == 0) { if (pthread_mutex_lock (&lock2) != 0) { puts ("1st locking of lock2 failed"); return 1; } if (pthread_create (&th, NULL, tf, NULL) != 0) { puts ("pthread_create failed"); return 1; } /* Let the new thread run. */ sleep (1); if (pthread_mutex_lock (&lock) != 0) { puts ("2nd locking of lock failed"); return 1; } puts ("child locked file"); } pthread_barrier_wait (b); if (pid != 0) { fl.l_type = F_WRLCK; if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0) { puts ("fifth fcntl succeeded"); return 1; } puts ("file locked by child"); } pthread_barrier_wait (b); if (pid == 0) { if (pthread_mutex_unlock (&lock2) != 0) { puts ("unlock of lock2 failed"); return 1; } if (pthread_join (th, NULL) != 0) { puts ("join failed"); return 1; } puts ("child's thread terminated"); } pthread_barrier_wait (b); if (pid != 0) { fl.l_type = F_WRLCK; if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLK, &fl)) == 0) { puts ("fifth fcntl succeeded"); return 1; } puts ("file still locked"); } pthread_barrier_wait (b); if (pid == 0) { _exit (0); } int status; if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) { puts ("waitpid failed"); return 1; } puts ("child terminated"); if (TEMP_FAILURE_RETRY (fcntl (fd, F_SETLKW, &fl)) != 0) { puts ("sixth fcntl failed"); return 1; } return status; }