Exemplo n.º 1
0
Arquivo: ind.c Projeto: dustin/ferret
void index_flush(Index *self)
{
    if (self->ir) {
        ir_commit(self->ir);
    } else if (self->iw) {
        iw_close(self->iw);
        self->iw = NULL;
    }
    self->has_writes = false;
}
Exemplo n.º 2
0
Arquivo: ind.c Projeto: dustin/ferret
void index_destroy(Index *self)
{
    mutex_destroy(&self->mutex);
    INDEX_CLOSE_READER(self);
    if (self->iw) iw_close(self->iw);
    store_deref(self->store);
    a_deref(self->analyzer);
    if (self->qp) qp_destroy(self->qp);
    if (self->key) hs_destroy(self->key);
    free(self);
}
Exemplo n.º 3
0
static void sort_multi_test_setup(Store *store1, Store *store2)
{
    int i;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_YES);
    IndexWriter *iw;
    SortTestData data[] = {            /* len mod */
        {"findall","a","6","0.01"},    /*  4   0  */
        {"findall","c","5","0.1"},     /*  3   3  */
        {"findall","e","2","0.001"},   /*  5   1  */
        {"findall","g","1","1.0"},     /*  3   3  */
        {"findall","i","3","0.0001"},  /*  6   2  */
        {"findall","", "4","10.0"},    /*  4   0  */
        {"findall","h","5","0.00001"}, /*  7   3  */
        {"findall","f","2","100.0"},   /*  5   1  */
        {"findall","d","3","1000.0"},  /*  6   2  */
        {"findall","b","4","0.000001"} /*  8   0  */
    };

    index_create(store1, fis);
    index_create(store2, fis);
    fis_deref(fis);

    iw = iw_open(store1, whitespace_analyzer_new(false), NULL);

    for (i = 0; i < NELEMS(data)/2; i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);

    iw = iw_open(store2, whitespace_analyzer_new(false), NULL);

    for (i = NELEMS(data)/2; i < NELEMS(data); i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);
}
Exemplo n.º 4
0
static void sort_test_setup(Store *store)
{
    int i;
    IndexWriter *iw;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_YES);
    index_create(store, fis);
    fis_deref(fis);

    iw = iw_open(store, whitespace_analyzer_new(false), NULL);

    for (i = 0; i < NELEMS(data); i++) {
        add_sort_test_data(&data[i], iw);
    }
    iw_close(iw);
}
Exemplo n.º 5
0
static void test_problem_text(TestCase *tc, void *data)
{
    Store *store = (Store *)data;
    IndexWriter *iw = create_iw(store);
    Document *problem_text = prep_doc();

    iw_add_doc(iw, problem_text);
    Aiequal(1, iw_doc_count(iw));
    Assert(!store->exists(store, "_0.cfs"),
           "data shouldn't have been written yet");
    iw_commit(iw);
    Assert(store->exists(store, "_0.cfs"), "data should now be written");
    iw_close(iw);
    Assert(store->exists(store, "_0.cfs"), "data should still be there");
}
Exemplo n.º 6
0
Arquivo: ind.c Projeto: dustin/ferret
INLINE void ensure_reader_open(Index *self)
{
    if (self->ir) {
        if (self->check_latest && !ir_is_latest(self->ir)) {
            INDEX_CLOSE_READER(self);
            self->ir = ir_open(self->store);
        }
        return;
    }
    if (self->iw) {
        iw_close(self->iw);
        self->iw = NULL;
    }
    self->ir = ir_open(self->store);
}
Exemplo n.º 7
0
void prepare_filter_index(Store *store)
{
    int i;
    IndexWriter *iw;
    FieldInfos *fis = fis_new(STORE_YES, INDEX_YES, TERM_VECTOR_NO);

    num      = intern("num");
    date     = intern("date");
    flipflop = intern("flipflop");

    struct FilterData data[FILTER_DOCS_SIZE] = {
        {"0", "20040601", "on"},
        {"1", "20041001", "off"},
        {"2", "20051101", "on"},
        {"3", "20041201", "off"},
        {"4", "20051101", "on"},
        {"5", "20041201", "off"},
        {"6", "20050101", "on"},
        {"7", "20040701", "off"},
        {"8", "20050301", "on"},
        {"9", "20050401", "off"}
    };

    index_create(store, fis);
    fis_deref(fis);

    iw = iw_open(store, whitespace_analyzer_new(false), NULL);
    for (i = 0; i < FILTER_DOCS_SIZE; i++) {
        Document *doc = doc_new();
        doc->boost = (float)(i+1);
        doc_add_field(doc, df_add_data(df_new(num), data[i].num));
        doc_add_field(doc, df_add_data(df_new(date), data[i].date));
        doc_add_field(doc, df_add_data(df_new(flipflop), data[i].flipflop));
        iw_add_doc(iw, doc);
        doc_destroy(doc);
    }
    iw_close(iw);
    return;
}
Exemplo n.º 8
0
int main(int argc, char *argv[]) {
	struct ap_list apl;
	struct ap_info api;
	struct iw_dev dev;
	struct pollfd pfd[2];
	struct deauth_thread_args ta;
	struct timeval tv1, tv2;
	suseconds_t msec;
	pthread_t deauth_thread;
	pthread_mutex_t chan_mutex, list_mutex, cnc_mutex;
	pthread_cond_t chan_cond;
	channelset_t chans;
	int ret, sigfd, c, n, chan;
	char *ifname, *chans_str;
	sigset_t exit_sig;
	time_t tm;

	if (argc < 2) {
		print_usage(stderr);
		return EXIT_FAILURE;
	}

	/* arguments */
	ifname = argv[argc-1];
	chans_str = NULL;

	while((c = getopt(argc, argv, "c:lh")) != -1) {
		switch (c) {
		case 'c':
			chans_str = optarg;
			break;
		case 'l':
			return print_interfaces();
		case 'h':
			print_usage(stdout);
			return EXIT_SUCCESS;
		case '?':
		default:
			return EXIT_FAILURE;
		}
	}

	if (argv[optind] != ifname) {
		print_usage(stderr);
		return EXIT_FAILURE;
	}

	if (getuid()) {
		fprintf(stderr, "Not root?\n");
		return EXIT_FAILURE;
	}

	/* init channel set */
	if (chans_str == NULL) {
		channel_zero(&chans);
		for (n=1; n<=14; n++)
			channel_set(&chans, n);
	} else {
		if (parse_chans_str(chans_str, &chans) == -1) {
			fprintf(stderr, "Can not parse the channels\n");
			return EXIT_FAILURE;
		}
	}

	/* init access point list */
	init_ap_list(&apl);

	/* init signals */
	sigemptyset(&exit_sig);
	sigaddset(&exit_sig, SIGINT);
	sigaddset(&exit_sig, SIGTERM);

	if (sigprocmask(SIG_BLOCK, &exit_sig, NULL) < 0) {
		err_msg("sigprocmask");
		return EXIT_FAILURE;
	}

	sigfd = signalfd(-1, &exit_sig, 0);
	if (sigfd < 0) {
		err_msg("signalfd");
		return EXIT_FAILURE;
	}

	pfd[0].fd = sigfd;
	pfd[0].revents = 0;
	pfd[0].events = POLLIN;

	/* init device */
	iw_init_dev(&dev);
	strncpy(dev.ifname, ifname, sizeof(dev.ifname)-1);

	if (iw_open(&dev) < 0) {
		print_error();
		goto _errout_no_thread;
	}

	pfd[1].fd = dev.fd_in;
	pfd[1].revents = 0;
	pfd[1].events = POLLIN;

	/* set channel */
	n = 0;
	chan = 0;
	do {
		chan = (chan % CHANNEL_MAX) + 1;
		if (channel_isset(&chans, chan))
			ret = iw_set_channel(&dev, chan);
		else
			ret = -1;
		/* if fails try next channel */
	} while(++n < CHANNEL_MAX && ret < 0);
	if (ret < 0) {
		print_error();
		goto _errout_no_thread;
	}

	/* start deauth thread */
	ta.stop = 0;
	ta.apl = &apl;
	ta.dev = &dev;
	pthread_mutex_init(&chan_mutex, NULL);
	ta.chan_mutex = &chan_mutex;
	pthread_cond_init(&chan_cond, NULL);
	ta.chan_cond = &chan_cond;
	pthread_mutex_init(&list_mutex, NULL);
	ta.list_mutex = &list_mutex;
	ta.chan_changed = 1;
	pthread_mutex_init(&cnc_mutex, NULL);
	ta.cnc_mutex = &cnc_mutex;
	ta.chan_need_change = 0;

	if (pthread_create(&deauth_thread, NULL, deauth_thread_func, &ta) < 0) {
		err_msg("pthread_create");
		goto _errout_no_thread;
	}

	clear_scr();
	update_scr(&apl, &dev);
	tm = time(NULL);
	gettimeofday(&tv1, NULL);

	while (!ta.stop) {
		if (poll(pfd, 2, 0) < 0) {
			err_msg("poll");
			goto _errout;
		}

		if (pfd[0].revents & POLLIN) /* got SIGTERM or SIGINT */
			break;

		if (pfd[1].revents & POLLIN) {
			ret = read_ap_info(&dev, &api);
			if (ret < 0 && ret != ERRNODATA) { /* error */
				print_error();
				goto _errout;
			} else if (ret == 0 && channel_isset(&chans, api.chan)) { /* got infos */
				pthread_mutex_lock(&list_mutex);
				if (add_or_update_ap(&apl, &api) < 0) {
					pthread_mutex_unlock(&list_mutex);
					print_error();
					goto _errout;
				}
				pthread_mutex_unlock(&list_mutex);
			}
		}

		gettimeofday(&tv2, NULL);
		if (tv2.tv_usec > tv1.tv_usec)
			msec = tv2.tv_usec - tv1.tv_usec;
		else
			msec = tv1.tv_usec - tv2.tv_usec;

		/* update screen every 0.5 second */
		if (msec >= 500000) {
			pthread_mutex_lock(&list_mutex);
			update_scr(&apl, &dev);
			pthread_mutex_unlock(&list_mutex);
			gettimeofday(&tv1, NULL);
		}

		/* change channel at least every 1 second */
		if (time(NULL) - tm >= 1) {
			if (!ta.chan_changed) {
				pthread_mutex_lock(&cnc_mutex);
				ta.chan_need_change = 0;
				pthread_mutex_unlock(&cnc_mutex);
				pthread_mutex_lock(&chan_mutex);
				n = 0;
				do {
					chan = (chan % CHANNEL_MAX) + 1;
					if (channel_isset(&chans, chan))
						ret = iw_set_channel(&dev, chan);
					else
						ret = -1;
					/* if fails try next channel */
				} while(++n < CHANNEL_MAX && ret < 0);
				/* if all channels failed */
				if (ret < 0) {
					print_error();
					goto _errout;
				}
				tm = time(NULL);
				ta.chan_changed = 1;
				pthread_cond_signal(&chan_cond);
				pthread_mutex_unlock(&chan_mutex);
			} else {
				pthread_mutex_lock(&cnc_mutex);
				ta.chan_need_change = 1;
				pthread_mutex_unlock(&cnc_mutex);
			}
		}
	}

	/* we got an error from deauth thread */
	if (ta.stop == 2)
		goto _errout;

	printf("\nExiting..\n");
	ta.stop = 1;
	pthread_mutex_unlock(&list_mutex);
	pthread_cond_broadcast(&chan_cond);
	pthread_mutex_unlock(&chan_mutex);
	pthread_join(deauth_thread, NULL);
	iw_close(&dev);
	free_ap_list(&apl);
	return EXIT_SUCCESS;
_errout:
	ta.stop = 1;
	pthread_mutex_unlock(&list_mutex);
	pthread_cond_broadcast(&chan_cond);
	pthread_mutex_unlock(&chan_mutex);
	pthread_join(deauth_thread, NULL);
_errout_no_thread:
	iw_close(&dev);
	free_ap_list(&apl);
	return EXIT_FAILURE;
}