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
util/util.c util/util.h \
util/viraudit.c util/viraudit.h \
util/virfile.c util/virfile.h \
+ util/virpidfile.c util/virpidfile.h \
util/xml.c util/xml.h \
util/virterror.c util/virterror_internal.h \
util/virkeycode.c util/virkeycode.h \
virEventRemoveHandle;
virFileAbsPath;
virFileBuildPath;
-virFileDeletePid;
virFileExists;
virFileFindMountPoint;
virFileHasSuffix;
virFileMatchesNameSuffix;
virFileOpenAs;
virFileOpenTty;
-virFilePid;
virFileReadAll;
virFileReadLimFD;
-virFileReadPid;
-virFileReadPidPath;
virFileResolveLink;
virFileSanitizePath;
virFileStripSuffix;
virFileFdopen;
+# virpidfile.h
+virPidFileBuildPath;
+virPidFileRead;
+virPidFileReadPath;
+virPidFileWrite;
+virPidFileWritePath;
+virPidFileDelete;
+virPidFileDeletePath;
+
+
# virterror_internal.h
virDispatchError;
virErrorMsg;
#include "memory.h"
#include "util.h"
#include "virfile.h"
+#include "virpidfile.h"
#define VIR_FROM_THIS VIR_FROM_LXC
goto cleanup;
if (pid > 0) {
- if ((rc = virFileWritePid(LXC_STATE_DIR, name, pid)) < 0) {
+ if ((rc = virPidFileWrite(LXC_STATE_DIR, name, pid)) < 0) {
virReportSystemError(-rc,
_("Unable to write pid file '%s/%s.pid'"),
LXC_STATE_DIR, name);
cleanup:
if (def)
- virFileDeletePid(LXC_STATE_DIR, def->name);
+ virPidFileDelete(LXC_STATE_DIR, def->name);
lxcControllerCleanupInterfaces(nveths, veths);
if (sockpath)
unlink(sockpath);
#include "stats_linux.h"
#include "hooks.h"
#include "virfile.h"
+#include "virpidfile.h"
#include "fdstream.h"
#include "domain_audit.h"
#include "domain_nwfilter.h"
virEventRemoveHandle(priv->monitorWatch);
VIR_FORCE_CLOSE(priv->monitor);
- virFileDeletePid(driver->stateDir, vm->def->name);
+ virPidFileDelete(driver->stateDir, vm->def->name);
virDomainDeleteConfig(driver->stateDir, NULL, vm);
virDomainObjSetState(vm, VIR_DOMAIN_SHUTOFF, reason);
goto cleanup;
/* 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,
_("Failed to read pid file %s/%s.pid"),
driver->stateDir, vm->def->name);
#include "network_conf.h"
#include "driver.h"
#include "buf.h"
+#include "virpidfile.h"
#include "util.h"
#include "command.h"
#include "memory.h"
if (obj->def->ips && (obj->def->nips > 0)) {
char *pidpath, *radvdpidbase;
- if (virFileReadPid(NETWORK_PID_DIR, obj->def->name,
+ if (virPidFileRead(NETWORK_PID_DIR, obj->def->name,
&obj->dnsmasqPid) == 0) {
/* Check that it's still alive */
if (kill(obj->dnsmasqPid, 0) != 0)
virReportOOMError();
goto cleanup;
}
- if (virFileReadPid(NETWORK_PID_DIR, radvdpidbase,
+ if (virPidFileRead(NETWORK_PID_DIR, radvdpidbase,
&obj->radvdPid) == 0) {
/* Check that it's still alive */
if (kill(obj->radvdPid, 0) != 0)
goto cleanup;
}
- if (!(pidfile = virFilePid(NETWORK_PID_DIR, network->def->name))) {
+ if (!(pidfile = virPidFileBuildPath(NETWORK_PID_DIR, network->def->name))) {
virReportOOMError();
goto cleanup;
}
* pid
*/
- ret = virFileReadPid(NETWORK_PID_DIR, network->def->name,
+ ret = virPidFileRead(NETWORK_PID_DIR, network->def->name,
&network->dnsmasqPid);
if (ret < 0)
goto cleanup;
virReportOOMError();
goto cleanup;
}
- if (!(pidfile = virFilePid(NETWORK_PID_DIR, radvdpidbase))) {
+ if (!(pidfile = virPidFileBuildPath(NETWORK_PID_DIR, radvdpidbase))) {
virReportOOMError();
goto cleanup;
}
* a dummy pidfile name - virCommand will create the pidfile we
* want to use (this is necessary because radvd's internal
* 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
* its own pidfile, so we just let it do so, with a slightly
* different name. Unused, but harmless.
if (virCommandRun(cmd, NULL) < 0)
goto cleanup;
- if (virFileReadPid(NETWORK_PID_DIR, radvdpidbase,
+ if (virPidFileRead(NETWORK_PID_DIR, radvdpidbase,
&network->radvdPid) < 0)
goto cleanup;
if (!(radvdpidbase = networkRadvdPidfileBasename(network->def->name))) {
virReportOOMError();
} else {
- virFileDeletePid(NETWORK_PID_DIR, radvdpidbase);
+ virPidFileDelete(NETWORK_PID_DIR, radvdpidbase);
VIR_FREE(radvdpidbase);
}
}
virReportOOMError();
goto cleanup;
}
- virFileDeletePid(NETWORK_PID_DIR, radvdpidbase);
+ virPidFileDelete(NETWORK_PID_DIR, radvdpidbase);
VIR_FREE(radvdpidbase);
}
#include "memory.h"
#include "hooks.h"
#include "virfile.h"
+#include "virpidfile.h"
#include "util.h"
#include "c-ctype.h"
#include "nodeinfo.h"
priv->gotShutdown = false;
VIR_FREE(priv->pidfile);
- if (!(priv->pidfile = virFilePid(driver->stateDir, vm->def->name))) {
+ if (!(priv->pidfile = virPidFileBuildPath(driver->stateDir, vm->def->name))) {
virReportSystemError(errno,
"%s", _("Failed to build pidfile path."));
goto cleanup;
/* wait for qemu process to show up */
if (ret == 0) {
- if (virFileReadPidPath(priv->pidfile, &vm->pid) < 0) {
+ if (virPidFileReadPath(priv->pidfile, &vm->pid) < 0) {
qemuReportError(VIR_ERR_INTERNAL_ERROR,
_("Domain %s didn't show up"), vm->def->name);
ret = -1;
#include "util.h"
#include "logging.h"
#include "virfile.h"
+#include "virpidfile.h"
#include "buf.h"
#include "ignore-value.h"
#include "verify.h"
}
if (pid > 0) {
- if (pidfile && (virFileWritePidPath(pidfile,pid) < 0)) {
+ if (pidfile && (virPidFileWritePath(pidfile,pid) < 0)) {
kill(pid, SIGTERM);
usleep(500*1000);
kill(pid, SIGTERM);
}
#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.
char **ttyName,
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);
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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__ */