From owner-freebsd-arch@FreeBSD.ORG Tue Aug 23 23:19:18 2005 Return-Path: X-Original-To: freebsd-arch@freebsd.org Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7795F16A41F for ; Tue, 23 Aug 2005 23:19:18 +0000 (GMT) (envelope-from pjd@garage.freebsd.pl) Received: from mail.garage.freebsd.pl (arm132.internetdsl.tpnet.pl [83.17.198.132]) by mx1.FreeBSD.org (Postfix) with ESMTP id B69F043D45 for ; Tue, 23 Aug 2005 23:19:17 +0000 (GMT) (envelope-from pjd@garage.freebsd.pl) Received: by mail.garage.freebsd.pl (Postfix, from userid 65534) id 5F84E52C3F; Wed, 24 Aug 2005 01:19:15 +0200 (CEST) Received: from localhost (dks251.neoplus.adsl.tpnet.pl [83.24.22.251]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.garage.freebsd.pl (Postfix) with ESMTP id BC8E952BC4; Wed, 24 Aug 2005 01:19:08 +0200 (CEST) Date: Wed, 24 Aug 2005 01:18:48 +0200 From: Pawel Jakub Dawidek To: Peter Jeremy Message-ID: <20050823231848.GD65338@garage.freebsd.pl> References: <20050822213028.GB4812@garage.freebsd.pl> <430A8F1A.6090607@freebsd.org> <20050823081758.GB47261@garage.freebsd.pl> <20050823094226.GD37107@cirb503493.alcatel.com.au> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="NklN7DEeGtkPCoo3" Content-Disposition: inline In-Reply-To: <20050823094226.GD37107@cirb503493.alcatel.com.au> X-PGP-Key-URL: http://people.freebsd.org/~pjd/pjd.asc X-OS: FreeBSD 7.0-CURRENT i386 User-Agent: mutt-ng devel (FreeBSD) X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on mail.garage.freebsd.pl X-Spam-Level: X-Spam-Status: No, score=-0.5 required=3.0 tests=BAYES_00,RCVD_IN_NJABL_DUL, RCVD_IN_SORBS_DUL autolearn=no version=3.0.4 Cc: FreeBSD-arch Subject: Re: New library: libpidfile. X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 23 Aug 2005 23:19:18 -0000 --NklN7DEeGtkPCoo3 Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Aug 23, 2005 at 07:42:26PM +1000, Peter Jeremy wrote: +> On Tue, 2005-Aug-23 10:17:58 +0200, Pawel Jakub Dawidek wrote: +> >In my first concept (when it was part of libutil) I allocated memory +> >to store needed informations, because I didn't wanted to use preallocat= ed +> >memory (someone linking libutil doesn't have to use pidfile +> >functionality). +>=20 +> Since there's an initialisation function, why not just malloc whatever +> memory you need? You can either store the pointer in a local static +> or have pidfile_open() return it as an opaque pointer that the user +> has to pass into other pidfile_XXX functions. I added a handle, so now it goes like this: struct pidfh *pfh; pid_t otherpid, childpid; pfh =3D pidfile_open("/var/run/daemon.pid", 0644, &otherpid); if (pfh =3D=3D NULL) { if (errno =3D=3D EEXIST) errx(EXIT_FAILURE, "Daemon already running, pid: %d.", otherpid); /* If we cannot create pidfile from other reasons, only warn. */ warn("Cannot open or create pidfile"); } if (daemon(0, 0) =3D=3D -1) { warn("Cannot daemonize"); pidfile_remove(pfh); exit(EXIT_FAILURE); } pidfile_write(pfh); for (;;) { /* Do work. */ childpid =3D fork(); switch (childpid) { case -1: syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno)); break; case 0: pidfile_close(pfh); /* Do child work. */ break; default: syslog(LOG_INFO, "Child %d started.", childpid); break; } } pidfile_remove(pfh); exit(EXIT_SUCCESS); I can't use atexit(3) anymore to remove pidfile, but I like it this way better, anyway. +> >It also now has 4 functions, which makes it a good candidate of small, +> >nice, lightweight library:) +>=20 +> IMHO, 4 functions is too small. I would prefer to have a smaller +> number of larger libraries and think this belongs in an existing +> library - eg libutil. Whilst this may form a nice lightweight +> library, if everyone wrote small, lightweight libraries, linking +> an application would require a mile-long command line. I talked about this with few more guys and they agree it should be a part of libutil, so I moved it there. +> Can I suggest two enhancements: +> 1) Allow NULL to be passed to pidfile_open() to indicate that the +> pidfile should be in /var/run/PROCESS_NAME.pid (and/or if the +> name doesn't include any '/' then default to /var/run) I implemented NULL behaviour, but I'm not sure about the second thing. I'd like to avoid it. +> 2) Write a wrapper function that calls pidfile_open() and if it's +> successful, exec's the rest of the command line. There is simlar wrapper: lockf(1), so I'll not duplicate functionality, but I changed daemon(8) to use pidfile(3). Thanks for all suggestions. --=20 Pawel Jakub Dawidek http://www.wheel.pl pjd@FreeBSD.org http://www.FreeBSD.org FreeBSD committer Am I Evil? Yes, I Am! --NklN7DEeGtkPCoo3 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (FreeBSD) iD8DBQFDC67YForvXbEpPzQRAi+nAJ0VNjhiOJJHFp43yZbKXiwQLT2OBACdHbzn sHKDzW9qI1cS3j76ssUyzAo= =kLCP -----END PGP SIGNATURE----- --NklN7DEeGtkPCoo3--