mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-14 18:27:21 +00:00
2003-10-24 sowmini.varadhan@sun.com
* lib/pid_output.c: (pid_output) call pid_output_lock if we have fcntl(). (pid_output_lock) grab exclusive write lock on pid file, rather than rely on (fragile) exclusive create.
This commit is contained in:
parent
b1809beadd
commit
e92fbaf27e
@ -21,10 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <zebra.h>
|
#include <zebra.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <log.h>
|
||||||
|
|
||||||
|
pid_t pid_output_lock(char *);
|
||||||
|
|
||||||
pid_t
|
pid_t
|
||||||
pid_output (char *path)
|
pid_output (char *path)
|
||||||
{
|
{
|
||||||
|
#ifndef HAVE_FCNTL
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
@ -38,40 +43,44 @@ pid_output (char *path)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return pid;
|
return pid;
|
||||||
|
#else
|
||||||
|
return pid_output_lock(path);
|
||||||
|
#endif /* HAVE_FCNTL */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_FCNTL
|
||||||
pid_t
|
pid_t
|
||||||
pid_output_lock (char *path)
|
pid_output_lock (char *path)
|
||||||
{
|
{
|
||||||
int tmp;
|
int tmp;
|
||||||
int fd;
|
int fd;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
char buf[16], *p;
|
char buf[16];
|
||||||
|
struct flock lock = { .l_type = F_WRLCK,
|
||||||
|
.l_whence = SEEK_END };
|
||||||
|
|
||||||
pid = getpid ();
|
pid = getpid ();
|
||||||
|
|
||||||
fd = open (path, O_RDWR | O_CREAT | O_EXCL, 0644);
|
fd = open (path, O_RDWR | O_CREAT, 0644);
|
||||||
if (fd < 0)
|
|
||||||
{
|
|
||||||
fd = open (path, O_RDONLY);
|
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
fprintf (stderr, "Can't creat pid lock file, exit\n");
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
read (fd, buf, sizeof (buf));
|
zlog_err( "Can't creat pid lock file %s (%s), exit",
|
||||||
if ((p = index (buf, '\n')) != NULL)
|
path, strerror(errno));
|
||||||
*p = 0;
|
|
||||||
fprintf (stderr, "Another process(%s) running, exit\n", buf);
|
|
||||||
}
|
|
||||||
exit (-1);
|
exit (-1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
memset (&lock, 0, sizeof(lock));
|
||||||
|
|
||||||
|
if (fcntl(fd, F_SETLK, &lock) < 0)
|
||||||
|
{
|
||||||
|
zlog_err("Could not lock pid_file %s, exit", path);
|
||||||
|
exit (-1);
|
||||||
|
}
|
||||||
|
|
||||||
sprintf (buf, "%d\n", (int) pid);
|
sprintf (buf, "%d\n", (int) pid);
|
||||||
tmp = write (fd, buf, strlen (buf));
|
tmp = write (fd, buf, strlen (buf));
|
||||||
close (fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
#endif /* HAVE_FCNTL */
|
||||||
|
Loading…
Reference in New Issue
Block a user