本文整理汇总了C++中VIR_FORCE_FCLOSE函数的典型用法代码示例。如果您正苦于以下问题:C++ VIR_FORCE_FCLOSE函数的具体用法?C++ VIR_FORCE_FCLOSE怎么用?C++ VIR_FORCE_FCLOSE使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了VIR_FORCE_FCLOSE函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: virCgroupDetectPlacement
/*
* Process /proc/self/cgroup figuring out what cgroup
* sub-path the current process is assigned to. ie not
* neccessarily in the root
*/
static int virCgroupDetectPlacement(virCgroupPtr group)
{
int i;
FILE *mapping = NULL;
char line[1024];
mapping = fopen("/proc/self/cgroup", "r");
if (mapping == NULL) {
VIR_ERROR(_("Unable to open /proc/self/cgroup"));
return -ENOENT;
}
while (fgets(line, sizeof(line), mapping) != NULL) {
char *controllers = strchr(line, ':');
char *path = controllers ? strchr(controllers+1, ':') : NULL;
char *nl = path ? strchr(path, '\n') : NULL;
if (!controllers || !path)
continue;
if (nl)
*nl = '\0';
*path = '\0';
controllers++;
path++;
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
const char *typestr = virCgroupControllerTypeToString(i);
int typelen = strlen(typestr);
char *tmp = controllers;
while (tmp) {
char *next = strchr(tmp, ',');
int len;
if (next) {
len = next-tmp;
next++;
} else {
len = strlen(tmp);
}
if (typelen == len && STREQLEN(typestr, tmp, len) &&
!(group->controllers[i].placement = strdup(STREQ(path, "/") ? "" : path)))
goto no_memory;
tmp = next;
}
}
}
VIR_FORCE_FCLOSE(mapping);
return 0;
no_memory:
VIR_FORCE_FCLOSE(mapping);
return -ENOMEM;
}
示例2: virtTestLoadFile
/* Allocate BUF to the size of FILE. Read FILE into buffer BUF.
Upon any failure, diagnose it and return -1, but don't bother trying
to preserve errno. Otherwise, return the number of bytes copied into BUF. */
int
virtTestLoadFile(const char *file, char **buf)
{
FILE *fp = fopen(file, "r");
struct stat st;
char *tmp;
int len, tmplen, buflen;
if (!fp) {
fprintf(stderr, "%s: failed to open: %s\n", file, strerror(errno));
return -1;
}
if (fstat(fileno(fp), &st) < 0) {
fprintf(stderr, "%s: failed to fstat: %s\n", file, strerror(errno));
VIR_FORCE_FCLOSE(fp);
return -1;
}
tmplen = buflen = st.st_size + 1;
if (VIR_ALLOC_N(*buf, buflen) < 0) {
fprintf(stderr, "%s: larger than available memory (> %d)\n", file, buflen);
VIR_FORCE_FCLOSE(fp);
return -1;
}
tmp = *buf;
(*buf)[0] = '\0';
if (st.st_size) {
/* read the file line by line */
while (fgets(tmp, tmplen, fp) != NULL) {
len = strlen(tmp);
/* stop on an empty line */
if (len == 0)
break;
/* remove trailing backslash-newline pair */
if (len >= 2 && tmp[len-2] == '\\' && tmp[len-1] == '\n') {
len -= 2;
tmp[len] = '\0';
}
/* advance the temporary buffer pointer */
tmp += len;
tmplen -= len;
}
if (ferror(fp)) {
fprintf(stderr, "%s: read failed: %s\n", file, strerror(errno));
VIR_FORCE_FCLOSE(fp);
VIR_FREE(*buf);
return -1;
}
}
VIR_FORCE_FCLOSE(fp);
return strlen(*buf);
}
示例3: linuxTestCompareFiles
static int
linuxTestCompareFiles(const char *cpuinfofile,
char *sysfs_dir,
virArch arch,
const char *outputfile)
{
int ret = -1;
char *actualData = NULL;
char *expectData = NULL;
virNodeInfo nodeinfo;
FILE *cpuinfo;
if (virtTestLoadFile(outputfile, &expectData) < 0)
goto fail;
cpuinfo = fopen(cpuinfofile, "r");
if (!cpuinfo) {
fprintf(stderr, "unable to open: %s : %s\n",
cpuinfofile, strerror(errno));
goto fail;
}
memset(&nodeinfo, 0, sizeof(nodeinfo));
if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, arch, &nodeinfo) < 0) {
if (virTestGetDebug()) {
virErrorPtr error = virSaveLastError();
if (error && error->code != VIR_ERR_OK)
fprintf(stderr, "\n%s\n", error->message);
virFreeError(error);
}
VIR_FORCE_FCLOSE(cpuinfo);
goto fail;
}
VIR_FORCE_FCLOSE(cpuinfo);
if (virAsprintf(&actualData,
"CPUs: %u/%u, MHz: %u, Nodes: %u, Sockets: %u, "
"Cores: %u, Threads: %u\n",
nodeinfo.cpus, VIR_NODEINFO_MAXCPUS(nodeinfo),
nodeinfo.mhz, nodeinfo.nodes, nodeinfo.sockets,
nodeinfo.cores, nodeinfo.threads) < 0)
goto fail;
if (STRNEQ(actualData, expectData)) {
virtTestDifference(stderr, expectData, actualData);
goto fail;
}
ret = 0;
fail:
VIR_FREE(expectData);
VIR_FREE(actualData);
return ret;
}
示例4: testCompareFiles
static int
testCompareFiles(virArch hostmachine, const char *xml_rel,
const char *cpuinfo_rel, const char *capabilities_rel)
{
char *expectxml = NULL;
char *actualxml = NULL;
FILE *fp1 = NULL, *fp2 = NULL;
virCapsPtr caps = NULL;
int ret = -1;
char *xml = NULL;
char *cpuinfo = NULL;
char *capabilities = NULL;
if (virAsprintf(&xml, "%s/%s", abs_srcdir, xml_rel) < 0 ||
virAsprintf(&cpuinfo, "%s/%s", abs_srcdir, cpuinfo_rel) < 0 ||
virAsprintf(&capabilities, "%s/%s", abs_srcdir, capabilities_rel) < 0)
goto fail;
if (virtTestLoadFile(xml, &expectxml) < 0)
goto fail;
if (!(fp1 = fopen(cpuinfo, "r")))
goto fail;
if (!(fp2 = fopen(capabilities, "r")))
goto fail;
if (!(caps = xenHypervisorMakeCapabilitiesInternal(NULL, hostmachine, fp1, fp2)))
goto fail;
if (!(actualxml = virCapabilitiesFormatXML(caps)))
goto fail;
if (STRNEQ(expectxml, actualxml)) {
virtTestDifference(stderr, expectxml, actualxml);
goto fail;
}
ret = 0;
fail:
VIR_FREE(expectxml);
VIR_FREE(actualxml);
VIR_FREE(xml);
VIR_FREE(cpuinfo);
VIR_FREE(capabilities);
VIR_FORCE_FCLOSE(fp1);
VIR_FORCE_FCLOSE(fp2);
virObjectUnref(caps);
return ret;
}
示例5: testCompareFiles
static int testCompareFiles(const char *hostmachine,
const char *xml_rel,
const char *cpuinfo_rel,
const char *capabilities_rel) {
char xmlData[MAX_FILE];
char *expectxml = &(xmlData[0]);
char *actualxml = NULL;
FILE *fp1 = NULL, *fp2 = NULL;
virCapsPtr caps = NULL;
int ret = -1;
char xml[PATH_MAX];
char cpuinfo[PATH_MAX];
char capabilities[PATH_MAX];
snprintf(xml, sizeof xml - 1, "%s/%s",
abs_srcdir, xml_rel);
snprintf(cpuinfo, sizeof cpuinfo - 1, "%s/%s",
abs_srcdir, cpuinfo_rel);
snprintf(capabilities, sizeof capabilities - 1, "%s/%s",
abs_srcdir, capabilities_rel);
if (virtTestLoadFile(xml, &expectxml, MAX_FILE) < 0)
goto fail;
if (!(fp1 = fopen(cpuinfo, "r")))
goto fail;
if (!(fp2 = fopen(capabilities, "r")))
goto fail;
if (!(caps = xenHypervisorMakeCapabilitiesInternal(NULL, hostmachine, fp1, fp2)))
goto fail;
if (!(actualxml = virCapabilitiesFormatXML(caps)))
goto fail;
if (STRNEQ(expectxml, actualxml)) {
virtTestDifference(stderr, expectxml, actualxml);
goto fail;
}
ret = 0;
fail:
free(actualxml);
VIR_FORCE_FCLOSE(fp1);
VIR_FORCE_FCLOSE(fp2);
virCapabilitiesFree(caps);
return ret;
}
示例6: virCgroupDetectMounts
/*
* Process /proc/mounts figuring out what controllers are
* mounted and where
*/
static int virCgroupDetectMounts(virCgroupPtr group)
{
int i;
FILE *mounts = NULL;
struct mntent entry;
char buf[CGROUP_MAX_VAL];
mounts = fopen("/proc/mounts", "r");
if (mounts == NULL) {
VIR_ERROR(_("Unable to open /proc/mounts"));
return -ENOENT;
}
while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
if (STRNEQ(entry.mnt_type, "cgroup"))
continue;
for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
const char *typestr = virCgroupControllerTypeToString(i);
int typelen = strlen(typestr);
char *tmp = entry.mnt_opts;
while (tmp) {
char *next = strchr(tmp, ',');
int len;
if (next) {
len = next-tmp;
next++;
} else {
len = strlen(tmp);
}
/* NB, the same controller can appear >1 time in mount list
* due to bind mounts from one location to another. Pick the
* first entry only
*/
if (typelen == len && STREQLEN(typestr, tmp, len) &&
!group->controllers[i].mountPoint &&
!(group->controllers[i].mountPoint = strdup(entry.mnt_dir)))
goto no_memory;
tmp = next;
}
}
}
VIR_FORCE_FCLOSE(mounts);
return 0;
no_memory:
VIR_FORCE_FCLOSE(mounts);
return -ENOMEM;
}
示例7: virStorageBackendVzIsMounted
static int
virStorageBackendVzIsMounted(virStoragePoolObjPtr pool)
{
int ret = -1;
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
FILE *mtab;
struct mntent ent;
char buf[1024];
VIR_AUTOFREE(char *) cluster = NULL;
if (virAsprintf(&cluster, "vstorage://%s", def->source.name) < 0)
return -1;
if ((mtab = fopen(_PATH_MOUNTED, "r")) == NULL) {
virReportSystemError(errno,
_("cannot read mount list '%s'"),
_PATH_MOUNTED);
goto cleanup;
}
while ((getmntent_r(mtab, &ent, buf, sizeof(buf))) != NULL) {
if (STREQ(ent.mnt_dir, def->target.path) &&
STREQ(ent.mnt_fsname, cluster)) {
ret = 1;
goto cleanup;
}
}
ret = 0;
cleanup:
VIR_FORCE_FCLOSE(mtab);
return ret;
}
示例8: hostsfileWrite
static int
hostsfileWrite(const char *path,
dnsmasqDhcpHost *hosts,
unsigned int nhosts)
{
char *tmp;
FILE *f;
bool istmp = true;
unsigned int i;
int rc = 0;
if (nhosts == 0)
return rc;
if (virAsprintf(&tmp, "%s.new", path) < 0)
return ENOMEM;
if (!(f = fopen(tmp, "w"))) {
istmp = false;
if (!(f = fopen(path, "w"))) {
rc = errno;
goto cleanup;
}
}
for (i = 0; i < nhosts; i++) {
if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) {
rc = errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
goto cleanup;
}
}
if (VIR_FCLOSE(f) == EOF) {
rc = errno;
goto cleanup;
}
if (istmp) {
if (rename(tmp, path) < 0) {
rc = errno;
unlink(tmp);
goto cleanup;
}
if (unlink(tmp) < 0) {
rc = errno;
goto cleanup;
}
}
cleanup:
VIR_FREE(tmp);
return rc;
}
示例9: openvzWriteConfigParam
static int
openvzWriteConfigParam(const char * conf_file, const char *param, const char *value)
{
char * temp_file = NULL;
int temp_fd = -1;
FILE *fp;
char *line = NULL;
size_t line_size = 0;
if (virAsprintf(&temp_file, "%s.tmp", conf_file)<0) {
virReportOOMError();
return -1;
}
fp = fopen(conf_file, "r");
if (fp == NULL)
goto error;
temp_fd = open(temp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (temp_fd == -1) {
goto error;
}
while (1) {
if (getline(&line, &line_size, fp) <= 0)
break;
if (!(STRPREFIX(line, param) && line[strlen(param)] == '=')) {
if (safewrite(temp_fd, line, strlen(line)) !=
strlen(line))
goto error;
}
}
if (safewrite(temp_fd, param, strlen(param)) < 0 ||
safewrite(temp_fd, "=\"", 2) < 0 ||
safewrite(temp_fd, value, strlen(value)) < 0 ||
safewrite(temp_fd, "\"\n", 2) < 0)
goto error;
if (VIR_FCLOSE(fp) < 0)
goto error;
if (VIR_CLOSE(temp_fd) < 0)
goto error;
if (rename(temp_file, conf_file) < 0)
goto error;
VIR_FREE(line);
return 0;
error:
VIR_FREE(line);
VIR_FORCE_FCLOSE(fp);
VIR_FORCE_CLOSE(temp_fd);
if (temp_file)
unlink(temp_file);
VIR_FREE(temp_file);
return -1;
}
示例10: nodeGetInfo
int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo)
{
virArch hostarch = virArchFromHost();
if (virStrcpyStatic(nodeinfo->model, virArchToString(hostarch)) == NULL)
return -1;
#ifdef __linux__
{
int ret = -1;
FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
if (!cpuinfo) {
virReportSystemError(errno,
_("cannot open %s"), CPUINFO_PATH);
return -1;
}
ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH, nodeinfo);
if (ret < 0)
goto cleanup;
/* Convert to KB. */
nodeinfo->memory = physmem_total() / 1024;
cleanup:
VIR_FORCE_FCLOSE(cpuinfo);
return ret;
}
#else
/* XXX Solaris will need an impl later if they port QEMU driver */
virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("node info not implemented on this platform"));
return -1;
#endif
}
示例11: nodeGetInfo
int nodeGetInfo(virConnectPtr conn ATTRIBUTE_UNUSED, virNodeInfoPtr nodeinfo) {
struct utsname info;
memset(nodeinfo, 0, sizeof(*nodeinfo));
uname(&info);
if (virStrcpyStatic(nodeinfo->model, info.machine) == NULL)
return -1;
#ifdef __linux__
{
int ret;
FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
if (!cpuinfo) {
virReportSystemError(errno,
_("cannot open %s"), CPUINFO_PATH);
return -1;
}
ret = linuxNodeInfoCPUPopulate(cpuinfo, nodeinfo, true);
VIR_FORCE_FCLOSE(cpuinfo);
if (ret < 0)
return -1;
/* Convert to KB. */
nodeinfo->memory = physmem_total () / 1024;
return ret;
}
#else
/* XXX Solaris will need an impl later if they port QEMU driver */
nodeReportError(VIR_ERR_NO_SUPPORT, "%s",
_("node info not implemented on this platform"));
return -1;
#endif
}
示例12: getDeviceType
/* Function to check if the type file in the given sysfs_path is a
* Direct-Access device (i.e. type 0). Return -1 on failure, type of
* the device otherwise.
*/
static int
getDeviceType(uint32_t host,
uint32_t bus,
uint32_t target,
uint32_t lun,
int *type)
{
char *type_path = NULL;
char typestr[3];
char *gottype, *p;
FILE *typefile;
int retval = 0;
if (virAsprintf(&type_path, "/sys/bus/scsi/devices/%u:%u:%u:%u/type",
host, bus, target, lun) < 0) {
virReportOOMError();
goto out;
}
typefile = fopen(type_path, "r");
if (typefile == NULL) {
virReportSystemError(errno,
_("Could not find typefile '%s'"),
type_path);
/* there was no type file; that doesn't seem right */
retval = -1;
goto out;
}
gottype = fgets(typestr, 3, typefile);
VIR_FORCE_FCLOSE(typefile);
if (gottype == NULL) {
virReportSystemError(errno,
_("Could not read typefile '%s'"),
type_path);
/* we couldn't read the type file; have to give up */
retval = -1;
goto out;
}
/* we don't actually care about p, but if you pass NULL and the last
* character is not \0, virStrToLong_i complains
*/
if (virStrToLong_i(typestr, &p, 10, type) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Device type '%s' is not an integer"),
typestr);
/* Hm, type wasn't an integer; seems strange */
retval = -1;
goto out;
}
VIR_DEBUG("Device type is %d", *type);
out:
VIR_FREE(type_path);
return retval;
}
示例13: hostsfileWrite
static int
hostsfileWrite(const char *path,
dnsmasqDhcpHost *hosts,
unsigned int nhosts)
{
char *tmp, *content = NULL;
FILE *f;
bool istmp = true;
int rc = 0;
/* even if there are 0 hosts, create a 0 length file, to allow
* for runtime addition.
*/
if (virAsprintf(&tmp, "%s.new", path) < 0)
return -ENOMEM;
if (!(f = fopen(tmp, "w"))) {
istmp = false;
if (!(f = fopen(path, "w"))) {
rc = -errno;
goto cleanup;
}
}
if (!(content = dnsmasqDhcpHostsToString(hosts, nhosts))) {
rc = -ENOMEM;
goto cleanup;
}
if (fputs(content, f) == EOF) {
rc = -errno;
VIR_FORCE_FCLOSE(f);
if (istmp)
unlink(tmp);
goto cleanup;
}
if (VIR_FCLOSE(f) == EOF) {
rc = -errno;
goto cleanup;
}
if (istmp && rename(tmp, path) < 0) {
rc = -errno;
unlink(tmp);
goto cleanup;
}
cleanup:
VIR_FREE(content);
VIR_FREE(tmp);
return rc;
}
示例14: linuxCPUStatsCompareFiles
static int
linuxCPUStatsCompareFiles(const char *cpustatfile,
size_t ncpus,
const char *outfile)
{
int ret = -1;
char *actualData = NULL;
FILE *cpustat = NULL;
virNodeCPUStatsPtr params = NULL;
virBuffer buf = VIR_BUFFER_INITIALIZER;
size_t i;
int nparams = 0;
if (!(cpustat = fopen(cpustatfile, "r"))) {
virReportSystemError(errno, "failed to open '%s': ", cpustatfile);
goto fail;
}
if (linuxNodeGetCPUStats(NULL, 0, NULL, &nparams) < 0)
goto fail;
if (VIR_ALLOC_N(params, nparams) < 0)
goto fail;
if (linuxNodeGetCPUStats(cpustat, VIR_NODE_CPU_STATS_ALL_CPUS, params,
&nparams) < 0)
goto fail;
if (linuxCPUStatsToBuf(&buf, VIR_NODE_CPU_STATS_ALL_CPUS,
params, nparams) < 0)
goto fail;
for (i = 0; i < ncpus; i++) {
if (linuxNodeGetCPUStats(cpustat, i, params, &nparams) < 0)
goto fail;
if (linuxCPUStatsToBuf(&buf, i, params, nparams) < 0)
goto fail;
}
if (!(actualData = virBufferContentAndReset(&buf))) {
virReportOOMError();
goto fail;
}
if (virtTestCompareToFile(actualData, outfile) < 0)
goto fail;
ret = 0;
fail:
virBufferFreeAndReset(&buf);
VIR_FORCE_FCLOSE(cpustat);
VIR_FREE(actualData);
VIR_FREE(params);
return ret;
}
示例15: openvz_copyfile
static int
openvz_copyfile(char* from_path, char* to_path)
{
char *line = NULL;
size_t line_size = 0;
FILE *fp;
int copy_fd;
int bytes_read;
fp = fopen(from_path, "r");
if (fp == NULL)
return -1;
copy_fd = open(to_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (copy_fd == -1) {
VIR_FORCE_FCLOSE(fp);
return -1;
}
while (1) {
if (getline(&line, &line_size, fp) <= 0)
break;
bytes_read = strlen(line);
if (safewrite(copy_fd, line, bytes_read) != bytes_read)
goto error;
}
if (VIR_FCLOSE(fp) < 0)
goto error;
if (VIR_CLOSE(copy_fd) < 0)
goto error;
VIR_FREE(line);
return 0;
error:
VIR_FREE(line);
VIR_FORCE_FCLOSE(fp);
VIR_FORCE_CLOSE(copy_fd);
return -1;
}