int main(int argc, char **argv) { PRFileDesc *sock[2]; PRStatus status; PRProcess *process; PRProcessAttr *attr; char buf[1024]; PRInt32 nBytes; PRInt32 exitCode; int idx; status = PR_NewTCPSocketPair(sock); if (status == PR_FAILURE) { fprintf(stderr, "PR_NewTCPSocketPair failed\n"); exit(1); } status = PR_SetFDInheritable(sock[0], PR_FALSE); if (status == PR_FAILURE) { fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); exit(1); } status = PR_SetFDInheritable(sock[1], PR_TRUE); if (status == PR_FAILURE) { fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); exit(1); } attr = PR_NewProcessAttr(); if (attr == NULL) { fprintf(stderr, "PR_NewProcessAttr failed\n"); exit(1); } status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET"); if (status == PR_FAILURE) { fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n"); exit(1); } process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr); if (process == NULL) { fprintf(stderr, "PR_CreateProcess failed\n"); exit(1); } PR_DestroyProcessAttr(attr); status = PR_Close(sock[1]); if (status == PR_FAILURE) { fprintf(stderr, "PR_Close failed\n"); exit(1); } for (idx = 0; idx < NUM_ITERATIONS; idx++) { strcpy(buf, "ping"); printf("ping process: sending \"%s\"\n", buf); nBytes = PR_Write(sock[0], buf, 5); if (nBytes == -1) { fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); exit(1); } memset(buf, 0, sizeof(buf)); nBytes = PR_Read(sock[0], buf, sizeof(buf)); if (nBytes == -1) { fprintf(stderr, "PR_Read failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); exit(1); } printf("ping process: received \"%s\"\n", buf); if (nBytes != 5) { fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n", nBytes); exit(1); } if (strcmp(buf, "pong") != 0) { fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n", buf); exit(1); } } status = PR_Close(sock[0]); if (status == PR_FAILURE) { fprintf(stderr, "PR_Close failed\n"); exit(1); } status = PR_WaitProcess(process, &exitCode); if (status == PR_FAILURE) { fprintf(stderr, "PR_WaitProcess failed\n"); exit(1); } if (exitCode == 0) { printf("PASS\n"); return 0; } else { printf("FAIL\n"); return 1; } }
static nsresult vboxsvcSpawnDaemon(void) { PRFileDesc *readable = nsnull, *writable = nsnull; PRProcessAttr *attr = nsnull; nsresult rv = NS_ERROR_FAILURE; PRFileDesc *devNull; // The ugly casts are necessary because the PR_CreateProcessDetached has // a const array of writable strings as a parameter. It won't write. */ char * const args[] = { (char *)VBoxSVCPath, (char *)"--auto-shutdown", 0 }; // Use a pipe to determine when the daemon process is in the position // to actually process requests. The daemon will write "READY" to the pipe. if (PR_CreatePipe(&readable, &writable) != PR_SUCCESS) goto end; PR_SetFDInheritable(writable, PR_TRUE); attr = PR_NewProcessAttr(); if (!attr) goto end; if (PR_ProcessAttrSetInheritableFD(attr, writable, VBOXSVC_STARTUP_PIPE_NAME) != PR_SUCCESS) goto end; devNull = PR_Open("/dev/null", PR_RDWR, 0); if (!devNull) goto end; PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, devNull); PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, devNull); PR_ProcessAttrSetStdioRedirect(attr, PR_StandardError, devNull); if (PR_CreateProcessDetached(VBoxSVCPath, args, nsnull, attr) != PR_SUCCESS) goto end; // Close /dev/null PR_Close(devNull); // Close the child end of the pipe to make it the only owner of the // file descriptor, so that unexpected closing can be detected. PR_Close(writable); writable = nsnull; char msg[10]; RT_ZERO(msg); if ( PR_Read(readable, msg, sizeof(msg)-1) != 5 || strcmp(msg, "READY")) { /* If several clients start VBoxSVC simultaneously only one can * succeed. So treat this as success as well. */ rv = NS_OK; goto end; } rv = NS_OK; end: if (readable) PR_Close(readable); if (writable) PR_Close(writable); if (attr) PR_DestroyProcessAttr(attr); return rv; }