2014-01-18 12:10:20 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
|
|
|
#include "Log.hxx"
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2015-08-15 16:42:07 +02:00
|
|
|
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
|
|
if (fd < 0) {
|
2014-01-18 12:10:20 +01:00
|
|
|
const std::string utf8 = path.ToUTF8();
|
|
|
|
FormatFatalSystemError("Failed to create pid file \"%s\"",
|
2015-03-05 08:09:05 +01:00
|
|
|
utf8.c_str());
|
2014-01-18 12:10:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PidFile(const PidFile &) = delete;
|
|
|
|
|
2014-01-18 12:01:09 +01:00
|
|
|
void Close() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Delete(const AllocatedPath &path) {
|
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
|
|
|
}
|
|
|
|
|
2014-01-18 12:10:20 +01:00
|
|
|
void Write(pid_t pid) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Write() {
|
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
|
|
|
|
ReadPidFile(Path path)
|
|
|
|
{
|
2015-08-15 16:42:07 +02:00
|
|
|
int fd = OpenFile(path, O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
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];
|
|
|
|
auto nbytes = read(fd, buffer, sizeof(buffer) - 1);
|
|
|
|
if (nbytes > 0) {
|
|
|
|
buffer[nbytes] = 0;
|
|
|
|
|
|
|
|
char *endptr;
|
|
|
|
auto value = strtoul(buffer, &endptr, 10);
|
|
|
|
if (endptr > buffer)
|
|
|
|
pid = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2015-08-15 16:39:52 +02:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2014-01-18 12:10:20 +01:00
|
|
|
#endif
|