Example #1
0
int main(int argc, char *argv[])
{
    int ch;
    char *stuff;

    char tmp_filename[] = "/tmp/lockalot.XXXXXXXXXX";
    struct pollfd pfd[1];

    while ((ch = getopt(argc, argv, "h?Lm:")) != -1) {
        switch (ch) {

        case 'L':
            Flag_Skip_Mlock = true;

        case 'm':
            Flag_Mem_Amount = flagtoul(ch, optarg, 1UL, MOSTMEMPOSSIBLE);
            break;

        case 'h':
        case '?':
        default:
            emit_help();
            /* NOTREACHED */
        }
    }
    argc -= optind;
    argv += optind;

    if (Flag_Mem_Amount == 0)
        emit_help();

    if ((stuff = malloc((size_t) Flag_Mem_Amount)) == NULL)
        err(EX_OSERR, "could not malloc() %lu bytes of memory",
            Flag_Mem_Amount);

    if (!Flag_Skip_Mlock)
        if (mlock(stuff, (size_t) Flag_Mem_Amount) == -1)
            err(EX_OSERR, "could not mlock() %lu bytes of memory",
                Flag_Mem_Amount);

    // Avoid busy loop if cannot block on input (e.g. started in background)
    if (isatty(STDIN_FILENO)) {
        pfd[0].fd = STDIN_FILENO;
    } else {
        fprintf(stderr, "notice: doing mkstemp to create file to poll...\n");
        if ((pfd[0].fd = mkstemp(tmp_filename)) == -1)
            err(EX_IOERR, "mkstemp failed to create tmp file");
    }
    pfd[0].events = POLLPRI;
    for (;;) {
        poll(pfd, 1, 60 * 10000);
    }

    /* NOTREACHED */
    exit(1);
}
Example #2
0
static ulong
parse_flags(char *flags)
{
	char *delim;
	ulong flag = 0;

	while (flags != NULL) {
		delim = strchr(flags, '|');
		if (delim != NULL)
			*delim++ = 0;
		flag |= flagtoul(flags);
		flags = delim;
	}
	return flag;
}
Example #3
0
int parse_opts(int argc, char *argv[])
{
    int ch;

    bool fourandsix = false;
    bool delayed_flood = false;

    Flag_AI_Family = AF_UNSPEC;
    Flag_Count = DEFAULT_STATS_INTERVAL;
    Flag_Delay = DEFAULT_DELAY;
    Flag_Padding = sizeof(uint32_t);

    while ((ch = getopt(argc, argv, "46C:c:d:flNP:p:")) != -1) {
        switch (ch) {

        case '4':
            if (fourandsix) {
                warnx("need just one of -4 or -6");
                emit_usage();
            }
            Flag_AI_Family = AF_INET;
            fourandsix = true;
            break;
        case '6':
            if (fourandsix) {
                warnx("need just one of -4 or -6");
                emit_usage();
            }
            Flag_AI_Family = AF_INET6;
            fourandsix = true;
            break;

        case 'C':
            Flag_Max_Send = flagtoul(ch, optarg, 0UL, (unsigned long) INT_MAX);
            break;

        case 'c':
            Flag_Count = flagtoul(ch, optarg, 0UL, (unsigned long) INT_MAX);
            break;

        case 'd':
            if (delayed_flood) {
                warnx("cannot both delay and flood packets");
                emit_usage();
            }
            Flag_Delay =
                (unsigned int) flagtoul(ch, optarg, 0UL,
                                        (unsigned long) INT_MAX);
            Flag_Flood = 0;
            delayed_flood = true;
            break;

        case 'f':
            if (delayed_flood) {
                warnx("cannot both delay and flood packets");
                emit_usage();
            }
            Flag_Flood = true;
            delayed_flood = true;
            break;

        case 'l':
            Flag_Line_Buf = true;
            break;

        case 'N':
            Flag_Nanoseconds = true;
            break;

        case 'P':
            // NOTE greatly restrict max size of packet by default
            Flag_Padding = (size_t) flagtoul(ch, optarg, 0UL, 8192UL);
            // ... but do need a minimum size for the counter in the packet
            if (Flag_Padding < sizeof(uint32_t))
                Flag_Padding = sizeof(uint32_t);
            break;

        case 'p':
            Flag_Port = optarg;
            break;

        case 'h':
        default:
            emit_usage();
            /* NOTREACHED */
        }
    }

    if (!Flag_Port) {
        warnx("-p port option is mandatory");
        emit_usage();
    }

    return optind;
}