static void do_read(void) { apr_file_t *file; apr_status_t status; if (apr_file_open(&file, testfile, APR_WRITE, APR_OS_DEFAULT, pool) != APR_SUCCESS) errmsg("Could not open test file.\n"); printf("Test file opened.\n"); status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK); if (!APR_STATUS_IS_EAGAIN(status)) { char msg[200]; errmsg(apr_psprintf(pool, "Expected APR_EAGAIN. Got %d: %s.\n", status, apr_strerror(status, msg, sizeof(msg)))); } printf("First attempt: we were properly locked out.\nWaiting for lock..."); fflush(stdout); if (apr_file_lock(file, APR_FLOCK_EXCLUSIVE) != APR_SUCCESS) errmsg("Could not establish lock on test file."); printf(" got it.\n"); (void) apr_file_close(file); printf("Exiting.\n"); }
///////////////////////////////////////////////////////////////////////////////// // // Function: // // Purpose: // // Parameters: // // Return value: // // Author: Komatsu Yuji(Zheng Chuyu) // ///////////////////////////////////////////////////////////////////////////////// void jhklog_lock(void) { if (jx_log == NULL) return; if (jx_log->fp != NULL) apr_file_lock(jx_log->fp, APR_FLOCK_EXCLUSIVE); }
/* * write JSON metadata to a file */ static apr_byte_t oidc_metadata_file_write(request_rec *r, const char *path, const char *data) { // TODO: completely erase the contents of the file if it already exists.... apr_file_t *fd = NULL; apr_status_t rc = APR_SUCCESS; apr_size_t bytes_written = 0; char s_err[128]; /* try to open the metadata file for writing, creating it if it does not exist */ if ((rc = apr_file_open(&fd, path, (APR_FOPEN_WRITE | APR_FOPEN_CREATE), APR_OS_DEFAULT, r->pool)) != APR_SUCCESS) { oidc_error(r, "file \"%s\" could not be opened (%s)", path, apr_strerror(rc, s_err, sizeof(s_err))); return FALSE; } /* lock the file and move the write pointer to the start of it */ apr_file_lock(fd, APR_FLOCK_EXCLUSIVE); apr_off_t begin = 0; apr_file_seek(fd, APR_SET, &begin); /* calculate the length of the data, which is a string length */ apr_size_t len = strlen(data); /* (blocking) write the number of bytes in the buffer */ rc = apr_file_write_full(fd, data, len, &bytes_written); /* check for a system error */ if (rc != APR_SUCCESS) { oidc_error(r, "could not write to: \"%s\" (%s)", path, apr_strerror(rc, s_err, sizeof(s_err))); return FALSE; } /* check that all bytes from the header were written */ if (bytes_written != len) { oidc_error(r, "could not write enough bytes to: \"%s\", bytes_written (%" APR_SIZE_T_FMT ") != len (%" APR_SIZE_T_FMT ")", path, bytes_written, len); return FALSE; } /* unlock and close the written file */ apr_file_unlock(fd); apr_file_close(fd); oidc_debug(r, "file \"%s\" written; number of bytes (%" APR_SIZE_T_FMT ")", path, len); return TRUE; }
/* locks a filehandle */ MVMint64 MVM_file_lock(MVMThreadContext *tc, MVMObject *oshandle, MVMint64 flag) { apr_status_t rv; MVMOSHandle *handle; int locktype = (int)flag; verify_filehandle_type(tc, oshandle, &handle, "lock filehandle"); if ((rv = apr_file_lock(handle->body.file_handle, locktype)) != APR_SUCCESS) { /* XXX this really should check what type of error was returned */ if (locktype & APR_FLOCK_NONBLOCK) return 0; MVM_exception_throw_apr_error(tc, rv, "Failed to lock filehandle: "); } return 1; }
/* NOTE: this function blocks until it acquires the lock */ APU_DECLARE(apr_status_t) apr_sdbm_lock(apr_sdbm_t *db, int type) { apr_status_t status; if (!(type == APR_FLOCK_SHARED || type == APR_FLOCK_EXCLUSIVE)) return APR_EINVAL; if (db->flags & SDBM_EXCLUSIVE_LOCK) { ++db->lckcnt; return APR_SUCCESS; } else if (db->flags & SDBM_SHARED_LOCK) { /* * Cannot promote a shared lock to an exlusive lock * in a cross-platform compatibile manner. */ if (type == APR_FLOCK_EXCLUSIVE) return APR_EINVAL; ++db->lckcnt; return APR_SUCCESS; } /* * zero size: either a fresh database, or one with a single, * unsplit data page: dirpage is all zeros. */ if ((status = apr_file_lock(db->dirf, type)) == APR_SUCCESS) { apr_finfo_t finfo; if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf)) != APR_SUCCESS) { (void) apr_file_unlock(db->dirf); return status; } SDBM_INVALIDATE_CACHE(db, finfo); ++db->lckcnt; if (type == APR_FLOCK_SHARED) db->flags |= SDBM_SHARED_LOCK; else if (type == APR_FLOCK_EXCLUSIVE) db->flags |= SDBM_EXCLUSIVE_LOCK; } return status; }
static void do_write(void) { apr_file_t *file; apr_status_t rv; if (apr_file_open(&file, testfile, APR_WRITE|APR_CREATE, APR_OS_DEFAULT, pool) != APR_SUCCESS) errmsg("Could not create file.\n"); printf("Test file created.\n"); if ((rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS) errmsg2("Could not lock the file", rv); printf("Lock created.\nSleeping..."); fflush(stdout); apr_sleep(apr_time_from_sec(30)); (void) apr_file_close(file); printf(" done.\nExiting.\n"); }
static void test_withlock(abts_case *tc, void *data) { apr_file_t *file; apr_status_t rv; int code; rv = apr_file_open(&file, TESTFILE, APR_WRITE|APR_CREATE, APR_OS_DEFAULT, p); APR_ASSERT_SUCCESS(tc, "Could not create file.", rv); ABTS_PTR_NOTNULL(tc, file); rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE); APR_ASSERT_SUCCESS(tc, "Could not lock the file.", rv); ABTS_PTR_NOTNULL(tc, file); code = launch_reader(tc); ABTS_INT_EQUAL(tc, FAILED_READ, code); (void) apr_file_close(file); }
static int file_lock(lua_State *L) { const char *options[] = { "shared", "exclusive", NULL }; const int flags[] = { APR_FLOCK_SHARED, APR_FLOCK_EXCLUSIVE }; apr_status_t status; lua_apr_file *file; int type; file = file_check(L, 1, 1); type = flags[luaL_checkoption(L, 2, NULL, options)]; if (!lua_isnoneornil(L, 3)) { luaL_checktype(L, 3, LUA_TSTRING); if (strcmp(lua_tostring(L, 3), "non-blocking") != 0) luaL_argerror(L, 3, "invalid option"); type |= APR_FLOCK_NONBLOCK; } status = apr_file_lock(file->handle, type); return push_file_status(L, file, status); }
int main(int argc, const char * const *argv) { apr_file_t *file; apr_status_t status; apr_pool_t *p; apr_initialize(); apr_pool_create(&p, NULL); if (apr_file_open(&file, TESTFILE, APR_WRITE, APR_OS_DEFAULT, p) != APR_SUCCESS) { exit(UNEXPECTED_ERROR); } status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK); if (status == APR_SUCCESS) { exit(SUCCESSFUL_READ); } if (APR_STATUS_IS_EAGAIN(status)) { exit(FAILED_READ); } exit(UNEXPECTED_ERROR); }
SWITCH_DECLARE(switch_status_t) switch_file_lock(switch_file_t *thefile, int type) { return apr_file_lock(thefile, type); }