Can someone find an OS where the attached program doesn't work? It
should print "success". So far tested for success: Linux 2.6, Solaris
10, FreeBSD 7.2, OpenBSD 4.2.

/*
   gcc test.c -o test -Wall
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#define TEMP_PATH "/tmp/ipctest.1234"

int main(int argc, char *argv[])
{
	int fd, ret;

	if (argv[1] != NULL && strcmp(argv[1], "1") == 0) {
		int fd = 3;

		usleep(500000);
		if ((ret = lseek(fd, 0, SEEK_CUR)) != 2) {
			printf("failed: 1. offset=%d\n", ret);
			return 1;
		}
		usleep(500000);
		if ((ret = lseek(fd, 0, SEEK_CUR)) != 3) {
			printf("failed: 2. offset=%d\n", ret);
			return 1;
		}
		printf("success\n");
		return 0;
	}

	fd = creat(TEMP_PATH, 0600);
	if (fd != 3) {
		if (fd == -1) perror("open()");
		if (dup2(fd, 3) < 0) perror("dup");
	}
	if (unlink(TEMP_PATH) < 0) perror("unlink()");

	if (write(fd, "foo", 3) < 0) perror("write()");
	lseek(fd, 1, SEEK_SET);
	if (fork() == 0) {
		char *args[] = { NULL, "1", NULL };

		args[0] = argv[0];
		execv(args[0], args);
	} else {
		lseek(fd, 2, SEEK_SET);
		sleep(1);
		lseek(fd, 3, SEEK_SET);
		sleep(1);
	}
	return 0;
}

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to