struct sc_ns_group *sc_open_ns_group(const char *group_name, const unsigned flags) { struct sc_ns_group *group = sc_alloc_ns_group(); debug("opening namespace group directory %s", sc_ns_dir); group->dir_fd = open(sc_ns_dir, O_DIRECTORY | O_PATH | O_CLOEXEC | O_NOFOLLOW); if (group->dir_fd < 0) { if (flags & SC_NS_FAIL_GRACEFULLY && errno == ENOENT) { free(group); return NULL; } die("cannot open directory for namespace group %s", group_name); } char lock_fname[PATH_MAX]; must_snprintf(lock_fname, sizeof lock_fname, "%s%s", group_name, SC_NS_LOCK_FILE); debug("opening lock file for namespace group %s", group_name); group->lock_fd = openat(group->dir_fd, lock_fname, O_CREAT | O_RDWR | O_CLOEXEC | O_NOFOLLOW, 0600); if (group->lock_fd < 0) { die("cannot open lock file for namespace group %s", group_name); } group->name = strdup(group_name); if (group->name == NULL) { die("cannot duplicate namespace group name %s", group_name); } return group; }
// Check that allocating a namespace group sets up internal data structures to // safe values. static void test_sc_alloc_ns_group() { struct sc_ns_group *group = NULL; group = sc_alloc_ns_group(); g_test_queue_free(group); g_assert_nonnull(group); g_assert_cmpint(group->dir_fd, ==, -1); g_assert_cmpint(group->lock_fd, ==, -1); g_assert_cmpint(group->event_fd, ==, -1); g_assert_cmpint(group->child, ==, 0); g_assert_cmpint(group->should_populate, ==, false); g_assert_null(group->name); }
static void test_sc_unlock_ns_mutex_precondition() { sc_test_use_fake_ns_dir(); if (g_test_subprocess()) { struct sc_ns_group *group = sc_alloc_ns_group(); g_test_queue_free(group); // Try to unlock the mutex, this should abort because we never opened the // lock file and don't have a valid file descriptor. sc_unlock_ns_mutex(group); return; } g_test_trap_subprocess(NULL, 0, 0); g_test_trap_assert_failed(); }
struct sc_ns_group *sc_open_ns_group(const char *group_name, const unsigned flags) { struct sc_ns_group *group = sc_alloc_ns_group(); debug("opening namespace group directory %s", sc_ns_dir); group->dir_fd = open(sc_ns_dir, O_DIRECTORY | O_PATH | O_CLOEXEC | O_NOFOLLOW); if (group->dir_fd < 0) { if (flags & SC_NS_FAIL_GRACEFULLY && errno == ENOENT) { free(group); return NULL; } die("cannot open directory for namespace group %s", group_name); } group->name = strdup(group_name); if (group->name == NULL) { die("cannot duplicate namespace group name %s", group_name); } return group; }