summaryrefslogtreecommitdiffstats
path: root/lib/pid_output.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pid_output.c')
-rw-r--r--lib/pid_output.c98
1 files changed, 61 insertions, 37 deletions
diff --git a/lib/pid_output.c b/lib/pid_output.c
index 5261babc..df1862bb 100644
--- a/lib/pid_output.c
+++ b/lib/pid_output.c
@@ -17,7 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * 02111-1307, USA.
*/
#include <zebra.h>
@@ -39,7 +39,7 @@ pid_output (const char *path)
oldumask = umask(0777 & ~PIDFILE_MASK);
fp = fopen (path, "w");
- if (fp != NULL)
+ if (fp != NULL)
{
fprintf (fp, "%d\n", (int) pid);
fclose (fp);
@@ -49,7 +49,7 @@ pid_output (const char *path)
/* XXX Why do we continue instead of exiting? This seems incompatible
with the behavior of the fcntl version below. */
zlog_warn("Can't fopen pid lock file %s (%s), continuing",
- path, safe_strerror(errno));
+ path, errtoa(errno, 0).str);
umask(oldumask);
return -1;
}
@@ -59,50 +59,74 @@ pid_output (const char *path)
pid_t
pid_output (const char *path)
{
- int tmp;
- int fd;
- pid_t pid;
- char buf[16];
- struct flock lock;
- mode_t oldumask;
+ const char* fail ;
+ int err ;
+
+ pid_t pid ;
+ mode_t oldumask ;
+ int fd ;
+ struct flock lock ;
+ char buf[32] ;
+ size_t pidsize ;
pid = getpid ();
oldumask = umask(0777 & ~PIDFILE_MASK);
- fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK);
+
+ fail = "Failed to open pid lock file '%s' for pid %d (%s)" ;
+ fd = open (path, O_RDWR | O_CREAT, PIDFILE_MASK) ;
+ err = errno ;
+
+ umask(oldumask);
+
if (fd < 0)
- {
- zlog_err("Can't create pid lock file %s (%s), exiting",
- path, safe_strerror(errno));
- umask(oldumask);
- exit(1);
- }
- else
- {
- size_t pidsize;
+ goto failed_err ;
- umask(oldumask);
- memset (&lock, 0, sizeof(lock));
+ memset (&lock, 0, sizeof(lock));
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
- lock.l_type = F_WRLCK;
- lock.l_whence = SEEK_SET;
+ if (fcntl(fd, F_SETLK, &lock) < 0)
+ {
+ fail = "Failed to write lock pid lock file '%s' for pid %d (%s)" ;
+ err = errno ;
- if (fcntl(fd, F_SETLK, &lock) < 0)
+ if ((err == EACCES) || (err == EAGAIN))
{
- zlog_err("Could not lock pid_file %s, exiting", path);
- exit(1);
- }
-
- sprintf (buf, "%d\n", (int) pid);
- pidsize = strlen(buf);
- if ((tmp = write (fd, buf, pidsize)) != (int)pidsize)
- zlog_err("Could not write pid %d to pid_file %s, rc was %d: %s",
- (int)pid,path,tmp,safe_strerror(errno));
- else if (ftruncate(fd, pidsize) < 0)
- zlog_err("Could not truncate pid_file %s to %u bytes: %s",
- path,(u_int)pidsize,safe_strerror(errno));
- }
+ fail = "Failed to write lock pid lock file '%s', "
+ "blocked by pid %d (%s)" ;
+ fcntl(fd, F_GETLK, &lock) ;
+ pid = lock.l_pid ;
+ } ;
+
+ goto failed_err ;
+ } ;
+
+ pidsize = sprintf (buf, "%d\n", (int)pid) ;
+
+ fail = "Failed to write pid to pid lock file '%s' for pid %d (%s)" ;
+ if (write(fd, buf, pidsize) != (ssize_t)pidsize)
+ goto failed ;
+
+ fail = "Failed to truncate pid lock file '%s' to length for pid %d (%s)" ;
+ if (ftruncate(fd, pidsize) < 0)
+ goto failed ;
+
+ fail = "Failed to fsync pid lock file '%s' for pid %d (%s)" ;
+ if (fsync(fd) < 0)
+ goto failed ;
+
return pid;
+
+failed:
+ err = errno ;
+
+failed_err:
+ zlog_err(fail, path, (int)pid, errtoa(err, 0).str) ;
+ fprintf(stderr, fail, path, (int)pid, errtoa(err, 0).str) ;
+ fprintf(stderr, "\n") ;
+
+ exit(1) ;
}
#endif /* HAVE_FCNTL */