DragonFlyBSD Kernel Audit
DF-2463 / idrv2463.c
← back to finding ↓ download raw
/*
 * idrv2463.c - variant of idrv with a long receiver window for DF-2463.
 * Same as idrv.c but sleeps 40s so the kernel receiver keeps calling
 * so_input()->pdu_alloc() for the whole memory-pressure window.
 */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/ioccom.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define ISCSISETSES _IOR('i', 1, int)
#define ISCSISETSOC _IOW('i', 2, int)

int main(int argc, char **argv) {
    const char *ip = argc > 1 ? argv[1] : "127.0.0.1";
    int port = argc > 2 ? atoi(argv[2]) : 3260;
    int secs = argc > 3 ? atoi(argv[3]) : 40;

    int fd = open("/dev/iscsi", O_RDWR);
    if (fd < 0) { perror("open /dev/iscsi"); return 2; }
    int n = -1;
    if (ioctl(fd, ISCSISETSES, &n) < 0) { perror("ISCSISETSES"); return 3; }
    printf("idrv: session id=%d\n", n); fflush(stdout);
    close(fd);

    char dev[32]; snprintf(dev, sizeof(dev), "/dev/iscsi%d", n);
    int nfd = open(dev, O_RDWR);
    if (nfd < 0) { perror(dev); return 4; }

    int soc = socket(AF_INET, SOCK_STREAM, 0);
    if (soc < 0) { perror("socket"); return 5; }
    struct sockaddr_in sa; memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    if (inet_pton(AF_INET, ip, &sa.sin_addr) != 1) { fprintf(stderr,"bad ip\n"); return 6; }
    if (connect(soc, (struct sockaddr*)&sa, sizeof(sa)) < 0) { perror("connect"); return 7; }
    if (ioctl(nfd, ISCSISETSOC, &soc) < 0) { perror("ISCSISETSOC"); return 8; }
    printf("idrv: receiver running for %ds\n", secs); fflush(stdout);

    sleep(secs);
    printf("idrv: alive after %ds\n", secs); fflush(stdout);
    close(nfd);
    return 0;
}