Пример #1
0
nfsstat4 dir_lookup(DB_TXN *txn, const struct nfs_inode *dir_ino,
		    const struct nfs_buf *str, int flags,
		    nfsino_t *inum_out)
{
	int rc;

	if (!dir_ino->mode)
		return NFS4ERR_ACCESS;
	if (dir_ino->type != NF4DIR) {
		if (dir_ino->type == NF4LNK)
			return NFS4ERR_SYMLINK;
		return NFS4ERR_NOTDIR;
	}
	if (!valid_utf8string(str))
		return NFS4ERR_INVAL;
	if (has_dots(str))
		return NFS4ERR_BADNAME;
	if (has_slash(str))
		return NFS4ERR_BADNAME;
	if (str->len > SRV_MAX_NAME)
		return NFS4ERR_NAMETOOLONG;

	rc = fsdb_dirent_get(&srv.fsdb, txn, dir_ino->inum, str,
			     flags, inum_out);

	if (rc == DB_NOTFOUND)
		return NFS4ERR_NOENT;
	if (rc)
		return NFS4ERR_IO;

	return NFS4_OK;
}
Пример #2
0
int run_program(struct tokens* tokens) {
  //Takes in a list of tokens that include the path and arguments for the program
  int token_length = tokens_get_length(tokens);
  
  if (token_length < 1)
  {
    printf("You must enter a valid command\n");
    return 0;
  }

  char* program = tokens_get_token(tokens, 0);
  //Grab arguments for program
  char* arguments[token_length + 1];
  int total_args = 0;
  for (int i = 0; i < token_length; ++i)
  {
    char* argument = tokens_get_token(tokens, i);

    //Assess redirection
    if (strcmp(argument, "<") == 0)
    {
      int fd = open(tokens_get_token(tokens, i+1), O_RDONLY);
      dup2(fd, 0);
      i+=1;
    } else if (strcmp(argument, ">") == 0)
    {
      int fd = open(tokens_get_token(tokens, i+1), O_CREAT|O_WRONLY, 0644);
      dup2(fd, 1);
      i+=1;
    } else if (strcmp(argument, "&") != 0) {
      //No redirection
      arguments[i] = argument;
      total_args += 1;
    }
  }
  arguments[total_args] = '\0'; //add NULL Terminator to the end
  //Check if we need to explore the PATH variable (no slashes in path)
  if (has_slash(program) < 0) 
  {
    char* path_env = getenv("PATH");
    char* token = strtok(path_env, ":");
    //modify program to have enough memory space
    char* full_path;
    while (token != NULL) {
      full_path = malloc((strlen(path_env) + strlen(program)) * sizeof(char));

      //copy token into full_path and strcat the program
      strcpy(full_path, token);
      sprintf(full_path, "%s/%s", full_path,program);
      //Fork a child and execute
      pid_t child_pid = fork();
      if (child_pid == 0)
      {
        execv(full_path, arguments);
        exit(2); //The exec command failed
      } else {
        int status;
        wait(&status);
        if (WEXITSTATUS(status) != 2)
        {
          return 0; //The program is a success!
        }
      }

      //free full path and grab the next environment path
      free(full_path);
      token = strtok(NULL, ":");
    }

    //Do one more path run from the current directory
    char current_path[1024];
    getcwd(current_path, sizeof(current_path));
    sprintf(current_path, "%s/%s", current_path,program);
    pid_t child_pid = fork();
    if (child_pid == 0)
    {
      execv(current_path, arguments);
      exit(2); //The exec command failed
    } else {
      free(full_path);
      int status;
      wait(&status);
      if (!WIFEXITED(status))
      {
        return 0; //The program is a success!
      }
    }
  } 
  else { //Otherwise, assume program is the full path
    execv(program, arguments);
  }
  return -1;
}
Пример #3
0
int main(int argc, char **argv)
{
  char *me = argv[0], *data, **new_argv;
  char *exe_path, *lib_path, *dll_path;
  int start, prog_end, end, count, fd, v, en, x11;
  int argpos, inpos, collcount = 1, fix_argv;

  if (config[7] == '[') {
    write_str(2, argv[0]);
    write_str(2, ": this is an unconfigured starter\n");
    return 1;
  }

  if (me[0] == '/') {
    /* Absolute path */
  } else if (has_slash(me)) {
    /* Relative path with a directory: */
    char *buf;
    long buflen = 4096;
    buf = (char *)malloc(buflen);
    me = path_append(getcwd(buf, buflen), me);
  } else {
    /* We have to find the executable by searching PATH: */
    char *path = copy_string(getenv("PATH")), *p, *m;
    int more;

    if (!path) {
      path = "";
    }

    while (1) {
      /* Try each element of path: */
      for (p = path; *p && (*p != ':'); p++) { }
      if (*p) {
	*p = 0;
	more = 1;
      } else
	more = 0;

      if (!*path)
	break;

      m = path_append(path, me);

      if (executable_exists(m)) {
	if (m[0] != '/')
	  m = path_append(getcwd(NULL, 0), m);
	me = m;
	break;
      }
      free(m);

      if (more)
	path = p + 1;
      else
	break;
    }
  }
  
  /* me is now an absolute path to the binary */

  /* resolve soft links */
  while (1) {
    int len, bufsize = 127;
    char *buf;
    buf = (char *)malloc(bufsize + 1);
    len = readlink(me, buf, bufsize);
    if (len < 0) {
      if (errno == ENAMETOOLONG) {
	/* Increase buffer size and try again: */
	bufsize *= 2;
	buf = (char *)malloc(bufsize + 1);
      } else
	break;
    } else {
      /* Resolve buf relative to me: */
      buf[len] = 0;
      buf = absolutize(buf, me);
      me = buf;
      buf = (char *)malloc(bufsize + 1);
    }
  }

  start = as_int(config + 8);
  prog_end = as_int(config + 12);
  end = as_int(config + 16);
  count = as_int(config + 20);
  x11 = as_int(config + 24);

  fix_argv = try_elf_section(me, &start, &prog_end, &end);

  {
    int offset, len;
    offset = _coldir_offset;
    while (1) {
      len = strlen(_coldir + offset);
      offset += len + 1;
      if (!_coldir[offset])
	break;
      collcount++;
    }
  }

  data = (char *)malloc(end - prog_end);
  new_argv = (char **)malloc((count + argc + (2 * collcount) + 8) * sizeof(char*));

  fd = open(me, O_RDONLY, 0);
  lseek(fd, prog_end, SEEK_SET);
  {
    int expected_length = end - prog_end;
    if (expected_length != read(fd, data, expected_length)) {
      printf("read failed to read all %i bytes from file %s\n", expected_length, me);
      abort();
    }
  }
  close(fd);
  
  exe_path = data;
  data = next_string(data);

  lib_path = data;
  data = next_string(data);

  exe_path = absolutize(exe_path, me);
  lib_path = absolutize(lib_path, me);

# ifdef OS_X
#  define LD_LIB_PATH "DYLD_LIBRARY_PATH"
# else
#  define LD_LIB_PATH "LD_LIBRARY_PATH"
# endif

  if (*lib_path) {
    dll_path = getenv(LD_LIB_PATH);
    if (!dll_path) {
      dll_path = "";
    }
    dll_path = string_append(dll_path, ":");
    dll_path = string_append(lib_path, dll_path);
    dll_path = string_append(LD_LIB_PATH "=", dll_path);
    putenv(dll_path);
  }

  new_argv[0] = me;

  argpos = 1;
  inpos = 1;

  /* Keep all X11 flags to the front: */
  if (x11) {
    int n;
    while (inpos < argc) {
      n = is_x_flag(argv[inpos]);
      if (!n)
	break;
      if (inpos + n > argc) {
	write_str(2, argv[0]);
	write_str(2, ": missing an argument for ");
	write_str(2, argv[inpos]);
	write_str(2, "\n");
	return 1;
      }
      while (n--) {
	new_argv[argpos++] = argv[inpos++];
      }
    }
  }

  /* Add -X and -S flags */
  {
    int offset, len;
    offset = _coldir_offset;
    new_argv[argpos++] = "-X";
    new_argv[argpos++] = absolutize(_coldir + offset, me);
    while (1) {
      len = strlen(_coldir + offset);
      offset += len + 1;
      if (!_coldir[offset])
	break;
      new_argv[argpos++] = "-S";
      new_argv[argpos++] = absolutize(_coldir + offset, me);
    }
  }

  if (fix_argv) {
    /* next three args are "-k" and numbers; fix 
       the numbers to match start and prog_end */
    fix_argv = argpos + 1;
  }

  /* Add built-in flags: */
  while (count--) {
    new_argv[argpos++] = data;
    data = next_string(data);
  }

  /* Propagate new flags (after the X11 flags) */
  while (inpos < argc) {
    new_argv[argpos++] = argv[inpos++];
  }

  new_argv[argpos] = NULL;

  if (fix_argv) {
    new_argv[fix_argv] = num_to_string(start);
    new_argv[fix_argv+1] = num_to_string(prog_end);
  }

  /* Execute the original binary: */

  v = execv(exe_path, new_argv);
  en = errno;

  write_str(2, argv[0]);
  write_str(2, ": failed to start ");
  write_str(2, exe_path);
  write_str(2, " (");
  write_str(2, strerror(en));
  write_str(2, ")\n");
  if (*lib_path) {
    write_str(2, " used library path ");
    write_str(2, lib_path);
    write_str(2, "\n");
  }

  return v;
}