Ejemplo n.º 1
0
static int
create_dir(struct pkg *pkg, struct pkg_dir *d)
{
	struct stat st;

	if (mkdirat(pkg->rootfd, RELATIVE_PATH(d->path), 0755) == -1)
		if (!mkdirat_p(pkg->rootfd, RELATIVE_PATH(d->path)))
			return (EPKG_FATAL);
	if (fstatat(pkg->rootfd, RELATIVE_PATH(d->path), &st, 0) == -1) {
		if (errno != ENOENT) {
			pkg_fatal_errno("Fail to stat directory %s", d->path);
		}
		if (fstatat(pkg->rootfd, RELATIVE_PATH(d->path), &st, AT_SYMLINK_NOFOLLOW) == 0) {
			unlinkat(pkg->rootfd, RELATIVE_PATH(d->path), 0);
		}
		if (mkdirat(pkg->rootfd, RELATIVE_PATH(d->path), 0755) == -1) {
			pkg_fatal_errno("Fail to create directory %s", d->path);
		}
	}

	if (st.st_uid == d->uid && st.st_gid == d->gid &&
	    (st.st_mode & ~S_IFMT) == (d->perm & ~S_IFMT)) {
		d->noattrs = true;
	}

	return (EPKG_OK);
}
Ejemplo n.º 2
0
static int
opendate(tm_t *date)
{
	char ypath[] = {0, 0, 0, 0, 0};
	char mpath[] = {0, 0, 0};
	char dpath[] = {0, 0, 0};


	strftime(ypath, sizeof (ypath), "%Y", date);
	strftime(mpath, sizeof (mpath), "%m", date);
	strftime(dpath, sizeof (dpath), "%d", date);

	mkdirat(dates_fd, ypath, ALLRWX);

	int yfd = openat(dates_fd, &ypath[0], O_RDONLY);

	mkdirat(yfd, &mpath[0], ALLRWX);

	int mfd = openat(yfd, &mpath[0], O_RDONLY);

	mkdirat(mfd, dpath, ALLRWX);

	int dfd = openat(mfd, &dpath[0], O_RDONLY);

	close(yfd);
	close(mfd);
	return (dfd);
}
Ejemplo n.º 3
0
CAMLprim value caml_extunix_mkdirat(value v_dirfd, value v_name, value v_mode)
{
  CAMLparam3(v_dirfd, v_name, v_mode);
  int ret = mkdirat(Int_val(v_dirfd), String_val(v_name), Int_val(v_mode));
  if (ret != 0) uerror("mkdirat", v_name);
  CAMLreturn(Val_unit);
}
Ejemplo n.º 4
0
/**
 * Ensure that the given path and all of the parent directories are created
 * with the desired permissions.
 */
int mkdirs(const char* path, mode_t perm) {
  char *buffer = strdup(path);
  char *token;
  int cwd = open("/", O_RDONLY);
  if (cwd == -1) {
    fprintf(LOGFILE, "Can't open / in %s - %s\n", path, strerror(errno));
    free(buffer);
    return -1;
  }
  for(token = strtok(buffer, "/"); token != NULL; token = strtok(NULL, "/")) {
    if (mkdirat(cwd, token, perm) != 0) {
      if (errno != EEXIST) {
        fprintf(LOGFILE, "Can't create directory %s in %s - %s\n", 
                token, path, strerror(errno));
        close(cwd);
        free(buffer);
        return -1;
      }
    }
    int new_dir = openat(cwd, token, O_RDONLY);
    close(cwd);
    cwd = new_dir;
    if (cwd == -1) {
      fprintf(LOGFILE, "Can't open %s in %s - %s\n", token, path, 
              strerror(errno));
      free(buffer);
      return -1;
    }
  }
  free(buffer);
  close(cwd);
  return 0;
}
Ejemplo n.º 5
0
void sys_stat_check_functions()
{
    (void)chmod((const char *)1234, (mode_t)0);
    (void)fchmod(0, (mode_t)0);
#if HAVE_XXXAT
    (void)fchmodat(0, (const char *)1234, (mode_t)0, 0);
#endif
    (void)fstat(0, (struct stat *)1234);
#if HAVE_XXXAT
    (void)fstatat(0, (const char *)1234, (struct stat *)1234, 0);
#endif
    (void)futimens(0, (const struct timespec *)1234);
    (void)lstat((const char *)1234, (struct stat *)1234);
    (void)mkdir((const char *)1234, (mode_t)0);
#if HAVE_XXXAT
    (void)mkdirat(0, (const char *)1234, (mode_t)0);
#endif
    (void)mkfifo((const char *)1234, (mode_t)0);
    (void)mkfifoat(0, (const char *)1234, (mode_t)0);
    (void)mknod((const char *)1234, (mode_t)0, (dev_t)0);
    (void)mknodat(0, (const char *)1234, (mode_t)0, (dev_t)0);
    (void)stat((const char *)1234, (struct stat *)1234);
    (void)umask((mode_t)0);
    (void)utimensat(0, (const char *)1234, (const struct timespec *)1234, 0);
}
Ejemplo n.º 6
0
int sc_nonfatal_mkpath(const char *const path, mode_t mode)
{
	// If asked to create an empty path, return immediately.
	if (strlen(path) == 0) {
		return 0;
	}
	// We're going to use strtok_r, which needs to modify the path, so we'll
	// make a copy of it.
	char *path_copy SC_CLEANUP(sc_cleanup_string) = NULL;
	path_copy = strdup(path);
	if (path_copy == NULL) {
		return -1;
	}
	// Open flags to use while we walk the user data path:
	// - Don't follow symlinks
	// - Don't allow child access to file descriptor
	// - Only open a directory (fail otherwise)
	const int open_flags = O_NOFOLLOW | O_CLOEXEC | O_DIRECTORY;

	// We're going to create each path segment via openat/mkdirat calls instead
	// of mkdir calls, to avoid following symlinks and placing the user data
	// directory somewhere we never intended for it to go. The first step is to
	// get an initial file descriptor.
	int fd SC_CLEANUP(sc_cleanup_close) = AT_FDCWD;
	if (path_copy[0] == '/') {
		fd = open("/", open_flags);
		if (fd < 0) {
			return -1;
		}
	}
	// strtok_r needs a pointer to keep track of where it is in the string.
	char *path_walker = NULL;

	// Initialize tokenizer and obtain first path segment.
	char *path_segment = strtok_r(path_copy, "/", &path_walker);
	while (path_segment) {
		// Try to create the directory.  It's okay if it already existed, but
		// return with error on any other error. Reset errno before attempting
		// this as it may stay stale (errno is not reset if mkdirat(2) returns
		// successfully).
		errno = 0;
		if (mkdirat(fd, path_segment, mode) < 0 && errno != EEXIST) {
			return -1;
		}
		// Open the parent directory we just made (and close the previous one
		// (but not the special value AT_FDCWD) so we can continue down the
		// path.
		int previous_fd = fd;
		fd = openat(fd, path_segment, open_flags);
		if (previous_fd != AT_FDCWD && close(previous_fd) != 0) {
			return -1;
		}
		if (fd < 0) {
			return -1;
		}
		// Obtain the next path segment.
		path_segment = strtok_r(NULL, "/", &path_walker);
	}
	return 0;
}
Ejemplo n.º 7
0
// XXX: check flags
static int little_open(sqlite3_vfs *self, const char* zName,
                sqlite3_file *f, int nOut, int *zOut) {

  int dfd;

  trace("OPEN %s\n", zName);

  little_file *file = (little_file*)f;

  (file->base_file).pMethods  = &little_methods;
  file->name      = zName;      // is it OK to hold on the ptr here?
  if (mkdir(zName,0777) == -1 && errno != EEXIST) return SQLITE_CANTOPEN;

  dfd = open(zName,O_RDONLY);
  if (mkdirat(dfd,"shared",0777) == -1 && errno != EEXIST) {
    close(dfd);
    return SQLITE_CANTOPEN;
  }
  close(dfd);

  file->lastblock = -1;
  file->dirty = 0;
  file->shared_lock_number = -1;

  if (0 != get_version(file->name, &(file->version), &(file->nextfreeblock))) {
    if (errno == ENOENT) {
      if (0 != recover(file))
        return SQLITE_CANTOPEN;
    }
  }
  return SQLITE_OK;
}
Ejemplo n.º 8
0
static int
openday(day_t day)
{
	char *path;
	switch (day) {

	case MON:
		path = "mon";
		break;
	case TUES:
		path = "tues";
		break;
	case WED:
		path = "wed";
		break;
	case THUR:
		path = "thur";
		break;
	case FRI:
		path = "fri";
		break;
	case SAT:
		path = "sat";
		break;
	case SUN:
		path = "sun";
		break;

	default:
		return (-1);
	}

	mkdirat(days_fd, path, ALLRWX);
	return (openat(days_fd, path, O_RDONLY));
}
Ejemplo n.º 9
0
void create_dir(void)
{
	if (mkdirat(parent_fd, name, S_IRWXU)) {
		perror("mkdir");
		exit(1);
	}
}
Ejemplo n.º 10
0
static int recv_dir(int socket, struct server_data* server_data, int dir_fd)
{
    char* dirname = NULL;
    int ret = -1;
    if (server_receive(socket, server_data, data_handler_stringify, &dirname) != 0) {
        VIR_DEBUG("server_receive was not successfull (on dir)!");
        goto err;
    }

    //permission 664
    if (mkdirat(dir_fd, dirname, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
        if (errno == EEXIST) {
            VIR_DEBUG("directory '%s' already exists. jump mkdir step", dirname);
        } else {
            VIR_DEBUG("errno: %s", strerror(errno));
            goto err;
        }
    }

    VIR_DEBUG("received dir '%s' (%lu byte(s)).", dirname, strlen(dirname)+1);

    ret = 0;
 err:
    free(dirname);
    return ret;
}
Ejemplo n.º 11
0
/**
 * glnx_mkdtempat:
 * @dfd: Directory fd
 * @tmpl: (type filename): template directory name, last 6 characters will be replaced
 * @mode: permissions to create the temporary directory with
 * @error: Error
 *
 * Similar to g_mkdtemp_full, but using openat.
 */
gboolean
glnx_mkdtempat (int dfd,
                gchar *tmpl,
                int mode,
                GError **error)
{
  int count;

  g_return_val_if_fail (tmpl != NULL, -1);

  for (count = 0; count < 100; count++)
    {
      glnx_gen_temp_name (tmpl);

      if (mkdirat (dfd, tmpl, mode) == -1)
        {
          if (errno == EEXIST)
            continue;

          /* Any other error will apply also to other names we might
           *  try, and there are 2^32 or so of them, so give up now.
           */
          glnx_set_prefix_error_from_errno (error, "%s", "mkdirat");
          return FALSE;
        }

      return TRUE;
    }

  g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
               "mkstempat ran out of combinations to try.");
  return FALSE;
}
Ejemplo n.º 12
0
/**
 * ot_util_ensure_directory_and_fsync:
 * @dir: Path to a directory
 * @cancellable: Cancellable
 * @error: Error
 *
 * Create @dir (and all intermediate parent directories), ensuring
 * that all entries are on disk.
 */
gboolean
ot_util_ensure_directory_and_fsync (GFile         *dir,
                                    GCancellable  *cancellable,
                                    GError       **error)
{
  gboolean ret = FALSE;
  int parentfd = -1;
  const char *basename = gs_file_get_basename_cached (dir);
  g_autoptr(GFile) parent = g_file_get_parent (dir);
  
 again:
  parentfd = open (gs_file_get_path_cached (parent),
                   O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC);
  if (parentfd == -1)
    {
      if (errno == ENOENT)
        {
          if (!ot_util_ensure_directory_and_fsync (parent, cancellable, error))
            goto out;
          goto again;
        }
      else
        {
          int errsv = errno;
          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
                       "opendir: %s", g_strerror (errsv));
          goto out;
        }
    }
  
  if (mkdirat (parentfd, basename, 0777) == -1)
    {
      if (errno == EEXIST)
        {
          ;
        }
      else
        {
          int errsv = errno;
          g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
                       "mkdirat: %s", g_strerror (errsv));
          goto out;
        }
    }

  if (fsync (parentfd) == -1)
    {
      int errsv = errno;
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
                   "fsync: %s", g_strerror (errsv));
      goto out;
    }

  ret = TRUE;
 out:
  if (parentfd != -1)
    (void) close (parentfd);
  return ret;
}
Ejemplo n.º 13
0
static int
callback_mkdir (const char *path, mode_t mode)
{
  path = ENSURE_RELPATH (path);
  if (mkdirat (basefd, path, mode) == -1)
    return -errno;
  return 0;
}
Ejemplo n.º 14
0
int main(int argc, char *argv[])
{
  int at_fd = AT_FDCWD;
  const char *path;
  mode_t mode = 0777;
  int want_pause = 0;
  unsigned int delay = 0;

  struct option opts[] = {
    { "at", 1, NULL, 'a' },
    { "help", 0, NULL, 'h' },
    { "mode", 1, NULL, 'm' },
    { "pause", 0, NULL, 'p' },
    { "sleep", 1, NULL, 's' },
    { NULL }
  };

  int c;
  while ((c = getopt_long(argc, argv, "a:hm:ps:", opts, 0)) != -1) {
    switch (c) {
    case 'a':
      at_fd = atoi(optarg);
      break;
    case 'h':
      usage(stdout, EXIT_SUCCESS);
      break;
    case 'm':
      mode = strtol(optarg, NULL, 0);
      break;
    case 'p':
      want_pause = 1;
      break;
    case 's':
      delay = strtoul(optarg, NULL, 0);
      break;
    case '?':
      fprintf(stderr, "Try '%s --help' for more information\n",
	      program_invocation_short_name);
      exit(EXIT_FAILURE);
    }
  }

  if (argc - optind < 1)
    usage(stderr, EXIT_FAILURE);

  path = argv[optind];

  if (mkdirat(at_fd, path, mode) < 0)
    FATAL("cannot mkdir '%s' with mode %04o: %m\n", path, mode);

  if (want_pause)
    pause();

  if (delay > 0)
    sleep(delay);

  return 0;
}
Ejemplo n.º 15
0
static int
opentodos(int dfd)
{
	if (dfd != -1) {
		mkdirat(dfd, "todos", ALLRWX);
		return (openat(dfd, "todos", O_RDONLY));
	}
	return (todos_fd);
}
Ejemplo n.º 16
0
bool TestInodeReuse(void) {
    BEGIN_TEST;

    ASSERT_EQ(mkdir("::reuse", 0755), 0);
    DIR* d = opendir("::reuse");
    ASSERT_NONNULL(d);
    for (size_t i = 0; i < 1000; i++) {
        ASSERT_EQ(mkdirat(dirfd(d), "foo", 0666), 0);
        if (reuse_subdirectory) {
            ASSERT_EQ(mkdirat(dirfd(d), "foo/bar", 0666), 0);
            ASSERT_EQ(unlinkat(dirfd(d), "foo/bar", 0), 0);
        }
        ASSERT_EQ(unlinkat(dirfd(d), "foo", 0), 0);
    }
    ASSERT_EQ(closedir(d), 0);
    ASSERT_EQ(rmdir("::reuse"), 0);
    END_TEST;
}
Ejemplo n.º 17
0
void test_create_dir_at()
{
  char *dir = alloc_filename("dir");
  int dir_fd = get_dir_fd(".");
  t_check_zero(mkdirat(dir_fd, dir, 0777));
  t_check_zero(unlinkat(dir_fd, dir, AT_REMOVEDIR));
  close(dir_fd);
  free(dir);
}
Ejemplo n.º 18
0
static int open_imgdir(void)
{
	char p[10];

	sprintf(p, "%d", cur_iter);
	mkdirat(wdir_fd, p, 0700);
	cur_imgdir = openat(wdir_fd, p, O_DIRECTORY);
	criu_set_images_dir_fd(cur_imgdir);
}
Ejemplo n.º 19
0
CAMLprim value netsys_mkdirat(value dirfd, value path, value perm)
{
#ifdef HAVE_AT
    if (mkdirat(Int_val(dirfd), String_val(path), Int_val(perm)) == -1) 
	uerror("mkdirat", path);
    return Val_unit;
#else
    invalid_argument("Netsys_posix.mkdirat not available");
#endif
}
Ejemplo n.º 20
0
/* create a directory */
int create_directory_at(int dirfd, const char *directory, mode_t mode, int mkparents)
{
	int rc;
	size_t len, l;
	char *copy;
	const char *iter;

	rc = mkdirat(dirfd, directory, mode);
	if (!rc || errno != ENOENT)
		return rc;

	/* check parent of dest */
	iter = strrchr(directory, '/');
	len = iter ? (size_t)(iter - directory) : 0;
	if (!len)
		return rc;
	copy = strndupa(directory, len);

	/* backward loop */
	l = len;
	rc = mkdirat(dirfd, copy, mode);
	while(rc) {
		if (errno != ENOENT)
			return rc;
		while (l && copy[l] != '/')
			l--;
		if (l == 0)
			return rc;
		copy[l] = 0;
		rc = mkdirat(dirfd, copy, mode);
	}
	/* forward loop */
	while(l < len) {
		copy[l] = '/';
		while (copy[++l]);
		rc = mkdirat(dirfd, copy, mode);
		if (rc)
			return rc;
	}

	/* finalize */
	return mkdirat(dirfd, directory, mode);
}
/* Replicates a mkdir -p/--parents command for the dir the filename is in */
static void mkdir_for_file(int wdir, const char *filename) {
    char tmp[256];
    strncpy(tmp, filename, sizeof(tmp));
    for(char *p = tmp + 1; *p; p++) {
        if(*p == '/') {
            *p = 0;
            mkdirat(wdir, tmp, S_IRWXU);
            *p = '/';
        }
    }
}
Ejemplo n.º 22
0
int
ast_mkdirat(int cwd, const char* path, mode_t mode)
{
	int	r = -1;

	PATHIFY(cwd, path, 1, 1);
	RESTART(r, mkdirat(cwd, path, mode));
	PATHEND();

	return r;
}
Ejemplo n.º 23
0
int exist_dtable::create(int dfd, const char * file, const params & config, dtable::iter * source, const ktable * shadow)
{
	int e_dfd, r;
	params base_config, dnebase_config;
	const dtable_factory * base = dtable_factory::lookup(config, "base");
	const dtable_factory * dnebase = dtable_factory::lookup(config, "dnebase");
	if(!base || !dnebase)
		return -ENOENT;
	if(!config.get("base_config", &base_config, params()))
		return -EINVAL;
	if(!config.get("dnebase_config", &dnebase_config, params()))
		return -EINVAL;
	
	if(!source_shadow_ok(source, shadow))
		return -EINVAL;
	
	r = mkdirat(dfd, file, 0755);
	if(r < 0)
		return r;
	e_dfd = openat(dfd, file, O_RDONLY);
	if(e_dfd < 0)
		goto fail_open;
	
	/* just to be sure */
	source->first();
	{
		dtable_skip_iter<dne_skip_test> base_source(source);
		r = base->create(e_dfd, "base", base_config, &base_source, NULL);
		if(r < 0)
			goto fail_base;
	}
	
	source->first();
	{
		full_ktable full_shadow(source);
		nonshadow_skip_test skip_test(shadow);
		dtable_skip_iter<nonshadow_skip_test> dnebase_source(source, skip_test);
		r = dnebase->create(e_dfd, "dnebase", dnebase_config, &dnebase_source, &full_shadow);
		if(r < 0)
			goto fail_dnebase;
	}
	
	close(e_dfd);
	return 0;
	
fail_dnebase:
	util::rm_r(e_dfd, "base");
fail_base:
	close(e_dfd);
fail_open:
	unlinkat(dfd, file, AT_REMOVEDIR);
	return (r < 0) ? r : -1;
}
Ejemplo n.º 24
0
static int create_directory(char *file, mode_t mode)
{
	int rc;
	char *last = strrchr(file, '/');
	if (last != NULL)
		*last = 0;
	rc = mkdirat(workdirfd, file, mode);
	if (rc) {
		if (errno == EEXIST)
			rc = 0;
		else if (errno == ENOENT) {
			rc = create_directory(file, mode);
			if (!rc)
				rc = mkdirat(workdirfd, file, mode);
		}
	}
	if (rc)
		ERROR("can't create directory %s", file);
	if (last != NULL)
		*last = '/';
	return rc;
}
Ejemplo n.º 25
0
int btree_dtable::create(int dfd, const char * file, const params & config, dtable::iter * source, const ktable * shadow)
{
	int bt_dfd, r;
	params base_config;
	dtable * base_dtable;
	const dtable_factory * base = dtable_factory::lookup(config, "base");
	if(!base)
		return -ENOENT;
	if(!config.get("base_config", &base_config, params()))
		return -EINVAL;
	if(!base->indexed_access(base_config))
		return -ENOSYS;
	
	if(!source_shadow_ok(source, shadow))
		return -EINVAL;
	
	r = mkdirat(dfd, file, 0755);
	if(r < 0)
		return r;
	bt_dfd = openat(dfd, file, O_RDONLY);
	if(bt_dfd < 0)
		goto fail_open;
	
	r = base->create(bt_dfd, "base", base_config, source, shadow);
	if(r < 0)
		goto fail_create;
	
	base_dtable = base->open(bt_dfd, "base", base_config, NULL);
	if(!base_dtable)
		goto fail_reopen;
	
	r = write_btree(bt_dfd, "btree", base_dtable);
	if(r < 0)
		goto fail_write;
	
	base_dtable->destroy();
	
	close(bt_dfd);
	return 0;
	
fail_write:
	base_dtable->destroy();
fail_reopen:
	util::rm_r(bt_dfd, "base");
fail_create:
	close(bt_dfd);
fail_open:
	unlinkat(dfd, file, AT_REMOVEDIR);
	return (r < 0) ? r : -1;
}
Ejemplo n.º 26
0
int main() {
  struct stat st;
  struct stat st2;

  openat(AT_FDCWD, PWD "openat.txt", O_CREAT | O_WRONLY, 0644);
  assert(stat("openat.txt", &st) == 0); // relative path

  assert(faccessat(AT_FDCWD, PWD "openat.txt", F_OK, 0) == 0);
  assert(fstatat(AT_FDCWD, PWD "openat.txt", &st2, 0) == 0);
  assert(fchmodat(AT_FDCWD, PWD "openat.txt", 0777, 0) == 0);

  struct timeval my_times[2];
  my_times[0].tv_sec = 0;
  my_times[0].tv_usec = 0;
  my_times[1].tv_sec = 0;
  my_times[1].tv_usec = 0;
  assert(futimesat(AT_FDCWD, PWD "openat.txt", my_times, 0) == 0);

  // see /etc/passwd, user 'pgbovine' is 508:100
  assert(fchownat(AT_FDCWD, PWD "openat.txt", 508, 100, 0) == 0);

  assert(linkat(AT_FDCWD, PWD "openat.txt", AT_FDCWD, PWD "openat_hardlink.txt", 0) == 0);
  assert(stat("openat_hardlink.txt", &st) == 0); // relative path

  assert(symlinkat(PWD "openat.txt", AT_FDCWD, PWD "openat_symlink.txt") == 0);
  assert(lstat("openat_symlink.txt", &st) == 0); // relative path

  char res[300];
  assert(readlinkat(AT_FDCWD, PWD "openat_symlink.txt", res, sizeof(res)) > 0);

  assert(renameat(AT_FDCWD, PWD "openat.txt", AT_FDCWD, PWD "openat_newname.txt", 0) == 0);
  assert(stat("openat.txt", &st) != 0); // should not exist anymore
  assert(stat("openat_newname.txt", &st) == 0); // relative path

  unlinkat(AT_FDCWD, PWD "openat_newname.txt", 0);
  unlinkat(AT_FDCWD, PWD "openat_hardlink.txt", 0);
  unlinkat(AT_FDCWD, PWD "openat_symlink.txt", 0);


  mknodat(AT_FDCWD, PWD "mknodat.fifo", S_IFIFO);
  assert(stat("mknodat.fifo", &st) == 0); // relative path
  unlinkat(AT_FDCWD, PWD "mknodat.fifo", 0);

  mkdirat(AT_FDCWD, PWD "mkdirat_dir", 0);
  assert(stat("mkdirat_dir", &st) == 0); // relative path
  unlinkat(AT_FDCWD, PWD "mkdirat_dir", AT_REMOVEDIR); // like 'rmdir'

  return 0;
}
void sc_cgroup_freezer_join(const char *snap_name, pid_t pid)
{
	// Format the name of the cgroup hierarchy.
	char buf[PATH_MAX] = { 0 };
	sc_must_snprintf(buf, sizeof buf, "snap.%s", snap_name);

	// Open the freezer cgroup directory.
	int cgroup_fd SC_CLEANUP(sc_cleanup_close) = -1;
	cgroup_fd = open(freezer_cgroup_dir,
			 O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
	if (cgroup_fd < 0) {
		die("cannot open freezer cgroup (%s)", freezer_cgroup_dir);
	}
	// Create the freezer hierarchy for the given snap.
	if (mkdirat(cgroup_fd, buf, 0755) < 0 && errno != EEXIST) {
		die("cannot create freezer cgroup hierarchy for snap %s",
		    snap_name);
	}
	// Open the hierarchy directory for the given snap.
	int hierarchy_fd SC_CLEANUP(sc_cleanup_close) = -1;
	hierarchy_fd = openat(cgroup_fd, buf,
			      O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
	if (hierarchy_fd < 0) {
		die("cannot open freezer cgroup hierarchy for snap %s",
		    snap_name);
	}
	// Since we may be running from a setuid but not setgid executable, ensure
	// that the group and owner of the hierarchy directory is root.root.
	if (fchownat(hierarchy_fd, "", 0, 0, AT_EMPTY_PATH) < 0) {
		die("cannot change owner of freezer cgroup hierarchy for snap %s to root.root", snap_name);
	}
	// Open the tasks file.
	int tasks_fd SC_CLEANUP(sc_cleanup_close) = -1;
	tasks_fd = openat(hierarchy_fd, "tasks",
			  O_WRONLY | O_NOFOLLOW | O_CLOEXEC);
	if (tasks_fd < 0) {
		die("cannot open tasks file for freezer cgroup hierarchy for snap %s", snap_name);
	}
	// Write the process (task) number to the tasks file. Linux task IDs are
	// limited to 2^29 so a long int is enough to represent it.
	// See include/linux/threads.h in the kernel source tree for details.
	int n = sc_must_snprintf(buf, sizeof buf, "%ld", (long)pid);
	if (write(tasks_fd, buf, n) < n) {
		die("cannot move process %ld to freezer cgroup hierarchy for snap %s", (long)pid, snap_name);
	}
	debug("moved process %ld to freezer cgroup hierarchy for snap %s",
	      (long)pid, snap_name);
}
Ejemplo n.º 28
0
void logging_enable(int argc, char **argv)
{
	int x;
	char timedata[64];
	struct tm tm;
	time_t t;

	time(&t);
#ifdef _WIN32
	localtime_s(&tm, &t);
#else
	localtime_r(&t, &tm);
#endif
	if(strftime(timedata, sizeof(timedata), "%c", &tm) <= 0) {
		timedata[0] = 0;
	}

	if(rotate("debug.log") < 0) {
		return;
	}
	if(rotate("create.dot") < 0) {
		return;
	}
	if(rotate("update.dot") < 0) {
		return;
	}
	if(mkdirat(tup_top_fd(), LOG_DIR, 0777) < 0) {
		if(errno != EEXIST) {
			perror(LOG_DIR);
			fprintf(stderr, "tup error: Unable to create debug log directory.\n");
			enabled = 0;
			return;
		}
	}
	logfile = fopen(LOG_NAME, "w+");
	if(!logfile) {
		perror(LOG_NAME);
		fprintf(stderr, "tup error: Unable to create debug log.\n");
		enabled = 0;
		return;
	}
	enabled = 1;
	log_debug("Tup update at %s:", timedata);
	for(x=0; x<argc; x++) {
		log_debug(" '%s'", argv[x]);
	}
	log_debug("\n");
}
Ejemplo n.º 29
0
int
process_tree(int srv_fd, int dstdir_fd)
{
	FTS		*ftsp;
	FTSENT		*entry;
	const char	*argv[2];
	const char	*path;

	argv[0] = ".";
	argv[1] = (char *)NULL;

	if ((ftsp = fts_open((char * const *)argv,
	    FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
		warn("fts_open");
		return -1;
	}

	while ((entry = fts_read(ftsp)) != NULL) {
		path = entry->fts_path + 2;
		switch (entry->fts_info) {
		case FTS_F:
			if (process_manpage(srv_fd, dstdir_fd, path) == -1) {
				fts_close(ftsp);
				return -1;
			}
			break;
		case FTS_D:
			if (*path != '\0' &&
			    mkdirat(dstdir_fd, path, S_IRWXU | S_IRGRP |
			      S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
			    errno != EEXIST) {
				warn("mkdirat(%s)", path);
				(void)fts_set(ftsp, entry, FTS_SKIP);
			}
			break;
		case FTS_DP:
			break;
		default:
			warnx("%s: not a regular file", path);
			break;
		}
	}

	fts_close(ftsp);
	return 0;
}
Ejemplo n.º 30
0
Archivo: dir.c Proyecto: 5kg/systemtap
int main()
{
  int fd;

  mkdir("foobar", 0765);
  //staptest// mkdir ("foobar", 0765) =

  chdir("foobar");
  //staptest// chdir ("foobar") = 0

  chdir("..");
  //staptest// chdir ("..") = 0

  fd = open("foobar", O_RDONLY);
  //staptest// open ("foobar", O_RDONLY[[[[.O_LARGEFILE]]]]?) = NNNN

  fchdir(fd);
  //staptest// fchdir (NNNN) = 0

  chdir("..");
  //staptest// chdir ("..") = 0

  close(fd);
  //staptest// close (NNNN) = 0

  rmdir("foobar");
  //staptest// rmdir ("foobar") = 0

  fd = open(".", O_RDONLY);
  //staptest// open (".", O_RDONLY[[[[.O_LARGEFILE]]]]?) = NNNN

#ifdef SYS_mkdirat
  mkdirat(fd, "xyzzy", 0765);
  //staptest// mkdirat (NNNN, "xyzzy", 0765) = 0

#endif

  close(fd);
  //staptest// close (NNNN) = 0

  rmdir("xyzzy");
  //staptest// rmdir ("xyzzy") =

  return 0;
}