Example #1
0
void
fs_log_pkg(fs_log_t *log, btp_pkg_t *pkg, int direct)
{
	unsigned int cnt;
	btp_arg_t *arg;
	
	if (!btp_pkg_check(pkg))
		return;
	
	fs_debug(log, " ");
	
	if (direct == FS_IN)
		fs_debug(log, "<# ---------------- INCOMING PACKAGE -------------- # ");
	else if (direct == FS_OUT)
		fs_debug(log, "<# ---------------- OUTGOING PACKAGE -------------- # ");
	else
		fs_debug(log, "<# ---------------- ---------------- -------------- #");
	
	fs_debug(log, "| opcode = %d | error = %d | argno = %d | size = %lu",
		btp_pkg_get_opcode(pkg),
		btp_pkg_get_error(pkg),
		btp_pkg_argno(pkg),
		btp_pkg_size(pkg)
		);
	
	if (btp_pkg_argno(pkg) > 0) {
		fs_debug(log, "  ------------------------------------------------  ");
		BTP_FOREACH(cnt, arg, pkg)
			fs_log_arg(log, arg);
	}
	
	fs_debug(log, "# ------------------------------------------------ #> ");
	fs_debug(log, " ");
}
Example #2
0
void
fs_log_arg(fs_log_t *log, btp_arg_t *arg)
{
	uint32_t size;
	int64_t number;
	char *str;
	unsigned char *binary;
	bool boolean;
	
	if (!btp_arg_check(arg))
		return;
	
	switch (btp_arg_type(arg)) {
		case BTP_TYPE_NUMBER:
			btp_arg_get_number(arg, &number),
			fs_debug(log, "| <num(%u)> | %s = %lu", sizeof(number), btp_arg_name(arg), number);
			break;
			
		case BTP_TYPE_STRING:
			btp_arg_get_string(arg, &str),
			fs_debug(log, "| <str(%u)> | %s = %s", strlen(str), btp_arg_name(arg), str);
			break;
			
		case BTP_TYPE_BINARY:
			btp_arg_get_binary(arg, &size, &binary),
			fs_debug(log, "| <bin(%u)> | %s = <binary>", size, btp_arg_name(arg));
			break;
			
		case BTP_TYPE_BOOL:
			btp_arg_get_bool(arg, &boolean),
			fs_debug(log, "| <num(%u)> | %s = %s", sizeof(bool), btp_arg_name(arg), boolean ? "true" : "false");
			break;
		
		default:
			fs_debug(log, "| <xxx(%u)> | %s = <unknoe data type>", btp_arg_vsize(arg) - BTP_VALUE_HEAD_SIZE, btp_arg_name(arg));
			break;
	}
}
Example #3
0
/* Cleanup after fscking with FSCK.  If REMOUNT is true, ask the filesystem
   to remount itself (to incorporate changes made by the fsck program).  If
   MAKE_WRITABLE is true, then if the filesystem should be made writable, do
   so (after remounting if applicable).  */
static void
fsck_cleanup (struct fsck *fsck, int remount, int make_writable)
{
  error_t err = 0;
  struct fs *fs = fsck->fs;

  /* Remove from chain.  */
  *fsck->self = fsck->next;
  if (fsck->next)
    fsck->next->self = fsck->self;

  fs_debug (fs, "Cleaning up after fsck (remount = %d, make_writable = %d)",
	    remount, make_writable);

  if (fs->mounted > 0)
    /* It's currently mounted; if the fsck modified the device, tell the
       running filesystem to remount it.  Also we may make it writable.  */
    {
      if (remount)
	{
	  fs_debug (fs, "Remounting");
	  err = fs_remount (fs);
	  if (err)
	    error (0, err, "%s: Cannot remount", fs->mntent.mnt_dir);
	}
      if (!err && make_writable && fsck->make_writable)
	{
	  fs_debug (fs, "Making writable");
	  err = fs_set_readonly (fs, 0);
	  if (err)
	    error (0, err, "%s: Cannot make writable", fs->mntent.mnt_dir);
	}
    }

   free (fsck);
}
Example #4
0
int main( int argc, char *argv[] )
{
	char line[1024];
	char cmd[1024];
	char arg1[1024];
	char arg2[1024];
	int result, args;

	if(argc!=3) {
		printf("use: %s <diskfile> <nblocks>\n",argv[0]);
		return 1;
	}

	if(!disk_init(argv[1],atoi(argv[2]))) {
		printf("couldn't initialize %s: %s\n",argv[1],strerror(errno));
		return 1;
	}

	printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size());

	while(1) {
		printf(" prompt> ");
		fflush(stdout);

		if(!fgets(line,sizeof(line),stdin)) break;

		if(line[0]=='\n') continue;
		line[strlen(line)-1] = 0;

		args = sscanf(line,"%s %s %s",cmd,arg1,arg2);
		if(args==0) continue;

		if(!strcmp(cmd,"format")) {
			if(args==1) {
				if(!fs_format()) {
					printf("disk formatted.\n");
				} else {
					printf("format failed!\n");
				}
			} else {
				printf("use: format\n");
			}
		} else if(!strcmp(cmd,"mount")) {
			if(args==1) {
				if(!fs_mount()) {
					printf("disk mounted.\n");
				} else {
					printf("mount failed!\n");
				}
			} else {
				printf("use: mount\n");
			}
		} else if(!strcmp(cmd,"debug")) {
			if(args==1) {
				fs_debug();
			} else {
				printf("use: debug\n");
			}
		} else if(!strcmp(cmd,"getsize")) {
			if(args==2) {
				result = fs_getsize(arg1);
				if(result>=0) {
					printf("file %s has size %d\n",arg1,result);
				} else {
					printf("getsize failed!\n");
				}
			} else {
				printf("use: getsize <filename>\n");
			}
			
		} else if(!strcmp(cmd,"create")) {
			if(args==2) {
				result = fs_create(arg1);
				if(result==0) {
					printf("created file %s\n",arg1);
				} else {
					printf("create failed!\n");
				}
			} else {
				printf("use: create <filename>\n");
			}
		} else if(!strcmp(cmd,"delete")) {
			if(args==2) {
				if(!fs_delete(arg1)) {
					printf("file %s deleted.\n",arg1);
				} else {
					printf("delete failed!\n");	
				}
			} else {
				printf("use: delete <filename>\n");
			}
		} else if(!strcmp(cmd,"cat")) {
			if(args==2) {
				if(!do_copyout(arg1,"/dev/stdout")) {
					printf("cat failed!\n");
				}
			} else {
				printf("use: cat <name>\n");
			}

		} else if(!strcmp(cmd,"copyin")) {
			if(args==3) {
				if(do_copyin(arg1,arg2)) {
					printf("copied file %s to  %s\n",arg1,arg2);
				} else {
					printf("copy failed!\n");
				}
			} else {
				printf("use: copyin <filename in host system> <filename in fs-miei-02>\n");
			}

		} else if(!strcmp(cmd,"copyout")) {
			if(args==3) {
				if(do_copyout(arg1,arg2)) {
					printf("copied myfs_file %s to file %s\n", arg1,arg2);
				} else {
					printf("copy failed!\n");
				}
			} else {
				printf("use: copyout <inumber> <filename>\n");
			}

		} else if(!strcmp(cmd,"help")) {
			printf("Commands are:\n");
			printf("    format\n");
			printf("    mount\n");
			printf("    debug\n");
			printf("    create\n");
			printf("    delete  <filename>\n");
			printf("    cat     <filename>\n");
			printf("    getsize <filename>\n");
			printf("    copyin  <file name in host system> <miei02-filename>\n");
			printf("    copyout <miei02-filename> <file name in host system>\n");
			printf("	dump <number_of_block_with_text_contents>\n");
			printf("    help\n");
			printf("    quit\n");
			printf("    exit\n");
		} else if(!strcmp(cmd,"quit")) {
			break;
		} else if(!strcmp(cmd,"exit")) {
			break;
		} else if (!strcmp(cmd, "dump")){
			if(args==2) {
				int blNo = atoi(arg1);
				printf("Dumping disk block %d\n", blNo);
				char b[4096];
				disk_read( blNo, b);
				printf("------------------------------\n");
				printf("%s", b);
				printf("\n------------------------------\n");
			}
			else {
				printf("use: dump <block_number>\n");
			}
		} else {
			printf("unknown command: %s\n",cmd);
			printf("type 'help' for a list of commands.\n");
			result = 1;
		}
	}

	printf("closing emulated disk.\n");
	disk_close();

	return 0;
}
Example #5
0
int main( int argc, char *argv[] )
{
	char line[1024];
	char cmd[1024];
	char arg1[1024];
	char arg2[1024];
	int inumber, result, args;

	if(argc!=3) {
		printf("use: %s <diskfile> <nblocks>\n",argv[0]);
		return 1;
	}

	if(!disk_init(argv[1],atoi(argv[2]))) {
		printf("couldn't initialize %s: %s\n",argv[1],strerror(errno));
		return 1;
	}

	printf("opened emulated disk image %s with %d blocks\n",argv[1],disk_size());

	while(1) {
		printf(" prompt> ");
		fflush(stdout);

		if(!fgets(line,sizeof(line),stdin)) break;

		if(line[0]=='\n') continue;
		line[strlen(line)-1] = 0;

		args = sscanf(line,"%s %s %s",cmd,arg1,arg2);
		if(args==0) continue;

		if(!strcmp(cmd,"format")) {
			if(args==1) {
				if(fs_format()) {
					printf("disk formatted.\n");
				} else {
					printf("format failed!\n");
				}
			} else {
				printf("use: format\n");
			}
		} else if(!strcmp(cmd,"mount")) {
			if(args==1) {
				if(fs_mount()) {
					printf("disk mounted.\n");
				} else {
					printf("mount failed!\n");
				}
			} else {
				printf("use: mount\n");
			}
		} else if(!strcmp(cmd,"umount")) {
			if(args==1) {
				if(fs_umount()) {
					printf("disk umounted.\n");
				} else {
					printf("umount failed!\n");
				}
			} else {
				printf("use: mount\n");
			}
		} else if(!strcmp(cmd,"debug")) {
			if(args==1) {
				fs_debug();
			} else {
				printf("use: debug\n");
			}
		} else if(!strcmp(cmd,"getsize")) {
			if(args==2) {
				inumber = atoi(arg1);
				result = fs_getsize(inumber);
				if(result>=0) {
					printf("inode %d has size %d\n",inumber,result);
				} else {
					printf("getsize failed!\n");
				}
			} else {
				printf("use: getsize <inumber>\n");
			}
			
		} else if(!strcmp(cmd,"create")) {
			if(args==1) {
				inumber = fs_create();
				if(inumber>=0) {
					printf("created inode %d\n",inumber);
				} else {
					printf("create failed!\n");
				}
			} else {
				printf("use: create\n");
			}
		} else if(!strcmp(cmd,"delete")) {
			if(args==2) {
				inumber = atoi(arg1);
				if(fs_delete(inumber)) {
					printf("inode %d deleted.\n",inumber);
				} else {
					printf("delete failed!\n");	
				}
			} else {
				printf("use: delete <inumber>\n");
			}
		} else if(!strcmp(cmd,"cat")) {
			if(args==2) {
				inumber = atoi(arg1);
				if(!do_copyout(inumber,"/dev/stdout")) {
					printf("cat failed!\n");
				}
			} else {
				printf("use: cat <inumber>\n");
			}

		} else if(!strcmp(cmd,"copyin")) {
			if(args==3) {
				inumber = atoi(arg2);
				if(do_copyin(arg1,inumber)) {
					printf("copied file %s to inode %d\n",arg1,inumber);
				} else {
					printf("copy failed!\n");
				}
			} else {
				printf("use: copyin <filename> <inumber>\n");
			}

		} else if(!strcmp(cmd,"copyout")) {
			if(args==3) {
				inumber = atoi(arg1);
				if(do_copyout(inumber,arg2)) {
					printf("copied inode %d to file %s\n",inumber,arg2);
				} else {
					printf("copy failed!\n");
				}
			} else {
				printf("use: copyout <inumber> <filename>\n");
			}

		} else if(!strcmp(cmd,"help")) {
			printf("Commands are:\n");
			printf("    format\n");
			printf("    mount\n");
			printf("    umount\n");
			printf("    debug\n");
			printf("    create\n");
			printf("    delete  <inode>\n");
			printf("    getsize  <inode>\n");
			printf("    cat     <inode>\n");
			printf("    copyin  <file> <inode>\n");
			printf("    copyout <inode> <file>\n");
			printf("    help\n");
			printf("    quit\n");
			printf("    exit\n");
		} else if(!strcmp(cmd,"quit")) {
			break;
		} else if(!strcmp(cmd,"exit")) {
			break;
		} else {
			printf("unknown command: %s\n",cmd);
			printf("type 'help' for a list of commands.\n");
			result = 1;
		}
	}

	printf("closing emulated disk.\n");
	disk_close();

	return 0;
}
Example #6
0
/* Wait for some fsck process to exit, cleaning up after it, and return its
   exit-status.  */
static int
fscks_wait (struct fscks *fscks)
{
  pid_t pid;
  int wstatus, status;
  struct fsck *fsck, *next;

  /* Cleanup fscks that didn't even start.  */
  for (fsck = fscks->running; fsck; fsck = next)
    {
      next = fsck->next;
      if (fsck->pid == 0)
	{
	  fs_debug (fsck->fs, "Pruning failed fsck");
	  fsck_cleanup (fsck, 0, 1);
	}
    }

  debug ("Waiting...");

  do
    pid = wait (&wstatus);
  while (pid < 0 && errno == EINTR);

  if (pid > 0)
    {
      if (WIFEXITED (wstatus))
	status = WEXITSTATUS (wstatus);
      else if (WIFSIGNALED (wstatus))
	status = FSCK_EX_SIGNAL;
      else
	status = FSCK_EX_ERROR;

      for (fsck = fscks->running; fsck; fsck = fsck->next)
	if (fsck->pid == pid)
	  {
	    int remount = (status != 0);
	    int make_writable = (status == 0 || FSCK_EX_IS_FIXED (status));
	    fs_debug (fsck->fs, "Fsck finished (status = %d)", status);
	    fsck_cleanup (fsck, remount, make_writable);
	    fscks->free_slots++;
	    break;
	  }
      if (! fsck)
	error (0, 0, "%d: Unknown process exited", pid);
    }
  else if (errno == ECHILD)
    /* There are apparently no child processes left, and we weren't told of
       their demise.  This can't happen.  */
    {
      while (fscks->running)
	{
	  error (0, 0, "%s: Fsck process disappeared!",
		 fscks->running->fs->mntent.mnt_fsname);
	  /* Be pessimistic -- remount the filesystem, but leave it
	     readonly.  */
	  fsck_cleanup (fscks->running, 1, 0);
	  fscks->free_slots++;
	}
      status = FSCK_EX_ERROR;
    }
  else
    status = FSCK_EX_ERROR;	/* What happened?  */

  return status;
}
Example #7
0
/* Start a fsck process for FS running, and add an entry for it to FSCKS.
   This also ensures that if FS is currently mounted, it will be made
   readonly first.  If the fsck is successfully started, 0 is returned,
   otherwise FSCK_EX_ERROR.  */
static int
fscks_start_fsck (struct fscks *fscks, struct fs *fs)
{
  error_t err;
  int mounted, make_writable;
  struct fsck *fsck;

  if (got_sigint)
    /* We got SIGINT, so we pretend that all fscks got a signal without even
       attempting to run them.  */
    {
      fs_debug (fs, "Forcing signal");
      return FSCK_EX_SIGNAL;
    }

#define CK(err, fmt, args...) \
    do { if (err) { error (0, err, fmt , ##args); return FSCK_EX_ERROR; } } while (0)

  fs_debug (fs, "Checking mounted state");
  err = fs_mounted (fs, &mounted);
  CK (err, "%s: Cannot check mounted state", fs->mntent.mnt_dir);

  if (mounted)
    {
      int readonly;

      fs_debug (fs, "Checking readonly state");
      err = fs_readonly (fs, &readonly);
      CK (err, "%s: Cannot check readonly state", fs->mntent.mnt_dir);

      if (fscks->flags & FSCK_F_DRYRUN)
	{
	  if (! readonly)
	    {
	      printf ("%s: writable filesystem %s would be made read-only\n",
		      program_invocation_name, fs->mntent.mnt_dir);
	      readonly = 1;
	    }
	}

      if (! readonly)
	{
	  fs_debug (fs, "Making readonly");
	  err = fs_set_readonly (fs, 1);
	  CK (err, "%s: Cannot make readonly", fs->mntent.mnt_dir);
	}

      make_writable = !readonly
	|| ((fscks->flags & FSCK_F_WRITABLE) && hasmntopt (&fs->mntent, "rw"));
      if (make_writable)
	{
	  fs_debug (fs, "Will make writable after fscking if possible");
	  make_writable = 1;
	}
    }
  else
    make_writable = 0;

#undef CK

  /* Ok, any mounted filesystem is safely readonly.  */

  fsck = malloc (sizeof (struct fsck));
  if (! fsck)
    {
      error (0, ENOMEM, "malloc");
      return FSCK_EX_ERROR;
    }

  fsck->fs = fs;
  fsck->make_writable = make_writable;
  fsck->next = fscks->running;
  if (fsck->next)
    fsck->next->self = &fsck->next;
  fsck->self = &fscks->running;
  fsck->pid = fs_start_fsck (fs, fscks->flags);
  fscks->running = fsck;

  if (fsck->pid)
    fscks->free_slots--;

  return 0;
}
Example #8
0
/* Starts FS's fsck program on FS's device, returning the pid of the process.
   If an error is encountered, prints an error message and returns 0.
   Filesystems that need not be fscked at all also return 0 (but don't print
   an error message).  */
static pid_t
fs_start_fsck (struct fs *fs, int flags)
{
  pid_t pid;
  char flags_buf[10];
  char *argv[4], **argp = argv;
  struct fstype *type;
  error_t err = fs_type (fs, &type);

  assert_perror (err);		/* Should already have been checked for. */
  assert (type->program);

  *argp++ = type->program;

  if (flags & (FSCK_F_PREEN|FSCK_F_YES|FSCK_F_NO|FSCK_F_FORCE|FSCK_F_SILENT))
    {
      char *p = flags_buf;
      *argp++ = flags_buf;
      *p++ = '-';
      if (flags & FSCK_F_PREEN)  *p++ = 'p';
      if (flags & FSCK_F_YES)    *p++ = 'y';
      if (flags & FSCK_F_NO)     *p++ = 'n';
      if (flags & FSCK_F_FORCE)  *p++ = 'f';
      if (flags & FSCK_F_SILENT) *p++ = 's';
      *p = '\0';
    }

  *argp++ = fs->mntent.mnt_fsname;
  *argp = 0;

  if (flags & FSCK_F_DRYRUN)
    {
      char *argz;
      size_t argz_len;
      argz_create (argv, &argz, &argz_len);
      argz_stringify (argz, argz_len, ' ');
      puts (argz);
      free (argz);
      return 0;
    }

  pid = fork ();
  if (pid < 0)
    {
      error (0, errno, "fork");
      return 0;
    }

  if (pid == 0)
    /* Child.  */
    {
      execv (type->program, argv);
      exit (FSCK_EX_EXEC);	/* Exec failed. */
    }

  if ((flags & FSCK_F_VERBOSE) || _debug)
    {
      char *argz;
      size_t argz_len;
      argz_create (argv, &argz, &argz_len);
      argz_stringify (argz, argz_len, ' ');
      fs_debug (fs, "Spawned pid %d: %s", pid, argz);
      if (flags & FSCK_F_VERBOSE)
	puts (argz);
      free (argz);
    }

  return pid;
}