Exemple #1
0
int main()
{
    uint32_t seq = 0;
    unsigned int i=0;
    proc_num = get_nprocs();
    gap = 100000000 / proc_num;

    signal(SIGUSR1,sig_handler);
    /*read from stdin*/
    for(i=0; i<16; i++)
    {
        scanf("%2x",&crypto_word[i]);
    }

    //scanf("%s",BUFF);
    //to();

    for(i=0; i<proc_num; i++)
    {
        beginSeq[i] = gap * i;
    }

    createChildProcess(proc_num);

    int waitNum = 0;
    while(wait(NULL))
    {
        waitNum++;
        if(waitNum==proc_num)
            break;
    }

    return 0;
}
//----------------------------------------------------
// Function called when we want to launch a process and it to be run as
// administrator on Vista (the binary containing this function must have
// a manifest specifying that).
// Returns the exit code of the process associated with the command if it
// could be launched and -1 otherwise.
//----------------------------------------------------
int run(char* argv[])
{
  PROCESS_INFORMATION procInfo; // info on the new process
  BOOL createOk;
  DWORD exitCode;
  int returnValue = -1;
  int millisToWait = 30000;
  int waitedMillis = 0;

  char command[COMMAND_SIZE];

  if (getCommandLine(argv, command, COMMAND_SIZE))
  {
    createOk = createChildProcess((char*)command, TRUE, &procInfo);

    if(createOk)
    {
      GetExitCodeProcess(procInfo.hProcess, &exitCode);
      while (exitCode == STILL_ACTIVE)
      {
        GetExitCodeProcess(procInfo.hProcess, &exitCode);
        Sleep(500);
        waitedMillis += 500;
        if (waitedMillis > millisToWait)
        {
          break;
        }
      }
      returnValue = exitCode;
    }
  }
  return returnValue;
} // run
static void sessionHandler(int port)
{
	struct sockaddr_in childSocket;
	socklen_t socketLen = sizeof(childSocket);
	int idx1, clientIdx;

	signal(SIGCHLD, sessionSignalHandler); /* register for the child's signal */
	if(createSocket(port) == 1)
	{
		fprintf(stderr, "sessionHandler: Unable to setup socket\n");
		exit(1);
	}

	while((clientIdx = accept(serverSocket, (struct sockaddr *)&childSocket, &socketLen)) > 0)
	{
		printf("sessionHandler: Client connection from %s, client index %d\n", inet_ntoa(childSocket.sin_addr), clientIdx);
		for(idx1=0; idx1 < MaxSessions; idx1++)
		{
			if(childPID[idx1] == 0)
			{
				serverPID[idx1] = clientIdx;
				childPID[idx1] = createChildProcess(socketService, clientIdx);
				break;
			}
		}

		if(idx1 == MaxSessions)
			fprintf(stderr, "sessionHandler: Max clients reached\n");

		socketLen = sizeof(childSocket);
	}
	shutdown(serverSocket, SHUT_RDWR);
	close(serverSocket);
}
Exemple #4
0
int main()
{
    uint32_t seq = 0;
    unsigned int i=0;
    proc_num = get_nprocs();

    if(proc_num==1)
        proc_num += 2;
    else if(proc_num==2)
        proc_num+=2;

    if(proc_num==3 || proc_num == 4)
        gap = 123000000 / proc_num;
    else
        gap = 100000000 / proc_num;

    /*read from stdin*/
    //scanf("%s",BUFF);
    //to();
    unsigned int temp;
    for(i=0;i<MD5_LEN;i++){
        scanf("%2x",&temp);
        crypto_word[i] = temp;
    }
    //if(proc_num == 1){
//
 //       one_proc();
  //      exit(0);
   // }
	signal(SIGUSR1,sig_handler);
    for(i=0;i<proc_num;i++){
        beginSeq[i] = gap * i;
    }

    createChildProcess(proc_num);

    /*mq_unlink(MSGQUEUE);
    struct mq_attr attr;
    attr.mq_maxmsg = 10000;
    attr.mq_msgsize = MSGSIZE;
    msgQueueId = mq_open(MSGQUEUE,O_RDWR|O_CREAT,0664,&attr);

    createChildProcess(proc_num);

    for(;seq<100000000;seq+=gap){
        mq_send(msgQueueId,&seq,4,0);
    }*/

    int waitNum = 0;
    while(wait(NULL)){
        waitNum++;
        if(waitNum==proc_num)
            break;
    }
    mq_close(msgQueueId);
    mq_unlink(MSGQUEUE);
    return 0;
}
int runServerProcess()
{
	int iResult;
	isForceQuit = false;

	if (acceptClientSocket() != 0) {
		return 1;
	}
	isConnectionEstablished = true;

	SECURITY_ATTRIBUTES saAttr;
	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
	saAttr.bInheritHandle = TRUE;
	saAttr.lpSecurityDescriptor = NULL;

	if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
		ErrorExit(TEXT("StdoutRd CreatePipe"));
	// Ensure the read handle to the pipe for STDOUT is not inherited.
	if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
		ErrorExit(TEXT("Stdout SetHandleInformation"));
	// Create a pipe for the child process's STDIN. 
	if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
		ErrorExit(TEXT("Stdin CreatePipe"));
	// Ensure the write handle to the pipe for STDIN is not inherited. 
	if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
		ErrorExit(TEXT("Stdin SetHandleInformation"));

	// Create the child process.
	createChildProcess();

	// Create thread for sending from pipe
	HANDLE hThread;
	DWORD dwThreadId;
	hThread = CreateThread(
		NULL,                                  // default security attributes
		0,                                     // use default stack size  
		readPipeAndSendToSocketThreadLoop,	   // thread function name
		&ClientSocket,			               // argument to thread function 
		0,                                     // use default creation flags 
		&dwThreadId);

	iResult = listenSocketWriteToPipeLoop();

	// Terminate thread
	DWORD exitCode;
	if (GetExitCodeThread(hThread, &exitCode) != 0) {
		if (TerminateThread(hThread, exitCode) != 0) {
			printf("close thread successfully\n");
		}
	}

	return 0;
}
int getJavaStartCommand(char *scriptFile) { 
	HANDLE hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup;
    SECURITY_ATTRIBUTES saAttr; 
    BOOL fSuccess; 
	
	//DebugBreak();
    // Set the bInheritHandle flag so pipe handles are inherited. 
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 
 
    // The steps for redirecting child process's STDOUT: 
    //     1. Create anonymous pipe to be STDOUT for child process. 
    //     2. Create a noninheritable duplicate of the read handle and
    //        close the inheritable read handle. 
    //     3. Set STDOUT for child process
 
    // Create a pipe to be used for the child process's STDOUT. 
     if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) 
        errorExit("Stdout pipe creation failed"); 
 
    // Create noninheritable read handle and close the inheritable read 
    // handle. 
    fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
        GetCurrentProcess(), &hChildStdoutRdDup , 0, FALSE,
        DUPLICATE_SAME_ACCESS);
	if(!fSuccess) {
		PrintEvent(_T("DuplicateHandle failed"));
		errorExit("DuplicateHandle failed");
	}
    CloseHandle(hChildStdoutRd);

	//
	// Now create the child process. 
    fSuccess = createChildProcess(scriptFile, hChildStdoutWr);

	if (!fSuccess) {
		PrintEvent(_T("Could not create process!!"));
		errorExit("Create process failed"); 
	}

	//PrintEvent(_T("Created Child Process Successfully"));

	// Close the write end of the pipe before reading from the 
    // read end of the pipe. 
    if (!CloseHandle(hChildStdoutWr)) 
        errorExit("Closing handle failed"); 

    // Read from pipe that is the standard output for child process. 
    readFromPipe(hChildStdoutRdDup); 

    return 0;
} 
Exemple #7
0
int main()
{
    uint32_t seq = 0;
    unsigned int i=0;
    int fifo_fd;
    struct stat statbuf;
    proc_num = 2;//get_nprocs();
    gap = 100000000 /2;
    //proc_num+=2;

    result = (char*)mmap(NULL,9,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
    signal(SIGUSR1,sig_handler);
    /*read from stdin*/
    for(i=0;i<16;i++){
        scanf("%2x",&crypto_word[i]);
    }

    struct mq_attr attr;
    attr.mq_maxmsg = 10000;
    attr.mq_msgsize = MSGSIZE;
    msgQueueId = mq_open(MSGQUEUE,O_RDWR|O_CREAT,0664,&attr);

    createChildProcess(proc_num);

    for(;seq<100000000;seq+=gap){
        mq_send(msgQueueId,&seq,4,0);
    }

    int waitNum = 0;
    while(wait(NULL)){
        waitNum++;
        if(waitNum==proc_num)
            break;
    }

    return 0;
}
Exemple #8
0
// ----------------------------------------------------
// Function used to launch a process for the given command
// If the process could be created it returns the pid of
// the created process and -1 otherwise.
// ----------------------------------------------------
int spawn(char* command, BOOL background)
{
  DWORD childPid = -1; // child's pid
  PROCESS_INFORMATION procInfo; // info on the new process
  BOOL createOk;

  createOk = createChildProcess(command, background, &procInfo);

  if(createOk)
  {
    childPid = procInfo.dwProcessId;
  }

  if (childPid != -1)
  {
    debug("The PID of the spawned process is %d.", childPid);
    return childPid;
  }
  else
  {
    debugError("Could not get the PID of the spawned process.");
    return -1;
  }
} // spawn
Exemple #9
0
//
// AbiPaint editImage
// ------------------
//   This is the function that we actually call to invoke the image editor.
//
//   parameters are:
//     AV_View* v
//     EV_EditMethodCallData *d
//
static DECLARE_ABI_PLUGIN_METHOD(editImage)
{
	UT_UNUSED(v);
    // Get the current view that the user is in.
    XAP_Frame *pFrame = XAP_App::getApp()->getLastFocussedFrame();
    FV_View* pView = static_cast<FV_View*>(pFrame->getCurrentView());

//
// get values from preference (initial plugin execution should have set sensible defaults)
//
	UT_String imageApp;  // holds MAXPATH\appName <space> MAXPATH\imagefilename
	bool bLeaveImageAsPNG;

	// read stuff from the preference value
	if (!prefsScheme->getValue(ABIPAINT_PREF_KEY_szProgramName, imageApp))
	{
		UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
		getDefaultApp(imageApp, bLeaveImageAsPNG);
	}
	
	// now that we have program name, try to get other flag (allows overriding default value)
	// Note: we allow overriding, otherwise if we don't adhere to user's setting
	//       then the use BMP or not menu should be greyed to note it has no effect
	prefsScheme->getValueBool(ABIPAINT_PREF_KEY_bLeaveImageAsPNG, &bLeaveImageAsPNG);


//
// generate a temp file name...
//
	char *szTempFileName = NULL;
	GError *err = NULL;
	gint fp = g_file_open_tmp ("XXXXXX", &szTempFileName, &err);
	if (err) {
		g_warning ("%s", err->message);
		g_error_free (err); err = NULL;
		return FALSE;
	}
	close(fp);

	UT_String szTmpPng = szTempFileName;
	szTmpPng += ".png";
	UT_String szTmp = szTmpPng; // default: our temp file is the created png file
	
	PT_DocPosition pos = pView->saveSelectedImage((const char *)szTmpPng.c_str());
	if(pos == 0)
	{
		remove(szTempFileName);
		g_free (szTempFileName); szTempFileName = NULL;
		pFrame->showMessageBox("You must select an Image before editing it", XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
		return false;
	}

#ifdef ENABLE_BMP
//
// Convert png into bmp for best compatibility with Windows programs
// NOTE: probably looses detail/information though!!! so if possible use PNG
//
	if (!bLeaveImageAsPNG)
	{
		szTmp = szTempFileName;
		szTmp += ".bmp";	// our temp file is a bmp file

		if (convertPNG2BMP(szTmpPng.c_str(), szTmp.c_str()))
		{
			pFrame->showMessageBox("Unable to convert PNG image data to BMP for external program use!", XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
			UT_ASSERT(UT_SHOULD_NOT_HAPPEN);

			remove(szTempFileName);
			g_free (szTempFileName); szTempFileName = NULL;
			remove(szTmpPng.c_str());
			return false;
		}
		// remove(szTmpPng.c_str());

	}
#endif
	
	// remove the temp file (that lacks proper extension)
	remove(szTempFileName);
	g_free (szTempFileName); szTempFileName = NULL;

//
// Get the initial file status.
//
	struct stat myFileStat;
	int ok = stat(szTmp.c_str(),&myFileStat);
	if(ok < 0)
	{
		UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
		remove(szTmpPng.c_str());
		remove(szTmp.c_str());	// should silently fail if exporting as PNG file
		return false;
	}
	time_t mod_time = myFileStat.st_mtime;

//	
// Fire up the image editor...
//
	ProcessInfo procInfo;
	if (!createChildProcess(imageApp.c_str(), szTmp.c_str(), &procInfo))

	{
		UT_String msg = "Unable to run program: ";  msg += imageApp + " " + szTmp;
		pFrame->showMessageBox(msg.c_str(), XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);

		// failed to spawn stuff, so do some cleanup and return failure
		remove(szTmpPng.c_str());
		remove(szTmp.c_str());	// should silently fail if exporting as PNG file
		return false;
	}

	lockGUI(d);

	while (isProcessStillAlive(procInfo))
	{
		UT_usleep(10000); // wait 10 milliseconds
		pFrame->nullUpdate();
		ok = stat(szTmp.c_str(),&myFileStat);
		if(ok == 0)
		{ 
			if(myFileStat.st_mtime != mod_time)
			{
				// wait for changes to settle (program done writing changes)
				// we use both modified time & file size, but really we
				// could just use file size as mod time doesn't appear to change for small images
				mod_time = myFileStat.st_mtime;
				off_t size = myFileStat.st_size;
				UT_usleep(100000); // wait 100 milliseconds (so program may have time to write something)
				ok = stat(szTmp.c_str(),&myFileStat);
				while((mod_time != myFileStat.st_mtime) || !size || (size > 0 && size != myFileStat.st_size))
				{
					mod_time = myFileStat.st_mtime;
					size = myFileStat.st_size;
					ok = stat(szTmp.c_str(),&myFileStat);
					UT_usleep(500000); // wait a while, let program write its data

					// just make sure the program is still running, otherwise we could get stuck in a loop
					if (!isProcessStillAlive(procInfo))
					{
						pFrame->showMessageBox("External image editor appears to have been terminated unexpectedly.", 
								XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
						//procInfo.hProcess = 0;
						goto Cleanup;
					}
				}
				mod_time = myFileStat.st_mtime;
				UT_usleep(100000); // wait a while just to make sure program is done with file

//
// OK replace the current image with this.
//
				IEGraphicFileType iegft = IEGFT_Unknown;
				FG_Graphic* pFG;
		
				UT_Error errorCode;
		
#ifdef ENABLE_BMP
//
// Convert bmp back to png (as we can not assume AbiWord has builtin BMP support [as its now an optional plugin])
// NOTE: probably looses detail/information though!!! so if possible use only PNG
//
				if (!bLeaveImageAsPNG)
				{
					if (convertBMP2PNG(szTmp.c_str(), szTmpPng.c_str()))
					{
						pFrame->showMessageBox("Unable to convert BMP image data back to PNG for AbiWord to import!", XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
						UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
						goto Cleanup;
					}
				}
#endif

				errorCode = IE_ImpGraphic::loadGraphic(szTmpPng.c_str(), iegft, &pFG);
				if(errorCode)
				{
					UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
					pFrame->showMessageBox("Error making pFG. Could not put image back into Abiword", XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
					goto Cleanup;
				}

				unlockGUI(d);

				pView->cmdUnselectSelection();
				pView->setPoint(pos);
				pView->extSelHorizontal(true, 1); // move point forward one
				errorCode = pView->cmdInsertGraphic(pFG);
				if (errorCode)
				{
					pFrame->showMessageBox("Could not put image back into Abiword", XAP_Dialog_MessageBox::b_O,XAP_Dialog_MessageBox::a_OK);
					UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
					DELETEP(pFG);
					goto Cleanup;
				}
				DELETEP(pFG);
//
// Reselect the image
//
				pView->setPoint(pos);
				pView->extSelHorizontal(true, 1); // move point forward one

				lockGUI(d);
			}
		}
	}

//
// Normal exit, delete the tempfile and return success
//
	remove(szTmpPng.c_str());
	remove(szTmp.c_str());	// should silently fail if exporting as PNG file
	unlockGUI(d);
	return true;

//
// Something went wrong.
//
 Cleanup: 
	remove(szTmpPng.c_str());
	remove(szTmp.c_str());	// should silently fail if exporting as PNG file
	unlockGUI(d);
//
// Kill the image editor.
//
	endProcess(procInfo);
	return false;
}