2014-01-18 12:10:20 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2014-01-18 12:10:20 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_PID_FILE_HXX
|
|
|
|
#define MPD_PID_FILE_HXX
|
|
|
|
|
|
|
|
#include "fs/FileSystem.hxx"
|
|
|
|
#include "fs/AllocatedPath.hxx"
|
2018-08-01 20:20:29 +02:00
|
|
|
#include "system/Error.hxx"
|
2014-01-18 12:10:20 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
2015-09-29 19:39:05 +02:00
|
|
|
#include <string.h>
|
2014-01-18 12:10:20 +01:00
|
|
|
#include <unistd.h>
|
2015-08-24 10:39:51 +02:00
|
|
|
#include <stdlib.h>
|
2015-08-15 16:42:07 +02:00
|
|
|
#include <fcntl.h>
|
2014-01-18 12:10:20 +01:00
|
|
|
|
|
|
|
class PidFile {
|
2015-08-15 16:42:07 +02:00
|
|
|
int fd;
|
2014-01-18 12:10:20 +01:00
|
|
|
|
|
|
|
public:
|
2015-08-15 16:42:07 +02:00
|
|
|
PidFile(const AllocatedPath &path):fd(-1) {
|
2014-01-18 12:10:20 +01:00
|
|
|
if (path.IsNull())
|
|
|
|
return;
|
|
|
|
|
2017-08-10 19:32:17 +02:00
|
|
|
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
|
2015-08-15 16:42:07 +02:00
|
|
|
if (fd < 0) {
|
2014-01-18 12:10:20 +01:00
|
|
|
const std::string utf8 = path.ToUTF8();
|
2018-08-01 20:20:29 +02:00
|
|
|
throw FormatErrno("Failed to create pid file \"%s\"",
|
|
|
|
utf8.c_str());
|
2014-01-18 12:10:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PidFile(const PidFile &) = delete;
|
|
|
|
|
2018-08-01 20:27:56 +02:00
|
|
|
void Close() noexcept {
|
2015-08-15 16:42:07 +02:00
|
|
|
if (fd < 0)
|
2014-01-18 12:01:09 +01:00
|
|
|
return;
|
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
close(fd);
|
2014-01-18 12:01:09 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:27:56 +02:00
|
|
|
void Delete(const AllocatedPath &path) noexcept {
|
2015-08-15 16:42:07 +02:00
|
|
|
if (fd < 0) {
|
2014-01-18 12:01:09 +01:00
|
|
|
assert(path.IsNull());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!path.IsNull());
|
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
close(fd);
|
2016-08-15 22:25:15 +02:00
|
|
|
unlink(path.c_str());
|
2014-01-18 12:01:09 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:27:56 +02:00
|
|
|
void Write(pid_t pid) noexcept {
|
2015-08-15 16:42:07 +02:00
|
|
|
if (fd < 0)
|
2014-01-18 12:10:20 +01:00
|
|
|
return;
|
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
char buffer[64];
|
|
|
|
sprintf(buffer, "%lu\n", (unsigned long)pid);
|
2015-09-29 19:39:05 +02:00
|
|
|
|
|
|
|
write(fd, buffer, strlen(buffer));
|
2015-08-15 16:42:07 +02:00
|
|
|
close(fd);
|
2014-01-18 12:10:20 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:27:56 +02:00
|
|
|
void Write() noexcept {
|
2015-08-15 16:42:07 +02:00
|
|
|
if (fd < 0)
|
2014-01-18 12:10:20 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Write(getpid());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-15 16:39:52 +02:00
|
|
|
gcc_pure
|
|
|
|
static inline pid_t
|
2017-05-08 14:44:49 +02:00
|
|
|
ReadPidFile(Path path) noexcept
|
2015-08-15 16:39:52 +02:00
|
|
|
{
|
2017-08-10 19:32:17 +02:00
|
|
|
auto fd = OpenFile(path, O_RDONLY, 0);
|
|
|
|
if (!fd.IsDefined())
|
2015-08-15 16:39:52 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
pid_t pid = -1;
|
2015-08-15 16:39:52 +02:00
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
char buffer[32];
|
2017-08-10 19:32:17 +02:00
|
|
|
auto nbytes = fd.Read(buffer, sizeof(buffer) - 1);
|
2015-08-15 16:42:07 +02:00
|
|
|
if (nbytes > 0) {
|
|
|
|
buffer[nbytes] = 0;
|
|
|
|
|
|
|
|
char *endptr;
|
|
|
|
auto value = strtoul(buffer, &endptr, 10);
|
|
|
|
if (endptr > buffer)
|
|
|
|
pid = value;
|
|
|
|
}
|
|
|
|
|
2015-08-15 16:39:52 +02:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2014-01-18 12:10:20 +01:00
|
|
|
#endif
|