Ejemplo n.º 1
0
void smpd_trim_logfile_old()
{
    static int count = 0;
    FILE *fout;
    long size;
    int num_read, num_written;
    int new_size;
    char *buffer;
    char copy_cmd[4096];
    int docopy = 0;
    static copy_number = 0;
    int a, b;

    /* check every 1000 iterations */
    if (count++ % 1000)
	return;

    fout = fopen(smpd_process.dbg_filename, "rb");
    if (fout == NULL)
    {
	/*printf("unable to open the smpd log file\n");*/
	return;
    }
    fseek(fout, 0, SEEK_END);
    size = ftell(fout);
    if (size > smpd_process.dbg_file_size)
    {
	if (copy_number == 0)
	{
	    srand(smpd_getpid());
	    copy_number = rand();
	}
	sprintf(copy_cmd, "copy %s %s.%d.old", smpd_process.dbg_filename, smpd_process.dbg_filename, copy_number);
	system(copy_cmd);
	new_size = smpd_process.dbg_file_size / 2;
	buffer = MPIU_Malloc(new_size);
	if (buffer != NULL)
	{
	    fseek(fout, - (long)new_size, SEEK_END);
	    num_read = read_file(fout, buffer, new_size);
	    if (num_read == new_size)
	    {
		fclose(fout);
		fout = fopen(smpd_process.dbg_filename, "rb");
		fseek(fout, -4, SEEK_END);
		fread(&a, 1, 4, fout);
		fclose(fout);
		if (a != *(int*)&buffer[new_size-4])
		{
		    FILE *ferr = fopen("c:\\temp\\smpd.err", "a+");
		    fprintf(ferr, "last 4 bytes not equal, a: %x != %x", a, *(int*)&buffer[new_size-4]);
		    fclose(ferr);
		}
		fout = fopen(smpd_process.dbg_filename, "wb");
		if (fout != NULL)
		{
		    num_written = write_file(fout, buffer, new_size);
		    docopy = 1;
		    if (num_written != new_size)
		    {
			FILE *ferr = fopen("c:\\temp\\smpd.err", "a+");
			fprintf(ferr, "wrote %d instead of %d bytes to the smpd log file.\n", num_written, new_size);
			fclose(ferr);
		    }
		    /*
		    else
		    {
			printf("wrote %d bytes to the smpd log file.\n", num_written);
			fflush(stdout);
		    }
		    */
		    fclose(fout);
		    fout = fopen(smpd_process.dbg_filename, "rb");
		    fseek(fout, -4, SEEK_END);
		    fread(&b, 1, 4, fout);
		    fclose(fout);
		    if (b != *(int*)&buffer[new_size-4])
		    {
			FILE *ferr = fopen("c:\\temp\\smpd.err", "a+");
			fprintf(ferr, "last 4 bytes not equal, b: %x != %x", b, *(int*)&buffer[new_size-4]);
			fclose(ferr);
		    }
		}
		/*
		else
		{
		    printf("unable to truncate the smpd log file.\n");
		    fflush(stdout);
		}
		*/
	    }
	    /*
	    else
	    {
		printf("read %d instead of %d bytes from the smpd log file.\n", num_read, new_size);
		fflush(stdout);
	    }
	    */
	    MPIU_Free(buffer);
	}
	/*
	else
	{
	    printf("malloc failed to allocate %d bytes.\n", smpd_process.dbg_file_size / 2);
	    fflush(stdout);
	}
	*/
    }
    /*
    else
    {
	printf("smpd file size: %d\n", size);
	fflush(stdout);
    }
    */
    fclose(fout);
    if (docopy)
    {
	sprintf(copy_cmd, "copy %s %s.%d.new", smpd_process.dbg_filename, smpd_process.dbg_filename, copy_number);
	system(copy_cmd);
	copy_number++;
    }
}
Ejemplo n.º 2
0
int smpd_dbg_printf(char *str, ...)
{
    va_list list;
    char *indent_str;
    char *cur_str;
    int num_bytes;

    if (smpd_process.id == -1)
	smpd_init_printf();

    if (!(smpd_process.dbg_state & (SMPD_DBG_STATE_STDOUT | SMPD_DBG_STATE_LOGFILE)))
	return 0;

#ifdef HAVE_WINDOWS_H
    if (!smpd_process.bOutputInitialized)
    {
	smpd_process.hOutputMutex = CreateMutex(NULL, FALSE, SMPD_OUTPUT_MUTEX_NAME);
	smpd_process.bOutputInitialized = TRUE;
    }
    WaitForSingleObject(smpd_process.hOutputMutex, INFINITE);
#endif

    /* write the formatted string to a global buffer */

    if (smpd_process.dbg_state & SMPD_DBG_STATE_TRACE)
	indent_str = indent;
    else
	indent_str = "";

    num_bytes = 0;
    if (smpd_process.dbg_state & SMPD_DBG_STATE_PREPEND_RANK)
    {
	/* prepend output with the process tree node id */
	num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "[%02d:%d]%s", smpd_process.id, smpd_getpid(), indent_str);
    }
    else
    {
	num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "%s", indent_str);
    }
    cur_str = &smpd_process.printf_buffer[num_bytes];

    va_start(list, str);
    num_bytes += vsnprintf(cur_str, SMPD_MAX_DBG_PRINTF_LENGTH - num_bytes, str, list);
    va_end(list);

    /* strip protected fields - passwords, etc */
    smpd_clean_output(smpd_process.printf_buffer);

    if (smpd_process.dbg_state & SMPD_DBG_STATE_STDOUT)
    {
	printf("%s", smpd_process.printf_buffer);
	fflush(stdout);
    }
    if ((smpd_process.dbg_state & SMPD_DBG_STATE_LOGFILE) && (smpd_process.dbg_filename[0] != '\0'))
    {
	FILE *fout = NULL;
	smpd_trim_logfile();
	fout = fopen(smpd_process.dbg_filename, "a+");
	if (fout == NULL)
	{
	    /*smpd_process.dbg_state ^= SMPD_DBG_STATE_LOGFILE;*/
	}
	else
	{
	    setvbuf(fout, NULL, _IONBF, 0);
	    fprintf(fout, "%s", smpd_process.printf_buffer);
	    fclose(fout);
	}
    }
    

#ifdef HAVE_WINDOWS_H
    ReleaseMutex(smpd_process.hOutputMutex);
#endif
    return num_bytes;
}
Ejemplo n.º 3
0
int smpd_init_process(void)
{
#ifdef HAVE_WINDOWS_H
    HMODULE hModule;
#else
    char *homedir;
    struct stat s;
#endif
#ifdef HAVE_SIGACTION
    struct sigaction act;
#endif

    smpd_enter_fn(FCNAME);

    /* initialize the debugging output print engine */
    smpd_init_printf();

    /* tree data */
    smpd_process.parent_id = -1;
    smpd_process.id = -1;
    smpd_process.level = -1;
    smpd_process.left_context = NULL;
    smpd_process.right_context = NULL;
    smpd_process.parent_context = NULL;
    smpd_process.set = SMPDU_SOCK_INVALID_SET;

    /* local data */
#ifdef HAVE_WINDOWS_H
    hModule = GetModuleHandle(NULL);
    if (!GetModuleFileName(hModule, smpd_process.pszExe, SMPD_MAX_EXE_LENGTH)) 
	smpd_process.pszExe[0] = '\0';
#else
    smpd_process.pszExe[0] = '\0';
#endif
    strcpy(smpd_process.SMPDPassword, SMPD_DEFAULT_PASSWORD);
    smpd_process.bPasswordProtect = SMPD_FALSE;
    smpd_process.bService = SMPD_FALSE;
    smpd_get_hostname(smpd_process.host, SMPD_MAX_HOST_LENGTH);
    smpd_process.UserAccount[0] = '\0';
    smpd_process.UserPassword[0] = '\0';
    smpd_process.closing = SMPD_FALSE;
    smpd_process.root_smpd = SMPD_FALSE;

    srand(smpd_getpid());

#ifdef HAVE_SIGACTION
    memset(&act, 0, sizeof(act));
    act.sa_handler = smpd_child_handler;
#ifdef SA_NODEFER
    act.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
#else
    act.sa_flags = SA_NOCLDSTOP;
#endif
    sigaction(SIGCHLD, &act, NULL);
#endif

#ifdef HAVE_WINDOWS_H
    smpd_process.hBombDiffuseEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    smpd_process.hLaunchProcessMutex = CreateMutex(NULL, FALSE, NULL);
#else
    /* Setting the smpd_filename to default filename is done in smpd_get_smpd_data()
     * Avoid duplicating the effort
     */ 
	/*
    homedir = getenv("HOME");
    if(homedir != NULL){
        strcpy(smpd_process.smpd_filename, homedir);
        if (smpd_process.smpd_filename[strlen(smpd_process.smpd_filename)-1] != '/')
	    strcat(smpd_process.smpd_filename, "/.smpd");
        else
	    strcat(smpd_process.smpd_filename, ".smpd");
    }else{
	strcpy(smpd_process.smpd_filename, ".smpd");
    }
    if (stat(smpd_process.smpd_filename, &s) == 0)
    {
	if (s.st_mode & 00077)
	{
	    printf("smpd file, %s, cannot be readable by anyone other than the current user, please set the permissions accordingly (0600).\n", smpd_process.smpd_filename);
	    smpd_process.smpd_filename[0] = '\0';
	}
    }
    else
    {
	smpd_process.smpd_filename[0] = '\0';
    }
	*/
#endif
	smpd_process.smpd_filename[0] = '\0';
	smpd_process.passphrase[0] = '\0';
    /* smpd_init_process() should not try to get the passphrase. Just initialize the values */
    /* smpd_get_smpd_data("phrase", smpd_process.passphrase, SMPD_PASSPHRASE_MAX_LENGTH); */

    smpd_exit_fn(FCNAME);
    return SMPD_SUCCESS;
}