Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Nov 2010 17:07:15 -0200
From:      "Carlos A. M. dos Santos" <unixmania@gmail.com>
To:        Dimitry Andric <dim@freebsd.org>
Cc:        FreeBSD-Hackers <freebsd-hackers@freebsd.org>, d@delphij.net
Subject:   Re: Is it possible to have file removed upon process exit?
Message-ID:  <AANLkTikH_uD1-yUe6KTufBvs9zRdQ%2BiMVQWcavBYRXU9@mail.gmail.com>
In-Reply-To: <4CF13D7A.4060904@FreeBSD.org>
References:  <4CEEC3BD.3080204@delphij.net> <4CF13D7A.4060904@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, Nov 27, 2010 at 3:18 PM, Dimitry Andric <dim@freebsd.org> wrote:
> On 2010-11-25 21:14, Xin LI wrote:
>>
>> For certain applications it is sometimes desirable to (e.g. for unix
>> domain sockets) have file removed when the process quit, regardless
>> whether the process is quit cleanly. =A0Is there a clean way to do this?
>
> Maybe your process could be the child of a parent which cleans up
> afterwards? =A0(This is an analogy from real life. ;)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

static char filename[] =3D "/tmp/tmpfXXXXXX";
static int fd =3D 0;

int main(void) {
	if ((fd =3D mkstemp(filename)) >=3D 0) {
		pid_t pid;
		if ((pid =3D fork()) > 0) {
			/* parent */
			wait(NULL);
			printf("unlinking '%s'\n", filename);
			unlink(filename);
			return EXIT_SUCCESS;
		} else {
			/* child */
			printf("file name is '%s'\n", filename);
			sleep(10);
			abort();
		}
	}
	return EXIT_FAILURE;
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTikH_uD1-yUe6KTufBvs9zRdQ%2BiMVQWcavBYRXU9>