static PRFileDesc* PR_CALLBACK pl_TopAccept ( PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout) { PRStatus rv; PRFileDesc *newfd, *layer = fd; PRFileDesc *newstack; PRBool newstyle_stack = PR_FALSE; PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); /* test for new style stack */ while (NULL != layer->higher) layer = layer->higher; newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; newstack = PR_NEW(PRFileDesc); if (NULL == newstack) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } *newstack = *fd; /* make a copy of the accepting layer */ newfd = (fd->lower->methods->accept)(fd->lower, addr, timeout); if (NULL == newfd) { PR_DELETE(newstack); return NULL; } if (newstyle_stack) { newstack->lower = newfd; newfd->higher = newstack; return newstack; } else { /* this PR_PushIOLayer call cannot fail */ rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); PR_ASSERT(PR_SUCCESS == rv); return newfd; /* that's it */ } }
/* * TCP_Client * Client Thread * Connect to the server at the address specified in the argument. * Fill in a buffer, write data to server, read it back and check * for data corruption. * Close the socket for server connection */ static void PR_CALLBACK TCP_Client(void *arg) { Client_Param *cp = (Client_Param *) arg; PRFileDesc *sockfd; buffer *in_buf, *out_buf; union PRNetAddr netaddr; PRInt32 bytes, i, j; bytes = cp->datalen; out_buf = PR_NEW(buffer); if (out_buf == NULL) { fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n"); failed_already=1; return; } in_buf = PR_NEW(buffer); if (in_buf == NULL) { fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n"); failed_already=1; return; } netaddr.inet.family = cp->server_addr.inet.family; netaddr.inet.port = cp->server_addr.inet.port; netaddr.inet.ip = cp->server_addr.inet.ip; for (i = 0; i < num_tcp_connections_per_client; i++) { if ((sockfd = PR_NewTCPSocket()) == NULL) { fprintf(stderr,"prsocket_test: PR_NewTCPSocket failed\n"); failed_already=1; return; } if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){ fprintf(stderr,"prsocket_test: PR_Connect failed\n"); failed_already=1; return; } for (j = 0; j < num_tcp_mesgs_per_connection; j++) { /* * fill in random data */ memset(out_buf->data, ((PRInt32) (&netaddr)) + i + j, bytes); /* * write to server */ if (writen(sockfd, out_buf->data, bytes) < bytes) { fprintf(stderr,"prsocket_test: ERROR - TCP_Client:writen\n"); failed_already=1; return; } DPRINTF(("TCP Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n", PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data)))); if (readn(sockfd, in_buf->data, bytes) < bytes) { fprintf(stderr,"prsocket_test: ERROR - TCP_Client:readn\n"); failed_already=1; return; } /* * verify the data read */ if (memcmp(in_buf->data, out_buf->data, bytes) != 0) { fprintf(stderr,"prsocket_test: ERROR - data corruption\n"); failed_already=1; return; } } /* * shutdown reads and writes */ if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_Shutdown\n"); failed_already=1; } PR_Close(sockfd); } PR_DELETE(out_buf); PR_DELETE(in_buf); /* * Decrement exit_counter and notify parent thread */ PR_EnterMonitor(cp->exit_mon); --(*cp->exit_counter); PR_Notify(cp->exit_mon); PR_ExitMonitor(cp->exit_mon); DPRINTF(("TCP_Client [0x%x] exiting\n", PR_GetCurrentThread())); }
/* * UDP Server * Server Thread * Bind an address to a socket, read data from clients and send data * back to clients */ static void PR_CALLBACK UDP_Server(void *arg) { Server_Param *sp = (Server_Param *) arg; PRFileDesc *sockfd; buffer *in_buf; PRNetAddr netaddr; PRInt32 bytes, i, rv; bytes = sp->datalen; /* * Create a udp socket */ if ((sockfd = PR_NewUDPSocket()) == NULL) { fprintf(stderr,"prsocket_test: PR_NewUDPSocket failed\n"); failed_already=1; return; } memset(&netaddr, 0 , sizeof(netaddr)); netaddr.inet.family = AF_INET; netaddr.inet.port = PR_htons(UDP_SERVER_PORT); netaddr.inet.ip = PR_htonl(INADDR_ANY); /* * try a few times to bind server's address, if addresses are in * use */ i = 0; while (PR_Bind(sockfd, &netaddr) < 0) { if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { netaddr.inet.port += 2; if (i++ < SERVER_MAX_BIND_COUNT) continue; } fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n"); perror("PR_Bind"); failed_already=1; return; } if (PR_GetSockName(sockfd, &netaddr) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n"); failed_already=1; return; } if (netaddr.inet.port != PR_htons(UDP_SERVER_PORT)) { fprintf(stderr,"prsocket_test: ERROR - tried to bind to UDP " "port %hu, but was bound to port %hu\n", UDP_SERVER_PORT, PR_ntohs(netaddr.inet.port)); failed_already=1; return; } DPRINTF(("PR_Bind: UDP Server netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n", netaddr.inet.ip, netaddr.inet.port)); udp_server_addr = netaddr; /* * We can't use the IP address returned by PR_GetSockName in * netaddr.inet.ip because netaddr.inet.ip is returned * as 0 (= INADDR_ANY). */ udp_server_addr.inet.ip = PR_htonl(INADDR_LOOPBACK); /* * Wake up parent thread because server address is bound and made * available in the global variable 'udp_server_addr' */ PR_PostSem(sp->addr_sem); bytes = sp->datalen; in_buf = PR_NEW(buffer); if (in_buf == NULL) { fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n"); failed_already=1; return; } /* * Receive datagrams from clients and send them back, unmodified, to the * clients */ memset(&netaddr, 0 , sizeof(netaddr)); for (i = 0; i < (num_udp_clients * num_udp_datagrams_per_client); i++) { DPRINTF(("UDP_Server: calling PR_RecvFrom client - ip = 0x%lx, port = %d bytes = %d inbuf = 0x%lx, inbuf[0] = 0x%lx\n", netaddr.inet.ip, netaddr.inet.port, rv, in_buf->data, in_buf->data[0])); rv = PR_RecvFrom(sockfd, in_buf->data, bytes, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("UDP_Server: PR_RecvFrom client - ip = 0x%lx, port = %d bytes = %d inbuf = 0x%lx, inbuf[0] = 0x%lx\n", netaddr.inet.ip, netaddr.inet.port, rv, in_buf->data, in_buf->data[0])); if (rv != bytes) { return; } rv = PR_SendTo(sockfd, in_buf->data, bytes, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT); if (rv != bytes) { return; } } PR_DELETE(in_buf); /* * Decrement exit_counter and notify parent thread */ PR_EnterMonitor(sp->exit_mon); --(*sp->exit_counter); PR_Notify(sp->exit_mon); PR_ExitMonitor(sp->exit_mon); DPRINTF(("UDP_Server [0x%x] exiting\n", PR_GetCurrentThread())); }
/* * TCP Server * Server Thread * Bind an address to a socket and listen for incoming connections * Start a Serve_Client thread for each incoming connection. */ static void PR_CALLBACK TCP_Server(void *arg) { PRThread *t; Server_Param *sp = (Server_Param *) arg; Serve_Client_Param *scp; PRFileDesc *sockfd, *newsockfd; PRNetAddr netaddr; PRInt32 i; /* * Create a tcp socket */ if ((sockfd = PR_NewTCPSocket()) == NULL) { fprintf(stderr,"prsocket_test: PR_NewTCPSocket failed\n"); goto exit; } memset(&netaddr, 0 , sizeof(netaddr)); netaddr.inet.family = AF_INET; netaddr.inet.port = PR_htons(TCP_SERVER_PORT); netaddr.inet.ip = PR_htonl(INADDR_ANY); /* * try a few times to bind server's address, if addresses are in * use */ i = 0; while (PR_Bind(sockfd, &netaddr) < 0) { if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { netaddr.inet.port += 2; if (i++ < SERVER_MAX_BIND_COUNT) continue; } fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n"); perror("PR_Bind"); failed_already=1; goto exit; } if (PR_Listen(sockfd, 32) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_Listen failed\n"); failed_already=1; goto exit; } if (PR_GetSockName(sockfd, &netaddr) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n"); failed_already=1; goto exit; } DPRINTF(("TCP_Server: PR_BIND netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n", netaddr.inet.ip, netaddr.inet.port)); tcp_server_addr.inet.family = netaddr.inet.family; tcp_server_addr.inet.port = netaddr.inet.port; tcp_server_addr.inet.ip = netaddr.inet.ip; /* * Wake up parent thread because server address is bound and made * available in the global variable 'tcp_server_addr' */ PR_PostSem(sp->addr_sem); for (i = 0; i < (num_tcp_clients * num_tcp_connections_per_client); i++) { if ((newsockfd = PR_Accept(sockfd, &netaddr, PR_INTERVAL_NO_TIMEOUT)) == NULL) { fprintf(stderr,"prsocket_test: ERROR - PR_Accept failed\n"); goto exit; } scp = PR_NEW(Serve_Client_Param); if (scp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); goto exit; } /* * Start a Serve_Client thread for each incoming connection */ scp->sockfd = newsockfd; scp->datalen = sp->datalen; t = PR_CreateThread(PR_USER_THREAD, Serve_Client, (void *)scp, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; goto exit; } DPRINTF(("TCP_Server: Created Serve_Client = 0x%lx\n", t)); } exit: if (sockfd) { PR_Close(sockfd); } /* * Decrement exit_counter and notify parent thread */ PR_EnterMonitor(sp->exit_mon); --(*sp->exit_counter); PR_Notify(sp->exit_mon); PR_ExitMonitor(sp->exit_mon); DPRINTF(("TCP_Server [0x%lx] exiting\n", PR_GetCurrentThread())); }
static void TCP_Server_Accept(void *arg) { Server_Param *sp = (Server_Param *) arg; PRThreadPool *tp = sp->tp; Serve_Client_Param *scp; PRFileDesc *newsockfd; PRJob *jobp; if ((newsockfd = PR_Accept(sp->iod.socket, &sp->netaddr, PR_INTERVAL_NO_TIMEOUT)) == NULL) { fprintf(stderr,"%s: ERROR - PR_Accept failed\n", program_name); failed_already=1; goto exit; } scp = PR_NEW(Serve_Client_Param); if (scp == NULL) { fprintf(stderr,"%s: PR_NEW failed\n", program_name); failed_already=1; goto exit; } /* * Start a Serve_Client job for each incoming connection */ scp->iod.socket = newsockfd; scp->iod.timeout = PR_SecondsToInterval(60); scp->datalen = tcp_mesg_size; scp->exit_mon = sp->exit_mon; scp->job_counterp = sp->job_counterp; scp->tp = sp->tp; PR_EnterMonitor(sp->exit_mon); (*sp->job_counterp)++; PR_ExitMonitor(sp->exit_mon); jobp = PR_QueueJob(tp, Serve_Client, scp, PR_FALSE); PR_ASSERT(NULL != jobp); DPRINTF(("TCP_Server: Created Serve_Client = 0x%lx\n", jobp)); /* * single-threaded update; no lock needed */ sp->conn_counter++; if (sp->conn_counter < (num_tcp_clients * num_tcp_connections_per_client)) { jobp = PR_QueueJob_Accept(tp, &sp->iod, TCP_Server_Accept, sp, PR_FALSE); PR_ASSERT(NULL != jobp); return; } jobp = PR_QueueJob_Timer(tp, PR_MillisecondsToInterval(500), print_stats, sp, PR_FALSE); PR_ASSERT(NULL != jobp); DPRINTF(("TCP_Server: Created print_stats timer job = 0x%lx\n", jobp)); exit: PR_EnterMonitor(sp->exit_mon); /* Wait for server jobs to finish */ while (0 != *sp->job_counterp) { PR_Wait(sp->exit_mon, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("TCP_Server: conn_counter = %d\n", *sp->job_counterp)); } PR_ExitMonitor(sp->exit_mon); if (sp->iod.socket) { PR_Close(sp->iod.socket); } PR_DestroyMonitor(sp->exit_mon); printf("%30s","TCP_Socket_Client_Server_Test:"); printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l, num_tcp_clients, num_tcp_connections_per_client); printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":", num_tcp_mesgs_per_connection, tcp_mesg_size); DPRINTF(("%s: calling PR_ShutdownThreadPool\n", program_name)); PR_ShutdownThreadPool(sp->tp); PR_DELETE(sp); }
static PRProcess * ForkAndExec( const char *path, char *const *argv, char *const *envp, const PRProcessAttr *attr) { PRProcess *process; int nEnv, idx; char *const *childEnvp; char **newEnvp = NULL; int flags; process = PR_NEW(PRProcess); if (!process) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } childEnvp = envp; if (attr && attr->fdInheritBuffer) { PRBool found = PR_FALSE; if (NULL == childEnvp) { #ifdef DARWIN childEnvp = *(_NSGetEnviron()); #else childEnvp = environ; #endif } for (nEnv = 0; childEnvp[nEnv]; nEnv++) { } newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *)); if (NULL == newEnvp) { PR_DELETE(process); PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } for (idx = 0; idx < nEnv; idx++) { newEnvp[idx] = childEnvp[idx]; if (!found && !strncmp(newEnvp[idx], "NSPR_INHERIT_FDS=", 17)) { newEnvp[idx] = attr->fdInheritBuffer; found = PR_TRUE; } } if (!found) { newEnvp[idx++] = attr->fdInheritBuffer; } newEnvp[idx] = NULL; childEnvp = newEnvp; } #ifdef AIX process->md.pid = (*pr_wp.forkptr)(); #elif defined(NTO) || defined(SYMBIAN) /* * fork() & exec() does not work in a multithreaded process. * Use spawn() instead. */ { int fd_map[3] = { 0, 1, 2 }; if (attr) { if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { fd_map[0] = dup(attr->stdinFd->secret->md.osfd); flags = fcntl(fd_map[0], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[0], F_SETFL, flags & ~O_NONBLOCK); } if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { fd_map[1] = dup(attr->stdoutFd->secret->md.osfd); flags = fcntl(fd_map[1], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[1], F_SETFL, flags & ~O_NONBLOCK); } if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { fd_map[2] = dup(attr->stderrFd->secret->md.osfd); flags = fcntl(fd_map[2], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[2], F_SETFL, flags & ~O_NONBLOCK); } PR_ASSERT(attr->currentDirectory == NULL); /* not implemented */ } #ifdef SYMBIAN /* In Symbian OS, we use posix_spawn instead of fork() and exec() */ posix_spawn(&(process->md.pid), path, NULL, NULL, argv, childEnvp); #else process->md.pid = spawn(path, 3, fd_map, NULL, argv, childEnvp); #endif if (fd_map[0] != 0) close(fd_map[0]); if (fd_map[1] != 1) close(fd_map[1]); if (fd_map[2] != 2) close(fd_map[2]); } #else process->md.pid = fork(); #endif if ((pid_t) -1 == process->md.pid) { PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } return NULL; } else if (0 == process->md.pid) { /* the child process */ /* * If the child process needs to exit, it must call _exit(). * Do not call exit(), because exit() will flush and close * the standard I/O file descriptors, and hence corrupt * the parent process's standard I/O data structures. */ #if !defined(NTO) && !defined(SYMBIAN) if (attr) { /* the osfd's to redirect stdin, stdout, and stderr to */ int in_osfd = -1, out_osfd = -1, err_osfd = -1; if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { in_osfd = attr->stdinFd->secret->md.osfd; if (dup2(in_osfd, 0) != 0) { _exit(1); /* failed */ } flags = fcntl(0, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(0, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { out_osfd = attr->stdoutFd->secret->md.osfd; if (dup2(out_osfd, 1) != 1) { _exit(1); /* failed */ } flags = fcntl(1, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(1, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { err_osfd = attr->stderrFd->secret->md.osfd; if (dup2(err_osfd, 2) != 2) { _exit(1); /* failed */ } flags = fcntl(2, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(2, F_SETFL, flags & ~O_NONBLOCK); } } if (in_osfd != -1) { close(in_osfd); } if (out_osfd != -1 && out_osfd != in_osfd) { close(out_osfd); } if (err_osfd != -1 && err_osfd != in_osfd && err_osfd != out_osfd) { close(err_osfd); } if (attr->currentDirectory) { if (chdir(attr->currentDirectory) < 0) { _exit(1); /* failed */ } } } if (childEnvp) { (void)execve(path, argv, childEnvp); } else { /* Inherit the environment of the parent. */ (void)execv(path, argv); } /* Whoops! It returned. That's a bad sign. */ _exit(1); #endif /* !NTO */ } if (newEnvp) { PR_DELETE(newEnvp); } #if defined(_PR_NATIVE_THREADS) PR_Lock(pr_wp.ml); if (0 == pr_wp.numProcs++) { PR_NotifyCondVar(pr_wp.cv); } PR_Unlock(pr_wp.ml); #endif return process; }
int rdf_GetURL (MWContext *cx, int method, Net_GetUrlExitFunc *exit_routine, RDFFile rdfFile) { MozillaEvent_rdf_GetURL *event; URL_Struct *urls = NULL; char *url; #ifdef DEBUG_gagan return 0; #endif if (cx == NULL) return 0; if (rdfFile->refreshingp && rdfFile->updateURL) { url = rdfFile->updateURL; } else { url = rdfFile->url; } if (strcmp(url, gNavCntrUrl) == 0) { urls = NET_CreateURLStruct(url, NET_CACHE_ONLY_RELOAD); #ifdef NU_CACHE if (!CacheManager_Contains(url)) { #else if (NET_IsURLInDiskCache(urls) || NET_IsURLInMemCache(urls)) { } else { #endif NET_FreeURLStruct(urls); urls = NULL; } } if (!urls) urls = NET_CreateURLStruct(url, (rdfFile->refreshingp ? NET_SUPER_RELOAD : NET_NORMAL_RELOAD)); if (urls == NULL) return 0; urls->fe_data = rdfFile; if (method) urls->method = method; if (PR_CurrentThread() == mozilla_thread) { htLoadBegins(urls, url); NET_GetURL(urls, FO_CACHE_AND_RDF, cx, rdf_GetUrlExitFunc); } else { /* send event to Mozilla thread */ if (mozilla_event_queue == NULL) return(0); event = PR_NEW(MozillaEvent_rdf_GetURL); if (event == NULL) return(0); PR_InitEvent(&(event->ce.event), cx, (PRHandleEventProc)rdf_HandleEvent_GetURL, (PRDestroyEventProc)rdf_DisposeEvent_GetURL); event->url = copyString(url); event->urls = urls; event->method = FO_CACHE_AND_RDF; event->cx = cx; event->exitFunc = rdf_GetUrlExitFunc; PR_PostEvent(mozilla_event_queue, &(event->ce.event)); } return 1; } #endif /* MOZILLA_CLIENT */ void possiblyRereadRDFFiles (void* data) { possiblyRefreshRDFFiles(); /* timerID = FE_SetTimeout(possiblyRereadRDFFiles, NULL, 1000 * 60 * 10); once every 10 minutes diabled for legal reasons.*/ } void RDFglueInitialize() { #ifdef MOZILLA_CLIENT timerID = FE_SetTimeout(possiblyRereadRDFFiles, NULL, 1000 * 60 * 10); /* once every 10 minutes */ if (gRLForbiddenDomains != NULL) { freeMem(gRLForbiddenDomains); gRLForbiddenDomains = NULL; } if (PREF_CopyCharPref("browser.relatedLinksDisabledForDomains", &gRLForbiddenDomains) != PREF_OK) { gRLForbiddenDomains = NULL; } #endif /* MOZILLA_CLIENT */ }
static PRInt32 UDP_Socket_Client_Server_Test(void) { int i; PRThread *t; PRSemaphore *server_sem; Server_Param *sparamp; Client_Param *cparamp; PRMonitor *mon2; PRInt32 datalen; PRInt32 udp_connect = 1; datalen = udp_datagram_size; thread_count = 0; /* * start the server thread */ sparamp = PR_NEW(Server_Param); if (sparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; return -1; } server_sem = PR_NewSem(0); if (server_sem == NULL) { fprintf(stderr,"prsocket_test: PR_NewSem failed\n"); failed_already=1; return -1; } mon2 = PR_NewMonitor(); if (mon2 == NULL) { fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n"); failed_already=1; return -1; } PR_EnterMonitor(mon2); sparamp->addr_sem = server_sem; sparamp->exit_mon = mon2; sparamp->exit_counter = &thread_count; sparamp->datalen = datalen; DPRINTF(("Creating UDP server")); t = PR_CreateThread(PR_USER_THREAD, UDP_Server, (void *)sparamp, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; return -1; } thread_count++; /* * wait till the server address is setup */ PR_WaitSem(server_sem); /* * Now start a bunch of client threads */ for (i = 0; i < num_udp_clients; i++) { cparamp = PR_NEW(Client_Param); if (cparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; return -1; } cparamp->server_addr = udp_server_addr; cparamp->exit_mon = mon2; cparamp->exit_counter = &thread_count; cparamp->datalen = datalen; /* * Cause every other client thread to connect udp sockets */ #ifndef XP_MAC cparamp->udp_connect = udp_connect; #else /* No support for UDP connects on Mac */ cparamp->udp_connect = 0; #endif if (udp_connect) udp_connect = 0; else udp_connect = 1; DPRINTF(("Creating UDP client %d\n", i)); t = PR_CreateThread(PR_USER_THREAD, UDP_Client, (void *) cparamp, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; return -1; } thread_count++; } /* Wait for server and client threads to exit */ while (thread_count) { PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("UDP Server - thread_count = %d\n", thread_count)); } PR_ExitMonitor(mon2); printf("%30s","UDP_Socket_Client_Server_Test: "); printf("%2ld Server %2ld Clients\n",1l, num_udp_clients); printf("%30s %2ld datagrams_per_client %4ld bytes_per_datagram\n",":", num_udp_datagrams_per_client, udp_datagram_size); return 0; }
static void PR_CALLBACK Client(void *arg) { PRStatus rv; PRIntn index; char buffer[1024]; PRFileDesc *fd = NULL; PRUintn clipping = DEFAULT_CLIPPING; PRThread *me = PR_GetCurrentThread(); CSClient_t *client = (CSClient_t*)arg; CSDescriptor_t *descriptor = PR_NEW(CSDescriptor_t); PRIntervalTime timeout = PR_MillisecondsToInterval(DEFAULT_CLIENT_TIMEOUT); for (index = 0; index < sizeof(buffer); ++index) buffer[index] = (char)index; client->started = PR_IntervalNow(); PR_Lock(client->ml); client->state = cs_run; PR_NotifyCondVar(client->stateChange); PR_Unlock(client->ml); TimeOfDayMessage("Client started at", me); while (cs_run == client->state) { PRInt32 bytes, descbytes, filebytes, netbytes; (void)PR_NetAddrToString(&client->serverAddress, buffer, sizeof(buffer)); TEST_LOG(cltsrv_log_file, TEST_LOG_INFO, ("\tClient(0x%p): connecting to server at %s\n", me, buffer)); fd = PR_Socket(domain, SOCK_STREAM, protocol); TEST_ASSERT(NULL != fd); rv = PR_Connect(fd, &client->serverAddress, timeout); if (PR_FAILURE == rv) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): conection failed (%d, %d)\n", me, PR_GetError(), PR_GetOSError())); goto aborted; } memset(descriptor, 0, sizeof(*descriptor)); descriptor->size = PR_htonl(descbytes = rand() % clipping); PR_snprintf( descriptor->filename, sizeof(descriptor->filename), "CS%p%p-%p.dat", client->started, me, client->operations); TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tClient(0x%p): sending descriptor for %u bytes\n", me, descbytes)); bytes = PR_Send( fd, descriptor, sizeof(*descriptor), SEND_FLAGS, timeout); if (sizeof(CSDescriptor_t) != bytes) { if (Aborted(PR_FAILURE)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): send descriptor timeout\n", me)); goto retry; } } TEST_ASSERT(sizeof(*descriptor) == bytes); netbytes = 0; while (netbytes < descbytes) { filebytes = sizeof(buffer); if ((descbytes - netbytes) < filebytes) filebytes = descbytes - netbytes; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tClient(0x%p): sending %d bytes\n", me, filebytes)); bytes = PR_Send(fd, buffer, filebytes, SEND_FLAGS, timeout); if (filebytes != bytes) { if (Aborted(PR_FAILURE)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): send data timeout\n", me)); goto retry; } } TEST_ASSERT(bytes == filebytes); netbytes += bytes; } filebytes = 0; while (filebytes < descbytes) { netbytes = sizeof(buffer); if ((descbytes - filebytes) < netbytes) netbytes = descbytes - filebytes; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tClient(0x%p): receiving %d bytes\n", me, netbytes)); bytes = PR_Recv(fd, buffer, netbytes, RECV_FLAGS, timeout); if (-1 == bytes) { if (Aborted(PR_FAILURE)) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): receive data aborted\n", me)); goto aborted; } else if (PR_IO_TIMEOUT_ERROR == PR_GetError()) TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): receive data timeout\n", me)); else TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tClient(0x%p): receive error (%d, %d)\n", me, PR_GetError(), PR_GetOSError())); goto retry; } if (0 == bytes) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tClient(0x%p): unexpected end of stream\n", PR_GetCurrentThread())); break; } filebytes += bytes; } rv = PR_Shutdown(fd, PR_SHUTDOWN_BOTH); if (Aborted(rv)) goto aborted; TEST_ASSERT(PR_SUCCESS == rv); retry: (void)PR_Close(fd); fd = NULL; TEST_LOG( cltsrv_log_file, TEST_LOG_INFO, ("\tClient(0x%p): disconnected from server\n", me)); PR_Lock(client->ml); client->operations += 1; client->bytesTransferred += 2 * descbytes; rv = PR_WaitCondVar(client->stateChange, rand() % clipping); PR_Unlock(client->ml); if (Aborted(rv)) break; } aborted: client->stopped = PR_IntervalNow(); PR_ClearInterrupt(); if (NULL != fd) rv = PR_Close(fd); PR_Lock(client->ml); client->state = cs_exit; PR_NotifyCondVar(client->stateChange); PR_Unlock(client->ml); PR_DELETE(descriptor); TEST_LOG( cltsrv_log_file, TEST_LOG_ALWAYS, ("\tClient(0x%p): stopped after %u operations and %u bytes\n", PR_GetCurrentThread(), client->operations, client->bytesTransferred)); } /* Client */
static PR_CALLBACK PLHashEntry* AllocEntry(void* pool, const void* key) { return PR_NEW(PLHashEntry); }
/* ** Get a FileDescriptor from the cache if one exists. If not allocate ** a new one from the heap. */ PRFileDesc *_PR_Getfd(void) { PRFileDesc *fd; /* ** $$$ ** This may look a little wasteful. We'll see. Right now I want to ** be able to toggle between caching and not at runtime to measure ** the differences. If it isn't too annoying, I'll leave it in. ** $$$$ ** ** The test is against _pr_fd_cache.limit_high. If that's zero, ** we're not doing the extended cache but going for performance. */ if (0 == _pr_fd_cache.limit_high) { PRStackElem *pop; PR_ASSERT(NULL != _pr_fd_cache.stack); pop = PR_StackPop(_pr_fd_cache.stack); if (NULL == pop) goto allocate; fd = (PRFileDesc*)((PRPtrdiff)pop - (PRPtrdiff)stack2fd); } else { do { if (NULL == _pr_fd_cache.head) goto allocate; /* nothing there */ if (_pr_fd_cache.count < _pr_fd_cache.limit_low) goto allocate; /* we "should" be able to extract an fd from the cache */ PR_Lock(_pr_fd_cache.ml); /* need the lock to do this safely */ fd = _pr_fd_cache.head; /* protected extraction */ if (NULL == fd) /* unexpected, but not fatal */ { PR_ASSERT(0 == _pr_fd_cache.count); PR_ASSERT(NULL == _pr_fd_cache.tail); } else { _pr_fd_cache.count -= 1; _pr_fd_cache.head = fd->higher; if (NULL == _pr_fd_cache.head) { PR_ASSERT(0 == _pr_fd_cache.count); _pr_fd_cache.tail = NULL; } PR_ASSERT(&_pr_faulty_methods == fd->methods); PR_ASSERT(PR_INVALID_IO_LAYER == fd->identity); PR_ASSERT(_PR_FILEDESC_FREED == fd->secret->state); } PR_Unlock(_pr_fd_cache.ml); } while (NULL == fd); /* then go around and allocate a new one */ } finished: fd->dtor = NULL; fd->lower = fd->higher = NULL; fd->identity = PR_NSPR_IO_LAYER; memset(fd->secret, 0, sizeof(PRFilePrivate)); return fd; allocate: fd = PR_NEW(PRFileDesc); if (NULL != fd) { fd->secret = PR_NEW(PRFilePrivate); if (NULL == fd->secret) PR_DELETE(fd); } if (NULL != fd) goto finished; else return NULL; } /* _PR_Getfd */
PRProcess * _PR_CreateOS2Process( const char *path, char *const *argv, char *const *envp, const PRProcessAttr *attr) { PRProcess *proc = NULL; char *cmdLine = NULL; char **newEnvp = NULL; char *envBlock = NULL; STARTDATA startData = {0}; APIRET rc; ULONG ulAppType = 0; PID pid = 0; char *pszComSpec; char pszEXEName[CCHMAXPATH] = ""; char pszFormatString[CCHMAXPATH]; char pszObjectBuffer[CCHMAXPATH]; char *pszFormatResult = NULL; /* * Variables for DosExecPgm */ char szFailed[CCHMAXPATH]; char *pszCmdLine = NULL; RESULTCODES procInfo; HFILE hStdIn = 0, hStdOut = 0, hStdErr = 0; HFILE hStdInSave = -1, hStdOutSave = -1, hStdErrSave = -1; proc = PR_NEW(PRProcess); if (!proc) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); goto errorExit; } if (assembleCmdLine(argv, &cmdLine) == -1) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); goto errorExit; } #ifdef MOZ_OS2_HIGH_MEMORY /* * DosQueryAppType() fails if path (the char* in the first argument) is in * high memory. If that is the case, the following moves it to low memory. */ if ((ULONG)path >= 0x20000000) { size_t len = strlen(path) + 1; char *copy = (char *)alloca(len); memcpy(copy, path, len); path = copy; } #endif if (envp == NULL) { newEnvp = NULL; } else { int i; int numEnv = 0; while (envp[numEnv]) { numEnv++; } newEnvp = (char **) PR_MALLOC((numEnv+1) * sizeof(char *)); for (i = 0; i <= numEnv; i++) { newEnvp[i] = envp[i]; } qsort((void *) newEnvp, (size_t) numEnv, sizeof(char *), compare); } if (assembleEnvBlock(newEnvp, &envBlock) == -1) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); goto errorExit; } rc = DosQueryAppType(path, &ulAppType); if (rc != NO_ERROR) { char *pszDot = strrchr(path, '.'); if (pszDot) { /* If it is a CMD file, launch the users command processor */ if (!stricmp(pszDot, ".cmd")) { rc = DosScanEnv("COMSPEC", (PSZ *)&pszComSpec); if (!rc) { strcpy(pszFormatString, "/C %s %s"); strcpy(pszEXEName, pszComSpec); ulAppType = FAPPTYP_WINDOWCOMPAT; } } } } if (ulAppType == 0) { PR_SetError(PR_UNKNOWN_ERROR, 0); goto errorExit; } if ((ulAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) { startData.SessionType = SSF_TYPE_PM; } else if (ulAppType & FAPPTYP_WINDOWCOMPAT) { startData.SessionType = SSF_TYPE_WINDOWABLEVIO; } else { startData.SessionType = SSF_TYPE_DEFAULT; } if (ulAppType & (FAPPTYP_WINDOWSPROT31 | FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSREAL)) { strcpy(pszEXEName, "WINOS2.COM"); startData.SessionType = PROG_31_STDSEAMLESSVDM; strcpy(pszFormatString, "/3 %s %s"); } startData.InheritOpt = SSF_INHERTOPT_SHELL; if (pszEXEName[0]) { pszFormatResult = PR_MALLOC(strlen(pszFormatString)+strlen(path)+strlen(cmdLine)); sprintf(pszFormatResult, pszFormatString, path, cmdLine); startData.PgmInputs = pszFormatResult; } else { strcpy(pszEXEName, path); startData.PgmInputs = cmdLine; } startData.PgmName = pszEXEName; startData.Length = sizeof(startData); startData.Related = SSF_RELATED_INDEPENDENT; startData.ObjectBuffer = pszObjectBuffer; startData.ObjectBuffLen = CCHMAXPATH; startData.Environment = envBlock; if (attr) { /* On OS/2, there is really no way to pass file handles for stdin, * stdout, and stderr to a new process. Instead, we can make it * a child process and make the given file handles a copy of our * stdin, stdout, and stderr. The child process then inherits * ours, and we set ours back. Twisted and gross I know. If you * know a better way, please use it. */ if (attr->stdinFd) { hStdIn = 0; DosDupHandle(hStdIn, &hStdInSave); DosDupHandle((HFILE) attr->stdinFd->secret->md.osfd, &hStdIn); } if (attr->stdoutFd) { hStdOut = 1; DosDupHandle(hStdOut, &hStdOutSave); DosDupHandle((HFILE) attr->stdoutFd->secret->md.osfd, &hStdOut); } if (attr->stderrFd) { hStdErr = 2; DosDupHandle(hStdErr, &hStdErrSave); DosDupHandle((HFILE) attr->stderrFd->secret->md.osfd, &hStdErr); } /* * Build up the Command Line for DosExecPgm */ pszCmdLine = PR_MALLOC(strlen(pszEXEName) + strlen(startData.PgmInputs) + 3); sprintf(pszCmdLine, "%s%c%s%c", pszEXEName, '\0', startData.PgmInputs, '\0'); rc = DosExecPgm(szFailed, CCHMAXPATH, EXEC_ASYNCRESULT, pszCmdLine, envBlock, &procInfo, pszEXEName); PR_DELETE(pszCmdLine); /* Restore our old values. Hope this works */ if (hStdInSave != -1) { DosDupHandle(hStdInSave, &hStdIn); DosClose(hStdInSave); } if (hStdOutSave != -1) { DosDupHandle(hStdOutSave, &hStdOut); DosClose(hStdOutSave); } if (hStdErrSave != -1) { DosDupHandle(hStdErrSave, &hStdErr); DosClose(hStdErrSave); } if (rc != NO_ERROR) { /* XXX what error code? */ PR_SetError(PR_UNKNOWN_ERROR, rc); goto errorExit; } proc->md.pid = procInfo.codeTerminate; } else { /* * If no STDIN/STDOUT redirection is not needed, use DosStartSession * to create a new, independent session */ rc = DosStartSession(&startData, &ulAppType, &pid); if ((rc != NO_ERROR) && (rc != ERROR_SMG_START_IN_BACKGROUND)) { PR_SetError(PR_UNKNOWN_ERROR, rc); goto errorExit; } proc->md.pid = pid; } if (pszFormatResult) { PR_DELETE(pszFormatResult); } PR_DELETE(cmdLine); if (newEnvp) { PR_DELETE(newEnvp); } if (envBlock) { PR_DELETE(envBlock); } return proc; errorExit: if (cmdLine) { PR_DELETE(cmdLine); } if (newEnvp) { PR_DELETE(newEnvp); } if (envBlock) { PR_DELETE(envBlock); } if (proc) { PR_DELETE(proc); } return NULL; } /* _PR_CreateOS2Process */
static PLHashEntry * DataSourceAllocEntry(void *pool, const void *key) { return PR_NEW(PLHashEntry); }
static PRInt32 TCP_Socket_Client_Server_Test(void) { int i; Client_Param *cparamp; PRMonitor *mon2; PRInt32 datalen; PRInt32 connections = 0; PRThread *thr; datalen = tcp_mesg_size; connections = 0; mon2 = PR_NewMonitor(); if (mon2 == NULL) { fprintf(stderr,"%s: PR_NewMonitor failed\n", program_name); failed_already=1; return -1; } /* * Start client jobs */ cparamp = PR_NEW(Client_Param); if (cparamp == NULL) { fprintf(stderr,"%s: PR_NEW failed\n", program_name); failed_already=1; return -1; } cparamp->server_addr.inet.family = PR_AF_INET; cparamp->server_addr.inet.port = PR_htons(server_port); cparamp->server_addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); cparamp->exit_mon = mon2; cparamp->exit_counter = &connections; cparamp->datalen = datalen; for (i = 0; i < num_tcp_clients; i++) { thr = PR_CreateThread(PR_USER_THREAD, TCP_Client, (void *)cparamp, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (NULL == thr) { fprintf(stderr,"%s: PR_CreateThread failed\n", program_name); failed_already=1; return -1; } PR_EnterMonitor(mon2); connections++; PR_ExitMonitor(mon2); DPRINTF(("Created TCP client = 0x%lx\n", thr)); } /* Wait for client jobs to exit */ PR_EnterMonitor(mon2); while (0 != connections) { PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("Client job count = %d\n", connections)); } PR_ExitMonitor(mon2); printf("%30s","TCP_Socket_Client_Server_Test:"); printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l, num_tcp_clients, num_tcp_connections_per_client); printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":", num_tcp_mesgs_per_connection, tcp_mesg_size); PR_DELETE(cparamp); return 0; }
/* * UDP_Client * Client Thread * Create a socket and bind an address * Communicate with the server at the address specified in the argument. * Fill in a buffer, write data to server, read it back and check * for data corruption. * Close the socket */ static void PR_CALLBACK UDP_Client(void *arg) { Client_Param *cp = (Client_Param *) arg; PRFileDesc *sockfd; buffer *in_buf, *out_buf; union PRNetAddr netaddr; PRInt32 bytes, i, rv; bytes = cp->datalen; out_buf = PR_NEW(buffer); if (out_buf == NULL) { fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n"); failed_already=1; return; } in_buf = PR_NEW(buffer); if (in_buf == NULL) { fprintf(stderr,"prsocket_test: failed to alloc buffer struct\n"); failed_already=1; return; } if ((sockfd = PR_NewUDPSocket()) == NULL) { fprintf(stderr,"prsocket_test: PR_NewUDPSocket failed\n"); failed_already=1; return; } /* * bind an address for the client, let the system chose the port * number */ memset(&netaddr, 0 , sizeof(netaddr)); netaddr.inet.family = AF_INET; netaddr.inet.ip = PR_htonl(INADDR_ANY); netaddr.inet.port = PR_htons(0); if (PR_Bind(sockfd, &netaddr) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n"); perror("PR_Bind"); return; } if (PR_GetSockName(sockfd, &netaddr) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_GetSockName failed\n"); failed_already=1; return; } DPRINTF(("PR_Bind: UDP Client netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n", netaddr.inet.ip, netaddr.inet.port)); netaddr.inet.family = cp->server_addr.inet.family; netaddr.inet.port = cp->server_addr.inet.port; netaddr.inet.ip = cp->server_addr.inet.ip; if (cp->udp_connect) { if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){ fprintf(stderr,"prsocket_test: PR_Connect failed\n"); failed_already=1; return; } } for (i = 0; i < num_udp_datagrams_per_client; i++) { /* * fill in random data */ DPRINTF(("UDP_Client [0x%lx]: out_buf = 0x%lx bytes = 0x%lx\n", PR_GetCurrentThread(), out_buf->data, bytes)); memset(out_buf->data, ((PRInt32) (&netaddr)) + i, bytes); /* * write to server */ if (cp->udp_connect) rv = PR_Send(sockfd, out_buf->data, bytes, 0, PR_INTERVAL_NO_TIMEOUT); else rv = PR_SendTo(sockfd, out_buf->data, bytes, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT); if (rv != bytes) { return; } DPRINTF(("UDP_Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n", PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data)))); if (cp->udp_connect) rv = PR_Recv(sockfd, in_buf->data, bytes, 0, PR_INTERVAL_NO_TIMEOUT); else rv = PR_RecvFrom(sockfd, in_buf->data, bytes, 0, &netaddr, PR_INTERVAL_NO_TIMEOUT); if (rv != bytes) { return; } DPRINTF(("UDP_Client [0x%lx]: in_buf = 0x%lx in_buf[0] = 0x%lx\n", PR_GetCurrentThread(), in_buf, (*((int *) in_buf->data)))); /* * verify the data read */ if (memcmp(in_buf->data, out_buf->data, bytes) != 0) { fprintf(stderr,"prsocket_test: ERROR - UDP data corruption\n"); failed_already=1; return; } } PR_Close(sockfd); PR_DELETE(in_buf); PR_DELETE(out_buf); /* * Decrement exit_counter and notify parent thread */ PR_EnterMonitor(cp->exit_mon); --(*cp->exit_counter); PR_Notify(cp->exit_mon); PR_ExitMonitor(cp->exit_mon); PR_DELETE(cp); DPRINTF(("UDP_Client [0x%x] exiting\n", PR_GetCurrentThread())); }
static PRStatus ProcessRequest(PRFileDesc *fd, CSServer_t *server) { PRStatus drv, rv; char buffer[1024]; PRFileDesc *file = NULL; PRThread * me = PR_GetCurrentThread(); PRInt32 bytes, descbytes, netbytes, filebytes = 0; CSDescriptor_t *descriptor = PR_NEW(CSDescriptor_t); PRIntervalTime timeout = PR_MillisecondsToInterval(DEFAULT_SERVER_TIMEOUT); TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tProcessRequest(0x%p): receiving desciptor\n", me)); bytes = PR_Recv( fd, descriptor, sizeof(*descriptor), RECV_FLAGS, timeout); if (-1 == bytes) { rv = PR_FAILURE; if (Aborted(rv)) goto exit; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tProcessRequest(0x%p): receive timeout\n", me)); } goto exit; } if (0 == bytes) { rv = PR_FAILURE; TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tProcessRequest(0x%p): unexpected end of file\n", me)); goto exit; } descbytes = PR_ntohl(descriptor->size); TEST_ASSERT(sizeof(*descriptor) == bytes); TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\t\tProcessRequest(0x%p): read descriptor {%d, %s}\n", me, descbytes, descriptor->filename)); file = PR_Open( descriptor->filename, (PR_CREATE_FILE | PR_WRONLY), 0666); if (NULL == file) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\tProcessRequest(0x%p): open file timeout\n", me)); goto aborted; } } TEST_ASSERT(NULL != file); filebytes = 0; while (filebytes < descbytes) { netbytes = sizeof(buffer); if ((descbytes - filebytes) < netbytes) netbytes = descbytes - filebytes; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tProcessRequest(0x%p): receive %d bytes\n", me, netbytes)); bytes = PR_Recv(fd, buffer, netbytes, RECV_FLAGS, timeout); if (-1 == bytes) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): receive data timeout\n", me)); goto aborted; } /* * XXX: I got (PR_CONNECT_RESET_ERROR, ERROR_NETNAME_DELETED) * on NT here. This is equivalent to ECONNRESET on Unix. * -wtc */ TEST_LOG( cltsrv_log_file, TEST_LOG_WARNING, ("\t\tProcessRequest(0x%p): unexpected error (%d, %d)\n", me, PR_GetError(), PR_GetOSError())); goto aborted; } if(0 == bytes) { TEST_LOG( cltsrv_log_file, TEST_LOG_WARNING, ("\t\tProcessRequest(0x%p): unexpected end of stream\n", me)); rv = PR_FAILURE; goto aborted; } filebytes += bytes; netbytes = bytes; /* The byte count for PR_Write should be positive */ MY_ASSERT(netbytes > 0); TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tProcessRequest(0x%p): write %d bytes to file\n", me, netbytes)); bytes = PR_Write(file, buffer, netbytes); if (netbytes != bytes) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): write file timeout\n", me)); goto aborted; } } TEST_ASSERT(bytes > 0); } PR_Lock(server->ml); server->operations += 1; server->bytesTransferred += filebytes; PR_Unlock(server->ml); rv = PR_Close(file); if (Aborted(rv)) goto aborted; TEST_ASSERT(PR_SUCCESS == rv); file = NULL; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\t\tProcessRequest(0x%p): opening %s\n", me, descriptor->filename)); file = PR_Open(descriptor->filename, PR_RDONLY, 0); if (NULL == file) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): open file timeout\n", PR_GetCurrentThread())); goto aborted; } TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): other file open error (%u, %u)\n", me, PR_GetError(), PR_GetOSError())); goto aborted; } TEST_ASSERT(NULL != file); netbytes = 0; while (netbytes < descbytes) { filebytes = sizeof(buffer); if ((descbytes - netbytes) < filebytes) filebytes = descbytes - netbytes; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\tProcessRequest(0x%p): read %d bytes from file\n", me, filebytes)); bytes = PR_Read(file, buffer, filebytes); if (filebytes != bytes) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): read file timeout\n", me)); else TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): other file error (%d, %d)\n", me, PR_GetError(), PR_GetOSError())); goto aborted; } TEST_ASSERT(bytes > 0); netbytes += bytes; filebytes = bytes; TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\t\tProcessRequest(0x%p): sending %d bytes\n", me, filebytes)); bytes = PR_Send(fd, buffer, filebytes, SEND_FLAGS, timeout); if (filebytes != bytes) { rv = PR_FAILURE; if (Aborted(rv)) goto aborted; if (PR_IO_TIMEOUT_ERROR == PR_GetError()) { TEST_LOG( cltsrv_log_file, TEST_LOG_ERROR, ("\t\tProcessRequest(0x%p): send data timeout\n", me)); goto aborted; } break; } TEST_ASSERT(bytes > 0); } PR_Lock(server->ml); server->bytesTransferred += filebytes; PR_Unlock(server->ml); rv = PR_Shutdown(fd, PR_SHUTDOWN_BOTH); if (Aborted(rv)) goto aborted; rv = PR_Close(file); if (Aborted(rv)) goto aborted; TEST_ASSERT(PR_SUCCESS == rv); file = NULL; aborted: PR_ClearInterrupt(); if (NULL != file) PR_Close(file); drv = PR_Delete(descriptor->filename); TEST_ASSERT(PR_SUCCESS == drv); exit: TEST_LOG( cltsrv_log_file, TEST_LOG_VERBOSE, ("\t\tProcessRequest(0x%p): Finished\n", me)); PR_DELETE(descriptor); #if defined(WIN95) PR_Sleep(PR_MillisecondsToInterval(200)); /* lth. see note [1] */ #endif return rv; } /* ProcessRequest */
static PRInt32 TCP_Socket_Client_Server_Test(void) { int i; PRThread *t; PRThreadScope scope; PRSemaphore *server_sem; Server_Param *sparamp; Client_Param *cparamp; PRMonitor *mon2; PRInt32 datalen; datalen = tcp_mesg_size; thread_count = 0; /* * start the server thread */ sparamp = PR_NEW(Server_Param); if (sparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; return -1; } server_sem = PR_NewSem(0); if (server_sem == NULL) { fprintf(stderr,"prsocket_test: PR_NewSem failed\n"); failed_already=1; return -1; } mon2 = PR_NewMonitor(); if (mon2 == NULL) { fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n"); failed_already=1; return -1; } PR_EnterMonitor(mon2); sparamp->addr_sem = server_sem; sparamp->exit_mon = mon2; sparamp->exit_counter = &thread_count; sparamp->datalen = datalen; t = PR_CreateThread(PR_USER_THREAD, TCP_Server, (void *)sparamp, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; return -1; } DPRINTF(("Created TCP server = 0x%lx\n", t)); thread_count++; /* * wait till the server address is setup */ PR_WaitSem(server_sem); /* * Now start a bunch of client threads */ cparamp = PR_NEW(Client_Param); if (cparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; return -1; } cparamp->server_addr = tcp_server_addr; cparamp->server_addr.inet.ip = PR_htonl(INADDR_LOOPBACK); cparamp->exit_mon = mon2; cparamp->exit_counter = &thread_count; cparamp->datalen = datalen; for (i = 0; i < num_tcp_clients; i++) { /* * Every other thread is a LOCAL/GLOBAL thread */ if (i & 1) scope = PR_LOCAL_THREAD; else scope = PR_GLOBAL_THREAD; t = PR_CreateThread(PR_USER_THREAD, TCP_Client, (void *) cparamp, PR_PRIORITY_NORMAL, scope, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; return -1; } DPRINTF(("Created TCP client = 0x%lx\n", t)); thread_count++; } /* Wait for server and client threads to exit */ while (thread_count) { PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("TCP Server - thread_count = %d\n", thread_count)); } PR_ExitMonitor(mon2); printf("%30s","TCP_Socket_Client_Server_Test:"); printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l, num_tcp_clients, num_tcp_connections_per_client); printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":", num_tcp_mesgs_per_connection, tcp_mesg_size); return 0; }
PRProcess* _MD_create_process (const char *path, char *const *argv, char *const *envp, const PRProcessAttr *attr) { PRProcess *process; int nEnv, idx; char *const *childEnvp; char **newEnvp = NULL; int flags; PRBool found = PR_FALSE; process = PR_NEW(PRProcess); if (!process) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } childEnvp = envp; if (attr && attr->fdInheritBuffer) { if (NULL == childEnvp) { childEnvp = environ; } for (nEnv = 0; childEnvp[nEnv]; nEnv++) { } newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *)); if (NULL == newEnvp) { PR_DELETE(process); PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } for (idx = 0; idx < nEnv; idx++) { newEnvp[idx] = childEnvp[idx]; if (!found && !strncmp(newEnvp[idx], "NSPR_INHERIT_FDS=", 17)) { newEnvp[idx] = attr->fdInheritBuffer; found = PR_TRUE; } } if (!found) { newEnvp[idx++] = attr->fdInheritBuffer; } newEnvp[idx] = NULL; childEnvp = newEnvp; } process->md.pid = fork(); if ((pid_t) -1 == process->md.pid) { PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } return NULL; } else if (0 == process->md.pid) { /* the child process */ /* * If the child process needs to exit, it must call _exit(). * Do not call exit(), because exit() will flush and close * the standard I/O file descriptors, and hence corrupt * the parent process's standard I/O data structures. */ if (attr) { /* the osfd's to redirect stdin, stdout, and stderr to */ int in_osfd = -1, out_osfd = -1, err_osfd = -1; if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { in_osfd = attr->stdinFd->secret->md.osfd; if (dup2(in_osfd, 0) != 0) { _exit(1); /* failed */ } flags = fcntl(0, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(0, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { out_osfd = attr->stdoutFd->secret->md.osfd; if (dup2(out_osfd, 1) != 1) { _exit(1); /* failed */ } flags = fcntl(1, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(1, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { err_osfd = attr->stderrFd->secret->md.osfd; if (dup2(err_osfd, 2) != 2) { _exit(1); /* failed */ } flags = fcntl(2, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(2, F_SETFL, flags & ~O_NONBLOCK); } } if (in_osfd != -1) { close(in_osfd); } if (out_osfd != -1 && out_osfd != in_osfd) { close(out_osfd); } if (err_osfd != -1 && err_osfd != in_osfd && err_osfd != out_osfd) { close(err_osfd); } if (attr->currentDirectory) { if (chdir(attr->currentDirectory) < 0) { _exit(1); /* failed */ } } } if (childEnvp) { (void)execve(path, argv, childEnvp); } else { /* Inherit the environment of the parent. */ (void)execv(path, argv); } /* Whoops! It returned. That's a bad sign. */ _exit(1); } if (newEnvp) { PR_DELETE(newEnvp); } return process; }
static PRProcess * ForkAndExec( const char *path, char *const *argv, char *const *envp, const PRProcessAttr *attr) { PRProcess *process; int nEnv, idx; char *const *childEnvp; char **newEnvp = NULL; int flags; #ifdef VMS char VMScurdir[FILENAME_MAX+1] = { '\0' } ; #endif process = PR_NEW(PRProcess); if (!process) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } childEnvp = envp; if (attr && attr->fdInheritBuffer) { PRBool found = PR_FALSE; if (NULL == childEnvp) { #ifdef DARWIN childEnvp = *(_NSGetEnviron()); #else childEnvp = environ; #endif } for (nEnv = 0; childEnvp[nEnv]; nEnv++) { } newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *)); if (NULL == newEnvp) { PR_DELETE(process); PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } for (idx = 0; idx < nEnv; idx++) { newEnvp[idx] = childEnvp[idx]; if (!found && !strncmp(newEnvp[idx], "NSPR_INHERIT_FDS=", 17)) { newEnvp[idx] = attr->fdInheritBuffer; found = PR_TRUE; } } if (!found) { newEnvp[idx++] = attr->fdInheritBuffer; } newEnvp[idx] = NULL; childEnvp = newEnvp; } #ifdef VMS /* ** Since vfork/exec is implemented VERY differently on OpenVMS, we have to ** handle the setting up of the standard streams very differently. And since ** none of this code can ever execute in the context of the child, we have ** to perform the chdir in the parent so the child is born into the correct ** directory (and then switch the parent back again). */ { int decc$set_child_standard_streams(int,int,int); int n, fd_stdin=0, fd_stdout=1, fd_stderr=2; /* Set up any standard streams we are given, assuming defaults */ if (attr) { if (attr->stdinFd) fd_stdin = attr->stdinFd->secret->md.osfd; if (attr->stdoutFd) fd_stdout = attr->stdoutFd->secret->md.osfd; if (attr->stderrFd) fd_stderr = attr->stderrFd->secret->md.osfd; } /* ** Put a lock around anything that isn't going to be thread-safe. */ PR_Lock(_pr_vms_fork_lock); /* ** Prepare the child's streams. We always do this in case a previous fork ** has left the stream assignments in some non-standard way. */ n = decc$set_child_standard_streams(fd_stdin,fd_stdout,fd_stderr); if (n == -1) { PR_SetError(PR_BAD_DESCRIPTOR_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } PR_Unlock(_pr_vms_fork_lock); return NULL; } /* Switch directory if we have to */ if (attr) { if (attr->currentDirectory) { if ( (getcwd(VMScurdir,sizeof(VMScurdir)) == NULL) || (chdir(attr->currentDirectory) < 0) ) { PR_SetError(PR_DIRECTORY_OPEN_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } PR_Unlock(_pr_vms_fork_lock); return NULL; } } } } #endif /* VMS */ #ifdef AIX process->md.pid = (*pr_wp.forkptr)(); #elif defined(NTO) || defined(SYMBIAN) /* * fork() & exec() does not work in a multithreaded process. * Use spawn() instead. */ { int fd_map[3] = { 0, 1, 2 }; if (attr) { if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { fd_map[0] = dup(attr->stdinFd->secret->md.osfd); flags = fcntl(fd_map[0], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[0], F_SETFL, flags & ~O_NONBLOCK); } if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { fd_map[1] = dup(attr->stdoutFd->secret->md.osfd); flags = fcntl(fd_map[1], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[1], F_SETFL, flags & ~O_NONBLOCK); } if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { fd_map[2] = dup(attr->stderrFd->secret->md.osfd); flags = fcntl(fd_map[2], F_GETFL, 0); if (flags & O_NONBLOCK) fcntl(fd_map[2], F_SETFL, flags & ~O_NONBLOCK); } PR_ASSERT(attr->currentDirectory == NULL); /* not implemented */ } #ifdef SYMBIAN /* In Symbian OS, we use posix_spawn instead of fork() and exec() */ posix_spawn(&(process->md.pid), path, NULL, NULL, argv, childEnvp); #else process->md.pid = spawn(path, 3, fd_map, NULL, argv, childEnvp); #endif if (fd_map[0] != 0) close(fd_map[0]); if (fd_map[1] != 1) close(fd_map[1]); if (fd_map[2] != 2) close(fd_map[2]); } #else process->md.pid = fork(); #endif if ((pid_t) -1 == process->md.pid) { PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } return NULL; } else if (0 == process->md.pid) { /* the child process */ /* * If the child process needs to exit, it must call _exit(). * Do not call exit(), because exit() will flush and close * the standard I/O file descriptors, and hence corrupt * the parent process's standard I/O data structures. */ #if !defined(NTO) && !defined(SYMBIAN) #ifdef VMS /* OpenVMS has already handled all this above */ #else if (attr) { /* the osfd's to redirect stdin, stdout, and stderr to */ int in_osfd = -1, out_osfd = -1, err_osfd = -1; if (attr->stdinFd && attr->stdinFd->secret->md.osfd != 0) { in_osfd = attr->stdinFd->secret->md.osfd; if (dup2(in_osfd, 0) != 0) { _exit(1); /* failed */ } flags = fcntl(0, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(0, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stdoutFd && attr->stdoutFd->secret->md.osfd != 1) { out_osfd = attr->stdoutFd->secret->md.osfd; if (dup2(out_osfd, 1) != 1) { _exit(1); /* failed */ } flags = fcntl(1, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(1, F_SETFL, flags & ~O_NONBLOCK); } } if (attr->stderrFd && attr->stderrFd->secret->md.osfd != 2) { err_osfd = attr->stderrFd->secret->md.osfd; if (dup2(err_osfd, 2) != 2) { _exit(1); /* failed */ } flags = fcntl(2, F_GETFL, 0); if (flags & O_NONBLOCK) { fcntl(2, F_SETFL, flags & ~O_NONBLOCK); } } if (in_osfd != -1) { close(in_osfd); } if (out_osfd != -1 && out_osfd != in_osfd) { close(out_osfd); } if (err_osfd != -1 && err_osfd != in_osfd && err_osfd != out_osfd) { close(err_osfd); } if (attr->currentDirectory) { if (chdir(attr->currentDirectory) < 0) { _exit(1); /* failed */ } } } #endif /* !VMS */ if (childEnvp) { (void)execve(path, argv, childEnvp); } else { /* Inherit the environment of the parent. */ (void)execv(path, argv); } /* Whoops! It returned. That's a bad sign. */ #ifdef VMS /* ** On OpenVMS we are still in the context of the parent, and so we ** can (and should!) perform normal error handling. */ PR_SetError(PR_UNKNOWN_ERROR, errno); PR_DELETE(process); if (newEnvp) { PR_DELETE(newEnvp); } if (VMScurdir[0] != '\0') chdir(VMScurdir); PR_Unlock(_pr_vms_fork_lock); return NULL; #else _exit(1); #endif /* VMS */ #endif /* !NTO */ } if (newEnvp) { PR_DELETE(newEnvp); } #ifdef VMS /* If we switched directories, then remember to switch back */ if (VMScurdir[0] != '\0') { chdir(VMScurdir); /* can't do much if it fails */ } PR_Unlock(_pr_vms_fork_lock); #endif /* VMS */ #if defined(_PR_NATIVE_THREADS) PR_Lock(pr_wp.ml); if (0 == pr_wp.numProcs++) { PR_NotifyCondVar(pr_wp.cv); } PR_Unlock(pr_wp.ml); #endif return process; }
/* * TransmitFile Server * Server Thread * Bind an address to a socket and listen for incoming connections * Create worker threads to service clients */ static void TransmitFile_Server(void *arg) { PRThread **t = NULL; /* an array of PRThread pointers */ Server_Param *sp = (Server_Param *) arg; Serve_Client_Param *scp; PRFileDesc *sockfd = NULL, *newsockfd; PRNetAddr netaddr; PRInt32 i; PRThreadScope scope; t = (PRThread**)PR_MALLOC(num_transmitfile_clients * sizeof(PRThread *)); if (t == NULL) { fprintf(stderr, "prsocket_test: run out of memory\n"); failed_already=1; goto exit; } /* * Create a tcp socket */ if ((sockfd = PR_NewTCPSocket()) == NULL) { fprintf(stderr,"prsocket_test: PR_NewTCPSocket failed\n"); failed_already=1; goto exit; } memset(&netaddr, 0 , sizeof(netaddr)); netaddr.inet.family = AF_INET; netaddr.inet.port = PR_htons(TCP_SERVER_PORT); netaddr.inet.ip = PR_htonl(INADDR_ANY); /* * try a few times to bind server's address, if addresses are in * use */ i = 0; while (PR_Bind(sockfd, &netaddr) < 0) { if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { netaddr.inet.port += 2; if (i++ < SERVER_MAX_BIND_COUNT) continue; } fprintf(stderr,"prsocket_test: ERROR - PR_Bind failed\n"); failed_already=1; perror("PR_Bind"); goto exit; } if (PR_Listen(sockfd, 32) < 0) { fprintf(stderr,"prsocket_test: ERROR - PR_Listen failed\n"); failed_already=1; goto exit; } if (PR_GetSockName(sockfd, &netaddr) < 0) { fprintf(stderr, "prsocket_test: ERROR - PR_GetSockName failed\n"); failed_already=1; goto exit; } DPRINTF(("TCP_Server: PR_BIND netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n", netaddr.inet.ip, netaddr.inet.port)); tcp_server_addr.inet.family = netaddr.inet.family; tcp_server_addr.inet.port = netaddr.inet.port; tcp_server_addr.inet.ip = netaddr.inet.ip; /* * Wake up parent thread because server address is bound and made * available in the global variable 'tcp_server_addr' */ PR_PostSem(sp->addr_sem); for (i = 0; i < num_transmitfile_clients ; i++) { if ((newsockfd = PR_Accept(sockfd, &netaddr, PR_INTERVAL_NO_TIMEOUT)) == NULL) { fprintf(stderr, "prsocket_test: ERROR - PR_Accept failed\n"); failed_already=1; goto exit; } scp = PR_NEW(Serve_Client_Param); if (scp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; goto exit; } /* * Start a Serve_Client thread for each incoming connection */ scp->sockfd = newsockfd; scp->datalen = sp->datalen; /* * create LOCAL and GLOBAL threads alternately */ if (i & 1) scope = PR_LOCAL_THREAD; else scope = PR_GLOBAL_THREAD; t[i] = PR_CreateThread(PR_USER_THREAD, Serve_TransmitFile_Client, (void *)scp, PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0); if (t[i] == NULL) { fprintf(stderr, "prsocket_test: PR_CreateThread failed\n"); failed_already=1; goto exit; } DPRINTF(("TransmitFile_Server: Created Serve_TransmitFile_Client = 0x%lx\n", t)); } /* * Wait for all the worker threads to end, so that we know * they are no longer using the small and large file fd's. */ for (i = 0; i < num_transmitfile_clients; i++) { PR_JoinThread(t[i]); } exit: if (t) { PR_DELETE(t); } if (sockfd) { PR_Close(sockfd); } /* * Decrement exit_counter and notify parent thread */ PR_EnterMonitor(sp->exit_mon); --(*sp->exit_counter); PR_Notify(sp->exit_mon); PR_ExitMonitor(sp->exit_mon); DPRINTF(("TransmitFile_Server [0x%lx] exiting\n", PR_GetCurrentThread())); }
PR_IMPLEMENT(PRSem *) PR_OpenSemaphore( const char *name, PRIntn flags, PRIntn mode, PRUintn value) { PRSem *sem; key_t key; union semun arg; struct sembuf sop; struct semid_ds seminfo; #define MAX_TRIES 60 PRIntn i; char osname[PR_IPC_NAME_SIZE]; if (_PR_MakeNativeIPCName(name, osname, sizeof(osname), _PRIPCSem) == PR_FAILURE) { return NULL; } /* Make sure the file exists before calling ftok. */ if (flags & PR_SEM_CREATE) { int osfd = open(osname, O_RDWR|O_CREAT, mode); if (-1 == osfd) { _PR_MD_MAP_OPEN_ERROR(errno); return NULL; } if (close(osfd) == -1) { _PR_MD_MAP_CLOSE_ERROR(errno); return NULL; } } key = ftok(osname, NSPR_IPC_KEY_ID); if ((key_t)-1 == key) { _PR_MD_MAP_DEFAULT_ERROR(errno); return NULL; } sem = PR_NEW(PRSem); if (NULL == sem) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return NULL; } if (flags & PR_SEM_CREATE) { sem->semid = semget(key, 1, mode|IPC_CREAT|IPC_EXCL); if (sem->semid >= 0) { /* creator of a semaphore is responsible for initializing it */ arg.val = 0; if (semctl(sem->semid, 0, SETVAL, arg) == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } /* call semop to set sem_otime to nonzero */ sop.sem_num = 0; sop.sem_op = value; sop.sem_flg = 0; if (semop(sem->semid, &sop, 1) == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } return sem; } if (errno != EEXIST || flags & PR_SEM_EXCL) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } } sem->semid = semget(key, 1, NSPR_SEM_MODE); if (sem->semid == -1) { _PR_MD_MAP_DEFAULT_ERROR(errno); PR_Free(sem); return NULL; } for (i = 0; i < MAX_TRIES; i++) { arg.buf = &seminfo; semctl(sem->semid, 0, IPC_STAT, arg); if (seminfo.sem_otime != 0) break; sleep(1); } if (i == MAX_TRIES) { PR_SetError(PR_IO_TIMEOUT_ERROR, 0); PR_Free(sem); return NULL; } return sem; }
/* * Socket_Misc_Test - test miscellaneous functions * */ static PRInt32 Socket_Misc_Test(void) { PRIntn i, rv = 0, bytes, count, len; PRThread *t; PRThreadScope scope; PRSemaphore *server_sem; Server_Param *sparamp; Client_Param *cparamp; PRMonitor *mon2; PRInt32 datalen; /* * We deliberately pick a buffer size that is not a nice multiple * of 1024. */ #define TRANSMITFILE_BUF_SIZE (4 * 1024 - 11) typedef struct { char data[TRANSMITFILE_BUF_SIZE]; } file_buf; file_buf *buf = NULL; /* * create file(s) to be transmitted */ if ((PR_MkDir(TEST_DIR, 0777)) < 0) { printf("prsocket_test failed to create dir %s\n",TEST_DIR); failed_already=1; return -1; } small_file_fd = PR_Open(SMALL_FILE_NAME, PR_RDWR | PR_CREATE_FILE,0777); if (small_file_fd == NULL) { fprintf(stderr,"prsocket_test failed to create/open file %s\n", SMALL_FILE_NAME); failed_already=1; rv = -1; goto done; } buf = PR_NEW(file_buf); if (buf == NULL) { fprintf(stderr,"prsocket_test failed to allocate buffer\n"); failed_already=1; rv = -1; goto done; } /* * fill in random data */ for (i = 0; i < TRANSMITFILE_BUF_SIZE; i++) { buf->data[i] = i; } count = 0; do { len = (SMALL_FILE_SIZE - count) > TRANSMITFILE_BUF_SIZE ? TRANSMITFILE_BUF_SIZE : (SMALL_FILE_SIZE - count); bytes = PR_Write(small_file_fd, buf->data, len); if (bytes <= 0) { fprintf(stderr, "prsocket_test failed to write to file %s\n", SMALL_FILE_NAME); failed_already=1; rv = -1; goto done; } count += bytes; } while (count < SMALL_FILE_SIZE); #ifdef XP_UNIX /* * map the small file; used in checking for data corruption */ small_file_addr = mmap(0, SMALL_FILE_SIZE, PROT_READ, MAP_PRIVATE, small_file_fd->secret->md.osfd, 0); if (small_file_addr == (void *) -1) { fprintf(stderr,"prsocket_test failed to mmap file %s\n", SMALL_FILE_NAME); failed_already=1; rv = -1; goto done; } #endif /* * header for small file */ small_file_header = PR_MALLOC(SMALL_FILE_HEADER_SIZE); if (small_file_header == NULL) { fprintf(stderr,"prsocket_test failed to malloc header file\n"); failed_already=1; rv = -1; goto done; } memset(small_file_header, (int) PR_IntervalNow(), SMALL_FILE_HEADER_SIZE); /* * setup large file */ large_file_fd = PR_Open(LARGE_FILE_NAME, PR_RDWR | PR_CREATE_FILE,0777); if (large_file_fd == NULL) { fprintf(stderr,"prsocket_test failed to create/open file %s\n", LARGE_FILE_NAME); failed_already=1; rv = -1; goto done; } /* * fill in random data */ for (i = 0; i < TRANSMITFILE_BUF_SIZE; i++) { buf->data[i] = i; } count = 0; do { len = (LARGE_FILE_SIZE - count) > TRANSMITFILE_BUF_SIZE ? TRANSMITFILE_BUF_SIZE : (LARGE_FILE_SIZE - count); bytes = PR_Write(large_file_fd, buf->data, len); if (bytes <= 0) { fprintf(stderr, "prsocket_test failed to write to file %s: (%ld, %ld)\n", LARGE_FILE_NAME, PR_GetError(), PR_GetOSError()); failed_already=1; rv = -1; goto done; } count += bytes; } while (count < LARGE_FILE_SIZE); #ifdef XP_UNIX /* * map the large file; used in checking for data corruption */ large_file_addr = mmap(0, LARGE_FILE_SIZE, PROT_READ, MAP_PRIVATE, large_file_fd->secret->md.osfd, 0); if (large_file_addr == (void *) -1) { fprintf(stderr,"prsocket_test failed to mmap file %s\n", LARGE_FILE_NAME); failed_already=1; rv = -1; goto done; } #endif datalen = tcp_mesg_size; thread_count = 0; /* * start the server thread */ sparamp = PR_NEW(Server_Param); if (sparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; rv = -1; goto done; } server_sem = PR_NewSem(0); if (server_sem == NULL) { fprintf(stderr,"prsocket_test: PR_NewSem failed\n"); failed_already=1; rv = -1; goto done; } mon2 = PR_NewMonitor(); if (mon2 == NULL) { fprintf(stderr,"prsocket_test: PR_NewMonitor failed\n"); failed_already=1; rv = -1; goto done; } PR_EnterMonitor(mon2); sparamp->addr_sem = server_sem; sparamp->exit_mon = mon2; sparamp->exit_counter = &thread_count; sparamp->datalen = datalen; t = PR_CreateThread(PR_USER_THREAD, TransmitFile_Server, (void *)sparamp, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); failed_already=1; rv = -1; goto done; } DPRINTF(("Created TCP server = 0x%x\n", t)); thread_count++; /* * wait till the server address is setup */ PR_WaitSem(server_sem); /* * Now start a bunch of client threads */ cparamp = PR_NEW(Client_Param); if (cparamp == NULL) { fprintf(stderr,"prsocket_test: PR_NEW failed\n"); failed_already=1; rv = -1; goto done; } cparamp->server_addr = tcp_server_addr; cparamp->server_addr.inet.ip = PR_htonl(INADDR_LOOPBACK); cparamp->exit_mon = mon2; cparamp->exit_counter = &thread_count; cparamp->datalen = datalen; for (i = 0; i < num_transmitfile_clients; i++) { /* * Every other thread is a LOCAL/GLOBAL thread */ if (i & 1) scope = PR_GLOBAL_THREAD; else scope = PR_LOCAL_THREAD; t = PR_CreateThread(PR_USER_THREAD, TransmitFile_Client, (void *) cparamp, PR_PRIORITY_NORMAL, scope, PR_UNJOINABLE_THREAD, 0); if (t == NULL) { fprintf(stderr,"prsocket_test: PR_CreateThread failed\n"); rv = -1; failed_already=1; goto done; } DPRINTF(("Created TransmitFile client = 0x%lx\n", t)); thread_count++; } /* Wait for server and client threads to exit */ while (thread_count) { PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT); DPRINTF(("Socket_Misc_Test - thread_count = %d\n", thread_count)); } PR_ExitMonitor(mon2); done: if (buf) { PR_DELETE(buf); } #ifdef XP_UNIX munmap(small_file_addr, SMALL_FILE_SIZE); munmap(large_file_addr, LARGE_FILE_SIZE); #endif PR_Close(small_file_fd); PR_Close(large_file_fd); if ((PR_Delete(SMALL_FILE_NAME)) == PR_FAILURE) { fprintf(stderr,"prsocket_test: failed to unlink file %s\n", SMALL_FILE_NAME); failed_already=1; } if ((PR_Delete(LARGE_FILE_NAME)) == PR_FAILURE) { fprintf(stderr,"prsocket_test: failed to unlink file %s\n", LARGE_FILE_NAME); failed_already=1; } if ((PR_RmDir(TEST_DIR)) == PR_FAILURE) { fprintf(stderr,"prsocket_test failed to rmdir %s: (%ld, %ld)\n", TEST_DIR, PR_GetError(), PR_GetOSError()); failed_already=1; } printf("%-29s%s","Socket_Misc_Test",":"); printf("%2d Server %2d Clients\n",1, num_transmitfile_clients); printf("%30s Sizes of Transmitted Files - %4d KB, %2d MB \n",":", SMALL_FILE_SIZE/1024, LARGE_FILE_SIZE/(1024 * 1024)); return rv; }
static PLHashEntry * DefaultAllocEntry(void *pool, const void *key) { return PR_NEW(PLHashEntry); }
/* * TCP Server * Server binds an address to a socket, starts a client process and * listens for incoming connections. * Each client connects to the server and sends a chunk of data * Starts a Serve_Client job for each incoming connection, to read * the data from the client and send it back to the client, unmodified. * Each client checks that data received from server is same as the * data it sent to the server. * Finally, the threadpool is shutdown */ static void PR_CALLBACK TCP_Server(void *arg) { PRThreadPool *tp = (PRThreadPool *) arg; Server_Param *sp; PRFileDesc *sockfd; PRNetAddr netaddr; PRMonitor *sc_mon; PRJob *jobp; int i; PRStatus rval; /* * Create a tcp socket */ if ((sockfd = PR_NewTCPSocket()) == NULL) { fprintf(stderr,"%s: PR_NewTCPSocket failed\n", program_name); return; } memset(&netaddr, 0 , sizeof(netaddr)); netaddr.inet.family = PR_AF_INET; netaddr.inet.port = PR_htons(TCP_SERVER_PORT); netaddr.inet.ip = PR_htonl(PR_INADDR_ANY); /* * try a few times to bind server's address, if addresses are in * use */ i = 0; while (PR_Bind(sockfd, &netaddr) < 0) { if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { netaddr.inet.port += 2; if (i++ < SERVER_MAX_BIND_COUNT) continue; } fprintf(stderr,"%s: ERROR - PR_Bind failed\n", program_name); perror("PR_Bind"); failed_already=1; return; } if (PR_Listen(sockfd, 32) < 0) { fprintf(stderr,"%s: ERROR - PR_Listen failed\n", program_name); failed_already=1; return; } if (PR_GetSockName(sockfd, &netaddr) < 0) { fprintf(stderr,"%s: ERROR - PR_GetSockName failed\n", program_name); failed_already=1; return; } DPRINTF(( "TCP_Server: PR_BIND netaddr.inet.ip = 0x%lx, netaddr.inet.port = %d\n", netaddr.inet.ip, netaddr.inet.port)); sp = PR_NEW(Server_Param); if (sp == NULL) { fprintf(stderr,"%s: PR_NEW failed\n", program_name); failed_already=1; return; } sp->iod.socket = sockfd; sp->iod.timeout = PR_SecondsToInterval(60); sp->datalen = tcp_mesg_size; sp->exit_mon = sc_mon; sp->job_counterp = &job_counter; sp->conn_counter = 0; sp->tp = tp; sp->netaddr = netaddr; /* create and cancel an io job */ jobp = PR_QueueJob_Accept(tp, &sp->iod, TCP_Server_Accept, sp, PR_FALSE); PR_ASSERT(NULL != jobp); rval = PR_CancelJob(jobp); PR_ASSERT(PR_SUCCESS == rval); /* * create the client process */ { #define MAX_ARGS 4 char *argv[MAX_ARGS + 1]; int index = 0; char port[32]; char path[1024 + sizeof("/thrpool_client")]; getcwd(path, sizeof(path)); (void)strcat(path, "/thrpool_client"); #ifdef XP_PC (void)strcat(path, ".exe"); #endif argv[index++] = path; sprintf(port,"%d",PR_ntohs(netaddr.inet.port)); if (_debug_on) { argv[index++] = "-d"; argv[index++] = "-p"; argv[index++] = port; argv[index++] = NULL; } else { argv[index++] = "-p"; argv[index++] = port; argv[index++] = NULL; } PR_ASSERT(MAX_ARGS >= (index - 1)); DPRINTF(("creating client process %s ...\n", path)); if (PR_FAILURE == PR_CreateProcessDetached(path, argv, NULL, NULL)) { fprintf(stderr, "thrpool_server: ERROR - PR_CreateProcessDetached failed\n"); failed_already=1; return; } } sc_mon = PR_NewMonitor(); if (sc_mon == NULL) { fprintf(stderr,"%s: PR_NewMonitor failed\n", program_name); failed_already=1; return; } sp->iod.socket = sockfd; sp->iod.timeout = PR_SecondsToInterval(60); sp->datalen = tcp_mesg_size; sp->exit_mon = sc_mon; sp->job_counterp = &job_counter; sp->conn_counter = 0; sp->tp = tp; sp->netaddr = netaddr; /* create and cancel a timer job */ jobp = PR_QueueJob_Timer(tp, PR_MillisecondsToInterval(5000), print_stats, sp, PR_FALSE); PR_ASSERT(NULL != jobp); rval = PR_CancelJob(jobp); PR_ASSERT(PR_SUCCESS == rval); DPRINTF(("TCP_Server: Accepting connections \n")); jobp = PR_QueueJob_Accept(tp, &sp->iod, TCP_Server_Accept, sp, PR_FALSE); PR_ASSERT(NULL != jobp); return; }