END_TEST START_TEST(cleanup_by_data) { int data1 = 1, data2 = 2; // Note: we don't care about exact value cleanup_register(cleanup_func, &data1); cleanup_register(cleanup_func, &data2); // Remove bottom one ck_assert(cleanup_unregister_data(cleanup_func, &data1)); // Top one should be still there but nothing else cleaned = 2; cleanup_run_all(); }
static int lua_cleanup_register_handle(lua_State *L) { struct lua_cleanup_data *data = lua_newuserdata(L, sizeof *data); luaL_newmetatable(L, CLEANUP_DATA_META); lua_setmetatable(L, -2); data->L = L; data->index = luaL_checkinteger(L, 1); cleanup_register(lua_cleanup_func, data); return 1; }
END_TEST START_TEST(cleanup_single) { // Test cleanup before we initialize it cleanup_run(cleanup_func); // Test cleanup it self int one = 1, two = 2; cleaned = 2; cleanup_register(cleanup_func, &one); cleanup_register(cleanup_func, &two); cleanup_run(cleanup_func); ck_assert_int_eq(1, cleaned); cleanup_run(cleanup_func); ck_assert_int_eq(0, cleaned); // Both functions should be unregisterd so this should fail ck_assert(!cleanup_unregister(cleanup_func)); // Check if we don't fail cleanup_run(cleanup_func); }
namreg_t * namreg_init( bool_t cumulative, bool_t delta, char *housekeepingdir ) { namreg_context_t *ncp; /* allocate and initialize context */ ncp = ( namreg_context_t * )calloc( 1, sizeof( namreg_context_t )); ASSERT( ncp ); /* generate a string containing the pathname of the namreg file */ ncp->nc_pathname = open_pathalloc( housekeepingdir, NAMETEMPLATE, 0 ); /* if not a cumulative restore, be sure the name registry gets removed * on exit. */ if ( ! cumulative ) { ( void )cleanup_register( namreg_abort_cleanup, ( void * )ncp, 0 ); } /* if this is a delta during a cumulative restore, the namreg file must * already exist. if not, create/truncate. */ if ( cumulative && delta ) { ncp->nc_fd = open_rwp( ncp->nc_pathname ); if ( ncp->nc_fd < 0 ) { mlog( MLOG_NORMAL, "could not open %s: %s\n", ncp->nc_pathname, strerror( errno )); return 0; } } else { ncp->nc_fd = open_trwp( ncp->nc_pathname ); if ( ncp->nc_fd < 0 ) { return 0; } } return ( namreg_t * )ncp; }
/* Make lock file. */ int mklock(u_int locks, const char *path) { char lock[MAXPATHLEN]; int fd; if (!(locks & LOCK_DOTLOCK)) return (0); if (ppath(lock, sizeof lock, "%s.lock", path) != 0) return (-1); fd = xcreate(lock, O_WRONLY, -1, -1, S_IRUSR|S_IWUSR); if (fd == -1) { if (errno == EEXIST) errno = EAGAIN; return (-1); } close(fd); cleanup_register(lock); return (0); }
HashTreeNode *hashtree_new(void) { HashTreeNode *tmp = hashtree_new_node("."); cleanup_register(tmp, (void (*)(void *))hashtree_destroy); return tmp; }