Ejemplo n.º 1
0
/* Loads the lump into the given buffer, which must be >= RTS_SoundLength() */
static void RTS_ReadLump(int32_t lump, void *dest)
{
    lumpinfo_t *l = &rts_lumpinfo[lump];

    klseek(l->handle, l->position, SEEK_SET);
    kread(l->handle, dest, l->size);
}
Ejemplo n.º 2
0
/*
= RTS_AddFile
=
= All files are optional, but at least one file must be found
= Files with a .rts extension are wadlink files with multiple lumps
= Other files are single lumps with the base filename for the lump name
*/
static int32_t RTS_AddFile(const char *filename)
{
    wadinfo_t  header;
    int32_t i, handle, length, startlump;
    filelump_t *fileinfo, *fileinfoo;

    // read the entire file in
    //      FIXME: shared opens

    handle = kopen4loadfrommod(filename, 0);
    if (handle < 0)
    {
        initprintf("RTS file \"%s\" was not found\n",filename);
        return -1;
    }

    startlump = rts_numlumps;

    // WAD file
    i = kread(handle, &header, sizeof(header));
    if (i != sizeof(header) || Bmemcmp(header.identification, "IWAD", 4))
    {
        initprintf("RTS file \"%s\" too short or doesn't have IWAD id\n", filename);
        kclose(handle);
        return -1;
    }

    header.numlumps = B_LITTLE32(header.numlumps);
    header.infotableofs = B_LITTLE32(header.infotableofs);

    length = header.numlumps*sizeof(filelump_t);
    fileinfo = fileinfoo = (filelump_t *)Xmalloc(length);

    klseek(handle, header.infotableofs, SEEK_SET);
    kread(handle, fileinfo, length);

    {
        lumpinfo_t *lump_p = (lumpinfo_t *)Xrealloc(
            rts_lumpinfo, (rts_numlumps + header.numlumps)*sizeof(lumpinfo_t));

        rts_lumpinfo = lump_p;
    }

    rts_numlumps += header.numlumps;

    for (i=startlump; i<rts_numlumps; i++, fileinfo++)
    {
        lumpinfo_t *lump = &rts_lumpinfo[i];

        lump->handle = handle;  // NOTE: cache1d-file is not closed!
        lump->position = B_LITTLE32(fileinfo->filepos);
        lump->size = B_LITTLE32(fileinfo->size);

        Bstrncpy(lump->name, fileinfo->name, 8);
    }

    Bfree(fileinfoo);

    return 0;
}
Ejemplo n.º 3
0
Archivo: kernel.c Proyecto: 274914765/C
/** Reads kernel memory.
 *  Seeks to the specified location in kmem, then
 *  does a read of given amount ob bytes into target buffer.
 *
 * @param off The location to seek.
 *
 * @param target The target buffer to read into.
 *
 * @param siz Number of bytes to read.
 *
 * @return gives 1 on success and 0 on failure.
 */
int klookup (unsigned long off, void *target, size_t siz)
{
    long retsiz;

    if (kmem < 0)
        return 0;

    if ((retsiz = klseek ((off_t) off)) != off)
    {
        snmp_log (LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
        snmp_log_perror ("klseek");
        return (0);
    }
    if ((retsiz = klread (target, siz)) != siz)
    {
        if (snmp_get_do_debugging ())
        {
            /*
             * these happen too often on too many architectures to print them
             * unless we're in debugging mode. People get very full log files. 
             */
            snmp_log (LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
            snmp_log_perror ("klread");
        }
        return (0);
    }
    DEBUGMSGTL (("verbose:kernel:klookup", "klookup(%lx, %p, %d) succeeded", off, target, siz));
    return (1);
}
Ejemplo n.º 4
0
int
klookup(unsigned long off,
	char   *target,
	int     siz)
{
  long retsiz;

  if (kmem < 0) return 0;

  if ((retsiz = klseek((off_t) off)) != off) {
    snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
    snmp_log_perror("klseek");
#ifdef EXIT_ON_BAD_KLREAD
    exit(1);
#endif
    return (0);
  }
  if ((retsiz = klread(target, siz)) != siz ) {
    if (snmp_get_do_debugging()) {
    /* these happen too often on too many architectures to print them
       unless we're in debugging mode. People get very full log files. */
      snmp_log(LOG_ERR, "klookup(%lx, %p, %d): ", off, target, siz);
      snmp_log_perror("klread");
    }
#ifdef EXIT_ON_BAD_KLREAD
    exit(1);
#endif
    return(0);
  }
  return (1);
}
Ejemplo n.º 5
0
uint64_t sys_lseek_file(int fd, uint64_t offset, int whence){

	int x =  klseek(fd,offset,whence);

	//printf("cursor : %d" , file_table[4].cursor);

	return x;

}
Ejemplo n.º 6
0
/*
====================
=
= RTS_ReadLump
=
= Loads the lump into the given buffer, which must be >= RTS_SoundLength()
=
====================
*/
void RTS_ReadLump (int32 lump, void *dest)
   {
   lumpinfo_t *l;

   if (lump >= numlumps)
      Error ("RTS_ReadLump: %i >= numlumps",lump);
   if (lump < 0)
      Error ("RTS_ReadLump: %i < 0",lump);
   l = lumpinfo+lump;
   klseek (l->handle, l->position, SEEK_SET);
   kread(l->handle,dest,l->size);
   }
Ejemplo n.º 7
0
int main () {
    
    // INIT FS HERE
    init_fs();
    
  int retval, i;
  int fd;
  int index_node_number;

  /* Some arbitrary data for our files */
  memset (data1, '1', sizeof (data1));
  memset (data2, '2', sizeof (data2));
  memset (data3, '3', sizeof (data3));


#ifdef TEST1
	kmkdir("/home");
	kcreat("/home/file");
  /* ****TEST 1: MAXIMUM file creation**** */

  /* Assumes the pre-existence of a root directory file "/"
     that is neither created nor deleted in this test sequence */

  /* Generate MAXIMUM regular files */
  for (i = 0; i < MAX_FILES + 1; i++) { // go beyond the limit
    sprintf (pathname, "/file%d", i);
    
    retval = kcreat (pathname);
    
    if (retval < 0) {
      fprintf (stderr, "kcreate: File creation error! status: %d\n", 
	       retval);
      
      if (i != MAX_FILES)
	exit (1);
    }
    
    memset (pathname, 0, 80);
  }   

  /* Delete all the files created */
  for (i = 0; i < MAX_FILES; i++) { 
    sprintf (pathname, "/file%d", i);
    
    retval = kunlink (pathname);
    
    if (retval < 0) {
      fprintf (stderr, "kunlink: File deletion error! status: %d\n", 
	       retval);
      
      exit (1);
    }
    
    memset (pathname, 0, 80);
  }

#endif // TEST1
  
#ifdef TEST2

  /* ****TEST 2: LARGEST file size**** */

  
  /* Generate one LARGEST file */
  retval = kcreat ("/bigfile");

  if (retval < 0) {
    fprintf (stderr, "kcreat: File creation error! status: %d\n", 
	     retval);

    exit (1);
  }

    printf("kcreat succesful\n");
    
  retval =  kopen ("/bigfile"); /* Open file to write to it */
  
  if (retval < 0) {
    fprintf (stderr, "kopen: File open error! status: %d\n", 
	     retval);

    exit (1);
  }

    printf("kopen succesful\n");
  fd = retval;			/* Assign valid fd */

  /* Try writing to all direct data blocks */
  retval = kwrite (fd, data1, sizeof(data1));
  
  if (retval < 0) {
    fprintf (stderr, "kwrite: File write STAGE1 error! status: %d\n", 
	     retval);

    exit (1);
  }

    printf("kwrite direct succesful\n");
#ifdef TEST_SINGLE_INDIRECT
  
  /* Try writing to all single-indirect data blocks */
  retval = kwrite (fd, data2, sizeof(data2));
    
  if (retval < 0) {
    fprintf (stderr, "kwrite: File write STAGE2 error! status: %d\n", 
	     retval);

    exit (1);
  }

    printf("kwrite 1 indirect succesful\n");
#ifdef TEST_DOUBLE_INDIRECT

  /* Try writing to all double-indirect data blocks */
  retval = kwrite (fd, data3, sizeof(data3));
  printf("kwrite 2 actually writes = %d, suppose to write = %ld\n", retval, sizeof(data3));
  if (retval < 0) {
    fprintf (stderr, "kwrite: File write STAGE3 error! status: %d\n", 
	     retval);

    exit (1);
  }
    
    printf("PASSED TEST2\n");

#endif // TEST_DOUBLE_INDIRECT

#endif // TEST_SINGLE_INDIRECT

#endif // TEST2

#ifdef TEST3

  /* ****TEST 3: Seek and Read file test**** */
  retval = klseek (fd, 0);	/* Go back to the beginning of your file */
  if (retval < 0) {
    fprintf (stderr, "klseek: File seek error! status: %d\n", 
	     retval);

    exit (1);
  }

  /* Try reading from all direct data blocks */
  retval = kread (fd, addr, sizeof(data1));
  
  if (retval < 0) {
    fprintf (stderr, "kread: File read STAGE1 error! status: %d\n", 
	     retval);

    exit (1);
  }
  /* Should be all 1s here... */
  printf ("Data at addr: %s\n", addr);
    
    printf("PASSED TEST3-Direct Block read\n");
    
#ifdef TEST_SINGLE_INDIRECT

  /* Try reading from all single-indirect data blocks */
  retval = kread (fd, addr, sizeof(data2));
  
  if (retval < 0) {
    fprintf (stderr, "kread: File read STAGE2 error! status: %d\n", 
	     retval);

    exit (1);
  }
  /* Should be all 2s here... */
  printf ("Data at addr: %s\n", addr);
    
    printf("PASSED TEST3-1 Redirect Block read\n");
#ifdef TEST_DOUBLE_INDIRECT

  /* Try reading from all double-indirect data blocks */
  retval = kread (fd, addr, sizeof(data3));
  
  if (retval < 0) {
    fprintf (stderr, "kread: File read STAGE3 error! status: %d\n", 
	     retval);

    exit (1);
  }
  /* Should be all 3s here... */
  printf ("Data at addr: %s\n", addr);
printf("PASSED TEST3-2 Redirect Block read\n");
#endif // TEST_DOUBLE_INDIRECT

#endif // TEST_SINGLE_INDIRECT

  /* Close the bigfile */
  retval = kclose (fd);
  
  if (retval < 0) {
    fprintf (stderr, "kclose: File close error! status: %d\n", 
	     retval);

    exit (1);
  }

  /* Remove the biggest file */

  retval = kunlink ("/bigfile");
	
  if (retval < 0) {
    fprintf (stderr, "kunlink: /bigfile file deletion error! status: %d\n", 
	     retval);
    
    exit (1);
  }

#endif // TEST3

#ifdef TEST4
  
  /* ****TEST 4: Make directory and read directory entries**** */
  retval = kmkdir ("/dir1");
    
  if (retval < 0) {
    fprintf (stderr, "kmkdir: Directory 1 creation error! status: %d\n", 
	     retval);

    exit (1);
  }

  retval = kmkdir ("/dir1/dir2");
    
  if (retval < 0) {
    fprintf (stderr, "kmkdir: Directory 2 creation error! status: %d\n", 
	     retval);

    exit (1);
  }

  retval = kmkdir ("/dir1/dir3");
    
  if (retval < 0) {
    fprintf (stderr, "kmkdir: Directory 3 creation error! status: %d\n", 
	     retval);

    exit (1);
  }

  retval =  kopen ("/dir1"); /* Open directory file to read its entries */
  
  if (retval < 0) {
    fprintf (stderr, "kopen: Directory open error! status: %d\n", 
	     retval);

    exit (1);
  }

  fd = retval;			/* Assign valid fd */

  memset (addr, 0, sizeof(addr)); /* Clear scratchpad memory */

  while ((retval = kreaddir (fd, addr))) { /* 0 indicates end-of-file */

    if (retval < 0) {
      fprintf (stderr, "kreaddir: Directory read error! status: %d\n", 
	       retval);
      
      exit (1);
    }
      /*
      printf("user\n");
      int zz = 0;
      for (zz; zz < 16; zz++) {
          printf("%d ", *((unsigned char *)addr + zz));
      }
      printf("\n");
       */
    index_node_number = atoi(&addr[14]);
    printf ("Contents at addr: [%s,%d]\n", addr, index_node_number);
  }

#endif // TEST4

#ifdef TEST5

  /* ****TEST 5: 2 process test**** */
  
  if((retval = fork())) {

    if(retval == -1) {
      fprintf(stderr, "Failed to fork\n");
      
      exit(1);
    }

    /* Generate 300 regular files */
    for (i = 0; i < 300; i++) { 
      sprintf (pathname, "/file_p_%d", i);
      
      retval = kcreat (pathname);
      
      if (retval < 0) {
	fprintf (stderr, "(Parent) kcreate: File creation error! status: %d\n", 
		 retval);

	exit(1);
      }
    
      memset (pathname, 0, 80);
    }
    
  }
  else {
    /* Generate 300 regular files */
    for (i = 0; i < 300; i++) { 
      sprintf (pathname, "/file_c_%d", i);
      
      retval = kcreat (pathname);
      
      if (retval < 0) {
	fprintf (stderr, "(Child) kcreate: File creation error! status: %d\n", 
		 retval);

	exit(1);
      }
    
      memset (pathname, 0, 80);
    }
  }

#endif // TEST5


  fprintf(stdout, "Congratulations,  you have passed all tests!!\n");

  return 0;
}
Ejemplo n.º 8
0
int
main(int argc, char **argv)
{
	int ch, i, jflag, npcbs;
	const char *core, *syst;

	nl[0].n_name = strdup("_tcp_debug");
	nl[1].n_name = strdup("_tcp_debx");

	jflag = npcbs = 0;
	while ((ch = getopt(argc, argv, "afjp:st")) != -1)
		switch (ch) {
		case 'a':
			++aflag;
			break;
		case 'f':
			++follow;
			setlinebuf(stdout);
			break;
		case 'j':
			++jflag;
			break;
		case 'p':
			if (npcbs >= TCP_NDEBUG)
				errx(1, "too many pcb's specified");
			(void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
			break;
		case 's':
			++sflag;
			break;
		case 't':
			++tflag;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	core = _PATH_KMEM;
	if (argc > 0) {
		syst = *argv;
		argc--, argv++;
		if (argc > 0) {
			core = *argv;
			argc--, argv++;
			++kflag;
		}
		/*
		 * Discard setgid privileges if not the running kernel so that
		 * bad guys can't print interesting stuff from kernel memory.
		 */
		if (setgid(getgid()) != 0)
			err(1, "setgid");
	}
	else
		syst = getbootfile();

	if (nlist(syst, nl) < 0 || !nl[0].n_value)
		errx(1, "%s: no namelist", syst);
	if ((memf = open(core, O_RDONLY)) < 0)
		err(2, "%s", core);
	if (setgid(getgid()) != 0)
		err(1, "setgid");
	if (kflag)
		errx(1, "can't do core files yet");
	(void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
	if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
	    sizeof(tcp_debx))
		err(3, "tcp_debx");
	(void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
	if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
	    sizeof(tcp_debug))
		err(3, "tcp_debug");
	/*
	 * If no control blocks have been specified, figure
	 * out how many distinct one we have and summarize
	 * them in tcp_pcbs for sorting the trace records
	 * below.
	 */
	if (!npcbs) {
		for (i = 0; i < TCP_NDEBUG; i++) {
			register struct tcp_debug *td = &tcp_debug[i];
			register int j;

			if (td->td_tcb == 0)
				continue;
			for (j = 0; j < npcbs; j++)
				if (tcp_pcbs[j] == td->td_tcb)
					break;
			if (j >= npcbs)
				tcp_pcbs[npcbs++] = td->td_tcb;
		}
		if (!npcbs)
			exit(0);
	}
	qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
	if (jflag) {
		for (i = 0;;) {
			printf("%p", (void *)tcp_pcbs[i]);
			if (++i == npcbs)
				break;
			fputs(", ", stdout);
		}
		putchar('\n');
	}
	else for (i = 0; i < npcbs; i++) {
		printf("\n%p:\n", tcp_pcbs[i]);
		dotrace(tcp_pcbs[i]);
	}
	exit(0);
}
Ejemplo n.º 9
0
int32 RTS_AddFile (char *filename)
   {
   wadinfo_t  header;
   lumpinfo_t *lump_p;
   uint32     i;
   int32      handle, length;
   int32      startlump;
   filelump_t *fileinfo, *fileinfoo;

//
// read the entire file in
//      FIXME: shared opens

   handle = kopen4load(filename, 0);
   if (handle < 0) {
	initprintf("RTS file %s was not found\n",filename);
	return -1;
   }

   startlump = numlumps;

   // WAD file
   initprintf("    Adding %s.\n",filename);
   kread( handle, &header, sizeof( header ) );
   if (strncmp(header.identification,"IWAD",4)) {
	initprintf("RTS file %s doesn't have IWAD id\n",filename);
	kclose(handle);
	return -1;
   }
   header.numlumps = IntelLong(header.numlumps);
   header.infotableofs = IntelLong(header.infotableofs);
   length = header.numlumps*sizeof(filelump_t);
   fileinfo = fileinfoo = (filelump_t*)malloc (length);
   if (!fileinfo) {
	initprintf("RTS file could not allocate header info\n");
	kclose(handle);
	return -1;
   }
   klseek (handle, header.infotableofs, SEEK_SET);
   kread(handle, fileinfo, length);

//
// Fill in lumpinfo
//
   lump_p = realloc(lumpinfo, (numlumps + header.numlumps)*sizeof(lumpinfo_t));
   if (!lump_p) {
	kclose(handle);
	return -1;
   }
   lumpinfo = lump_p;

   numlumps += header.numlumps;

   lump_p = &lumpinfo[startlump];

   for (i=startlump ; i<(uint32)numlumps ; i++,lump_p++, fileinfo++)
      {
      lump_p->handle = handle;
      lump_p->position = IntelLong(fileinfo->filepos);
      lump_p->size = IntelLong(fileinfo->size);
      strncpy (lump_p->name, fileinfo->name, 8);
      }

   free(fileinfoo);

   return 0;
   }
Ejemplo n.º 10
0
void
dotrace(caddr_t tcpcb)
{
	struct tcp_debug *td;
	int i;
	int prev_debx, family;

	prev_debx = tcp_debx;
again:	if (--tcp_debx < 0)
		tcp_debx = TCP_NDEBUG - 1;
	for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
		td = &tcp_debug[i];
		if (tcpcb && td->td_tcb != tcpcb)
			continue;
		ntime = ntohl(td->td_time);
#ifdef INET6
		family = td->td_family;
#else
		family = AF_INET;
#endif
		switch(family) {
		case AF_INET:
			tcp_trace(td->td_act, td->td_ostate,
				  (struct tcpcb *)td->td_tcb,
				  &td->td_cb, td->td_family, &td->td_ti.ti_i,
				  &td->td_ti.ti_t, td->td_req);
			break;
#ifdef INET6
		case AF_INET6:
			tcp_trace(td->td_act, td->td_ostate,
				  (struct tcpcb *)td->td_tcb,
				  &td->td_cb, td->td_family, &td->td_ti6.ip6,
				  &td->td_ti6.th, td->td_req);
			break;
#endif
		}
		if (i == tcp_debx)
			goto done;
	}
	for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
		td = &tcp_debug[i];
		if (tcpcb && td->td_tcb != tcpcb)
			continue;
		ntime = ntohl(td->td_time);
#ifdef INET6
		family = td->td_family;
#else
		family = AF_INET;
#endif
		switch(family) {
		case AF_INET:
			tcp_trace(td->td_act, td->td_ostate,
				  (struct tcpcb *)td->td_tcb,
				  &td->td_cb, td->td_family, &td->td_ti.ti_i,
				  &td->td_ti.ti_t, td->td_req);
			break;
#ifdef INET6
		case AF_INET6:
			tcp_trace(td->td_act, td->td_ostate,
				  (struct tcpcb *)td->td_tcb,
				  &td->td_cb, td->td_family, &td->td_ti6.ip6,
				  &td->td_ti6.th, td->td_req);
			break;
#endif
		}
	}
done:	if (follow) {
		prev_debx = tcp_debx + 1;
		if (prev_debx >= TCP_NDEBUG)
			prev_debx = 0;
		do {
			sleep(1);
			klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
			if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
			    sizeof(tcp_debx))
				err(3, "tcp_debx");
		} while (tcp_debx == prev_debx);
		klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
		if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
		    sizeof(tcp_debug))
			err(3, "tcp_debug");
		goto again;
	}
}
Ejemplo n.º 11
0
static int F_CALLBACKAPI f_seek(unsigned int handle, int pos, signed char mode)
{
	return klseek(handle - 1, pos, mode);
}
Ejemplo n.º 12
0
struct loaderplugin_t* pluginloader_loadPluginFromFile(struct pluginloader_t* loader, char* pluginPath)
{
	if (!loader || !pluginPath)
		return NULL;

	int fd = kopen(pluginPath, O_RDONLY, 0);
	if (fd < 0)
	{
		WriteLog(LL_Error, "could not load plugin (%s).", pluginPath);
		return NULL;
	}

	struct stat statbuf;
	kmemset(&statbuf, 0, sizeof(statbuf));

	// Get the file size
	int res = kfstat(fd, &statbuf);
	if (res < 0)
	{
		WriteLog(LL_Error, "could not get %d handle stat: %d", fd, res);
		kclose(fd);
		return NULL;
	}

	// Verify that our plugin is under the size limit
	size_t pluginSize = statbuf.st_size;
	if (pluginSize > PLUGIN_MAXSIZE)
	{
		WriteLog(LL_Warn, "plugin (%s) is too large, skipping.", pluginPath);
		kclose(fd);
		return NULL;
	}

	uint8_t* pluginData = (uint8_t*)kmalloc(pluginSize);
	if (!pluginData)
	{
		WriteLog(LL_Error, "could not allocate space for plugin (%s) (size: %lld)", pluginPath, pluginSize);
		kclose(fd);
		return NULL;
	}
	kmemset(pluginData, 0, pluginSize);

	// Read out our plugin data
	klseek(fd, 0, SEEK_SET);
	kread(fd, pluginData, pluginSize);
	kclose(fd);

	// Allocate a new entry for our list
	struct loaderplugin_t* entry = (struct loaderplugin_t*)kmalloc(sizeof(struct loaderplugin_t));
	if (!entry)
	{
		WriteLog(LL_Error, "could not allocate new entry");
		kfree(pluginData, pluginSize);
		return NULL;
	}

	entry->data = pluginData;
	entry->dataLength = pluginSize;
	entry->plugin = NULL; // do not create the plugin here
	
	size_t pathLength = strlen(pluginPath);
	if (pathLength > sizeof(entry->path))
		WriteLog(LL_Warn, "path is too long, not setting");
	else
		kmemcpy(entry->path, pluginPath, pathLength);

	return entry;
}