DragonFlyBSD Kernel Audit
DF-2663 / diocgtest.c
← back to finding ↓ download raw
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/diskslice.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int argc, char **argv) {
    struct partinfo p;
    int fd, r, i;
    for (i = 1; i < argc; i++) {
        memset(&p, 0xAA, sizeof(p)); /* poison to detect partial fill */
        fd = open(argv[i], O_RDONLY);
        if (fd < 0) { printf("%s: open failed %s\n", argv[i], strerror(errno)); continue; }
        r = ioctl(fd, DIOCGPART, &p);
        if (r == 0)
            printf("%s: DIOCGPART OK media_size=0x%jx blksize=%u\n",
                   argv[i], (uintmax_t)p.media_size, p.media_blksize);
        else
            printf("%s: DIOCGPART FAIL errno=%d (%s)\n", argv[i], errno, strerror(errno));
        close(fd);
    }
    return 0;
}