END_TEST START_TEST(test_buffer_read_write) { const int32_t data[] = {0, 1, 2, 3, 4, 5, 6, 7}; int32_t buf[nelems(data)]; gpudata *d; int err; unsigned int i; d = ops->buffer_alloc(ctx, sizeof(data), NULL, 0, NULL); ck_assert(d != NULL); err = ops->buffer_write(d, 0, data, sizeof(data)); ck_assert_int_eq(err, GA_NO_ERROR); memset(buf, 0, sizeof(data)); err = ops->buffer_read(buf, d, 0, sizeof(data)); ck_assert_int_eq(err, GA_NO_ERROR); for (i = 0; i < nelems(data); i++) { ck_assert_int_eq(data[i], buf[i]); } memset(buf, 0, sizeof(data)); err = ops->buffer_read(buf, d, sizeof(int32_t), sizeof(data)-sizeof(int32_t)); ck_assert_int_eq(err, GA_NO_ERROR); for (i = 0; i < nelems(data)-1; i++) { ck_assert_int_eq(data[i+1], buf[i]); } err = ops->buffer_write(d, sizeof(int32_t)*2, data, sizeof(data)-(sizeof(int32_t)*2)); ck_assert_int_eq(err, GA_NO_ERROR); memset(buf, 0, sizeof(data)); err = ops->buffer_read(buf, d, 0, sizeof(data)); ck_assert_int_eq(err, GA_NO_ERROR); for (i = 0; i < nelems(data)-2; i++) { ck_assert_int_eq(data[i], buf[i+2]); } for (i = 0; i < 2; i++) { ck_assert_int_eq(data[i], buf[i]); } ops->buffer_release(d); }
END_TEST START_TEST(test_buffer_move) { const int32_t data[] = {0, 1, 2, 3, 4, 5, 6, 7}; int32_t buf[nelems(data)]; gpudata *d; gpudata *d2; int err; unsigned int i; d = ops->buffer_alloc(ctx, sizeof(data), NULL, 0, NULL); ck_assert(d != NULL); d2 = ops->buffer_alloc(ctx, sizeof(data)*2, NULL, 0, NULL); ck_assert(d != NULL); err = ops->buffer_write(d, 0, data, sizeof(data)); ck_assert(err == GA_NO_ERROR); err = ops->buffer_move(d2, sizeof(data), d, 0, sizeof(data)); ck_assert(err == GA_NO_ERROR); err = ops->buffer_read(buf, d2, sizeof(data), sizeof(data)); ck_assert(err == GA_NO_ERROR); for (i = 0; i < nelems(data); i++) { ck_assert_int_eq(buf[i], data[i]); } err = ops->buffer_move(d2, 0, d, sizeof(uint32_t), sizeof(data)-sizeof(uint32_t)); ck_assert(err == GA_NO_ERROR); err = ops->buffer_read(buf, d2, 0, sizeof(data)); ck_assert(err == GA_NO_ERROR); for (i = 0; i < nelems(data)-1; i++) { ck_assert_int_eq(buf[i], data[i+1]); } ops->buffer_release(d); ops->buffer_release(d2); }
struct chasen_t * chasen_init() { int i; char *option[] = {"chasen", "-i", "w", NULL}; static struct chasen_t c; chasen_getopt_argv(option, stderr); for (i = 0; i < nelems(z); i++) { z[i].len = strlen(z[i].nam); } mt.len = strlen(mt.nam); return &c; }
int main(int argc, char *argv[]) { static const char *unlink_paths[] = { "dev/shm", "dev/ptmx", NULL }; static const dir_op_t dirs[] = { dir("dev", 0755), dir("dev/net", 0755), dir("dev/shm", 0755), dir("etc", 0755), dir("proc", 0755), dir("sys", 0755), dir("tmp", 01777), dir("dev/pts", 0755), }; static const char *devnodes[] = { "/dev/null", "/dev/zero", "/dev/full", "/dev/random", "/dev/urandom", "/dev/tty", "/dev/net/tun", "/dev/console", NULL }; static const mount_point mount_table[] = { { "/proc", "/proc", "bind", NULL, MS_BIND|MS_REC }, { "/sys", "/sys", "bind", NULL, MS_BIND|MS_REC }, { "/dev/shm", "/dev/shm", "bind", NULL, MS_BIND }, { "/dev/pts", "/dev/pts", "bind", NULL, MS_BIND }, }; const char *root; int rootfd; char to[4096]; int i; exit_if(argc < 2, "Usage: %s /path/to/root", argv[0]); root = argv[1]; /* Make stage2's root a mount point. Chrooting an application in a * directory which is not a mount point is not nice because the * application would not be able to remount "/" it as private mount. * This allows Docker to run inside rkt. * The recursive flag is to preserve volumes mounted previously by * systemd-nspawn via "rkt run -volume". * */ pexit_if(mount(root, root, "bind", MS_BIND | MS_REC, NULL) == -1, "Make / a mount point failed"); rootfd = open(root, O_DIRECTORY | O_CLOEXEC); pexit_if(rootfd < 0, "Failed to open directory \"%s\"", root); /* Some images have annoying symlinks that are resolved as dangling * links before the chroot in stage1. E.g. "/dev/shm" -> "/run/shm" * Just remove the symlinks. */ for (i = 0; unlink_paths[i]; i++) { pexit_if(unlinkat(rootfd, unlink_paths[i], 0) != 0 && errno != ENOENT && errno != EISDIR, "Failed to unlink \"%s\"", unlink_paths[i]) } /* Create the directories */ umask(0); for (i = 0; i < nelems(dirs); i++) { const dir_op_t *d = &dirs[i]; pexit_if(mkdirat(rootfd, d->name, d->mode) == -1 && errno != EEXIST, "Failed to create directory \"%s/%s\"", root, d->name); } exit_if(!ensure_etc_hosts_exists(root, rootfd), "Failed to ensure \"%s/etc/hosts\" exists", root); close(rootfd); /* systemd-nspawn already creates few /dev entries in the container * namespace: copy_devnodes() * http://cgit.freedesktop.org/systemd/systemd/tree/src/nspawn/nspawn.c?h=v219#n1345 * * But they are not visible by the apps because they are "protected" by * the chroot. * * Bind mount them individually over the chroot border. * * Do NOT bind mount the whole directory /dev because it would shadow * potential individual bind mount by stage0 ("rkt run --volume..."). * * Do NOT use mknod, it would not work for /dev/console because it is * a bind mount to a pts and pts device nodes only work when they live * on a devpts filesystem. */ for (i = 0; devnodes[i]; i++) { const char *from = devnodes[i]; int fd; /* If the file does not exist, skip it. It might be because * the kernel does not provide it (e.g. kernel compiled without * CONFIG_TUN) or because systemd-nspawn does not provide it * (/dev/net/tun is not available with systemd-nspawn < v217 */ if (access(from, F_OK) != 0) continue; exit_if(snprintf(to, sizeof(to), "%s%s", root, from) >= sizeof(to), "Path too long: \"%s\"", to); /* The mode does not matter: it will be bind-mounted over. */ fd = open(to, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644); if (fd != -1) close(fd); pexit_if(mount(from, to, "bind", MS_BIND, NULL) == -1, "Mounting \"%s\" on \"%s\" failed", from, to); } /* Bind mount directories */ for (i = 0; i < nelems(mount_table); i++) { const mount_point *mnt = &mount_table[i]; exit_if(snprintf(to, sizeof(to), "%s/%s", root, mnt->target) >= sizeof(to), "Path too long: \"%s\"", to); pexit_if(mount(mnt->source, to, mnt->type, mnt->flags, mnt->options) == -1, "Mounting \"%s\" on \"%s\" failed", mnt->source, to); } /* /dev/ptmx -> /dev/pts/ptmx */ exit_if(snprintf(to, sizeof(to), "%s/dev/ptmx", root) >= sizeof(to), "Path too long: \"%s\"", to); pexit_if(symlink("/dev/pts/ptmx", to) == -1, "Failed to create /dev/ptmx symlink"); return EXIT_SUCCESS; }