MU_TEST(client_server, handle_invalidation) { LWMsgContext* context = NULL; LWMsgProtocol* protocol = NULL; LWMsgPeer* client = NULL; LWMsgPeer* server = NULL; CounterHandle* handle = NULL; CounterRequest request; LWMsgCall* call; LWMsgSession* session = NULL; LWMsgParams in = LWMSG_PARAMS_INITIALIZER; LWMsgParams out = LWMSG_PARAMS_INITIALIZER; LWMsgHandleType locality = 0; struct timespec ts = {0, 50000000}; MU_TRY(lwmsg_context_new(NULL, &context)); MU_TRY(lwmsg_protocol_new(context, &protocol)); MU_TRY(lwmsg_protocol_add_protocol_spec(protocol, counterprotocol_spec)); MU_TRY(lwmsg_peer_new(context, protocol, &server)); MU_TRY(lwmsg_peer_add_dispatch_spec(server, counter_dispatch)); MU_TRY(lwmsg_peer_add_listen_endpoint(server, LWMSG_CONNECTION_MODE_LOCAL, TEST_ENDPOINT, 0600)); MU_TRY(lwmsg_peer_start_listen(server)); MU_TRY(lwmsg_peer_new(context, protocol, &client)); MU_TRY(lwmsg_peer_add_connect_endpoint(client, LWMSG_CONNECTION_MODE_LOCAL, TEST_ENDPOINT)); request.counter = 0; MU_TRY(lwmsg_peer_acquire_call(client, &call)); session = lwmsg_call_get_session(call); in.tag = COUNTER_OPEN; in.data = &request; MU_TRY(lwmsg_call_dispatch(call, &in, &out, NULL, NULL)); MU_ASSERT_EQUAL(MU_TYPE_INTEGER, out.tag, COUNTER_OPEN_SUCCESS); lwmsg_call_release(call); handle = out.data; MU_TRY(lwmsg_peer_stop_listen(server)); nanosleep(&ts, NULL); MU_TRY(lwmsg_peer_start_listen(server)); MU_TRY(lwmsg_peer_acquire_call(client, &call)); MU_ASSERT_EQUAL(MU_TYPE_INTEGER, lwmsg_session_get_handle_location(session, handle, &locality), LWMSG_STATUS_INVALID_HANDLE); MU_TRY(lwmsg_session_release_handle(session, handle)); MU_TRY(lwmsg_peer_disconnect(client)); lwmsg_peer_delete(client); MU_TRY(lwmsg_peer_stop_listen(server)); lwmsg_peer_delete(server); }
static NTSTATUS LwIoThreadInit( void ) { NTSTATUS Status = 0; BOOL bInLock = FALSE; LWIO_LOCK_MUTEX(bInLock, &gLock); if (!gpLwIoProtocol) { LwIoInitialize(); } if (!gbStateKeyInit) { Status = LwErrnoToNtStatus(pthread_key_create(&gStateKey, LwIoThreadStateDestruct)); BAIL_ON_NT_STATUS(Status); gbStateKeyInit = TRUE; } if (!gpProcessCreds) { Status = LwIoInitProcessCreds(); BAIL_ON_NT_STATUS(Status); } if (!gpClient) { Status = NtIpcLWMsgStatusToNtStatus(lwmsg_peer_new(NULL, gpLwIoProtocol, &gpClient)); BAIL_ON_NT_STATUS(Status); Status = NtIpcLWMsgStatusToNtStatus(lwmsg_peer_add_connect_endpoint( gpClient, LWMSG_ENDPOINT_DIRECT, "lwio")); Status = NtIpcLWMsgStatusToNtStatus(lwmsg_peer_add_connect_endpoint( gpClient, LWMSG_ENDPOINT_LOCAL, LWIO_SERVER_FILENAME)); BAIL_ON_NT_STATUS(Status); } if (!gpSession) { Status = NtIpcLWMsgStatusToNtStatus(lwmsg_peer_connect( gpClient, &gpSession)); BAIL_ON_NT_STATUS(Status); } error: LWIO_UNLOCK_MUTEX(bInLock, &gLock); return Status; }
VOID LwmEvtOpenServerOnce( VOID ) { DWORD dwError = 0; dwError = MAP_LWMSG_ERROR(lwmsg_protocol_new(NULL, &gContext.pProtocol)); BAIL_ON_EVT_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_protocol_add_protocol_spec(gContext.pProtocol, LwEvtIPCGetProtocolSpec())); BAIL_ON_EVT_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_new(NULL, gContext.pProtocol, &gContext.pClient)); BAIL_ON_EVT_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_connect_endpoint( gContext.pClient, LWMSG_ENDPOINT_DIRECT, "eventlog")); BAIL_ON_EVT_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_connect_endpoint( gContext.pClient, LWMSG_ENDPOINT_LOCAL, CACHEDIR "/" EVT_SERVER_FILENAME)); BAIL_ON_EVT_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_connect(gContext.pClient, &gContext.pSession)); BAIL_ON_EVT_ERROR(dwError); cleanup: gdwOnceError = dwError; return; error: if (gContext.pClient) { lwmsg_peer_delete(gContext.pClient); gContext.pClient = NULL; } if (gContext.pProtocol) { lwmsg_protocol_delete(gContext.pProtocol); gContext.pProtocol = NULL; } goto cleanup; }
static void __LwSmIpcCallInit( void ) { DWORD dwError = 0; dwError = MAP_LWMSG_STATUS(lwmsg_protocol_new(NULL, &gpProtocol)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_protocol_add_protocol_spec( gpProtocol, LwSmIpcGetProtocolSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_new(NULL, gpProtocol, &gpClient)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_connect_endpoint( gpClient, (LWMsgEndpointType)LWMSG_CONNECTION_MODE_LOCAL, SM_ENDPOINT)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_connect( gpClient, &gpSession)); BAIL_ON_ERROR(dwError); cleanup: gOnceError = dwError; return; error: if (gpClient) { lwmsg_peer_delete(gpClient); } if (gpProtocol) { lwmsg_protocol_delete(gpProtocol); } goto cleanup; }
static DWORD LwSmIpcInit( VOID ) { DWORD dwError = ERROR_SUCCESS; dwError = MAP_LWMSG_STATUS(lwmsg_context_new(NULL, &gState.pIpcContext)); BAIL_ON_ERROR(dwError); lwmsg_context_set_log_function( gState.pIpcContext, LwSmLogIpc, NULL); dwError = MAP_LWMSG_STATUS(lwmsg_protocol_new(gState.pIpcContext, &gState.pContainerProtocol)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_protocol_add_protocol_spec( gState.pContainerProtocol, LwSmGetContainerProtocolSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_new( gState.pIpcContext, gState.pContainerProtocol, &gState.pDirectServer)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_listen_endpoint( gState.pDirectServer, LWMSG_ENDPOINT_DIRECT, "Container", 0)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_dispatch_spec( gState.pDirectServer, LwSmGetContainerDispatchSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_start_listen(gState.pDirectServer)); BAIL_ON_ERROR(dwError); error: return dwError; }
static DWORD LwSmStartIpcServer( VOID ) { DWORD dwError = 0; SM_LOG_VERBOSE("Starting IPC server"); if (!gState.bContainer) { dwError = MAP_LWMSG_STATUS(lwmsg_protocol_new(gState.pIpcContext, &gState.pContolProtocol)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_protocol_add_protocol_spec( gState.pContolProtocol, LwSmIpcGetProtocolSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_new( gState.pIpcContext, gState.pContolProtocol, &gState.pControlServer)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_dispatch_spec( gState.pControlServer, LwSmGetDispatchSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_listen_endpoint( gState.pControlServer, LWMSG_ENDPOINT_LOCAL, SM_ENDPOINT, 0666)); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_start_listen(gState.pControlServer)); BAIL_ON_ERROR(dwError); } dwError = MAP_LWMSG_STATUS(lwmsg_peer_new( gState.pIpcContext, gState.pContainerProtocol, &gState.pContainerServer)); BAIL_ON_ERROR(dwError); if (gState.bContainer) { dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_dispatch_spec( gState.pContainerServer, LwSmGetContainerDispatchSpec())); BAIL_ON_ERROR(dwError); if (!gState.pGroup) { dwError = MAP_LWMSG_STATUS(lwmsg_peer_accept_fd( gState.pContainerServer, LWMSG_ENDPOINT_PAIR, 4)); BAIL_ON_ERROR(dwError); } else { dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_connect_endpoint( gState.pContainerServer, LWMSG_ENDPOINT_LOCAL, SC_ENDPOINT)); BAIL_ON_ERROR(dwError); dwError = LwSmContainerRegister(gState.pContainerServer, gState.pGroup); BAIL_ON_ERROR(dwError); } } else { dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_listen_endpoint( gState.pContainerServer, LWMSG_ENDPOINT_LOCAL, SC_ENDPOINT, 0666)); dwError = MAP_LWMSG_STATUS(lwmsg_peer_add_dispatch_spec( gState.pContainerServer, LwSmGetContainerRegisterDispatchSpec())); BAIL_ON_ERROR(dwError); dwError = MAP_LWMSG_STATUS(lwmsg_peer_start_listen(gState.pContainerServer)); BAIL_ON_ERROR(dwError); } cleanup: return dwError; error: goto cleanup; }
DWORD LWNetSrvStartListenThread( void ) { PSTR pszCachePath = NULL; PSTR pszCommPath = NULL; BOOLEAN bDirExists = FALSE; DWORD dwError = 0; dwError = LWNetSrvGetCachePath(&pszCachePath); BAIL_ON_LWNET_ERROR(dwError); dwError = LwCheckFileTypeExists( pszCachePath, LWFILE_DIRECTORY, &bDirExists); BAIL_ON_LWNET_ERROR(dwError); if (!bDirExists) { // Directory should be RWX for root and accessible to all // (so they can see the socket. mode_t mode = S_IRWXU | S_IRGRP| S_IXGRP | S_IROTH | S_IXOTH; dwError = LwCreateDirectory(pszCachePath, mode); BAIL_ON_LWNET_ERROR(dwError); } dwError = LwAllocateStringPrintf(&pszCommPath, "%s/%s", pszCachePath, LWNET_SERVER_FILENAME); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_context_new(NULL, &gpContext)); BAIL_ON_LWNET_ERROR(dwError); lwmsg_context_set_log_function(gpContext, LWNetSrvLogIpc, NULL); /* Set up IPC protocol object */ dwError = MAP_LWMSG_ERROR(lwmsg_protocol_new(gpContext, &gpProtocol)); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_protocol_add_protocol_spec( gpProtocol, LWNetIPCGetProtocolSpec())); BAIL_ON_LWNET_ERROR(dwError); /* Set up IPC server object */ dwError = MAP_LWMSG_ERROR(lwmsg_peer_new(gpContext, gpProtocol, &gpServer)); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_dispatch_spec( gpServer, LWNetSrvGetDispatchSpec())); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint( gpServer, LWMSG_ENDPOINT_DIRECT, "netlogon", 0)); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint( gpServer, LWMSG_ENDPOINT_LOCAL, pszCommPath, 0666)); BAIL_ON_LWNET_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_start_listen(gpServer)); error: LWNET_SAFE_FREE_STRING(pszCachePath); LWNET_SAFE_FREE_STRING(pszCommPath); if (dwError) { if (gpServer) { lwmsg_peer_stop_listen(gpServer); lwmsg_peer_delete(gpServer); gpServer = NULL; } } return dwError; }
DWORD RegSrvStartListenThread( void ) { PSTR pszCachePath = NULL; PSTR pszCommPath = NULL; BOOLEAN bDirExists = FALSE; DWORD dwError = 0; static LWMsgTime idleTimeout = {30, 0}; dwError = RegSrvGetCachePath(&pszCachePath); BAIL_ON_REG_ERROR(dwError); dwError = RegCheckDirectoryExists(pszCachePath, &bDirExists); BAIL_ON_REG_ERROR(dwError); if (!bDirExists) { // Directory should be RWX for root and accessible to all // (so they can see the socket. mode_t mode = S_IRWXU | S_IRGRP| S_IXGRP | S_IROTH | S_IXOTH; dwError = RegCreateDirectory(pszCachePath, mode); BAIL_ON_REG_ERROR(dwError); } dwError = LwRtlCStringAllocatePrintf(&pszCommPath, "%s/%s", pszCachePath, REG_SERVER_FILENAME); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_context_new(NULL, &gpContext)); BAIL_ON_REG_ERROR(dwError); lwmsg_context_set_log_function(gpContext, RegSrvLogIpc, NULL); /* Set up IPC protocol object */ dwError = MAP_LWMSG_ERROR(lwmsg_protocol_new(gpContext, &gpProtocol)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_protocol_add_protocol_spec( gpProtocol, RegIPCGetProtocolSpec())); BAIL_ON_REG_ERROR(dwError); /* Set up IPC server object */ dwError = MAP_LWMSG_ERROR(lwmsg_peer_new(gpContext, gpProtocol, &gpServer)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_dispatch_spec( gpServer, RegSrvGetDispatchSpec())); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint( gpServer, LWMSG_ENDPOINT_DIRECT, "lwreg", 0)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_add_listen_endpoint( gpServer, LWMSG_ENDPOINT_LOCAL, pszCommPath, 0666)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_max_listen_clients( gpServer, MAX_CLIENTS)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_max_listen_backlog( gpServer, REG_MAX(5, MAX_CLIENTS / 4))); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_timeout( gpServer, LWMSG_TIMEOUT_IDLE, &idleTimeout)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_set_listen_session_functions( gpServer, RegSrvIpcConstructSession, RegSrvIpcDestructSession, NULL)); BAIL_ON_REG_ERROR(dwError); dwError = MAP_LWMSG_ERROR(lwmsg_peer_start_listen(gpServer)); error: LWREG_SAFE_FREE_STRING(pszCachePath); LWREG_SAFE_FREE_STRING(pszCommPath); if (dwError) { if (gpServer) { lwmsg_peer_stop_listen(gpServer); lwmsg_peer_delete(gpServer); gpServer = NULL; } } return dwError; }
MU_TEST(stress, parallel) { Data data; pthread_t threads[NUM_THREADS]; int i; LWMsgContext* context = NULL; LWMsgProtocol* protocol = NULL; LWMsgPeer* client = NULL; LWMsgPeer* server = NULL; CounterRequest request; CounterReply* reply; LWMsgCall* call; LWMsgParams in = LWMSG_PARAMS_INITIALIZER; LWMsgParams out = LWMSG_PARAMS_INITIALIZER; LWMsgTime timeout = {1, 0}; MU_TRY(lwmsg_context_new(NULL, &context)); lwmsg_context_set_log_function(context, lwmsg_test_log_function, NULL); MU_TRY(lwmsg_protocol_new(context, &protocol)); MU_TRY(lwmsg_protocol_add_protocol_spec(protocol, counterprotocol_spec)); MU_TRY(lwmsg_peer_new(context, protocol, &server)); MU_TRY(lwmsg_peer_add_dispatch_spec(server, counter_dispatch)); MU_TRY(lwmsg_peer_add_listen_endpoint(server, LWMSG_CONNECTION_MODE_LOCAL, TEST_ENDPOINT, 0600)); MU_TRY(lwmsg_peer_set_max_listen_clients(server, MAX_CLIENTS)); MU_TRY(lwmsg_peer_set_timeout(server, LWMSG_TIMEOUT_IDLE, &timeout)); MU_TRY(lwmsg_peer_start_listen(server)); MU_TRY(lwmsg_peer_new(context, protocol, &client)); MU_TRY(lwmsg_peer_add_connect_endpoint(client, LWMSG_CONNECTION_MODE_LOCAL, TEST_ENDPOINT)); request.counter = 0; MU_TRY(lwmsg_peer_acquire_call(client, &call)); in.tag = COUNTER_OPEN; in.data = &request; MU_TRY(lwmsg_call_dispatch(call, &in, &out, NULL, NULL)); MU_ASSERT_EQUAL(MU_TYPE_INTEGER, out.tag, COUNTER_OPEN_SUCCESS); lwmsg_call_release(call); data.client = client; data.handle = out.data; data.iters = NUM_ITERS; data.go = 0; pthread_mutex_init(&data.lock, NULL); pthread_cond_init(&data.event, NULL); pthread_mutex_lock(&data.lock); for (i = 0; i < NUM_THREADS; i++) { pthread_create(&threads[i], NULL, add_thread, &data); } data.go = 1; pthread_cond_broadcast(&data.event); pthread_mutex_unlock(&data.lock); for (i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } MU_TRY(lwmsg_peer_acquire_call(client, &call)); in.tag = COUNTER_READ; in.data = data.handle; MU_TRY(lwmsg_call_dispatch(call, &in, &out, NULL, NULL)); MU_ASSERT_EQUAL(MU_TYPE_INTEGER, out.tag, COUNTER_READ_SUCCESS); reply = out.data; MU_ASSERT_EQUAL(MU_TYPE_INTEGER, reply->counter, NUM_THREADS * NUM_ITERS); lwmsg_call_destroy_params(call, &out); lwmsg_call_release(call); MU_TRY(lwmsg_peer_acquire_call(client, &call)); in.tag = COUNTER_CLOSE; in.data = data.handle; MU_TRY(lwmsg_call_dispatch(call, &in, &out, NULL, NULL)); MU_ASSERT_EQUAL(MU_TYPE_INTEGER, out.tag, COUNTER_CLOSE_SUCCESS); lwmsg_call_destroy_params(call, &out); lwmsg_call_release(call); MU_TRY(lwmsg_peer_disconnect(client)); lwmsg_peer_delete(client); MU_TRY(lwmsg_peer_stop_listen(server)); lwmsg_peer_delete(server); pthread_mutex_destroy(&data.lock); pthread_cond_destroy(&data.event); }
int main(int argc, char** argv) { int ret = 0; LWMsgStatus status = LWMSG_STATUS_SUCCESS; LWMsgContext* context = NULL; LWMsgProtocol* protocol = NULL; LWMsgPeer* server = NULL; /* Create context */ status = lwmsg_context_new(NULL, &context); if (status) { goto error; } /* Set log function */ lwmsg_context_set_log_function(context, log_message, NULL); /* Create protocol */ status = lwmsg_protocol_new(context, &protocol); if (status) { goto error; } /* Add protocol spec */ status = lwmsg_protocol_add_protocol_spec(protocol, fserv_get_protocol()); if (status) { goto error; } /* Create peer */ status = lwmsg_peer_new(context, protocol, &server); if (status) { goto error; } /* Add dispatch spec */ status = lwmsg_peer_add_dispatch_spec(server, fserver_get_dispatch()); if (status) { goto error; } /* Add listen endpoint */ status = lwmsg_peer_add_listen_endpoint(server, LWMSG_ENDPOINT_LOCAL, FSERV_SOCKET_PATH, (S_IRWXU | S_IRWXG | S_IRWXO)); if (status) { goto error; } ret = run(server); error: if (server) { lwmsg_peer_delete(server); } if (protocol) { lwmsg_protocol_delete(protocol); } if (status != LWMSG_STATUS_SUCCESS) { ret = -1; } exit(ret ? 1 : 0); }