Skip to content

Instantly share code, notes, and snippets.

@ultraist
Created August 20, 2009 10:05
Show Gist options
  • Save ultraist/170949 to your computer and use it in GitHub Desktop.
Save ultraist/170949 to your computer and use it in GitHub Desktop.
// http://twitter.com/ultraistter/statuses/851812566
// gcc unlinktest.c -o unlinktest
// ./ulinktest &
// rm foobar
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define _UNLINK_CHILD 0
#define TEST_FILE "./foobar"
int main(void)
{
int fd = open(TEST_FILE, O_RDWR|O_CREAT, S_IWUSR|S_IRUSR);
int ret;
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
ret = flock(fd, LOCK_EX);
if (ret == -1) {
perror("flock");
return EXIT_FAILURE;
}
#if _UNLINK_CHILD
if (!fork()) {
ret = unlink(TEST_FILE);
if (ret == -1) {
perror("unlink");
}
exit(EXIT_SUCCESS);
}
#endif
sleep(10);
ret = flock(fd, LOCK_UN);
if (ret == -1) {
perror("funlock");
}
close(fd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment