Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
incorrect use of pidfile(3)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dag-Erling Smørgrav  
View profile  
 More options Oct 13 2011, 6:54 am
From: Dag-Erling Smørgrav <d...@des.no>
Date: Thu, 13 Oct 2011 12:54:38 +0200
Local: Thurs, Oct 13 2011 6:54 am
Subject: incorrect use of pidfile(3)
I looked at some of the programs that use pidfile(3) in base, and they
pretty much all get it wrong.  Consider these two scenarios:

1) common case

    process A                           process B

    main()
      pidfile_open() -> success
      perform_initialization()
      daemon()
        pidfile_write() -> success
        perform_work()                  main()
                                          pidfile_open() -> EEXIST
                                          exit()

2) very unlikely but still possible case

    process A                           process B

    main()
      pidfile_open() -> success         main()
      perform_initialization()            pidfile_open() -> EAGAIN
      daemon()                            perform_initialization()
        pidfile_write() -> success        daemon()
        perform_work()                      perform_work()

The problem is that most of them (at least the ones I checked) ignore a
pidfile_open() failure unless errno == EEXIST.

How do we fix this?  My suggestion is to loop until pidfile_open()
succeeds or errno != EAGAIN.  Does anyone have any objections to that
approach?

DES
--
Dag-Erling Smørgrav - d...@des.no
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pawel Jakub Dawidek  
View profile  
 More options Oct 13 2011, 7:30 am
From: Pawel Jakub Dawidek <p...@FreeBSD.org>
Date: Thu, 13 Oct 2011 13:30:26 +0200
Local: Thurs, Oct 13 2011 7:30 am
Subject: Re: incorrect use of pidfile(3)

I think we already do that internally in pidfile_open(). Can you take a look at
the source and confirm that this is what you mean?

--
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://yomoli.com

  application_pgp-signature_part
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dag-Erling Smørgrav  
View profile  
 More options Oct 13 2011, 7:51 am
From: Dag-Erling Smørgrav <d...@des.no>
Date: Thu, 13 Oct 2011 13:51:26 +0200
Local: Thurs, Oct 13 2011 7:51 am
Subject: Re: incorrect use of pidfile(3)
Pawel Jakub Dawidek <p...@FreeBSD.org> writes:

> Dag-Erling Smørgrav <d...@des.no> writes:
> > How do we fix this?  My suggestion is to loop until pidfile_open()
> > succeeds or errno != EAGAIN.  Does anyone have any objections to that
> > approach?
> I think we already do that internally in pidfile_open(). Can you take a look at
> the source and confirm that this is what you mean?

No, it doesn't; pidfile_open(3) returns NULL with errno == EAGAIN if the
pidfile is locked but empty, as is the case in the window between a
successful pidfile_open(3) and the first pidfile_write(3).  This is
documented in the man page:

     [EAGAIN]           Some process already holds the lock on the given pid‐
                        file, but the file is truncated.  Most likely, the
                        existing daemon is writing new PID into the file.

I have a patch that adds a pidfile to dhclient(8), where I do this:

        for (;;) {
                pidfile = pidfile_open(path_dhclient_pidfile, 0600, &otherpid);
                if (pidfile != NULL || errno != EAGAIN)
                        break;
                sleep(1);
        }
        if (pidfile == NULL) {
                if (errno == EEXIST)
                        error("dhclient already running, pid: %d.", otherpid);
                warning("Cannot open or create pidfile: %m");
        }

I'm not sure I agree with the common idiom (which I copied here) of
ignoring all other errors than EEXIST, but that's a different story.

DES
--
Dag-Erling Smørgrav - d...@des.no
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos A. M. dos Santos  
View profile  
 More options Oct 13 2011, 8:21 am
From: "Carlos A. M. dos Santos" <unixma...@gmail.com>
Date: Thu, 13 Oct 2011 09:21:11 -0300
Local: Thurs, Oct 13 2011 8:21 am
Subject: Re: incorrect use of pidfile(3)
2011/10/13 Dag-Erling Smørgrav <d...@des.no>:

You are also ignoring the return value of sleep(1), which would tell
you if the call was interrupted by a signal handler. This can be fine
for dhclient(8) but other utilities might require some guards against
such interruptions.

--
"The flames are all long gone, but the pain lingers on"
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dag-Erling Smørgrav  
View profile  
 More options Oct 13 2011, 8:54 am
From: Dag-Erling Smørgrav <d...@des.no>
Date: Thu, 13 Oct 2011 14:54:16 +0200
Local: Thurs, Oct 13 2011 8:54 am
Subject: Re: incorrect use of pidfile(3)

After discussing this with pjd@ on IRC, I arrived at the attached patch,
which increases the length of time pidfile_open() itself waits (I hadn't
noticed that it already looped) and sets *pidptr to -1 if it fails to read
a pid.

DES
--
Dag-Erling Smørgrav - d...@des.no

  pidfile_open-loop.diff
< 1K Download

_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pawel Jakub Dawidek  
View profile  
 More options Oct 13 2011, 9:48 am
From: Pawel Jakub Dawidek <p...@FreeBSD.org>
Date: Thu, 13 Oct 2011 15:48:42 +0200
Local: Thurs, Oct 13 2011 9:48 am
Subject: Re: incorrect use of pidfile(3)

On Thu, Oct 13, 2011 at 02:54:16PM +0200, Dag-Erling Smørgrav wrote:
> After discussing this with pjd@ on IRC, I arrived at the attached patch,
> which increases the length of time pidfile_open() itself waits (I hadn't
> noticed that it already looped) and sets *pidptr to -1 if it fails to read
> a pid.

I'm still in opinion that EWOULDBLOCK and EAGAIN (which is the same
value on FreeBSD) should be converted to EEXIST on pidfile_open()
return.

Also if we now have for loop, why not to put count in there?

I'm not very happy about touching pidptr in case of error other than
EEXIST. This is not documented, but a bit unexpected anyway.

I'm happy with increasing the timeout.

--
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://yomoli.com

  application_pgp-signature_part
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dag-Erling Smørgrav  
View profile  
 More options Oct 13 2011, 10:11 am
From: Dag-Erling Smørgrav <d...@des.no>
Date: Thu, 13 Oct 2011 16:11:40 +0200
Local: Thurs, Oct 13 2011 10:11 am
Subject: Re: incorrect use of pidfile(3)
Pawel Jakub Dawidek <p...@FreeBSD.org> writes:

> I'm still in opinion that EWOULDBLOCK and EAGAIN (which is the same
> value on FreeBSD) should be converted to EEXIST on pidfile_open()
> return.

The historical (and documented) behavior is to return EAGAIN.

> Also if we now have for loop, why not to put count in there?

Because if we do, there will be a nanosleep after the last
pidfile_read() attempt.  We need to break the loop after pidfile_read()
failed but before nanosleep().

> I'm not very happy about touching pidptr in case of error other than
> EEXIST. This is not documented, but a bit unexpected anyway.

Well, it was your idea, I just moved it to before the loop :)

DES
--
Dag-Erling Smørgrav - d...@des.no
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jos Backus  
View profile   Translate to Translated (View Original)
 More options Oct 13 2011, 10:16 pm
From: Jos Backus <j...@catnook.com>
Date: Thu, 13 Oct 2011 19:16:01 -0700
Local: Thurs, Oct 13 2011 10:16 pm
Subject: Re: incorrect use of pidfile(3)
Why not import daemontools? It's public domain these days. Pidfiles are a
hacky mess. UNIX already has a way to track processes which avoids all these
issues, with very little overhead.

Jos
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pawel Jakub Dawidek  
View profile  
 More options Oct 14 2011, 6:05 am
From: Pawel Jakub Dawidek <p...@FreeBSD.org>
Date: Fri, 14 Oct 2011 12:05:22 +0200
Local: Fri, Oct 14 2011 6:05 am
Subject: Re: incorrect use of pidfile(3)

On Thu, Oct 13, 2011 at 04:11:40PM +0200, Dag-Erling Smørgrav wrote:
> Pawel Jakub Dawidek <p...@FreeBSD.org> writes:
> > I'm still in opinion that EWOULDBLOCK and EAGAIN (which is the same
> > value on FreeBSD) should be converted to EEXIST on pidfile_open()
> > return.

> The historical (and documented) behavior is to return EAGAIN.

We don't want to duplicate the code of handling EAGAIN into every single
pidfile(3) consumer. This is why we hav pidfile(3) API in the first
place - to make it easy for people to use.

> > Also if we now have for loop, why not to put count in there?

> Because if we do, there will be a nanosleep after the last
> pidfile_read() attempt.  We need to break the loop after pidfile_read()
> failed but before nanosleep().

Right, ok.

> > I'm not very happy about touching pidptr in case of error other than
> > EEXIST. This is not documented, but a bit unexpected anyway.

> Well, it was your idea, I just moved it to before the loop :)

In my patch *pidptr was set to -1 only in the case of EAGAIN from
pidfile_read(), not for every other error.

BTW. With your patch we will continue even when flopen(3) failed for
other reasons, instead of returning NULL. Checking for fd being -1
should not be done in the same statement with other checks.

After proposed changes it would look like this, what do you think?

        http://people.freebsd.org/~pjd/patches/pidfile.3.patch

--
Pawel Jakub Dawidek                       http://www.wheelsystems.com
FreeBSD committer                         http://www.FreeBSD.org
Am I Evil? Yes, I Am!                     http://yomoli.com

  application_pgp-signature_part
< 1K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dag-Erling Smørgrav  
View profile  
 More options Oct 14 2011, 8:44 am
From: Dag-Erling Smørgrav <d...@des.no>
Date: Fri, 14 Oct 2011 14:44:01 +0200
Local: Fri, Oct 14 2011 8:44 am
Subject: Re: incorrect use of pidfile(3)
Pawel Jakub Dawidek <p...@FreeBSD.org> writes:

> After proposed changes it would look like this, what do you think?

>    http://people.freebsd.org/~pjd/patches/pidfile.3.patch

Looks OK to me, but you should also remove the paragraph about EAGAIN in
the man page.

DES
--
Dag-Erling Smørgrav - d...@des.no
_______________________________________________
freebsd-curr...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscr...@freebsd.org"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »