Esempio n. 1
0
void set_thread_affinity(int cpu_number)
{
	int ret = 0;
	cpu_set_t mask;
	CPU_ZERO(&mask);
	CPU_SET(cpu_number%cpu_online, &mask);
	if ((ret = pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask)) != 0)
	{
		posix_error(ret, "set thread affinity failed");
	}
}
Esempio n. 2
0
static PyObject *
_uptime_secs(PyObject *self, PyObject *args)
{
    int iRslt = -1;
    double cur_uptime = 0.0;
    struct sysinfo si;

	// Make sure that no arguments were specified
    if(!PyArg_ParseTuple(args, ":secs"))
        return NULL;
    errno = 0;
    iRslt = sysinfo(&si);
    if(iRslt < 0)
        return posix_error();
    cur_uptime = (double)(si.uptime);

#ifdef DEBUG
	// If we are in debug mode, check to see if the uptime has
	// been overriden.
    if (override_secs != 0.0)
 	    cur_uptime = override_secs;
#endif

	// Now check for a wrap.
	if (cur_uptime < (last_uptime - 1.0)) {
#ifdef VERBOSE
 	        printf("Detected a wrap! (%lf and %lf).\n", cur_uptime, last_uptime);
#endif
	        // OK, we've got a wrap.
     	        if (last_uptime > max_uptime) {
		      max_uptime = last_uptime;
#ifdef VERBOSE
		      printf("max_uptime is now set to %lf.\n", max_uptime);
#endif
		}
		uptime_offset += (unsigned long) max_uptime;
#ifdef VERBOSE
		printf("uptime_offset is now %ld.\n", uptime_offset);
#endif
	}
	
	last_uptime = cur_uptime;
   
	// If we've had a wrap, add the uptime_offset to the current uptime
	if (uptime_offset != 0) {
   	        cur_uptime += (double) uptime_offset;
	}
#ifdef EXTRA_VERBOSE
	printf("uptime_secs: Returning %lf.\n", cur_uptime);
#endif
        return Py_BuildValue("d", cur_uptime);
}
static PyObject *
subterfugue_atcallstop(PyObject *self, PyObject *args)
{
  int pid, stopsig, result;

  if (!PyArg_Parse(args, "(ii)", &pid, &stopsig))
    return NULL;

  result = atcallstop(pid, stopsig);
  if (result == -1)
    return posix_error();
  return Py_BuildValue("i", result);
}
Esempio n. 4
0
static PyObject *
_uptime_raw_secs(PyObject *self, PyObject *args)
{
    int iRslt = -1;
    double cur_uptime = 0.0;
    struct sysinfo si;
    // Make sure that no arguments were specified:
    if (!PyArg_ParseTuple(args, ":secs"))
        return NULL;
    errno = 0;
    iRslt = sysinfo(&si);
    if(iRslt < 0)
        return posix_error();
    cur_uptime = (double)(si.uptime);
    return Py_BuildValue("d", cur_uptime);
}
void Pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
	int rc;

	if ((rc = pthread_mutex_init(mutex, attr)) != 0)
		posix_error(rc, "Pthread_mutex_init error");
}
void Pthread_mutex_unlock(pthread_mutex_t *mutex) {
	int rc;

	if ((rc = pthread_mutex_unlock(mutex)) != 0)
		posix_error(rc, "Pthread_mutex_unlock error");
}
void Pthread_join(pthread_t tid, void **thread_return) {
	int rc;

	if ((rc = pthread_join(tid, thread_return)) != 0)
		posix_error(rc, "Pthread_join error");
}
/* $begin detach */
void Pthread_detach(pthread_t tid) {
	int rc;

	if ((rc = pthread_detach(tid)) != 0)
		posix_error(rc, "Pthread_detach error");
}
Esempio n. 9
0
void Pthread_detach(pthread_t tid)
{
	int rt = pthread_detach(tid);
	if( rt != 0)
		posix_error(rt, "pthread_detach error");
}
void Pthread_cancel(pthread_t tid) {
	int rc;

	if ((rc = pthread_cancel(tid)) != 0)
		posix_error(rc, "Pthread_cancel error");
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
	struct thread_data *threads;
	struct thread_data *thread;
	int i, ret, ch;

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0)
		{
			usage_error(argv[0]);
		}
		init_program_parameter(argc, argv);
	}

	program_parameter(argv[0]);

	create_matrix(&matrix_a);
	create_matrix(&matrix_b);
	create_matrix(&matrix_c);
	create_matrix(&matrix_d);
	random_matrix(matrix_a);
	random_matrix(matrix_b);

	nonmal_matrix_multipy(matrix_a, matrix_b, matrix_d);

	threads = (struct thread_data *)malloc(pthread_max * sizeof(struct thread_data));
	if (threads == NULL)
	{
		unix_error("malloc threads failed");
	}

	cpu_online = sysconf(_SC_NPROCESSORS_CONF);

	for(i = 0; i < pthread_max; i++)
	{
		thread = threads + i;
		thread->index = i;
		if ((ret = pthread_create(&thread->thread_id, NULL, thread_func, thread)) != 0)
		{
			posix_error(ret, "pthread_create failed");
		}
	}

	for(i = 0; i < pthread_max; i++)
	{
		thread = threads + i;
		if ((ret = pthread_join(thread->thread_id, NULL)) != 0)
		{
			posix_error(ret, "pthread_join failed");
		}
	}

	if (matrix_equal(matrix_c, matrix_d) == 0)
	{
		unix_error("runtime error");
	}
	if (dump)
	{
		dump_matrix("matrix A", matrix_a);
		dump_matrix("matrix B", matrix_b);
		dump_matrix("matrix C", matrix_c);
		dump_matrix("matrix D", matrix_d);
	}
	statistics(threads);

	free_matrix(matrix_a);
	free_matrix(matrix_b);
	free_matrix(matrix_c);
	free_matrix(matrix_d);
	free(threads);
	return 0;
}
Esempio n. 12
0
void Pthread_cond_destroy(pthread_cond_t *cond) {
	int rc;

	if ((rc = pthread_cond_destroy(cond)) != 0)
		posix_error(rc, "Pthread_cond_destroy");
}
Esempio n. 13
0
bool MemFile::open (const std::string &path_, bool uncompress)
{
	MEMORY mem(MAX_IMAGE_SIZE + 1);
	size_t uRead = 0;

	// Check if zlib is available
#ifdef HAVE_ZLIBSTAT
	bool have_zlib = true;
#elif defined(HAVE_ZLIB)
	bool have_zlib = CheckLibrary("zlib", "zlibVersion") && zlibVersion()[0] == ZLIB_VERSION[0];
#else
	bool have_zlib = false;
#endif

#ifdef HAVE_ZLIB
	// Try opening as a zip file
	if (uncompress && have_zlib)
	{
		unzFile hfZip = unzOpen(path_.c_str());
		if (hfZip)
		{
			int nRet;
			unz_file_info sInfo;
			uLong ulMaxSize = 0;

			// Iterate through the contents of the zip looking for a file with a suitable size
			for (nRet = unzGoToFirstFile(hfZip); nRet == UNZ_OK; nRet = unzGoToNextFile(hfZip))
			{
				char szFile[MAX_PATH];

				// Get details of the current file
				unzGetCurrentFileInfo(hfZip, &sInfo, szFile, MAX_PATH, nullptr, 0, nullptr, 0);

				// Ignore directories and empty files
				if (!sInfo.uncompressed_size)
					continue;

				// If the file extension is recognised, read the file contents
				// ToDo: GetFileType doesn't really belong here?
				if (GetFileType(szFile) != ftUnknown && unzOpenCurrentFile(hfZip) == UNZ_OK)
				{
					nRet = unzReadCurrentFile(hfZip, mem, static_cast<unsigned int>(mem.size));
					unzCloseCurrentFile(hfZip);
					break;
				}

				// Rememeber the largest uncompressed file size
				if (sInfo.uncompressed_size > ulMaxSize)
					ulMaxSize = sInfo.uncompressed_size;
			}

			// Did we fail to find a matching extension?
			if (nRet == UNZ_END_OF_LIST_OF_FILE)
			{
				// Loop back over the archive
				for (nRet = unzGoToFirstFile(hfZip); nRet == UNZ_OK; nRet = unzGoToNextFile(hfZip))
				{
					// Get details of the current file
					unzGetCurrentFileInfo(hfZip, &sInfo, nullptr, 0, nullptr, 0, nullptr, 0);

					// Open the largest file found about
					if (sInfo.uncompressed_size == ulMaxSize && unzOpenCurrentFile(hfZip) == UNZ_OK)
					{
						nRet = unzReadCurrentFile(hfZip, mem, static_cast<unsigned int>(mem.size));
						unzCloseCurrentFile(hfZip);
						break;
					}
				}
			}

			// Close the zip archive
			unzClose(hfZip);

			// Stop if something went wrong
			if (nRet < 0)
				throw util::exception("zip extraction failed (", nRet, ")");

			uRead = nRet;
			m_compress = Compress::Zip;
		}
		else
		{
			// Open as gzipped or uncompressed
			gzFile gf = gzopen(path_.c_str(), "rb");
			if (gf == Z_NULL)
				throw posix_error(errno, path_.c_str());

			uRead = gzread(gf, mem, static_cast<unsigned>(mem.size));
			m_compress = (gztell(gf) != FileSize(path_)) ? Compress::Gzip : Compress::None;
			gzclose(gf);
		}
	}
	else
#endif // HAVE_ZLIB
	{
		// Open as normal file if we couldn't use zlib above
		FILE *f = fopen(path_.c_str(), "rb");
		if (!f)
			throw posix_error(errno, path_.c_str());

		uRead = fread(mem, 1, mem.size, f);
		fclose(f);
		m_compress = Compress::None;
	}


	// zip compressed? (and not handled above)
	if (uncompress && mem[0U] == 'P' && mem[1U] == 'K')
		throw util::exception("zlib (zlib1.dll) is required for zip support");

	// gzip compressed?
	if (uncompress && mem[0U] == 0x1f && mem[1U] == 0x8b && mem[2U] == 0x08)
	{
		if (!have_zlib)
			throw util::exception("zlib (zlib1.dll) is required for gzip support");

#ifdef HAVE_ZLIB
		MEMORY mem2(MAX_IMAGE_SIZE + 1);
		uint8_t flags = mem[3];
		auto pb = mem.pb + 10, pbEnd = mem.pb + mem.size;

		if (flags & 0x04)	// EXTRA_FIELD
		{
			if (pb < pbEnd - 1)
				pb += 2 + pb[0] + (pb[1] << 8);
		}

		if (flags & 0x08 || flags & 0x10)	// ORIG_NAME or COMMENT
		{
			while (pb < pbEnd && *pb++);
		}

		if (flags & 0x02)	// HEAD_CRC
			pb += 2;

		z_stream stream = {};
		stream.next_in = pb;
		stream.avail_in = (pb < pbEnd) ? static_cast<uInt>(pbEnd - pb) : 0;
		stream.next_out = mem2.pb;
		stream.avail_out = mem2.size;

		auto zerr = inflateInit2(&stream, -MAX_WBITS);
		if (zerr == Z_OK)
		{
			zerr = inflate(&stream, Z_FINISH);
			inflateEnd(&stream);
		}

		if (zerr != Z_STREAM_END)
			throw util::exception("gzip decompression failed (", zerr, ")");

		memcpy(mem.pb, mem2.pb, uRead = stream.total_out);
		m_compress = Compress::Zip;
#endif
	}

	// bzip2 compressed?
	if (uncompress && mem[0U] == 'B' && mem[1U] == 'Z')
	{
#ifdef HAVE_BZIP2
		if (!CheckLibrary("bzip2", "BZ2_bzBuffToBuffDecompress"))
#endif
			throw util::exception("libbz2 (bzip2.dll) is required for bzip2 support");

#ifdef HAVE_BZIP2
		MEMORY mem2(MAX_IMAGE_SIZE + 1);

		auto uBzRead = static_cast<unsigned>(mem2.size);
		auto bzerr = BZ2_bzBuffToBuffDecompress(reinterpret_cast<char *>(mem2.pb), &uBzRead, reinterpret_cast<char *>(mem.pb), uRead, 0, 0);
		if (bzerr != BZ_OK)
			throw util::exception("bzip2 decompression failed (", bzerr, ")");

		memcpy(mem.pb, mem2.pb, uRead = uBzRead);
		m_compress = Compress::Bzip2;
#endif // HAVE_BZIP2
	}

	if (uRead <= MAX_IMAGE_SIZE)
		return open(mem.pb, static_cast<int>(uRead), path_);

	throw util::exception("file size too big");
}
void Pthread_cond_broadcast(pthread_cond_t *cond) {
	int rc;

	if ((rc = pthread_cond_broadcast(cond)) != 0)
		posix_error(rc, "Pthread_cond_broadcast error");
}
Esempio n. 15
0
static PyObject *
subterfugue_mainloop(PyObject *self, PyObject *args)
{
  int pid, wpid, status, scno, eax, waitchannelhack;
  int beforecall = -1;

  if (!PyArg_Parse(args, "(ii)", &pid, &waitchannelhack))
    return NULL;

  numtraced++;

  while (1) {
    int result;

#ifndef __WALL
#define __WALL 0x40000000
#endif
    wpid = wait4(-1, &status, WUNTRACED | __WALL, NULL);
    if (wpid == -1)
      return posix_error();

    DBG("Checking pids: %d, %d\n", pid, wpid);
    if (pid != wpid)		/* XXX: could maybe be handled w/o giving up */
      goto giveup;
    DBG("This looks good\n");
    if (!WIFSTOPPED(status)) 
      goto giveup;
    DBG("checking SIGTRAP\n");
    if (!waitchannelhack) {
      if (WSTOPSIG(status) != (SIGTRAP | 0x80))
	goto giveup;
    } else {
      int r = atcallstop(pid, WSTOPSIG(status));
      if (r == -1)
	return posix_error();	/* shouldn't happen, but tell the caller */
      if (!r)
	goto giveup;
    }
      
    /* Only check the system call number on the before stop, because some
       calls (e.g., sigreturn) stomp that number.  On the after stop, by the
       time we get here it "must" be okay to ignore this stop.  (Skipping
       these checks also speeds things up a bit.)
    */
    if (beforecall) {
      DBG("getting scno\n");
      scno = ptrace(PTRACE_PEEKUSER, wpid, 4*ORIG_EAX, PTRACE_ARGTYPE &scno);
      DBG("scno is %d (%m)\n", scno);
      if (scno < 0 || scno >= MAXCALLNUMBER)
	goto giveup;

      DBG("looking if call_ignored\n");
      if (!call_ignored[scno])
	goto giveup;

      eax = ptrace(PTRACE_PEEKUSER, wpid, 4*EAX, 0);
      if ((eax != -ENOSYS) /* && beforecall */)
	goto giveup;
    }

    DBG("ok, this call is ignored here\n" );
    beforecall = !beforecall;

    result = ptrace(PTRACE_SYSCALL, pid, 0, 0);
    if (result == -1)
      return posix_error();	/* shouldn't happen, but tell the caller */
      
    numignored++;
    if (!(numignored % 1000))
      DBG("numignored = %d, numtraced = %d\n", numignored, numtraced);
  }

giveup:  
  /* if beforecall is nonzero, change 'insyscall' status of _pid_ to that returned */

  //  fprintf( stderr, "Returning %d, %d, %d\n", wpid, status, beforecall );
  return Py_BuildValue("(iii)", wpid, status, beforecall);
}
Esempio n. 16
0
void Pthread_create(pthread_t *tidp, pthread_attr_t *attrp, void * (*routine)(void *), void *argp) {
	int rc;

	if ((rc = pthread_create(tidp, attrp, routine, argp)) != 0)
		posix_error(rc, "Pthread_create error");
}
Esempio n. 17
0
void Pthread_barrier_destroy(pthread_barrier_t *barrier) {
	int rc;

	if ((rc = pthread_barrier_destroy(barrier)) != 0)
		posix_error(rc, "Pthread_barrier_destroy");
}
void Pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr) {
	int rc;

	if ((rc = pthread_cond_init(cond, attr)) != 0)
		posix_error(rc, "Pthread_cond_init error");
}
Esempio n. 19
0
void Pthread_join(pthread_t tid, void **thread_return)
{
	int rt = pthread_join(tid, thread_return);
	if( rt != 0 )
		posix_error(rt, "pthread_join error");
}
void Pthread_cond_signal(pthread_cond_t *cond) {
	int rc;

	if ((rc = pthread_cond_signal(cond)) != 0)
		posix_error(rc, "Pthread_cond_signal error");
}
Esempio n. 21
0
void Pthread_mutex_destroy(pthread_mutex_t *mutex) {
	int rc;

	if ((rc = pthread_mutex_destroy(mutex)) != 0)
		posix_error(rc, "Pthread_mutex_destroy");
}
void Pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
	int rc;

	if ((rc = pthread_cond_wait(cond, mutex)) != 0)
		posix_error(rc, "Pthread_cond_wait error");
}
Esempio n. 23
0
void Pthread_cancel(pthread_t tid)
{
	int rt = pthread_cancel(tid);
	if(rt != 0)
		posix_error(rt, "Pthread_cancel error");
}