Exemple #1
0
void open_mud_log( void )
{
   FILE *error_log;
   char buf[256];
   int logindex;

   // Stop trying after 100K log files. If you have this many it's not good anyway.
   for( logindex = 1000; logindex < 100000; ++logindex )
   {
      snprintf( buf, 256, "../log/%d.log", logindex );
      if( exists_file( buf ) )
         continue;
      else if( logindex < 100000 )
         break;
      else
      {
         fprintf( stderr, "%s", "You have too damn many log files! Clean them up!" );
         exit( 1 );
      }
   }

   if( !( error_log = fopen( buf, "a" ) ) )
   {
      fprintf( stderr, "Unable to append to %s.", buf );
      exit( 1 );
   }

   dup2( fileno( error_log ), STDERR_FILENO );
   FCLOSE( error_log );
}
Exemple #2
0
char* ask_load_game() {
	
	/* Allocating the string for the return of the file name */
	char* filename = malloc(MAX_FILE_NAME * sizeof(char));
	
	printf("\nEnter the loading file's name: ");
	
	/* Getting the file name from the user */
	fgets(filename, MAX_FILE_NAME, stdin);
	
	/* Ungetting the last character readed if the string entered is too long */
	ungetc(filename[strlen(filename)-1], stdin);
	
	/* Emptying the buffer (so that extra characters won't be used for the next answer */
	empty_buffer();
	
	/* If the last character of the name is a new line (that fgets can put into a string), replacing it with a string terminating character */
	if(filename[strlen(filename)-1] == NL)
		filename[strlen(filename)-1] = '\0';

	/* Verifying that the file exixts */
	if(exists_file(filename, R_OK) == NO) {
		
			/* If not, printing an error, freeing the string and returning NULL */
			printf("\nThis file does not exist or is not valid\n\n");
			free(filename);
			return NULL;
		}
	
	return filename;
}
Exemple #3
0
void load_coefs(float coefs[]) {
	short i;
	/* If the file containing the coefficients exists */
	if(exists_file(COEFS_FILENAME, R_OK)) {
		/* Opening it */
		FILE* f1 = fopen(COEFS_FILENAME, "r");
		
		/* It there was a problem during the opening */
		if(!f1) {
		perror("FILE : Error opening the file");
		exit(EXIT_FAILURE);
		}
		
		/* Otherwise, reading the coefficients and storing them in the array */
		for(i=0; i<COEF_NB; i++)
			fscanf(f1, "%f", &coefs[i]);
		
		fclose(f1);
	}
	
	/* If the file does not exist, setting the coefficients to 1 */
	else {
		for(i=0; i<COEF_NB; i++)
			coefs[i] = 1;
	}
}
Exemple #4
0
int ask_save_game(Game* game) {
	
	/* Testing whether the game is saved of not. If not, asking the players if they want to save it */
	if(game->saved == NO) {
		
		/* If the answer is yes, retrieving the name of the file */
		if(ask_YN("Do you wish to save the game?") == YES) {
			
			char filename[MAX_FILE_NAME];
			printf("\nType the saving file's name (maximum of %d characters) : ", MAX_FILE_NAME);
			
			fgets(filename, MAX_FILE_NAME, stdin);
			
			/* Ungetting the last character readed if the string entered is too long */
			ungetc(filename[strlen(filename)-1], stdin);
			
			/* Emptying the buffer (so that extra characters won't be used for the next answer */
			empty_buffer();
			
			/* If the last character of the name is a new line (that fgets can put into a string), replacing it with a string terminating character */
			if(filename[strlen(filename)-1] == NL)
				filename[strlen(filename)-1] = '\0';
			
			/* If the file already exists and can be opened for writing, asking if the user wants to continue */
			if(exists_file(filename, W_OK) == YES) {
				printf("\nThis file already exists, saving the game will erase all its contents");
				
				/* If he doesn't want to, returning NO for the saving */
				if(ask_YN("\nDo you wish to continue?") == NO)
					return NO;
			}
			
			/* Saving the game, and if it has been saved, printing a message */
			if(game_saving(game, filename))
				printf("\nYour game has been saved in the file '%s'\n", filename);
			
			/* If it has not been saved, printing an error */
			else
				printf("\nSomething went wrong, the game has not been saved - don't get mad at me...");
		}
		
		/* If player don't want to save the game, returning NO for the saving */
		else
			return NO;
	}
	
	/* If the game was already saved, printing a message */
	else {
		printf("\nThe current game is already saved");
		
		/* Returning no for the saving */
		return NO;
	}
	
	/* Returning YES if it has been saved */
	return YES;
}
Exemple #5
0
/*
 * Check if the SSL/TLS certificate exists in the certificates file.
 */
int
check_cert(X509 *pcert, unsigned char *pmd, unsigned int *pmdlen)
{
	int n, r;
	FILE *fd;
	char b;
	char *certf;
	X509 *cert;
	unsigned char md[EVP_MAX_MD_SIZE];
	unsigned int mdlen;

	r = 0;
	cert = NULL;

	n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_CERTS);

	if (env.pathmax != -1 && n > env.pathmax)
		fatal(ERROR_PATHNAME,
		    "pathname limit %ld exceeded: %d\n", env.pathmax, n);

	certf = (char *)xmalloc((n + 1) * sizeof(char));
	snprintf(certf, n + 1, "%s/%s", env.home, PATHNAME_CERTS);

	if (!exists_file(certf)) {
		xfree(certf);
		return 0;
	}

	fd = fopen(certf, "r");

	xfree(certf);

	if (fd == NULL)
		return -1;

	while ((cert = PEM_read_X509(fd, &cert, NULL, NULL)) != NULL) {
		if (X509_subject_name_cmp(cert, pcert) != 0 ||
		    X509_issuer_name_cmp(cert, pcert) != 0)
			continue;

		if (!X509_digest(cert, EVP_md5(), md, &mdlen) ||
		    *pmdlen != mdlen)
			continue;

		if (memcmp(pmd, md, mdlen) != 0) {
			r = -1;
			break;
		}
		r = 1;
		break;
	}

	fclose(fd);
	X509_free(cert);

	return r;
}
Exemple #6
0
void reset_colors( char_data * ch )
{
   if( !ch->isnpc(  ) )
   {
      char filename[256];

      snprintf( filename, 256, "%s%s", COLOR_DIR, "default" );
      if( exists_file( filename ) )
      {
         FILE *fp;
         int max_colors = 0;

         if( !( fp = fopen( filename, "r" ) ) )
         {
            memcpy( &ch->pcdata->colors, &default_set, sizeof( default_set ) );
            return;
         }

         while( !feof( fp ) )
         {
            char *word = fread_word( fp );

            if( !str_cmp( word, "MaxColors" ) )
            {
               max_colors = fread_number( fp );
               continue;
            }

            if( !str_cmp( word, "Colors" ) )
            {
               for( int x = 0; x < max_colors; ++x )
                  ch->pcdata->colors[x] = fread_number( fp );
               continue;
            }

            if( !str_cmp( word, "End" ) )
            {
               FCLOSE( fp );
               return;
            }
         }
         FCLOSE( fp );
         return;
      }
      else
         memcpy( &ch->pcdata->colors, &default_set, sizeof( default_set ) );
   }
   else
      log_printf( "%s: Attempting to reset NPC colors: %s", __func__, ch->short_descr );
}
Exemple #7
0
/*
 * Check if the SSL/TLS certificate exists in the certificates file.
 */
int
check_cert(X509 *pcert, unsigned char *pmd, unsigned int *pmdlen)
{
	int r;
	FILE *fd;
	char *certf;
	X509 *cert;
	unsigned char md[EVP_MAX_MD_SIZE];
	unsigned int mdlen;

	r = 0;
	cert = NULL;

	certf = get_filepath("certificates");
	if (!exists_file(certf)) {
		xfree(certf);
		return 0;
	}
	fd = fopen(certf, "r");
	xfree(certf);
	if (fd == NULL)
		return -1;

	while ((cert = PEM_read_X509(fd, &cert, NULL, NULL)) != NULL) {
		if (X509_subject_name_cmp(cert, pcert) != 0 ||
		    X509_issuer_and_serial_cmp(cert, pcert) != 0)
			continue;

		if (!X509_digest(cert, EVP_md5(), md, &mdlen) ||
		    *pmdlen != mdlen)
			continue;

		if (memcmp(pmd, md, mdlen) != 0) {
			r = -1;
			break;
		}
		r = 1;
		break;
	}

	fclose(fd);
	X509_free(cert);

	return r;
}
Exemple #8
0
//////////////////////////////////
// Read the verifier output (from hardware execution) and display the results
//////////////////////////////////
void verify_miss_rate(const std::string kernelname,
                      const std::string benchname) {
	std::string filename = output_dir+"/"+benchname+"/"+kernelname+".prof";
	
	// Test if the file exists
	std::ifstream exists_file(filename);
	if (!exists_file) {
		message("No verifier data information available, skipping verification");
		return;
	}
	
	// Parse the file line by line
	unsigned counter = 0;
	unsigned long data;
	unsigned long miss = 0;
	unsigned long hit = 0;
	std::ifstream input_file(filename);
	while (input_file >> data) {
		if (counter == 0) { hit = data; }
		if (counter == 1) { miss = data; }
		counter++;
	}
	
	// Prepare to append to the output file
	std::ofstream file;
	file.open(output_dir+"/"+benchname+"/"+kernelname+".out", std::fstream::app);
	file << std::endl;
	
	// Output verification data to stdout
	message("Cache miss rate according to verification data:");
	float miss_rate = 100*miss/(double)(miss+hit);
	std::cout << "### \t Total accesses: " << (miss+hit) << std::endl;
	std::cout << "### \t Misses: " << miss << std::endl;
	std::cout << "### \t Hits: " << hit << std::endl;
	std::cout << "### \t Miss rate: " << miss_rate << "%" << std::endl;
	
	// Output verification data to file
	file << "verified_misses: " << miss << std::endl;
	file << "verified_hits: " << hit << std::endl;
	file << "verified_miss_rate: " << miss_rate << std::endl;
	
	// Close the output file
	file.close();
}
Exemple #9
0
/*
 * Create a file with the specified permissions.
 */
int
create_file(char *fname, mode_t mode)
{
    int fd;

    fd = 0;

    if (!exists_file(fname)) {
        fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
        if (fd == -1) {
            error("could not create file %s; %s\n", fname,
                  strerror(errno));
            return -1;
        }
        close(fd);
    }

    return 0;
}
Exemple #10
0
//////////////////////////////////
// Function to read the hardware settings from a file
//////////////////////////////////
Settings get_settings(void) {
	std::string filename = config_dir+"/"+"current.conf";
	
	// Test if the file exists
	std::ifstream exists_file(filename);
	if (!exists_file) {
		std::cout << "### Error: could not read settings file '" << filename << "'" << std::endl;
		message("");
		exit(0);
	}
	
	// Open the settings file for reading
	std::ifstream input_file(filename);
	
	// Then proceed to the parse the data
	std::string identifier;
	unsigned line_size, cache_bytes, cache_ways, num_mshr, mem_latency, mem_latency_stddev;
	input_file >> identifier >> line_size;
	input_file >> identifier >> cache_bytes;
	input_file >> identifier >> cache_ways;
	input_file >> identifier >> num_mshr;
	input_file >> identifier >> mem_latency;
	input_file >> identifier >> mem_latency_stddev;
	
	// Store the data in the settings data-structure
	Settings hardware = {
	  line_size,
	  cache_bytes,
	  cache_bytes/line_size,
	  cache_ways,
	  cache_bytes/(line_size*cache_ways),
	  num_mshr,
	  NUM_CORES,
	  WARP_SIZE,
	  MAX_ACTIVE_THREADS,
	  MAX_ACTIVE_BLOCKS,
	  mem_latency,
	  mem_latency_stddev
	};
	
	// Close the file and return
	return hardware;
}
Exemple #11
0
//////////////////////////////////
// Function to parse the memory access trace (input)
//////////////////////////////////
Dim3 read_file(std::vector<Thread> &threads,
               const std::string kernelname,
               const std::string benchname,
               const std::string suitename) {
	unsigned num_threads = 0;
	unsigned num_accesses = 0;
	std::string filename = trace_dir+"/"+suitename+"/"+benchname+"/"+kernelname+".trc";
	
	// Test if the file exists, return if it does not exist
	std::ifstream exists_file(filename);
	if (!exists_file) {
		return Dim3({0,0,0});
	}
	
	// Open the file for reading
	std::cout << SPLIT_STRING << std::endl;
	message("");
	std::cout << "### Reading the trace file for '" << kernelname << "'...";
	std::ifstream input_file(filename);
	
	// First get the blocksize from the trace file
	std::string temp_string;
	Dim3 blockdim;
	input_file >> temp_string >> blockdim.x >> blockdim.y >> blockdim.z;
	
	// Then proceed to the actual trace data
	unsigned thread, direction, bytes;
	unsigned long address;
    unsigned pc, block;
	while (input_file >> thread >> address >> bytes >> pc >> block) {
        direction = 0;
		
		// Consider only loads (stores are not cached in Fermi's L1 caches)
		if (direction == 0) {
		
			// Count the number of accesses and threads
			num_accesses++;
			num_threads = (num_threads > thread) ? num_threads : thread + 1;
			
			// Store the data in the Thread class
			Access access = { direction, address, 1, bytes, address+bytes-1, pc, block };
            if (access.address == 0)
                access.width = 0;
			threads[thread].append_access(access);
		}
	}
	std::cout << "done" << std::endl;
	
	// Test if the file actually contained memory accesses - exit otherwise
	if (!(num_accesses > 0 && num_threads > 0)) {
		std::cout << "### Error: '" << filename << "' is not a valid memory access trace" << std::endl;
		message("");
		return Dim3({0,0,0});
	}
	
	// Reduce the size of the threads vector
	threads.resize(num_threads);
	threads.shrink_to_fit();
	
	// Print additional information and return the threadblock dimensions
	std::cout << "### Blocksize: (" << blockdim.x << "," << blockdim.y << "," << blockdim.z << ")" << std::endl;
	std::cout << "### Total threads: " << num_threads << std::endl;
	std::cout << "### Total memory accesses: " << num_accesses << "" << std::endl;
	return blockdim;
}
Exemple #12
0
void directory_check( void )
{
   char buf[256];
   size_t x;

   // Successful directory check will drop this file in the area dir once done.
   if( exists_file( "DIR_CHECK_PASSED" ) )
      return;

   fprintf( stderr, "Checking for required directories...\n" );

   // This should really never happen but you never know.
   if( chdir( "../log" ) )
   {
      fprintf( stderr, "Creating required directory: ../log\n" );
      snprintf( buf, 256, "mkdir ../log" );

      if( system( buf ) )
      {
         fprintf( stderr, "FATAL ERROR :: Unable to create required directrory: ../log\n" );
         exit( 1 );
      }
   }

   for( x = 0; x < sizeof( directory_table ) / sizeof( directory_table[0] ); ++x )
   {
      if( chdir( directory_table[x] ) )
      {
         snprintf( buf, 256, "mkdir %s", directory_table[x] );
         log_printf( "Creating required directory: %s", directory_table[x] );

         if( system( buf ) )
         {
            log_printf( "FATAL ERROR :: Unable to create required directory: %s. Must be corrected manually.", directory_table[x] );
            exit( 1 );
         }
      }

      if( !str_cmp( directory_table[x], PLAYER_DIR ) )
      {
         short alpha_loop;
         char dirname[256];

         for( alpha_loop = 0; alpha_loop <= 25; ++alpha_loop )
         {
            snprintf( dirname, 256, "%s%c", PLAYER_DIR, 'a' + alpha_loop );
            if( chdir( dirname ) )
            {
               log_printf( "Creating required directory: %s", dirname );
               snprintf( buf, 256, "mkdir %s", dirname );

               if( system( buf ) )
               {
                  log_printf( "FATAL ERROR :: Unable to create required directory: %s. Must be corrected manually.", dirname );
                  exit( 1 );
               }
            }
            else
               chdir( ".." );
         }
      }
   }

   // Made it? Sweet. Drop the check file so we don't do this on every last reboot.
   log_string( "Directory check passed." );
   chdir( "../area" );
   system( "touch DIR_CHECK_PASSED" );
}
Exemple #13
0
/*
 * IMAPFilter: an IMAP mail filtering utility.
 */
int
main(int argc, char *argv[])
{
	int c;
	char *cafile = NULL, *capath = NULL;

	setlocale(LC_CTYPE, "");

	opts.verbose = 0;
	opts.interactive = 0;
	opts.log = NULL;
	opts.config = NULL;
	opts.oneline = NULL;
	opts.debug = NULL;

	opts.truststore = NULL;
	if (exists_dir("/etc/ssl/certs"))
		opts.truststore = "/etc/ssl/certs";
	else if (exists_file("/etc/ssl/cert.pem"))
		opts.truststore = "/etc/ssl/cert.pem";

	env.home = NULL;
	env.pathmax = -1;

	while ((c = getopt(argc, argv, "Vc:d:e:il:t:v?")) != -1) {
		switch (c) {
		case 'V':
			version();
			/* NOTREACHED */
			break;
		case 'c':
			opts.config = optarg;
			break;
		case 'd':
			opts.debug = optarg;
			break;
		case 'e':
			opts.oneline = optarg;
			break;
		case 'i':
			opts.interactive = 1;
			break;
		case 'l':
			opts.log = optarg;
			break;
		case 't':
			opts.truststore = optarg;
			break;
		case 'v':
			opts.verbose = 1;
			break;
		case '?':
		default:
			usage();
			/* NOTREACHED */
			break;
		}
	}

	get_pathmax();
	open_debug();
	create_homedir();
	catch_signals();
	open_log();
	if (opts.config == NULL)
		opts.config = get_filepath("config.lua");

	buffer_init(&ibuf, INPUT_BUF);
	buffer_init(&obuf, OUTPUT_BUF);
	buffer_init(&nbuf, NAMESPACE_BUF);
	buffer_init(&cbuf, CONVERSION_BUF);

	regexp_compile(responses);

	SSL_library_init();
	SSL_load_error_strings();
	ssl3ctx = SSL_CTX_new(SSLv3_client_method());
	ssl23ctx = SSL_CTX_new(SSLv23_client_method());
	tls1ctx = SSL_CTX_new(TLSv1_client_method());
#if OPENSSL_VERSION_NUMBER >= 0x01000100fL
	tls11ctx = SSL_CTX_new(TLSv1_1_client_method());
	tls12ctx = SSL_CTX_new(TLSv1_2_client_method());
#endif
	if (exists_dir(opts.truststore))
		capath = opts.truststore;
	else if (exists_file(opts.truststore))
		cafile = opts.truststore;
	SSL_CTX_load_verify_locations(ssl3ctx, cafile, capath);
	SSL_CTX_load_verify_locations(ssl23ctx, cafile, capath);
	SSL_CTX_load_verify_locations(tls1ctx, cafile, capath);
#if OPENSSL_VERSION_NUMBER >= 0x01000100fL
	SSL_CTX_load_verify_locations(tls11ctx, cafile, capath);
	SSL_CTX_load_verify_locations(tls12ctx, cafile, capath);
#endif

	start_lua();
#if LUA_VERSION_NUM < 502
	{
		list *l;
		session *s;

		l = sessions;
		while (l != NULL) {
			s = l->data;
			l = l->next;

			request_logout(s);
		}
	}
#endif
	stop_lua();

	SSL_CTX_free(ssl3ctx);
	SSL_CTX_free(ssl23ctx);
	SSL_CTX_free(tls1ctx);
#if OPENSSL_VERSION_NUMBER >= 0x01000100fL
	SSL_CTX_free(tls11ctx);
	SSL_CTX_free(tls12ctx);
#endif
	ERR_free_strings();

	regexp_free(responses);

	buffer_free(&ibuf);
	buffer_free(&obuf);
	buffer_free(&nbuf);
	buffer_free(&cbuf);

	xfree(env.home);

	close_log();
	close_debug();

	exit(0);
}