static void service_process(void (WINAPI *p_service_main)(DWORD, char **)) { BOOL res; SERVICE_TABLE_ENTRYA servtbl[] = { {service_name, p_service_main}, {NULL, NULL} }; res = WaitNamedPipeA(named_pipe_name, NMPWAIT_USE_DEFAULT_WAIT); if(!res) return; pipe_handle = CreateFileA(named_pipe_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(pipe_handle == INVALID_HANDLE_VALUE) return; service_trace("Starting..."); service_stop_event = CreateEventA(NULL, TRUE, FALSE, NULL); service_ok(service_stop_event != NULL, "Could not create event: %u\n", GetLastError()); if(!service_stop_event) return; res = StartServiceCtrlDispatcherA(servtbl); service_ok(res, "StartServiceCtrlDispatcher failed: %u\n", GetLastError()); /* Let service thread terminate */ Sleep(50); CloseHandle(service_stop_event); CloseHandle(pipe_handle); }
/*! \brief Send a message to server instance */ void QInterProcessChannel::sendMessage(const QByteArray& msg) { if (msg.count()) { // a lot of simultaneous clients cause that pipe sometimes omits // some of these clients despite results of writing functions are ok ;/ // workaround: reserve mutex, wait 100ms before every write operation HANDLE mutex = CreateMutexA(NULL, FALSE, qPrintable(blockerMutexStr)); WaitForSingleObject(mutex, INFINITE); Sleep(100); HANDLE hPipe; // loop to get pipe's handle for (;;) { hPipe = CreateFileA(qPrintable(pipeName), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hPipe != INVALID_HANDLE_VALUE) break; if (GetLastError() != ERROR_PIPE_BUSY) { ReleaseMutex(mutex); return; } // wait max 1s and retry WaitNamedPipeA(qPrintable(pipeName), 1000); } // ok, now we have pipe's handle, we can prepare buffers and write to the // pipe char buffer[bufferSize]; strncpy(buffer, msg.constData(), bufferSize); buffer[bufferSize - 1] = 0; DWORD bytesWritten; bool writeSuccess = WriteFile(hPipe, buffer, strlen(buffer) + 1, &bytesWritten, NULL); if (!writeSuccess) { ReleaseMutex(mutex); return; } CloseHandle(hPipe); ReleaseMutex(mutex); } else { // qWarning("::sendMessage(%d): Empty messages are not carried out...", // GetCurrentProcessId()); } }
char* rdp_rds_module_start(RDS_MODULE_COMMON* module) { BOOL status; rdsModuleRdp* rdp; char lpCommandLine[256]; const char* endpoint = "RDP"; long xres, yres,colordepth; char* pipeName = (char*) malloc(256); rdp = (rdsModuleRdp*) module; rdp->SessionId = rdp->commonModule.sessionId; WLog_Print(rdp->log, WLOG_DEBUG, "RdsModuleStart: SessionId: %d Endpoint: %s", (int) rdp->commonModule.sessionId, endpoint); freerds_named_pipe_get_endpoint_name((int) rdp->commonModule.sessionId, endpoint, pipeName, 256); freerds_named_pipe_clean(pipeName); ZeroMemory(&(rdp->si), sizeof(STARTUPINFO)); rdp->si.cb = sizeof(STARTUPINFO); ZeroMemory(&(rdp->pi), sizeof(PROCESS_INFORMATION)); initResolutions(rdp->commonModule.baseConfigPath, &g_Config, &rdp->commonModule.envBlock, &xres, &yres, &colordepth); sprintf_s(lpCommandLine, sizeof(lpCommandLine), "%s /tmp/rds.rdp /size:%dx%d", "freerds-rdp", (int) xres, (int) yres); WLog_Print(rdp->log, WLOG_DEBUG, "Starting process with command line: %s", lpCommandLine); status = CreateProcessA(NULL, lpCommandLine, NULL, NULL, FALSE, 0, rdp->commonModule.envBlock, NULL, &(rdp->si), &(rdp->pi)); WLog_Print(rdp->log, WLOG_DEBUG, "Process created with status: %d", status); if (!WaitNamedPipeA(pipeName, 5 * 1000)) { fprintf(stderr, "WaitNamedPipe failure: %s\n", pipeName); return NULL; } return pipeName; }
static int tp_npipe_open(pbRPCTransportContext* context, int timeout) { HANDLE hNamedPipe = 0; char pipeName[] = "\\\\.\\pipe\\FreeRDS_SessionManager"; NpTransportContext *np = (NpTransportContext *) context; if (!WaitNamedPipeA(pipeName, timeout)) { return -1; } hNamedPipe = CreateFileA(pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if ((!hNamedPipe) || (hNamedPipe == INVALID_HANDLE_VALUE)) { return -1; } np->handle = hNamedPipe; return 0; }
bool MWinNamedPipeClient::Create(const char *pipename,DWORD waittime,bool timeouterror) { Destroy(); if (WaitNamedPipeA(pipename, waittime) == FALSE) { if(timeouterror==true) { MStdPrintf("**Unable to connect to Pipe Server\n"); } Destroy(); return false; } mhPipe=CreateFileA(pipename,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if(mhPipe==INVALID_HANDLE_VALUE) { Destroy(); return false; } return true; }
int tp_npipe_open(pbRPCContext* context, int timeout) { HANDLE hNamedPipe = 0; char pipeName[] = "\\\\.\\pipe\\FreeRDS_Manager"; if (!WaitNamedPipeA(pipeName, timeout)) { return -1; } hNamedPipe = CreateFileA(pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if ((!hNamedPipe) || (hNamedPipe == INVALID_HANDLE_VALUE)) { return -1; } context->hPipe = hNamedPipe; return 0; }
static HANDLE RPCSS_NPConnect(void) { HANDLE the_pipe; DWORD dwmode, wait_result; HANDLE master_mutex = RPCSS_GetMasterMutex(); WINE_TRACE("\n"); while (TRUE) { wait_result = WaitForSingleObject(master_mutex, MASTER_MUTEX_TIMEOUT); switch (wait_result) { case WAIT_ABANDONED: case WAIT_OBJECT_0: break; case WAIT_FAILED: case WAIT_TIMEOUT: default: WINE_ERR("This should never happen: couldn't enter mutex.\n"); return NULL; } /* try to open the client side of the named pipe. */ the_pipe = CreateFileA( NAME_RPCSS_NAMED_PIPE, /* pipe name */ GENERIC_READ | GENERIC_WRITE, /* r/w access */ 0, /* no sharing */ NULL, /* no security attributes */ OPEN_EXISTING, /* open an existing pipe */ 0, /* default attributes */ NULL /* no template file */ ); if (the_pipe != INVALID_HANDLE_VALUE) break; if (GetLastError() != ERROR_PIPE_BUSY) { WINE_WARN("Unable to open named pipe %s (assuming unavailable).\n", wine_dbgstr_a(NAME_RPCSS_NAMED_PIPE)); break; } WINE_WARN("Named pipe busy (will wait)\n"); if (!ReleaseMutex(master_mutex)) WINE_ERR("Failed to release master mutex. Expect deadlock.\n"); /* wait for the named pipe. We are only willing to wait only 5 seconds. It should be available /very/ soon. */ if (! WaitNamedPipeA(NAME_RPCSS_NAMED_PIPE, MASTER_MUTEX_WAITNAMEDPIPE_TIMEOUT)) { WINE_ERR("Named pipe unavailable after waiting. Something is probably wrong.\n"); return NULL; } } if (the_pipe != INVALID_HANDLE_VALUE) { dwmode = PIPE_READMODE_MESSAGE; /* SetNamedPipeHandleState not implemented ATM, but still seems to work somehow. */ if (! SetNamedPipeHandleState(the_pipe, &dwmode, NULL, NULL)) WINE_WARN("Failed to set pipe handle state\n"); } if (!ReleaseMutex(master_mutex)) WINE_ERR("Uh oh, failed to leave the RPC Master Mutex!\n"); return the_pipe; }
void try_add_route_with_wait(void) { DWORD result; HANDLE h_pipe; struct rt_msghdr *msg; struct sockaddr_storage *pss; struct sockaddr_in *psin; int msgsize; int nbytes; int i; if (!WaitNamedPipeA(XORPRTM_PIPENAME, NMPWAIT_USE_DEFAULT_WAIT)) { fprintf(stderr, "No named pipe instances available.\n"); return; } h_pipe = CreateFileA(XORPRTM_PIPENAME, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (h_pipe == INVALID_HANDLE_VALUE) { result = GetLastError(); fprintf(stderr, "error opening pipe: %d\n", result); return; } fprintf(stderr, "connected\n"); msgsize = sizeof(*msg) + (sizeof(struct sockaddr_storage) * 3); msg = malloc(msgsize); if (msg == NULL) { fprintf(stderr, "cannot allocate routing socket message\n"); CloseHandle(h_pipe); return; } ZeroMemory(msg, msgsize); /* Fill out routing message header */ msg->rtm_type = RTM_ADD; msg->rtm_msglen = msgsize; msg->rtm_version = RTM_VERSION; msg->rtm_addrs |= RTA_DST | RTA_NETMASK | RTA_GATEWAY; msg->rtm_pid = GetCurrentProcessId(); pss = (struct sockaddr_storage *)(msg + 1); /* Fill out destination XXX 192.0.2.0 in little endian */ psin = (struct sockaddr_in *)pss; psin->sin_family = AF_INET; psin->sin_addr.s_addr = 0x000200C0; /* Fill out next-hop XXX 192.168.123.6 in little endian */ psin = (struct sockaddr_in *)++pss; psin->sin_family = AF_INET; psin->sin_addr.s_addr = 0x067BA8C0; /* Fill out netmask XXX 255.255.255.0 in little endian */ psin = (struct sockaddr_in *)++pss; psin->sin_family = AF_INET; psin->sin_addr.s_addr = 0x00FFFFFF; /* Try to add a route 3 times to test callbacks */ for (i = 0; i < 3; i++) { fprintf(stderr, "attempting to add a route\n", GetLastError()); result = WriteFile(h_pipe, msg, msgsize, &nbytes, NULL); if (result == 0) { fprintf(stderr, "error %d writing to pipe\n", GetLastError()); } else { fprintf(stderr, "sent request %d\n", i); print_rtmsg(msg, msgsize); } /* Block and read a single reply. */ result = ReadFile(h_pipe, msg, msgsize, &nbytes, NULL); if (result == 0) { fprintf(stderr, "error %d reading from pipe\n", GetLastError()); } else { fprintf(stderr, "got reply %d, printing\n", i); print_rtmsg(msg, msgsize); } } fprintf(stderr, "done\n"); CloseHandle(h_pipe); free(msg); }
CPipe::CPipe(char *szName, DWORD dWait){ int iTemp; int iFlag = 1; WCHAR wbuffer[MAX_PATH+1]; dTHX; hPipe = 0; dBufferSize = BUFFER_SIZE; dBytes = 0; char szPipeName[PIPE_NAME_SIZE + 1]; dwOpenMode = PIPE_ACCESS_DUPLEX; dwPipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT; nMaxInstances = PIPE_UNLIMITED_INSTANCES; nOutBufferSize = dBufferSize; nInBufferSize = dBufferSize; nDefaultTimeOut = PIPE_TIMEOUT; lpSecurityAttributes= NULL; iError = 0; strcpy((char *)szError, ""); cBuffer = new char [dBufferSize]; if (! cBuffer){ dBufferSize = 0; } memset((void *)szError, 0, ERROR_TEXT_SIZE); memset((void *)szPipeName, 0, PIPE_NAME_SIZE + 1); if (strncmp((char *)szName, "\\\\", 2) == 0){ iPipeType = CLIENT; iTemp = 0; }else{ iPipeType = SERVER; strcpy(szPipeName, PIPE_NAME_PREFIX); iTemp = strlen(PIPE_NAME_PREFIX); } strncat(szPipeName, szName, PIPE_NAME_SIZE - iTemp); if (USING_WIDE()) { A2WHELPER(szPipeName, wbuffer, sizeof(wbuffer)); } if(iPipeType == SERVER){ if (USING_WIDE()) { hPipe = CreateNamedPipeW(wbuffer, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes); } else { hPipe = CreateNamedPipeA(szPipeName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes); } }else{ while(iFlag){ if (USING_WIDE()) { hPipe = CreateFileW(wbuffer, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL); } else { hPipe = CreateFileA(szPipeName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL); } if (GetLastError() == ERROR_PIPE_BUSY){ if (USING_WIDE()) iFlag = WaitNamedPipeW(wbuffer, dWait); else iFlag = WaitNamedPipeA(szPipeName, dWait); }else{ iFlag = 0; } } } if (cBuffer == 0){ iError = 998; strcpy((char *)szError, "Could not allocate a buffer for the pipe connection"); } if (hPipe == INVALID_HANDLE_VALUE){ iError = 999; strcpy((char *)szError, "Could not connect"); delete this; } }
static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait) { RpcConnection_np *npc = (RpcConnection_np *) Connection; HANDLE pipe; DWORD err, dwMode; TRACE("connecting to %s\n", pname); while (TRUE) { DWORD dwFlags = 0; if (Connection->QOS) { dwFlags = SECURITY_SQOS_PRESENT; switch (Connection->QOS->qos->ImpersonationType) { case RPC_C_IMP_LEVEL_DEFAULT: /* FIXME: what to do here? */ break; case RPC_C_IMP_LEVEL_ANONYMOUS: dwFlags |= SECURITY_ANONYMOUS; break; case RPC_C_IMP_LEVEL_IDENTIFY: dwFlags |= SECURITY_IDENTIFICATION; break; case RPC_C_IMP_LEVEL_IMPERSONATE: dwFlags |= SECURITY_IMPERSONATION; break; case RPC_C_IMP_LEVEL_DELEGATE: dwFlags |= SECURITY_DELEGATION; break; } if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC) dwFlags |= SECURITY_CONTEXT_TRACKING; } pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwFlags, 0); if (pipe != INVALID_HANDLE_VALUE) break; err = GetLastError(); if (err == ERROR_PIPE_BUSY) { TRACE("connection failed, error=%x\n", err); return RPC_S_SERVER_TOO_BUSY; } if (!wait) return RPC_S_SERVER_UNAVAILABLE; if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) { err = GetLastError(); WARN("connection failed, error=%x\n", err); return RPC_S_SERVER_UNAVAILABLE; } } /* success */ memset(&npc->ovl, 0, sizeof(npc->ovl)); /* pipe is connected; change to message-read mode. */ dwMode = PIPE_READMODE_MESSAGE; SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL); npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL); npc->pipe = pipe; return RPC_S_OK; }