ex_t dbx_recover(bfpath *bfp, bool catastrophic, bool force)
{
    dbe_t *env = xcalloc(1, sizeof(dbe_t));

    /* set exclusive/write lock for recovery */
    while ((force || needs_recovery())
	    && (db_try_glock(bfp, F_WRLCK, F_SETLKW) <= 0))
	rand_sleep(10000,1000000);

    /* ok, when we have the lock, a concurrent process may have
     * proceeded with recovery */
    if (!(force || needs_recovery()))
	return EX_OK;

    if (DEBUG_DATABASE(0))
        fprintf(dbgout, "running %s data base recovery\n",
	    catastrophic ? "catastrophic" : "regular");
    env = dbe_xinit(env, bfp,
		    catastrophic ? DB_RECOVER_FATAL : DB_RECOVER);
    if (env == NULL) {
	exit(EX_ERROR);
    }

    clear_lockfile();
    dbx_cleanup_lite(env);

    return EX_OK;
}
void
remainderSection(int id)
{
  // DO NOT TOUCH THIS FUNCTION
  int isFemale = femaleOnly ? 1 : (id % 2 == 0);
  rand_sleep();
}
Esempio n. 3
0
int     dot_lockfile(const char *path, VSTRING *why)
{
    char   *lock_file;
    int     count;
    struct stat st;
    int     fd;
    int     status = -1;

    lock_file = concatenate(path, ".lock", (char *) 0);

    for (count = 1; /* void */ ; count++) {

	/*
	 * Attempt to create the lock. This code relies on O_EXCL | O_CREAT
	 * to not follow symlinks. With NFS file systems this operation can
	 * at the same time succeed and fail with errno of EEXIST.
	 */
	if ((fd = open(lock_file, O_WRONLY | O_EXCL | O_CREAT, 0)) >= 0) {
	    close(fd);
	    status = 0;
	    break;
	}
	if (count >= var_flock_tries)
	    break;

	/*
	 * We can deal only with "file exists" errors. Any other error means
	 * we better give up trying.
	 */
	if (errno != EEXIST)
	    break;

	/*
	 * Break the lock when it is too old. Give up when we are unable to
	 * remove a stale lock.
	 */
	if (stat(lock_file, &st) == 0)
	    if (time((time_t *) 0) > st.st_ctime + var_flock_stale)
		if (unlink(lock_file) < 0)
		    if (errno != ENOENT)
			break;

	rand_sleep(var_flock_delay * MILLION, var_flock_delay * MILLION / 2);
    }
    if (status && why)
	vstring_sprintf(why, "unable to create lock file %s: %m", lock_file);

    myfree(lock_file);
    return (status);
}
static int dbx_sync(DB_ENV *dbe, int ret)
{
#if DB_AT_LEAST(3,0) && DB_AT_MOST(4,0)
    /* flush dirty pages in buffer pool */
    while (ret == DB_INCOMPLETE) {
	rand_sleep(10000,1000000);
	ret = BF_MEMP_SYNC(dbe, NULL);
    }
#else
    (void)dbe;
    ret = 0;
#endif

    return ret;
}
Esempio n. 5
0
int     main(int argc, char **argv)
{
    int     delay;
    int     variation;

    msg_vstream_init(argv[0], VSTREAM_ERR);
    if (argc != 3)
        msg_fatal("usage: %s delay variation", argv[0]);
    if ((delay = atoi(argv[1])) <= 0)
        msg_fatal("bad delay: %s", argv[1]);
    if ((variation = atoi(argv[2])) < 0)
        msg_fatal("bad variation: %s", argv[2]);
    rand_sleep(delay * 1000000, variation * 1000000);
    exit(0);
}
Esempio n. 6
0
static ex_t get_robx(bfpath *bfp)
{
    double rx;
    int ret = 0;

    init_wordlist("word", bfp->filepath, 0, WL_REGULAR);
    rx = compute_robinson_x();
    if (rx < 0)
	return EX_ERROR;

    if (onlyprint)
	printf("%f\n", rx);
    else {
	dsv_t val;
	word_t *word_robx = word_news(ROBX_W);

	/* since compute_robinson_x() closes the wordlists, 
	   init_wordlist() must be called again */
	init_wordlist("word", bfp->filepath, 0, WL_REGULAR);

	open_wordlists(DS_WRITE);

	val.goodcount = 0;
	val.spamcount = (uint32_t) (rx * 1000000);
	do {
	    ret = ds_write(word_lists->dsh, word_robx, &val);
	    if (ret == DS_ABORT_RETRY) {
		rand_sleep(1000, 1000000);
		begin_wordlist(word_lists);
	    }
	} while (ret == DS_ABORT_RETRY);

	close_wordlists(true);
	free_wordlists();

	word_free(word_robx);
    }

    return ret ? EX_ERROR : EX_OK;
}
Esempio n. 7
0
static int do_discover_conn(struct conn * conn,struct discovery_info * di)
{
	rand_sleep(conf_max_discovery_interval);


	struct wtpinfo *  wtpinfo;
	wtpinfo = get_wtpinfo();
//	wtpinfo_print(wtpinfo);


//	struct timespec tstart,tcur;
	struct radioinfo ri;
	memset(&ri,0,sizeof(ri));

	ri.rmac[0]=6;
	ri.rmac[2]=14;
	ri.rmac[3]=14;
	ri.rmac[4]=14;
	ri.rmac[5]=14;
	ri.rmac[6]=14;
	ri.rmac[7]=14;


#ifdef WITH_CW_LOG_DEBUG
	char str[100];
	sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
	cw_log_debug0("Sending discovery request to %s",str);
#endif

	int rc;

	do {
		rc = cwsend_discovery_request(conn,&ri,wtpinfo);
		if (rc<0){
			if (errno == EINTR)
				continue;
			if (errno == EMSGSIZE){
				conn->mtu-=4;
				cw_log_debug2("Setting mtu to %i",conn->mtu);
				continue;
			}
		}
		break;

	}while (rc<0);
	

	if (rc < 0 )
	{
		char str[100];
		sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
		cw_log(LOG_ERR,"Error sending discovery request to %s: %s",str,strerror(errno));
		return 0;
	}

	struct connlist * connlist;
	connlist = connlist_create(30);

//	clock_gettime(CLOCK_REALTIME,&tstart);	
//
	int tstart = time(0);	
	int treset = 0;
	do {
		char buf[2048];
		int  buflen=2048;
		struct sockaddr_storage sa;
		socklen_t fromlen=sizeof(struct sockaddr_storage);
		rc = recvfrom(conn->sock,buf,buflen,0,(struct sockaddr*)&sa,&fromlen);
		if (rc<0){
			if (errno==EINTR)
				rc=0;
			if (errno==EAGAIN)
				rc=0;
			if (errno==EWOULDBLOCK)
				rc=0;
		}
		if (rc>0) {
#ifdef WITH_CW_LOG_DEBUG
			char str[100];
			sock_addrtostr((struct sockaddr*)&sa,str,100);
			cw_log_debug0("Received packet from %s",str);
#endif

			struct conn * rconn; 
			rconn = connlist_get(connlist,(struct sockaddr*)&sa);

			if (!rconn){
				rconn = conn_create_noq(conn->sock,(struct sockaddr*)&sa); //msg_cb,NULL,0);
	//			rconn->pmsgarg=conn->pmsgarg;
				rconn->mtu = conn->mtu;
				rconn->seqnum=conn->seqnum;
				connlist_add(connlist,rconn);
			}

			conn_process_packet(rconn,(uint8_t*)buf,rc,msg_cb,di);
		}

		/* reset discovery timer after we have received the first response */
		if ( di->response_count == 1 && !treset ){
			tstart=time(0);
			treset=1;
		}

		//clock_gettime(CLOCK_REALTIME,&tcur);
//		printf("TTSub: %i %i\n",time(0)-tstart, conf_discovery_interval);
	}while(time(0)-tstart < conf_discovery_interval && rc>=0 );			


	if (rc <0){
		char str[100];
		sock_addrtostr((struct sockaddr*)&conn->addr,str,100);
		cw_log(LOG_ERR,"Error sendings discovery request to %s: %s",str,strerror(errno));
	}

	connlist_destroy(connlist);
	return 1;
}