From owner-freebsd-hackers@FreeBSD.ORG Sat Nov 27 19:22:15 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 89A0B106566C; Sat, 27 Nov 2010 19:22:15 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 041748FC0C; Sat, 27 Nov 2010 19:22:14 +0000 (UTC) Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id oARJLpYh069058 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 27 Nov 2010 21:21:51 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4) with ESMTP id oARJLpoM057573; Sat, 27 Nov 2010 21:21:51 +0200 (EET) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4/Submit) id oARJLp4A057572; Sat, 27 Nov 2010 21:21:51 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 27 Nov 2010 21:21:51 +0200 From: Kostik Belousov To: "Carlos A. M. dos Santos" Message-ID: <20101127192151.GL2392@deviant.kiev.zoral.com.ua> References: <4CEEC3BD.3080204@delphij.net> <4CF13D7A.4060904@FreeBSD.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="4xm7XMBDuRVjShWb" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-3.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00, DNS_FROM_OPENWHOIS autolearn=no version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: FreeBSD-Hackers , Dimitry Andric , d@delphij.net Subject: Re: Is it possible to have file removed upon process exit? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Nov 2010 19:22:15 -0000 --4xm7XMBDuRVjShWb Content-Type: text/plain; charset=koi8-r Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Nov 27, 2010 at 05:07:15PM -0200, Carlos A. M. dos Santos wrote: > On Sat, Nov 27, 2010 at 3:18 PM, Dimitry Andric 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. =9AIs there a clean way to do thi= s? > > > > Maybe your process could be the child of a parent which cleans up > > afterwards? =9A(This is an analogy from real life. ;) >=20 > #include > #include > #include > #include > #include >=20 > static char filename[] =3D "/tmp/tmpfXXXXXX"; > static int fd =3D 0; >=20 > 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; > } This approach has usual problems of making a mess if the program want to fork() for other reasons, since the child should continue to execute a logic in your case, but cannot wait for already forked processes. Usual advice is to have child monitoring the liveness of the parent. You can either create a pipe before fork and read(2) from it in child, never writing from parent. read(2) will return when parent exits. Or, periodically compare getppid() with 1 in child, and do the cleanup when equal. Usually, it is too much hassle to do any of the tricks, normal system cleanup of /tmp on reboot is good enough. --4xm7XMBDuRVjShWb Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (FreeBSD) iEYEARECAAYFAkzxWk8ACgkQC3+MBN1Mb4hFRACg24LrAWKaYm+Zln4xRowBI4ey 1sgAoNUUGfzBVfoWHJHLQUxiGv+0sU6M =5nIm -----END PGP SIGNATURE----- --4xm7XMBDuRVjShWb--