mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
Move pidfile functions into util/virpidfile.{c,h}
The functions for manipulating pidfiles are in util/util.{c,h}. We will shortly be adding some further pidfile related functions. To avoid further growing util.c, this moves the pidfile related functions into a dedicated virpidfile.{c,h}. The functions are also all renamed to have 'virPidFile' as their name prefix * util/util.h, util/util.c: Remove all pidfile code * util/virpidfile.c, util/virpidfile.h: Add new APIs for pidfile handling. * lxc/lxc_controller.c, lxc/lxc_driver.c, network/bridge_driver.c, qemu/qemu_process.c: Add virpidfile.h include and adapt for API renames
This commit is contained in:
parent
e48427051d
commit
f80a4ed77a
@ -84,6 +84,7 @@ UTIL_SOURCES = \
|
|||||||
util/util.c util/util.h \
|
util/util.c util/util.h \
|
||||||
util/viraudit.c util/viraudit.h \
|
util/viraudit.c util/viraudit.h \
|
||||||
util/virfile.c util/virfile.h \
|
util/virfile.c util/virfile.h \
|
||||||
|
util/virpidfile.c util/virpidfile.h \
|
||||||
util/xml.c util/xml.h \
|
util/xml.c util/xml.h \
|
||||||
util/virterror.c util/virterror_internal.h \
|
util/virterror.c util/virterror_internal.h \
|
||||||
util/virkeycode.c util/virkeycode.h \
|
util/virkeycode.c util/virkeycode.h \
|
||||||
|
@ -1033,7 +1033,6 @@ virEventAddHandle;
|
|||||||
virEventRemoveHandle;
|
virEventRemoveHandle;
|
||||||
virFileAbsPath;
|
virFileAbsPath;
|
||||||
virFileBuildPath;
|
virFileBuildPath;
|
||||||
virFileDeletePid;
|
|
||||||
virFileExists;
|
virFileExists;
|
||||||
virFileFindMountPoint;
|
virFileFindMountPoint;
|
||||||
virFileHasSuffix;
|
virFileHasSuffix;
|
||||||
@ -1044,11 +1043,8 @@ virFileMakePath;
|
|||||||
virFileMatchesNameSuffix;
|
virFileMatchesNameSuffix;
|
||||||
virFileOpenAs;
|
virFileOpenAs;
|
||||||
virFileOpenTty;
|
virFileOpenTty;
|
||||||
virFilePid;
|
|
||||||
virFileReadAll;
|
virFileReadAll;
|
||||||
virFileReadLimFD;
|
virFileReadLimFD;
|
||||||
virFileReadPid;
|
|
||||||
virFileReadPidPath;
|
|
||||||
virFileResolveLink;
|
virFileResolveLink;
|
||||||
virFileSanitizePath;
|
virFileSanitizePath;
|
||||||
virFileStripSuffix;
|
virFileStripSuffix;
|
||||||
@ -1123,6 +1119,16 @@ virFileFclose;
|
|||||||
virFileFdopen;
|
virFileFdopen;
|
||||||
|
|
||||||
|
|
||||||
|
# virpidfile.h
|
||||||
|
virPidFileBuildPath;
|
||||||
|
virPidFileRead;
|
||||||
|
virPidFileReadPath;
|
||||||
|
virPidFileWrite;
|
||||||
|
virPidFileWritePath;
|
||||||
|
virPidFileDelete;
|
||||||
|
virPidFileDeletePath;
|
||||||
|
|
||||||
|
|
||||||
# virterror_internal.h
|
# virterror_internal.h
|
||||||
virDispatchError;
|
virDispatchError;
|
||||||
virErrorMsg;
|
virErrorMsg;
|
||||||
|
@ -56,6 +56,7 @@
|
|||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
|
#include "virpidfile.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_LXC
|
#define VIR_FROM_THIS VIR_FROM_LXC
|
||||||
|
|
||||||
@ -1136,7 +1137,7 @@ int main(int argc, char *argv[])
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
if ((rc = virFileWritePid(LXC_STATE_DIR, name, pid)) < 0) {
|
if ((rc = virPidFileWrite(LXC_STATE_DIR, name, pid)) < 0) {
|
||||||
virReportSystemError(-rc,
|
virReportSystemError(-rc,
|
||||||
_("Unable to write pid file '%s/%s.pid'"),
|
_("Unable to write pid file '%s/%s.pid'"),
|
||||||
LXC_STATE_DIR, name);
|
LXC_STATE_DIR, name);
|
||||||
@ -1179,7 +1180,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (def)
|
if (def)
|
||||||
virFileDeletePid(LXC_STATE_DIR, def->name);
|
virPidFileDelete(LXC_STATE_DIR, def->name);
|
||||||
lxcControllerCleanupInterfaces(nveths, veths);
|
lxcControllerCleanupInterfaces(nveths, veths);
|
||||||
if (sockpath)
|
if (sockpath)
|
||||||
unlink(sockpath);
|
unlink(sockpath);
|
||||||
|
@ -50,6 +50,7 @@
|
|||||||
#include "stats_linux.h"
|
#include "stats_linux.h"
|
||||||
#include "hooks.h"
|
#include "hooks.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
|
#include "virpidfile.h"
|
||||||
#include "fdstream.h"
|
#include "fdstream.h"
|
||||||
#include "domain_audit.h"
|
#include "domain_audit.h"
|
||||||
#include "domain_nwfilter.h"
|
#include "domain_nwfilter.h"
|
||||||
@ -1030,7 +1031,7 @@ static void lxcVmCleanup(lxc_driver_t *driver,
|
|||||||
virEventRemoveHandle(priv->monitorWatch);
|
virEventRemoveHandle(priv->monitorWatch);
|
||||||
VIR_FORCE_CLOSE(priv->monitor);
|
VIR_FORCE_CLOSE(priv->monitor);
|
||||||
|
|
||||||
virFileDeletePid(driver->stateDir, vm->def->name);
|
virPidFileDelete(driver->stateDir, vm->def->name);
|
||||||
virDomainDeleteConfig(driver->stateDir, NULL, vm);
|
virDomainDeleteConfig(driver->stateDir, NULL, vm);
|
||||||
|
|
||||||
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
|
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
|
||||||
@ -1612,7 +1613,7 @@ static int lxcVmStart(virConnectPtr conn,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
/* And get its pid */
|
/* And get its pid */
|
||||||
if ((r = virFileReadPid(driver->stateDir, vm->def->name, &vm->pid)) < 0) {
|
if ((r = virPidFileRead(driver->stateDir, vm->def->name, &vm->pid)) < 0) {
|
||||||
virReportSystemError(-r,
|
virReportSystemError(-r,
|
||||||
_("Failed to read pid file %s/%s.pid"),
|
_("Failed to read pid file %s/%s.pid"),
|
||||||
driver->stateDir, vm->def->name);
|
driver->stateDir, vm->def->name);
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
#include "network_conf.h"
|
#include "network_conf.h"
|
||||||
#include "driver.h"
|
#include "driver.h"
|
||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
|
#include "virpidfile.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
@ -218,7 +219,7 @@ networkFindActiveConfigs(struct network_driver *driver) {
|
|||||||
if (obj->def->ips && (obj->def->nips > 0)) {
|
if (obj->def->ips && (obj->def->nips > 0)) {
|
||||||
char *pidpath, *radvdpidbase;
|
char *pidpath, *radvdpidbase;
|
||||||
|
|
||||||
if (virFileReadPid(NETWORK_PID_DIR, obj->def->name,
|
if (virPidFileRead(NETWORK_PID_DIR, obj->def->name,
|
||||||
&obj->dnsmasqPid) == 0) {
|
&obj->dnsmasqPid) == 0) {
|
||||||
/* Check that it's still alive */
|
/* Check that it's still alive */
|
||||||
if (kill(obj->dnsmasqPid, 0) != 0)
|
if (kill(obj->dnsmasqPid, 0) != 0)
|
||||||
@ -236,7 +237,7 @@ networkFindActiveConfigs(struct network_driver *driver) {
|
|||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (virFileReadPid(NETWORK_PID_DIR, radvdpidbase,
|
if (virPidFileRead(NETWORK_PID_DIR, radvdpidbase,
|
||||||
&obj->radvdPid) == 0) {
|
&obj->radvdPid) == 0) {
|
||||||
/* Check that it's still alive */
|
/* Check that it's still alive */
|
||||||
if (kill(obj->radvdPid, 0) != 0)
|
if (kill(obj->radvdPid, 0) != 0)
|
||||||
@ -728,7 +729,7 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(pidfile = virFilePid(NETWORK_PID_DIR, network->def->name))) {
|
if (!(pidfile = virPidFileBuildPath(NETWORK_PID_DIR, network->def->name))) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
@ -765,7 +766,7 @@ networkStartDhcpDaemon(virNetworkObjPtr network)
|
|||||||
* pid
|
* pid
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ret = virFileReadPid(NETWORK_PID_DIR, network->def->name,
|
ret = virPidFileRead(NETWORK_PID_DIR, network->def->name,
|
||||||
&network->dnsmasqPid);
|
&network->dnsmasqPid);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -818,7 +819,7 @@ networkStartRadvd(virNetworkObjPtr network)
|
|||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (!(pidfile = virFilePid(NETWORK_PID_DIR, radvdpidbase))) {
|
if (!(pidfile = virPidFileBuildPath(NETWORK_PID_DIR, radvdpidbase))) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
@ -885,7 +886,7 @@ networkStartRadvd(virNetworkObjPtr network)
|
|||||||
* a dummy pidfile name - virCommand will create the pidfile we
|
* a dummy pidfile name - virCommand will create the pidfile we
|
||||||
* want to use (this is necessary because radvd's internal
|
* want to use (this is necessary because radvd's internal
|
||||||
* daemonization and pidfile creation causes a race, and the
|
* daemonization and pidfile creation causes a race, and the
|
||||||
* virFileReadPid() below will fail if we use them).
|
* virPidFileRead() below will fail if we use them).
|
||||||
* Unfortunately, it isn't possible to tell radvd to not create
|
* Unfortunately, it isn't possible to tell radvd to not create
|
||||||
* its own pidfile, so we just let it do so, with a slightly
|
* its own pidfile, so we just let it do so, with a slightly
|
||||||
* different name. Unused, but harmless.
|
* different name. Unused, but harmless.
|
||||||
@ -901,7 +902,7 @@ networkStartRadvd(virNetworkObjPtr network)
|
|||||||
if (virCommandRun(cmd, NULL) < 0)
|
if (virCommandRun(cmd, NULL) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (virFileReadPid(NETWORK_PID_DIR, radvdpidbase,
|
if (virPidFileRead(NETWORK_PID_DIR, radvdpidbase,
|
||||||
&network->radvdPid) < 0)
|
&network->radvdPid) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
@ -1919,7 +1920,7 @@ static int networkShutdownNetworkVirtual(struct network_driver *driver,
|
|||||||
if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name))) {
|
if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name))) {
|
||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
} else {
|
} else {
|
||||||
virFileDeletePid(NETWORK_PID_DIR, radvdpidbase);
|
virPidFileDelete(NETWORK_PID_DIR, radvdpidbase);
|
||||||
VIR_FREE(radvdpidbase);
|
VIR_FREE(radvdpidbase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2486,7 +2487,7 @@ static int networkUndefine(virNetworkPtr net) {
|
|||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
virFileDeletePid(NETWORK_PID_DIR, radvdpidbase);
|
virPidFileDelete(NETWORK_PID_DIR, radvdpidbase);
|
||||||
VIR_FREE(radvdpidbase);
|
VIR_FREE(radvdpidbase);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@
|
|||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "hooks.h"
|
#include "hooks.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
|
#include "virpidfile.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "c-ctype.h"
|
#include "c-ctype.h"
|
||||||
#include "nodeinfo.h"
|
#include "nodeinfo.h"
|
||||||
@ -2771,7 +2772,7 @@ int qemuProcessStart(virConnectPtr conn,
|
|||||||
priv->gotShutdown = false;
|
priv->gotShutdown = false;
|
||||||
|
|
||||||
VIR_FREE(priv->pidfile);
|
VIR_FREE(priv->pidfile);
|
||||||
if (!(priv->pidfile = virFilePid(driver->stateDir, vm->def->name))) {
|
if (!(priv->pidfile = virPidFileBuildPath(driver->stateDir, vm->def->name))) {
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
"%s", _("Failed to build pidfile path."));
|
"%s", _("Failed to build pidfile path."));
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -2880,7 +2881,7 @@ int qemuProcessStart(virConnectPtr conn,
|
|||||||
|
|
||||||
/* wait for qemu process to show up */
|
/* wait for qemu process to show up */
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
if (virFileReadPidPath(priv->pidfile, &vm->pid) < 0) {
|
if (virPidFileReadPath(priv->pidfile, &vm->pid) < 0) {
|
||||||
qemuReportError(VIR_ERR_INTERNAL_ERROR,
|
qemuReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("Domain %s didn't show up"), vm->def->name);
|
_("Domain %s didn't show up"), vm->def->name);
|
||||||
ret = -1;
|
ret = -1;
|
||||||
|
@ -39,6 +39,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "virfile.h"
|
#include "virfile.h"
|
||||||
|
#include "virpidfile.h"
|
||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
#include "ignore-value.h"
|
#include "ignore-value.h"
|
||||||
#include "verify.h"
|
#include "verify.h"
|
||||||
@ -493,7 +494,7 @@ virExecWithHook(const char *const*argv,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
if (pidfile && (virFileWritePidPath(pidfile,pid) < 0)) {
|
if (pidfile && (virPidFileWritePath(pidfile,pid) < 0)) {
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
usleep(500*1000);
|
usleep(500*1000);
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
|
152
src/util/util.c
152
src/util/util.c
@ -1151,158 +1151,6 @@ int virFileOpenTtyAt(const char *ptmx ATTRIBUTE_UNUSED,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char* virFilePid(const char *dir, const char* name)
|
|
||||||
{
|
|
||||||
char *pidfile;
|
|
||||||
if (virAsprintf(&pidfile, "%s/%s.pid", dir, name) < 0)
|
|
||||||
return NULL;
|
|
||||||
return pidfile;
|
|
||||||
}
|
|
||||||
|
|
||||||
int virFileWritePid(const char *dir,
|
|
||||||
const char *name,
|
|
||||||
pid_t pid)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
|
||||||
rc = -EINVAL;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (virFileMakePath(dir) < 0) {
|
|
||||||
rc = -errno;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virFilePid(dir, name))) {
|
|
||||||
rc = -ENOMEM;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virFileWritePidPath(pidfile, pid);
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
int virFileWritePidPath(const char *pidfile,
|
|
||||||
pid_t pid)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
int fd;
|
|
||||||
FILE *file = NULL;
|
|
||||||
|
|
||||||
if ((fd = open(pidfile,
|
|
||||||
O_WRONLY | O_CREAT | O_TRUNC,
|
|
||||||
S_IRUSR | S_IWUSR)) < 0) {
|
|
||||||
rc = -errno;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(file = VIR_FDOPEN(fd, "w"))) {
|
|
||||||
rc = -errno;
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fprintf(file, "%d", pid) < 0) {
|
|
||||||
rc = -errno;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (VIR_FCLOSE(file) < 0)
|
|
||||||
rc = -errno;
|
|
||||||
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int virFileReadPidPath(const char *path,
|
|
||||||
pid_t *pid)
|
|
||||||
{
|
|
||||||
FILE *file;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
*pid = 0;
|
|
||||||
|
|
||||||
if (!(file = fopen(path, "r"))) {
|
|
||||||
rc = -errno;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fscanf(file, "%d", pid) != 1) {
|
|
||||||
rc = -EINVAL;
|
|
||||||
VIR_FORCE_FCLOSE(file);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (VIR_FCLOSE(file) < 0) {
|
|
||||||
rc = -errno;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int virFileReadPid(const char *dir,
|
|
||||||
const char *name,
|
|
||||||
pid_t *pid)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
char *pidfile = NULL;
|
|
||||||
*pid = 0;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
|
||||||
rc = -EINVAL;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virFilePid(dir, name))) {
|
|
||||||
rc = -ENOMEM;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virFileReadPidPath(pidfile, pid);
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
int virFileDeletePid(const char *dir,
|
|
||||||
const char *name)
|
|
||||||
{
|
|
||||||
int rc = 0;
|
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
|
||||||
rc = -EINVAL;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virFilePid(dir, name))) {
|
|
||||||
rc = -ENOMEM;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unlink(pidfile) < 0 && errno != ENOENT)
|
|
||||||
rc = -errno;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Creates an absolute path for a potentially relative path.
|
* Creates an absolute path for a potentially relative path.
|
||||||
|
@ -120,21 +120,6 @@ int virFileOpenTtyAt(const char *ptmx,
|
|||||||
char **ttyName,
|
char **ttyName,
|
||||||
int rawmode);
|
int rawmode);
|
||||||
|
|
||||||
char* virFilePid(const char *dir,
|
|
||||||
const char *name);
|
|
||||||
|
|
||||||
int virFileWritePidPath(const char *path,
|
|
||||||
pid_t pid) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
int virFileWritePid(const char *dir,
|
|
||||||
const char *name,
|
|
||||||
pid_t pid) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
int virFileReadPidPath(const char *path,
|
|
||||||
pid_t *pid) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
int virFileReadPid(const char *dir,
|
|
||||||
const char *name,
|
|
||||||
pid_t *pid) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
int virFileDeletePid(const char *dir,
|
|
||||||
const char *name);
|
|
||||||
|
|
||||||
char *virArgvToString(const char *const *argv);
|
char *virArgvToString(const char *const *argv);
|
||||||
|
|
||||||
|
199
src/util/virpidfile.c
Normal file
199
src/util/virpidfile.c
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
/*
|
||||||
|
* virpidfile.c: manipulation of pidfiles
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
||||||
|
* Copyright (C) 2006, 2007 Binary Karma
|
||||||
|
* Copyright (C) 2006 Shuveb Hussain
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include "virpidfile.h"
|
||||||
|
#include "virfile.h"
|
||||||
|
#include "memory.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
|
||||||
|
char *virPidFileBuildPath(const char *dir, const char* name)
|
||||||
|
{
|
||||||
|
char *pidfile;
|
||||||
|
|
||||||
|
if (virAsprintf(&pidfile, "%s/%s.pid", dir, name) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return pidfile;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileWritePath(const char *pidfile,
|
||||||
|
pid_t pid)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
int fd;
|
||||||
|
FILE *file = NULL;
|
||||||
|
|
||||||
|
if ((fd = open(pidfile,
|
||||||
|
O_WRONLY | O_CREAT | O_TRUNC,
|
||||||
|
S_IRUSR | S_IWUSR)) < 0) {
|
||||||
|
rc = -errno;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(file = VIR_FDOPEN(fd, "w"))) {
|
||||||
|
rc = -errno;
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fprintf(file, "%d", pid) < 0) {
|
||||||
|
rc = -errno;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (VIR_FCLOSE(file) < 0)
|
||||||
|
rc = -errno;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileWrite(const char *dir,
|
||||||
|
const char *name,
|
||||||
|
pid_t pid)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
char *pidfile = NULL;
|
||||||
|
|
||||||
|
if (name == NULL || dir == NULL) {
|
||||||
|
rc = -EINVAL;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (virFileMakePath(dir) < 0) {
|
||||||
|
rc = -errno;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
||||||
|
rc = -ENOMEM;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = virPidFileWritePath(pidfile, pid);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(pidfile);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileReadPath(const char *path,
|
||||||
|
pid_t *pid)
|
||||||
|
{
|
||||||
|
FILE *file;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
*pid = 0;
|
||||||
|
|
||||||
|
if (!(file = fopen(path, "r"))) {
|
||||||
|
rc = -errno;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fscanf(file, "%d", pid) != 1) {
|
||||||
|
rc = -EINVAL;
|
||||||
|
VIR_FORCE_FCLOSE(file);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (VIR_FCLOSE(file) < 0) {
|
||||||
|
rc = -errno;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileRead(const char *dir,
|
||||||
|
const char *name,
|
||||||
|
pid_t *pid)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
char *pidfile = NULL;
|
||||||
|
*pid = 0;
|
||||||
|
|
||||||
|
if (name == NULL || dir == NULL) {
|
||||||
|
rc = -EINVAL;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
||||||
|
rc = -ENOMEM;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = virPidFileReadPath(pidfile, pid);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(pidfile);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileDeletePath(const char *pidfile)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
|
if (unlink(pidfile) < 0 && errno != ENOENT)
|
||||||
|
rc = -errno;
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int virPidFileDelete(const char *dir,
|
||||||
|
const char *name)
|
||||||
|
{
|
||||||
|
int rc = 0;
|
||||||
|
char *pidfile = NULL;
|
||||||
|
|
||||||
|
if (name == NULL || dir == NULL) {
|
||||||
|
rc = -EINVAL;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
||||||
|
rc = -ENOMEM;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = virPidFileDeletePath(pidfile);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(pidfile);
|
||||||
|
return rc;
|
||||||
|
}
|
50
src/util/virpidfile.h
Normal file
50
src/util/virpidfile.h
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* virpidfile.h: manipulation of pidfiles
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
||||||
|
* Copyright (C) 2006, 2007 Binary Karma
|
||||||
|
* Copyright (C) 2006 Shuveb Hussain
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library 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
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __VIR_PIDFILE_H__
|
||||||
|
# define __VIR_PIDFILE_H__
|
||||||
|
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include "internal.h"
|
||||||
|
|
||||||
|
char *virPidFileBuildPath(const char *dir,
|
||||||
|
const char *name);
|
||||||
|
|
||||||
|
int virPidFileWritePath(const char *path,
|
||||||
|
pid_t pid) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
int virPidFileWrite(const char *dir,
|
||||||
|
const char *name,
|
||||||
|
pid_t pid) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
int virPidFileReadPath(const char *path,
|
||||||
|
pid_t *pid) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
int virPidFileRead(const char *dir,
|
||||||
|
const char *name,
|
||||||
|
pid_t *pid) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
int virPidFileDeletePath(const char *path);
|
||||||
|
int virPidFileDelete(const char *dir,
|
||||||
|
const char *name);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* __VIR_PIDFILE_H__ */
|
Loading…
Reference in New Issue
Block a user