ntpd diff -- /var/run/ntpd.pid

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

ntpd diff -- /var/run/ntpd.pid

marcus.popp (Bugzilla)
Hi,

i've created a diff for the ntpd to store the pid in /var/run/ntpd.pid.

Maybe somebody is willing to integrate it?

hth,

Marcus.


Index: ntpd.8
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntpd.8,v
retrieving revision 1.14
diff -u -r1.14 ntpd.8
--- ntpd.8      21 Jun 2005 19:55:18 -0000      1.14
+++ ntpd.8      23 Aug 2006 19:19:41 -0000
@@ -105,6 +105,9 @@
 default
 .Nm
 configuration file
+.It Pa /var/run/ntpd.pid
+Contains the process ID of the
+.Nm
 .El
 .Sh SEE ALSO
 .Xr date 1 ,
Index: ntpd.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntpd.c,v
retrieving revision 1.41
diff -u -r1.41 ntpd.c
--- ntpd.c      21 Feb 2006 23:47:00 -0000      1.41
+++ ntpd.c      23 Aug 2006 19:19:41 -0000
@@ -78,6 +78,7 @@
 main(int argc, char *argv[])
 {
        struct ntpd_conf         conf;
+       static FILE             *fin = NULL;
        struct pollfd            pfd[POLL_MAX];
        pid_t                    chld_pid = 0, pid;
        const char              *conffile;
@@ -91,6 +92,8 @@
        log_init(1);            /* log to stderr until daemonized */
        res_init();             /* XXX */

+       conf.pid_file = NTPD_PID_FILE;
+
        while ((ch = getopt(argc, argv, "df:sS")) != -1) {
                switch (ch) {
                case 'd':
@@ -146,6 +149,17 @@
        signal(SIGCHLD, sighdlr);
        signal(SIGHUP, sighdlr);

+       if (!conf.debug) {
+       // Record our pid in /var/run/ntpd.pid
+               fin = fopen(conf.pid_file, "w");
+               if (fin == NULL) {
+                       fprintf(stderr,"Couldn't create pid file \"%s\"", conf.pid_file);
+               } else {
+                       fprintf(fin, "%ld\n", (long) getpid());
+                       fclose(fin);
+               }
+       }
+
        close(pipe_chld[1]);

        if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
@@ -208,6 +222,7 @@
                        fatal("wait");
        } while (pid != -1 || (pid == -1 && errno == EINTR));

+       unlink(conf.pid_file);
        msgbuf_clear(&ibuf->w);
        free(ibuf);
        log_info("Terminating");
Index: ntpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/ntpd.h,v
retrieving revision 1.61
diff -u -r1.61 ntpd.h
--- ntpd.h      24 Sep 2005 00:32:03 -0000      1.61
+++ ntpd.h      23 Aug 2006 19:19:41 -0000
@@ -57,6 +57,7 @@
 #define        SETTIME_MIN_OFFSET      180     /* min offset for settime at start */
 #define        SETTIME_TIMEOUT         15      /* max seconds to wait with -s */
 #define        LOG_NEGLIGEE            128     /* negligible drift to not log (ms) */
+#define        NTPD_PID_FILE           "/var/run/ntpd.pid"

 enum client_state {
        STATE_NONE,
@@ -130,6 +131,7 @@
        u_int8_t                                settime;
        u_int8_t                                debug;
        u_int32_t                               scale;
+       char                                    *pid_file;
 };

 struct buf {

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Theo de Raadt
> i've created a diff for the ntpd to store the pid in /var/run/ntpd.pid.

In general we no longer believe in pid files.

They have a tendency to lay around sometimes after processes have
died, and thus you cannot send a signal to the number in such a file.
If you do, you may kill some other process.  They are just not worthy
of trust.

Please use pkill and pgrep instead, which will give you up-to-date
information.

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Henning Brauer-5
In reply to this post by marcus.popp (Bugzilla)
* Marcus Popp <[hidden email]> [2006-08-23 21:29]:
> i've created a diff for the ntpd to store the pid in /var/run/ntpd.pid.
> Maybe somebody is willing to integrate it?

no. pid files are wrong.

--
BS Web Services, http://www.bsws.de/, [hidden email], [hidden email]
OpenBSD-based Webhosting, Mail Services, Managed Servers, ...
Unix is very simple, but it takes a genius to understand the simplicity.
(Dennis Ritchie)

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Stuart Henderson
In reply to this post by Theo de Raadt
> Please use pkill and pgrep instead, which will give you up-to-date
> information.

...but beware, you might want to "pgrep -l $FOO" before you
"pkill $FOO", particularly where FOO=ssh...

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Thorsten Glaser-3
Stuart Henderson dixit:

>...but beware, you might want to "pgrep -l $FOO" before you
>"pkill $FOO", particularly where FOO=ssh...

If your ssh(1) runs as root, you've done something wrong anyway.

//mirabile
--
I believe no one can invent an algorithm. One just happens to hit upon it
when God enlightens him. Or only God invents algorithms, we merely copy them.
If you don't believe in God, just consider God as Nature if you won't deny
existence. -- Coywolf Qi Hunt

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Han Boetes
In reply to this post by Stuart Henderson
Stuart Henderson wrote:
> > Please use pkill and pgrep instead, which will give you up-to-date
> > information.
>
> ...but beware, you might want to "pgrep -l $FOO" before you
> "pkill $FOO", particularly where FOO=ssh...

pkill -HUP -f /usr/sbin/sshd



# Han

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Miod Vallat
In reply to this post by Thorsten Glaser-3
> >...but beware, you might want to "pgrep -l $FOO" before you
> >"pkill $FOO", particularly where FOO=ssh...
>
> If your ssh(1) runs as root, you've done something wrong anyway.

Did you realize pgrep ssh matches sshd?

Miod

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Thorsten Glaser-3
Miod Vallat dixit:

>> >...but beware, you might want to "pgrep -l $FOO" before you
>> >"pkill $FOO", particularly where FOO=ssh...
>>
>> If your ssh(1) runs as root, you've done something wrong anyway.
>
>Did you realize pgrep ssh matches sshd?

Sure, but if I execute 'sudo pkill ssh' I've done more than that wrong.
('sudo -u someotheruser pkill ssh' works as well)

//mirabile
--
I believe no one can invent an algorithm. One just happens to hit upon it
when God enlightens him. Or only God invents algorithms, we merely copy them.
If you don't believe in God, just consider God as Nature if you won't deny
existence. -- Coywolf Qi Hunt

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Garance A Drosihn
In reply to this post by Theo de Raadt
At 1:33 PM -0600 8/23/06, Theo de Raadt wrote:

>  >  I've created a diff for the ntpd to store the pid
>  >  in /var/run/ntpd.pid.
>
>In general we no longer believe in pid files.
>
>They have a tendency to lay around sometimes after processes have
>died, and thus you cannot send a signal to the number in such a
>file.  If you do, you may kill some other process.  They are just
>not worthy of trust.
>
>Please use pkill and pgrep instead, which will give you up-to-date
>information.

Fwiw, FreeBSD added a -F parameter to pkill and pgrep, which
reads in a pidfile (given with that parameter), and also allows
you to specify the process-name that you expect that find at
that pid.  So you could:

    pkill -F /var/run/ntpd.pid ntpd

to make sure you're killing the process you expect to be killing.

--
Garance Alistair Drosehn            =   [hidden email]
Senior Systems Programmer           or  [hidden email]
Rensselaer Polytechnic Institute    or  [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Martin Schröder
In reply to this post by Han Boetes
2006/8/23, Han Boetes <[hidden email]>:
> pkill -HUP -f /usr/sbin/sshd

pkill -x sshd

Best
   Martin

Reply | Threaded
Open this post in threaded view
|

Re: ntpd diff -- /var/run/ntpd.pid

Michael Shalayeff-2
In reply to this post by marcus.popp (Bugzilla)
On Wed, Aug 23, 2006 at 07:20:37PM +0000, Marcus Popp wrote:
> Hi,
re

> i've created a diff for the ntpd to store the pid in /var/run/ntpd.pid.
> Maybe somebody is willing to integrate it?

there is no need for ntpd.pid as generally they are only
useful for newsyslog and ntpd clearly logs into plain
syslog so no file to reopen. there is no other activities
that ntpd is needed to do upon signal (except dying ;) either.

cu

> Index: ntpd.8
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ntpd/ntpd.8,v
> retrieving revision 1.14
> diff -u -r1.14 ntpd.8
> --- ntpd.8      21 Jun 2005 19:55:18 -0000      1.14
> +++ ntpd.8      23 Aug 2006 19:19:41 -0000
> @@ -105,6 +105,9 @@
>  default
>  .Nm
>  configuration file
> +.It Pa /var/run/ntpd.pid
> +Contains the process ID of the
> +.Nm
>  .El
>  .Sh SEE ALSO
>  .Xr date 1 ,
> Index: ntpd.c
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ntpd/ntpd.c,v
> retrieving revision 1.41
> diff -u -r1.41 ntpd.c
> --- ntpd.c      21 Feb 2006 23:47:00 -0000      1.41
> +++ ntpd.c      23 Aug 2006 19:19:41 -0000
> @@ -78,6 +78,7 @@
>  main(int argc, char *argv[])
>  {
>         struct ntpd_conf         conf;
> +       static FILE             *fin = NULL;
>         struct pollfd            pfd[POLL_MAX];
>         pid_t                    chld_pid = 0, pid;
>         const char              *conffile;
> @@ -91,6 +92,8 @@
>         log_init(1);            /* log to stderr until daemonized */
>         res_init();             /* XXX */
>
> +       conf.pid_file = NTPD_PID_FILE;
> +
>         while ((ch = getopt(argc, argv, "df:sS")) != -1) {
>                 switch (ch) {
>                 case 'd':
> @@ -146,6 +149,17 @@
>         signal(SIGCHLD, sighdlr);
>         signal(SIGHUP, sighdlr);
>
> +       if (!conf.debug) {
> +       // Record our pid in /var/run/ntpd.pid
> +               fin = fopen(conf.pid_file, "w");
> +               if (fin == NULL) {
> +                       fprintf(stderr,"Couldn't create pid file \"%s\"", conf.pid_file);
> +               } else {
> +                       fprintf(fin, "%ld\n", (long) getpid());
> +                       fclose(fin);
> +               }
> +       }
> +
>         close(pipe_chld[1]);
>
>         if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
> @@ -208,6 +222,7 @@
>                         fatal("wait");
>         } while (pid != -1 || (pid == -1 && errno == EINTR));
>
> +       unlink(conf.pid_file);
>         msgbuf_clear(&ibuf->w);
>         free(ibuf);
>         log_info("Terminating");
> Index: ntpd.h
> ===================================================================
> RCS file: /cvs/src/usr.sbin/ntpd/ntpd.h,v
> retrieving revision 1.61
> diff -u -r1.61 ntpd.h
> --- ntpd.h      24 Sep 2005 00:32:03 -0000      1.61
> +++ ntpd.h      23 Aug 2006 19:19:41 -0000
> @@ -57,6 +57,7 @@
>  #define        SETTIME_MIN_OFFSET      180     /* min offset for settime at start */
>  #define        SETTIME_TIMEOUT         15      /* max seconds to wait with -s */
>  #define        LOG_NEGLIGEE            128     /* negligible drift to not log (ms) */
> +#define        NTPD_PID_FILE           "/var/run/ntpd.pid"
>
>  enum client_state {
>         STATE_NONE,
> @@ -130,6 +131,7 @@
>         u_int8_t                                settime;
>         u_int8_t                                debug;
>         u_int32_t                               scale;
> +       char                                    *pid_file;
>  };
>
>  struct buf {
>

--
    paranoic mickey       (my employers have changed but, the name has remained)