mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
util: pidfile: use VIR_AUTOFREE instead of VIR_FREE for scalar types
By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
05a8dd36c0
commit
d00fc4178c
@ -97,29 +97,18 @@ int virPidFileWrite(const char *dir,
|
|||||||
const char *name,
|
const char *name,
|
||||||
pid_t pid)
|
pid_t pid)
|
||||||
{
|
{
|
||||||
int rc;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (virFileMakePath(dir) < 0) {
|
if (virFileMakePath(dir) < 0)
|
||||||
rc = -errno;
|
return -errno;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileWritePath(pidfile, pid);
|
return virPidFileWritePath(pidfile, pid);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -170,25 +159,17 @@ int virPidFileRead(const char *dir,
|
|||||||
const char *name,
|
const char *name,
|
||||||
pid_t *pid)
|
pid_t *pid)
|
||||||
{
|
{
|
||||||
int rc;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
*pid = 0;
|
*pid = 0;
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileReadPath(pidfile, pid);
|
return virPidFileReadPath(pidfile, pid);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -219,20 +200,20 @@ int virPidFileReadPathIfAlive(const char *path,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
bool isLink;
|
bool isLink;
|
||||||
char *procPath = NULL;
|
|
||||||
char *procLink = NULL;
|
|
||||||
size_t procLinkLen;
|
size_t procLinkLen;
|
||||||
char *resolvedBinPath = NULL;
|
|
||||||
char *resolvedProcLink = NULL;
|
|
||||||
const char deletedText[] = " (deleted)";
|
const char deletedText[] = " (deleted)";
|
||||||
size_t deletedTextLen = strlen(deletedText);
|
size_t deletedTextLen = strlen(deletedText);
|
||||||
pid_t retPid;
|
pid_t retPid;
|
||||||
|
VIR_AUTOFREE(char *) procPath = NULL;
|
||||||
|
VIR_AUTOFREE(char *) procLink = NULL;
|
||||||
|
VIR_AUTOFREE(char *) resolvedBinPath = NULL;
|
||||||
|
VIR_AUTOFREE(char *) resolvedProcLink = NULL;
|
||||||
|
|
||||||
/* only set this at the very end on success */
|
/* only set this at the very end on success */
|
||||||
*pid = -1;
|
*pid = -1;
|
||||||
|
|
||||||
if ((ret = virPidFileReadPath(path, &retPid)) < 0)
|
if ((ret = virPidFileReadPath(path, &retPid)) < 0)
|
||||||
goto cleanup;
|
return ret;
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
/* Check that it's still alive. Safe to skip this sanity check on
|
/* Check that it's still alive. Safe to skip this sanity check on
|
||||||
@ -252,13 +233,12 @@ int virPidFileReadPathIfAlive(const char *path,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virAsprintf(&procPath, "/proc/%lld/exe", (long long)retPid) < 0) {
|
if (virAsprintf(&procPath, "/proc/%lld/exe", (long long)retPid) < 0)
|
||||||
ret = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((ret = virFileIsLink(procPath)) < 0)
|
if ((ret = virFileIsLink(procPath)) < 0)
|
||||||
goto cleanup;
|
return ret;
|
||||||
|
|
||||||
isLink = ret;
|
isLink = ret;
|
||||||
|
|
||||||
if (isLink && virFileLinkPointsTo(procPath, binPath)) {
|
if (isLink && virFileLinkPointsTo(procPath, binPath)) {
|
||||||
@ -275,27 +255,21 @@ int virPidFileReadPathIfAlive(const char *path,
|
|||||||
* "$procpath (deleted)". Read that link, remove the " (deleted)"
|
* "$procpath (deleted)". Read that link, remove the " (deleted)"
|
||||||
* part, and see if it has the same canonicalized name as binpath.
|
* part, and see if it has the same canonicalized name as binpath.
|
||||||
*/
|
*/
|
||||||
if (!(procLink = areadlink(procPath))) {
|
if (!(procLink = areadlink(procPath)))
|
||||||
ret = -errno;
|
return -errno;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
procLinkLen = strlen(procLink);
|
procLinkLen = strlen(procLink);
|
||||||
if (procLinkLen > deletedTextLen)
|
if (procLinkLen > deletedTextLen)
|
||||||
procLink[procLinkLen - deletedTextLen] = 0;
|
procLink[procLinkLen - deletedTextLen] = 0;
|
||||||
|
|
||||||
if ((ret = virFileResolveAllLinks(binPath, &resolvedBinPath)) < 0)
|
if ((ret = virFileResolveAllLinks(binPath, &resolvedBinPath)) < 0)
|
||||||
goto cleanup;
|
return ret;
|
||||||
if ((ret = virFileResolveAllLinks(procLink, &resolvedProcLink)) < 0)
|
if ((ret = virFileResolveAllLinks(procLink, &resolvedProcLink)) < 0)
|
||||||
goto cleanup;
|
return ret;
|
||||||
|
|
||||||
ret = STREQ(resolvedBinPath, resolvedProcLink) ? 0 : -1;
|
ret = STREQ(resolvedBinPath, resolvedProcLink) ? 0 : -1;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(procPath);
|
|
||||||
VIR_FREE(procLink);
|
|
||||||
VIR_FREE(resolvedProcLink);
|
|
||||||
VIR_FREE(resolvedBinPath);
|
|
||||||
|
|
||||||
/* return the originally set pid of -1 unless we proclaim success */
|
/* return the originally set pid of -1 unless we proclaim success */
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
*pid = retPid;
|
*pid = retPid;
|
||||||
@ -326,24 +300,15 @@ int virPidFileReadIfAlive(const char *dir,
|
|||||||
pid_t *pid,
|
pid_t *pid,
|
||||||
const char *binpath)
|
const char *binpath)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileReadPathIfAlive(pidfile, pid, binpath);
|
return virPidFileReadPathIfAlive(pidfile, pid, binpath);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -361,24 +326,15 @@ int virPidFileDeletePath(const char *pidfile)
|
|||||||
int virPidFileDelete(const char *dir,
|
int virPidFileDelete(const char *dir,
|
||||||
const char *name)
|
const char *name)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileDeletePath(pidfile);
|
return virPidFileDeletePath(pidfile);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int virPidFileAcquirePath(const char *path,
|
int virPidFileAcquirePath(const char *path,
|
||||||
@ -470,24 +426,15 @@ int virPidFileAcquire(const char *dir,
|
|||||||
bool waitForLock,
|
bool waitForLock,
|
||||||
pid_t pid)
|
pid_t pid)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileAcquirePath(pidfile, waitForLock, pid);
|
return virPidFileAcquirePath(pidfile, waitForLock, pid);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -518,24 +465,15 @@ int virPidFileRelease(const char *dir,
|
|||||||
const char *name,
|
const char *name,
|
||||||
int fd)
|
int fd)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
VIR_AUTOFREE(char *) pidfile = NULL;
|
||||||
char *pidfile = NULL;
|
|
||||||
|
|
||||||
if (name == NULL || dir == NULL) {
|
if (name == NULL || dir == NULL)
|
||||||
rc = -EINVAL;
|
return -EINVAL;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(pidfile = virPidFileBuildPath(dir, name))) {
|
if (!(pidfile = virPidFileBuildPath(dir, name)))
|
||||||
rc = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = virPidFileReleasePath(pidfile, fd);
|
return virPidFileReleasePath(pidfile, fd);
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(pidfile);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -545,8 +483,7 @@ virPidFileConstructPath(bool privileged,
|
|||||||
const char *progname,
|
const char *progname,
|
||||||
char **pidfile)
|
char **pidfile)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
VIR_AUTOFREE(char *) rundir = NULL;
|
||||||
char *rundir = NULL;
|
|
||||||
|
|
||||||
if (privileged) {
|
if (privileged) {
|
||||||
/*
|
/*
|
||||||
@ -556,29 +493,26 @@ virPidFileConstructPath(bool privileged,
|
|||||||
if (!statedir) {
|
if (!statedir) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
"%s", _("No statedir specified"));
|
"%s", _("No statedir specified"));
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
if (virAsprintf(pidfile, "%s/run/%s.pid", statedir, progname) < 0)
|
if (virAsprintf(pidfile, "%s/run/%s.pid", statedir, progname) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
if (!(rundir = virGetUserRuntimeDirectory()))
|
if (!(rundir = virGetUserRuntimeDirectory()))
|
||||||
goto cleanup;
|
return -1;
|
||||||
|
|
||||||
if (virFileMakePathWithMode(rundir, 0700) < 0) {
|
if (virFileMakePathWithMode(rundir, 0700) < 0) {
|
||||||
virReportSystemError(errno,
|
virReportSystemError(errno,
|
||||||
_("Cannot create user runtime directory '%s'"),
|
_("Cannot create user runtime directory '%s'"),
|
||||||
rundir);
|
rundir);
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virAsprintf(pidfile, "%s/%s.pid", rundir, progname) < 0)
|
if (virAsprintf(pidfile, "%s/%s.pid", rundir, progname) < 0)
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
return 0;
|
||||||
cleanup:
|
|
||||||
VIR_FREE(rundir);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user