Example #1
0
int main (int argc, char **argv)
{
	int i, devin, devout;
	int width;
	int height;
	char *image_out, *image_new;
	char palette[10] = {'\0'};
	
	if (argc != 5) {
		printf("Usage:\n\n");
		printf("invert input output widthxheight rgb24|yuv420p\n\n");
		printf("example: invert /dev/video0 /dev/video1 352x288 yuv420p\n\n");
		exit(1);
	}
	sscanf(argv[3], "%dx%d", &width, &height);
	sscanf(argv[4], "%s", palette);

	if (!strcmp(palette,"rgb24")) 
		fmt = VIDEO_PALETTE_RGB24;
	else if (!strcmp(palette,"yuv420p")) 
		fmt = VIDEO_PALETTE_YUV420P;
	else fmt = VIDEO_PALETTE_RGB24;

	
	image_out = malloc(width * height * 3);

	devin = open (argv[1], O_RDWR);

	if (devin < 0) {
		printf("Failed to open input video device [%s]\nError:[%s]\n", argv[1], strerror(errno));
		exit(1);
	}

	devout = open (argv[2], O_RDWR);

	if (devout < 0) {
		printf ("Failed to open output video device [%s]\nError:[%s]\n", argv[2], strerror(errno));
		exit(1);
	}

	image_new = start_capture(devin, width, height);

	if (!image_new) {
		printf("Capture error \n Error[%s]\n", strerror(errno));
		exit(1);
	}

	start_pipe(devout, width, height);

	signal(SIGTERM, sig_handler);
	
	printf("Starting video stream.\n");
	
	while ( (next_capture(devin, image_new, width, height)) && (noexit) ) {
		for (i = width * height * 3; i >= 0; i--) 
			image_out[i] =-image_new[i];

		if (put_image(devout, image_out, width, height) == 0) {
			if ((read_img) && (image_new))
				free(image_new);
			else if (fmt == VIDEO_PALETTE_GREY)
				munmap(image_new, width * height * 3);
			else
				munmap(image_new, width * height);
			free(image_out);
			exit(1);
		}	
	}

	printf("You bought vaporware!\nError[%s]\n", strerror(errno));

	close (devin);
	close (devout);

	if ((read_img) && (image_new))
		free(image_new);
	else if (fmt == VIDEO_PALETTE_GREY)
		munmap(image_new, width * height * 3);			
	else
		munmap(image_new, width * height);

	free(image_out);

	exit(0);
}
Example #2
0
int main(int argc, char *argv[])
{
    const char	*prog = xbasename(argv[0]);
    int	i, cmd = 0, checking_flag = 0;

    printf("Network UPS Tools %s %s\n", prog, UPS_VERSION);

    /* if no configuration file is specified on the command line, use default */
    configfile = xmalloc(SMALLBUF);
    snprintf(configfile, SMALLBUF, "%s/upsmon.conf", confpath());
    configfile = xrealloc(configfile, strlen(configfile) + 1);

    run_as_user = xstrdup(RUN_AS_USER);

    while ((i = getopt(argc, argv, "+Dhic:f:pu:VK46")) != -1) {
        switch (i) {
        case 'c':
            if (!strncmp(optarg, "fsd", strlen(optarg)))
                cmd = SIGCMD_FSD;
            if (!strncmp(optarg, "stop", strlen(optarg)))
                cmd = SIGCMD_STOP;
            if (!strncmp(optarg, "reload", strlen(optarg)))
                cmd = SIGCMD_RELOAD;

            /* bad command name given */
            if (cmd == 0)
                help(argv[0]);
            break;
        case 'D':
            nut_debug_level++;
            break;
        case 'f':
            free(configfile);
            configfile = xstrdup(optarg);
            break;
        case 'h':
            help(argv[0]);
            break;
        case 'K':
            checking_flag = 1;
            break;
        case 'p':
            use_pipe = 0;
            break;
        case 'u':
            free(run_as_user);
            run_as_user = xstrdup(optarg);
            break;
        case 'V':
            /* just show the banner */
            exit(EXIT_SUCCESS);
        case '4':
            opt_af = AF_INET;
            break;
        case '6':
            opt_af = AF_INET6;
            break;
        default:
            help(argv[0]);
            break;
        }
    }

    if (cmd) {
        sendsignal(prog, cmd);
        exit(EXIT_SUCCESS);
    }

    /* otherwise, we are being asked to start.
     * so check if a previous instance is running by sending signal '0'
     * (Ie 'kill <pid> 0') */
    if (sendsignal(prog, 0) == 0) {
        printf("Fatal error: A previous upsmon instance is already running!\n");
        printf("Either stop the previous instance first, or use the 'reload' command.\n");
        exit(EXIT_FAILURE);
    }

    argc -= optind;
    argv += optind;

    open_syslog(prog);

    loadconfig();

    if (checking_flag)
        exit(check_pdflag());

    if (shutdowncmd == NULL)
        printf("Warning: no shutdown command defined!\n");

    /* we may need to get rid of a flag from a previous shutdown */
    if (powerdownflag != NULL)
        clear_pdflag();
    /* FIXME (else): POWERDOWNFLAG is not defined!!
     * => fallback to a default value */

    if (totalpv < minsupplies) {
        printf("\nFatal error: insufficient power configured!\n\n");

        printf("Sum of power values........: %d\n", totalpv);
        printf("Minimum value (MINSUPPLIES): %d\n", minsupplies);

        printf("\nEdit your upsmon.conf and change the values.\n");
        exit(EXIT_FAILURE);
    }

    if (nut_debug_level < 1) {
        background();
    } else {
        upsdebugx(1, "debug level is '%d'", nut_debug_level);
    }

    /* only do the pipe stuff if the user hasn't disabled it */
    if (use_pipe) {
        struct passwd	*new_uid = get_user_pwent(run_as_user);

        /* === root parent and unprivileged child split here === */
        start_pipe();

        /* write the pid file now, as we will soon lose root */
        writepid(prog);

        become_user(new_uid);
    } else {
        upslogx(LOG_INFO, "Warning: running as one big root process by request (upsmon -p)");

        writepid(prog);
    }

    /* prep our signal handlers */
    setup_signals();

    /* reopen the log for the child process */
    closelog();
    open_syslog(prog);

    while (exit_flag == 0) {
        utype_t	*ups;

        /* check flags from signal handlers */
        if (userfsd)
            forceshutdown();

        if (reload_flag)
            reload_conf();

        for (ups = firstups; ups != NULL; ups = ups->next)
            pollups(ups);

        recalc();

        /* make sure the parent hasn't died */
        if (use_pipe)
            check_parent();

        /* reap children that have exited */
        waitpid(-1, NULL, WNOHANG);

        sleep(sleepval);
    }

    upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
    upsmon_cleanup();

    exit(EXIT_SUCCESS);
}