static int
networkEnableIpForwarding(void)
{
- return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n");
+ return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n", 0);
}
#define SYSCTL_PATH "/proc/sys"
goto cleanup;
}
- if (virFileWriteStr(field, "1") < 0) {
+ if (virFileWriteStr(field, "1", 0) < 0) {
virReportSystemError(errno,
_("cannot enable %s"), field);
goto cleanup;
goto cleanup;
}
- if (virFileWriteStr(field, "0") < 0) {
+ if (virFileWriteStr(field, "0", 0) < 0) {
virReportSystemError(errno,
_("cannot disable %s"), field);
goto cleanup;
goto cleanup;
}
- if (virFileWriteStr(field, "1") < 0) {
+ if (virFileWriteStr(field, "1", 0) < 0) {
virReportSystemError(errno,
_("cannot enable %s"), field);
goto cleanup;
* bound by the stub.
*/
pciDriverFile(path, sizeof(path), driver, "new_id");
- if (virFileWriteStr(path, dev->id) < 0) {
+ if (virFileWriteStr(path, dev->id, 0) < 0) {
virReportSystemError(errno,
_("Failed to add PCI device ID '%s' to %s"),
dev->id, driver);
* your root filesystem.
*/
pciDeviceFile(path, sizeof(path), dev->name, "driver/unbind");
- if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) {
+ if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to unbind PCI device '%s'"), dev->name);
return -1;
if (!virFileLinkPointsTo(path, drvdir)) {
/* Xen's pciback.ko wants you to use new_slot first */
pciDriverFile(path, sizeof(path), driver, "new_slot");
- if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) {
+ if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to add slot for PCI device '%s' to %s"),
dev->name, driver);
}
pciDriverFile(path, sizeof(path), driver, "bind");
- if (virFileWriteStr(path, dev->name) < 0) {
+ if (virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to bind PCI device '%s' to %s"),
dev->name, driver);
* ID table so that 'drivers_probe' works below.
*/
pciDriverFile(path, sizeof(path), driver, "remove_id");
- if (virFileExists(path) && virFileWriteStr(path, dev->id) < 0) {
+ if (virFileExists(path) && virFileWriteStr(path, dev->id, 0) < 0) {
virReportSystemError(errno,
_("Failed to remove PCI ID '%s' from %s"),
dev->id, driver);
pciDeviceFile(path, sizeof(path), dev->name, "driver");
if (virFileExists(drvdir) && virFileLinkPointsTo(path, drvdir)) {
pciDriverFile(path, sizeof(path), driver, "unbind");
- if (virFileWriteStr(path, dev->name) < 0) {
+ if (virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to bind PCI device '%s' to %s"),
dev->name, driver);
/* Xen's pciback.ko wants you to use remove_slot on the specific device */
pciDriverFile(path, sizeof(path), driver, "remove_slot");
- if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) {
+ if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to remove slot for PCI device '%s' to %s"),
dev->name, driver);
*/
pciDriverFile(path, sizeof(path), driver, "remove_id");
if (!virFileExists(drvdir) || virFileExists(path)) {
- if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name) < 0) {
+ if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
virReportSystemError(errno,
_("Failed to trigger a re-probe for PCI device '%s'"),
dev->name);
return len;
}
-/* Truncate @path and write @str to it.
+/* Truncate @path and write @str to it. If @mode is 0, ensure that
+ @path exists; otherwise, use @mode if @path must be created.
Return 0 for success, nonzero for failure.
Be careful to preserve any errno value upon failure. */
-int virFileWriteStr(const char *path, const char *str)
+int virFileWriteStr(const char *path, const char *str, mode_t mode)
{
int fd;
- if ((fd = open(path, O_WRONLY|O_TRUNC)) == -1)
+ if (mode)
+ fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, mode);
+ else
+ fd = open(path, O_WRONLY|O_TRUNC);
+ if (fd == -1)
return -1;
if (safewrite(fd, str, strlen(str)) < 0) {
- int saved_errno = errno;
VIR_FORCE_CLOSE(fd);
- errno = saved_errno;
return -1;
}