init: Open /etc/motd with O_CLOEXEC to avoid leaking this file to children

This commit is contained in:
apio 2022-10-28 17:13:45 +02:00
parent c312d81de4
commit 77d331b258

View File

@ -1,4 +1,5 @@
#include <errno.h>
#include <fcntl.h>
#include <luna.h>
#include <stdio.h>
#include <sys/wait.h>
@ -6,10 +7,16 @@
void show_motd()
{
FILE* fp = fopen("/etc/motd", "r");
int fd = open("/etc/motd", O_RDONLY | O_CLOEXEC);
if (fd < 0)
{
if (errno != ENOENT) { perror("open"); }
return;
}
FILE* fp = fdopen(fd, "r");
if (!fp)
{
if (errno != ENOENT) { perror("fopen"); }
perror("fopen");
return;
}