예제 #1
0
static void drcomd_daemon(struct drcom_handle *h)
{
	int s;
	int r;

	s = init_daemon_socket();
	if(s < 0)
		exit(-1);

	if(setup_sig_handlers()<0){
		logerr("sig handlers not setup, exit.\n");
		exit(1);
	}

	loginfo("drcomd %s started.\n", DRCOM_VERSION);

	while (1) {
		int maxfd;
		fd_set readfds;

		FD_ZERO(&readfds);
		FD_SET(s, &readfds);
		FD_SET(sigusr1_pipe[READ_END], &readfds);
		
		maxfd = s;
		if(maxfd < sigusr1_pipe[READ_END])
			maxfd = sigusr1_pipe[READ_END];

		unblock_sigusr1();
		r = select(maxfd+1, &readfds, NULL,NULL, NULL);
		if(r<0){
			if(errno != EINTR)
				logerr("signal caught\n");
			continue;
		}
		if(FD_ISSET(sigusr1_pipe[READ_END], &readfds)){
			char buf[256];
			int *sig = (int*)buf;

			read(sigusr1_pipe[READ_END], &buf, sizeof(buf));
			do_signals(h, *sig);
		}
		if(!FD_ISSET(s, &readfds))
			continue;

		block_sigusr1();
		do_one_client(s, h);
	}

	/* FIXME: 
	 * drcom_clean_up();
	 * drcom_destroy_handle();
	 * close_daemon_socket(); 
	 */
}
예제 #2
0
파일: replika.c 프로젝트: buytenh/replika
int main(int argc, char *argv[])
{
	static struct option long_options[] = {
		{ "block-size", required_argument, 0, 'b' },
		{ "hash-algo", required_argument, 0, 'h' },
		{ "hash-algorithm", required_argument, 0, 'h' },
		{ "max-iter", required_argument, 0, 'i' },
		{ "freeze", required_argument, 0, 'f' },
		{ "loop", no_argument, 0, 'l' },
		{ 0, 0, 0, 0 },
	};
	int i;
	int freeze_count = 0;

	gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);

	if (!gcry_check_version(GCRYPT_VERSION)) {
		fprintf(stderr, "libgcrypt version mismatch\n");
		return 1;
	}

	while (1) {
		int c;

		c = getopt_long(argc, argv, "b:h:i:f:l", long_options, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'b':
			if (sscanf(optarg, "%i", &block_size) != 1) {
				fprintf(stderr, "cannot parse block size: "
						"%s\n", optarg);
				return 1;
			}

			if ((block_size & 7) != 0) {
				fprintf(stderr, "error: block size must be "
						"a multiple of 8\n");
				return 1;
			}

			break;

		case 'h':
			hash_algo = gcry_md_map_name(optarg);
			if (hash_algo == 0) {
				fprintf(stderr, "unknown hash algorithm "
						"name: %s\n", optarg);
				return 1;
			}

			break;

		case 'i':
			if (sscanf(optarg, "%i", &max_iterations) != 1) {
				fprintf(stderr, "cannot parse iteration "
						"count: %s\n", optarg);
				return 1;
			}

			break;

		case 'f':
			if (freeze_count == MAX_FREEZE) {
				fprintf(stderr, "too many --freeze options\n");
				return 1;
			}

			freeze_fs[freeze_count++] = optarg;

			break;

		case 'l':
			loop = 1;
			break;

		case '?':
			return 1;

		default:
			abort();
		}
	}

	if (optind + 4 != argc) {
		fprintf(stderr, "%s: [opts] <src> <srchashmap> <dst> "
				"<dsthashmap>\n", argv[0]);
		fprintf(stderr, " -b, --block-size=SIZE    hash block size\n");
		fprintf(stderr, " -f, --freeze=MOUNTPOINT  freeze "
				"filesystem\n");
		fprintf(stderr, " -h, --hash-algo=ALGO     hash algorithm\n");
		fprintf(stderr, " -i, --max-iter=ITER      maximum number of "
				"iterations\n");
		fprintf(stderr, " -l, --loop               create consistent "
				"image copy\n");
		return 1;
	}

	hash_size = gcry_md_get_algo_dlen(hash_algo);

	fd_src = open(argv[optind], O_RDONLY);
	if (fd_src < 0) {
		perror("opening src");
		return 1;
	}

	sizeblocks = lseek(fd_src, 0, SEEK_END);
	if (sizeblocks < 0) {
		perror("lseek");
		return 1;
	}
	sizeblocks = (sizeblocks + block_size - 1) / block_size;

	if (strcmp(argv[optind + 1], "-")) {
		int fd_srchashmap;

		if (loop) {
			fprintf(stderr, "consistent image copy requested, "
					"but source hashmap specified\n");
			return 1;
		}

		fd_srchashmap = open(argv[optind + 1], O_RDONLY);
		if (fd_srchashmap < 0) {
			perror("opening srchashmap");
			return 1;
		}

		srchashmap = malloc(sizeblocks * hash_size);
		if (srchashmap == NULL) {
			fprintf(stderr, "out of memory allocating hash map\n");
			return 1;
		}

		if (read(fd_srchashmap, srchashmap, sizeblocks * hash_size)
		    != sizeblocks * hash_size) {
			fprintf(stderr, "error reading hash map\n");
			return 1;
		}

		close(fd_srchashmap);
	} else {
		srchashmap = NULL;
	}

	fd_dst = open(argv[optind + 2], O_CREAT | O_RDWR, 0666);
	if (fd_dst < 0) {
		perror("opening dst");
		return 1;
	}

	fd_dsthashmap = open(argv[optind + 3], O_CREAT | O_RDWR, 0666);
	if (fd_dsthashmap < 0) {
		perror("opening dsthashmap");
		return 1;
	}

	dsthashmap = malloc(sizeblocks * hash_size);
	if (dsthashmap == NULL) {
		fprintf(stderr, "out of memory allocating hash map\n");
		return 1;
	}

	if (read(fd_dsthashmap, dsthashmap, sizeblocks * hash_size)
	    != sizeblocks * hash_size) {
		fprintf(stderr, "error reading hash map\n");
		return 1;
	}

	setup_sig_handlers();

	if (freeze_count) {
		for (i = 0; i < MAX_FREEZE; i++)
			freeze_fd[i] = -1;

		atexit(thaw_fs);

		fprintf(stderr, "freezing filesystems\n");

		for (i = 0; i < freeze_count; i++) {
			int fd;

			fd = open(freeze_fs[i], O_RDONLY);
			if (fd < 0) {
				fprintf(stderr, "error opening freeze mount "
						"point %s (%s)\n", freeze_fs[i],
						strerror(errno));
				return 1;
			}

			if (ioctl(fd, FIFREEZE, 0) < 0) {
				fprintf(stderr, "error freezing fs %s (%s)\n",
						freeze_fs[i], strerror(errno));
				return 1;
			}

			freeze_fd[i] = fd;
		}
	}

	if (loop) {
		for (i = 0; i < max_iterations; i++) {
			fd_off = 0;
			again = 0;

			fprintf(stderr, "scanning for differences... ");
			run_threads(copy_thread_no_hashmap);
			if (!signal_quit_flag) {
				fprintf(stderr, "done                      "
						"                          \n");
			}

			if (!again)
				break;

			if (i == max_iterations - 1) {
				fprintf(stderr, "maximum iteration count "
						"reached, bailing out\n");
			} else {
				fprintf(stderr, "repeating scanning due to "
						"differences\n");
			}
		}
	} else {
		fd_off = 0;

		if (srchashmap == NULL) {
			fprintf(stderr, "scanning for differences... ");
			run_threads(copy_thread_no_hashmap);
		} else {
			off_t off;

			mismatch_idx = 0;

			mismatch_cnt = 0;
			for (off = 0; off < sizeblocks; off++) {
				if (memcmp(srchashmap + off * hash_size,
					   dsthashmap + off * hash_size,
					   hash_size)) {
					mismatch_cnt++;
				}
			}

			fprintf(stderr, "copying differences... ");
			run_threads(copy_thread_hashmap);
		}
		if (!signal_quit_flag) {
			fprintf(stderr, "done                      "
					"                          \n");
		}
	}

	fprintf(stderr, "flushing buffers... ");
	close(fd_src);
	close(fd_dst);
	close(fd_dsthashmap);
	fprintf(stderr, "done\n");

	return signal_quit_flag ? EXIT_FAILURE : EXIT_SUCCESS;
}
예제 #3
0
int main(int argc, char **argv)
{
	catcierge_args_t *args = &grb.args;

	fprintf(stderr, "\nCatcierge Grabber v" CATCIERGE_VERSION_STR " (" CATCIERGE_GIT_HASH_SHORT "");

	// Was this built with changes in the git sources?
	#ifdef CATCIERGE_GIT_TAINTED
	fprintf(stderr, "-tainted");
	#endif

	fprintf(stderr, ")\n(C) Joakim Soderberg 2013-2016\n\n");

	fprintf(stderr, "Library versions:\n");
	fprintf(stderr, " OpenCV v%d.%d.%d\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
	#ifdef WITH_ZMQ
	fprintf(stderr, "   CZMQ v%d.%d.%d\n", CZMQ_VERSION_MAJOR, CZMQ_VERSION_MINOR, CZMQ_VERSION_PATCH);
	#endif
	fprintf(stderr, "\n");

	// TODO: Enable specifying pid path on command line.
	#ifndef _WIN32
	pid_fd = create_pid_file(argv[0], PID_PATH, FD_CLOEXEC);
	#endif

	if (catcierge_grabber_init(&grb))
	{
		fprintf(stderr, "Failed to init\n");
		return -1;
	}

	if (catcierge_args_init(args, argv[0]))
	{
		fprintf(stderr, "Failed to init args\n");
		return -1;
	}

	if (catcierge_args_parse(args, argc, argv))
	{
		return -1;
	}

	if (args->nocolor)
	{
		catcierge_nocolor = 1;
	}

	catcierge_print_settings(args);

	setup_sig_handlers();

	if (args->log_path)
	{
		if (!(grb.log_file = fopen(args->log_path, "a+")))
		{
			CATERR("Failed to open log file \"%s\"\n", args->log_path);
		}
	}

	#ifdef RPI
	if (catcierge_setup_gpio(&grb))
	{
		CATERR("Failed to setup GPIO pins\n");
		return -1;
	}

	CATLOG("Initialized GPIO pins\n");
	#endif // RPI

	assert((args->matcher_type == MATCHER_TEMPLATE)
		|| (args->matcher_type == MATCHER_HAAR));

	if (catcierge_matcher_init(&grb.matcher, catcierge_get_matcher_args(args)))
	{
		CATERR("Failed to init %s matcher\n", grb.matcher->name);
		return -1;
	}

	CATLOG("Initialized catcierge image recognition\n");

	if (catcierge_output_init(&grb, &grb.output))
	{
		CATERR("Failed to init output template system\n");
		return -1;
	}

	if (catcierge_output_load_templates(&grb.output,
			args->inputs, args->input_count))
	{
		CATERR("Failed to load output templates\n");
		return -1;
	}

	CATLOG("Initialized output templates\n");

	#ifdef WITH_RFID
	catcierge_init_rfid_readers(&grb);
	#endif

	catcierge_setup_camera(&grb);

	#ifdef WITH_ZMQ
	catcierge_zmq_init(&grb);
	#endif

	CATLOG("Starting detection!\n");
	// TODO: Create a catcierge_grb_start(grb) function that does this instead.
	catcierge_fsm_start(&grb);

	// Run the program state machine.
	do
	{
		if (!catcierge_timer_isactive(&grb.frame_timer))
		{
			catcierge_timer_start(&grb.frame_timer);
		}

		// Always feed the RFID readers.
		#ifdef WITH_RFID
		if ((args->rfid_inner_path || args->rfid_outer_path) 
			&& catcierge_rfid_ctx_service(&grb.rfid_ctx))
		{
			CATERRFPS("Failed to service RFID readers\n");
		}
		#endif // WITH_RFID

		grb.img = catcierge_get_frame(&grb);

		catcierge_run_state(&grb);
		catcierge_print_spinner(&grb);
	} while (
		grb.running
		#ifdef WITH_ZMQ
		&& !zctx_interrupted
		#endif
		);

	catcierge_matcher_destroy(&grb.matcher);
	catcierge_output_destroy(&grb.output);
	catcierge_destroy_camera(&grb);
	#ifdef WITH_ZMQ
	catcierge_zmq_destroy(&grb);
	#endif
	catcierge_grabber_destroy(&grb);
	catcierge_args_destroy(&grb.args);

	if (grb.log_file)
	{
		fclose(grb.log_file);
	}

	#ifndef _WIN32
	if (pid_fd > 0)
	{
		close(pid_fd);
		unlink(PID_PATH);
	}
	#endif

	return 0;
}