Re: [patch] rc.subr support for pidfile-based daemons

2018-06-02 Thread Theo de Raadt
IL Ka  wrote:

> >
> > pid files can easily get out of sync with the expected process (example:
> > daemon starts, writes a pid file, later it crashes. at any point between
> > the crash and you trying to stop/restart the daemon another process
> > could be assigned the same pid, then the wrong process is signalled).
> >
> 
> Wow, I've never thought about that)
> pidfiles are so common on unicies, and mentioned in docs like "standard"
> approach:
> For example: http://uwsgi-docs.readthedocs.io/en/latest/Management.html
> # using kill to send the signal
> kill -HUP `cat /tmp/project-master.pid`
> 
> Is it because other OSes do not randomize pids as OpenBSD does (they use
> sequental pids)
> so chances for pid reuse are lower there?
> 
> Or do they simply "do not care"?

They do not care.



Re: [patch] rc.subr support for pidfile-based daemons

2018-06-02 Thread IL Ka
>
> pid files can easily get out of sync with the expected process (example:
> daemon starts, writes a pid file, later it crashes. at any point between
> the crash and you trying to stop/restart the daemon another process
> could be assigned the same pid, then the wrong process is signalled).
>

Wow, I've never thought about that)
pidfiles are so common on unicies, and mentioned in docs like "standard"
approach:
For example: http://uwsgi-docs.readthedocs.io/en/latest/Management.html
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`

Is it because other OSes do not randomize pids as OpenBSD does (they use
sequental pids)
so chances for pid reuse are lower there?

Or do they simply "do not care"?

Anyway, if this is done intentionally, my approach is not correct.

Ilya.


Re: [patch] rc.subr support for pidfile-based daemons

2018-06-02 Thread Stuart Henderson
On 2018/06/02 23:07, Il Ka wrote:
> rc.subr(8) uses pgrep(1)/pkill(1) to control daemons
> using their command lines.

This is intentional.

pid files can easily get out of sync with the expected process (example:
daemon starts, writes a pid file, later it crashes. at any point between
the crash and you trying to stop/restart the daemon another process
could be assigned the same pid, then the wrong process is signalled).
The timescale could be days/weeks/longer.

(I believe there is still a possible race with pkill, but there the
window is tiny, much less than a second.)



[patch] rc.subr support for pidfile-based daemons

2018-06-02 Thread Il Ka
Hello,

rc.subr(8) uses pgrep(1)/pkill(1) to control daemons
using their command lines.

But in some cases it is more convenient to use pidfiles.
Many services do so, inluding apache and postgresql.
But not all services do have special tool for that.

I run gunicorn (python wsgi server) that is able to
produce pidfile, but lacks of tool to control it.

So, I added ``daemon_pidfile`` variable to rc.subr(8).
If this variable is not set then everything works as usual.

But if you set it then system uses it to check (ps -p), 
stop and reload (kill) daemon.

I then simply tell gunicorn to write its pidfile and 
everything works.

I believe this approach may be used by other rc scripts also.

Diff below does not have man yet, and I am ready to write it
if everyone would agree with my changes.

Ilya.



Index: etc/rc.d/rc.subr
===
RCS file: /cvs/src/etc/rc.d/rc.subr,v
retrieving revision 1.127
diff -u -p -u -r1.127 rc.subr
--- etc/rc.d/rc.subr5 Jun 2017 18:31:23 -   1.127
+++ etc/rc.d/rc.subr2 Jun 2018 19:33:29 -
@@ -49,6 +49,7 @@ _rc_write_runfile() {
cat >${_RC_RUNFILE}