|
a/src/utils/pathut.cpp |
|
b/src/utils/pathut.cpp |
|
... |
|
... |
811 |
// as we'd like to reset the pid inside the file when we're done, it
|
811 |
// as we'd like to reset the pid inside the file when we're done, it
|
812 |
// would be very difficult to do it right and it's probably best left
|
812 |
// would be very difficult to do it right and it's probably best left
|
813 |
// alone.
|
813 |
// alone.
|
814 |
Pidfile::~Pidfile()
|
814 |
Pidfile::~Pidfile()
|
815 |
{
|
815 |
{
|
816 |
if (m_fd >= 0) {
|
816 |
this->close();
|
817 |
::close(m_fd);
|
|
|
818 |
}
|
|
|
819 |
m_fd = -1;
|
|
|
820 |
}
|
817 |
}
|
821 |
|
818 |
|
822 |
pid_t Pidfile::read_pid()
|
819 |
pid_t Pidfile::read_pid()
|
823 |
{
|
820 |
{
|
824 |
int fd = ::open(m_path.c_str(), O_RDONLY);
|
821 |
int fd = ::open(m_path.c_str(), O_RDONLY);
|
|
... |
|
... |
855 |
lockdata.l_len = 0;
|
852 |
lockdata.l_len = 0;
|
856 |
lockdata.l_type = F_WRLCK;
|
853 |
lockdata.l_type = F_WRLCK;
|
857 |
lockdata.l_whence = SEEK_SET;
|
854 |
lockdata.l_whence = SEEK_SET;
|
858 |
if (fcntl(m_fd, F_SETLK, &lockdata) != 0) {
|
855 |
if (fcntl(m_fd, F_SETLK, &lockdata) != 0) {
|
859 |
int serrno = errno;
|
856 |
int serrno = errno;
|
860 |
(void)::close(m_fd);
|
857 |
this->close()
|
861 |
errno = serrno;
|
858 |
errno = serrno;
|
862 |
m_reason = "fcntl lock failed";
|
859 |
m_reason = "fcntl lock failed";
|
863 |
return -1;
|
860 |
return -1;
|
864 |
}
|
861 |
}
|
865 |
#else
|
862 |
#else
|
|
... |
|
... |
867 |
return 0;
|
864 |
return 0;
|
868 |
#else
|
865 |
#else
|
869 |
int operation = LOCK_EX | LOCK_NB;
|
866 |
int operation = LOCK_EX | LOCK_NB;
|
870 |
if (flock(m_fd, operation) == -1) {
|
867 |
if (flock(m_fd, operation) == -1) {
|
871 |
int serrno = errno;
|
868 |
int serrno = errno;
|
872 |
(void)::close(m_fd);
|
869 |
this->close();
|
873 |
errno = serrno;
|
870 |
errno = serrno;
|
874 |
m_reason = "flock failed";
|
871 |
m_reason = "flock failed";
|
875 |
return -1;
|
872 |
return -1;
|
876 |
}
|
873 |
}
|
877 |
#endif // ! win32
|
874 |
#endif // ! win32
|
878 |
#endif // ! sun
|
875 |
#endif // ! sun
|
879 |
|
876 |
|
880 |
if (ftruncate(m_fd, 0) != 0) {
|
877 |
if (ftruncate(m_fd, 0) != 0) {
|
881 |
/* can't happen [tm] */
|
878 |
/* can't happen [tm] */
|
882 |
int serrno = errno;
|
879 |
int serrno = errno;
|
883 |
(void)::close(m_fd);
|
880 |
this->close();
|
884 |
errno = serrno;
|
881 |
errno = serrno;
|
885 |
m_reason = "ftruncate failed";
|
882 |
m_reason = "ftruncate failed";
|
886 |
return -1;
|
883 |
return -1;
|
887 |
}
|
884 |
}
|
888 |
return 0;
|
885 |
return 0;
|
|
... |
|
... |
913 |
return 0;
|
910 |
return 0;
|
914 |
}
|
911 |
}
|
915 |
|
912 |
|
916 |
int Pidfile::close()
|
913 |
int Pidfile::close()
|
917 |
{
|
914 |
{
|
|
|
915 |
int ret = -1;
|
|
|
916 |
if (m_fd >= 0) {
|
918 |
return ::close(m_fd);
|
917 |
ret = ::close(m_fd);
|
|
|
918 |
m_fd = -1;
|
|
|
919 |
}
|
|
|
920 |
return ret;
|
919 |
}
|
921 |
}
|
920 |
|
922 |
|
921 |
int Pidfile::remove()
|
923 |
int Pidfile::remove()
|
922 |
{
|
924 |
{
|
923 |
return unlink(m_path.c_str());
|
925 |
return unlink(m_path.c_str());
|