-/* $OpenBSD: resolvd.c,v 1.27 2022/09/01 13:24:28 martijn Exp $ */
+/* $OpenBSD: resolvd.c,v 1.28 2022/09/02 09:39:55 florian Exp $ */
/*
* Copyright (c) 2021 Florian Obser <florian@openbsd.org>
* Copyright (c) 2021 Theo de Raadt <deraadt@openbsd.org>
void
regen_resolvconf(char *why)
{
- int i, fd;
+ struct iovec iov[UIO_MAXIOV];
+ int i, fd, len, iovcnt = 0;
linfo("rebuilding: %s", why);
return;
}
+ memset(iov, 0, sizeof(iov));
+
#ifndef SMALL
- if (unwind_running)
- dprintf(fd, "nameserver 127.0.0.1 # resolvd: unwind\n");
+ if (unwind_running) {
+ len = asprintf((char **)&iov[iovcnt].iov_base,
+ "nameserver 127.0.0.1 # resolvd: unwind\n");
+ if (len < 0) {
+ lwarn("asprintf");
+ goto err;
+ }
+ iov[iovcnt++].iov_len = len;
+ }
#endif /* SMALL */
for (i = 0; i < ASR_MAXNS; i++) {
ifnam = if_indextoname(learned[i].if_index,
ifnambuf);
- dprintf(fd, "%snameserver %s # resolvd: %s\n",
+ len = asprintf((char **)&iov[iovcnt].iov_base,
+ "%snameserver %s # resolvd: %s\n",
#ifndef SMALL
unwind_running ? "#" : "",
#else
#endif
learned[i].ip,
ifnam ? ifnam : "");
+ if (len < 0) {
+ lwarn("asprintf");
+ goto err;
+ }
+ iov[iovcnt++].iov_len = len;
}
}
*end = '\0';
if (strstr(line, "# resolvd: "))
continue;
- dprintf(fd, "%s\n", line);
+ len = asprintf((char **)&iov[iovcnt].iov_base, "%s\n",
+ line);
+ if (len < 0) {
+ lwarn("asprintf");
+ goto err;
+ }
+ iov[iovcnt++].iov_len = len;
+ if (iovcnt >= UIO_MAXIOV) {
+ lwarnx("too many user-managed lines");
+ goto err;
+ }
}
free(line);
fclose(fp);
}
+ if (writev(fd, iov, iovcnt) == -1) {
+ lwarn("writev");
+ goto err;
+ }
+
+ if (fsync(fd) == -1) {
+ lwarn("fsync");
+ goto err;
+ }
if (rename(_PATH_RESCONF_NEW, _PATH_RESCONF) == -1)
goto err;
}
newkevent = 1;
- return;
+ goto out;
err:
if (fd != -1)
close(fd);
unlink(_PATH_RESCONF_NEW);
+ out:
+ for (i = 0; i < iovcnt; i++)
+ free(iov[i].iov_base);
+
}
int