1
1
#include <limits.h>
2
+ #include <err.h>
2
3
#include <errno.h>
3
4
#include <fcntl.h>
4
5
#include <unistd.h>
@@ -29,7 +30,7 @@ void set_rlimits(const char *rlimits)
29
30
30
31
lim_id = strtoull (item , & item , 10 );
31
32
if (lim_id == ULLONG_MAX ) {
32
- printf ( "Invalid rlimit ID\n" );
33
+ fprintf ( stderr , "Invalid rlimit ID\n" );
33
34
break ;
34
35
}
35
36
@@ -41,7 +42,7 @@ void set_rlimits(const char *rlimits)
41
42
rlim .rlim_cur = lim_cur ;
42
43
rlim .rlim_max = lim_max ;
43
44
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 );
45
46
}
46
47
47
48
if (* item != '\0' ) {
@@ -78,76 +79,60 @@ int main(int argc, char **argv)
78
79
pipe (pipefd );
79
80
80
81
pid = fork ();
81
- if (pid == 0 ) {
82
+ if (pid == -1 )
83
+ err (-1 , "fork" );
84
+ else if (pid == 0 ) {
82
85
close (pipefd [1 ]);
83
86
dup2 (pipefd [0 ], 0 );
84
87
close (pipefd [0 ]);
85
88
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" );
90
92
} 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
92
95
close (pipefd [1 ]);
93
96
waitpid (pid , & wstatus , 0 );
94
97
}
95
98
96
99
printf ("Mounting LUKS root filesystem\n" );
97
100
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)" );
102
103
103
104
chdir ("/luksroot" );
104
105
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" );
109
108
chroot ("." );
110
109
}
111
110
112
111
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)" );
117
114
118
115
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)" );
123
118
124
119
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)" );
129
122
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)" );
134
125
135
126
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)" );
140
129
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)" );
145
132
146
133
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)" );
151
136
152
137
/* May fail if already exists and that's fine. */
153
138
symlink ("/proc/self/fd" , "/dev/fd" );
0 commit comments