/* * Purpose: Test whether a specified lock would be granted if requested * Returns: nlm_granted (or error code) * Notes: */ nlm_testres * nlm_test_1_svc(nlm_testargs *arg, struct svc_req *rqstp) { static nlm_testres res; struct nlm4_lock arg4; struct nlm4_holder *holder; nlmtonlm4(&arg->alock, &arg4); if (debug_level) log_from_addr("nlm_test", rqstp); holder = testlock(&arg4, 0); /* * Copy the cookie from the argument into the result. Note that this * is slightly hazardous, as the structure contains a pointer to a * malloc()ed buffer that will get freed by the caller. However, the * main function transmits the result before freeing the argument * so it is in fact safe. */ res.cookie = arg->cookie; if (holder == NULL) { res.stat.stat = nlm_granted; } else { res.stat.stat = nlm_denied; memcpy(&res.stat.nlm_testrply_u.holder, holder, sizeof(struct nlm_holder)); res.stat.nlm_testrply_u.holder.l_offset = holder->l_offset; res.stat.nlm_testrply_u.holder.l_len = holder->l_len; } return (&res); }
void * nlm_test_msg_1_svc(nlm_testargs *arg, struct svc_req *rqstp) { nlm_testres result; static char dummy; struct sockaddr_in *addr; CLIENT *cli; int success; struct timeval timeo; struct nlm4_lock arg4; struct nlm4_holder *holder; nlmtonlm4(&arg->alock, &arg4); if (debug_level) log_from_addr("nlm_test_msg", rqstp); holder = testlock(&arg4, 0); result.cookie = arg->cookie; if (holder == NULL) { result.stat.stat = nlm_granted; } else { result.stat.stat = nlm_denied; memcpy(&result.stat.nlm_testrply_u.holder, holder, sizeof(struct nlm_holder)); result.stat.nlm_testrply_u.holder.l_offset = (unsigned int)holder->l_offset; result.stat.nlm_testrply_u.holder.l_len = (unsigned int)holder->l_len; } /* * nlm_test has different result type to the other operations, so * can't use transmit_result() in this case */ addr = svc_getcaller(rqstp->rq_xprt); if ((cli = get_client(addr, NLM_VERS)) != NULL) { /* No timeout - not expecting response */ timerclear(&timeo); success = clnt_call(cli, NLM_TEST_RES, xdr_nlm_testres, &result, xdr_void, &dummy, timeo); if (debug_level > 2) syslog(LOG_DEBUG, "clnt_call returns %d", success); } return NULL; }
bool checkFilelock(const char* lockfile) { ifstream testlock(lockfile); if(testlock){ #ifdef DEBUG printf("Filelock \"%s\" already exist! \n", lockfile); #endif return false; }else{ #ifdef DEBUG printf("File lock \"%s\" check ok \n", lockfile); #endif return true; } }
int main() { int fd1, fd2; pid_t pid; fd1 = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE); if (fd1 < 0) { printf("can't open %s.\n", LOCKFILE); exit(1); } printf("parent pid: %d.\n", getpid()); getlock(fd1); getlock2(fd1); if ((pid = fork()) < 0) { printf("fork error.\n"); exit(1); } else if (pid == 0) { printf("child pid: %d.\n", getpid()); fd2 = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE); if (fd2 < 0) { printf("can't open %s.\n", LOCKFILE); exit(1); } testlock(fd1); getlock2(fd2); getlock2(fd1); } else { while(1) sleep(10); } return 0; }
/*! * Test for a share mode lock * * @param ad (rw) handle * @param eid (r) datafork or ressource fork * @param off (r) sharemode lock to test * * @returns 1 if there's an existing lock, 0 if there's no lock, * -1 in case any error occured */ int ad_testlock(struct adouble *ad, int eid, const off_t off) { int ret = 0; off_t lock_offset; LOG(log_debug, logtype_default, "ad_testlock(%s, off: %jd (%s): BEGIN", eid == ADEID_DFORK ? "data" : "reso", (intmax_t)off, shmdstrfromoff(off)); if (eid == ADEID_DFORK) { lock_offset = off; } else { /* rfork */ lock_offset = rf2off(off); } ret = testlock(&ad->ad_data_fork, lock_offset, 1); LOG(log_debug, logtype_default, "ad_testlock: END: %d", ret); return ret; }
/* * Simple lock test */ int main( int ac, char *av[] ) { register int i; register int index; if (ac > 2) { numThreads = atoi(av[2]); if (ac > 3) loopCount = atoi(av[3]); } for (i = 0; i < 3; i++) { std::cout << "\n"; for (index = 0; index < (int)(sizeof(testinfo)/sizeof(testinfo[0])); index++) { fprintf(stderr, "%d: %s\n", i, testinfo[index].pName); testlock(numThreads, index); } } std::cout << "Bye.\n"; }