nfs-utils - 2 of 6 - statd - creates a pid file
Subsystems (2)
PATCH 1/1 nfs-utils - 2 of 6 - statd - creates a pid file
This patch added code that will make
statd create a pid file (rpc.statd) in/var/run. This was need to help our rc
script detect the status of stated.
SteveD.
utils/statd/statd.c renamed from utils/statd/statd.c.org +55 -1
@@ -21,6 +21,9 @@#include <grp.h> #include <sys/types.h> #include <fcntl.h> +#include <errno.h> +#include <netdb.h> +#include <sys/wait.h> #include "statd.h" #include "version.h"
@@ -72,6 +75,51 @@ extern void simulator (int, char **);#endif /* +* Create a pid file +*/ +static const char *piddir="/var/run/rpc.statd" ; +static const char *pidfile="/var/run/rpc.statd/rpc.statd.pid" ; +static void +delete_pidfile (void ) +{ +if (unlink (pidfile) <0 ) { +log (L_WARNING ,"Unlinking %s failed: errno %d (%s)\n" , pidfile, + errno,strerror (errno)); + } + +return ; +} +static void +create_pidfile (void ) +{ +FILE *fp; + +if (access (pidfile,F_OK ) ==0 ) { +die ("rpc.statd is already running (check PID in %s)\n" , pidfile); + } + +/* +* A directory is need so the pid file can be remove +* once the privs have been dropped. +*/ +if (mkdir (piddir,0755 ) <0 && errno !=EEXIST ) { +die ("Creating %s directory failed: errno %d (%s)\n" , pidfile, + errno,strerror (errno)); + } +if ((fp =fopen (pidfile,"w" )) ==NULL ) { +die ("Opening %s failed: errno %d (%s)\n" , pidfile, + errno,strerror (errno)); + } +fprintf (fp,"%d" ,getpid ()); +if (fclose (fp) <0 ) { +log (L_WARNING ,"Closing %s failed: errno %d (%s)\n" , pidfile, + errno,strerror (errno)); + } + +return ; +} + +/* * Privilege dropper */ static void
@@ -94,6 +142,10 @@ drop_privs(void)die ("Cannot start - cannot drop privs: getpwnam()\n" ); } +if (chown (piddir, pw->pw_uid, pw->pw_gid) <0 ) { +log (L_WARNING ,"Owner change of '%s' to user '%s' failed: errno %d (%s)\n" , + pidfile, pw->pw_name, errno,strerror (errno)); + }initgroups (pw->pw_name, pw->pw_gid);setgid (pw->pw_gid);
@@ -343,6 +395,8 @@ int main (int argc, char **argv) }log_init (name_p, version_p); +create_pidfile (); +atexit (delete_pidfile);/* Child. */ signal (SIGHUP , killer);
@@ -364,7 +418,7 @@ int main (int argc, char **argv)die ("Startup failed: Please start rpc.statd as root\n" );/* Arm the ****** resolver before chroot() so it doesn't fail -* trying to open /etc/ for the dozenth time +* trying to open /etc/host for the dozenth time */ sethostent (1 );