Skip to content

Commit bf4cca5

Browse files
committed
Minor fixes to init/init.c
o Make sure we print errors and warnings to stderr. o Don't assume fork succeeds. o Use err(3) and warn(3), cuts some lines.
1 parent 587aa1f commit bf4cca5

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

init/init.c

+29-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <limits.h>
2+
#include <err.h>
23
#include <errno.h>
34
#include <fcntl.h>
45
#include <unistd.h>
@@ -29,7 +30,7 @@ void set_rlimits(const char *rlimits)
2930

3031
lim_id = strtoull(item, &item, 10);
3132
if (lim_id == ULLONG_MAX) {
32-
printf("Invalid rlimit ID\n");
33+
fprintf(stderr, "Invalid rlimit ID\n");
3334
break;
3435
}
3536

@@ -41,7 +42,7 @@ void set_rlimits(const char *rlimits)
4142
rlim.rlim_cur = lim_cur;
4243
rlim.rlim_max = lim_max;
4344
if (setrlimit(lim_id, &rlim) != 0) {
44-
printf("Error setting rlimit for ID=%lld\n", lim_id);
45+
fprintf(stderr, "Can't set rlimit for ID=%llu\n", lim_id);
4546
}
4647

4748
if (*item != '\0') {
@@ -78,76 +79,60 @@ int main(int argc, char **argv)
7879
pipe(pipefd);
7980

8081
pid = fork();
81-
if (pid == 0) {
82+
if (pid == -1)
83+
err(-1, "fork");
84+
else if (pid == 0) {
8285
close(pipefd[1]);
8386
dup2(pipefd[0], 0);
8487
close(pipefd[0]);
8588

86-
if (execl("/sbin/cryptsetup", "cryptsetup", "open", "/dev/vda", "luksroot", "-", NULL) < 0) {
87-
perror("execl");
88-
exit(-1);
89-
}
89+
if (execl("/sbin/cryptsetup", "cryptsetup", "open", "/dev/vda",
90+
"luksroot", "-", NULL) < 0)
91+
err(-1, "execl");
9092
} else {
91-
write(pipefd[1], passp, strnlen(passp, 128));
93+
if (write(pipefd[1], passp, strnlen(passp, 128)) < 0)
94+
warn("write"); // XXX - ignores short count
9295
close(pipefd[1]);
9396
waitpid(pid, &wstatus, 0);
9497
}
9598

9699
printf("Mounting LUKS root filesystem\n");
97100

98-
if (mount("/dev/mapper/luksroot", "/luksroot", "ext4", 0, NULL) < 0) {
99-
perror("mount(/luksroot)");
100-
exit(-1);
101-
}
101+
if (mount("/dev/mapper/luksroot", "/luksroot", "ext4", 0, NULL) < 0)
102+
err(-1, "mount(/luksroot)");
102103

103104
chdir("/luksroot");
104105

105-
if (mount(".", "/", NULL, MS_MOVE, NULL)) {
106-
perror("remount root");
107-
exit(-1);
108-
}
106+
if (mount(".", "/", NULL, MS_MOVE, NULL))
107+
err(-1, "remount root");
109108
chroot(".");
110109
}
111110

112111
if (mount("proc", "/proc", "proc",
113-
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
114-
perror("mount(/proc)");
115-
exit(-1);
116-
}
112+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0)
113+
err(-1, "mount(/proc)");
117114

118115
if (mount("sysfs", "/sys", "sysfs",
119-
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
120-
perror("mount(/sys)");
121-
exit(-1);
122-
}
116+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0)
117+
err(-1, "mount(/sys)");
123118

124119
if (mount("cgroup2", "/sys/fs/cgroup", "cgroup2",
125-
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
126-
perror("mount(/sys/fs/cgroup)");
127-
exit(-1);
128-
}
120+
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0)
121+
err(-1, "mount(/sys/fs/cgroup)");
129122

130-
if (mkdir("/dev/pts", 0755) < 0 && errno != EEXIST) {
131-
perror("mkdir(/dev/pts)");
132-
exit(-1);
133-
}
123+
if (mkdir("/dev/pts", 0755) < 0 && errno != EEXIST)
124+
err(-1, "mkdir(/dev/pts)");
134125

135126
if (mount("devpts", "/dev/pts", "devpts",
136-
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
137-
perror("mount(/dev/pts)");
138-
exit(-1);
139-
}
127+
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0)
128+
err(-1, "mount(/dev/pts)");
140129

141-
if (mkdir("/dev/shm", 0755) < 0 && errno != EEXIST) {
142-
perror("mkdir(/dev/shm)");
143-
exit(-1);
144-
}
130+
if (mkdir("/dev/shm", 0755) < 0 && errno != EEXIST)
131+
err(-1, "mkdir(/dev/shm)");
145132

146133
if (mount("tmpfs", "/dev/shm", "tmpfs",
147-
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
148-
perror("mount(/dev/shm)");
149-
exit(-1);
150-
}
134+
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0)
135+
err(-1, "mount(/dev/shm)");
151136

152137
/* May fail if already exists and that's fine. */
153138
symlink("/proc/self/fd", "/dev/fd");

0 commit comments

Comments
 (0)