Skip to content

Commit 1a5454f

Browse files
wtarreaupaulmckrcu
authored andcommitted
selftests/nolibc: recreate and populate /dev and /proc if missing
Most of the time the program will be run alone in an initramfs. There is no value in requiring the user to populate /dev and /proc for such tests, we can do it ourselves, and it participates to the tests at the same time. What's done here is that when called as init (getpid()==1) we check if /dev exists or create it, if /dev/console and /dev/null exists, otherwise we try to mount a devtmpfs there, and if it fails we fall back to mknod. The console is reopened if stdout was closed. Finally /proc is created and mounted if /proc/self cannot be found. This is sufficient for most tests. Signed-off-by: Willy Tarreau <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent aa73a86 commit 1a5454f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tools/testing/selftests/nolibc/nolibc-test.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,54 @@ int run_stdlib(int min, int max)
530530
return ret;
531531
}
532532

533+
/* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
534+
int prepare(void)
535+
{
536+
struct stat stat_buf;
537+
538+
/* It's possible that /dev doesn't even exist or was not mounted, so
539+
* we'll try to create it, mount it, or create minimal entries into it.
540+
* We want at least /dev/null and /dev/console.
541+
*/
542+
if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
543+
if (stat("/dev/console", &stat_buf) != 0 ||
544+
stat("/dev/null", &stat_buf) != 0) {
545+
/* try devtmpfs first, otherwise fall back to manual creation */
546+
if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
547+
mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
548+
mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3));
549+
}
550+
}
551+
}
552+
553+
/* If no /dev/console was found before calling init, stdio is closed so
554+
* we need to reopen it from /dev/console. If it failed above, it will
555+
* still fail here and we cannot emit a message anyway.
556+
*/
557+
if (close(dup(1)) == -1) {
558+
int fd = open("/dev/console", O_RDWR);
559+
560+
if (fd >= 0) {
561+
if (fd != 0)
562+
dup2(fd, 0);
563+
if (fd != 1)
564+
dup2(fd, 1);
565+
if (fd != 2)
566+
dup2(fd, 2);
567+
if (fd > 2)
568+
close(fd);
569+
puts("\nSuccessfully reopened /dev/console.");
570+
}
571+
}
572+
573+
/* try to mount /proc if not mounted. Silently fail otherwise */
574+
if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
575+
if (stat("/proc/self", &stat_buf) != 0)
576+
mount("/proc", "/proc", "proc", 0, 0);
577+
}
578+
579+
return 0;
580+
}
533581

534582
/* This is the definition of known test names, with their functions */
535583
static struct test test_names[] = {
@@ -550,6 +598,14 @@ int main(int argc, char **argv, char **envp)
550598

551599
environ = envp;
552600

601+
/* when called as init, it's possible that no console was opened, for
602+
* example if no /dev file system was provided. We'll check that fd#1
603+
* was opened, and if not we'll attempt to create and open /dev/console
604+
* and /dev/null that we'll use for later tests.
605+
*/
606+
if (getpid() == 1)
607+
prepare();
608+
553609
/* the definition of a series of tests comes from either argv[1] or the
554610
* "NOLIBC_TEST" environment variable. It's made of a comma-delimited
555611
* series of test names and optional ranges:

0 commit comments

Comments
 (0)