mogilefsd: writing pid file

Brad Fitzpatrick brad at danga.com
Fri Dec 29 01:14:50 UTC 2006


Thanks!  Checked in, slightly updated, in svn 568.


On Thu, 28 Dec 2006, Radu Greab wrote:

>
> Hi,
>
> I have a patch to make mogilefsd write its pid to a configurable file.
>
>
> Thanks,
> Radu Greab
>
> === trunk/server/mogilefsd
> ==================================================================
> --- trunk/server/mogilefsd	(revision 68700)
> +++ trunk/server/mogilefsd	(local)
> @@ -86,12 +87,22 @@
>  openlog('mogilefsd', 'pid', 'daemon');
>  Mgd::log('info', 'beginning run');
>
> +if (MogileFS->config("pidfile") &&
> +    !MogileFS::Util::write_pidfile( MogileFS->config("pidfile") )) {
> +    Mgd::log('info', "Couldn't write pidfile, ending run");
> +    closelog();
> +    exit 1;
> +}
> +
>  # Install signal handlers.
>  $SIG{TERM}  = sub {
>      my @children = MogileFS::ProcManager->child_pids;
>      print STDERR scalar @children, " children to kill.\n" if $DEBUG;
>      my $count = kill( 'TERM' => @children );
>      print STDERR "Sent SIGTERM to $count children.\n" if $DEBUG;
> +    Mgd::Util::remove_pidfile( MogileFS->config("pidfile") ) if MogileFS->config("pidfile");
> +    Mgd::log('info', 'ending run');
> +    closelog();
>      exit 0;
>  };
>  $SIG{INT}  = sub {
> @@ -99,6 +110,9 @@
>      print STDERR scalar @children, " children to kill.\n" if $DEBUG;
>      my $count = kill( 'INT' => @children );
>      print STDERR "Sent SIGINT to $count children.\n" if $DEBUG;
> +    Mgd::Util::remove_pidfile( MogileFS->config("pidfile") ) if MogileFS->config("pidfile");
> +    Mgd::log('info', 'ending run');
> +    closelog();
>      exit 0;
>  };
>  $SIG{PIPE} = 'IGNORE';  # catch them by hand
> === trunk/server/lib/MogileFS/Util.pm
> ==================================================================
> --- trunk/server/lib/MogileFS/Util.pm	(revision 68700)
> +++ trunk/server/lib/MogileFS/Util.pm	(local)
> @@ -104,6 +104,28 @@
>      open(STDERR, "+>&STDIN");
>  }
>
> +sub write_pidfile {
> +    my $pidfile = shift;
> +
> +    if (!open(PIDFILE, ">$pidfile")) {
> +        Mgd::log('err', "couldn't create pidfile '$pidfile': $!");
> +        return 0;
> +    }
> +    if (!(print PIDFILE $$, "\n") || !close(PIDFILE)) {
> +        Mgd::log('err', "couldn't write into pidfile '$pidfile': $!");
> +        remove_pidfile($pidfile);
> +        return 0;
> +    }
> +
> +    return 1;
> +}
> +
> +sub remove_pidfile {
> +    my $pidfile = shift;
> +
> +    unlink $pidfile;
> +}
> +
>  # input:
>  #   given an array of arrayrefs of [ item, weight ], returns weighted randomized
>  #   list of items (without the weights, not arrayref; just list)
> === trunk/server/lib/MogileFS/Config.pm
> ==================================================================
> --- trunk/server/lib/MogileFS/Config.pm	(revision 68700)
> +++ trunk/server/lib/MogileFS/Config.pm	(local)
> @@ -59,6 +59,7 @@
>      $max_disk_age,
>      $node_timeout,          # time in seconds to wait for storage node responses
>      $old_repl_compat,
> +    $pidfile,
>     );
>
>  our $default_mindevcount;
> @@ -88,6 +89,7 @@
>                               'node_timeout=i' => \$cmdline{node_timeout},
>                               'no_schema_check' => \$cmdline{no_schema_check},
>                               'old_repl_compat=i' => \$cmdline{old_repl_compat},
> +                             'pidfile=s'     => \$cmdline{pidfile},
>                               );
>
>      # warn of old/deprecated options
> @@ -150,6 +152,8 @@
>
>      $old_repl_compat = choose_value( 'old_repl_compat', 1 );
>
> +    $pidfile        = choose_value( 'pidfile', undef );
> +
>      choose_value( 'no_schema_check', 0 );
>
>      # now load plugins
>
>


More information about the mogilefs mailing list