Exemple #1
0
/*
 * This function is called only once by the gmond.  Use to
 * initialize data structures, etc or just return SYNAPSE_SUCCESS;
 */
g_val_t
metric_init(void)
{
   g_val_t rval;
   char * dummy;
   struct stat struct_stat;

   num_cpustates = num_cpustates_func();

   /* scaling_max_freq will contain the max CPU speed if available */
   cpufreq = 0;
   if ( stat(SCALING_MAX_FREQ, &struct_stat) == 0 ) {
      cpufreq = 1;
      dummy = sys_devices_system_cpu;
      slurpfile(SCALING_MAX_FREQ, &dummy, 32);
   }

   dummy = proc_cpuinfo;
   rval.int32 = slurpfile("/proc/cpuinfo", &dummy, BUFFSIZE);
   if (proc_cpuinfo == NULL)
      proc_cpuinfo = dummy;

   if ( rval.int32 == SLURP_FAILURE )
      {
         err_msg("metric_init() got an error from slurpfile() /proc/cpuinfo");
         rval.int32 = SYNAPSE_FAILURE;
         return rval;
      }

   dummy = proc_sys_kernel_osrelease;
   rval.int32 = slurpfile("/proc/sys/kernel/osrelease", &dummy,
                          MAX_G_STRING_SIZE);
   if ( rval.int32 == SLURP_FAILURE )
      {
         err_msg("metric_init() got an error from slurpfile()");
         rval.int32 = SYNAPSE_FAILURE;
         return rval;
      }

   /* Get rid of pesky \n in osrelease */
   proc_sys_kernel_osrelease[rval.int32-1] = '\0';

   dummy = update_file(&proc_net_dev);
   if ( dummy == NULL )
      {
         err_msg("metric_init() got an error from update_file()");
         rval.int32 = SYNAPSE_FAILURE;
         return rval;
      }

   update_ifdata("metric_inint");

   rval.int32 = SYNAPSE_SUCCESS;
   return rval;
}
Exemple #2
0
/*
 * Read a zip file that has a zip comment in the end of the central
 * directory record.
 */
static void
verify(const char *refname)
{
	char *p;
	size_t s;
	struct archive *a;
	struct archive_entry *ae;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	/* Symlinks can only be extracted with the seeking reader. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("file0", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG | 0644, archive_entry_mode(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("build.sh", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG | 0755, archive_entry_mode(ae));
	assertEqualInt(23, archive_entry_size(ae));

	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
}
/*
 * This function is called only once by the gmond.  Use to 
 * initialize data structures, etc or just return SYNAPSE_SUCCESS;
 */
g_val_t
metric_init(void)
{
   struct utsname u;
   g_val_t rval;

   num_cpustates = num_cpustates_func();

   rval.int32 = slurpfile("/proc/cpuinfo", proc_cpuinfo, BUFFSIZE);
   if ( rval.int32 == SYNAPSE_FAILURE ) {
         err_msg("metric_init() got an error from slurpfile() /proc/cpuinfo");
         return rval;
   }  

   if (uname(&u) == -1) {
      strncpy(sys_osname, "unknown", MAX_G_STRING_SIZE);
      strncpy(sys_osrelease, "unknown", MAX_G_STRING_SIZE);
   } else {
      strncpy(sys_osname, u.sysname, MAX_G_STRING_SIZE);
      sys_osname[MAX_G_STRING_SIZE - 1] = '\0';
      strncpy(sys_osrelease, u.release, MAX_G_STRING_SIZE);
      sys_osrelease[MAX_G_STRING_SIZE - 1] = '\0';
   }

   rval.int32 = SYNAPSE_SUCCESS;
   return rval;
}
Exemple #4
0
static void
test_extract_length_at_end(void)
{
	const char *refname = "test_read_format_zip_length_at_end.zip";
	char *p;
	size_t s;
	struct archive *a;

	extract_reference_file(refname);

	/* Verify extraction with seeking reader. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
	verify_extract_length_at_end(a, 1);

	/* Verify extraction with streaming reader. */
	p = slurpfile(&s, refname);
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 108));
	verify_extract_length_at_end(a, 0);
	free(p);
}
static void
test_symlink(void)
{
	const char *refname = "test_read_format_zip_symlink.zip";
	char *p;
	size_t s;
	struct archive *a;
	struct archive_entry *ae;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	/* Symlinks can only be extracted with the seeking reader. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("file", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(archive_entry_is_encrypted(ae), 0);
	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("symlink", archive_entry_pathname(ae));
	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
	assertEqualInt(0, archive_entry_size(ae));
	assertEqualString("file", archive_entry_symlink(ae));
	assertEqualInt(archive_entry_is_encrypted(ae), 0);
	assertEqualIntA(a, archive_read_has_encrypted_entries(a), 0);

	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
}
Exemple #6
0
static void
test_compat_zip_6(void)
{
	const char *refname = "test_compat_zip_6.zip";
	struct archive *a;
	void *p;
	size_t s;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 7));
	compat_zip_6_verify(a);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));

	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 7));
	compat_zip_6_verify(a);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
	free(p);
}
Exemple #7
0
void process_loadstate ( void ) 
{
   char *p, **endptr;

   if ( ! slurpfile ( "/proc/loadavg" )){
      return;
   }
   p = buffer;

   now.load_one     = (short)( strtod(p, (char **)NULL) * 100.00 );

   p = skip_token(p);
   p = skip_whitespace(p);
   now.load_five    = (short)( strtod(p, (char **)NULL) * 100.00 );

   p = skip_token(p);
   p = skip_whitespace(p);
   now.load_fifteen = (short)( strtod(p, (char **)NULL) * 100.00 );

   p = skip_token(p);
   p = skip_whitespace(p);

   now.proc_run     = (short)( strtod(p, (char **)NULL) );

   while (isdigit(*p)) p++;
   p++;  /* Skip the slash */

   now.proc_total   = (short)( strtod(p, (char **)NULL) );

}
Exemple #8
0
/*
 * Issue 226: Try to reproduce hang when reading archives where the
 * length-at-end marker ends exactly on a block boundary.
 */
static void
test_compat_zip_7(void)
{
	const char *refname = "test_compat_zip_7.xps";
	struct archive *a;
	struct archive_entry *ae;
	void *p;
	size_t s;
	int i;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	for (i = 1; i < 1000; ++i) {
		assert((a = archive_read_new()) != NULL);
		assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_zip(a));
		assertEqualIntA(a, ARCHIVE_OK, read_open_memory_minimal(a, p, s, i));

		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
		assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a));

		assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
	}
	free(p);
}
char *
update_file (timely_file *tf)
{
    int rval;
    struct timeval now;
    char *bp;

    gettimeofday(&now, NULL);
    if(timediff(&now,&tf->last_read) > tf->thresh) {
        bp = tf->buffer;
        rval = slurpfile(tf->name, &bp, tf->buffersize);
        if(rval == SYNAPSE_FAILURE) {
            err_msg("update_file() got an error from slurpfile() reading %s",
                    tf->name);
        } else {
            tf->last_read = now;
            if (tf->buffer == NULL) {
               tf->buffer = bp;
               if (rval > tf->buffersize)
                  tf->buffersize = ((rval/tf->buffersize) + 1) * tf->buffersize;
            }
        }
    }
    return tf->buffer;
}
Exemple #10
0
char *cpu_scaling_governor_func( void ) {
	char *p;
	if ( stat(CPU_FREQ_SCALING_GOVERNOR, &struct_stat) == 0 ) {
		if(slurpfile(CPU_FREQ_SCALING_GOVERNOR, sys_devices_system_cpu, 32)) {
			p = sys_devices_system_cpu;
		}
	}
	return p;
}
Exemple #11
0
/**
 * A file with leading garbage (similar to an SFX file).
 */
static void
test_compat_zip_4(void)
{
	const char *refname = "test_compat_zip_4.zip";
	struct archive_entry *ae;
	struct archive *a;
	void *p;
	size_t s;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	/* SFX files require seek support. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 18));

	/* First entry. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("foo", archive_entry_pathname(ae));
	assertEqualInt(4, archive_entry_size(ae));
	assert(archive_entry_size_is_set(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0412, archive_entry_perm(ae));

	/* Second entry. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("bar", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(4, archive_entry_size(ae));
	assert(archive_entry_size_is_set(ae));
	assertEqualInt(0567, archive_entry_perm(ae));

	/* Third entry. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("baz", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(4, archive_entry_size(ae));
	assert(archive_entry_size_is_set(ae));
	assertEqualInt(0644, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));

	/* Try reading without seek support and watch it fail. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_FATAL, read_open_memory(a, p, s, 3));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
	free(p);
}
Exemple #12
0
int cpu_cur_freq_func( void ) {
	char *p;
	int val;

	if ( stat(CPU_FREQ_SCALING_CUR_FREQ, &struct_stat) == 0 ) {
		if(slurpfile(CPU_FREQ_SCALING_CUR_FREQ, sys_devices_system_cpu, 32)) {
			p = sys_devices_system_cpu;
			val = (strtol( p, (char **)NULL , 10 ) / 1000 );
		}
	}
	return val;
}
Exemple #13
0
void process_cpustate ( void ) 
{
   char *p;
   static double cpu_user, cpu_nice, cpu_system, cpu_idle;
   double d;
   double user_jiffies, nice_jiffies, system_jiffies,
          idle_jiffies, relative_jiffies, absolute_jiffies;

   if (! slurpfile ( "/proc/stat" )){
      return;
   }

   p = skip_token ( buffer );
   p = skip_whitespace ( p );

   d            = strtod( p, (char **)NULL ); 
   user_jiffies = d - cpu_user;
   cpu_user     = d;

   p = skip_token ( p );
   p = skip_whitespace ( p );

   d            = strtod( p, (char **)NULL );
   nice_jiffies = d - cpu_nice;
   cpu_nice     = d;

   p = skip_token ( p );
   p = skip_whitespace ( p );
 
   d              = strtod( p, (char **)NULL );
   system_jiffies = d - cpu_system;
   cpu_system     = d;

   p = skip_token ( p );
   p = skip_whitespace ( p );

   d            = strtod( p, (char **)NULL );
   idle_jiffies = d - cpu_idle;
   cpu_idle     = d;

   relative_jiffies = user_jiffies   + nice_jiffies +
                      system_jiffies + idle_jiffies;
 
   absolute_jiffies = cpu_user + cpu_nice +
                      cpu_system + cpu_idle;

   now.cpu_user     = (user_jiffies   /relative_jiffies)*1000.00;
   now.cpu_nice     = (nice_jiffies   /relative_jiffies)*1000.00;
   now.cpu_system   = (system_jiffies /relative_jiffies)*1000.00;
   now.cpu_idle     = (idle_jiffies   /relative_jiffies)*1000.00;
   now.cpu_aidle    = (cpu_idle       /absolute_jiffies)*1000.00;   

}
char *update_file(timely_file *tf)
{
  int now,rval;
  now = time(0);
  if(now - tf->last_read > tf->thresh) {
    rval = slurpfile(tf->name, tf->buffer, BUFFSIZE);
    if(rval == SYNAPSE_FAILURE) {
      err_msg("update_file() got an error from slurpfile() reading %s",
              tf->name);
    }
    else tf->last_read = now;
  }
  return tf->buffer;
}
Exemple #15
0
int *cpu_available_freq_func( void ) {
	char *p;
	static int val[3];

	if ( stat(CPU_FREQ_SCALING_AVAILABLE_FREQ, &struct_stat) == 0 ) {
		if(slurpfile(CPU_FREQ_SCALING_AVAILABLE_FREQ, sys_devices_system_cpu_available, 128)) {
			p = sys_devices_system_cpu_available;
			val[0] = (strtol( p, (char **)NULL , 10 ) / 1000 );
			p = skip_token(p);
			val[1] = (strtol( p, (char **)NULL , 10 ) / 1000 );
			p = skip_token(p);
			val[2] = (strtol( p, (char **)NULL , 10 ) / 1000 );
		}
	}
	return val;
}
Exemple #16
0
/*
 * Issue 185:  Test a regression that got in between 2.6 and 2.7 that
 * broke extraction of Zip entries with length-at-end.
 */
static void
test_compat_zip_3(void)
{
	const char *refname = "test_compat_zip_3.zip";
	struct archive_entry *ae;
	struct archive *a;

	extract_reference_file(refname);
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));

	/* First entry. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("soapui-4.0.0/", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_size(ae));
	assert(archive_entry_size_is_set(ae));
	assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));

	/* Second entry. */
	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("soapui-4.0.0/soapui-settings.xml", archive_entry_pathname(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(1030, archive_entry_size(ae));
	assert(archive_entry_size_is_set(ae));

	/* Extract under a different name. */
	archive_entry_set_pathname(ae, "test_3.txt");
	if(libz_enabled) {
		char *p;
		size_t s;
		assertEqualIntA(a, ARCHIVE_OK, archive_read_extract(a, ae, 0));
		/* Verify the first 12 bytes actually got written to disk correctly. */
		p = slurpfile(&s, "test_3.txt");
		assertEqualInt(s, 1030);
		assertEqualMem(p, "<?xml versio", 12);
		free(p);
	} else {
		skipping("Skipping ZIP compression check, no libz support");
	}
	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
}
Exemple #17
0
static char *update_file(timely_file *tf)
{
    int rval;
    struct timeval now;
    gettimeofday(&now, NULL);
    if(timediff(&now,&tf->last_read) > tf->thresh) {
        rval = slurpfile(tf->name, tf->buffer, BUFFSIZE);
        if(rval == SYNAPSE_FAILURE) {
            err_msg("update_file() got an error from slurpfile() reading %s",
                    tf->name);
            return (char *)SYNAPSE_FAILURE;
        }
        else 
            tf->last_read = now;
    }
    return tf->buffer;
}
Exemple #18
0
void process_meminfo ( void ) 
{
   char *p;

   if ( ! slurpfile ( "/proc/meminfo" )){
      return;
   }

   p = strstr( buffer, "MemTotal:" );
   p = skip_token(p);
   p = skip_whitespace(p);
  
   now.mem_total  = strtol( p, (char **)NULL, 10 );
 
   p = strstr( buffer, "SwapTotal:" );
   p = skip_token(p);
   p = skip_whitespace(p);

   now.swap_total = strtol( p, (char **)NULL, 10 );  

}
Exemple #19
0
void process_memstate ( void ) 
{
   char *p;

   if ( ! slurpfile( "/proc/meminfo" ) ){
      return;
   }

   p = strstr( buffer, "MemFree:" );
   p = skip_token(p);
   p = skip_whitespace(p);

   now.mem_free  = strtod( p, (char **)NULL);

   p = strstr( buffer, "MemShared:" );
   p = skip_token(p);
   p = skip_whitespace(p);
   
   now.mem_shared = strtod( p, (char **)NULL);

   p = strstr( buffer, "Buffers:" );
   p = skip_token(p);
   p = skip_whitespace(p);

   now.mem_buffers = strtod( p, (char **)NULL);

   p = strstr( buffer, "Cached:" );
   p = skip_token(p);
   p = skip_whitespace(p);

   now.mem_cached = strtod( p, (char **)NULL);

   p = strstr( buffer, "SwapFree:" );
   p = skip_token(p);
   p = skip_whitespace(p);

   now.swap_free = strtod( p, (char **)NULL );

}
Exemple #20
0
void process_cpuinfo ( void ) 
{
   char *p; 

   if ( ! slurpfile ( "/proc/cpuinfo" )) {
      return;
   }

   /* Count the number of processors */
   for ( p = buffer ; *p != '\0' ; p++ ){
      if (! strncmp( p, "processor", 9) ){
         now.cpu_num++;
      } 
   }

   p = buffer;
   p = strstr( p, "cpu MHz" );
   p = strchr( p, ':' );
   p++;
   p = skip_whitespace(p);
   now.cpu_speed = strtol( p, (char **)NULL , 10 ); 

}
Exemple #21
0
static void
test_fuzz(const struct files *filesets)
{
	const void *blk;
	size_t blk_size;
	int64_t blk_offset;
	int n;

	for (n = 0; filesets[n].names != NULL; ++n) {
		const size_t buffsize = 30000000;
		struct archive_entry *ae;
		struct archive *a;
		char *rawimage = NULL, *image = NULL, *tmp = NULL;
		size_t size = 0, oldsize = 0;
		int i, q;

		extract_reference_files(filesets[n].names);
		if (filesets[n].uncompress) {
			int r;
			/* Use format_raw to decompress the data. */
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_raw(a));
			r = archive_read_open_filenames(a, filesets[n].names, 16384);
			if (r != ARCHIVE_OK) {
				archive_read_free(a);
				if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
					skipping("Cannot uncompress fileset");
				} else {
					skipping("Cannot uncompress %s", filesets[n].names[0]);
				}
				continue;
			}
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_next_header(a, &ae));
			rawimage = malloc(buffsize);
			size = archive_read_data(a, rawimage, buffsize);
			assertEqualIntA(a, ARCHIVE_EOF,
			    archive_read_next_header(a, &ae));
			assertEqualInt(ARCHIVE_OK,
			    archive_read_free(a));
			assert(size > 0);
			if (filesets[n].names[0] == NULL || filesets[n].names[1] == NULL) {
				failure("Internal buffer is not big enough for "
					"uncompressed test files");
			} else {
				failure("Internal buffer is not big enough for "
					"uncompressed test file: %s", filesets[n].names[0]);
			}
			if (!assert(size < buffsize)) {
				free(rawimage);
				continue;
			}
		} else {
			for (i = 0; filesets[n].names[i] != NULL; ++i)
			{
				tmp = slurpfile(&size, filesets[n].names[i]);
				char *newraw = (char *)realloc(rawimage, oldsize + size);
				if (!assert(newraw != NULL))
				{
					free(rawimage);
					continue;
				}
				rawimage = newraw;
				memcpy(rawimage + oldsize, tmp, size);
				oldsize += size;
				size = oldsize;
				free(tmp);
			}
		}
		if (size == 0)
			continue;
		image = malloc(size);
		assert(image != NULL);
		if (image == NULL) {
			free(rawimage);
			return;
		}
		srand((unsigned)time(NULL));

		for (i = 0; i < 1000; ++i) {
			FILE *f;
			int j, numbytes, trycnt;

			/* Fuzz < 1% of the bytes in the archive. */
			memcpy(image, rawimage, size);
			q = (int)size / 100;
			if (q < 4)
				q = 4;
			numbytes = (int)(rand() % q);
			for (j = 0; j < numbytes; ++j)
				image[rand() % size] = (char)rand();

			/* Save the messed-up image to a file.
			 * If we crash, that file will be useful. */
			for (trycnt = 0; trycnt < 3; trycnt++) {
				f = fopen("after.test.failure.send.this.file."
				    "to.libarchive.maintainers.with.system.details", "wb");
				if (f != NULL)
					break;
#if defined(_WIN32) && !defined(__CYGWIN__)
				/*
				 * Sometimes previous close operation does not completely
				 * end at this time. So we should take a wait while
				 * the operation running.
				 */
				Sleep(100);
#endif
			}
			assertEqualInt((size_t)size, fwrite(image, 1, (size_t)size, f));
			fclose(f);

			// Try to read all headers and bodies.
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_all(a));

			if (0 == archive_read_open_memory(a, image, size)) {
				while(0 == archive_read_next_header(a, &ae)) {
					while (0 == archive_read_data_block(a,
						&blk, &blk_size, &blk_offset))
						continue;
				}
				archive_read_close(a);
			}
			archive_read_free(a);

			// Just list headers, skip bodies.
			assert((a = archive_read_new()) != NULL);
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_filter_all(a));
			assertEqualIntA(a, ARCHIVE_OK,
			    archive_read_support_format_all(a));

			if (0 == archive_read_open_memory(a, image, size)) {
				while(0 == archive_read_next_header(a, &ae)) {
				}
				archive_read_close(a);
			}
			archive_read_free(a);
}
		free(image);
		free(rawimage);
	}
}
Exemple #22
0
char *update_file(sensor_slurp *sf)
{
	int rval = slurpfile(sf->name, sf->buffer, SAMPLEBUFFERSIZE);
	return sf->buffer;
}
Exemple #23
0
int main(int argc, char *argv[])
{
  // Set locale required for Glib-2.0:
  setlocale(LC_ALL, "");

  GError *error = NULL;
  GOptionContext *context;
  
  // Process command line arguments.
  context = g_option_context_new(" - convert fortune file to omikuji file");
  g_option_context_add_main_entries(context, options, NULL);
  if (!g_option_context_parse(context, &argc, &argv, &error)) {
    g_printerr("%s\n", error->message);
    return 1;
  }
  else if (error != NULL) {
    g_printerr("%s\n", error->message);
    g_clear_error(&error);
  }

  if (inputEncoding == NULL)
    g_get_charset(&inputEncoding);

  GRegex *regex = compileRegularExpression(&error);
  if (regex == NULL) {
    g_printerr("%s\n", error->message);
    return 2;
  }

  GList *comments = NULL, *fortunes = NULL;
  gchar *buf;
  // Process input files.
  for (gint i = 1; i < argc; i++) {
    buf = slurpfile(argv[i], &error);
    if (buf == NULL) {
      if (error->domain == G_FILE_ERROR)
        g_printerr("%s\n", error->message);
      else 
        g_printerr("Failed to parse %s: %s\n", argv[i], error->message);
      if (outputFilename)
        return 3;
      else {
        g_clear_error(&error);
        continue;
      }
    }
    GMatchInfo *match_info;
    gint start = 0, end = 0, mstart, mend;
    gboolean isComment = FALSE;
    if (g_regex_match(regex, buf, 0, &match_info)) {
      while (g_match_info_matches(match_info)) {
        if (g_match_info_fetch_pos(match_info, 0, &mstart, &mend)) {
          if (start == 0) {
            start = mend;
            gunichar next = g_utf8_get_char(&buf[mend]);
            if (g_unichar_isspace(next)) {
              start++;
              if (g_unichar_type(next) == G_UNICODE_SPACE_SEPARATOR)
                isComment = TRUE;
              else
                isComment = FALSE;
            }
            else
              isComment = TRUE;
            if (!g_match_info_next(match_info, &error))
              g_clear_error(&error);
          }
          else {
            if ((isComment && !skipComments) || !isComment) {
              end = mstart;
              GString *string = g_string_sized_new(end - start);
              for (gint j = start; j < end; j++)
                g_string_append_c(string, buf[j]);
              g_string_append_c(string, '\0');
              if (isComment)
                comments = g_list_append(comments, string);
              else
                fortunes = g_list_append(fortunes, string);
            }
            start = 0; end = 0;
          }
        }
      }
      g_match_info_free(match_info);
      if (outputFilename == NULL) {
        outputFilename = g_path_get_basename(argv[i]);
        if (outputFilename) {
          gpointer temp = g_try_realloc(outputFilename, strlen(outputFilename) + 5);
          if (temp) {
            gboolean delStr = TRUE;
            outputFilename = (gchar *) temp;
            strcat(outputFilename, ".omi");
            writeOmikujiFile(outputFilename, comments, fortunes);
            if (comments) {
              g_list_foreach(comments, stringDelete, &delStr);
              g_list_free(comments);
              comments = NULL;
            }
            g_list_foreach(fortunes, stringDelete, &delStr);
            g_list_free(fortunes);
            fortunes = NULL;
          }
          g_free(outputFilename);
          outputFilename = NULL;
        }
      }
    }
    g_free(buf);
  }

  g_regex_unref(regex);

  if (outputFilename) {
    gboolean delStr = TRUE;
    writeOmikujiFile(outputFilename, comments, fortunes);
    if (comments) {
      g_list_foreach(comments, stringDelete, &delStr);
      g_list_free(comments);
      comments = NULL;
    }
    g_list_foreach(fortunes, stringDelete, &delStr);
    g_list_free(fortunes);
    fortunes = NULL;
  }

  return 0;
}
Exemple #24
0
int main(int argc, char **argv)
{
	static const int limit = sizeof(tests) / sizeof(tests[0]);
	int i, tests_run = 0, tests_failed = 0, opt;
	time_t now;
	char *progname, *p;
	char tmpdir[256];
	char tmpdir_timestamp[256];

	/*
	 * Name of this program, used to build root of our temp directory
	 * tree.
	 */
	progname = p = argv[0];
	while (*p != '\0') {
		if (*p == '/')
			progname = p + 1;
		++p;
	}

	/* Get the target program from environment, if available. */
	testprog = getenv("BSDCPIO");

	/* Get the directory holding test files from environment. */
	refdir = getenv("BSDCPIO_TEST_FILES");

	/*
	 * Parse options.
	 */
	while ((opt = getopt(argc, argv, "kp:qr:")) != -1) {
		switch (opt) {
		case 'k':
			dump_on_failure = 0;
			break;
		case 'p':
			testprog = optarg;
			break;
		case 'q':
			quiet_flag++;
			break;
		case 'r':
			refdir = optarg;
			break;
		case '?':
		default:
			usage(progname);
		}
	}
	argc -= optind;
	argv += optind;

	/*
	 * Sanity-check that our options make sense.
	 */
	if (testprog == NULL)
		usage(progname);


	/*
	 * Create a temp directory for the following tests.
	 * Include the time the tests started as part of the name,
	 * to make it easier to track the results of multiple tests.
	 */
	now = time(NULL);
	for (i = 0; i < 1000; i++) {
		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
		    "%Y-%m-%dT%H.%M.%S",
		    localtime(&now));
		sprintf(tmpdir, "/tmp/%s.%s-%03d", progname, tmpdir_timestamp, i);
		if (mkdir(tmpdir,0755) == 0)
			break;
		if (errno == EEXIST)
			continue;
		fprintf(stderr, "ERROR: Unable to create temp directory %s\n",
		    tmpdir);
		exit(1);
	}

	/*
	 * If the user didn't specify a directory for locating
	 * reference files, use the current directory for that.
	 */
	if (refdir == NULL) {
		systemf("/bin/pwd > %s/refdir", tmpdir);
		refdir = slurpfile(NULL, "%s/refdir", tmpdir);
		p = refdir + strlen(refdir);
		while (p[-1] == '\n') {
			--p;
			*p = '\0';
		}
	}

	/*
	 * Banner with basic information.
	 */
	if (!quiet_flag) {
		printf("Running tests in: %s\n", tmpdir);
		printf("Reference files will be read from: %s\n", refdir);
		printf("Running tests on: %s\n", testprog);
	}

	/*
	 * Run some or all of the individual tests.
	 */
	if (argc == 0) {
		/* Default: Run all tests. */
		for (i = 0; i < limit; i++) {
			if (test_run(i, tmpdir))
				tests_failed++;
			tests_run++;
		}
	} else {
		while (*(argv) != NULL) {
			i = atoi(*argv);
			if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) {
				printf("*** INVALID Test %s\n", *argv);
				usage(progname);
			} else {
				if (test_run(i, tmpdir))
					tests_failed++;
				tests_run++;
			}
			argv++;
		}
	}

	/*
	 * Report summary statistics.
	 */
	if (!quiet_flag) {
		printf("\n");
		printf("%d of %d test groups reported failures\n",
		    tests_failed, tests_run);
		printf(" Total of %d individual tests failed.\n", failures);
		printf(" Total of %d individual tests were skipped.\n", skips);
	}
	return (tests_failed);
}
Exemple #25
0
/**
 * Issue 152: A file generated by a tool that doesn't really
 * believe in populating local file headers at all.  This
 * is only readable with the seeking reader.
 */
static void
test_compat_zip_5(void)
{
	const char *refname = "test_compat_zip_5.zip";
	struct archive_entry *ae;
	struct archive *a;
	void *p;
	size_t s;

	extract_reference_file(refname);
	p = slurpfile(&s, refname);

	/* Verify with seek support.
	 * Everything works correctly here. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 18));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Metadata/Job_PT.xml", archive_entry_pathname(ae));
	assertEqualInt(3559, archive_entry_size(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Metadata/MXDC_Empty_PT.xml", archive_entry_pathname(ae));
	assertEqualInt(456, archive_entry_size(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Metadata/Page1_Thumbnail.JPG", archive_entry_pathname(ae));
	assertEqualInt(1495, archive_entry_size(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));
	/* TODO: Read some of the file data and verify it.
	   The code to read uncompressed Zip entries with "file at end" semantics
	   is tricky and should be verified more carefully. */

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Pages/_rels/1.fpage.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Pages/1.fpage", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Resources/Fonts/3DFDBC8B-4514-41F1-A808-DEA1C79BAC2B.odttf", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/_rels/FixedDocument.fdoc.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/FixedDocument.fdoc", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("_rels/FixedDocumentSequence.fdseq.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("FixedDocumentSequence.fdseq", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("_rels/.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("[Content_Types].xml", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));

	/* Try reading without seek support. */
	assert((a = archive_read_new()) != NULL);
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 3));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Metadata/Job_PT.xml", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_size(ae));
	assert(!archive_entry_size_is_set(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Metadata/MXDC_Empty_PT.xml", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_size(ae));
	assert(!archive_entry_size_is_set(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Metadata/Page1_Thumbnail.JPG", archive_entry_pathname(ae));
	assertEqualInt(0, archive_entry_size(ae));
	assert(!archive_entry_size_is_set(ae));
	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
	assertEqualInt(0666, archive_entry_perm(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Pages/_rels/1.fpage.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Pages/1.fpage", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/Resources/Fonts/3DFDBC8B-4514-41F1-A808-DEA1C79BAC2B.odttf", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/_rels/FixedDocument.fdoc.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("Documents/1/FixedDocument.fdoc", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("_rels/FixedDocumentSequence.fdseq.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("FixedDocumentSequence.fdseq", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("_rels/.rels", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
	assertEqualString("[Content_Types].xml", archive_entry_pathname(ae));

	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));

	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
	free(p);
}