Skip to content

Instantly share code, notes, and snippets.

@zhouqiang-cl
Created December 25, 2019 08:12
Show Gist options
  • Save zhouqiang-cl/687ed0546ea61bdc1f37e7898cda8d03 to your computer and use it in GitHub Desktop.
Save zhouqiang-cl/687ed0546ea61bdc1f37e7898cda8d03 to your computer and use it in GitHub Desktop.
failure-gen.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#define PATH_MAX_LEN 128
#define BUF_LEN sizeof(struct inotify_event) + PATH_MAX_LEN + 1
static void handleInotifyEvent(struct inotify_event *ie)
{
void *p = NULL;
switch (ie->mask) {
case IN_CREATE:
exit(0);
case IN_DELETE:
printf("IN_DELETE\n");
while (1) {
p = calloc(1, sizeof(1024 * 1024 * 1024));
}
break;
default:
printf("Unsupported mask: %d :(\n", ie->mask);
}
if (ie->len > 0)
printf("fname = %s\n", ie->name);
return;
}
int main(int argc, char *argv[])
{
char buf[BUF_LEN] __attribute__((aligned(8)));
struct inotify_event *ie;
int ifd, nr;
char *p;
if (argc < 2) {
fprintf(stderr, "Usage %s pathname\n", argv[0]);
exit(1);
}
ifd = inotify_init();
if (ifd == -1) {
perror("Init inotify failed: ");
exit(1);
}
if (inotify_add_watch(ifd, argv[1], IN_ALL_EVENTS) == -1) {
perror("Add watch failed: ");
exit(1);
}
while (1) {
nr = read(ifd, buf, BUF_LEN);
if (nr == -1) {
perror("Read from ifd failed: ");
exit(1);
}
if (nr == 0) {
fprintf(stderr, "Read from ifd return 0!\n");
exit(1);
}
printf("Read %d bytes from ifd\n", nr);
p = buf;
ie = (struct inotify_event *)p;
handleInotifyEvent(ie);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment